From patchwork Mon Sep 11 13:50:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 721460 Delivered-To: patch@linaro.org Received: by 2002:adf:f64d:0:b0:31d:da82:a3b4 with SMTP id x13csp1110395wrp; Mon, 11 Sep 2023 06:50:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGntZ5j7u02whLvbOHLk3+yHiRCrjAQwzjoSSBu2BBnT4xG98oLWvZQQSyrQN4HUEJ33LvR X-Received: by 2002:a19:5514:0:b0:500:8257:4b34 with SMTP id n20-20020a195514000000b0050082574b34mr6507899lfe.68.1694440245881; Mon, 11 Sep 2023 06:50:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694440245; cv=none; d=google.com; s=arc-20160816; b=DHKxiULZ3BWf07TlpfCHwvY6WJfYesgXYN9lqWTeQe7Nu/R1gBC3fAb25v04jlUlma 98fPB7Dx96kvreNE90VbC6oX4tcUpiRuwuSy4KqNMOlK2lpKgEu+/N9+Pr7frAGyxaRn +4RYwnzVGlZum9fhVRFzMEDi4o0sqP4aJtIAQgKLKE1Wr3Z891TAEVNpUlSZSAe+BW0K PvksrZvX4vF2hxhVM1fUGuYLA8B8KsWB/zdHtA5sfABKiXXlJGaiIkc8mHfKmqMXcPnr 3BOuGS218Jzqp40QjCcjDZfnX4O8LtxFuOFSdth82x7AQkMpRstys8JjqUrLfnxtCvxQ +AzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dmarc-filter:delivered-to; bh=sdr42wBcyMdV7kIiTns1svwk/p8zrFOBf3ER77yXvYU=; fh=Ji/BrrBV5vvGE0PSJoc8P6vA3gQfM/DANnlOBrK0hiY=; b=hkiRMSYoNNrfZCw+p1coZODOGA1uSI4PY3nRMVNQ7zZ+FZiqiXksGXC6L/j1/mFldU IUiUvzhu825nu/dswWPqLberL/obLsTySNMeTUY0ibAHk4Qw9iVsMD9uNvwNRsmKSUWQ +gyRdKN+E1sNWHHzNX7iwVxV7yEI05UpmdWgmpDVnWJyHv1ksK8dL4iLUEt1qMj+Op1b 2ujdnflJUCGERi+uEJyaKMUCHLrMSHxJDiFJtnojwVXFsKHwbcCFEvyuMWVAYZda9DXh o3YHbEPVidxfLRNi77EgOFNBofqmOCERjdcupdaZz7vehJF0eI8WpwE9Q+X8ITaY2oJv JCHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=en2yiUjR; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m13-20020a1709060d8d00b00991f5560a1dsi6245287eji.32.2023.09.11.06.50.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 06:50:45 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=en2yiUjR; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9747E3854806 for ; Mon, 11 Sep 2023 13:50:44 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2f.google.com (mail-oa1-x2f.google.com [IPv6:2001:4860:4864:20::2f]) by sourceware.org (Postfix) with ESMTPS id 51FD33857725 for ; Mon, 11 Sep 2023 13:50:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 51FD33857725 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-1c8d895a602so2627731fac.2 for ; Mon, 11 Sep 2023 06:50:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694440215; x=1695045015; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=sdr42wBcyMdV7kIiTns1svwk/p8zrFOBf3ER77yXvYU=; b=en2yiUjRRGAR1E1pZq2qvg5+d640QFQzb8yhEu4fvL7ZI0iNKwDssEkujnr2F0qEOZ oZWcQCfWqbzZlGVsq0NjPtGMg5UIeHFIAcUYNDiDELboVUD9+hutJxlWlVCZ8xp+dBug sLl+hYwQ+A6GJbAnHo53g5tCGn9DjUiOZOyygJnfekHRV/auddyOijPf/C4ZPheDPEcK SVb3QSiIlfYzI/OArSq+Y0CooQDWtPLgSJtYYW4gZ+H2WMwxCdwJbUUQDJSHdXvDVl2I o7q3RqdBGnc26CFUl5RGLC+E6YfEhsDOKstUjOk7iRmfANe8RhbzJslYpaW4dtP5wFsx qQLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694440215; x=1695045015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sdr42wBcyMdV7kIiTns1svwk/p8zrFOBf3ER77yXvYU=; b=b/QivDi63eushONiH3IY3fCHCjy+0YTH8H2Vd1LC0B/kfFMgwSXSa1qXcxPiSZBmKd u61fNPV8pfSKdm1VgQ+ppfAxab9L6WkFy7O6cMqzhINsdDzkN4THVUwNynK7THK40x9s j2N3ZlfmjvN9cLfwavKG+7zVb1rjS+ZD5YkrWWDLaDuCRIJpF8RnRSEv4IO/LQD2GJBQ QIE9yHcFn1OlOnOFGNQAN2AxkcYzDkk64OLTiWwTrO7XWgXj3t+dqw7+XIJ3ghti5wD9 spNKvoAgLVYyTuktlysPnRMsWEPauMO5ms7yuSZTOSgBBkZSpqJ9O3qT/TXOe26GrkCb 7XHg== X-Gm-Message-State: AOJu0YyiXkrFNjMnm4/2KrBAqjLd0TZgWA7X6pCDm4SvT3bequDv65ub wujquVftet8AuROxnIfncYSK1DXCnkKCWLxkdiY2sQ== X-Received: by 2002:a05:6870:239f:b0:1ba:64a5:d2d1 with SMTP id e31-20020a056870239f00b001ba64a5d2d1mr10320295oap.20.1694440215136; Mon, 11 Sep 2023 06:50:15 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:8dba:c245:9a76:bfb3]) by smtp.gmail.com with ESMTPSA id w3-20020a056870a2c300b001c4d922b4fasm4025505oak.8.2023.09.11.06.50.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 06:50:14 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Joe Simmons-Talbott Subject: [PATCH v2 2/2] stdio: Remove __printf_fp_buffer_2 alloca usage Date: Mon, 11 Sep 2023 10:50:06 -0300 Message-Id: <20230911135006.3390666-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230911135006.3390666-1-adhemerval.zanella@linaro.org> References: <20230911135006.3390666-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.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 . */ -/* The gmp headers need some configuration frobs. */ -#define HAVE_ALLOCA 1 - #include #include -#include #include #include #include @@ -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) \