home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / tvme30.zip / HEAPVIEW.CPP < prev    next >
C/C++ Source or Header  |  1995-08-02  |  4KB  |  132 lines

  1. // File    : HEAPVIEW.CPP
  2. // Author  : Eric Woodruff,  CIS ID: 72134,1150
  3. // Updated : Wed 08/02/95 17:54:19
  4. // Note    : Copyright 1993-95, Eric Woodruff, All rights reserved
  5. // Compiler: Borland C++ 3.1 to 4.xx
  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. // To enable free EMS/XMS memory reporting, add the REPORT_EMS_XMS definition
  12. // to the project or make file.
  13. //
  14.  
  15. #if defined(__FLAT__)
  16. #include <windows.h>
  17. #endif
  18.  
  19. #include <alloc.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #define Uses_TRect
  24. #define Uses_TView
  25. #include <tv.h>
  26.  
  27. #if !defined(__DPMI16__) && !defined(__DPMI32__)
  28.  
  29. #if defined(REPORT_EMS_XMS) && !defined(SHAREWARE)
  30. #include <tvirtmem.h>       // Report free EMS/XMS memory too.
  31. #endif
  32.  
  33. #endif
  34.  
  35. #include <heapview.h>
  36.  
  37. //
  38. // Heap Viewer functions
  39. //
  40. THeapView::THeapView(TRect &r) : TView(r)
  41. {
  42.     oldMem = 0L;
  43.     update();
  44. }
  45.  
  46. void THeapView::draw()
  47. {
  48.     TDrawBuffer b;
  49.     char c = getColor(2);
  50.  
  51.     b.moveChar(0, ' ', c, size.x);
  52.     b.moveStr(0, heapStr, c);
  53.     writeLine(0, 0, size.x, 1, b);
  54. }
  55.  
  56. void THeapView::update()
  57. {
  58. #if !defined(__DPMI16__) && !defined(__DPMI32__)
  59.     // In real mode, it will operate as it always has done.
  60.     long total = farcoreleft();
  61.     struct farheapinfo heap;
  62. #else
  63.     #if !defined(__FLAT__)
  64.         // In 16 bit protected mode, a call to farcoreleft() is all
  65.         // that is required.
  66.         long total = farcoreleft();
  67.     #else
  68.         // Even though you free() and/or delete memory in 32 bit protected
  69.         // mode, a call to farcoreleft() will not reflect a change that
  70.         // increases the amount of free memory, thus it always appears that
  71.         // your application has memory leaks when it really doesn't.
  72.         // The same is true of the code below, but it does reflect a more
  73.         // accurate amount when the GlobalAlloc()-type functions are used.
  74.         _MEMORYSTATUS MemStat;
  75.         MemStat.dwLength = sizeof(_MEMORYSTATUS);   // *Must* set length!
  76.         GlobalMemoryStatus(&MemStat);
  77.  
  78.         // Total free = Available physical memory + Available paging file space
  79.         long total = MemStat.dwAvailPhys + MemStat.dwAvailPageFile;
  80.     #endif
  81. #endif
  82.  
  83.     switch(heapcheck())
  84.     {
  85.         case _HEAPEMPTY:
  86.             strcpy(heapStr, "No heap!");
  87.             total = -1;
  88.             break;
  89.  
  90.         case _HEAPCORRUPT:
  91.             strcpy(heapStr, "Heap corrupt!");
  92.             total = -2;
  93.             break;
  94.  
  95.         case _HEAPOK:
  96. #if !defined(__DPMI16__) && !defined(__DPMI32__)
  97.             // Count up the free blocks scattered about prior to the highest
  98.             // allocated block in real mode.
  99.             heap.ptr = NULL;
  100.             while(farheapwalk(&heap) != _HEAPEND)
  101.                 if(!heap.in_use)
  102.                     total += heap.size;
  103.  
  104.     #if defined(REPORT_EMS_XMS) && !defined(SHAREWARE)
  105.             long freeEMS, freeXMS;
  106.  
  107.             TVirtualMemory::queryFree(&freeEMS, &freeXMS);
  108.  
  109.             // Report all values in K-bytes.
  110.             sprintf(heapStr, "E:%5ld X:%5ld C:%3ld", freeEMS / 1024L,
  111.                 freeXMS / 1024L, total / 1024L);
  112.  
  113.             // Make oldMem change value if necessary.  The value isn't
  114.             // important, just as long as it forces a redraw.
  115.             total += freeEMS;
  116.             total += freeXMS;
  117.     #else
  118.             sprintf(heapStr, "Free Memory: %8ld", total);
  119.     #endif
  120. #else
  121.             sprintf(heapStr, "Free Memory: %8ld", total);
  122. #endif
  123.             break;
  124.     }
  125.  
  126.     if(total != oldMem)
  127.     {
  128.         oldMem = total;
  129.         drawView();
  130.     }
  131. }
  132.