4.8.1. Support for malloc

malloc(), realloc(), calloc(), and free() are built on a heap abstract data type. You can either:

The default implementations of malloc(), realloc(), and calloc() maintain an 8-byte aligned heap.

Heap1: Standard heap implementation

Heap1, the default implementation, implements the smallest and simplest heap manager. The heap is managed as a singly-linked list of free blocks held in increasing address order. The allocation policy is first-fit by address.

This implementation has low overheads, but the cost of malloc() or free() grows linearly with the number of free blocks. The smallest block that can be allocated is four bytes and there is an additional overhead of four bytes. If you expect more than 100 unallocated blocks it is recommended that you use Heap2.

Heap2: Alternative heap implementation

Heap2 provides a compact implementation with the 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:

  • IMPORT __use_realtime_heap from assembly language

  • #pragma import(__use_realtime_heap) from C.

You can also define your own heap implementation. See Creating your own storage-management system for more information.

Using Heap2

The Heap2 real-time heap implementation must know how much address space the heap will span. 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. If you return zero for size, the heap extent is set to 4GB.

Using a heap implementation from bare machine C

To use a heap implementation in an application that does not define main() and does not initialize the C library:

  1. Call _init_alloc(base, , top) to define the base and top of the memory you want to manage as a heap.

  2. Define the function unsigned __rt_heap_extend(unsigned size, void ** block) to handle calls to extend the heap when it becomes full.


alloca() behaves identically to malloc() except that alloca() has automatic garbage collection (see alloca()).

Copyright © 1999-2001 ARM Limited. All rights reserved.ARM DUI 0067D