home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classinc.pak / MEMMGR.H < prev    next >
C/C++ Source or Header  |  1997-07-23  |  14KB  |  477 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  MEMMGR.H                                                              */
  4. /*                                                                        */
  5. /*  Copyright (c) 1991, 1994 Borland International                        */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( CLASSLIB_MEMMGR_H )
  11. #define CLASSLIB_MEMMGR_H
  12.  
  13. #if !defined( __STDLIB_H )
  14. #include <stdlib.h>
  15. #endif
  16.  
  17. #if !defined( __DOS_H )
  18. #include <dos.h>
  19. #endif
  20.  
  21. #if !defined( __CHECKS_H )
  22. #include <checks.h>
  23. #endif
  24.  
  25. #if !defined( CLASSLIB_DEFS_H )
  26. #include <classlib/defs.h>
  27. #endif
  28.  
  29. #if !defined( CLASSLIB_STDTEMPL_H )
  30. #include <classlib/stdtempl.h>
  31. #endif
  32.  
  33. #if !defined( CLASSLIB_ALLOCTR_H )
  34. #include <classlib/alloctr.h>
  35. #endif
  36.  
  37. #pragma option -Vo-
  38. #if defined( BI_CLASSLIB_NO_po )
  39. #pragma option -po-
  40. #endif
  41.  
  42. /*------------------------------------------------------------------------*/
  43. /*                                                                        */
  44. /*  template <class Alloc> class TMBlockList                              */
  45. /*                                                                        */
  46. /*  Used internally.                                                      */
  47. /*                                                                        */
  48. /*------------------------------------------------------------------------*/
  49.  
  50. template <class Alloc> class TMBaseMemBlocks;
  51.  
  52. template <class Alloc> class TMBlockList
  53. {
  54.  
  55. public:
  56.  
  57.     TMBlockList( TMBlockList * );
  58.  
  59. private:
  60.  
  61.     void *operator new( size_t, size_t, const Alloc& );
  62.     void *operator new [] ( size_t, size_t, const Alloc& );
  63.     void operator delete( void * );
  64.     void operator delete [] ( void * );
  65.  
  66.     TMBlockList *Next;
  67.  
  68.     friend TMBaseMemBlocks<Alloc>;
  69.  
  70. };
  71.  
  72. template <class Alloc>
  73. inline TMBlockList<Alloc>::TMBlockList( TMBlockList<Alloc> *nxt ) :
  74.     Next( nxt )
  75. {
  76. }
  77.  
  78. template <class Alloc>
  79. inline void *TMBlockList<Alloc>::operator new ( size_t sz,
  80.                                             size_t extra,
  81.                                             const Alloc& a )
  82. {
  83.     return new(a)char[sz+extra];
  84. }
  85.  
  86. template <class Alloc>
  87. inline void TMBlockList<Alloc>::operator delete ( void *ptr )
  88. {
  89.     Alloc::operator delete(ptr);
  90. }
  91.  
  92. template <class Alloc>
  93. inline void TMBlockList<Alloc>::operator delete [] ( void _BIDSFAR * ptr )
  94. {
  95.     Alloc::operator delete [] (ptr);
  96. }
  97.  
  98. #if defined( BI_OLDNAMES )
  99. #define MBlockList TMBlockList
  100. #endif
  101.  
  102. /*------------------------------------------------------------------------*/
  103. /*                                                                        */
  104. /*  class TBlockList                                                      */
  105. /*                                                                        */
  106. /*  Used internally.                                                      */
  107. /*                                                                        */
  108. /*------------------------------------------------------------------------*/
  109.  
  110. class TBlockList : public TMBlockList<TStandardAllocator>
  111. {
  112.  
  113. public:
  114.  
  115.     TBlockList( TBlockList *blk ) :
  116.         TMBlockList<TStandardAllocator>( blk ) {}
  117.  
  118. };
  119.  
  120. #if defined( BI_OLDNAMES )
  121. #define BlockList TBlockList
  122. #endif
  123.  
  124. /*------------------------------------------------------------------------*/
  125. /*                                                                        */
  126. /*  template <class Alloc> class TMBaseMemBlocks                          */
  127. /*                                                                        */
  128. /*  Used internally.                                                      */
  129. /*                                                                        */
  130. /*------------------------------------------------------------------------*/
  131.  
  132. template <class Alloc> class TMBaseMemBlocks :
  133.     public Alloc
  134. {
  135.  
  136. public:
  137.  
  138.     TMBaseMemBlocks( size_t = 8192 );
  139.     ~TMBaseMemBlocks();
  140.  
  141.     char *Block() const { return REINTERPRET_CAST(char *,CurBlock); }
  142.     unsigned Count() const { return BlockCount; }
  143.     AllocBlock( size_t );
  144.     void FreeTo( unsigned );
  145.     size_t GetBlockSize() const { return BlockSize; }
  146.  
  147. private:
  148.  
  149.     TMBlockList<Alloc> *CurBlock;
  150.     const size_t BlockSize;
  151.     unsigned BlockCount;
  152.  
  153. };
  154.  
  155. template <class Alloc>
  156. inline TMBaseMemBlocks<Alloc>::TMBaseMemBlocks( size_t sz ) :
  157.     CurBlock(0),
  158.     BlockCount(0),
  159.     BlockSize(sz)
  160. {
  161.     CHECK( sz != 0 );
  162. }
  163.  
  164. template <class Alloc>
  165. inline TMBaseMemBlocks<Alloc>::~TMBaseMemBlocks()
  166. {
  167. #if !defined( BI_WINDOWS_WEP_BUG )
  168.     FreeTo( 0 );
  169. #endif
  170. }
  171.  
  172. template <class Alloc> int TMBaseMemBlocks<Alloc>::AllocBlock( size_t sz )
  173. {
  174.     TMBlockList<Alloc> *temp = 
  175.         new( max(sz,BlockSize), *this ) TMBlockList<Alloc>( CurBlock-1 );
  176.     if( temp == 0 )
  177.         return 0;
  178.     CurBlock = temp+1;
  179.     BlockCount++;
  180.     return 1;
  181. }
  182.  
  183. template <class Alloc> void TMBaseMemBlocks<Alloc>::FreeTo( unsigned term )
  184. {
  185.     PRECONDITION( BlockCount >= term );
  186.     while( BlockCount > term )
  187.         {
  188.         TMBlockList<Alloc> *temp = CurBlock-1;
  189.         CurBlock = (temp->Next)+1;
  190.         delete temp;
  191.         BlockCount--;
  192.         }
  193. }
  194.  
  195. #if defined( BI_OLDNAMES )
  196. #define MBaseMemBlocks TMBaseMemBlocks
  197. #endif
  198.  
  199. /*------------------------------------------------------------------------*/
  200. /*                                                                        */
  201. /*  class TBaseMemBlocks                                                  */
  202. /*                                                                        */
  203. /*  Used internally.                                                      */
  204. /*                                                                        */
  205. /*------------------------------------------------------------------------*/
  206.  
  207. class TBaseMemBlocks : public TMBaseMemBlocks<TStandardAllocator>
  208. {
  209.  
  210. public:
  211.  
  212.     TBaseMemBlocks( size_t sz = 8192 ) :
  213.         TMBaseMemBlocks<TStandardAllocator>(sz) {}
  214.  
  215. };
  216.  
  217. #if defined( BI_OLDNAMES )
  218. #define BaseMemBlocks TBaseMemBlocks
  219. #endif
  220.  
  221. /*------------------------------------------------------------------------*/
  222. /*                                                                        */
  223. /*  template <class Alloc> class TMMemStack                               */
  224. /*                                                                        */
  225. /*  Managed memory stack.  Implements mark and release style memory       */
  226. /*  management, using the allocator Alloc.                                */
  227. /*                                                                        */
  228. /*------------------------------------------------------------------------*/
  229.  
  230. template <class Alloc> class TMMarker;
  231.  
  232. template <class Alloc> class TMMemStack
  233. {
  234.  
  235. public:
  236.  
  237.     friend TMMarker<Alloc>;
  238.  
  239.     TMMemStack( size_t = 8192 );
  240.  
  241.     void *Allocate( size_t );
  242.  
  243. #if defined( BI_OLDNAMES )
  244.     void *allocate( size_t sz ) { return Allocate( sz ); }
  245. #endif  // BI_OLDNAMES
  246.  
  247. private:
  248.  
  249.     TMBaseMemBlocks<Alloc> Data;
  250.     size_t CurLoc;
  251.  
  252. };
  253.  
  254. #if defined( BI_OLDNAMES )
  255. #define MMemStack TMMemStack
  256. #endif
  257.  
  258. template <class Alloc>
  259. inline void *operator new( size_t sz, TMMemStack<Alloc>& m )
  260. {
  261.     return m.Allocate( sz );
  262. }
  263.  
  264. template <class Alloc>
  265. inline void *operator new [] ( size_t sz, TMMemStack<Alloc>& m )
  266. {
  267.     return m.Allocate( sz );
  268. }
  269.  
  270. template <class Alloc> inline TMMemStack<Alloc>::TMMemStack( size_t sz ) :
  271.     Data( sz ),
  272.     CurLoc(sz)
  273. {
  274.     CHECK( sz != 0 );
  275. }
  276.  
  277. template <class Alloc> void *TMMemStack<Alloc>::Allocate( size_t sz )
  278. {
  279.     sz = max( 1U, sz );
  280.     if( sz > Data.GetBlockSize() - CurLoc )
  281.         if( Data.AllocBlock( sz ) == 0 )
  282.             return 0;
  283.         else
  284.             CurLoc = 0;
  285.     void *temp = Data.Block() + CurLoc;
  286.     CurLoc += sz;
  287.     return temp;
  288. }
  289.  
  290. /*------------------------------------------------------------------------*/
  291. /*                                                                        */
  292. /*  template <class Alloc> class TMMarker                                 */
  293. /*                                                                        */
  294. /*  Provides the mark for TMMemStack.                                     */
  295. /*                                                                        */
  296. /*------------------------------------------------------------------------*/
  297.  
  298. template <class Alloc> class TMMarker
  299. {
  300.  
  301. public:
  302.  
  303.     TMMarker( TMMemStack<Alloc>& ms ) :
  304.         Memstk(ms),
  305.         Blk(ms.Data.Count()),
  306.         CurLoc(ms.CurLoc)
  307.         {
  308.         }
  309.  
  310.     ~TMMarker()
  311.         {
  312.         PRECONDITION( Blk < Memstk.Data.Count() ||
  313.               (Blk == Memstk.Data.Count() && CurLoc <= Memstk.CurLoc )
  314.             );
  315.         Memstk.Data.FreeTo( Blk );
  316.         Memstk.CurLoc = CurLoc;
  317.         }
  318.  
  319.  
  320. private:
  321.  
  322.     TMMemStack<Alloc>& Memstk;
  323.     const unsigned Blk;
  324.     const size_t CurLoc;
  325.  
  326. };
  327.  
  328. #if defined( BI_OLDNAMES )
  329. #define MMarker TMMarker
  330. #endif
  331.  
  332. /*------------------------------------------------------------------------*/
  333. /*                                                                        */
  334. /*  class TMemStack                                                       */
  335. /*                                                                        */
  336. /*  Implements mark and release style memory management using the         */
  337. /*  standard allocator.                                                   */
  338. /*                                                                        */
  339. /*------------------------------------------------------------------------*/
  340.  
  341. class TMemStack : public TMMemStack<TStandardAllocator>
  342. {
  343. public:
  344.  
  345.     TMemStack( size_t sz = 8192 ) : TMMemStack<TStandardAllocator>(sz) {}
  346.  
  347. };
  348.  
  349. #if defined( BI_OLDNAMES )
  350. #define MemStack TMemStack
  351. #endif
  352.  
  353. /*------------------------------------------------------------------------*/
  354. /*                                                                        */
  355. /*  template <class Alloc> class TMarker                                  */
  356. /*                                                                        */
  357. /*  Provides the mark for TMemStack.                                      */
  358. /*                                                                        */
  359. /*------------------------------------------------------------------------*/
  360.  
  361. class TMarker : public TMMarker<TStandardAllocator>
  362. {
  363.  
  364. public:
  365.  
  366.     TMarker( TMMemStack<TStandardAllocator>& ms ) :
  367.         TMMarker<TStandardAllocator>(ms) {}
  368. };
  369.  
  370.  
  371. /*------------------------------------------------------------------------*/
  372. /*                                                                        */
  373. /*  template <class Alloc> class TMMemBlocks                              */
  374. /*                                                                        */
  375. /*  Managed single-size block allocator.  Allocates blocks                */
  376. /*  of the size specified in the constructor, using the memory            */
  377. /*  manager specified by Alloc.                                           */
  378. /*                                                                        */
  379. /*------------------------------------------------------------------------*/
  380.  
  381. template <class Alloc> class TMMemBlocks
  382. {
  383.  
  384. public:
  385.  
  386.     TMMemBlocks( size_t, unsigned = 100 );
  387.  
  388.     void *Allocate( size_t );
  389.     void Free( void * );
  390.  
  391. #if defined( BI_OLDNAMES )
  392.     void *allocate( size_t sz ) { return Allocate(sz); }
  393.     void free( void *ptr ) { Free(ptr); }
  394. #endif  // BI_OLDNAMES
  395.  
  396. private:
  397.  
  398.     void *FreeList;
  399.     TMMemStack<Alloc> Mem;
  400.     size_t Size;
  401.  
  402. };
  403.  
  404. #if defined( BI_OLDNAMES )
  405. #define MMemBlocks TMemBlocks
  406. #endif
  407.  
  408. template <class Alloc>
  409. inline TMMemBlocks<Alloc>::TMMemBlocks( size_t sz, unsigned count ) :
  410.     Mem( sz*count ),
  411.     FreeList(0),
  412.     Size( max(sz,sizeof(void *)) )
  413. {
  414.     CHECK( sz != 0 && count != 0 );
  415. }
  416.  
  417. #pragma argsused
  418. template <class Alloc>
  419. inline void *TMMemBlocks<Alloc>::Allocate( size_t sz )
  420. {
  421.     PRECONDITION( Size == max(sz,sizeof(void *)) );
  422.     if( FreeList == 0 )
  423.         return Mem.Allocate( Size );
  424.     else
  425.         {
  426.         void *temp = FreeList;
  427.         FreeList = *(void **)temp;
  428.         return temp;
  429.         }
  430. }
  431.  
  432. template <class Alloc>
  433. inline void TMMemBlocks<Alloc>::Free( void *block )
  434. {
  435.     *(void **)block = FreeList;
  436.     FreeList = block;
  437. }
  438.  
  439. /*------------------------------------------------------------------------*/
  440. /*                                                                        */
  441. /*  class TMemBlocks                                                      */
  442. /*                                                                        */
  443. /*  Single-size block allocator.  Allocates blocks of the size            */
  444. /*  specified in the constructor, using the global operator new           */
  445. /*  and operator delete.                                                  */
  446. /*                                                                        */
  447. /*------------------------------------------------------------------------*/
  448.  
  449. class TMemBlocks : public TMMemBlocks<TStandardAllocator>
  450. {
  451.  
  452. public:
  453.  
  454.     TMemBlocks( size_t sz, unsigned n = 100 ) :
  455.         TMMemBlocks<TStandardAllocator>( sz, n ) {}
  456.  
  457. };
  458.  
  459. #if defined( BI_OLDNAMES )
  460. #define MemBlocks TMemBlocks
  461. #endif
  462.  
  463. typedef TMMemBlocks<TStandardAllocator> TStandardBlocks;
  464.  
  465. #if defined( BI_PLAT_WIN16 ) && defined( BI_DATA_FAR )
  466. typedef TMMemBlocks<TSharedAllocator> TSharedBlocks;
  467. #endif
  468.  
  469. #if defined( BI_CLASSLIB_NO_po )
  470. #pragma option -po.
  471. #endif
  472.  
  473. #pragma option -Vo.
  474.  
  475. #endif  // CLASSLIB_MEMMGR_H
  476.  
  477.