arena.h file
Fixed-capacity arena that can free individual blocks.
Typedefs
- using MemHeap = struct MemHeap
Functions
- auto arena_create(size_t capacity) -> MemHeap*
- Create an arena with a fixed usable capacity.
- 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.
- auto arena_alloc(MemHeap* a, size_t n) -> void*
- Allocate a 16-byte aligned block from the arena.
- auto arena_alloc_aligned(MemHeap* a, size_t n, size_t alignment) -> void*
- Allocate a block aligned to at least
alignment. - 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.
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(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_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_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. |