Str.free: a copy given back, "" left alone Met in libro's Epub.done: every program that copies strings and frees them wrote Mem.free(a, ADR(c[0])) behind a test of LEN(c) > 0, since copy gives "" for an empty string, which was never the allocator's. Library growth that breaks no program; Str's test frees a copy and an empty one, which Heap would stop at.
| @@ -554,20 +554,6 @@ | ||
| 554 | 554 | Met in libro's `Term`, `Flow` and `Epub`, and rml's `Rml`, wherever |
| 555 | 555 | a byte is a number. |
| 556 | 556 | |
| 557 | -## `Str.free` | |
| 558 | - | |
| 559 | -`Str.copy(a, s)` gives a copy from the allocator `a`; giving it back | |
| 560 | -is `Mem.free(a, ADR(c[0]))`, and only if `LEN(c) > 0`, since an empty | |
| 561 | -copy is `""` and was never the allocator's. Every program that copies | |
| 562 | -strings and frees them writes that test. | |
| 563 | - | |
| 564 | -``` | |
| 565 | -proc free*(a: ^Mem.Allocator; s: []U8); (* a copy Str.copy made, given back; "" is none *) | |
| 566 | -``` | |
| 567 | - | |
| 568 | -Library growth that breaks no program; three lines. Met in libro's | |
| 569 | -`Epub.done`. | |
| 570 | - | |
| 571 | 557 | ## The width of a character on a terminal |
| 572 | 558 | |
| 573 | 559 | `Str` says, rightly, that it doesn't know how wide a character is on a |
| @@ -451,6 +451,7 @@ | ||
| 451 | 451 | proc eq*(a, b: []U8): BOOL; |
| 452 | 452 | proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *) |
| 453 | 453 | proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) |
| 454 | +proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *) | |
| 454 | 455 | proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) |
| 455 | 456 | proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) |
| 456 | 457 | proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) |
| @@ -21,6 +21,13 @@ | ||
| 21 | 21 | return p[0..LEN(s)], true; |
| 22 | 22 | end; |
| 23 | 23 | |
| 24 | +(* Gives back to a a copy that copy made from it; "", which copy gives | |
| 25 | + for an empty string, was never a's, and is left alone. *) | |
| 26 | +proc free*(a: ^Mem.Allocator; s: []U8); | |
| 27 | +begin | |
| 28 | + if LEN(s) > 0 then Mem.free(a, ADR(s[0])); end; | |
| 29 | +end; | |
| 30 | + | |
| 24 | 31 | (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) |
| 25 | 32 | proc cmp*(a, b: []U8): INT; |
| 26 | 33 | var i, n: INT; |
| @@ -575,6 +582,9 @@ | ||
| 575 | 582 | t, ok := copy(Heap.heap, buf); |
| 576 | 583 | buf[0] := 'x'; |
| 577 | 584 | if not ok or (t # "abc") then Rt.stop("Str.copy"); end; |
| 585 | + free(Heap.heap, t); | |
| 586 | + t, ok := copy(Heap.heap, ""); | |
| 587 | + free(Heap.heap, t); (* Heap would stop the program at a block it never gave *) | |
| 578 | 588 | if (cmp("a", "b") # -1) or (cmp("b", "a") # 1) or (cmp("ab", "ab") # 0) |
| 579 | 589 | or (cmp("a", "ab") # -1) or (cmp("", "") # 0) then Rt.stop("Str.cmp"); end; |
| 580 | 590 | if not eq("x", "x") or eq("x", "y") then Rt.stop("Str.eq"); end; |
| @@ -10,6 +10,10 @@ | ||
| 10 | 10 | s's bytes, copied into memory from a, to keep after s's own are |
| 11 | 11 | gone; "" and false if a has no room. |
| 12 | 12 | |
| 13 | +proc free*(a: ^Mem.Allocator; s: []U8); | |
| 14 | + Gives back to a a copy that copy made from it; "", which copy gives | |
| 15 | + for an empty string, was never a's, and is left alone. | |
| 16 | + | |
| 13 | 17 | proc cmp*(a, b: []U8): INT; |
| 14 | 18 | -1, 0 or 1 as a sorts before, with or after b, byte by byte. |
| 15 | 19 |