_uheapset -- Validate and Set Memory Heap

Format

#include <umalloc.h>
int _heapset(Heap_t heap, unsigned int fill);

Language Level: Extension
_uheapset checks the heap you specify for minimal consistency by checking all allocated and freed objects on the heap (similar to _uheapchk). It then sets each byte of the heap's free objects to the value of fill.

Using _uheapset can help you locate problems where your program continues to use a freed pointer to an object. After you set the free heap to a specific value, when your program tries to interpret the set values in the freed object as data, unexpected results occur, indicating a problem.

_uheapset works just like _heapset, except that you specify the heap to check; _heapset always checks the default heap.

Note: Using the _uheapchk, _uheapset, and _uheap_walk functions (and their equivalents for the default heap) may add overhead to each object allocated from the heap.

Return Value
_uheapset returns one of the following values, defined in both <umalloc.h> and <malloc.h>:

_HEAPBADBEGIN The heap specified is not valid. It may have been closed or destroyed.
_HEAPBADNODE A memory node is corrupted, or the heap is damaged.
_HEAPEMPTY The heap has not been initialized.
_HEAPOK The heap appears to be consistent.

Example
This example creates a heap and allocates and frees memory from it. It then calls _uheapset to set the freed memory to a value.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main (void)
{
   char  *ptr;
   int   rc;
   if (NULL == (ptr = (char*)malloc(10))) {
      puts("Could not allocate memory block.");
      exit(EXIT_FAILURE);
   }
   memset(ptr,'A',5);
   free(ptr);
   if (_HEAPOK != (rc = _heapset('X'))) {
      switch(rc) {
         case _HEAPEMPTY:
            puts("The heap has not been initialized.");
            break;
         case _HEAPBADNODE:
            puts("A memory node is corrupted or the heap is damaged.");
            break;
         case _HEAPBADBEGIN:
            puts("The heap specified is not valid.");
            break;
      }
      exit(rc);
   }
   return 0;
}



Managing Memory with Multiple Heaps
Memory Management


_heapmin -- Release Unused Memory from Default Heap
_heapset -- Validate and Set Default Heap
_uheapchk -- Validate Memory Heap
_uheap_walk -- Return Information about Memory Heap
<umalloc.h>