Sign In | Register |

Memory Heaps

The memory heap component is part of the real-time kernel and provides dynamic allocation of variable-sized memory from a larger contiguous block of memory. A heap has the benefit of being able to allocate any size of memory, but the trade-off is that it can suffer from fragmentation and the time it takes to allocate the memory is not deterministic. Therefore, allocations from a heap cannot be performed from an ISR, a memory pool must be used for those scenarios.

The memory heap maintains a list of free blocks and upon a request for allocation, the heap will take the first free block that can accommodate the request. If the free block is larger than the amount of memory requested, the heap will split the free block and return only the portion that was requested.

When memory is returned, the heap will merge (defrag) the returned memory with any adjacent memory free memory blocks.

Basic Usage

When creating a heap, a contiguous region of memory must be provided that will be managed by the heap. An example snippet below provides a 2KB block of memory to be managed by a memory heap.

#include "Kernel/kernel_heap.h"
#include <assert.h>

static HEAP heap;                                   /* Allocate a memory heap */

void APP_Example(void)
{
    static BYTE heap_mem[2048];                     /* Allocate the source memory managed by the heap */
    STATUS status;


    status = HEAP_Create(&heap,                     /* Create a memory heap */
                         "Heap1",                   /* A name for the heap (for debugging purposes) */
                         heap_mem,                  /* A pointer to the memory used by the heap */
                         sizeof(heap_mem));         /* The size in bytes of the heap */

    assert(status == SUCCESS);
}

Any size of memory can be allocated from a heap. An example snippet below allocates a block of memory and accesses the block and then later releases the memory back to the heap.

#include "Kernel/kernel_heap.h"

/* An application-defined object */
typedef struct MYOBJECT {
    UINT32 value1;
    UINT32 value2;
} MYOBJECT;

void APP_Example(void)
{
    MYOBJECT* obj;


    obj = HEAP_Alloc(&heap, sizeof(MYOBJECT));  /* Allocate memory for an object */
    if (obj) {                                  /* Successfully allocated? */
	
        obj->value1 = 0;                        /* Use the memory... */
        obj->value2 = 1;


        HEAP_Free(&heap, obj);                  /* Return the memory to the heap */
    }
}

API Reference

STATUS HEAP_Create(HEAP* heap, const CHAR* name, void* mem, UINT32 size)
Creates and initializes a memory heap from a contiguous region of memory.
PARAMETERS
heap A pointer to a caller allocated memory heap to be initialized.
name A pointer to a NULL terminated string that represents a name for the heap
mem A pointer to a caller allocated region of memory to be managed by the heap.
size The size, in bytes, of the given region of memory.
RETURNS
SUCCESS The memory heap was created and initialized.
ERR_NULLREFERENCE The argument 'heap' or 'mem' was found to be NULL.
ERR_INVALIDCONTEXT This operation is not supported from the context of an interrupt service routine (ISR).
ERR_ALREADYINITIALIZED The specified heap has already been created and initialized.
STATUS HEAP_Destroy(HEAP* heap)
Destroys and removes a memory heap from the kernel.
PARAMETERS
heap A pointer to a caller allocated memory heap to be destroyed.
RETURNS
SUCCESS The memory heap was successfully destroyed and removed from the kernel.
ERR_NULLREFERENCE The argument 'heap' was found to be NULL.
ERR_INVALIDCONTEXT The operation is not supported from the context of an interrupt service routine (ISR).
ERR_NOTINITIALIZED The specified heap has not been created and initialized.
void* HEAP_Alloc(HEAP* heap, UINT32 size)
Allocates and returns memory from a heap.
PARAMETERS
heap A pointer to the heap in which to allocate memory.
size The size, in bytes, of the memory to be allocated and returned.
RETURNS
A pointer to the memory that has been allocated upon success; otherwise NULL on any error.
REMARKS
This performs a linear search and therefore is an O(n) operation where n is the number of free blocks within the heap.
Not thread safe.
void* HEAP_Calloc(HEAP* heap, UINT32 size)
Allocates and returns memory from a heap that has been cleared (zero initialized).
PARAMETERS
heap A pointer to the heap in which to allocate memory.
size The size, in bytes, of the memory to be allocated and returned.
RETURNS
A pointer to the memory that has been allocated and cleared upon success; otherwise NULL on any error.
REMARKS
This performs a linear search and therefore is an O(n) operation where n is the number of free blocks within the heap.
Not thread safe.
STATUS HEAP_Free(HEAP* heap, void* mem)
Releases and returns memory back to a memory heap.
PARAMETERS
heap A pointer to the heap to receive the memory.
mem A pointer to the memory to be returned to the heap.
RETURNS
SUCCESS The memory was returned to the heap.
ERR_NULLREFERENCE The argument 'heap' or 'mem' was found to be NULL.
ERR_NOTINITIALIZED The specified heap has not been created and initialized.
ERR_INVALIDOPERATION The memory was not used from the heap and could not be returned.
REMARKS
This is an O(1) operation.
Not thread safe.
UINT32 HEAP_FreeBytes(HEAP* heap)
Returns the total number of available free bytes remaining within a memory heap.
PARAMETERS
heap A pointer to the target memory heap.
RETURNS
The total number of available free bytes remaining within the heap.
REMARKS
The returned value does not indicate the total number of continuous bytes that could be allocated as the heap could be fragmented.
Not thread safe.
STATUS HEAP_Verify(HEAP* heap)
Verifies a memory heap against any corruption.
PARAMETERS
heap A pointer to the heap to be verified.
RETURNS
SUCCESS The memory heap has no detectable corruption.
ERR_NULLREFERENCE The argument 'heap' was found to be NULL.
ERR_NOTINITIALIZED The specified heap has not been created and initialized.
ERR_CORRUPTED The memory heap has been corrupted.
REMARKS
Not thread safe.