Skip to content

std.heap

bump arena; growable (64 KiB chunks), reserved, or over a fixed buffer

pub MemError :: errors {
out_of_memory,
}
  • out_of_memory: OS alloc failed, or fixed/reserved cap full
pub Arena :: struct {
first: usize,
head: usize,
mode: u8,
}

first oldest chunk, head current bump chunk, mode an ARENA_* value

pub Mark :: struct {
chunk: usize,
used: usize,
}

saved bump position for restore

pub make :: fn() -> Arena

reserves nothing until the first alloc

pub reserve :: fn(max: usize) -> Arena

max bytes of address space, committed lazily; pointers never move

pub fixed :: fn(buf: []u8) -> Arena

no syscalls, can’t grow; buf must outlive the arena

pub alloc_bytes :: fn(self: *Arena, n: usize) -> []u8

aborts on exhaustion

pub try_alloc_bytes :: fn(self: *Arena, n: usize) -> []u8 ! MemError

.out_of_memory instead of aborting

pub alloc :: fn($T: type, self: *Arena, n: usize) -> []T

16-byte aligned; aborts on exhaustion

pub try_alloc :: fn($T: type, self: *Arena, n: usize) -> []T ! MemError

fallible alloc

pub alloc_one :: fn($T: type, self: *Arena) -> *T

aborts on exhaustion

pub dupe :: fn($T: type, self: *Arena, src: []const T) -> []T

copy into the arena; aborts on exhaustion

pub new :: fn($T: type, self: *Arena) -> *T

zone spellings of alloc_one / alloc

pub new_slice :: fn($T: type, self: *Arena, n: usize) -> []T
pub reset :: fn(self: *Arena)

rewind, keep chunks, O(1); everything allocated before is invalid

pub mark :: fn(self: *Arena) -> Mark

for restore; invalid after the next reset/deinit

pub restore :: fn(self: *Arena, m: Mark)

frees everything since m for reuse; chunks are kept

pub deinit :: fn(self: *Arena)

memory back to the OS (fixed: just cleared); reusable after