Arena Allocator

#import "std/arena"

Simple arena allocator is memory storage operating on internally pre-allocated memory block. Arena can be used to store any arbitrary data. One of the reasons why to use Arena allocator is reduction of memory allocation overhead; the memory resource is acquired only once (on arena_make or arena_resize call).

Size of the allocated memory block is fixed, that means calling alloc in case there is not enough space in the arena ends up with an error. However arena can be resizes by arena_resize if needed (all previously memory allocated using arena becomes invalid).

Supported Operations

- [x] Allocate
- [-] Reallocate (Cause an error.)
- [-] Free
- [x] Release
- [x] Reset

std.ARENA_ALIGNMENT

ARENA_ALIGNMENT :: DEFAULT_ALIGNMENT

Alignment of each arena allocation. This value should be large enough to properly align all possible data, we also can use this value to precisely predict required arena size in advance.

File: arena.bl

std.ArenaAllocator

ArenaAllocator :: struct {
    allocator: *Allocator;
    mem: *u8;
    top: *u8;
    space: usize;
    allocated: usize;
}

The Arena Allocator type.

Members

File: arena.bl

std.arena_make

arena_make :: fn (size : usize: 0, allocator : *Allocator: null) ArenaAllocator #inline

Make new arena instance. In case the size is not 0, the arena memory chunk is preallocated right away to size bytes. The allocator is used to preallocate memory chunk in case it's specified, otherwise default allocator set in application context is used.

Call release_allocator to release all resources used by arena if it's not needed anymore.

File: arena.bl

std.arena_resize

arena_resize :: fn (a: *ArenaAllocator, size: usize) Error

Resize & reset arena. Previously allocated memory block is kept for later use, however all memory acquired from this arena becomes invalid (arena is set to an empty state).

File: arena.bl

std.arena_required_additional_space

arena_required_additional_space :: fn (count_of_allocations: s32) usize #inline

This helper function might be used to precisely calculate additional space required by arena to store count_of_allocations elements in advance.

File: arena.bl