2.2.2 Bump Allocator¶
-
struct ZixBumpAllocator¶
A simple bump-pointer allocator that never frees.
This is about the simplest possible allocator that is useful in practice. It uses a user-provided memory buffer with a fixed size, and allocates by simply “bumping” the top offset like a stack. This approach is simple, extremely fast, and hard real-time safe, but at the cost of being limited to narrow use cases since there is (almost) no support for deallocation.
Using this allocator requires knowing up-front the total amount of memory that will be allocated (without reuse). Typically this makes sense in short scopes like a function call.
This allocator adheres to standard C semantics as much as possible, but is much more restrictive. Specifically:
All allocations are aligned to sizeof(uintmax_t).
Both free() and realloc() only work on the most recently allocated pointer, essentially serving as a cheap pop and pop-push, respectively.
Calling free() means that realloc() will fail and free() will do nothing until the next allocation. In other words, free() can’t be used twice in a row.
There is no relocation: realloc() always returns either the input pointer, or null.
-
ZixAllocator base¶
Base allocator instance.
-
void *buffer¶
User-owned memory buffer.
-
size_t last¶
Last allocation offset in bytes.
-
size_t top¶
Stack top/end offset in bytes.
-
size_t capacity¶
Size of buffer in bytes (the maximum top)
-
ZixBumpAllocator zix_bump_allocator(size_t capacity, void *buffer)¶
Return a bump allocator that works within a provided buffer.