Syntax
#include <umalloc.h> int _uheap_walk(Heap_t heap, int (*callback_fn)(const void *object, size_t size, int flag, int status, const char* file, int line) );Description
For each object, _uheap_walk passes your function:
object
_uheap_walk calls callback_fn for each object until one of the following occurs:
You may want to code your callback_fn to return a nonzero value if the status of the object is not _HEAPOK. Even if callback_fn returns 0 for an object that is corrupted, _heap_walk cannot continue because of the state of the heap and returns to its caller.
You can use callback_fn to process information from _uheap_walk in various ways. For example, you may want to print the information to a file, or use it to generate your own error messages. You can use the information to look for memory leaks and objects incorrectly allocated or freed from the heap. It can be especially useful to call _uheap_walk when _uheapchk returns an error.
Note:
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 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 = _umalloc(myheap, 100)) || NULL == (p2 = _umalloc(myheap, 200)) || NULL == (p3 = _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 ****************************************************************************/ }