arena.c file
Arena allocator implementation.
Classes
Typedefs
- using FreeNode = struct FreeNode
- Free-list node inside an arena.
Functions
- static auto page_size(void) -> size_t
- static auto align_up(const size_t n, const size_t a) -> size_t
- auto arena_create(size_t capacity) -> MemHeap*
- Create an arena with a fixed usable capacity.
- static void recycle_node(MemHeap* a, FreeNode* node)
- static auto take_node(MemHeap* a) -> FreeNode*
- void arena_destroy(MemHeap* a)
- Destroy an arena and release all of its memory.
- void arena_reset(MemHeap* a)
- Discard every live block and keep the arena's mapping.
- static auto align_up_ok(const size_t n, const size_t a, size_t* out) -> int
- auto arena_alloc_aligned(MemHeap* a, size_t n, size_t alignment) -> void*
- Allocate a block aligned to at least
alignment. - auto arena_alloc(MemHeap* a, size_t n) -> void*
- Allocate a 16-byte aligned block from the arena.
- void arena_free(MemHeap* a, void* p)
- Return a previously allocated block to the arena.
- auto arena_capacity(const MemHeap* a) -> size_t
- Query the arena's usable capacity.
- auto arena_used(const MemHeap* a) -> size_t
- Query how many bytes are currently allocated from the arena.
Defines
- #define _POSIX_C_SOURCE
- #define _DEFAULT_SOURCE
- #define MAP_ANONYMOUS
- #define ALIGN
- #define HDR_SIZE
Typedef documentation
Function documentation
MemHeap* arena_create(size_t capacity)
Create an arena with a fixed usable capacity.
| Parameters | |
|---|---|
| capacity | Maximum number of bytes the arena can hold. |
| Returns | New arena on success, or NULL if capacity is 0 or allocation fails. |
void arena_destroy(MemHeap* a)
Destroy an arena and release all of its memory.
| Parameters | |
|---|---|
| a | Arena to destroy; ignored if NULL. |
void arena_reset(MemHeap* a)
Discard every live block and keep the arena's mapping.
| Parameters | |
|---|---|
| a | Arena to reset; ignored if NULL. |
The cost follows the free-node list, not the number of live blocks. An arena that has only been allocated from has a single free node, so the reset is constant time. Pages already touched stay committed.
void* arena_alloc_aligned(MemHeap* a, size_t n, size_t alignment)
Allocate a block aligned to at least alignment.
| Parameters | |
|---|---|
| a | Arena to allocate from. |
| n | Number of payload bytes requested. |
| alignment | Required alignment. Zero and non-powers of two are rejected. |
| Returns | Pointer to the payload, or NULL if the request cannot be satisfied. |
alignment must be a power of two. The payload is aligned to the greater of alignment and 16. Padding used to reach that alignment stays inside the block and is reclaimed by arena_.
void* arena_alloc(MemHeap* a, size_t n)
Allocate a 16-byte aligned block from the arena.
| Parameters | |
|---|---|
| a | Arena to allocate from. |
| n | Number of payload bytes requested. |
| Returns | Pointer to the payload, or NULL if the request cannot be satisfied. |
void arena_free(MemHeap* a, void* p)
Return a previously allocated block to the arena.
| Parameters | |
|---|---|
| a | Arena that owns the block. |
| p | Pointer returned by arena_ |
If the free list cannot record the block, it stays allocated and arena_ is left unchanged, so the space is not lost.
size_t arena_capacity(const MemHeap* a)
Query the arena's usable capacity.
| Parameters | |
|---|---|
| a | Arena to query. |
| Returns | Capacity in bytes, or 0 if a is NULL. |
size_t arena_used(const MemHeap* a)
Query how many bytes are currently allocated from the arena.
| Parameters | |
|---|---|
| a | Arena to query. |
| Returns | Bytes in use, or 0 if a is NULL. |