runtime/stdlib: add MaxAvail function

This commit is contained in:
slederer 2026-04-18 03:30:54 +02:00
parent 72b6ab6a30
commit 3fd6011e36
5 changed files with 75 additions and 24 deletions

View file

@ -1792,6 +1792,38 @@ MAX_XT:
FPADJ 4
RET
; find largest free chunk on heap
; args: none
; returns: size of largest free chunk in bytes
MAXAVAIL:
FPADJ -4
LOADC 0
STORE 0 ; start with zero as result
LOADCP _HEAP_ANCHOR
MXAV_L:
DUP ; dup chunk ptr for later
INC 4 ; move to size field
LOADI ; load chunk size
LOAD 0 ; compare with current result value
CMPU.S0 LE ; compare with keeping first arg on stack
CBRANCH MXAV_NEXT ; if smaller or equal, no change
STORE 0 ; else store as new value
BRANCH MXAV_NEXT2
MXAV_NEXT:
DROP
MXAV_NEXT2:
LOADI ; load next ptr
DUP
LOADCP _HEAP_ANCHOR ; compare with anchor
CMPU NE
CBRANCH MXAV_L ; if not equal, loop
MXAV_XT:
DROP ; drop chunk ptr
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

View file

@ -150,6 +150,7 @@ 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;
function MaxAvail:integer; external;
(* from stdlib *)
function copy(s:string[256];index,count:integer):string[256]; external;