From nobody Sun Jun 7 21:00:59 2026 X-Original-To: dev-commits-src-main@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 4gYSJD34KLz6g54R for ; Sun, 07 Jun 2026 21:01:00 +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 "R13" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gYSJC5Ngzz3Spb for ; Sun, 07 Jun 2026 21:00:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1780866059; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KNWfD+O4oSU8Pww1MLQR5nzG0UYLovALqWo966dgRl0=; b=metKhJbT/1jtOkl/cLZ0RockXnI0LyGRKNhPOCdYDJOqeO9PHktXAFDHB8TPKFoD/yP/zV dOT6HJ8dnlzyzaUA5QhZqwojIYrH8SdGURArQXATiIdnDp4IUTOKL8yR/EUjGAnIh04q/8 dt9ZSJn7dOsSOq8sJFbw5+xyWA74N9s1eDXvAQJM7YezXT9M5aNsIKYeI4mgK+HMEuFXBa 5Zfgkk3QSKxzY88yv7J1w3Gw8XsZehzlquog11UIUQ1L2aNHa1D3SrG2EjXotR1faztPpk oBL85XrRoHHzaaTp1xxtrbjgV6ZAkwuK33975jJQseEdHB7JxzFwKxCXyra2YQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1780866059; a=rsa-sha256; cv=none; b=OVzDO+JQ97TVtx8EHWkRBPqcKjLSNUt/pnDWEI2b6Xt6mpmaoMYwGHBEcWCd5RtzAp5vwm l3SOJbnMIDXN5vlZkWhGZ+a+kXT+kAI0vgk271SdgvypszaXasB27dOYjuWYzmOU+R0EkQ 6c5wi0TOqRgykbGrWFPBFE43a8tBKAqpGyfRKuSufKUf75d6OKaP8veZLFXpn0ewqrTfk4 EisGN1TNCSaJ4X39qEDK0CGwcCs+YZ9Lkdbg26hZHujUsopLPSdHfBYYn6nTSWBbslElDi O07rwrXEKv7NwVRoCZDZx1NcK0hbNdbY8YRrHf6KDBq5n0hxlKGgnNdHGuyHmQ== 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=1780866059; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KNWfD+O4oSU8Pww1MLQR5nzG0UYLovALqWo966dgRl0=; b=c5SClNGFilMgS2kg0Gx1IxVylR78vtUnyUchftX0tRFni4psF0IvPOlMVMVU514aukB8xO 7Gq1SK60n2OmhytsYs1ZYNjgVyBiuN+sxgzXVmRa1d0F3jwgYN61UzLHAbCLwVdCS7NQz8 Zkqy4LTZmI2Vy4BphyZJ8xYIdSGNJqgQjTQt2s+bqqAFwjnt4Q27+ufisfa4+2alkofFYa 3bsKCnnp0FLcg5NF79y4Aszc4Q5AXbYx49WATPIJqi7wA2ID8gcyeWSQjdameVLkFfgHHz 4hc/H3fOLj00BQfMGltBcDtGuasg2r08Qjf0ZgYt4sU1BeWoKXyrkHaCeIVBIQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gYSJC4qwsz3Rf for ; Sun, 07 Jun 2026 21:00:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 4729a by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sun, 07 Jun 2026 21:00:59 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Faraz Vahedi From: Robert Clausecker Subject: git: 5f732742ad5b - main - libc: Add free_sized() and free_aligned_sized() as per C23 List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@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: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5f732742ad5b3133a860a8969cf2bd13dc9ac358 Auto-Submitted: auto-generated Date: Sun, 07 Jun 2026 21:00:59 +0000 Message-Id: <6a25dc0b.4729a.7f7225a7@gitrepo.freebsd.org> The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=5f732742ad5b3133a860a8969cf2bd13dc9ac358 commit 5f732742ad5b3133a860a8969cf2bd13dc9ac358 Author: Faraz Vahedi AuthorDate: 2026-05-16 18:36:17 +0000 Commit: Robert Clausecker CommitDate: 2026-06-07 20:59:19 +0000 libc: Add free_sized() and free_aligned_sized() as per C23 Add C23 sized deallocation entry points as thin wrappers around free(3). Implementations may ignore size and alignment hints, so behaviour stays correct for existing allocations without validating caller metadata yet. When jemalloc is updated to 5.3.1, rewire these to je_free_sized() and je_free_aligned_sized() so deallocation can use the allocator's sized deallocation (free_sized for fast paths and free_aligned_sized for correct aligned hints.) Please note this change satisfies the standard interface only. Both functions should be delegated to jemalloc after the upgrade so callers get the intended allocator behaviour; until then, hints are unused and neither sized nor aligned-sized deallocation optimizations apply. Signed-off-by: Faraz Vahedi Reviewed by: fuz Pull Request: https://github.com/freebsd/freebsd-src/pull/2201 MFC after: 1 month --- include/stdlib.h | 2 + lib/libc/stdlib/malloc/Makefile.inc | 9 +++ lib/libc/stdlib/malloc/Symbol.map | 7 ++ lib/libc/stdlib/malloc/free_aligned_sized.c | 21 ++++++ lib/libc/stdlib/malloc/free_sized.3 | 100 ++++++++++++++++++++++++++++ lib/libc/stdlib/malloc/free_sized.c | 20 ++++++ lib/libc/stdlib/memory.3 | 17 ++++- 7 files changed, 175 insertions(+), 1 deletion(-) diff --git a/include/stdlib.h b/include/stdlib.h index 305aea4b8672..82a347f5317e 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -173,6 +173,8 @@ _Noreturn void */ #if __ISO_C_VISIBLE >= 2023 size_t memalignment(const void *) __pure2; +void free_sized(void *, size_t) __noexcept; +void free_aligned_sized(void *, size_t, size_t) __noexcept; #endif /* __ISO_C_VISIBLE >= 2023 */ /* diff --git a/lib/libc/stdlib/malloc/Makefile.inc b/lib/libc/stdlib/malloc/Makefile.inc index 3bae4ff1505b..e7e3b49a355a 100644 --- a/lib/libc/stdlib/malloc/Makefile.inc +++ b/lib/libc/stdlib/malloc/Makefile.inc @@ -1,3 +1,12 @@ SYM_MAPS+=${LIBC_SRCTOP}/stdlib/malloc/Symbol.map +.PATH: ${LIBC_SRCTOP}/stdlib/malloc + +MISRCS+= free_aligned_sized.c \ + free_sized.c + +MAN+= free_sized.3 + +MLINKS+= free_sized.3 free_aligned_sized.3 + .include "${LIBC_SRCTOP}/stdlib/malloc/${OPT_LIBC_MALLOC}/Makefile.inc" diff --git a/lib/libc/stdlib/malloc/Symbol.map b/lib/libc/stdlib/malloc/Symbol.map index d3aa7f3f9988..d1fefd8ca0c2 100644 --- a/lib/libc/stdlib/malloc/Symbol.map +++ b/lib/libc/stdlib/malloc/Symbol.map @@ -47,6 +47,13 @@ FBSD_1.4 { __mallctlbymib; }; +FBSD_1.9 { + free_sized; + free_aligned_sized; + __free_sized; + __free_aligned_sized; +}; + FBSDprivate_1.0 { _malloc_thread_cleanup; _malloc_prefork; diff --git a/lib/libc/stdlib/malloc/free_aligned_sized.c b/lib/libc/stdlib/malloc/free_aligned_sized.c new file mode 100644 index 000000000000..6bb238e0b2ba --- /dev/null +++ b/lib/libc/stdlib/malloc/free_aligned_sized.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2026 Faraz Vahedi + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +void __free(void *); + +void +__free_aligned_sized(void *ptr, size_t alignment, size_t size) +{ + + (void)alignment; + (void)size; + __free(ptr); +} + +__weak_reference(__free_aligned_sized, free_aligned_sized); diff --git a/lib/libc/stdlib/malloc/free_sized.3 b/lib/libc/stdlib/malloc/free_sized.3 new file mode 100644 index 000000000000..f208c829d933 --- /dev/null +++ b/lib/libc/stdlib/malloc/free_sized.3 @@ -0,0 +1,100 @@ +.\" +.\" Copyright (c) 2026 Faraz Vahedi +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd May 17, 2026 +.Dt FREE_SIZED 3 +.Os +.Sh NAME +.Nm free_sized , +.Nm free_aligned_sized +.Nd C23 sized deallocation functions +.Sh SYNOPSIS +.Lb libc +.In stdlib.h +.Ft void +.Fn free_sized "void *ptr" "size_t size" +.Ft void +.Fn free_aligned_sized "void *ptr" "size_t alignment" "size_t size" +.Sh DESCRIPTION +The +.Fn free_sized +function deallocates the memory referenced by +.Fa ptr +that was previously allocated by +.Xr malloc 3 , +.Xr realloc 3 , +or +.Xr calloc 3 . +The +.Fa size +argument shall equal the size passed to the allocation function. +The result of an +.Xr aligned_alloc 3 +may not be passed to +.Fn free_sized . +.Pp +The +.Fn free_aligned_sized +function deallocates memory referenced by +.Fa ptr +that was previously allocated by +.Xr aligned_alloc 3 . +The +.Fa alignment +and +.Fa size +arguments shall equal the values supplied to the allocation function. +The result of an +.Xr malloc 3 , +.Xr calloc 3 , +or +.Xr realloc 3 +may not be passed to +.Fn free_aligned_sized . +.Pp +If +.Fa ptr +is neither a null pointer nor a pointer returned by the allocation functions +described above for the corresponding deallocation function, the behaviour is +undefined, and so is supplying a +.Fa size +or +.Fa alignment +that does not match the original allocation. +.Sh IMPLEMENTATION NOTES +The C standard permits an implementation to ignore the +.Fa size +and +.Fa alignment +hints. +The current implementation forwards to +.Xr free 3 +without validating these arguments, so behaviour remains correct for +well-formed use. +.Pp +These functions will be wired to the system allocator's sized deallocation, +once supported, as with the rest of the memory allocation API, so that +.Fa size +and +.Fa alignment +hints are used for performance and security benefits. +That behaviour conforms to the practice recommended for each function in the +C standard. +.Sh SEE ALSO +.Xr free 3 , +.Xr malloc 3 , +.Xr calloc 3 , +.Xr realloc 3 , +.Xr aligned_alloc 3 , +.Xr jemalloc 3 +.Sh STANDARDS +The +.Fn free_sized +and +.Fn free_aligned_sized +functions conform to +.St -isoC-2023 . +.Sh AUTHOR +.An Faraz Vahedi Aq Mt kfv@kfv.io diff --git a/lib/libc/stdlib/malloc/free_sized.c b/lib/libc/stdlib/malloc/free_sized.c new file mode 100644 index 000000000000..b9242ac13862 --- /dev/null +++ b/lib/libc/stdlib/malloc/free_sized.c @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2026 Faraz Vahedi + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +void __free(void *); + +void +__free_sized(void *ptr, size_t size) +{ + + (void)size; + __free(ptr); +} + +__weak_reference(__free_sized, free_sized); diff --git a/lib/libc/stdlib/memory.3 b/lib/libc/stdlib/memory.3 index b7703cf44bd5..16674e8f208e 100644 --- a/lib/libc/stdlib/memory.3 +++ b/lib/libc/stdlib/memory.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd October 18, 2023 +.Dd May 17, 2026 .Dt MEMORY 3 .Os .Sh NAME @@ -33,6 +33,8 @@ .Nm alloca , .Nm calloc , .Nm free , +.Nm free_sized , +.Nm free_aligned_sized , .Nm malloc , .Nm posix_memalign , .Nm realloc , @@ -53,6 +55,10 @@ .Fn calloc "size_t nelem" "size_t elsize" .Ft void .Fn free "void *ptr" +.Ft void +.Fn free_sized "void *ptr" "size_t size" +.Ft void +.Fn free_aligned_sized "void *ptr" "size_t alignment" "size_t size" .Ft void * .Fn malloc "size_t size" .Ft int @@ -79,6 +85,8 @@ individual manual pages. .Xr alloca 3 , .Xr calloc 3 , .Xr free 3 , +.Xr free_aligned_sized 3 , +.Xr free_sized 3 , .Xr malloc 3 , .Xr posix_memalign 3 , .Xr realloc 3 , @@ -101,3 +109,10 @@ and .Fn posix_memalign functions conform to .St -p1003.1-2001 . +.Pp +The +.Fn free_sized +and +.Fn free_aligned_sized +functions conform to +.St -isoC-2023 .