From 947d70f381a5b60b8541dce7a4622e3796ccf87b Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:46:29 +0100 Subject: [PATCH] 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 --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 4877ab3..c82f8e7 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -92,6 +92,8 @@ ... end; proc init*(var ar: Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *) +proc mark*(const ar: Arena): INT; (* where ar stands now, for release *) +proc release*(var ar: Arena; m: INT); (* all allocated since mark m freed; a mark past where it stands stops the program *) proc reset*(var ar: Arena); (* every allocation forgotten; the memory used again *) proc done*(var ar: Arena); (* its memory given back *) ``` @@ -804,6 +806,8 @@ proc tryPush*(var v: Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) proc tryAdd*(var v: Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) proc resize*(var v: Vec; n: INT); (* n elements: cut short, or the new ones zero *) +proc insert*(var v: Vec; i, n: INT); (* n zero elements opened at i; stops the program if there is no room *) +proc remove*(var v: Vec; i, n: INT); (* the n elements from i taken out, those after moved down *) proc all*(const v: Vec): []T; (* the elements, until it next grows *) proc reset*(var v: Vec); (* emptied, its memory kept *) proc done*(var v: Vec); (* its memory given back *) diff --git a/lib/Arena.raz b/lib/Arena.raz index ae2504c..5bd19e4 100644 --- a/lib/Arena.raz +++ b/lib/Arena.raz @@ -36,10 +36,24 @@ return ar.buf # nil; end; +(* Where ar stands now, for release. *) +proc mark*(const ar: Arena): INT; +begin + return ar.used; +end; + +(* Everything allocated from ar since mark m freed at once; a mark past + where it stands stops the program. *) +proc release*(var ar: Arena; m: INT); +begin + if (m < 0) or (m > ar.used) then Rt.stop("Arena.release: mark past the arena's end"); end; + ar.used := m; +end; + (* Forgets every allocation; the memory is used again. *) proc reset*(var ar: Arena); begin - ar.used := 0; + release(ar, 0); end; (* Gives the arena's block back to the system; what it handed out is @@ -53,7 +67,7 @@ (* The module's test. *) proc main*; -var ar: Arena; a: ^Mem.Allocator; p, q: ^U8; +var ar: Arena; a: ^Mem.Allocator; p, q: ^U8; m: INT; begin if not init(ar, 4096) then Rt.stop("Arena.init"); end; a := ADR(ar.base); @@ -64,6 +78,11 @@ if Mem.alloc(a, -1, 1) # nil then Rt.stop("Arena: negative"); end; reset(ar); if Mem.alloc(a, 4096, 1) # p then Rt.stop("Arena.reset"); end; + m := mark(ar); + p := Mem.alloc(a, 100, 1); + q := Mem.alloc(a, 100, 1); + release(ar, m); + if (mark(ar) # m) or (Mem.alloc(a, 100, 1) # p) then Rt.stop("Arena.release"); end; done(ar); end; diff --git a/lib/Vec.raz b/lib/Vec.raz index c79eb1e..65fcf66 100644 --- a/lib/Vec.raz +++ b/lib/Vec.raz @@ -115,6 +115,28 @@ v.len := n; end; +(* n zero elements opened at i, those from i on moved up; i from 0 to + v.len. Stops the program, as push does, if there is no room. *) +proc insert*(var v: Vec; i, n: INT); +var zero: T; k: INT; +begin + if (i < 0) or (i > v.len) or (n < 0) then Rt.stop("Vec.insert: out of range"); end; + need(v, n); + (* COPY goes the right way for the overlap *) + COPY(v.data[i + n..v.len + n], v.data[i..v.len]); + for k := i to i + n - 1 do v.data[k] := zero; end; + v.len := v.len + n; +end; + +(* The n elements from i taken out, those after them moved down; i + n + at most v.len. *) +proc remove*(var v: Vec; i, n: INT); +begin + if (i < 0) or (n < 0) or (i + n > v.len) then Rt.stop("Vec.remove: out of range"); end; + COPY(v.data[i..v.len - n], v.data[i + n..v.len]); + v.len := v.len - n; +end; + (* The elements, until the array next grows. *) proc all*(const v: Vec): []T; var none: []T; diff --git a/test/run/ArenaRelease.expect b/test/run/ArenaRelease.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/ArenaRelease.raz b/test/run/ArenaRelease.raz new file mode 100644 index 0000000..b80d676 --- /dev/null +++ b/test/run/ArenaRelease.raz @@ -0,0 +1,16 @@ +module ArenaRelease; +(* a mark from before the arena's present position is released; one past + where the arena stands stops the program *) +import Arena, Mem, Rt; + +proc main*; +var ar: Arena.Arena; m: INT; +begin + if not Arena.init(ar, 4096) then Rt.stop("no arena"); end; + _ := Mem.alloc(ADR(ar.base), 10, 1); + m := Arena.mark(ar); + Arena.release(ar, 0); + Arena.release(ar, m); +end; + +end ArenaRelease. diff --git a/test/run/ArenaRelease.status b/test/run/ArenaRelease.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/ArenaRelease.status @@ -0,0 +1 @@ +2 diff --git a/test/run/ArenaRelease.stderr b/test/run/ArenaRelease.stderr new file mode 100644 index 0000000..fb11108 --- /dev/null +++ b/test/run/ArenaRelease.stderr @@ -0,0 +1 @@ +Arena.release: mark past the arena's end diff --git a/test/run/VecEdit.expect b/test/run/VecEdit.expect new file mode 100644 index 0000000..33c67da --- /dev/null +++ b/test/run/VecEdit.expect @@ -0,0 +1,12 @@ +0 0 1 2 3 4 5 (7) +0 0 1 2 3 4 5 0 (8) +0 0 1 0 0 2 3 4 5 0 (10) +0 0 1 0 0 2 3 4 5 0 (10) +0 0 1 8 9 2 3 4 5 0 (10) +1 8 9 2 3 4 5 0 (8) +1 3 4 5 0 (5) +1 3 4 5 (4) +1 3 4 5 (4) +(0) +0 0 0 (3) +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (16) diff --git a/test/run/VecEdit.raz b/test/run/VecEdit.raz new file mode 100644 index 0000000..5fefdbe --- /dev/null +++ b/test/run/VecEdit.raz @@ -0,0 +1,52 @@ +module VecEdit; +(* Vec's insert and remove: at the start, the middle and the end, n of 0, + growth past the first block, and removing everything. *) +import Fmt, Heap, Rt, Ints := Vec(INT); + +(* Says the elements of v on one line. *) +proc show(const v: Ints.Vec); +var s: []INT; i: INT; +begin + s := Ints.all(v); + for i := 0 to LEN(s) - 1 do Fmt.print("{} ", [Fmt.int(s[i])]); end; + Fmt.print("({})\n", [Fmt.int(v.len)]); +end; + +(* Fills v with 1 to n. *) +proc fill(var v: Ints.Vec; n: INT); +var i: INT; +begin + Ints.reset(v); + for i := 1 to n do Ints.push(v, i); end; +end; + +proc main*; +var v: Ints.Vec; i: INT; s: []INT; +begin + Ints.init(v, Heap.heap); + fill(v, 5); + Ints.insert(v, 0, 2); show(v); + Ints.insert(v, 7, 1); show(v); + Ints.insert(v, 3, 2); show(v); + Ints.insert(v, 3, 0); show(v); + s := Ints.all(v); + s[3] := 8; s[4] := 9; + show(v); + Ints.remove(v, 0, 2); show(v); + Ints.remove(v, 1, 3); show(v); + Ints.remove(v, v.len - 1, 1); show(v); + Ints.remove(v, 1, 0); show(v); + Ints.remove(v, 0, v.len); show(v); + Ints.insert(v, 0, 3); show(v); + fill(v, 16); + Ints.insert(v, 8, 100); + s := Ints.all(v); + 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 + Rt.stop("VecEdit: growth"); + end; + for i := 1 to 100 do Ints.remove(v, 8, 1); end; + show(v); + Ints.done(v); +end; + +end VecEdit.