| |||

Home > The ARM C and C++ libraries > Heap2, alternative heap implementation |

Heap2 provides a compact implementation with the performance
cost of `malloc()`

or `free()`

growing
logarithmically with the number of free blocks. The allocation policy
is first-fit by address. The smallest block that can be allocated
is 12 bytes and there is an additional overhead of four bytes.

Heap2 is recommended when you require near constant-time performance in the presence of hundreds of free blocks. To select the alternative standard implementation, use either of the following:

`IMPORT __use_realtime_heap`

from assembly language`#pragma import(__use_realtime_heap)`

from C.

The Heap2 real-time heap implementation must know the maximum address space that the heap spans. The smaller the address range, the more efficient the algorithm is.

By default, the heap extent is taken to be 16MB starting at
the beginning of the heap (defined as the start of the first chunk
of memory given to the heap manager by `__rt_initial_stackheap()`

or `__rt_heap_extend()`

).

The heap bounds are given by:

struct __heap_extent { unsigned base, range; }; __value_in_regs struct __heap_extent __user_heap_extent( unsigned defaultbase, unsigned defaultsize);

The function prototype for `__user_heap_extent()`

is
in `rt_misc.h`

.

(The Heap1 algorithm does not require the bounds on the heap extent. Therefore, it never calls this function.)

You must redefine `__user_heap_extent()`

if:

you require a heap to span more than 16MB of address space

your memory model can supply a block of memory at a lower address than the first one supplied.

If you know in advance that the address space bounds of your
heap are small, you do not have to redefine `__user_heap_extent()`

,
but it does speed up the heap algorithms if you do.

The input parameters are the default values that are used if this routine is not defined. You can, for example, leave the default base value unchanged and only adjust the size.

The size field returned must be a power of two. The library does not check this and fails in unexpected ways if this requirement is not met. If you return a size of zero, the extent of the heap is set to 4GB.