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 <kfv@kfv.io>
Reviewed by:	fuz
Pull Request:	https://github.com/freebsd/freebsd-src/pull/2201
MFC after:	1 month
This commit is contained in:
Faraz Vahedi
2026-05-16 22:06:17 +03:30
committed by Robert Clausecker
parent c3f6dcea19
commit 5f732742ad
7 changed files with 175 additions and 1 deletions
+2
View File
@@ -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 */
/*
+9
View File
@@ -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"
+7
View File
@@ -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;
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2026 Faraz Vahedi <kfv@kfv.io>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <sys/cdefs.h>
#include <stdlib.h>
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);
+100
View File
@@ -0,0 +1,100 @@
.\"
.\" Copyright (c) 2026 Faraz Vahedi <kfv@kfv.io>
.\"
.\" 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
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2026 Faraz Vahedi <kfv@kfv.io>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <sys/cdefs.h>
#include <stdlib.h>
void __free(void *);
void
__free_sized(void *ptr, size_t size)
{
(void)size;
__free(ptr);
}
__weak_reference(__free_sized, free_sized);
+16 -1
View File
@@ -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 .