home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / FACETV.ZIP / HEAPVIEW.CPP < prev    next >
C/C++ Source or Header  |  1994-01-04  |  2KB  |  111 lines

  1. /************************************************************************
  2. **
  3. ** @(#)heapview.cpp    04/01/93    Chris Ahlstrom
  4. **
  5. **  --------------------------
  6. **  73340.26!compuserve.com
  7. **  --------------------------
  8. **
  9. ** The THeapView from Borland's GADGETS modules.  I stole it! And
  10. ** added the ability to turn it on and off at run-time.
  11. **
  12. ** Some code Copyright (c) 1991 by Borland International
  13. **
  14. ** Gadgets for the Turbo Vision Demo.  Includes the heap view which
  15. ** displays the current heap space at the right end of the status line.
  16. **
  17. *************************************************************************/
  18.  
  19. #define HEAPVIEW_cpp
  20.  
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <strstrea.h>
  25. #include <iomanip.h>
  26. #include <alloc.h>
  27.  
  28. #include "heapview.h"
  29.  
  30.  
  31. THeapView::THeapView
  32. (
  33.     TRect& r
  34. ) :
  35.     TView( r )
  36. {
  37.     oldMem    = 0;
  38.     newMem    = heapSize();
  39.     turnHeapOff    = 0;
  40. }
  41.  
  42.  
  43. void
  44. THeapView::draw ()
  45. {
  46.     TDrawBuffer buf;
  47.     char c = getColor(2);
  48.  
  49.     buf.moveChar(0, ' ', c, size.x);
  50.     buf.moveStr(0, heapStr, c);
  51.     writeLine(0, 0, size.x, 1, buf);
  52. }
  53.  
  54.  
  55. void
  56. THeapView::update ()
  57. {
  58.     if ((newMem = heapSize()) != oldMem)
  59.     {
  60.         oldMem = newMem;
  61.         drawView();
  62.     }
  63. }
  64.  
  65.  
  66. void
  67. THeapView::clearLine ()
  68. {
  69.     ostrstream totalStr(heapStr, sizeof heapStr);
  70.     totalStr << "            " << ends;
  71.     drawView();                    // draw the blank string
  72.     turnHeapOff = 1;                // tell idle() what to do
  73. }
  74.  
  75.  
  76. long
  77. THeapView::heapSize ()
  78. {
  79.     long total = farcoreleft();
  80.     struct farheapinfo heap;
  81.     ostrstream totalStr(heapStr, sizeof heapStr);
  82.  
  83.     switch (farheapcheck())
  84.     {
  85.     case _HEAPEMPTY:
  86.  
  87.     strcpy(heapStr, "     No heap");
  88.     total = -1;
  89.     break;
  90.  
  91.     case _HEAPCORRUPT:
  92.  
  93.     strcpy(heapStr, "Heap corrupt");
  94.     total = -2;
  95.     break;
  96.  
  97.     case _HEAPOK:
  98.  
  99.     heap.ptr = NULL;
  100.     while(farheapwalk(&heap) != _HEAPEND)
  101.     {
  102.         if (!heap.in_use)
  103.         total += heap.size;
  104.     }
  105.     totalStr << setw(12) << total << ends;
  106.     break;
  107.     }
  108.     return(total);
  109. }
  110.  
  111.