From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by sourceware.org (Postfix) with ESMTPS id 0C795385829E for ; Fri, 16 Feb 2024 14:04:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C795385829E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0C795385829E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c32 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708092305; cv=none; b=i9WXJFPOsPrkF+7irNuf+D90vL8xDA+ro4pi1HoiL19i4pzlleSEwyFyAncDfXbND4Z1Ut3+/SqvttxHRHA0O7abx+ozVn42yW/JE9VCfKnQ/TosHs4eHMhbhe8RQ179WHmfPPV943g/WGpyjZpCSA7GGQJaXt80G+nHB1igLMM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708092305; c=relaxed/simple; bh=bj7cE9lFAQSFpwMz/kn+Ek9bbgO7jPWDdSarkF7quPQ=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=SOTGFsLn/2VrR5tLi7Jro6PvnRK0sPdKG7BoUhb5fr79TNB93ojk1ZsfWXsPI03s8YUzXTpNB1w7iyXBmO0i+pN/Crw+GNzT/kPP25Y2CwOn+QQM0nOpUPZys5QPym9u0JyPcnbYwQPebd0aiJma5dsU+x2tZauspczLGZGOUfI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc32.google.com with SMTP id 006d021491bc7-59a29a93f38so108713eaf.0 for ; Fri, 16 Feb 2024 06:04:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708092296; x=1708697096; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=H9UYAaiI5A3J6/MaeHckyX79CY2DJaAKd7CEY7NkXjI=; b=hKVZWsgsME6VEAf9VLi9i0mNAuRbstFN0eXo6Gejzck2LkfQBpRlnsIx3cMl5P00cG gPxZ4JDedziR5/1zs5i/UNJjLeveT+Mnf8oeaI7TVsJ8y/IfLXfZvtvy5WyYduy098t1 WB5ziIf3d9eoyB7F918I8sjEHQ73DgUamMIFibKMhhFb5qoyP3whqZYpSvcxYX+Rtd70 MuAvo3Ld5j2scrKMwKIOs5bTkSJcZhmbffh5sr8zjAZaB4QQkJ3wheXMmrdWI3vuVOUa bL4BAt7pGK28K5x1Ned8RrxbEeSBlZ1zpgIgCiYlt9wSPA0je+ZWas1PY0+PtdB8R5PQ FcSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708092296; x=1708697096; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=H9UYAaiI5A3J6/MaeHckyX79CY2DJaAKd7CEY7NkXjI=; b=ZoSmLpzjetyEh25viDsx0Vb023RzAkQui3+OBfakssSGppNuKhXLKDtoL94urayaKI CiP0/Mn++3wfXBkkYNbx6Ja2S7ExTnz7yFHQt3REn9fBBpwIJxYtckuSekOBxygYLLod RwqM6PvUZiluJXPUVLd5f+hWeub//C7XVLjovnCwoXhs27pV6j4AWFCtxPFZOAgUkbGa C0PrLoN/oVSfF4RsOHIvXc4GXpHtA7GYrJKvXpAFbbzAJ2l90iafdxzM2CiSa2PXBp2o FGqiVe7xVTD6IjmbphV+wQ6t+78cHoRe1MK6o/87pj4BVmnLX91pOaNS9ak5Yy4alwPE 86Gg== X-Forwarded-Encrypted: i=1; AJvYcCVfwssOY2+1ZOsgqqzcTe90HOgkMNsYbUs6ue+B2L4ImcASgjQXi/i5zFvAMPKb+90yy7Y0512H5VS98WncAL3seTpW6fIvL3eX X-Gm-Message-State: AOJu0YzaiJTgRLqT2EgM9Uz+0JKZtyU42GpVX6l2L6T2czj6WSh2SWHk 6vtZSW4g6COCz2+NXPRmfyTNi3dFBSr3uXssYPLcCGdidbCL1QOiwwdiI9G3hi/GOA32TCN7ndE V X-Google-Smtp-Source: AGHT+IH3P38fQpsD5HEtNHdXh5vACLSLWYQHV71ieNItCf21dDozLAsMayhRMAV8jDwmHYmO4kuCQg== X-Received: by 2002:a05:6358:5925:b0:17a:b810:aacf with SMTP id g37-20020a056358592500b0017ab810aacfmr5333677rwf.18.1708092296067; Fri, 16 Feb 2024 06:04:56 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c0:8177:542a:9f26:15a9:5ce4? ([2804:1b3:a7c0:8177:542a:9f26:15a9:5ce4]) by smtp.gmail.com with ESMTPSA id k28-20020a635a5c000000b005dcc075d5edsm1884215pgm.60.2024.02.16.06.04.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Feb 2024 06:04:54 -0800 (PST) Message-ID: <8c10b0c8-56f7-4645-9331-d79b4eaa97b9@linaro.org> Date: Fri, 16 Feb 2024 11:04:52 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 07/11] libio: Extract __printf_buffer_asprintf_init from asprintf implementation Content-Language: en-US To: Florian Weimer , libc-alpha@sourceware.org References: <345c3f94450a38be9408f158a211c82a657034b1.1707491940.git.fweimer@redhat.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <345c3f94450a38be9408f158a211c82a657034b1.1707491940.git.fweimer@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 09/02/24 12:25, Florian Weimer wrote: > And __printf_buffer_asprintf_free as well. This will allow to reuse > the asprintf implementation for more general buffer handling, similar > to open_memstream, but without the mandatory heap allocation. LGTM, thanks. Reviewed-by: Adhemerval Zanella > --- > include/printf_buffer.h | 30 +++++++++++++++++++++++++----- > libio/vasprintf.c | 40 +++++++++++++++++----------------------- > 2 files changed, 42 insertions(+), 28 deletions(-) > > diff --git a/include/printf_buffer.h b/include/printf_buffer.h > index fb0b42178e..cee5afb581 100644 > --- a/include/printf_buffer.h > +++ b/include/printf_buffer.h > @@ -317,6 +317,31 @@ void __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *, > int __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *) > attribute_hidden; > > +/* Size of the initial on-stack buffer for asprintf. It should be > + large enough to copy almost all asprintf usages with just a single > + (final, correctly sized) heap allocation. */ > +#define PRINTF_BUFFER_SIZE_ASPRINTF 200 On my system the journactl message has a mean size of 124 and a median of so I guess a size of 200 should cover most of the usage. > + > +struct __printf_buffer_asprintf > +{ > + /* base.write_base points either to a heap-allocated buffer, or to > + the direct array below. */ > + struct __printf_buffer base; > + > + /* Initial allocation. */ > + char direct[PRINTF_BUFFER_SIZE_ASPRINTF]; > +}; > + > +/* Sets up BUF for writing via __printf_buffer. */ > +void __printf_buffer_asprintf_init (struct __printf_buffer_asprintf *buf) > + attribute_hidden; > + > +/* Deallocates any allocated memory in *BUF. (This is not the usual > + done routine for asprintf because that has to preserve allocation.) > + Always returns -1 to indicate an error. */ > +int __printf_buffer_asprintf_free (struct __printf_buffer_asprintf *buf) > + attribute_hidden; > + > /* Flush function implementations follow. They are called from > __printf_buffer_flush. Generic code should not call these flush > functions directly. Some modes have inline implementations. */ > @@ -363,11 +388,6 @@ void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *) > /* Temporary buffer used during floating point digit translation. */ > #define PRINTF_BUFFER_SIZE_DIGITS 64 > > -/* Size of the initial on-stack buffer for asprintf. It should be > - large enough to copy almost all asprintf usages with just a single > - (final, correctly sized) heap allocation. */ > -#define PRINTF_BUFFER_SIZE_ASPRINTF 200 > - > /* This should cover most of the packet-oriented file descriptors, > where boundaries between writes could be visible to readers. But > it is still small enough not to cause too many stack overflow issues. */ > diff --git a/libio/vasprintf.c b/libio/vasprintf.c > index edcfab2323..f8413eedfe 100644 > --- a/libio/vasprintf.c > +++ b/libio/vasprintf.c > @@ -34,18 +34,6 @@ > #include > #include > > -struct __printf_buffer_asprintf > -{ > - /* base.write_base points either to a heap-allocated buffer, or to > - the direct array below. */ > - struct __printf_buffer base; > - > - /* Initial allocation. 200 should be large enough to copy almost > - all asprintf usages with just a single (final, correctly sized) > - heap allocation. */ > - char direct[PRINTF_BUFFER_SIZE_ASPRINTF]; > -}; > - > void > __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf) > { > @@ -90,23 +78,32 @@ __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf) > buf->base.write_end = new_buffer + new_size; > } > > +void > +__printf_buffer_asprintf_init (struct __printf_buffer_asprintf *buf) > +{ > + __printf_buffer_init (&buf->base, buf->direct, array_length (buf->direct), > + __printf_buffer_mode_asprintf); > +} > + > +int > +__printf_buffer_asprintf_free (struct __printf_buffer_asprintf *buf) > +{ > + if (buf->base.write_base != buf->direct) > + free (buf->base.write_base); > + return -1; > +} > > int > __vasprintf_internal (char **result_ptr, const char *format, va_list args, > unsigned int mode_flags) > { > struct __printf_buffer_asprintf buf; > - __printf_buffer_init (&buf.base, buf.direct, array_length (buf.direct), > - __printf_buffer_mode_asprintf); > + __printf_buffer_asprintf_init (&buf); > > __vprintf_buffer (&buf.base, format, args, mode_flags); > int done = __printf_buffer_done (&buf.base); > if (done < 0) > - { > - if (buf.base.write_base != buf.direct) > - free (buf.base.write_base); > - return done; > - } > + return __printf_buffer_asprintf_free (&buf); > > /* Transfer to the final buffer. */ > char *result; > @@ -122,10 +119,7 @@ __vasprintf_internal (char **result_ptr, const char *format, va_list args, > { > result = realloc (buf.base.write_base, size + 1); > if (result == NULL) > - { > - free (buf.base.write_base); > - return -1; > - } > + return __printf_buffer_asprintf_free (&buf); > } > > /* Add NUL termination. */