Create a Fixed-Size Heap

Before you create a heap, you need to get the block of memory that will make up the heap.

You can get this block of memory by statically allocating it, or by calling one of the following APIs:.

Make sure the block is large enough to satisfy all the memory requests your program will make of it, as well as the internal information for managing the heap. Once the block is fully allocated, further allocation requests to the heap will fail.

The internal information requires _HEAP_MIN_SIZE bytes (_HEAP_MIN_SIZE is defined in <umalloc.h>); you cannot create a heap smaller than this. Add the amount of memory your program requires to this value to determine the size of the block you need to get.

Also make sure the block is the correct type for the heap you are creating:

Once you have the block of memory, create the heap with _ucreate.

Example of Creating a Fixed-Size Heap

Heap_t fixedHeap;                                  /* this is the "heap handle" */
static char block[_HEAP_MIN_SIZE + 5000];          /* get memory for internal info*/ 
                                                   /* plus 5000 bytes for the heap */

fixedHeap = _ucreate(block, (_HEAP_MIN_SIZE+5000), /* block to use */
                             !_BLOCK_CLEAN,        /* memory is not set to 0  (1)*/ 
                             _HEAP_REGULAR,        /* regular memory  (2)*/ 
                             NULL, NULL);          /* (3) */

Notes:

  1. The !_BLOCK_CLEAN parameter indicates that the memory in the block has not been initialized to 0. If it were set to 0 (for example, by VirtualAlloc or memset), you would specify _BLOCK_CLEAN. The calloc and _ucalloc functions use this information to improve their efficiency; if the memory is already initialized to 0, they don't need to initialize it.
    Note that VirtualAlloc and DosAllocMem return an area of 0's. You can also use memset to initialize the memory; however, memset commits all the memory at once, which could slow overall performance.
  2. The fourth parameter indicates what type of memory the heap contains: regular (_HEAP_REGULAR) or shared (_HEAP_SHARED).
  3. For a fixed-size heap, the last two parameters are always NULL.



Memory Management Functions
Types of Memory


Add to a Fixed-Size Heap