Format
#include <umalloc.h> void *_umalloc(Heap_t heap, size_t size);
Language Level: Extension
_umalloc allocates a memory block of size bytes from the heap you specify. Unlike
_ucalloc, _umalloc does not initialize all bits to 0.
_umalloc works just like malloc, except that you specify the heap to use; malloc always allocates from the default heap. A debug version of this function, _debug_umalloc, is also provided.
If the heap does not have enough memory for the request, _umalloc calls the getmore_fn that you specified when you created the heap with _ucreate.
To reallocate or free memory allocated with _umalloc, use the non-heap-specific realloc and free. These functions always check what heap the memory was allocated from.
Return Value
_umalloc returns a pointer to the
reserved space. If size
was specified as 0, or if your getmore_fn
cannot provide enough memory, _umalloc returns NULL. Passing
_umalloc a heap that is not valid results in undefined behavior.
Example
This example creates a heap and uses
_umalloc to allocate memory from the heap.
#include <stdlib.h> #include <stdio.h> #include <umalloc.h>
int main(void)
{
Heap_t myheap;
char *ptr;
/* Use default heap as user heap */ myheap = _udefault(NULL);
if (NULL == (ptr = (char*)_umalloc(myheap, 100))) {
puts("Cannot allocate memory from user heap.");
exit(EXIT_FAILURE);
}
free(ptr);
return 0;
}
![]()
calloc -- Reserve and
Initialize Storage
_debug_umalloc
-- Reserve Memory Blocks from User Heap
free -- Release
Storage Blocks
malloc --
Reserve Storage Block
realloc --
Change Reserved Storage Block Size
_ucalloc --
Reserve and Initialize Memory from User Heap
_ucreate --
Create a Memory Heap
<umalloc.h>