home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / vrac / ve2tv103.zip / HEAPVIEW.CPP < prev    next >
C/C++ Source or Header  |  1994-07-31  |  3KB  |  103 lines

  1. // File    : HEAPVIEW.CPP
  2. // Author  : Eric Woodruff,  CIS ID: 72134,1150
  3. // Updated : Sun 07/31/94 15:56:28
  4. // Note    : Copyright 1993-94, Eric Woodruff, All rights reserved
  5. // Compiler: Borland C++ 3.1 to 4.02
  6. //
  7. // This file contains the Heap Viewer class.  This heap viewer will report the
  8. // proper amount of memory regardless of whether the application is running
  9. // in real mode, 16 bit protected mode, or 32 bit protected mode.
  10. //
  11.  
  12. #if defined(__FLAT__)
  13. #include <windows.h>
  14. #endif
  15.  
  16. #include <alloc.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #define Uses_TRect
  21. #define Uses_TView
  22. #include <tv.h>
  23.  
  24. #include <heapview.h>
  25.  
  26. //
  27. // Heap Viewer functions
  28. //
  29. THeapView::THeapView(TRect &r) : TView(r)
  30. {
  31.     oldMem = 0L;
  32.     update();
  33. }
  34.  
  35. void THeapView::draw()
  36. {
  37.     TDrawBuffer b;
  38.     char c = getColor(2);
  39.  
  40.     b.moveChar(0, ' ', c, size.x);
  41.     b.moveStr(0, heapStr, c);
  42.     writeLine(0, 0, size.x, 1, b);
  43. }
  44.  
  45. void THeapView::update()
  46. {
  47. #if !defined(__DPMI16__) && !defined(__DPMI32__)
  48.     // In real mode, it will operate as it always has done.
  49.     long total = farcoreleft();
  50.     struct farheapinfo heap;
  51. #else
  52.     #if !defined(__FLAT__)
  53.         // In 16 bit protected mode, a call to farcoreleft() is all
  54.         // that is required.
  55.         long total = farcoreleft();
  56.     #else
  57.         // Even though you free() and/or delete memory in 32 bit protected
  58.         // mode, a call to farcoreleft() will not reflect a change that
  59.         // increases the amount of free memory, thus it always appears that
  60.         // your application has memory leaks when it really doesn't.
  61.         // The same is true of the code below, but it does reflect a more
  62.         // accurate amount when the GlobalAlloc()-type functions are used.
  63.         _MEMORYSTATUS MemStat;
  64.         MemStat.dwLength = sizeof(_MEMORYSTATUS);   // *Must* set length!
  65.         GlobalMemoryStatus(&MemStat);
  66.  
  67.         // Total free = Available physical memory + Available paging file space
  68.         long total = MemStat.dwAvailPhys + MemStat.dwAvailPageFile;
  69.     #endif
  70. #endif
  71.  
  72.     switch(heapcheck())
  73.     {
  74.         case _HEAPEMPTY:
  75.             strcpy(heapStr, "No heap!");
  76.             total = -1;
  77.             break;
  78.  
  79.         case _HEAPCORRUPT:
  80.             strcpy(heapStr, "Heap corrupt!");
  81.             total = -2;
  82.             break;
  83.  
  84.         case _HEAPOK:
  85. #if !defined(__DPMI16__) && !defined(__DPMI32__)
  86.             // Count up the free blocks scattered about prior to the highest
  87.             // allocated block in real mode.
  88.             heap.ptr = NULL;
  89.             while(farheapwalk(&heap) != _HEAPEND)
  90.                 if(!heap.in_use)
  91.                     total += heap.size;
  92. #endif
  93.             sprintf(heapStr, "Free Memory: %8ld", total);
  94.             break;
  95.     }
  96.  
  97.     if(total != oldMem)
  98.     {
  99.         oldMem = total;
  100.         drawView();
  101.     }
  102. }
  103.