Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>_heapwalk() walks through heap</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 _heapwalk()             Walks through heap

 #include   <malloc.h>

  int _heapwalk(entry);
  struct _heapinfo *entry;    Information about next heap entry

    _heapwalk() walks through the heap, one entry per call, returning a
    pointer to a _heapinfo structure (defined in malloc.h) that holds
    information about the next heap entry. The structure has the
    following definition:

           struct _heapinfo {
              int far *_pentry;    Heap entry pointer
              size_t _size;        Size of heap entry
              int _useflag;        Entry "in-use" flag
              };

    Calls to _heapwalk() which return _HEAPOK will set _useflag to either
    _FREEENTRY or _USEDENTRY (defined in malloc.h). To obtain this
    information about the first entry in the heap, pass _heapwalk() a
    pointer to a _heapinfo structure whose _pentry field is NULL.

    _heapwalk() will map to the appropriate version depending on the
    memory model used:

    Data model   Program model    _heapwalk() mapped to
    ----------   -------------    --------------------
      small         small         _nheapwalk() - near heap
      small         medium        _nheapwalk() - near heap
      large         compact       _fheapwalk() - far heap
      large         large         _fheapwalk() - far heap

    Returns:    One of the following manifest constants, defined in
                malloc.h:

                    Constant         Meaning
                    -------------    -----------------------------
                    _HEAPOK          The heap appears to be consistent
                                     so far, and _heapinfo contains
                                     information about the next entry.
                    _HEAPEMPTY       The heap has not been initialized.
                    _HEAPBADPTR      The _pentry field of the entry
                                     structure does not contain a valid
                                     pointer into the heap.
                    _HEAPBADBEGIN    No initial header information found.
                    _HEAPBADNODE     A bad node found, or heap is
                                     damaged.
                    _HEAPEND         The heap end was reached
                                     successfully.

      Notes:    _heapwalk() is provided as an aid to debugging heap-
                related problems in programs.

   Portability:     MS-DOS only

 -------------------------------- Example ---------------------------------

 This program walks the heap from the beginning. It prints out each heap
 entry's use, location, and size. It reports the heap status if it is other
 than OK.

           #include <malloc.h>

           main()
           {
              void walk_heap();
              char *ptr1;

              walk_heap();
              ptr1 = malloc(100);
              walk_heap();
              malloc(500);
              walk_heap();
              free(ptr1);
              walk_heap();
           }

           void walk_heap()
           {
              struct _heapinfo entry;
              int heapstatus;

              entry._pentry = NULL;   /* start at beginning of heap */
              while (heapstatus = _heapwalk(entry)) == _HEAPOK)
                {
                printf("%6s block at %p of size %4.4X\n",
                      (entry._useflag == _USEDENTRY ? "USED" : "FREE"),
                      entry._pentry, entry._size);
                }
              switch (heapstatus)
                {
                case _HEAPEMPTY:
                      printf("Heap is empty\n");
                      break;
                case _HEAPEND:
                      printf("End of heap reached OK\n");
                      break;
                case _HEAPBADPTR:
                      printf("Heap has bad pointer\n");
                      break;
                case _HEAPBADBEGIN:
                      printf("Heap has bad initial header\n");
                      break;
                case _HEAPBADNODE:
                      printf("Heap has bad node\n");
                      break;
                }
           }

 This is sample output from the program:

 Heap is empty
   USED block at 23E4:10A6 of size 0064
   FREE block at 23E4:110C of size 0EF2
 End of heap reached OK
   USED block at 23E4:10A6 of size 0064
   USED block at 23E4:110C of size 01F4
   FREE block at 23E4:1302 of size 0CFC
 End of heap reached OK
   FREE block at 23E4:10A6 of size 0064
   USED block at 23E4:110C of size 01F4
   FREE block at 23E4:1302 of size 0CFC
 End of heap reached OK


See Also: _heapchk() _heapset()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson