_mheap -- Query Memory Heap for Allocated Object

Format

#include <umalloc.h>
Heap_t _mheap(void *ptr);

Language Level: Extension
_mheap determines from which heap the object specified by ptr was allocated. The ptr must be a valid pointer that was returned from a runtime allocation function (_ucalloc, malloc, realloc, and so on). If the pointer is not valid, the results of _mheap are undefined.

Return Value
_mheap returns the handle of the heap from which the object was allocated. If the object was allocated from the runtime heap, _mheap returns _RUNTIME_HEAP. If the object passed to _mheap is NULL, _mheap returns NULL. If the object is not valid, _mheap either returns NULL or the results are unpredictable and an exception may occur.

Example
This example allocates a block of memory from the heap, then uses _mheap to determine which heap the block came from.

#include <stdlib.h>
#include <stdio.h>
#include <umalloc.h>
int main(void)
{
   char  *ptr;
   if (NULL == (ptr = (char*)malloc(10))) {
        puts("Could not allocate memory block.");
        exit(EXIT_FAILURE);
   }
   printf("Handle of heap used is 0x%x\n", _mheap(ptr));
   return 0;
   /****************************************************
      The output should be similar to:
      Handle of heap used is 0x70000
   ****************************************************/
}



Memory Management


_msize -- Return Number of Bytes Allocated
_ucreate -- Create a Memory Heap
_ustats -- Get information about Heap
<umalloc.h>