core/region
skip to the library navigationShared: available on every enabled target.
An allocator scope that records allocations through a parent and releases them together.
over makes a region's handle over the caller's ledger, space(capacity), and a copy of the parent's handle:
| Capacity | Ledger |
|---|---|
| zero | grows through the parent; growing_space names it |
| positive | a fixed caller ledger |
Individual frees do nothing. Every allocation consumes a ledger entry until release, whether or not its payload is still live.
A resize of the newest allocation goes to the parent, and its entry follows the block. A resize of any other block moves it into a new entry, and the old one is freed at release.
release takes the ledger and the same parent and frees payloads in reverse order. It is refused while the region's handle is still used. End all payload uses before releasing the region.
Items
Types 4
Also on this page
public allocation: type = struct
address: usize
extent: usize
end allocation
Ledger entry storing an allocated address and its byte extent. A region records one for every payload it hands out.
public growing_space: type = space_value(0)
Caller-owned state of a region whose ledger grows through its parent: the one ledger a capacity of zero means, since a bounded ledger of no records could hold nothing. Declare it zeroed.
public region: type (provider: type) = region_value(provider)
An allocator that takes its payloads from a parent and records each one. It is a handle: copies share one ledger. Releasing its state frees all recorded payloads; the parent must outlive the region.
public space: type (fixed capacity: usize) = space_value(capacity)
Caller-owned state of a region: its ledger. With a positive capacity the ledger is these capacity records and no more; with zero it grows through the parent. Declare it zeroed and give it to over, which fills it; it must outlive the region handle, and release empties it.
public over: (fixed capacity: usize, provider: type is mem.allocator,
inout state: space(capacity), parent: provider)
-> (handle: region(provider) from state, parent)
Fill state for a region over parent and return its handle. The handle holds a copy of the parent's handle. Every allocation takes one ledger record until release, since an individual free returns nothing. A bounded ledger that is full reports mem.out_of_memory and returns the new payload to the parent; a growing ledger's own allocation failure does the same.
public release: (fixed capacity: usize, provider: type is mem.allocator,
inout state: space(capacity), parent: provider) -> none
Free recorded payloads in reverse order through parent, which must be the parent the region was made over, and give a growing ledger's blocks back to it; state is then empty and may be given to over again. End every payload use and every use of the region's handle first. An arena parent keeps the bytes it handed out, since it reclaims nothing until its backing ends.
Example program
This complete program is one of the compiler's runtime tests. View source.
The program, 29 lines
import core/arena import core/mem import core/region 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 ledger: region.space(1) = zeroed scratch := region.over(ledger, memory) address := try mem.allocate(scratch, usize(4), usize(1)) address.val = 42 return when address.val <> 42 _ = mem.allocate(scratch, usize(4), usize(1)) else (problem) _ = problem mem.release(scratch, address, usize(4)) ok = true return end end exercise public main: () -> (code: i32) = code = 1 ok := exercise() else false if ok then code = 42 end if end main