home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / c / actlib11 / tvtools / heap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-14  |  2.2 KB  |  89 lines

  1. /*-------------------------------------------------------------------*/
  2. /*                                                                   */
  3. /*   Heap.cpp:  Heap for the Turbo Vision Demo.                      */
  4. /*              Display the current heap space at the right end      */
  5. /*              of the status line.                                  */
  6. /*-------------------------------------------------------------------*/
  7.  
  8.  
  9. #define Uses_TApplication
  10. #define Uses_THeapView
  11. #include "tvtools.h"
  12.  
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <strstrea.h>
  17. #include <iomanip.h>
  18. #include <alloc.h>
  19.  
  20.  
  21.  
  22. //
  23. // ------------- Heap Viewer functions
  24. //
  25.  
  26. THeapView::THeapView( TRect& r ) : TView( r )
  27. {
  28.   if ( ! size.x )
  29.      {
  30.        TRect r = TApplication::application->getExtent();
  31.        r.a.x = r.b.x - 13; r.a.y = r.b.y - 1;
  32.        growMode = gfGrowAll;
  33.        moveTo( r.a.x, r.a.y );
  34.        growTo( r.b.x, r.b.y );
  35.        growMode = 0;
  36.      }
  37.  
  38.   oldMem = 0;
  39.   newMem = heapSize();
  40. }
  41.  
  42.  
  43. void THeapView::draw()
  44. {
  45.     TDrawBuffer buf;
  46.     char c = getColor(2);
  47.  
  48.     buf.moveChar( 0, ' ', c, size.x );
  49.     buf.moveStr( 0, heapStr, c );
  50.     writeLine( 0, 0, size.x, 1, buf );
  51. }
  52.  
  53.  
  54. void THeapView::update()
  55. {
  56.     if ( (newMem = heapSize()) != oldMem )
  57.        {
  58.          oldMem = newMem;
  59.          drawView();
  60.        }
  61. }
  62.  
  63.  
  64. long THeapView::heapSize()
  65. {
  66.   long total = farcoreleft();
  67.   struct farheapinfo heap;
  68.   ostrstream totalStr( heapStr, sizeof heapStr );
  69.  
  70.   switch( farheapcheck() )
  71.         {
  72.           case _HEAPEMPTY  : strcpy(heapStr, "     No heap");
  73.                              total = -1;
  74.                              break;
  75.  
  76.           case _HEAPCORRUPT: strcpy(heapStr, "Heap corrupt");
  77.                              total = -2;
  78.                              break;
  79.  
  80.           case _HEAPOK     : heap.ptr = NULL;
  81.                              while ( farheapwalk(&heap) != _HEAPEND )
  82.                                    if ( ! heap.in_use ) total += heap.size;
  83.                              totalStr << setw(12) << total << ends;
  84.                              break;
  85.         }
  86.  
  87.   return total;
  88. }
  89.