<h1>Native Heap Management</h1>

<h2>Synopsis</h2>

<code>(import (system heap))</code>

Native memory allocation and deallocation utilities.

<h2>Operations</h2>

<h3>parameter:

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.

<h3>procedure: <code>(heap-alloc
<h3>procedure: <code>(heap-alloc <i>size</i>

Allocates the given number of bytes using the current <tt>heap-allocator</tt> and returns two values. Before allocating, <tt>heap-collect</tt> is called to perform cleanup. Interrupts are disabled while <tt>heap-alloc</tt> 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. Calling the release procedure more than once has no adverse effects.

The release procedure is also registered with a guardian and executed by <tt>heap-collect</tt> 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.

<h3>procedure:

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