package core.alloc.arena
This allocator is mostly used for making many fixed-size allocation (i.e. allocations that will not need to change in size, such as game entities or position structs). The power of this allocator over the heap allocator for this purpose is that it is much faster, since the logic is simpler. Another power of this allocator over something such as a dynamic array is that the dynamic array could relocate and cause any pointers to the data inside to become invalidated; this is definitely not behaviour you want. This arena allocator can grow as large as needed, while guaranteeing that the memory inside of it will never move.
Arena :: struct {
backing_allocator: Allocator;
first_arena: &ArenaBlock;
current_arena: &ArenaBlock;
size: u32;
arena_size: u32;
}
Stores internal details used during arena allocations.
arena_alloc_proc :: (data: rawptr, aa: AllocationAction, size: u32, align: u32, oldptr: rawptr) -> rawptr
auto :: macro (size, $dest: Code) -> void
auto :: macro (body: Code, size: i32) -> i32
Creates an arena allocator and automatically applies it to the context's allocator in the current scope.
foo :: () {
alloc.arena.auto();
// Lazily allocate everything, knowing that it will
// be freed when this function returns.
for 100 {
s := string.copy("Make a copy of me!");
}
}
auto_temp :: macro (body: Code) -> i32
Creates an arena allocator to be used as the temporary allocator in the code block.
foo :: () {
alloc.arena.auto_temp() {
for 1000 {
// Will be automatically freed
x := new_temp(i32);
}
}
}
get_allocated_arenas :: (arena: &Arena) -> u32
Returns the number of pages in the arena.
get_allocated_bytes :: (arena: &Arena) -> u32
Returns the number of bytes used by the arena.
make :: (backing: Allocator, arena_size: u32) -> Arena
Makes a new arena.
arena_size
specifies the size of each individual arena page, which must be at least 4 bytes in size (but should be quite a bit large).