Skip to content

libc: Add free_sized() and free_aligned_sized() as per C23#2201

Open
kfv wants to merge 1 commit into
freebsd:mainfrom
kfv:libc/stdlib/free_sized
Open

libc: Add free_sized() and free_aligned_sized() as per C23#2201
kfv wants to merge 1 commit into
freebsd:mainfrom
kfv:libc/stdlib/free_sized

Conversation

@kfv
Copy link
Copy Markdown
Contributor

@kfv kfv commented May 16, 2026

Add free_sized() (§7.24.3.4) and free_aligned_sized() (§7.24.3.5) as per C23.

For now they're simply thin wrappers around free(3) as the standard allows implementations to ignore the size and alignment hints (§7.24.3.4.3, §7.24.3.5.3,) so the behaviour is correct and conformant.

Once jemalloc is bumped to 5.3.1, it'd be well worth rewiring these to use the allocator's sized deallocation for the performance and security benefits.

@kfv kfv force-pushed the libc/stdlib/free_sized branch from 6e3ad77 to 1632cc4 Compare May 16, 2026 19:48
@clausecker
Copy link
Copy Markdown
Contributor

Please bump the man page date when you update a man page.

CC @bsdimp for additional review, though LGTM from my side.

@kfv kfv force-pushed the libc/stdlib/free_sized branch from 1632cc4 to 7c95dd7 Compare May 16, 2026 22:05
@kfv
Copy link
Copy Markdown
Contributor Author

kfv commented May 16, 2026

Please bump the man page date when you update a man page.

Sure, my apologies. Done.

Comment thread lib/libc/stdlib/malloc/free_aligned_sized.c Outdated
Comment thread lib/libc/stdlib/memory.3
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>
@kfv kfv force-pushed the libc/stdlib/free_sized branch from 7c95dd7 to 43c0c4c Compare May 17, 2026 07:23
@kfv kfv requested a review from bsdimp May 17, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants