hosted/heap
skip to the library navigationHosted targets only.
An explicit allocator over the host runtime heap.
new returns a heap: a handle that borrows nothing, because the heap belongs to the process. Allocations remain manual obligations. Nothing installs an ambient allocator, and containers still receive the provider explicitly.
The provider is written once for every hosted system. It keeps eight size classes, of 32 to 4096 bytes, over chunks it maps through platform/posix, or on Windows through platform/windows's VirtualAlloc:
- A freed slot goes back on its class's list and is the next one handed out.
- A larger request gets a mapping of its own, rounded to the system's page and unmapped when it is freed.
A resize takes the first of these that applies:
- The block grows or shrinks in place while its slot or mapping holds it.
- A mapping grows in place where the pages after it are free.
- On Linux,
mremapmoves a mapping's pages. - Anything else is copied.
On Windows a mapping reserves whole 64 KiB units of address space and commits the pages it holds. It grows in place into the rest of its reservation, and past that it is copied, since Windows moves no pages.
The heap honours every usize alignment: it over-allocates and keeps a header before the block.
Threads share the heap:
- Once the program has started a thread, the size classes are behind one lock of
hosted/thread/lock. A program that has started none never takes it. - A mapping of its own takes no lock.
- A C thread calling in under
--link=libcis none of the program's, and two of them may not use the heap at once.
When the heap refuses a request, it refuses before any state changes.
Items
public heap: type = system_value
The host heap as an allocator capability. The value is a handle that borrows nothing: the heap belongs to the process, and copies share it. It allocates and frees through size classes over memory it maps from the system. A resize grows a block in place when its slot or mapping holds the new extent or its mapping can grow where it is; otherwise it moves the mapping's pages with Linux's mremap or copies the block. On Windows a mapping reserves its address space in the system's 64 KiB units and grows in place into what its reservation holds beyond it. Available only on hosted targets. Threads share the heap: once the program has started a thread through hosted/thread, one lock guards its size classes, and a program that has started none never takes it. A block larger than the largest class is a mapping of its own and needs no lock. A C thread calling in under --link=libc is no thread of the program's, and two of them may not use it at once.
Every usize alignment is accepted. Zero and one mean no stricter than byte alignment. A zero-byte request still returns a distinct, non-null, freeable token on success. The provider reserves a header plus at most alignment - 1 bytes and refuses an overflow or an extent above the host's PTRDIFF_MAX before asking the host for memory. Any other failure to obtain memory is reported through mem.out_of_memory, before the provider's own state changes.
Freeing trusts the two words this heap keeps before every block: it frees the extent they hold and does not read the size passed. So freeing a block this heap did not give, freeing one twice, or writing below a block's first byte is undefined, as it is with C's free; nothing here can tell such a block from one of its own. A block larger than the largest class is unmapped when it is freed, and a refusal to unmap, which the system gives only for an address or a length it never mapped, keeps the pages, costing their memory and nothing else.
public new: () -> (handle: heap)
Return a handle to the host heap. Making one does not allocate; callers retain and free each successful allocation explicitly.
Example program
This complete program is one of the compiler's runtime tests. View source.
The program, 296 lines
import core/mem import core/vec import hosted/heap no_allocation: atom allocation: type = no_allocation | ptr mut u8 same_block: (saved: allocation, block: ptr mut u8) -> (same: bool) = match saved no_allocation: same = false ptr (present): same = usize(present) == usize(block) end match end same_block is_absent: (value: allocation) -> (empty: bool) = match value no_allocation: empty = true ptr: empty = false end match end is_absent -- This fixture-local provider is an observer, not the reusable failure -- wrapper `core/fault` supplies. It delegates every operation -- to the real heap while retaining the two simultaneously live extents. audit_state: type = struct inner: heap.heap first_live: bool first_block: allocation first_size: usize second_live: bool second_block: allocation second_size: usize live: usize peak_live: usize allocations: usize frees: usize first_alloc_size: usize second_alloc_size: usize first_free_size: usize second_free_size: usize coexistence: bool mismatch: bool end audit_state audit: type = struct state: ptr mut audit_state end audit new_audit: () -> (state: audit_state) = state = (inner: heap.new(), first_live: false, first_block: no_allocation, first_size: 0, second_live: false, second_block: no_allocation, second_size: 0, live: 0, peak_live: 0, allocations: 0, frees: 0, first_alloc_size: 0, second_alloc_size: 0, first_free_size: 0, second_free_size: 0, coexistence: false, mismatch: false) end new_audit audit_alloc: (self: audit, size: usize, alignment: usize) -> (block: ptr mut u8) ! mem.out_of_memory = state: ptr mut audit_state = self.state block = try mem.allocate(state.val.inner, size, alignment) if state.val.first_live and state.val.second_live then mem.release(state.val.inner, block, size) fail mem.out_of_memory end if if state.val.live > 0 then state.val.coexistence = true end if inc state.val.live if state.val.live > state.val.peak_live then state.val.peak_live = state.val.live end if inc state.val.allocations if state.val.allocations == 1 then state.val.first_alloc_size = size elsif state.val.allocations == 2 then state.val.second_alloc_size = size end if if alignment > 1 and usize(block) % alignment <> 0 then state.val.mismatch = true end if if not state.val.first_live then state.val.first_live = true state.val.first_block = block state.val.first_size = size else state.val.second_live = true state.val.second_block = block state.val.second_size = size end if end audit_alloc audit_free: (self: audit, block: ptr mut u8, size: usize) -> none = state: ptr mut audit_state = self.state mut matched: bool = false if state.val.first_live and same_block(state.val.first_block, block) then if size <> state.val.first_size then state.val.mismatch = true end if state.val.first_live = false state.val.first_block = no_allocation matched = true elsif state.val.second_live and same_block(state.val.second_block, block) then if size <> state.val.second_size then state.val.mismatch = true end if state.val.second_live = false state.val.second_block = no_allocation matched = true end if if not matched then state.val.mismatch = true else dec state.val.live end if inc state.val.frees if state.val.frees == 1 then state.val.first_free_size = size elsif state.val.frees == 2 then state.val.second_free_size = size end if mem.release(state.val.inner, block, size) end audit_free audit_grow: (self: audit, block: ptr mut u8, old_size: usize, new_size: usize, alignment: usize) -> (grown: bool) = _ = self.state _ = block _ = old_size _ = new_size _ = alignment grown = false end audit_grow -- Resizing moves the block, through this provider's own allocation -- and free. audit_resize: (self: audit, block: ptr mut u8, old_size: usize, new_size: usize, alignment: usize) -> (moved: ptr mut u8) ! mem.out_of_memory = moved = try mem.relocate(self, block, old_size, new_size, alignment) end audit_resize audit is mem.allocator (alloc: audit_alloc, grow: audit_grow, resize: audit_resize, free: audit_free) node: type = struct value: i32 end node node_1: node = (value: 1) node_2: node = (value: 2) node_3: node = (value: 3) node_4: node = (value: 4) node_5: node = (value: 5) node_6: node = (value: 6) node_7: node = (value: 7) node_8: node = (value: 8) node_9: node = (value: 9) public main: () -> (code: i32) = code = 1 mut observed := new_audit() observer: audit = (state: addr observed) mut trace: i32 = 0 maximum: usize = 18446744073709551615 one: usize = 1 mut refused: bool = false impossible: allocation = mem.allocate(observer, maximum, one) else (problem) _ = problem refused = true no_allocation end if refused and is_absent(impossible) and observed.allocations == 0 then trace = trace + 1 end if first_size: usize = 17 first_alignment: usize = 24 second_size: usize = 31 second_alignment: usize = 64 first: ptr mut u8 = mem.allocate (observer, first_size, first_alignment) else (problem) _ = problem return end second: ptr mut u8 = mem.allocate (observer, second_size, second_alignment) else (problem) _ = problem return end first_last: ptr mut u8 = ptr(usize(first) + first_size - 1) second_last: ptr mut u8 = ptr(usize(second) + second_size - 1) first.val = 11 first_last.val = 17 second.val = 23 second_last.val = 31 if usize(first) <> 0 and usize(second) <> 0 and usize(first) <> usize(second) and usize(first) % first_alignment == 0 and usize(second) % second_alignment == 0 and first.val == 11 and first_last.val == 17 and second.val == 23 and second_last.val == 31 and observed.live == 2 and observed.peak_live == 2 and observed.coexistence and not observed.mismatch then trace = trace + 2 end if mem.release(observer, first, first_size) mem.release(observer, second, second_size) empty_size: usize = 0 empty_alignment: usize = 0 empty: ptr mut u8 = mem.allocate (observer, empty_size, empty_alignment) else (problem) _ = problem return end another_empty: ptr mut u8 = mem.allocate (observer, empty_size, one) else (problem) _ = problem return end if usize(empty) <> 0 and usize(another_empty) <> 0 and usize(empty) <> usize(another_empty) and observed.live == 2 then trace = trace + 4 end if mem.release(observer, empty, empty_size) mem.release(observer, another_empty, empty_size) if observed.live == 0 and observed.allocations == 4 and observed.frees == 4 and not observed.first_live and not observed.second_live and not observed.mismatch and is_absent(observed.first_block) and is_absent(observed.second_block) then trace = trace + 8 end if mut growth := new_audit() grower: audit = (state: addr growth) mut values := vec.new(item: ptr node) vec.push(values, grower, addr node_1) else none vec.push(values, grower, addr node_2) else none vec.push(values, grower, addr node_3) else none vec.push(values, grower, addr node_4) else none vec.push(values, grower, addr node_5) else none vec.push(values, grower, addr node_6) else none vec.push(values, grower, addr node_7) else none vec.push(values, grower, addr node_8) else none vec.push(values, grower, addr node_9) else none at_first: usize = 0 at_eighth: usize = 7 at_ninth: usize = 8 kept_first: ptr node = vec.get(values, at_first) else addr node_9 kept_eighth: ptr node = vec.get(values, at_eighth) else addr node_9 kept_ninth: ptr node = vec.get(values, at_ninth) else addr node_1 if kept_first.val.value == 1 and kept_eighth.val.value == 8 and kept_ninth.val.value == 9 and vec.length(values) == 9 and vec.capacity(values) == 16 and growth.allocations == 2 and growth.frees == 1 and growth.live == 1 and growth.peak_live == 2 and growth.coexistence and growth.first_alloc_size == 8 * sizeof ptr node and growth.second_alloc_size == 16 * sizeof ptr node and growth.first_free_size == 8 * sizeof ptr node and not growth.mismatch then trace = trace + 16 end if vec.release(values, grower) if vec.length(values) == 0 and vec.capacity(values) == 0 and growth.allocations == 2 and growth.frees == 2 and growth.live == 0 and not growth.first_live and not growth.second_live and is_absent(growth.first_block) and is_absent(growth.second_block) and growth.second_free_size == 16 * sizeof ptr node and not growth.mismatch then trace = trace + 32 end if if trace == 63 then code = 42 else code = trace end if end main