raz / commit 7235b7bd

commit
7235b7bd8f48b1b59dd3a93f37fb252a4d4db7a8
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
fe7939cd
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.

patch · log

4 files changed, +15 -14

PROPOSALS.md modified +0 -14

@@ -554,20 +554,6 @@
554554 Met in libro's `Term`, `Flow` and `Epub`, and rml's `Rml`, wherever
555555 a byte is a number.
556556
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-
571557 ## The width of a character on a terminal
572558
573559 `Str` says, rightly, that it doesn't know how wide a character is on a

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

@@ -451,6 +451,7 @@
451451 proc eq*(a, b: []U8): BOOL;
452452 proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *)
453453 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 *)
454455 proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *)
455456 proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *)
456457 proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *)

lib/Str.raz (history) modified +10 -0

@@ -21,6 +21,13 @@
2121 return p[0..LEN(s)], true;
2222 end;
2323
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+
2431 (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *)
2532 proc cmp*(a, b: []U8): INT;
2633 var i, n: INT;
@@ -575,6 +582,9 @@
575582 t, ok := copy(Heap.heap, buf);
576583 buf[0] := 'x';
577584 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 *)
578588 if (cmp("a", "b") # -1) or (cmp("b", "a") # 1) or (cmp("ab", "ab") # 0)
579589 or (cmp("a", "ab") # -1) or (cmp("", "") # 0) then Rt.stop("Str.cmp"); end;
580590 if not eq("x", "x") or eq("x", "y") then Rt.stop("Str.eq"); end;

test/doc/Str.expect (history) modified +4 -0

@@ -10,6 +10,10 @@
1010 s's bytes, copied into memory from a, to keep after s's own are
1111 gone; "" and false if a has no room.
1212
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+
1317 proc cmp*(a, b: []U8): INT;
1418 -1, 0 or 1 as a sorts before, with or after b, byte by byte.
1519