| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | <title>Memory and allocators - Raz, Set, Go</title> |
| 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | <meta property="og:title" content="Memory and allocators - Raz, Set, Go"> |
| 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | <meta property="og:image" content="https://raz.lucela.org/social.png"> |
| 12 | <meta name="twitter:card" content="summary_large_image"> |
| 13 | <link rel="icon" href="../favicon.svg" type="image/svg+xml"> |
| 14 | <link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32"> |
| 15 | <link rel="apple-touch-icon" href="../apple-touch-icon.png"> |
| 16 | <link rel="stylesheet" href="../style.css"> |
| 17 | </head> |
| 18 | <body> |
| 19 | <header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header> |
| 20 | <main> |
| 21 | <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'> |
| 22 | <li><a href="hello.html">Hello, world</a></li> |
| 23 | <li><a href="modules.html">Modules</a></li> |
| 24 | <li><a href="integers.html">Integers</a></li> |
| 25 | <li><a href="values.html">Values and conversions</a></li> |
| 26 | <li><a href="procedures.html">Procedures and several results</a></li> |
| 27 | </ol> |
| 28 | <h3>Set</h3><ol start='6'> |
| 29 | <li><a href="slices.html">Arrays, slices and strings</a></li> |
| 30 | <li><a href="records.html">Records and pointers</a></li> |
| 31 | <li><a href="variants.html">Enumerations, sets, variants and match</a></li> |
| 32 | <li><a href="memory.html" aria-current="page">Memory and allocators</a></li> |
| 33 | </ol> |
| 34 | <h3>Go</h3><ol start='10'> |
| 35 | <li><a href="errors.html">Errors</a></li> |
| 36 | <li><a href="defer.html">Defer</a></li> |
| 37 | <li><a href="checks.html">When it goes wrong</a></li> |
| 38 | <li><a href="generics.html">Generic modules</a></li> |
| 39 | <li><a href="c.html">Talking to C</a></li> |
| 40 | <li><a href="threads.html">Threads</a></li> |
| 41 | </ol></aside><article><p class="kicker">Set · lesson 9 of 15</p><h1>Memory and allocators</h1> |
| 42 | <div class="file"><span>Stack.raz</span></div><pre class="code"><code><span class="kw">module</span> Stack; |
| 43 | <span class="kw">import</span> Out, Mem, Heap, Arena, SYSTEM; |
| 44 | |
| 45 | <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 46 | |
| 47 | <span class="comment">(* a node holding v on top of top, from the allocator a; |
| 48 | nil if a has no room *)</span> |
| 49 | <span class="kw">proc</span> push(top: ^Node; v: <span class="ty">INT</span>; a: ^Mem.Allocator): ^Node; |
| 50 | <span class="kw">var</span> n: ^Node; |
| 51 | <span class="kw">begin</span> |
| 52 | <span class="ty">NEW</span>(n, a); |
| 53 | <span class="kw">if</span> n # <span class="ty">nil</span> <span class="kw">then</span> |
| 54 | n^.next := top; |
| 55 | n^.val := v; |
| 56 | <span class="kw">end</span>; |
| 57 | <span class="kw">return</span> n; |
| 58 | <span class="kw">end</span>; |
| 59 | |
| 60 | <span class="kw">proc</span> main*; |
| 61 | <span class="kw">var</span> top, n: ^Node; i: <span class="ty">INT</span>; ar: Arena.Arena; |
| 62 | <span class="kw">begin</span> |
| 63 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> top := push(top, i, Heap.heap); <span class="kw">end</span>; |
| 64 | <span class="kw">while</span> top # <span class="ty">nil</span> <span class="kw">do</span> |
| 65 | Out.int(top^.val); Out.char(<span class="string">' '</span>); |
| 66 | n := top; |
| 67 | top := top^.next; |
| 68 | Mem.free(Heap.heap, ^<span class="ty">U8</span>(n)); |
| 69 | <span class="kw">end</span>; |
| 70 | Out.ln; |
| 71 | <span class="kw">if</span> <span class="kw">not</span> Arena.init(ar, <span class="number">100000</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 72 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">1000</span> <span class="kw">do</span> top := push(top, i, SYSTEM.<span class="ty">ADR</span>(ar.base)); <span class="kw">end</span>; |
| 73 | Out.int(top^.val); Out.ln; |
| 74 | Arena.done(ar); <span class="comment">(* every node at once *)</span> |
| 75 | <span class="kw">end</span>; |
| 76 | |
| 77 | <span class="kw">end</span> Stack.</code></pre> |
| 78 | <pre class="output"><code>3 2 1 |
| 79 | 1000 |
| 80 | </code></pre> |
| 81 | <p>raz has no garbage collector, and no allocator of its own: memory comes from an allocator, a <code>Mem.Allocator</code>, which the program chooses and passes. <code>NEW(n, a)</code> sets <code>n</code> to a fresh, zeroed <code>Node</code> from <code>a</code>, or to <code>nil</code> if <code>a</code> has no room. <code>Heap.heap</code> is the general one, which <code>Mem.free</code> gives memory back to. An <code>Arena</code> hands out memory from one block and frees it all at once, as the last lines above do.</p> |
| 82 | <h2>Why an allocator is a parameter</h2> |
| 83 | <p>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.</p> |
| 84 | <div class="file"><span>Pool.raz</span></div><pre class="code"><code><span class="kw">module</span> Pool; |
| 85 | <span class="kw">import</span> Out, Mem, Arena; |
| 86 | <span class="kw">import</span> SYSTEM; |
| 87 | |
| 88 | <span class="kw">type</span> Item = <span class="kw">record</span> id: <span class="ty">INT</span>; next: ^Item; <span class="kw">end</span>; |
| 89 | |
| 90 | <span class="comment">(* n items, linked; or nil if the allocator ran dry (what came is lost with it) *)</span> |
| 91 | <span class="kw">proc</span> make(n: <span class="ty">INT</span>; a: ^Mem.Allocator): ^Item; |
| 92 | <span class="kw">var</span> head, it: ^Item; i: <span class="ty">INT</span>; |
| 93 | <span class="kw">begin</span> |
| 94 | head := <span class="ty">nil</span>; |
| 95 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> n <span class="kw">do</span> |
| 96 | <span class="ty">NEW</span>(it, a); |
| 97 | <span class="kw">if</span> it = <span class="ty">nil</span> <span class="kw">then</span> <span class="kw">return</span> <span class="ty">nil</span>; <span class="kw">end</span>; |
| 98 | it^.id := i; |
| 99 | it^.next := head; |
| 100 | head := it; |
| 101 | <span class="kw">end</span>; |
| 102 | <span class="kw">return</span> head; |
| 103 | <span class="kw">end</span>; |
| 104 | |
| 105 | <span class="kw">proc</span> count(it: ^Item): <span class="ty">INT</span>; |
| 106 | <span class="kw">var</span> n: <span class="ty">INT</span>; |
| 107 | <span class="kw">begin</span> |
| 108 | n := <span class="number">0</span>; |
| 109 | <span class="kw">while</span> it # <span class="ty">nil</span> <span class="kw">do</span> <span class="ty">INC</span>(n); it := it^.next; <span class="kw">end</span>; |
| 110 | <span class="kw">return</span> n; |
| 111 | <span class="kw">end</span>; |
| 112 | |
| 113 | <span class="kw">proc</span> main*; |
| 114 | <span class="kw">var</span> ar: Arena.Arena; small: Arena.Arena; list: ^Item; |
| 115 | <span class="kw">begin</span> |
| 116 | <span class="kw">if</span> <span class="kw">not</span> Arena.init(ar, <span class="number">1000000</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 117 | list := make(<span class="number">1000</span>, SYSTEM.<span class="ty">ADR</span>(ar.base)); |
| 118 | Out.int(count(list)); Out.str(<span class="string">" items from a big arena\n"</span>); |
| 119 | Arena.reset(ar); <span class="comment">(* every allocation forgotten *)</span> |
| 120 | list := make(<span class="number">10</span>, SYSTEM.<span class="ty">ADR</span>(ar.base)); |
| 121 | Out.int(count(list)); Out.str(<span class="string">" after a reset\n"</span>); |
| 122 | Arena.done(ar); |
| 123 | |
| 124 | <span class="kw">if</span> <span class="kw">not</span> Arena.init(small, <span class="number">4096</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 125 | list := make(<span class="number">1000</span>, SYSTEM.<span class="ty">ADR</span>(small.base)); |
| 126 | <span class="kw">if</span> list = <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="string">"a small arena ran out, and said so\n"</span>); <span class="kw">end</span>; |
| 127 | Arena.done(small); |
| 128 | <span class="kw">end</span>; |
| 129 | |
| 130 | <span class="kw">end</span> Pool.</code></pre> |
| 131 | <pre class="output"><code>1000 items from a big arena |
| 132 | 10 after a reset |
| 133 | a small arena ran out, and said so |
| 134 | </code></pre> |
| 135 | <p>The run-out case is an ordinary <code>nil</code> to test, not an exception that unwinds past your code.</p> |
| 136 | <h2>The heap</h2> |
| 137 | <p><code>Heap.heap</code> serves any size and takes memory back with <code>Mem.free</code>. 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.</p> |
| 138 | <div class="file"><span>Free.raz</span></div><pre class="code"><code><span class="kw">module</span> Free; |
| 139 | <span class="kw">import</span> Out, Mem, Heap; |
| 140 | <span class="kw">import</span> SYSTEM; |
| 141 | |
| 142 | <span class="kw">type</span> Cell = <span class="kw">record</span> val: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 143 | |
| 144 | <span class="kw">proc</span> main*; |
| 145 | <span class="kw">var</span> a, b: ^Cell; i, total: <span class="ty">INT</span>; |
| 146 | <span class="kw">begin</span> |
| 147 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span> |
| 148 | <span class="ty">NEW</span>(a, Heap.heap); |
| 149 | a^.val := i * i; |
| 150 | total := total + a^.val; |
| 151 | Mem.free(Heap.heap, ^<span class="ty">U8</span>(a)); <span class="comment">(* freed at once: its block is reused *)</span> |
| 152 | <span class="kw">end</span>; |
| 153 | <span class="ty">NEW</span>(b, Heap.heap); |
| 154 | Out.int(total); Out.char(<span class="string">' '</span>); Out.int(b^.val); Out.ln; <span class="comment">(* a new block is zeroed *)</span> |
| 155 | Mem.free(Heap.heap, ^<span class="ty">U8</span>(b)); |
| 156 | <span class="kw">end</span>; |
| 157 | |
| 158 | <span class="kw">end</span> Free.</code></pre> |
| 159 | <pre class="output"><code>55 0 |
| 160 | </code></pre> |
| 161 | <h2>Buffers that grow</h2> |
| 162 | <p><code>Buf</code> 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.</p> |
| 163 | <div class="file"><span>Build.raz</span></div><pre class="code"><code><span class="kw">module</span> Build; |
| 164 | <span class="kw">import</span> Out, Buf, Heap, Str; |
| 165 | |
| 166 | <span class="kw">proc</span> main*; |
| 167 | <span class="kw">var</span> b: Buf.Buf; i: <span class="ty">INT</span>; |
| 168 | <span class="kw">begin</span> |
| 169 | Buf.init(b, Heap.heap); |
| 170 | <span class="kw">defer</span> Buf.done(b); |
| 171 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span> |
| 172 | Buf.str(b, <span class="string">"item "</span>); |
| 173 | Buf.add(b, <span class="ty">U8</span>(<span class="string">'0'</span> + i)); |
| 174 | Buf.add(b, <span class="string">','</span>); |
| 175 | <span class="kw">end</span>; |
| 176 | Out.str(Buf.bytes(b)); Out.ln; |
| 177 | Out.int(b.len); Out.str(<span class="string">" bytes\n"</span>); |
| 178 | Buf.reset(b); |
| 179 | Str.join(b, [<span class="string">"red"</span>, <span class="string">"green"</span>, <span class="string">"blue"</span>], <span class="string">", "</span>); |
| 180 | Out.str(Buf.bytes(b)); Out.ln; |
| 181 | Buf.reset(b); |
| 182 | Str.replace(b, <span class="string">"a-b-c"</span>, <span class="string">"-"</span>, <span class="string">" and "</span>); |
| 183 | Out.str(Buf.bytes(b)); Out.ln; |
| 184 | <span class="kw">end</span>; |
| 185 | |
| 186 | <span class="kw">end</span> Build.</code></pre> |
| 187 | <pre class="output"><code>item 1,item 2,item 3,item 4,item 5, |
| 188 | 35 bytes |
| 189 | red, green, blue |
| 190 | a and b and c |
| 191 | </code></pre> |
| 192 | <p><code>Str.join</code> and <code>Str.replace</code> build into a <code>Buf</code> too: the parts with a separator between them, or a string with one text replaced by another wherever it is.</p> |
| 193 | <h2>Pages</h2> |
| 194 | <p>Under the allocators are pages from the system: <code>Mem.pages(size)</code> gives zeroed memory in whole pages, or <code>nil</code>, and <code>Mem.unmap</code> gives it back. Most programs never need to call it directly.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="variants.html">← Enumerations, sets, variants and match</a><a href="index.html">Contents</a><a class="next" href="errors.html">Errors →</a></div></article></div> |
| 195 | </main> |
| 196 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 197 | </body> |
| 198 | </html> |