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

  1. /*--------------------------------------------------------------------*/
  2. /*                                                                    */
  3. /*  ALLOCTR.H                                                         */
  4. /*                                                                    */
  5. /*  Copyright (c) 1992, 1994 Borland International                    */
  6. /*  All Rights Reserved                                               */
  7. /*                                                                    */
  8. /*--------------------------------------------------------------------*/
  9.  
  10. #if !defined( CLASSLIB_ALLOCTR_H )
  11. #define CLASSLIB_ALLOCTR_H
  12.  
  13. #if !defined(___DEFS_H)
  14. #include <_defs.h>
  15. #endif
  16.  
  17. #if !defined( __STDLIB_H )
  18. #include <stdlib.h>
  19. #endif
  20.  
  21. #if !defined( CLASSLIB_DEFS_H )
  22. #include <classlib/defs.h>
  23. #endif
  24.  
  25. #if defined( BI_CLASSLIB_NO_po )
  26. #pragma option -po-
  27. #endif
  28.  
  29. /*------------------------------------------------------------------------*/
  30. /*                                                                        */
  31. /*  class TStandardAllocator                                              */
  32. /*                                                                        */
  33. /*  Provides class-specific operator new and operator delete that         */
  34. /*  simply call the global operator new and operator delete.  That        */
  35. /*  is, TStandardAllocator does not provide any specialized behavior.     */
  36. /*  It is used in the non-managed versions of the parametrized            */
  37. /*  containers.                                                           */
  38. /*                                                                        */
  39. /*------------------------------------------------------------------------*/
  40.  
  41. class _BIDSCLASS TStandardAllocator
  42. {
  43. public:
  44.     friend void _BIDSFAR * _RTLENTRY _EXPFUNC32 
  45.             operator new( size_t sz, const TStandardAllocator& )
  46.         { return ::operator new( sz ); }
  47.     friend void _BIDSFAR * _RTLENTRY _EXPFUNC32 
  48.             operator new [] ( size_t sz, const TStandardAllocator& )
  49.         { return ::operator new [] ( sz ); }
  50.     friend void _BIDSFAR * _RTLENTRY _EXPFUNC32 
  51.          operator new( unsigned, void *ptr )
  52.         { return ptr; }
  53.     friend void _BIDSFAR * _RTLENTRY _EXPFUNC32 
  54.          operator new [] ( unsigned, void *ptr )
  55.         { return ptr; }
  56.     void _RTLENTRY _EXPFUNC32 operator delete( void _BIDSFAR *ptr )
  57.         { ::operator delete( ptr ); }
  58.     void _RTLENTRY _EXPFUNC32 operator delete [] ( void _BIDSFAR *ptr )
  59.         { ::operator delete [] ( ptr ); }
  60. };
  61.  
  62. #if defined( BI_OLDNAMES )
  63. #define BI_StandardAllocator TStandardAllocator
  64. #endif
  65.  
  66. /*------------------------------------------------------------------------*/
  67. /*                                                                        */
  68. /*  class TSharedAllocator                                                */
  69. /*                                                                        */
  70. /*  Provides class-specific operator new and operator delete that         */
  71. /*  allocate from shared memory.                                          */
  72. /*                                                                        */
  73. /*------------------------------------------------------------------------*/
  74.  
  75. class _BIDSFARCLASS TSharedAllocator
  76. {
  77. public:
  78.     friend void _BIDSFARDATA *operator new( size_t sz, const TSharedAllocator& );
  79.     friend void _BIDSFARDATA *operator new [] ( size_t sz, const TSharedAllocator& );
  80.     void operator delete( void _BIDSFARDATA *ptr );
  81.     void operator delete [] ( void _BIDSFARDATA *ptr );
  82. };
  83.  
  84. #if defined( BI_OLDNAMES )
  85. #define BI_SharedAllocator TSharedAllocator
  86. #endif
  87.  
  88. /*------------------------------------------------------------------------*/
  89. /*                                                                        */
  90. /*  template <class T, class Alloc> class TManaged_T                      */
  91. /*                                                                        */
  92. /*  Provides a parametrized wrapper for type T which uses a               */
  93. /*  class-specific operator new and operator delete supplied              */
  94. /*  by Alloc.                                                             */
  95. /*                                                                        */
  96. /*------------------------------------------------------------------------*/
  97.  
  98. template <class T, class Alloc> class TManaged_T : private Alloc
  99. {
  100.  
  101. public:
  102.  
  103.     Alloc::operator delete;
  104.     Alloc::operator delete [];
  105.  
  106.     TManaged_T() {}
  107.     TManaged_T( const T& t ) : data(t)
  108.         {
  109.         }
  110.  
  111.     const TManaged_T& operator = ( const TManaged_T& t )
  112.         {
  113.         data = t.data;
  114.         return *this;
  115.         }
  116.  
  117.     operator T&()
  118.         {
  119.         return data;
  120.         }
  121.  
  122. private:
  123.  
  124.     T data;
  125.  
  126. };
  127.  
  128. #if defined( BI_OLDNAMES )
  129. #define BI_Managed_T TManaged_T
  130. #endif
  131.  
  132. /*------------------------------------------------------------------------*/
  133. /*                                                                        */
  134. /*  TSharedAllocator::operator new                                        */
  135. /*  TSharedAllocator::operator delete                                     */
  136. /*                                                                        */
  137. /*  When compiling for WINDOWS, allocates memory as GMEM_DDESHARE.        */
  138. /*  When compiling for DOS, uses the global operator new and              */
  139. /*  operator delete.                                                      */
  140. /*                                                                        */
  141. /*------------------------------------------------------------------------*/
  142.  
  143. #if !defined( BI_PLAT_WIN32 ) && !defined( BI_PLAT_OS2 )
  144. //  Can't test for BI_PLAT_WIN16, because some memory models
  145. //  of the classlib aren't built with Windows switches
  146.  
  147. #if !defined( __WINDOWS_H )
  148. #define STRICT
  149. #include <windows.h>
  150. #endif
  151.  
  152. #if !defined( __DOS_H )
  153. #include <dos.h>
  154. #endif
  155.  
  156. inline void _BIDSFARDATA *operator new( size_t sz,
  157.                                 const TSharedAllocator& )
  158. {
  159.     return GlobalLock( GlobalAlloc( GPTR | GMEM_DDESHARE, sz ) );
  160. }
  161.  
  162. inline void _BIDSFARDATA *operator new [] ( size_t sz,
  163.                                       const TSharedAllocator& )
  164. {
  165.     return GlobalLock( GlobalAlloc( GPTR | GMEM_DDESHARE, sz ) );
  166. }
  167.  
  168. inline void TSharedAllocator::operator delete( void _BIDSFARDATA *ptr )
  169. {
  170.     HGLOBAL hMem = (HGLOBAL)GlobalHandle(FP_SEG(ptr));
  171.     if( GlobalUnlock(hMem) == 0 )
  172.         GlobalFree(hMem);
  173. }
  174.  
  175. inline void TSharedAllocator::operator delete [] ( void _BIDSFARDATA *ptr )
  176. {
  177.     HGLOBAL hMem = (HGLOBAL)GlobalHandle(FP_SEG(ptr));
  178.     if( GlobalUnlock(hMem) == 0 )
  179.         GlobalFree(hMem);
  180. }
  181.  
  182. #else
  183.  
  184. inline void _BIDSFAR *operator new( size_t sz,
  185.                                     const TSharedAllocator& )
  186. {
  187.     return ::operator new( sz );
  188. }
  189.  
  190. inline void _BIDSFAR *operator new [] ( size_t sz,
  191.                                     const TSharedAllocator& )
  192. {
  193.     return ::operator new [] ( sz );
  194. }
  195.  
  196. inline void TSharedAllocator::operator delete( void _BIDSFAR *ptr )
  197. {
  198.     ::operator delete( ptr );
  199. }
  200.  
  201. inline void TSharedAllocator::operator delete [] ( void _BIDSFAR *ptr )
  202. {
  203.     ::operator delete [] ( ptr );
  204. }
  205.  
  206. #endif  // !BI_PLAT_WIN32 && !BI_PLAT_OS2
  207.  
  208. #if defined( BI_CLASSLIB_NO_po )
  209. #pragma option -po.
  210. #endif
  211.  
  212. #endif  // CLASSLIB_ALLOCTR_H
  213.  
  214.