1-- The hosted heap is separate from core/mem's freestanding allocator
2-- protocol and caller-backed providers. These fixed scalar/pointer
3-- routines are supplied by the selected hosted runtime [1975].
4
5import core/mem
6
7no_allocation: atom
8allocation: type = no_allocation | ptr mut u8
9
10extern(c) _landin_host_heap_allocate:
11 (size: usize, alignment: usize) -> (block: allocation)
12extern(c) _landin_host_heap_release: (block: ptr mut u8) -> none
13
14--- Host-heap allocator capability. Allocates and frees through runtime
15--- helpers; does not grow allocations in place. Available only on hosted
16--- targets.
17---
18--- Every usize alignment is accepted. Zero and one mean no stricter than
19--- byte alignment. A zero-byte request still returns a distinct, non-null,
20--- freeable token on success. The host shim reserves one pointer word plus
21--- at most alignment - 1 bytes and refuses an overflow or an extent above
22--- the host's PTRDIFF_MAX before calling libc. Any other libc failure is
23--- reported through mem.out_of_memory.
24public system: type = struct
25 reserved: u8
26end system
27
28system_alloc: (inout state: system, size: usize, alignment: usize)
29 -> (block: ptr mut u8) ! mem.out_of_memory =
30 _ = state.reserved
31 allocated: allocation = _landin_host_heap_allocate(size, alignment)
32 match allocated
33 no_allocation: fail mem.out_of_memory
34 ptr (address): block = address
35 end match
36end system_alloc
37
38system_free: (inout state: system, block: ptr mut u8, size: usize) -> none =
39 _ = state.reserved
40 _ = size
41 _landin_host_heap_release(block)
42end system_free
43
44system_grow: (inout state: system, block: ptr mut u8,
45 old_size: usize, new_size: usize, alignment: usize)
46 -> (grown: bool) =
47 _ = state.reserved
48 _ = block
49 _ = old_size
50 _ = new_size
51 _ = alignment
52 grown = false
53end system_grow
54
55system is mem.allocator (alloc: system_alloc, grow: system_grow,
56 free: system_free)
57
58--- Create the host heap provider state. The state itself does not allocate;
59--- callers retain and free each successful allocation explicitly.
60public host: () -> (state: system) =
61 state = (reserved: 0)
62end host