r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • Feb 13 '26
Allocators from C to Zig
https://antonz.org/allocators/This is an overview of allocator usage and availability in a few low level languages: Rust, Zig, Odin, C3, Hare and C.
I think it might be interesting to people designing standard libraries for their languages.
43
Upvotes
View all comments
Show parent comment
2
u/matthieum Feb 15 '26
You're not... there's just edge cases all the time.
Memory allocators will typically round-up allocation sizes, rather than allocate the exact number of bytes request. For example, you ask for 1535 bytes, and get handed 1536 bytes instead. Then you ask to shrink that down to 1047 bytes, but it turns out that rounded up it's still 1536 bytes, so you already have a block of the appropriate size.
Shrinking in place is even more complicated. For example, mimalloc uses a trick where the "class" of allocation (small, medium, large) is encoded in the alignment of the memory block. Thus if you have a large block and ask to shrink it in place to a medium block, it can't be done: in place requires that the address (and thus alignment) be preserved, but the alignment needs to change to reflect the change of class.
In fact, even shrinking with reallocation may fail for odd reasons, when bucketing is involved. If the current bucket cannot be converted -- maybe it contains other live allocations -- and the target bucket is already full and cannot be extended (the OS refuses), then failure occurs (temporarily).