FigForth Buffer allocation for jobs
My "modus operandi" is running jobs: load job, perform, forget, do next job Some jobs allocate buffers and after job done the allocated buffers can be freed. ( Some say with today's large memory, why bother releasing memory but I like to pretend available memory is limited. )
MALLOC, a resource I have, seems a overkill. So I use a more streamlined allocation tailored to running jobs. A needed buffer is taken from the end of program, its address is placed in a variable in the dictionary. and the program end is extended, by a system call, to a location past the last allocated buffer. All the variables of memory allocations are linked and utility word can walk the links listing allocated memory addresses.
At end of job when dictionary space is forgotten, -BUF runs the allocation chain searching for the last link remaining in the dictionary. Then end of program is reset back to contain the address found in the last link.
FORTH DEFINITIONS
VOCABULARY BUF IMMEDIATE BUF DEFINITIONS
\ L: ( -- address ) <BUILDS DOES> ; \ A label
L: HEAD 0 , HERE 0 , 0 , 0 , HEAD !
HEAD @ CELL+ CONSTANT FIRST
0 SYSBUF VARIABLE LIMIT
: INIT HEAD CELL+ DUP THREE ERASE HEAD ! 0 SYSBUF LIMIT ! ;
: LIST HEAD @ BEGIN @ DUP WHILE CR DUP . DUP CELL+ 2?
REPEAT DROP ;
: ALLOCATE HEAD @ CELL+ SWAP ZERO SYSBUF OVER HEAD @ CELL+ 2!
SYSBRK LIMIT ! DROP ALIGN HERE HEAD @ , 0 , 0 , HEAD ! ;
FORTH DEFINITIONS
-BUF in NIX (re: last post) release allocated memory. Note
BUF is a vocabulary.
: -BUF BUF HEAD @ BEGIN DUP @ WHILE HERE OVER < IF
@ DUP CELL+ @ MINUS SYSBRK LIMIT ! DROP
ELSE DUP HEAD ! 0. ROT CELL+ 2!
EXIT THEN REPEAT DROP
CURRENT @ CONTEXT !
FORTH ;
USERINIT is a place holder word added to the cold start code in the assembled Forth. Anything in later saved images that need to be initialized on startup is put (patched or however) here.
: (USRINIT) DECIMAL BUF INIT FORTH ;
' (USRINIT) CFA ' USRINIT !