From 302c3acd02019b4d39b581a84903c2b0515622e0 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:22:41 +0100 Subject: [PATCH] Heap: sure, the heap that stops the program when memory runs out --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 466c406..b9f4c79 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -75,6 +75,7 @@ module Heap; var heap*: ^Mem.Allocator; +var sure*: ^Mem.Allocator; (* heap, but running out of memory stops the program *) module Arena; type Arena* = record @@ -87,7 +88,9 @@ ``` `NEW(p, a)` calls `a^.alloc`. `Heap` is the general -allocator. An arena hands out memory from +allocator; `Heap.heap` gives `nil` when memory runs out, and `Heap.sure` +stops the program instead, as `Buf` does, so that `NEW(p, Heap.sure)` +needs no check. An arena hands out memory from one block: its `free` does nothing, and `reset` forgets everything. ``` diff --git a/lib/Heap.raz b/lib/Heap.raz index 996e4fc..170436c 100644 --- a/lib/Heap.raz +++ b/lib/Heap.raz @@ -7,7 +7,7 @@ twice, or freeing what the heap never gave out, stops the program. One lock keeps it whole across threads. *) module Heap; -import Mem, Lock, Rt; +import Mem, Lock, Rt, Sys; const Classes = 9; @@ -25,9 +25,13 @@ cur: ^U8; (* where the next new small block comes from *) left: INT; lock: Lock.Lock; - std: Mem.Allocator; + std, surer: Mem.Allocator; (* The heap, for NEW(p, Heap.heap) and Mem's procedures. *) heap*: ^Mem.Allocator; + (* The heap, but running out of memory stops the program, as + Buf.add does, instead of giving nil: for NEW where nil would be + a bug nobody checks for. *) + sure*: ^Mem.Allocator; (* The block p, with size for its header, given out. *) proc give(p: ^U8; size: INT): ^U8; @@ -107,10 +111,24 @@ end; end; +(* The heap's alloc, but with no room the program stops, with the + calls under way. *) +proc allocSure(a: ^Mem.Allocator; size, align: INT): ^U8; +var p: ^U8; +begin + p := alloc(a, size, align); + if p = nil then + _ := Sys.write(Sys.stderr, "Heap: out of memory\n"); + Rt.trace; + Rt.exit(2); + end; + return p; +end; + (* The module's test: blocks of every class and big ones, written, freed and taken again. *) proc main*; -var p: [40]^U8; i, j, n: INT; again: ^U8; +var p: [40]^U8; i, j, n: INT; again: ^U8; q: ^INT; begin for i := 0 to 39 do n := (i * i * 37) mod 9000 + 1; @@ -129,6 +147,10 @@ again := Mem.alloc(heap, (25 * 37) mod 9000 + 1, 8); if again # p[5] then Rt.stop("Heap: a freed block isn't taken again"); end; for i := 0 to 39 do Mem.free(heap, p[i]); end; + NEW(q, sure); + if (q = nil) or (INT(q) mod 8 # 0) then Rt.stop("Heap: sure"); end; + q^ := 7; + Mem.free(sure, ^U8(q)); if Mem.alloc(heap, -1, 8) # nil then Rt.stop("Heap: negative size"); end; if Mem.alloc(heap, 7FFFFFFFFFFFFFFFH - 4, 8) # nil then Rt.stop("Heap: huge size"); end; end; @@ -136,4 +158,6 @@ begin std := {alloc: alloc, free: free}; heap := ADR(std); + surer := {alloc: allocSure, free: free}; + sure := ADR(surer); end Heap. diff --git a/test/run/HeapSure.expect b/test/run/HeapSure.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/HeapSure.raz b/test/run/HeapSure.raz new file mode 100644 index 0000000..d3fc8ff --- /dev/null +++ b/test/run/HeapSure.raz @@ -0,0 +1,12 @@ +module HeapSure; +(* Heap.sure stops the program, with the calls under way, when the heap + has no room: here for a size it refuses on any machine *) +import Heap, Mem, Rt; + +proc main*; +begin + _ := Mem.alloc(Heap.sure, 7FFFFFFFFFFFFFFFH - 4, 8); + Rt.stop("HeapSure: not stopped"); +end; + +end HeapSure. diff --git a/test/run/HeapSure.status b/test/run/HeapSure.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/HeapSure.status @@ -0,0 +1 @@ +2 diff --git a/test/run/HeapSure.stderr b/test/run/HeapSure.stderr new file mode 100644 index 0000000..344bd75 --- /dev/null +++ b/test/run/HeapSure.stderr @@ -0,0 +1,4 @@ +Heap: out of memory + in Heap.allocSure at lib/Heap.raz:122:9: a = 0x?, size = 9223372036854775803, align = 8, p = nil + in Mem.alloc at lib/Mem.raz:33:5: a = 0x?, size = 9223372036854775803, align = 8 + in HeapSure.main at test/run/HeapSure.raz:8:5