Next: , Previous: Dictionary allocation, Up: Memory


5.7.3 Heap allocation

Heap allocation supports deallocation of allocated memory in any order. Dictionary allocation is not affected by it (i.e., it does not end a contiguous region). In Gforth, these words are implemented using the standard C library calls malloc(), free() and resize().

The memory region produced by one invocation of allocate or resize is internally contiguous. There is no contiguity between such a region and any other region (including others allocated from the heap).

allocate       u – a-addr wior        memory       “allocate”

Allocate u address units of contiguous data space. The initial contents of the data space is undefined. If the allocation is successful, a-addr is the start address of the allocated region and wior is 0. If the allocation fails, a-addr is undefined and wior is a non-zero I/O result code.

free       a-addr – wior        memory       “free”

Return the region of data space starting at a-addr to the system. The region must originally have been obtained using allocate or resize. If the operational is successful, wior is 0. If the operation fails, wior is a non-zero I/O result code.

resize       a-addr1 u – a-addr2 wior        memory       “resize”

Change the size of the allocated area at a-addr1 to u address units, possibly moving the contents to a different area. a-addr2 is the address of the resulting area. If the operation is successful, wior is 0. If the operation fails, wior is a non-zero I/O result code. If a-addr1 is 0, Gforth's (but not the Standard) resize allocates u address units.