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
| @@ -92,6 +92,8 @@ | ||
| 92 | 92 | ... |
| 93 | 93 | end; |
| 94 | 94 | 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 *) | |
| 95 | 97 | proc reset*(var ar: Arena); (* every allocation forgotten; the memory used again *) |
| 96 | 98 | proc done*(var ar: Arena); (* its memory given back *) |
| 97 | 99 | ``` |
| @@ -804,6 +806,8 @@ | ||
| 804 | 806 | proc tryPush*(var v: Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) |
| 805 | 807 | proc tryAdd*(var v: Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) |
| 806 | 808 | 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 *) | |
| 807 | 811 | proc all*(const v: Vec): []T; (* the elements, until it next grows *) |
| 808 | 812 | proc reset*(var v: Vec); (* emptied, its memory kept *) |
| 809 | 813 | proc done*(var v: Vec); (* its memory given back *) |
| @@ -36,10 +36,24 @@ | ||
| 36 | 36 | return ar.buf # nil; |
| 37 | 37 | end; |
| 38 | 38 | |
| 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 | + | |
| 39 | 53 | (* Forgets every allocation; the memory is used again. *) |
| 40 | 54 | proc reset*(var ar: Arena); |
| 41 | 55 | begin |
| 42 | - ar.used := 0; | |
| 56 | + release(ar, 0); | |
| 43 | 57 | end; |
| 44 | 58 | |
| 45 | 59 | (* Gives the arena's block back to the system; what it handed out is |
| @@ -53,7 +67,7 @@ | ||
| 53 | 67 | |
| 54 | 68 | (* The module's test. *) |
| 55 | 69 | proc main*; |
| 56 | -var ar: Arena; a: ^Mem.Allocator; p, q: ^U8; | |
| 70 | +var ar: Arena; a: ^Mem.Allocator; p, q: ^U8; m: INT; | |
| 57 | 71 | begin |
| 58 | 72 | if not init(ar, 4096) then Rt.stop("Arena.init"); end; |
| 59 | 73 | a := ADR(ar.base); |
| @@ -64,6 +78,11 @@ | ||
| 64 | 78 | if Mem.alloc(a, -1, 1) # nil then Rt.stop("Arena: negative"); end; |
| 65 | 79 | reset(ar); |
| 66 | 80 | 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; | |
| 67 | 86 | done(ar); |
| 68 | 87 | end; |
| 69 | 88 |
| @@ -115,6 +115,28 @@ | ||
| 115 | 115 | v.len := n; |
| 116 | 116 | end; |
| 117 | 117 | |
| 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 | + | |
| 118 | 140 | (* The elements, until the array next grows. *) |
| 119 | 141 | proc all*(const v: Vec): []T; |
| 120 | 142 | var none: []T; |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +Arena.release: mark past the arena's end |
| @@ -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) |
| @@ -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. |