You can add blocks of memory to a fixed-size or dynamically-sized heap with _uaddmem. This can be useful if you have a large amount of memory that is allocated conditionally. Like the starting block, you must first allocate memory for a block of memory. This block will be added to the current heap, so make sure the block you add is of the same type of memory as the heap to which you are adding it. For example, to add 64K to fixedHeap:
static char newblock[65536];
_uaddmem(fixedHeap, /* heap to add to */
newblock, 65536, /* block to add */
_BLOCK_CLEAN); /* sets memory to 0 */
When you call _umalloc (or a similar function) for a dynamically-sized heap, _umalloc tries to allocate the memory from the initial block you provided to _ucreate. If not enough memory is there, it then calls the heap-expanding function you specified as a parameter to _ucreate. Your function then gets more memory from the operating system and adds it to the heap. It is up to you how you do this.
Your function must have the following prototype:
void *(*functionName)(Heap_t uh, size_t *size, int *clean);
Where functionName identifies the function (you can name it however you want), uh is the heap to be expanded, and size is the size of the allocation request passed by _umalloc. You probably want to return enough memory at a time to satisfy several allocations; otherwise every subsequent allocation has to call your heap-expanding function, reducing your program's execution speed. Make sure that you update the size parameter if you return more than the size requested.
Your function must also set the clean parameter to either _BLOCK_CLEAN, to indicate the memory has been set to 0, or !_BLOCK_CLEAN, to indicate that the memory has not been initialized.
The following fragment shows an example of a heap-expanding function:
static void *expandHeap(Heap_t uh, size_t *length, int *clean)
{
char *newblock;
/* round the size up to a multiple of 64K * /
*length = (*length / 65536) * 65536 + 65536;
*clean = _BLOCK_CLEAN; /* mark the block as "clean" */
return(newblock); /* return new memory block */
}