| 1 | (* A growable byte buffer, its memory from an allocator. *) |
| 2 | module Buf; |
| 3 | import Mem, Arena, Rt, Sys; |
| 4 | import SYSTEM; |
| 5 | |
| 6 | type |
| 7 | Buf* = record |
| 8 | a: ^Mem.Allocator; |
| 9 | data: ^U8; |
| 10 | len*, cap: INT; |
| 11 | end; |
| 12 | |
| 13 | (* An empty buffer, which takes its memory from a. *) |
| 14 | proc init*(var b: Buf; a: ^Mem.Allocator); |
| 15 | begin |
| 16 | b := {a: a}; |
| 17 | end; |
| 18 | |
| 19 | (* Room for n more bytes; false if the allocator has none. *) |
| 20 | proc room*(var b: Buf; n: INT): BOOL; |
| 21 | var cap: INT; p: ^U8; |
| 22 | begin |
| 23 | if b.len + n <= b.cap then return true; end; |
| 24 | cap := b.cap * 2; |
| 25 | if cap < 64 then cap := 64; end; |
| 26 | while cap < b.len + n do cap := cap * 2; end; |
| 27 | if b.a = nil then |
| 28 | (* a zero Buf has no allocator: the core has no heap to assume, |
| 29 | so init must give it one. Said plainly, with the calls under |
| 30 | way, rather than as a nil pointer in Mem.alloc *) |
| 31 | _ := Sys.write(Sys.stderr, "Buf used before Buf.init\n"); |
| 32 | Rt.trace; |
| 33 | Rt.exit(2); |
| 34 | end; |
| 35 | p := Mem.alloc(b.a, cap, 1); |
| 36 | if p = nil then return false; end; |
| 37 | if b.len > 0 then COPY(p[0..b.len], b.data[0..b.len]); end; |
| 38 | if b.data # nil then Mem.free(b.a, b.data); end; |
| 39 | b.data := p; |
| 40 | b.cap := cap; |
| 41 | return true; |
| 42 | end; |
| 43 | |
| 44 | (* Room for n more bytes, or the program stops: a buffer that went on |
| 45 | short of what was added to it would be wrong data, and nothing would |
| 46 | say so. A program that can do better asks room first. *) |
| 47 | proc need(var b: Buf; n: INT); |
| 48 | begin |
| 49 | if not room(b, n) then |
| 50 | _ := Sys.write(Sys.stderr, "Buf: out of memory\n"); |
| 51 | Rt.trace; |
| 52 | Rt.exit(2); |
| 53 | end; |
| 54 | end; |
| 55 | |
| 56 | (* Adds the byte c at the end. *) |
| 57 | proc add*(var b: Buf; c: U8); |
| 58 | begin |
| 59 | need(b, 1); |
| 60 | b.data[b.len] := c; |
| 61 | INC(b.len); |
| 62 | end; |
| 63 | |
| 64 | (* Adds the bytes of s at the end. *) |
| 65 | proc str*(var b: Buf; s: []const U8); |
| 66 | begin |
| 67 | need(b, LEN(s)); |
| 68 | COPY(b.data[b.len..b.len + LEN(s)], s); |
| 69 | INC(b.len, LEN(s)); |
| 70 | end; |
| 71 | |
| 72 | (* Room for n more bytes, and those bytes, for a read to fill: the |
| 73 | program then adds to len what it filled. Good until the buffer next |
| 74 | grows, as bytes is. *) |
| 75 | proc spare*(var b: Buf; n: INT): []U8; |
| 76 | begin |
| 77 | if n = 0 then return []; end; (* a new buffer has no data to slice *) |
| 78 | need(b, n); |
| 79 | return b.data[b.len..b.len + n]; |
| 80 | end; |
| 81 | |
| 82 | (* What the buffer holds, until it next grows. *) |
| 83 | proc bytes*(const b: Buf): []U8; |
| 84 | begin |
| 85 | if b.len = 0 then return []; end; |
| 86 | return b.data[0..b.len]; |
| 87 | end; |
| 88 | |
| 89 | (* Keeps only the first n bytes, and the memory, for what comes next. |
| 90 | An n past what the buffer holds is the program's mistake, and stops |
| 91 | it, as an index past the end does. *) |
| 92 | proc cut*(var b: Buf; n: INT); |
| 93 | begin |
| 94 | if (n < 0) or (n > b.len) then |
| 95 | _ := Sys.write(Sys.stderr, "Buf.cut past the end\n"); |
| 96 | Rt.trace; |
| 97 | Rt.exit(2); |
| 98 | end; |
| 99 | b.len := n; |
| 100 | end; |
| 101 | |
| 102 | (* Empties the buffer, keeping its memory for what comes next. *) |
| 103 | proc reset*(var b: Buf); |
| 104 | begin |
| 105 | cut(b, 0); |
| 106 | end; |
| 107 | |
| 108 | (* Gives the buffer's memory back, leaving it empty. *) |
| 109 | proc done*(var b: Buf); |
| 110 | begin |
| 111 | if b.data # nil then Mem.free(b.a, b.data); end; |
| 112 | b.data := nil; |
| 113 | b.len := 0; |
| 114 | b.cap := 0; |
| 115 | end; |
| 116 | |
| 117 | (* The module's test, over an arena. *) |
| 118 | proc main*; |
| 119 | var ar: Arena.Arena; b: Buf; i: INT; s: []U8; |
| 120 | begin |
| 121 | if not Arena.init(ar, 1 shl 20) then Rt.stop("Buf: no arena"); end; |
| 122 | init(b, SYSTEM.ADR(ar.base)); |
| 123 | if LEN(bytes(b)) # 0 then Rt.stop("Buf: not empty"); end; |
| 124 | for i := 0 to 9999 do add(b, U8(i mod 251)); end; |
| 125 | str(b, "end"); |
| 126 | s := bytes(b); |
| 127 | if (LEN(s) # 10003) or (s[9998] # U8(9998 mod 251)) or (s[10000..10003] # "end") then |
| 128 | Rt.stop("Buf: contents"); |
| 129 | end; |
| 130 | reset(b); |
| 131 | str(b, "again"); |
| 132 | if bytes(b) # "again" then Rt.stop("Buf.reset"); end; |
| 133 | cut(b, 2); |
| 134 | str(b, "ain"); |
| 135 | cut(b, 5); |
| 136 | if bytes(b) # "again" then Rt.stop("Buf.cut"); end; |
| 137 | s := spare(b, 3); |
| 138 | COPY(s, "abc"); |
| 139 | INC(b.len, 2); |
| 140 | if (LEN(s) # 3) or (bytes(b) # "againab") then Rt.stop("Buf.spare"); end; |
| 141 | done(b); |
| 142 | Arena.done(ar); |
| 143 | end; |
| 144 | |
| 145 | end Buf. |