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.
| @@ -82,58 +82,6 @@ | ||
| 82 | 82 | the budget; and, across modules, the imported module's |
| 83 | 83 | text kept to be read again, which is to be checked. |
| 84 | 84 | |
| 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 | - | |
| 137 | 85 | ## A slice that outlives what it points at |
| 138 | 86 | |
| 139 | 87 | ``` |
| @@ -469,6 +469,7 @@ | ||
| 469 | 469 | proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) |
| 470 | 470 | proc add*(b: ^Buf; c: U8); |
| 471 | 471 | proc str*(b: ^Buf; s: []U8); |
| 472 | +proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) | |
| 472 | 473 | proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) |
| 473 | 474 | proc reset*(b: ^Buf); (* emptied, its memory kept *) |
| 474 | 475 | proc done*(b: ^Buf); (* its memory given back *) |
| @@ -532,7 +533,11 @@ | ||
| 532 | 533 | ``` |
| 533 | 534 | |
| 534 | 535 | 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 | |
| 536 | 541 | `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and |
| 537 | 542 | compares its keys with the procedures it is given: |
| 538 | 543 | `import Count := Map([]U8, INT, Str.hash, Str.eq);`. |
| @@ -40,24 +40,42 @@ | ||
| 40 | 40 | return true; |
| 41 | 41 | end; |
| 42 | 42 | |
| 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); | |
| 46 | 47 | 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); | |
| 50 | 52 | end; |
| 51 | 53 | end; |
| 52 | 54 | |
| 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. *) | |
| 55 | 64 | 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; | |
| 56 | 75 | 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]; | |
| 61 | 79 | end; |
| 62 | 80 | |
| 63 | 81 | (* What the buffer holds, until it next grows. *) |
| @@ -98,6 +116,10 @@ | ||
| 98 | 116 | reset(ADR(b)); |
| 99 | 117 | str(ADR(b), "again"); |
| 100 | 118 | 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; | |
| 101 | 123 | done(ADR(b)); |
| 102 | 124 | Arena.done(ADR(ar)); |
| 103 | 125 | end; |
| @@ -15,13 +15,13 @@ | ||
| 15 | 15 | |
| 16 | 16 | (* Adds the file at path to b; false if it can't be read. *) |
| 17 | 17 | proc read*(path: []U8; b: ^Buf.Buf): BOOL; |
| 18 | -var fd, n: INT; chunk: [4096]U8; | |
| 18 | +var fd, n: INT; | |
| 19 | 19 | begin |
| 20 | 20 | fd := Sys.openRead(path); |
| 21 | 21 | if fd < 0 then return false; end; |
| 22 | 22 | 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; | |
| 25 | 25 | until n <= 0; |
| 26 | 26 | Sys.close(fd); |
| 27 | 27 | return n = 0; |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -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 |
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | Buf used before Buf.init |
| 2 | 2 | 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? | |
| 4 | 5 | in BufInit.main at test/run/BufInit.raz:9:5 |