(system heap)

Native Heap Management


(import (system heap))

Native memory allocation and deallocation utilities.


parameter: heap-allocator

A pair of native allocation and deallocation procedures. The allocation procedure should take a size and produce the address of a zeroed block of memory, the deallocation procedure consumes the address of a block previously returned by the allocation procedure.

procedure: (heap-alloc size)

procedure: (heap-alloc size wrap)

Allocates the given number of bytes using the current heap-allocator and returns two values. Before allocating, heap-collect is called to perform cleanup. Interrupts are disabled while heap-alloc runs.

If a wrapper procedure is given, the first returned value is the result of applying it to the address of the new memory block, otherwise the address is returned as is.

The second returned value is a release procedure that deallocates the memory block, unless it has already been deallocated. Interrupts are disabled while the release procedure runs. Calling the release procedure more than once has no adverse effects.

The release procedure is also registered with a guardian and executed by heap-collect once all references to the constructed wrapper have gone out of scope. If no wrapper was constructed or the wrapper procedure returned some atom, the release procedure itself is guarded.

procedure: (heap-collect)

Invokes the release procedures for any memory blocks that have gone out of scope.