From nobody Fri Aug 7 01:48:19 2026 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4hGRr41b1gz6nf3D for ; Fri, 07 Aug 2026 01:48:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hGRr406hVz3pcH for ; Fri, 07 Aug 2026 01:48:20 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786067300; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XdH7ci1RCtvMlpTIjfXWyL8ryEYBVFJNyxg/ftswvNo=; b=bZ2L8JfIMQOnYeV6/OjAiFUtb5EXNBXCjRpMS0M20Xmi5PJ/ErFFcwyFVcEXL5lHlITz+V cCgdI8UHij2obXEeZblWH0xwyyVVqoT84+CYWsUTVyfS1PwxODsjsS9xfGRsFTDN/Obra1 yehcyErTmxMN7mZvzkeV+xbpTm0+6eH2J1jXSNNJ9KbkP7ytgLWnHeEoErrkR0YGCN1chh 0hyk4BaJeX3Lw/SsCce3w0z1RLr9++lDUuT2GLPnHPixHC7dNIawsh/5r/jbHtMmX92KTG d/L65uiDY8u5bjuFumE+KxIHGGB1uS1kKuNpp5UvBL3QFH37e1op0bZ1jAWRwA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1786067300; a=rsa-sha256; cv=none; b=xxonDSsxK6WHKNw04nnbGi1Z6vkk05MIPZuRSM3r3wJg8H7qioU31Q40YzaZWRti1eZlGw 9UJ65G3E09M1swgjhCM/oZ61CHBvVsz+AZwek2eN0B2z/WzgfTdy1excQ1Fb6yACtGo9dB w2m/nnhz+MFCRav2xoqJmsFOysU/GG5j2PddLR2KiAC+h1nUmHAQF66aq8ynKHqFIOkFzm +nHHQ2LNHHC/W7v4ezXidnXDDGxSOOnMEqs3pDNjqjiCIk1zoy2E+t9XUG4OyhVOa58L+u B50GtzJwIrzXsmgzSmbQwrX0N4OW96BDRCmTilS8SGvT8+TFJvDPmih66Bz4sQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786067300; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=XdH7ci1RCtvMlpTIjfXWyL8ryEYBVFJNyxg/ftswvNo=; b=pX3td49eSMLaeKwGOwQQNx8/WaisX+PT+SnL47AMjd9RA99W9FMXgdRf924d2RrJegUuOt 3DS2Bhc2Ju2WhBGWloyWfe4xAX0S1o9NDpt/yVkg98OWoIdYNG0M2bcZj719KJ1hvu0UN7 jHjp7n5oyq8AUPAkrPYhK8A7gutcwxov+Sw1QqtnGPz7P8I0SzYJvgBcYG9KwPRMXKtC1G qv1y9h8MmuJTsZ+VwRJBQE865sTUvSUdc90cK7f35lEEQQDwUAOCaxMDsw5yWNSbpqgAGB unAXjcxmD4lvKw2MjA3YVZVaN7xXHIC6Bvj5pIVU/cYz+A971dA2uhv6v/5sMA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hGRr360Dtzmxj for ; Fri, 07 Aug 2026 01:48:19 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3bba3 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 07 Aug 2026 01:48:19 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 3d29c3a3ec99 - stable/15 - stdio: *memstream: decouple the buffer size from the stream length List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: 3d29c3a3ec9951ab2b40b9f470f40b24f1f6cfa7 Auto-Submitted: auto-generated Date: Fri, 07 Aug 2026 01:48:19 +0000 Message-Id: <6a753963.3bba3.1a304c45@gitrepo.freebsd.org> The branch stable/15 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=3d29c3a3ec9951ab2b40b9f470f40b24f1f6cfa7 commit 3d29c3a3ec9951ab2b40b9f470f40b24f1f6cfa7 Author: Kyle Evans AuthorDate: 2026-08-01 03:34:37 +0000 Commit: Kyle Evans CommitDate: 2026-08-06 23:37:43 +0000 stdio: *memstream: decouple the buffer size from the stream length It's useful to be able to track both facts with a single variable, but it also makes it more difficult to change how the buffer size scales. As an example, Apple's implementation seems to scale the buffer size by 1.5x on growth, presumably in an attempt to reduce trips into realloc(). This might be questionable in the face of stdio buffering, but avoiding serious churn in the line- or un-buffered case is a net positive if doing so isn't incredibly invasive. Reviewed by: des, jhb, obiwac Sponsored by: Klara, Inc. (cherry picked from commit 781defc7eb061a82b1e3e8dbc6b34993f23e79a6) --- lib/libc/stdio/open_memstream.c | 13 +++++++++---- lib/libc/stdio/open_wmemstream.c | 12 ++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/libc/stdio/open_memstream.c b/lib/libc/stdio/open_memstream.c index 29de688376bf..d0a22bcfe59f 100644 --- a/lib/libc/stdio/open_memstream.c +++ b/lib/libc/stdio/open_memstream.c @@ -46,6 +46,7 @@ struct memstream { char **bufp; size_t *sizep; + size_t size; ssize_t len; fpos_t offset; }; @@ -60,19 +61,22 @@ memstream_grow(struct memstream *ms, fpos_t newoff) newsize = SSIZE_MAX - 1; else newsize = newoff; - if (newsize > ms->len) { + if (newsize > ms->size) { buf = realloc(*ms->bufp, newsize + 1); if (buf == NULL) return (0); #ifdef DEBUG fprintf(stderr, "MS: %p growing from %zd to %zd\n", - ms, ms->len, newsize); + ms, ms->size, newsize); #endif - memset(buf + ms->len + 1, 0, newsize - ms->len); + memset(buf + ms->size + 1, 0, newsize - ms->size); *ms->bufp = buf; - ms->len = newsize; + ms->size = newsize; } + + if (newsize > ms->len) + ms->len = newsize; return (1); } @@ -192,6 +196,7 @@ open_memstream(char **bufp, size_t *sizep) } ms->bufp = bufp; ms->sizep = sizep; + ms->size = 0; ms->len = 0; ms->offset = 0; memstream_update(ms); diff --git a/lib/libc/stdio/open_wmemstream.c b/lib/libc/stdio/open_wmemstream.c index 0bed4cff5d11..d1844e573b1b 100644 --- a/lib/libc/stdio/open_wmemstream.c +++ b/lib/libc/stdio/open_wmemstream.c @@ -46,6 +46,7 @@ struct wmemstream { wchar_t **bufp; size_t *sizep; + size_t size; ssize_t len; fpos_t offset; mbstate_t mbstate; @@ -61,18 +62,20 @@ wmemstream_grow(struct wmemstream *ms, fpos_t newoff) newsize = SSIZE_MAX / sizeof(wchar_t) - 1; else newsize = newoff; - if (newsize > ms->len) { + if (newsize > ms->size) { buf = reallocarray(*ms->bufp, newsize + 1, sizeof(wchar_t)); if (buf == NULL) return (0); #ifdef DEBUG fprintf(stderr, "WMS: %p growing from %zd to %zd\n", - ms, ms->len, newsize); + ms, ms->size, newsize); #endif - wmemset(buf + ms->len + 1, 0, newsize - ms->len); + wmemset(buf + ms->size + 1, 0, newsize - ms->size); *ms->bufp = buf; - ms->len = newsize; + ms->size = newsize; } + if (newsize > ms->len) + ms->len = newsize; return (1); } @@ -252,6 +255,7 @@ open_wmemstream(wchar_t **bufp, size_t *sizep) } ms->bufp = bufp; ms->sizep = sizep; + ms->size = 0; ms->len = 0; ms->offset = 0; memset(&ms->mbstate, 0, sizeof(mbstate_t));