Skip to content

<util/arena.h>

Bump allocator over a caller-provided buffer.

Allocations only move a cursor forward; nothing is freed individually. ArenaReset() releases everything at once.

#include <util/arena.h>

Structures

Arena

An arena: a buffer and how much of it is used. The heap's reservation and current break.

Field Type Description
base uint8_t * Start of the buffer.
size size_t Buffer size in bytes.
offset size_t Bytes handed out so far.
base VirtAddr Start of the reservation.
brk VirtAddr Current break.
mapped VirtAddr End of the reservation.

Functions

ArenaInit

void ArenaInit(Arena *a, void *buf, size_t size);

Sets up an arena over buf.

The arena does not own the buffer.

Parameter Direction Description
a out Arena to initialise.
buf in Backing buffer.
size in Size of buf in bytes.

ArenaAlloc

void * ArenaAlloc(Arena *a, size_t size);

Takes size bytes from the arena, with no alignment.

Parameter Direction Description
a inout Arena to allocate from.
size in Bytes wanted.

Returns: The memory, or NULL if fewer than size bytes are left.

ArenaAllocAligned

void * ArenaAllocAligned(Arena *a, size_t size, size_t align);

Takes size bytes from the arena, aligned to align.

Skips padding bytes as needed to reach the alignment.

Parameter Direction Description
a inout Arena to allocate from.
size in Bytes wanted.
align in Alignment in bytes; must be a power of two.

Returns: The memory, or NULL if padding plus size does not fit.

ArenaReset

void ArenaReset(Arena *a);

Releases every allocation at once; the buffer is kept for reuse.

Parameter Direction Description
a inout Arena to reset.

ArenaDestroy

void ArenaDestroy(Arena *a);

Forgets the buffer and zeroes the arena.

The buffer itself is not freed.

Parameter Direction Description
a out Arena to clear.