raz / commit 947d70f3

commit
947d70f381a5b60b8541dce7a4622e3796ccf87b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0b79ce2b
Arena.mark and release; Vec.insert and remove: freeing back to a mark, and opening and closing a gap in the middle of a Vec

patch · log

9 files changed, +129 -2

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

@@ -92,6 +92,8 @@
9292 ...
9393 end;
9494 proc init*(var ar: Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *)
95+proc mark*(const ar: Arena): INT; (* where ar stands now, for release *)
96+proc release*(var ar: Arena; m: INT); (* all allocated since mark m freed; a mark past where it stands stops the program *)
9597 proc reset*(var ar: Arena); (* every allocation forgotten; the memory used again *)
9698 proc done*(var ar: Arena); (* its memory given back *)
9799 ```
@@ -804,6 +806,8 @@
804806 proc tryPush*(var v: Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *)
805807 proc tryAdd*(var v: Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *)
806808 proc resize*(var v: Vec; n: INT); (* n elements: cut short, or the new ones zero *)
809+proc insert*(var v: Vec; i, n: INT); (* n zero elements opened at i; stops the program if there is no room *)
810+proc remove*(var v: Vec; i, n: INT); (* the n elements from i taken out, those after moved down *)
807811 proc all*(const v: Vec): []T; (* the elements, until it next grows *)
808812 proc reset*(var v: Vec); (* emptied, its memory kept *)
809813 proc done*(var v: Vec); (* its memory given back *)

lib/Arena.raz (history) modified +21 -2

@@ -36,10 +36,24 @@
3636 return ar.buf # nil;
3737 end;
3838
39+(* Where ar stands now, for release. *)
40+proc mark*(const ar: Arena): INT;
41+begin
42+ return ar.used;
43+end;
44+
45+(* Everything allocated from ar since mark m freed at once; a mark past
46+ where it stands stops the program. *)
47+proc release*(var ar: Arena; m: INT);
48+begin
49+ if (m < 0) or (m > ar.used) then Rt.stop("Arena.release: mark past the arena's end"); end;
50+ ar.used := m;
51+end;
52+
3953 (* Forgets every allocation; the memory is used again. *)
4054 proc reset*(var ar: Arena);
4155 begin
42- ar.used := 0;
56+ release(ar, 0);
4357 end;
4458
4559 (* Gives the arena's block back to the system; what it handed out is
@@ -53,7 +67,7 @@
5367
5468 (* The module's test. *)
5569 proc main*;
56-var ar: Arena; a: ^Mem.Allocator; p, q: ^U8;
70+var ar: Arena; a: ^Mem.Allocator; p, q: ^U8; m: INT;
5771 begin
5872 if not init(ar, 4096) then Rt.stop("Arena.init"); end;
5973 a := ADR(ar.base);
@@ -64,6 +78,11 @@
6478 if Mem.alloc(a, -1, 1) # nil then Rt.stop("Arena: negative"); end;
6579 reset(ar);
6680 if Mem.alloc(a, 4096, 1) # p then Rt.stop("Arena.reset"); end;
81+ m := mark(ar);
82+ p := Mem.alloc(a, 100, 1);
83+ q := Mem.alloc(a, 100, 1);
84+ release(ar, m);
85+ if (mark(ar) # m) or (Mem.alloc(a, 100, 1) # p) then Rt.stop("Arena.release"); end;
6786 done(ar);
6887 end;
6988

lib/Vec.raz (history) modified +22 -0

@@ -115,6 +115,28 @@
115115 v.len := n;
116116 end;
117117
118+(* n zero elements opened at i, those from i on moved up; i from 0 to
119+ v.len. Stops the program, as push does, if there is no room. *)
120+proc insert*(var v: Vec; i, n: INT);
121+var zero: T; k: INT;
122+begin
123+ if (i < 0) or (i > v.len) or (n < 0) then Rt.stop("Vec.insert: out of range"); end;
124+ need(v, n);
125+ (* COPY goes the right way for the overlap *)
126+ COPY(v.data[i + n..v.len + n], v.data[i..v.len]);
127+ for k := i to i + n - 1 do v.data[k] := zero; end;
128+ v.len := v.len + n;
129+end;
130+
131+(* The n elements from i taken out, those after them moved down; i + n
132+ at most v.len. *)
133+proc remove*(var v: Vec; i, n: INT);
134+begin
135+ if (i < 0) or (n < 0) or (i + n > v.len) then Rt.stop("Vec.remove: out of range"); end;
136+ COPY(v.data[i..v.len - n], v.data[i + n..v.len]);
137+ v.len := v.len - n;
138+end;
139+
118140 (* The elements, until the array next grows. *)
119141 proc all*(const v: Vec): []T;
120142 var none: []T;

test/run/ArenaRelease.expect (history) added +0 -0

An empty file.

test/run/ArenaRelease.raz (history) added +16 -0

@@ -0,0 +1,16 @@
1+module ArenaRelease;
2+(* a mark from before the arena's present position is released; one past
3+ where the arena stands stops the program *)
4+import Arena, Mem, Rt;
5+
6+proc main*;
7+var ar: Arena.Arena; m: INT;
8+begin
9+ if not Arena.init(ar, 4096) then Rt.stop("no arena"); end;
10+ _ := Mem.alloc(ADR(ar.base), 10, 1);
11+ m := Arena.mark(ar);
12+ Arena.release(ar, 0);
13+ Arena.release(ar, m);
14+end;
15+
16+end ArenaRelease.

test/run/ArenaRelease.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/ArenaRelease.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+Arena.release: mark past the arena's end

test/run/VecEdit.expect (history) added +12 -0

@@ -0,0 +1,12 @@
1+0 0 1 2 3 4 5 (7)
2+0 0 1 2 3 4 5 0 (8)
3+0 0 1 0 0 2 3 4 5 0 (10)
4+0 0 1 0 0 2 3 4 5 0 (10)
5+0 0 1 8 9 2 3 4 5 0 (10)
6+1 8 9 2 3 4 5 0 (8)
7+1 3 4 5 0 (5)
8+1 3 4 5 (4)
9+1 3 4 5 (4)
10+(0)
11+0 0 0 (3)
12+1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (16)

test/run/VecEdit.raz (history) added +52 -0

@@ -0,0 +1,52 @@
1+module VecEdit;
2+(* Vec's insert and remove: at the start, the middle and the end, n of 0,
3+ growth past the first block, and removing everything. *)
4+import Fmt, Heap, Rt, Ints := Vec(INT);
5+
6+(* Says the elements of v on one line. *)
7+proc show(const v: Ints.Vec);
8+var s: []INT; i: INT;
9+begin
10+ s := Ints.all(v);
11+ for i := 0 to LEN(s) - 1 do Fmt.print("{} ", [Fmt.int(s[i])]); end;
12+ Fmt.print("({})\n", [Fmt.int(v.len)]);
13+end;
14+
15+(* Fills v with 1 to n. *)
16+proc fill(var v: Ints.Vec; n: INT);
17+var i: INT;
18+begin
19+ Ints.reset(v);
20+ for i := 1 to n do Ints.push(v, i); end;
21+end;
22+
23+proc main*;
24+var v: Ints.Vec; i: INT; s: []INT;
25+begin
26+ Ints.init(v, Heap.heap);
27+ fill(v, 5);
28+ Ints.insert(v, 0, 2); show(v);
29+ Ints.insert(v, 7, 1); show(v);
30+ Ints.insert(v, 3, 2); show(v);
31+ Ints.insert(v, 3, 0); show(v);
32+ s := Ints.all(v);
33+ s[3] := 8; s[4] := 9;
34+ show(v);
35+ Ints.remove(v, 0, 2); show(v);
36+ Ints.remove(v, 1, 3); show(v);
37+ Ints.remove(v, v.len - 1, 1); show(v);
38+ Ints.remove(v, 1, 0); show(v);
39+ Ints.remove(v, 0, v.len); show(v);
40+ Ints.insert(v, 0, 3); show(v);
41+ fill(v, 16);
42+ Ints.insert(v, 8, 100);
43+ s := Ints.all(v);
44+ if (v.len # 116) or (s[7] # 8) or (s[8] # 0) or (s[107] # 0) or (s[108] # 9) or (s[115] # 16) then
45+ Rt.stop("VecEdit: growth");
46+ end;
47+ for i := 1 to 100 do Ints.remove(v, 8, 1); end;
48+ show(v);
49+ Ints.done(v);
50+end;
51+
52+end VecEdit.