public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org, Joe Simmons-Talbott <josimmon@redhat.com>
Subject: [PATCH v2 2/2] stdio: Remove __printf_fp_buffer_2 alloca usage
Date: Mon, 11 Sep 2023 10:50:06 -0300	[thread overview]
Message-ID: <20230911135006.3390666-3-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20230911135006.3390666-1-adhemerval.zanella@linaro.org>

And replace with a scratch_buffer.
---
 stdio-common/printf_fp.c | 36 ++++++++++--------------------------
 1 file changed, 10 insertions(+), 26 deletions(-)

diff --git a/stdio-common/printf_fp.c b/stdio-common/printf_fp.c
index 9ddf61fd6d..dd387393f0 100644
--- a/stdio-common/printf_fp.c
+++ b/stdio-common/printf_fp.c
@@ -17,12 +17,8 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-/* The gmp headers need some configuration frobs.  */
-#define HAVE_ALLOCA 1
-
 #include <array_length.h>
 #include <libioP.h>
-#include <alloca.h>
 #include <ctype.h>
 #include <float.h>
 #include <gmp-mparam.h>
@@ -168,11 +164,6 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf,
   /* General helper (carry limb).  */
   mp_limb_t cy;
 
-  /* Buffer in which we produce the output.  */
-  char *wbuffer = NULL;
-  /* Flag whether wbuffer and buffer are malloc'ed or not.  */
-  int buffer_malloced = 0;
-
   p->expsign = 0;
 
   if (p->special)
@@ -600,6 +591,9 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf,
     int dig_max;
     int significant;
     char spec = _tolower (info->spec);
+    struct scratch_buffer wscbuf;
+
+    scratch_buffer_init (&wscbuf);
 
     if (spec == 'e')
       {
@@ -662,7 +656,7 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf,
     /* Allocate buffer for output.  We need two more because while rounding
        it is possible that we need two more characters in front of all the
        other output.  If the amount of memory we have to allocate is too
-       large use `malloc' instead of `alloca'.  */
+       large use `malloc' instead of a static buffer.  */
     if (__glibc_unlikely (chars_needed >= (size_t) -1 - 2
 			  || chars_needed < fracdig_max))
       {
@@ -671,21 +665,12 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf,
 	__printf_buffer_mark_failed (buf);
 	return;
       }
-    size_t wbuffer_to_alloc = 2 + chars_needed;
-    buffer_malloced = ! __libc_use_alloca (wbuffer_to_alloc);
-    if (__builtin_expect (buffer_malloced, 0))
+    if (!scratch_buffer_set_array_size (&wscbuf, 1, 2 + chars_needed))
       {
-	wbuffer = malloc (wbuffer_to_alloc);
-	if (wbuffer == NULL)
-	  {
-	    /* Signal an error to the caller.  */
-	    __printf_buffer_mark_failed (buf);
-	    return;
-	  }
+	__printf_buffer_mark_failed (buf);
+	return;
       }
-    else
-      wbuffer = alloca (wbuffer_to_alloc);
-    wcp = wstartp = wbuffer + 2;	/* Let room for rounding.  */
+    wcp = wstartp = wscbuf.data + 2;	/* Let room for rounding.  */
 
     /* Do the real work: put digits in allocated buffer.  */
     if (p->expsign == 0 || p->type != 'f')
@@ -846,6 +831,8 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf,
 		  }
 	      }
 	  }
+
+	scratch_buffer_free (&wscbuf);
       }
 
     /* Now remove unnecessary '0' at the end of the string.  */
@@ -952,9 +939,6 @@ __printf_fp_buffer_2 (struct hack_digit_param *p, struct __printf_buffer *buf,
     if (info->left)
       __printf_buffer_pad (buf, info->pad, width);
   }
-
-  if (buffer_malloced)
-    free (wbuffer);
 }
 
 #define PRINTF_FP_FETCH(P, FLOAT, SUFFIX, MANT_DIG)			\
-- 
2.34.1


  parent reply	other threads:[~2023-09-11 13:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-11 13:50 [PATCH v2 0/2] Remove print_fp " Adhemerval Zanella
2023-09-11 13:50 ` [PATCH v2 1/2] stdio: Remove __printf_fp_buffer_1 " Adhemerval Zanella
2023-09-11 13:50 ` Adhemerval Zanella [this message]
2023-09-14 14:06 [PATCH v2 0/2] Remove print_fp " Adhemerval Zanella
2023-09-14 14:06 ` [PATCH v2 2/2] stdio: Remove __printf_fp_buffer_2 " Adhemerval Zanella

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=20230911135006.3390666-3-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=josimmon@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).