public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Florian Weimer <fweimer@redhat.com>, libc-alpha@sourceware.org
Subject: Re: [PATCH 07/11] libio: Extract __printf_buffer_asprintf_init from asprintf implementation
Date: Fri, 16 Feb 2024 11:04:52 -0300	[thread overview]
Message-ID: <8c10b0c8-56f7-4645-9331-d79b4eaa97b9@linaro.org> (raw)
In-Reply-To: <345c3f94450a38be9408f158a211c82a657034b1.1707491940.git.fweimer@redhat.com>



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  <adhemerval.zanella@linaro.org>

> ---
>  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 <string.h>
>  #include <printf_buffer.h>
>  
> -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.  */

  reply	other threads:[~2024-02-16 14:04 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-09 15:24 [PATCH 00/11] Build getdomainname, gethostname, syslog with fortification Florian Weimer
2024-02-09 15:24 ` [PATCH 01/11] misc: Build getdomainname " Florian Weimer
2024-02-12 17:14   ` Adhemerval Zanella Netto
2024-02-12 17:30     ` Andreas Schwab
2024-02-12 19:29       ` Florian Weimer
2024-02-13  9:12         ` Andreas Schwab
2024-02-13  9:23           ` Florian Weimer
2024-02-13  9:32             ` Andreas Schwab
2024-02-09 15:24 ` [PATCH 02/11] misc: Build gethostname " Florian Weimer
2024-02-12 17:25   ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 03/11] libio: Add fortify wrapper for internal __snprintf Florian Weimer
2024-02-12 17:34   ` Adhemerval Zanella Netto
2024-02-13 12:13     ` Florian Weimer
2024-02-13 13:16       ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 04/11] syslog: Update misc/tst-syslog to check reported %n value Florian Weimer
2024-02-13 11:59   ` Adhemerval Zanella Netto
2024-02-15 13:23     ` Florian Weimer
2024-02-09 15:25 ` [PATCH 05/11] syslog: Build with fortification Florian Weimer
2024-02-13 12:26   ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 06/11] stdio: Rename __printf_buffer to __vfprintf_buffer Florian Weimer
2024-02-16 13:40   ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 07/11] libio: Extract __printf_buffer_asprintf_init from asprintf implementation Florian Weimer
2024-02-16 14:04   ` Adhemerval Zanella Netto [this message]
2024-02-09 15:25 ` [PATCH 08/11] stdio-common: Introduce the __printf_buffer function Florian Weimer
2024-02-16 14:12   ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 09/11] stdio-common: Allow skipping initial bytes in __printf_buffer for %n Florian Weimer
2024-02-16 14:13   ` Adhemerval Zanella Netto
2024-02-09 15:25 ` [PATCH 10/11] stdio-common: Support large offsets with %lln Florian Weimer
2024-02-16 14:20   ` Adhemerval Zanella Netto
2024-02-09 15:26 ` [PATCH 11/11] syslog: Use a printf buffer directly to construct the entire packet Florian Weimer
2024-02-14 15:16   ` Adhemerval Zanella Netto
2024-02-15 13:02     ` Florian Weimer
2024-02-16 13:35       ` Adhemerval Zanella Netto
2024-02-16 15:58   ` Adhemerval Zanella Netto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8c10b0c8-56f7-4645-9331-d79b4eaa97b9@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).