_rtl_heapwalk
Go Up to malloc.h Index
Header File
malloc.h
Prototype
int _rtl_heapwalk(_HEAPINFO *hi);
Description
Inspects the heap node by node.
Note: This function replaces _heapwalk which is obsolete.
_rtl_heapwalk assumes the heap is correct. Use _heapchk to verify the heap before using _rtl_heapwalk. _HEAPOK is returned with the last block on the heap. _HEAPEND will be returned on the next call to _rtl_heapwalk.
_rtl_heapwalk receives a pointer to a structure of type _HEAPINFO (declared in malloc.h). Note that the _HEAPINFO structure must be allocated on the heap (using malloc()). You cannot pass the address of a variable declared on the stack.
For the first call to _rtl_heapwalk, set the hi._pentry field to NULL. _rtl_heapwalk returns with hi._pentry containing the address of the first block.
hi._size | holds the size of the block in bytes. |
hi._useflag | is a flag that is set to _USEDENTRY if the block is currently in use. If the block is free, hi._useflag is set to _FREEENTRY. |
Return Value
This function returns one of the following values:
_HEAPBADNODE | A corrupted heap block has been found |
_HEAPBADPTR | The _pentry field does not point to a valid heap block |
_HEAPEMPTY | No heap exists |
_HEAPEND | The end of the heap has been reached |
_HEAPOK | The _heapinfo block contains valid information about the next heap block |
Portability
POSIX | ANSI C | ANSI C++ | Win32 | Win64 | macOS | |
---|---|---|---|---|---|---|
_rtl_heapwalk | + |
Example
#include <stdio.h>
#include <malloc.h>
#include <alloc.h>
#define NUM_PTRS 10
#define NUM_BYTES 16
int main(void) {
_HEAPINFO *hi;
char *array[NUM_PTRS];
int i;
hi = (_HEAPINFO*) malloc(sizeof(_HEAPINFO));
for (i = 0; i < NUM_PTRS; i++)
array[i] = (char *) malloc(NUM_BYTES);
for (i = 0; i < NUM_PTRS; i += 2)
free(array[i]);
hi->_pentry = NULL;
printf(" Size Status\n");
printf(" ---- ------\n");
while (_rtl_heapwalk(hi) == _HEAPOK)
printf("%7u %s\n", hi->_size, hi->_useflag ? "used" : "free");
free(hi);
return 0;
}