home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CLASSINC.PAK / ALLOCTR.H next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.1 KB  |  234 lines

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