6.7.4.1 Memory blocks and heap allocation

Additional words for dealing with memory blocks are described in Memory Blocks. An alternative to the following words are among the $tring words (see $tring words).

save-mem ( addr1 u – addr2 u  ) gforth-0.2 “save-mem”

Copy the memory block addr u to addr2, which is the start of a newly heap allocated u-byte region.

extend-mem ( addr1 u1 u – addr addr2 u2  ) gforth-experimental “extend-mem”

Addr1 u1 is a memory block in heap memory. Increase the size of this memory block by u aus, possibly reallocating it. C-addr2 u2 is the resulting memory block (u2=u1+u), addr is the start of the u additional aus (addr=addr2+u1).

free-mem-var ( addr –  ) gforth-experimental “free-mem-var”

Addr is the address of a 2variable containing a memory block descriptor c-addr u in heap memory; free-mem-var frees the memory block and stores 0 0 in the 2variable.

Usage example:

2variable myblock
"foo" save-mem myblock 2!
myblock 2@ "bar" tuck >r >r extend-mem myblock 2! r> swap r> move
myblock 2@ type \ prints "foobar"
myblock free-mem-var