home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLASSINC.ZIP / MEMMGR.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.7 KB  |  258 lines

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