raz / commit 9b4fd42f

commit
9b4fd42f81f14c77a204a83486d9d8f077aebb3a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
73d8ebbe
Buf: out of memory stops; Buf.spare to read into

add and str dropped bytes silently when the allocator ran out, a
corrupt buffer nobody hears of; now they stop with "Buf: out of
memory", the calls under way, status 2, as a failed check does.
room still asks first. Breaks no program whose memory suffices.

spare(b, n) gives room for n more bytes and those bytes, for
Sys.read to fill, then INC(b^.len, n). Files.read uses it: one copy
of each byte, not two. It stops too when there's no room, not "",
which a read would take for the end of the file.

From PROPOSALS.md; test/run/BufFull, BufInit's trace through need.

patch · log

9 files changed, +64 -69

PROPOSALS.md modified +0 -52

@@ -82,58 +82,6 @@
8282 the budget; and, across modules, the imported module's
8383 text kept to be read again, which is to be checked.
8484
85-## `Buf` when memory runs out
86-
87-`Buf.add` and `Buf.str` call `room`, and when it fails they drop the
88-bytes and return as if they had added them. The buffer is then short,
89-and nothing says so: in sauce a short buffer is an object written to
90-disk with bytes missing, a corrupt store. A program must call `room`
91-before every `add` to be safe, which no one does.
92-
93-The proposal: when `room` fails, `add` and `str` stop the program, as a
94-failed check does: "Buf: out of memory", the calls under way, status
95-2. `room` stays as it is, the way for a program that can do something
96-better to ask first.
97-
98-An `ERROR` result would be raz's usual way, but it would put a `try` or
99-an `_ :=` at every call, for a failure almost no program can deal with,
100-and it breaks every program that calls them as statements (a dropped
101-result, an error under `-Werror`): a thaw. Stopping breaks no program
102-whose memory suffices, and changes only what happens when it doesn't,
103-from silent damage to a stop, so it can come with any release.
104-
105-Met in porting sauce, which wraps every `Buf.str` in a `Buf.room`
106-(`Sbase.add`).
107-
108-## Reading into a `Buf`
109-
110-`Sys.read` fills a `[]U8` it is given. A `Buf`'s free room,
111-`data[len..cap]`, is its own, and `bytes` gives only `data[0..len]`, so
112-there is no slice of it to read into. A program that reads a file into
113-a `Buf` reads into an array on its stack and copies that in with
114-`Buf.str`: every byte is handled twice, and the stack holds a buffer as
115-big as each read.
116-
117-```
118-proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, for a read to fill; "" if the allocator has none *)
119-```
120-
121-The program reads into what `spare` gives and adds what it got to
122-`len`, which is already exported:
123-
124-```
125-s := Buf.spare(ADR(b), 65536);
126-n := Sys.read(fd, s);
127-if n > 0 then INC(b.len, n); end;
128-```
129-
130-The slice is good until the buffer next grows, as `bytes` is. It
131-breaks no program. `len` past `cap` is still the program's mistake to
132-make, as it is today; a `Buf.grow(b, n)` that checks it could stand
133-for the `INC`, if wanted.
134-
135-Met in sauce's `readFd`, which reads every file of the store this way.
136-
13785 ## A slice that outlives what it points at
13886
13987 ```

doc/raz-lib.md (history) modified +6 -1

@@ -469,6 +469,7 @@
469469 proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *)
470470 proc add*(b: ^Buf; c: U8);
471471 proc str*(b: ^Buf; s: []U8);
472+proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *)
472473 proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *)
473474 proc reset*(b: ^Buf); (* emptied, its memory kept *)
474475 proc done*(b: ^Buf); (* its memory given back *)
@@ -532,7 +533,11 @@
532533 ```
533534
534535 A `Buf`, a `List` and a `Map` take their memory from the allocator
535-`init` gives them. `Fmt`'s format takes the next argument at each
536+`init` gives them. `Buf.add`, `str` and `spare` stop the program, as a
537+failed check does, if the allocator has no room; `room` asks first. A
538+program reads into a `Buf` through `spare`, then adds what it read to
539+`len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then
540+INC(b^.len, n); end;`. `Fmt`'s format takes the next argument at each
536541 `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and
537542 compares its keys with the procedures it is given:
538543 `import Count := Map([]U8, INT, Str.hash, Str.eq);`.

lib/Buf.raz (history) modified +34 -12

@@ -40,24 +40,42 @@
4040 return true;
4141 end;
4242
43-(* Adds the byte c at the end; if room for it can't be had, it is
44- dropped and the buffer is as it was. *)
45-proc add*(b: ^Buf; c: U8);
43+(* Room for n more bytes, or the program stops: a buffer that went on
44+ short of what was added to it would be wrong data, and nothing would
45+ say so. A program that can do better asks room first. *)
46+proc need(b: ^Buf; n: INT);
4647 begin
47- if room(b, 1) then
48- b^.data[b^.len] := c;
49- INC(b^.len);
48+ if not room(b, n) then
49+ _ := Sys.write(Sys.stderr, "Buf: out of memory\n");
50+ Rt.trace;
51+ Rt.exit(2);
5052 end;
5153 end;
5254
53-(* Adds the bytes of s at the end; if room for them can't be had, none
54- is added. *)
55+(* Adds the byte c at the end. *)
56+proc add*(b: ^Buf; c: U8);
57+begin
58+ need(b, 1);
59+ b^.data[b^.len] := c;
60+ INC(b^.len);
61+end;
62+
63+(* Adds the bytes of s at the end. *)
5564 proc str*(b: ^Buf; s: []U8);
65+begin
66+ need(b, LEN(s));
67+ COPY(b^.data[b^.len..b^.len + LEN(s)], s);
68+ INC(b^.len, LEN(s));
69+end;
70+
71+(* Room for n more bytes, and those bytes, for a read to fill: the
72+ program then adds to len what it filled. Good until the buffer next
73+ grows, as bytes is. *)
74+proc spare*(b: ^Buf; n: INT): []U8;
5675 begin
57- if room(b, LEN(s)) then
58- COPY(b^.data[b^.len..b^.len + LEN(s)], s);
59- INC(b^.len, LEN(s));
60- end;
76+ if n = 0 then return ""; end; (* a new buffer has no data to slice *)
77+ need(b, n);
78+ return b^.data[b^.len..b^.len + n];
6179 end;
6280
6381 (* What the buffer holds, until it next grows. *)
@@ -98,6 +116,10 @@
98116 reset(ADR(b));
99117 str(ADR(b), "again");
100118 if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end;
119+ s := spare(ADR(b), 3);
120+ COPY(s, "abc");
121+ INC(b.len, 2);
122+ if (LEN(s) # 3) or (bytes(ADR(b)) # "againab") then Rt.stop("Buf.spare"); end;
101123 done(ADR(b));
102124 Arena.done(ADR(ar));
103125 end;

lib/Files.raz (history) modified +3 -3

@@ -15,13 +15,13 @@
1515
1616 (* Adds the file at path to b; false if it can't be read. *)
1717 proc read*(path: []U8; b: ^Buf.Buf): BOOL;
18-var fd, n: INT; chunk: [4096]U8;
18+var fd, n: INT;
1919 begin
2020 fd := Sys.openRead(path);
2121 if fd < 0 then return false; end;
2222 repeat
23- n := Sys.read(fd, chunk);
24- if n > 0 then Buf.str(b, chunk[0..n]); end;
23+ n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *)
24+ if n > 0 then INC(b^.len, n); end;
2525 until n <= 0;
2626 Sys.close(fd);
2727 return n = 0;

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

An empty file.

test/run/BufFull.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+module BufFull;
2+(* a Buf whose allocator runs out stops the program, with the calls
3+ under way, rather than dropping what it was given *)
4+import Buf, Arena, Rt;
5+
6+proc main*;
7+var ar: Arena.Arena; b: Buf.Buf; big: [10000]U8;
8+begin
9+ if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end;
10+ Buf.init(ADR(b), ADR(ar.base));
11+ Buf.str(ADR(b), big);
12+end;
13+
14+end BufFull.

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

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

test/run/BufFull.stderr (history) added +4 -0

@@ -0,0 +1,4 @@
1+Buf: out of memory
2+ in Buf.need at lib/Buf.raz:50:9: b = 0x?, n = 10000
3+ in Buf.str at lib/Buf.raz:66:5: b = 0x?
4+ in BufFull.main at test/run/BufFull.raz:11:5

test/run/BufInit.stderr (history) modified +2 -1

@@ -1,4 +1,5 @@
11 Buf used before Buf.init
22 in Buf.room at lib/Buf.raz:31:9: b = 0x?, n = 5, cap = 64, p = nil
3- in Buf.str at lib/Buf.raz:57:5: b = 0x?
3+ in Buf.need at lib/Buf.raz:48:5: b = 0x?, n = 5
4+ in Buf.str at lib/Buf.raz:66:5: b = 0x?
45 in BufInit.main at test/run/BufInit.raz:9:5