home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / memory / conv.cpp next >
Encoding:
C/C++ Source or Header  |  1993-06-29  |  2.0 KB  |  121 lines

  1. #include "..\MEMORY\MEMORY.HPP"
  2. #include <alloc.h>
  3.  
  4. // ------------------------------------------------------------------
  5. // File:        CONV.CPP
  6. // Path:        ...\REHACK\MEMORY\CONV.CPP
  7. // Version:        0.01
  8. // Author:        Pat Reilly
  9. // CIS Id:        71333,2764
  10. // Created On:    6/28/93
  11. // Modified On:
  12. // Description:    ConvMemory class for REHACK. See MEMORY.TXT for
  13. //                more details.
  14. // Tabs:        4
  15. // ------------------------------------------------------------------
  16.  
  17. long ConvMemory::memPoolSize = 0L;
  18.  
  19. ConvMemory::ConvMemory()
  20. {
  21.     vp = 0;
  22. }
  23.  
  24. ConvMemory::ConvMemory(word sz)
  25. {
  26.     vp = 0;
  27.     allocate(sz);
  28. }
  29.  
  30. bool ConvMemory::allocate(word sz)
  31. {
  32.     // Fail if already have mem allocated or if sz == 0.
  33.     if( vp != 0 || sz == 0 )
  34.         return false;
  35.  
  36.     // Cannot dip into the safety pool. Ensure there's enough memory.
  37.     long l = maxAvail();
  38.     if( l-(long)sz < memPoolSize )
  39.         return false;
  40.  
  41.     vp = new char [sz];
  42.     if( vp != 0 )
  43.         memsize = sz;
  44.     else
  45.         memsize = 0;
  46.     return bool(vp!= 0);
  47. }
  48.  
  49. void ConvMemory::free()
  50. {
  51.     // Don't free if the lockflag is still set.
  52.     if(lockflag)
  53.         return;
  54.  
  55.     delete vp;
  56.     vp = 0;
  57. }
  58.  
  59. void FAR* ConvMemory::lock()
  60. {
  61.     if( vp )
  62.         lockflag++;
  63.     return vp;
  64. }
  65.  
  66. void ConvMemory::unlock()
  67. {
  68.     if(lockflag)
  69.         lockflag--;
  70. }
  71.  
  72. long ConvMemory::memAvail()
  73. {
  74.     // Get total amt available, taking fragmentation into account.
  75.     long l = coreleft();
  76.     struct heapinfo heap;
  77.  
  78.     switch( heapcheck() )
  79.         {
  80.         case _HEAPEMPTY:
  81.             break;
  82.  
  83.         case _HEAPCORRUPT:
  84.             l = 0;
  85.             break;
  86.  
  87.         case _HEAPOK:
  88.             heap.ptr = NULL;
  89.             while(heapwalk(&heap) != _HEAPEND)
  90.                 if(!heap.in_use)
  91.                     l += heap.size;
  92.             break;
  93.         }
  94.     return l;
  95. }
  96.  
  97. long ConvMemory::maxAvail()
  98. {
  99.     // Search in fragmented heap, as well as top of heap.
  100.     long l = coreleft();
  101.     struct heapinfo heap;
  102.  
  103.     switch( heapcheck() )
  104.         {
  105.         case _HEAPEMPTY:
  106.             break;
  107.  
  108.         case _HEAPCORRUPT:
  109.             l = 0;
  110.             break;
  111.  
  112.         case _HEAPOK:
  113.             heap.ptr = NULL;
  114.             while(heapwalk(&heap) != _HEAPEND)
  115.                 if( !heap.in_use && heap.size > l )
  116.                     l = heap.size;
  117.             break;
  118.         }
  119.     return l;
  120. }
  121.