From 7235b7bd8f48b1b59dd3a93f37fb252a4d4db7a8 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 07:24:12 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index cb6c1da..ac5db96 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -554,20 +554,6 @@ Met in libro's `Term`, `Flow` and `Epub`, and rml's `Rml`, wherever a byte is a number. -## `Str.free` - -`Str.copy(a, s)` gives a copy from the allocator `a`; giving it back -is `Mem.free(a, ADR(c[0]))`, and only if `LEN(c) > 0`, since an empty -copy is `""` and was never the allocator's. Every program that copies -strings and frees them writes that test. - -``` -proc free*(a: ^Mem.Allocator; s: []U8); (* a copy Str.copy made, given back; "" is none *) -``` - -Library growth that breaks no program; three lines. Met in libro's -`Epub.done`. - ## The width of a character on a terminal `Str` says, rightly, that it doesn't know how wide a character is on a diff --git a/doc/raz-lib.md b/doc/raz-lib.md index a675a23..77a3db2 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -451,6 +451,7 @@ proc eq*(a, b: []U8): BOOL; proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *) proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) +proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *) proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) diff --git a/lib/Str.raz b/lib/Str.raz index 61ec2fb..04aad28 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -21,6 +21,13 @@ return p[0..LEN(s)], true; end; +(* Gives back to a a copy that copy made from it; "", which copy gives + for an empty string, was never a's, and is left alone. *) +proc free*(a: ^Mem.Allocator; s: []U8); +begin + if LEN(s) > 0 then Mem.free(a, ADR(s[0])); end; +end; + (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) proc cmp*(a, b: []U8): INT; var i, n: INT; @@ -575,6 +582,9 @@ t, ok := copy(Heap.heap, buf); buf[0] := 'x'; if not ok or (t # "abc") then Rt.stop("Str.copy"); end; + free(Heap.heap, t); + t, ok := copy(Heap.heap, ""); + free(Heap.heap, t); (* Heap would stop the program at a block it never gave *) if (cmp("a", "b") # -1) or (cmp("b", "a") # 1) or (cmp("ab", "ab") # 0) or (cmp("a", "ab") # -1) or (cmp("", "") # 0) then Rt.stop("Str.cmp"); end; if not eq("x", "x") or eq("x", "y") then Rt.stop("Str.eq"); end; diff --git a/test/doc/Str.expect b/test/doc/Str.expect index 1630d91..5b46c08 100644 --- a/test/doc/Str.expect +++ b/test/doc/Str.expect @@ -10,6 +10,10 @@ s's bytes, copied into memory from a, to keep after s's own are gone; "" and false if a has no room. +proc free*(a: ^Mem.Allocator; s: []U8); + Gives back to a a copy that copy made from it; "", which copy gives + for an empty string, was never a's, and is left alone. + proc cmp*(a, b: []U8): INT; -1, 0 or 1 as a sorts before, with or after b, byte by byte.