| 1 | title: Memory and allocators |
| 2 | part: Set |
| 3 | summary: No garbage collector; you choose where memory comes from, and what happens when it runs out. |
| 4 | --- |
| 5 | ```raz Stack run |
| 6 | ``` |
| 7 | |
| 8 | raz has no garbage collector, and no allocator of its own: memory comes from an allocator, a `Mem.Allocator`, which the program chooses and passes. `NEW(n, a)` sets `n` to a fresh, zeroed `Node` from `a`, or to `nil` if `a` has no room. `Heap.heap` is the general one, which `Mem.free` gives memory back to. An `Arena` hands out memory from one block and frees it all at once, as the last lines above do. |
| 9 | |
| 10 | ## Why an allocator is a parameter |
| 11 | |
| 12 | A procedure that allocates takes the allocator it uses, so its caller decides where the memory comes from and when it goes: the heap for something that lives long, an arena for everything one request needs. Nothing in the library allocates behind your back. |
| 13 | |
| 14 | ```raz Pool run |
| 15 | module Pool; |
| 16 | import Out, Mem, Arena; |
| 17 | import SYSTEM; |
| 18 | |
| 19 | type Item = record id: INT; next: ^Item; end; |
| 20 | |
| 21 | (* n items, linked; or nil if the allocator ran dry (what came is lost with it) *) |
| 22 | proc make(n: INT; a: ^Mem.Allocator): ^Item; |
| 23 | var head, it: ^Item; i: INT; |
| 24 | begin |
| 25 | head := nil; |
| 26 | for i := 1 to n do |
| 27 | NEW(it, a); |
| 28 | if it = nil then return nil; end; |
| 29 | it^.id := i; |
| 30 | it^.next := head; |
| 31 | head := it; |
| 32 | end; |
| 33 | return head; |
| 34 | end; |
| 35 | |
| 36 | proc count(it: ^Item): INT; |
| 37 | var n: INT; |
| 38 | begin |
| 39 | n := 0; |
| 40 | while it # nil do INC(n); it := it^.next; end; |
| 41 | return n; |
| 42 | end; |
| 43 | |
| 44 | proc main*; |
| 45 | var ar: Arena.Arena; small: Arena.Arena; list: ^Item; |
| 46 | begin |
| 47 | if not Arena.init(ar, 1000000) then return; end; |
| 48 | list := make(1000, SYSTEM.ADR(ar.base)); |
| 49 | Out.int(count(list)); Out.str(" items from a big arena\n"); |
| 50 | Arena.reset(ar); (* every allocation forgotten *) |
| 51 | list := make(10, SYSTEM.ADR(ar.base)); |
| 52 | Out.int(count(list)); Out.str(" after a reset\n"); |
| 53 | Arena.done(ar); |
| 54 | |
| 55 | if not Arena.init(small, 4096) then return; end; |
| 56 | list := make(1000, SYSTEM.ADR(small.base)); |
| 57 | if list = nil then Out.str("a small arena ran out, and said so\n"); end; |
| 58 | Arena.done(small); |
| 59 | end; |
| 60 | |
| 61 | end Pool. |
| 62 | ``` |
| 63 | |
| 64 | The run-out case is an ordinary `nil` to test, not an exception that unwinds past your code. |
| 65 | |
| 66 | ## The heap |
| 67 | |
| 68 | `Heap.heap` serves any size and takes memory back with `Mem.free`. Freeing a block twice, or one that was never given out, stops the program with a message instead of corrupting the heap. It is safe to call from several threads. |
| 69 | |
| 70 | ```raz Free run |
| 71 | module Free; |
| 72 | import Out, Mem, Heap; |
| 73 | import SYSTEM; |
| 74 | |
| 75 | type Cell = record val: INT; end; |
| 76 | |
| 77 | proc main*; |
| 78 | var a, b: ^Cell; i, total: INT; |
| 79 | begin |
| 80 | for i := 1 to 5 do |
| 81 | NEW(a, Heap.heap); |
| 82 | a^.val := i * i; |
| 83 | total := total + a^.val; |
| 84 | Mem.free(Heap.heap, ^U8(a)); (* freed at once: its block is reused *) |
| 85 | end; |
| 86 | NEW(b, Heap.heap); |
| 87 | Out.int(total); Out.char(' '); Out.int(b^.val); Out.ln; (* a new block is zeroed *) |
| 88 | Mem.free(Heap.heap, ^U8(b)); |
| 89 | end; |
| 90 | |
| 91 | end Free. |
| 92 | ``` |
| 93 | |
| 94 | ## Buffers that grow |
| 95 | |
| 96 | `Buf` is a growable byte buffer; like everything else it takes the allocator it grows from. It is what the program uses to build a string of unknown length. |
| 97 | |
| 98 | ```raz Build run |
| 99 | module Build; |
| 100 | import Out, Buf, Heap, Str; |
| 101 | |
| 102 | proc main*; |
| 103 | var b: Buf.Buf; i: INT; |
| 104 | begin |
| 105 | Buf.init(b, Heap.heap); |
| 106 | defer Buf.done(b); |
| 107 | for i := 1 to 5 do |
| 108 | Buf.str(b, "item "); |
| 109 | Buf.add(b, U8('0' + i)); |
| 110 | Buf.add(b, ','); |
| 111 | end; |
| 112 | Out.str(Buf.bytes(b)); Out.ln; |
| 113 | Out.int(b.len); Out.str(" bytes\n"); |
| 114 | Buf.reset(b); |
| 115 | Str.join(b, ["red", "green", "blue"], ", "); |
| 116 | Out.str(Buf.bytes(b)); Out.ln; |
| 117 | Buf.reset(b); |
| 118 | Str.replace(b, "a-b-c", "-", " and "); |
| 119 | Out.str(Buf.bytes(b)); Out.ln; |
| 120 | end; |
| 121 | |
| 122 | end Build. |
| 123 | ``` |
| 124 | |
| 125 | `Str.join` and `Str.replace` build into a `Buf` too: the parts with a separator between them, or a string with one text replaced by another wherever it is. |
| 126 | |
| 127 | ## Pages |
| 128 | |
| 129 | Under the allocators are pages from the system: `Mem.pages(size)` gives zeroed memory in whole pages, or `nil`, and `Mem.unmap` gives it back. Most programs never need to call it directly. |