_ucalloc -- Reserve and Initialize Memory from User Heap

Format

#include <umalloc.h>
void *_ucalloc(Heap_t heap, size_t num, size_t size);

Language Level: Extension
_ucalloc allocates memory for an array of num elements, each of length size bytes, from the heap you specify. It then initializes all bits of each element to 0.

_ucalloc works just like calloc except that you specify the heap to use; calloc always allocates from the default heap. A debug version of this function, _debug_ucalloc, is also provided.

If the heap does not have enough memory for the request, _ucalloc calls the getmore_fn that you specified when you created the heap with _ucreate.

To reallocate or free memory allocated with _ucalloc, use the non-heap-specific realloc and free. These functions always check what heap the memory was allocated from.

Return Value
_ucalloc returns a pointer to the reserved space. If size or num was specified as zero, or if your getmore_fn cannot provide enough memory, _ucalloc returns NULL. Passing _ucalloc a heap that is not valid results in undefined behavior.

Example
This example creates a heap myheap and then uses _ucalloc to allocate memory from it.

#include <stdlib.h>
#include <stdio.h>
#include <umalloc.h>
#include <string.h>
int main(void)
{
   Heap_t  myheap;
   char    *ptr;
   /* Use default heap as user heap */
   myheap = _udefault(NULL);
   if (NULL == (ptr = (char*)_ucalloc(myheap, 100, 1))) {
      puts("Cannot allocate memory from user heap.");
      exit(EXIT_FAILURE);
   }
   memset(ptr, 'x', 10);
   free(ptr);
   return 0;
}



Memory Management


calloc -- Reserve and Initialize Storage
_debug_ucalloc -- Reserve and Initialize Memory from User Heap
free -- Release Storage Blocks
realloc -- Change Reserved Storage Block Size
_ucreate -- Create a Memory Heap
_umalloc -- Reserve Memory Blocks from User Heap
<umalloc.h>