From 165517a9c8446ebaafba45b296a3aee138648c0f Mon Sep 17 00:00:00 2001 From: slederer Date: Sun, 24 Aug 2025 02:04:42 +0200 Subject: [PATCH] runtime: add MemAvail function --- lib/runtime.s | 27 +++++++++++++++++++++++++++ lib/stdlib.inc | 1 + 2 files changed, 28 insertions(+) diff --git a/lib/runtime.s b/lib/runtime.s index 6f248b9..a230e09 100644 --- a/lib/runtime.s +++ b/lib/runtime.s @@ -1764,6 +1764,33 @@ MEM_DUMP_L0: DROP RET +; calculate total free heap space +; args: none +; returns: cumulative size of all free chunks in bytes +MEMAVAIL: + FPADJ -4 + LOADC 0 + STORE 0 ; start with zero as result + + LOADCP _HEAP_ANCHOR +MAV_L: + DUP ; dup chunk ptr for later + INC 4 ; move to size field + LOADI ; load chunk size + LOAD 0 ; add to current result value + ADD + STORE 0 + + LOADI ; load next ptr + DUP + LOADCP _HEAP_ANCHOR ; compare with anchor + CMPU NE + CBRANCH MAV_L ; if not equal, loop +MAX_XT: + LOAD 0 ; put result value on stack + FPADJ 4 + RET + ; check if a pointer is part of the free list ; args: pointer returned by MEM_ALLOC ; throws runtime error if the pointer is found diff --git a/lib/stdlib.inc b/lib/stdlib.inc index e0b08a7..3ba4a4c 100644 --- a/lib/stdlib.inc +++ b/lib/stdlib.inc @@ -148,6 +148,7 @@ procedure appendchar(var s:string; aChar:char); external; procedure strmoveup(var s:string;index,length,delta:integer); external; procedure strmovedown(var s:string;index,length,delta:integer); external; procedure RuntimeError(var s:string); external; +function MemAvail:integer; external; (* from stdlib *) function copy(s:string[256];index,count:integer):string[256]; external;