raz / commit 302c3acd

commit
302c3acd02019b4d39b581a84903c2b0515622e0
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
62fab28b
Heap: sure, the heap that stops the program when memory runs out

patch · log

6 files changed, +48 -4

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

@@ -75,6 +75,7 @@
7575
7676 module Heap;
7777 var heap*: ^Mem.Allocator;
78+var sure*: ^Mem.Allocator; (* heap, but running out of memory stops the program *)
7879
7980 module Arena;
8081 type Arena* = record
@@ -87,7 +88,9 @@
8788 ```
8889
8990 `NEW(p, a)` calls `a^.alloc`. `Heap` is the general
90-allocator. An arena hands out memory from
91+allocator; `Heap.heap` gives `nil` when memory runs out, and `Heap.sure`
92+stops the program instead, as `Buf` does, so that `NEW(p, Heap.sure)`
93+needs no check. An arena hands out memory from
9194 one block: its `free` does nothing, and `reset` forgets everything.
9295
9396 ```

lib/Heap.raz (history) modified +27 -3

@@ -7,7 +7,7 @@
77 twice, or freeing what the heap never gave out, stops the program.
88 One lock keeps it whole across threads. *)
99 module Heap;
10-import Mem, Lock, Rt;
10+import Mem, Lock, Rt, Sys;
1111
1212 const
1313 Classes = 9;
@@ -25,9 +25,13 @@
2525 cur: ^U8; (* where the next new small block comes from *)
2626 left: INT;
2727 lock: Lock.Lock;
28- std: Mem.Allocator;
28+ std, surer: Mem.Allocator;
2929 (* The heap, for NEW(p, Heap.heap) and Mem's procedures. *)
3030 heap*: ^Mem.Allocator;
31+ (* The heap, but running out of memory stops the program, as
32+ Buf.add does, instead of giving nil: for NEW where nil would be
33+ a bug nobody checks for. *)
34+ sure*: ^Mem.Allocator;
3135
3236 (* The block p, with size for its header, given out. *)
3337 proc give(p: ^U8; size: INT): ^U8;
@@ -107,10 +111,24 @@
107111 end;
108112 end;
109113
114+(* The heap's alloc, but with no room the program stops, with the
115+ calls under way. *)
116+proc allocSure(a: ^Mem.Allocator; size, align: INT): ^U8;
117+var p: ^U8;
118+begin
119+ p := alloc(a, size, align);
120+ if p = nil then
121+ _ := Sys.write(Sys.stderr, "Heap: out of memory\n");
122+ Rt.trace;
123+ Rt.exit(2);
124+ end;
125+ return p;
126+end;
127+
110128 (* The module's test: blocks of every class and big ones, written,
111129 freed and taken again. *)
112130 proc main*;
113-var p: [40]^U8; i, j, n: INT; again: ^U8;
131+var p: [40]^U8; i, j, n: INT; again: ^U8; q: ^INT;
114132 begin
115133 for i := 0 to 39 do
116134 n := (i * i * 37) mod 9000 + 1;
@@ -129,6 +147,10 @@
129147 again := Mem.alloc(heap, (25 * 37) mod 9000 + 1, 8);
130148 if again # p[5] then Rt.stop("Heap: a freed block isn't taken again"); end;
131149 for i := 0 to 39 do Mem.free(heap, p[i]); end;
150+ NEW(q, sure);
151+ if (q = nil) or (INT(q) mod 8 # 0) then Rt.stop("Heap: sure"); end;
152+ q^ := 7;
153+ Mem.free(sure, ^U8(q));
132154 if Mem.alloc(heap, -1, 8) # nil then Rt.stop("Heap: negative size"); end;
133155 if Mem.alloc(heap, 7FFFFFFFFFFFFFFFH - 4, 8) # nil then Rt.stop("Heap: huge size"); end;
134156 end;
@@ -136,4 +158,6 @@
136158 begin
137159 std := {alloc: alloc, free: free};
138160 heap := ADR(std);
161+ surer := {alloc: allocSure, free: free};
162+ sure := ADR(surer);
139163 end Heap.

test/run/HeapSure.expect (history) added +0 -0

An empty file.

test/run/HeapSure.raz (history) added +12 -0

@@ -0,0 +1,12 @@
1+module HeapSure;
2+(* Heap.sure stops the program, with the calls under way, when the heap
3+ has no room: here for a size it refuses on any machine *)
4+import Heap, Mem, Rt;
5+
6+proc main*;
7+begin
8+ _ := Mem.alloc(Heap.sure, 7FFFFFFFFFFFFFFFH - 4, 8);
9+ Rt.stop("HeapSure: not stopped");
10+end;
11+
12+end HeapSure.

test/run/HeapSure.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/HeapSure.stderr (history) added +4 -0

@@ -0,0 +1,4 @@
1+Heap: out of memory
2+ in Heap.allocSure at lib/Heap.raz:122:9: a = 0x?, size = 9223372036854775803, align = 8, p = nil
3+ in Mem.alloc at lib/Mem.raz:33:5: a = 0x?, size = 9223372036854775803, align = 8
4+ in HeapSure.main at test/run/HeapSure.raz:8:5