home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-1.ZIP / CLASSINC.ZIP / MEMMGR.H < prev    next >
C/C++ Source or Header  |  1992-02-18  |  5KB  |  248 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  MEMMGR.H                                                              */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991                                  */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __MEMMGR_H )
  11. #define __MEMMGR_H
  12.  
  13. #if !defined( __STDTEMPL_H )
  14. #include <StdTempl.h>
  15. #endif    // __STDTEMPL_H
  16.  
  17. #if !defined( __RESOURCE_H )
  18. #include <Resource.h>
  19. #endif    // __RESOURCE_H
  20.  
  21. #if !defined( __STDLIB_H )
  22. #include <StdLib.h>
  23. #endif    // __STDLIB_H
  24.  
  25. #if !defined( __CHECKS_H )
  26. #include <Checks.h>
  27. #endif    // __CHECKS_H
  28.  
  29. _CLASSDEF(HeaderBlock)
  30. _CLASSDEF(BlockList)
  31. _CLASSDEF(BaseMemBlocks)
  32. _CLASSDEF(MemStack)
  33. _CLASSDEF(Marker)
  34. _CLASSDEF(BMarker)
  35.  
  36. class _CLASSTYPE HeaderBlock
  37. {
  38.  
  39. public:
  40.  
  41.     void _FAR *operator new( size_t, size_t );
  42.     void _FAR *operator new( size_t );
  43.  
  44. };
  45.  
  46. inline void _FAR *HeaderBlock::operator new( size_t sz, size_t extra )
  47. {
  48.     return ::operator new( sz + extra );
  49. }
  50.  
  51. inline void _FAR *HeaderBlock::operator new( size_t )
  52. {
  53.     CHECK(0);
  54.     return 0;
  55. }
  56.  
  57. class _CLASSTYPE BlockList : public HeaderBlock
  58. {
  59.  
  60. public:
  61.  
  62.     BlockList( BlockList _FAR * );
  63.  
  64. private:
  65.  
  66.     BlockList _FAR *next;
  67.  
  68.     friend class BaseMemBlocks;
  69.  
  70. };
  71.  
  72. inline BlockList::BlockList( BlockList _FAR *nxt ) :
  73.     next( nxt )
  74. {
  75. }
  76.  
  77. class _CLASSTYPE BaseMemBlocks
  78. {
  79.  
  80. public:
  81.  
  82.     BaseMemBlocks( size_t = 8192 );
  83.     ~BaseMemBlocks();
  84.  
  85.     char _FAR *block() const { return (char _FAR *)curBlock; }
  86.     unsigned count() const { return blockCount; }
  87.     allocBlock( size_t );
  88.     void freeTo( unsigned );
  89.  
  90. protected:
  91.  
  92.     const size_t blockSize;
  93.  
  94. private:
  95.  
  96.     BlockList _FAR *curBlock;
  97.     unsigned blockCount;
  98.  
  99. };
  100.  
  101. inline BaseMemBlocks::BaseMemBlocks( size_t sz ) :
  102.     curBlock(0),
  103.     blockCount(0),
  104.     blockSize(sz)
  105. {
  106.     CHECK( sz != 0 );
  107. }
  108.  
  109. inline BaseMemBlocks::~BaseMemBlocks()
  110. {
  111. #if !defined( WINDOWS_WEP_BUG )
  112.     freeTo( 0 );
  113. #endif
  114. }
  115.  
  116. class _CLASSTYPE MemStack : public BaseMemBlocks
  117. {
  118.  
  119. public:
  120.  
  121.     friend class Marker;
  122.  
  123.     MemStack( size_t = 8192 );
  124.  
  125.     void *allocate( size_t );
  126.  
  127. private:
  128.  
  129.     size_t curLoc;
  130.  
  131. };
  132.  
  133. inline void _FAR *operator new( size_t sz, MemStack& m )
  134. {
  135.     return m.allocate( sz );
  136. }
  137.  
  138. inline MemStack::MemStack( size_t sz ) :
  139.     BaseMemBlocks( sz ),
  140.     curLoc(sz)
  141. {
  142.     CHECK( sz != 0 );
  143. }
  144.  
  145. class _CLASSTYPE Marker
  146. {
  147.  
  148. public:
  149.  
  150.     Marker( MemStack _FAR & ms ) :
  151.         memstk(ms),
  152.         blk(ms.count()),
  153.         curLoc(ms.curLoc)
  154.         {
  155.         }
  156.  
  157.     ~Marker()
  158.         {
  159.         PRECONDITION( blk < memstk.count() ||
  160.               (blk == memstk.count() && curLoc <= memstk.curLoc )
  161.             );
  162.         memstk.freeTo( blk );
  163.         memstk.curLoc = curLoc;
  164.         }
  165.  
  166.  
  167. private:
  168.  
  169.     const unsigned blk;
  170.     const size_t curLoc;
  171.     MemStack& memstk;
  172.  
  173. };
  174.  
  175. class _CLASSTYPE MemBlocks
  176. {
  177.  
  178. public:
  179.  
  180.     MemBlocks( size_t, unsigned = 100 );
  181.  
  182.     void _FAR *allocate( size_t );
  183.     void free( void _FAR * );
  184.  
  185. private:
  186.  
  187.     void _FAR *freeList;
  188.     MemStack mem;
  189.     size_t size;
  190.  
  191.     friend class BMarker;
  192.  
  193. };
  194.  
  195. inline MemBlocks::MemBlocks( size_t sz, unsigned count ) :
  196.     mem( sz*count ),
  197.     freeList(0),
  198.     size( max(sz, sizeof(void _FAR *)) )
  199. {
  200.     CHECK( sz != 0 && count != 0 );
  201. }
  202.  
  203. #pragma argsused
  204. inline void _FAR *MemBlocks::allocate( size_t sz )
  205. {
  206.     PRECONDITION( size == max(sz, sizeof(void _FAR *)) );
  207.     if( freeList == 0 )
  208.         return mem.allocate( size );
  209.     else
  210.         {
  211.         void _FAR *temp = freeList;
  212.         freeList = *(void _FAR * _FAR *)temp;
  213.         return temp;
  214.         }
  215. }
  216.  
  217. inline void MemBlocks::free( void _FAR * block )
  218. {
  219.     *(void _FAR * _FAR *)block = freeList;
  220.     freeList = block;
  221. }
  222.  
  223. class _CLASSTYPE BMarker
  224. {
  225.  
  226. public:
  227.  
  228.     BMarker( MemBlocks _FAR & mb ) :
  229.         memstk(mb.mem),
  230.         blk(mb.mem.count())
  231.         {}
  232.  
  233.     ~BMarker()
  234.         {
  235.         PRECONDITION( blk <= memstk.count() );
  236.         memstk.freeTo( blk );
  237.         }
  238.  
  239.  
  240. private:
  241.  
  242.     const unsigned blk;
  243.     MemStack _FAR & memstk;
  244.  
  245. };
  246.  
  247. #endif  // __MEMMGR_H
  248.