Skip to content

heap manager examples

sborshch edited this page Feb 29, 2016 · 8 revisions

Some usage examples

Memory pool declaration

At first, memory pool must be declared, for example (all heap stuff placed in namespace heap):

#include <stdint.h>
#include <heap.h>

heap::pool<4096> HeapPool;   // size in bytes

Heap pool may have any language-supported name.

Another example - placing heap pool in separate memory section:

__attribute__((section(".heap")))
heap::pool<4096> HeapPool;   // size in bytes

Heap configuration file

Heap configuration file heapcfg.h must be created and added to the user project. See thread-safe guard page for more details.

Heap Manager object declaration

Heap manager can be declared in any user's source file as template-class object with HeapPool argument:

heap::manager<heap_guard> heap::Manager(HeapPool);

Alternative way to declare heap pool

Internally, heap pool is an array of int type. This comes from alignment requirements on target platform. The user can declare heap pool in C-language manner, for example:

int HeapPool[ 4096/sizeof(int) ];

In this case heap manager declaration looks like:

heap::manager<heap_guard> heap::Manager(HeapPool, sizeof(HeapPool));

Or if memory pool object declared in the same scope (i.e. type and size of the object are known), there is more short alternative:

heap::manager<heap_guard> heap::manager(HeapPool);

Or memory pool can be declared as properly aligned external raw memory array:

extern void * const Heap_pool_start;
extern size_t const Heap_pool_size;
heap::manager<heap_guard> heap::manager(Heap_pool_start, Heap_pool_size);

The recommended approach is to use heap::pool<> because it is less error-prone.

Note, that object name heap::Manager and template parameter name heap_guard are predefined and cannot be changed by user.

Clone this wiki locally