home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number3 / heapstat.c < prev    next >
C/C++ Source or Header  |  1991-06-19  |  2KB  |  58 lines

  1. /* HEAPSTAT.C Heap status routines for Microsoft C */
  2.  
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include "heapstat.h"
  6. #ifdef __WATCOMC__
  7.                /* there isn't a far version in 386 mode */
  8. #define _fheapwalk _heapwalk
  9. #define _fheapchk  _heapchk
  10.                /* also typedef difference in malloc.h */
  11. int (*walkf[2])(struct _heapinfo *phi) = { _nheapwalk, _fheapwalk };
  12. /* Watcom uses different return values for heap management */
  13. static char *heap_status[] = { "ok", "empty", "bad-begin",
  14.     "bad-node", "end", "bad-ptr" };
  15. #else /* Microsoft C heap stuff */
  16. int (*walkf[2])(_HEAPINFO *phi) =   { _nheapwalk, _fheapwalk };
  17. static char *heap_status[] = { "?free?", "empty", "ok", "bad-begin",
  18.     "bad-node", "end", "bad-ptr" };
  19. #endif
  20.  
  21. int (*statf[2])(void) =             { _nheapchk, _fheapchk } ;
  22.  
  23. void heap_stats(HEAP h, HEAP_STATS *hs)
  24. {
  25.     struct _heapinfo hi;
  26.  
  27.     hs->heap = h;
  28.     hs->used = hs->free = hs->blks = 0;
  29.     hi._pentry = 0;
  30.     /* walk the heap, calling walk function for each block */
  31.     while ((*walkf[h])(&hi) == _HEAPOK)
  32.     {
  33.         switch (hi._useflag)
  34.         {
  35.             case _USEDENTRY: hs->used += hi._size; break;
  36.             case _FREEENTRY: hs->free += hi._size; break;
  37.         }
  38.         hs->blks++;
  39.     }
  40.     hs->status = (*statf[h])();
  41.  
  42. #ifdef __WATCOMC__    /* Watcom uses positive numbers */
  43.   hs->status_str = heap_status[ hs->status];
  44. #else
  45.   hs->status_str = heap_status[- hs->status];
  46. #endif
  47. }
  48.  
  49. void print_heap_stats(HEAP_STATS *hs)
  50. {
  51.   printf("%s heap used=%lu free=%lu blks=%lu status=%s\n",
  52.         (hs->heap == NEARHEAP) ? "near" : "far",
  53.         hs->used,
  54.         hs->free,
  55.         hs->blks,
  56.         hs->status_str);
  57. }
  58.