Example (_uheap_walk -- Return Information about Memory Heap)

This example creates a heap and performs memory operations on it. _uheap_walk then traverses the heap and calls callback_function for each memory object. The callback_function prints a message about each memory block.

#include <stdlib.h>
#include <stdio.h>
#include <umalloc.h>
int _Optlink callback_function(const void *pentry, size_t sz, int useflag,
                               int status, const char *filename, size_t line)
{
   if (_HEAPOK != status) {
      puts("status is not _HEAPOK.");
      exit(status);
   }
   if (_USEDENTRY == useflag)
      printf("allocated  %p     %u\n", pentry, sz);
   else
      printf("freed      %p     %u\n", pentry, sz);
   return 0;
}
int main(void)
{
   Heap_t  myheap;
   char    *p1, *p2, *p3;
   /* User default heap as user heap */
   myheap = _udefault(NULL);
   if (NULL == (p1 = (char*)_umalloc(myheap, 100)) ||
       NULL == (p2 = (char*)_umalloc(myheap, 200)) ||
       NULL == (p3 = (char*)_umalloc(myheap, 300))) {
      puts("Cannot allocate memory from user heap.");
      exit(EXIT_FAILURE);
   }
   free(p2);
   puts("usage      address   size\n-----      -------   ----");
   _uheap_walk(myheap, callback_function);
   free(p1);
   free(p3);
   return 0;
   /***********************************************************************
      The output should be similar to:
      usage      address   size
      -----      -------   ----
      allocated  73A20     300
      allocated  738C0     100
       :
       :
      freed      73930     224
   ***********************************************************************/
}