home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 12.ddi / CLASSINC.PAK / ALLOCTR.H next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  6.9 KB  |  197 lines

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