raz / commit 821f83e6

commit
821f83e6e7c441cec39e4c315704b459ca2bd335
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
3fee2db6
Arena takes the arena by var (report 2026-10.33)

Arena.init, reset and done take var ar: Arena; none kept the pointer.
The allocator an arena gives, ADR(ar.base), stays a pointer, as NEW and
Buf.init take one. Callers, the tutorial's and volume 2 say so. The seed
compiles Arena, which Buf imports; the fixpoint holds.

patch · log

11 files changed, +40 -40

doc/raz-lib.md (history) modified +3 -3

@@ -78,9 +78,9 @@
7878 base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *)
7979 ...
8080 end;
81-proc init*(ar: ^Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *)
82-proc reset*(ar: ^Arena); (* every allocation forgotten; the memory used again *)
83-proc done*(ar: ^Arena); (* its memory given back *)
81+proc init*(var ar: Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *)
82+proc reset*(var ar: Arena); (* every allocation forgotten; the memory used again *)
83+proc done*(var ar: Arena); (* its memory given back *)
8484 ```
8585
8686 `NEW(p, a)` calls `a^.alloc`. `Heap` is the general

doc/raz-tutorial.7 (history) modified +2 -2

@@ -686,10 +686,10 @@
686686 Mem.free(Heap.heap, ^U8(n));
687687 end;
688688 Out.ln;
689- if not Arena.init(ADR(ar), 100000) then return; end;
689+ if not Arena.init(ar, 100000) then return; end;
690690 for i := 1 to 1000 do top := push(top, i, ADR(ar.base)); end;
691691 Out.int(top^.val); Out.ln;
692- Arena.done(ADR(ar)); (* every node at once *)
692+ Arena.done(ar); (* every node at once *)
693693 end;
694694
695695 end Stack.

examples/Sh.raz (history) modified +2 -2

@@ -302,7 +302,7 @@
302302 proc run(script: []U8);
303303 var p: Pipeline; t: Token; ok: BOOL;
304304 begin
305- Arena.reset(ADR(arena));
305+ Arena.reset(arena);
306306 s := script;
307307 at := 0;
308308 repeat
@@ -361,7 +361,7 @@
361361 proc main*: INT;
362362 var p: Opt.Parser; script, file: []U8; given, found: BOOL; b: Buf.Buf;
363363 begin
364- if not Arena.init(ADR(arena), 1 shl 24) then Rt.stop("Sh: no memory"); end;
364+ if not Arena.init(arena, 1 shl 24) then Rt.stop("Sh: no memory"); end;
365365 Opt.init(ADR(p), "Sh", [
366366 Opt.usage("", "Sh [-c script | file]"),
367367 Opt.option("", 'c', "command", "script", "run script, not standard input")]);

lib/Arena.raz (history) modified +16 -16

@@ -26,45 +26,45 @@
2626 end;
2727
2828 (* An arena of size bytes; false if the system has none. *)
29-proc init*(ar: ^Arena; size: INT): BOOL;
29+proc init*(var ar: Arena; size: INT): BOOL;
3030 begin
31- ar^.base := {alloc: alloc, free: free};
32- ar^.buf := Mem.pages(size);
33- ar^.used := 0;
34- ar^.cap := size;
35- if ar^.buf = nil then ar^.cap := 0; end;
36- return ar^.buf # nil;
31+ ar.base := {alloc: alloc, free: free};
32+ ar.buf := Mem.pages(size);
33+ ar.used := 0;
34+ ar.cap := size;
35+ if ar.buf = nil then ar.cap := 0; end;
36+ return ar.buf # nil;
3737 end;
3838
3939 (* Forgets every allocation; the memory is used again. *)
40-proc reset*(ar: ^Arena);
40+proc reset*(var ar: Arena);
4141 begin
42- ar^.used := 0;
42+ ar.used := 0;
4343 end;
4444
4545 (* Gives the arena's block back to the system; what it handed out is
4646 gone with it. *)
47-proc done*(ar: ^Arena);
47+proc done*(var ar: Arena);
4848 begin
49- if ar^.buf # nil then Mem.unmap(ar^.buf, ar^.cap); end;
50- ar^.buf := nil;
51- ar^.cap := 0;
49+ if ar.buf # nil then Mem.unmap(ar.buf, ar.cap); end;
50+ ar.buf := nil;
51+ ar.cap := 0;
5252 end;
5353
5454 (* The module's test. *)
5555 proc main*;
5656 var ar: Arena; a: ^Mem.Allocator; p, q: ^U8;
5757 begin
58- if not init(ADR(ar), 4096) then Rt.stop("Arena.init"); end;
58+ if not init(ar, 4096) then Rt.stop("Arena.init"); end;
5959 a := ADR(ar.base);
6060 p := Mem.alloc(a, 3, 1);
6161 q := Mem.alloc(a, 8, 8);
6262 if (p = nil) or (q # p + 8) then Rt.stop("Arena: alignment"); end;
6363 if Mem.alloc(a, 5000, 1) # nil then Rt.stop("Arena: too big"); end;
6464 if Mem.alloc(a, -1, 1) # nil then Rt.stop("Arena: negative"); end;
65- reset(ADR(ar));
65+ reset(ar);
6666 if Mem.alloc(a, 4096, 1) # p then Rt.stop("Arena.reset"); end;
67- done(ADR(ar));
67+ done(ar);
6868 end;
6969
7070 end Arena.

lib/Buf.raz (history) modified +2 -2

@@ -117,7 +117,7 @@
117117 proc main*;
118118 var ar: Arena.Arena; b: Buf; i: INT; s: []U8;
119119 begin
120- if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Buf: no arena"); end;
120+ if not Arena.init(ar, 1 shl 20) then Rt.stop("Buf: no arena"); end;
121121 init(b, ADR(ar.base));
122122 if LEN(bytes(b)) # 0 then Rt.stop("Buf: not empty"); end;
123123 for i := 0 to 9999 do add(b, U8(i mod 251)); end;
@@ -138,7 +138,7 @@
138138 INC(b.len, 2);
139139 if (LEN(s) # 3) or (bytes(b) # "againab") then Rt.stop("Buf.spare"); end;
140140 done(b);
141- Arena.done(ADR(ar));
141+ Arena.done(ar);
142142 end;
143143
144144 end Buf.

lib/Fmt.raz (history) modified +4 -4

@@ -134,7 +134,7 @@
134134 var ar: Arena.Arena; b: Buf.Buf; s: []U8; n: INT;
135135 begin
136136 Out.flush;
137- if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end;
137+ if not Arena.init(ar, 1 shl 20) then Rt.stop("Fmt.eprint: no arena"); end;
138138 Buf.init(b, ADR(ar.base));
139139 format(ADR(b), f, args);
140140 s := Buf.bytes(b);
@@ -144,7 +144,7 @@
144144 elsif n # Sys.errIntr then break; (* a signal broke in: try again *)
145145 end;
146146 end;
147- Arena.done(ADR(ar));
147+ Arena.done(ar);
148148 end;
149149
150150 (* Appends f to b, each {} replaced by the next of args. *)
@@ -157,7 +157,7 @@
157157 proc main*;
158158 var ar: Arena.Arena; b: Buf.Buf; zero: F64;
159159 begin
160- if not Arena.init(ADR(ar), 1 shl 16) then Rt.stop("Fmt: no arena"); end;
160+ if not Arena.init(ar, 1 shl 16) then Rt.stop("Fmt: no arena"); end;
161161 Buf.init(b, ADR(ar.base));
162162 write(b, "{} {} {} {}|{}{}{} {{}}", [int(-42), uint(0FFFFFFFFFFFFFFFFH), hex(48879),
163163 str("s"), char('c'), bool(true), int(0)]);
@@ -173,7 +173,7 @@
173173 write(b, "{} {} {} {} {} {}", [real(1.5E17, 2), real(-9.999E20, 2), real(1.0E15, 0), real(1.7976931348623157E308, 3),
174174 real(1.0 / zero, 2), real(zero / zero, 2)]);
175175 if Buf.bytes(b) # "1.50e17 -1.00e21 1e15 1.798e308 inf nan" then Rt.stop("Fmt.real: an exponent"); end;
176- Arena.done(ADR(ar));
176+ Arena.done(ar);
177177 end;
178178
179179 end Fmt.

site/content/tour/09-memory.txt (history) modified +5 -5

@@ -43,18 +43,18 @@
4343 proc main*;
4444 var ar: Arena.Arena; small: Arena.Arena; list: ^Item;
4545 begin
46- if not Arena.init(ADR(ar), 1000000) then return; end;
46+ if not Arena.init(ar, 1000000) then return; end;
4747 list := make(1000, ADR(ar.base));
4848 Out.int(count(list)); Out.str(" items from a big arena\n");
49- Arena.reset(ADR(ar)); (* every allocation forgotten *)
49+ Arena.reset(ar); (* every allocation forgotten *)
5050 list := make(10, ADR(ar.base));
5151 Out.int(count(list)); Out.str(" after a reset\n");
52- Arena.done(ADR(ar));
52+ Arena.done(ar);
5353
54- if not Arena.init(ADR(small), 4096) then return; end;
54+ if not Arena.init(small, 4096) then return; end;
5555 list := make(1000, ADR(small.base));
5656 if list = nil then Out.str("a small arena ran out, and said so\n"); end;
57- Arena.done(ADR(small));
57+ Arena.done(small);
5858 end;
5959
6060 end Pool.

test/run/Alloc.raz (history) modified +3 -3

@@ -51,16 +51,16 @@
5151 b^.data[999] := 7;
5252 Out.int(b^.data[999] + b^.data[0]); Out.ln;
5353 Mem.free(Heap.heap, ^U8(b));
54- if not Arena.init(ADR(ar), 8192) then Out.str("no arena"); end;
54+ if not Arena.init(ar, 8192) then Out.str("no arena"); end;
5555 l := build(ADR(ar.base), 100);
5656 Out.int(sum(l)); Out.ln;
5757 l := build(ADR(ar.base), 1000);
5858 if l = nil then Out.str("nil "); end;
59- Arena.reset(ADR(ar));
59+ Arena.reset(ar);
6060 NEW(again, ADR(ar.base));
6161 if again # nil then Out.str("again "); Out.int(again^.val); end;
6262 Out.ln;
63- Arena.done(ADR(ar));
63+ Arena.done(ar);
6464 end;
6565
6666 end Alloc.

test/run/BufFull.raz (history) modified +1 -1

@@ -6,7 +6,7 @@
66 proc main*;
77 var ar: Arena.Arena; b: Buf.Buf; big: [10000]U8;
88 begin
9- if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end;
9+ if not Arena.init(ar, 4096) then Rt.stop("no arena"); end;
1010 Buf.init(b, ADR(ar.base));
1111 Buf.str(b, big);
1212 end;

test/run/VecFull.raz (history) modified +1 -1

@@ -6,7 +6,7 @@
66 proc main*;
77 var ar: Arena.Arena; v: Ints.Vec; i: INT;
88 begin
9- if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end;
9+ if not Arena.init(ar, 4096) then Rt.stop("no arena"); end;
1010 Ints.init(ADR(v), ADR(ar.base));
1111 for i := 1 to 10000 do Ints.push(ADR(v), i); end;
1212 end;

test/run/VecTry.raz (history) modified +1 -1

@@ -6,7 +6,7 @@
66 proc main*;
77 var ar: Arena.Arena; v: Ints.Vec; n: INT; p: ^INT; ok: BOOL;
88 begin
9- if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end;
9+ if not Arena.init(ar, 4096) then Rt.stop("no arena"); end;
1010 Ints.init(ADR(v), ADR(ar.base));
1111 n := 0;
1212 while Ints.tryPush(ADR(v), n) do INC(n); end;