core/zone
skip to the library navigationShared: available on every enabled target.
A bump allocator that grows by taking blocks from a parent allocator.
| Call | What it does |
|---|---|
zone.over | Fill the caller's zone.space, declared zeroed, and return the handle, a zone.zone(provider), from it. The handle holds a copy of the parent's handle. Nothing is allocated. |
zone.reset | Give every allocation back at once, through the handle. Every block except the newest goes back to the parent. |
zone.release | Take the space and the same parent and return every block. It is refused while the handle is still used. |
The zone hands out the newest block's bytes in address order. When they run out, it takes a new block from the parent, twice as large as the last. A request too large for that block, or a parent that refuses it, gets a block just large enough. If the parent refuses that too, the allocation fails with out_of_memory and the zone is unchanged.
An individual free reclaims nothing. Only the latest allocation grows in place or gives back its tail; growing any other moves it.
The newest block is the largest, and reset keeps it. So a loop that resets the zone after each round settles on one block that holds a whole round.
Items
public space: type = zone_state
The caller's state of a zone: where its newest block is and how much of that block is in use. Declare it zeroed and give it to over, which fills it. It must outlive the zone's handle. release empties it.
public zone: type (provider: type) = zone_value(provider)
A bump allocator that grows by taking blocks from a parent allocator. The value is a handle, so copies share one zone. Freeing a single allocation reclaims nothing. reset gives every allocation back at once, and release returns the blocks to the parent. Only the most recent allocation grows in place or gives back its tail. Growing any other moves it, and its old bytes are not reclaimed.
public over: (provider: type is mem.allocator, inout state: space,
parent: provider)
-> (handle: zone(provider) from state, parent)
Fill state for a zone over parent and return the zone's handle. Nothing is allocated. The handle holds a copy of the parent's handle, and the parent must outlive the zone.
public release: (provider: type is mem.allocator, inout state: space,
parent: provider) -> none
Return every block to parent, which must be the parent the zone was made over. state is then empty and may be given to over again. End every use of the zone's allocations first. The checker refuses the call while the zone's handle is still used.
public reset: (provider: type is mem.allocator, memory: zone(provider))
-> none
Give every allocation of the zone back at once. The newest block, which is the largest, stays for the allocations that follow. Every older block goes back to the parent. Every allocation the zone made is then free, and a program must not use one, just as after free.
Example program
This complete program is one of the compiler's runtime tests. View source.
The program, 28 lines
import core/arena import core/mem import core/zone exercise: () -> (ok: bool) ! mem.out_of_memory = ok = false mut backing: [64]u8 = zeroed mut arena_space: arena.space = zeroed memory := arena.over_unchecked(arena_space, addr backing[0], 64) mut state: zone.space = zeroed scratch := zone.over(state, memory) address := try mem.allocate(scratch, usize(4), usize(1)) address.val = 42 return when address.val <> 42 zone.reset(scratch) again := try mem.allocate(scratch, usize(4), usize(1)) return when usize(again) <> usize(address) zone.release(state, memory) ok = true end exercise public main: () -> (code: i32) = code = 1 ok := exercise() else false if ok then code = 42 end if end main