raz / site / content / tour / 09-memory.txt

129 lines · 3.9 KiB · raw · history · last change a5145f64

1title: Memory and allocators
2part: Set
3summary: No garbage collector; you choose where memory comes from, and what happens when it runs out.
4---
5```raz Stack run
6```
7
8raz 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
12A 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
15module Pool;
16import Out, Mem, Arena;
17import SYSTEM;
18
19type 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) *)
22proc make(n: INT; a: ^Mem.Allocator): ^Item;
23var head, it: ^Item; i: INT;
24begin
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;
34end;
35
36proc count(it: ^Item): INT;
37var n: INT;
38begin
39 n := 0;
40 while it # nil do INC(n); it := it^.next; end;
41 return n;
42end;
43
44proc main*;
45var ar: Arena.Arena; small: Arena.Arena; list: ^Item;
46begin
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);
59end;
60
61end Pool.
62```
63
64The 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
71module Free;
72import Out, Mem, Heap;
73import SYSTEM;
74
75type Cell = record val: INT; end;
76
77proc main*;
78var a, b: ^Cell; i, total: INT;
79begin
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));
89end;
90
91end 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
99module Build;
100import Out, Buf, Heap, Str;
101
102proc main*;
103var b: Buf.Buf; i: INT;
104begin
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;
120end;
121
122end 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
129Under 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.