(* A growable byte buffer, its memory from an allocator. *) module Buf; import Mem, Arena, Rt, Sys; import SYSTEM; type Buf* = record a: ^Mem.Allocator; data: ^U8; len*, cap: INT; end; (* An empty buffer, which takes its memory from a. *) proc init*(var b: Buf; a: ^Mem.Allocator); begin b := {a: a}; end; (* Room for n more bytes; false if the allocator has none. *) proc room*(var b: Buf; n: INT): BOOL; var cap: INT; p: ^U8; begin if b.len + n <= b.cap then return true; end; cap := b.cap * 2; if cap < 64 then cap := 64; end; while cap < b.len + n do cap := cap * 2; end; if b.a = nil then (* a zero Buf has no allocator: the core has no heap to assume, so init must give it one. Said plainly, with the calls under way, rather than as a nil pointer in Mem.alloc *) _ := Sys.write(Sys.stderr, "Buf used before Buf.init\n"); Rt.trace; Rt.exit(2); end; p := Mem.alloc(b.a, cap, 1); if p = nil then return false; end; if b.len > 0 then COPY(p[0..b.len], b.data[0..b.len]); end; if b.data # nil then Mem.free(b.a, b.data); end; b.data := p; b.cap := cap; return true; end; (* Room for n more bytes, or the program stops: a buffer that went on short of what was added to it would be wrong data, and nothing would say so. A program that can do better asks room first. *) proc need(var b: Buf; n: INT); begin if not room(b, n) then _ := Sys.write(Sys.stderr, "Buf: out of memory\n"); Rt.trace; Rt.exit(2); end; end; (* Adds the byte c at the end. *) proc add*(var b: Buf; c: U8); begin need(b, 1); b.data[b.len] := c; INC(b.len); end; (* Adds the bytes of s at the end. *) proc str*(var b: Buf; s: []const U8); begin need(b, LEN(s)); COPY(b.data[b.len..b.len + LEN(s)], s); INC(b.len, LEN(s)); end; (* Room for n more bytes, and those bytes, for a read to fill: the program then adds to len what it filled. Good until the buffer next grows, as bytes is. *) proc spare*(var b: Buf; n: INT): []U8; begin if n = 0 then return []; end; (* a new buffer has no data to slice *) need(b, n); return b.data[b.len..b.len + n]; end; (* What the buffer holds, until it next grows. *) proc bytes*(const b: Buf): []U8; begin if b.len = 0 then return []; end; return b.data[0..b.len]; end; (* Keeps only the first n bytes, and the memory, for what comes next. An n past what the buffer holds is the program's mistake, and stops it, as an index past the end does. *) proc cut*(var b: Buf; n: INT); begin if (n < 0) or (n > b.len) then _ := Sys.write(Sys.stderr, "Buf.cut past the end\n"); Rt.trace; Rt.exit(2); end; b.len := n; end; (* Empties the buffer, keeping its memory for what comes next. *) proc reset*(var b: Buf); begin cut(b, 0); end; (* Gives the buffer's memory back, leaving it empty. *) proc done*(var b: Buf); begin if b.data # nil then Mem.free(b.a, b.data); end; b.data := nil; b.len := 0; b.cap := 0; end; (* The module's test, over an arena. *) proc main*; var ar: Arena.Arena; b: Buf; i: INT; s: []U8; begin if not Arena.init(ar, 1 shl 20) then Rt.stop("Buf: no arena"); end; init(b, SYSTEM.ADR(ar.base)); if LEN(bytes(b)) # 0 then Rt.stop("Buf: not empty"); end; for i := 0 to 9999 do add(b, U8(i mod 251)); end; str(b, "end"); s := bytes(b); if (LEN(s) # 10003) or (s[9998] # U8(9998 mod 251)) or (s[10000..10003] # "end") then Rt.stop("Buf: contents"); end; reset(b); str(b, "again"); if bytes(b) # "again" then Rt.stop("Buf.reset"); end; cut(b, 2); str(b, "ain"); cut(b, 5); if bytes(b) # "again" then Rt.stop("Buf.cut"); end; s := spare(b, 3); COPY(s, "abc"); INC(b.len, 2); if (LEN(s) # 3) or (bytes(b) # "againab") then Rt.stop("Buf.spare"); end; done(b); Arena.done(ar); end; end Buf.