Next-gen malloc for musl libc - Working draft
This is a draft of the upcoming next-gen
malloc implementation for
musl libc. It is not complete, lacking some logic for strategy to
obtain new memory, memalign-family functions, etc. as well as some
hardening features and optimizations that the final version is
expected to include.
Makefile builds static and shared library files that
programs can link with, or use with
LD_PRELOAD (for the shared
This allocator organizes memory dynamically into small slab-style groups of up to 32 identical-size allocation units with status controlled by bitmasks, and utilizes a mix of in-band and out-of-band metadata to isolate sensitive state from regions easily accessible through out-of-bounds writes, while avoiding the need for expensive global data structures.
The closest analogue among other well-known allocators is probably OpenBSD's omalloc.
Base allocation granularity and alignment is 16 bytes. Large
allocations are made individually by mmap, but they also have
out-of-band metadata records treating them as special one-member
free to validate them. Smaller
allocations come from groups of one of 48 size classes, spaced
linearly up to 128 (the first 8 classes), then roughly geometrically
with four steps per doubling, but adjusted to divide powers of two
with minimal remainder (waste).