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

  1. //----------------------------------------------------------------------------
  2. // Borland Class Library
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.6  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined(CLASSLIB_HEAPSEL_H)
  9. #define CLASSLIB_HEAPSEL_H
  10.  
  11. #if !defined(CLASSLIB_DEFS_H)
  12. # include <classlib/defs.h>
  13. #endif
  14. #if !defined(__DOS_H) && !defined(_INC_DOS)
  15. # include <dos.h>
  16. #endif
  17. #if !defined(__STDLIB_H) && !defined(_INC_STDLIB)
  18. # include <stdlib.h>
  19. #endif
  20.  
  21. #if defined( BI_CLASSLIB_NO_po )
  22. # pragma option -po-
  23. #endif
  24.  
  25. #if defined(BI_NAMESPACE)
  26. namespace ClassLib {
  27. #endif
  28.  
  29. /*------------------------------------------------------------------------*/
  30. /*                                                                        */
  31. /*  The class THeapSelector provides a mechanism for assuring that two    */
  32. /*  objects will be allocated from the same heap. Any class that inherits */
  33. /*  from THeapSelector can use the placement new operator provided by     */
  34. /*  this class:                                                           */
  35. /*                                                                        */
  36. /*      class Derived : public THeapSelector                              */
  37. /*      {                                                                 */
  38. /*      public:                                                           */
  39. /*          void CreateArray() { Data = new(*this)_TCHAR[17]; }             */
  40. /*      private:                                                          */
  41. /*          _TCHAR *Data;                                                   */
  42. /*      };                                                                */
  43. /*                                                                        */
  44. /*  In this way, any call to CreateArray() on an object of type Derived   */
  45. /*  will allocate the array from the heap that was used to allocate the   */
  46. /*  object itself, regardless of which module actually makes the call to  */
  47. /*  CreateArray(). This is especially useful when the allocation is done  */
  48. /*  in a virtual function that may be overridden in a separate module.    */
  49. /*  Allocating from the same heap as the object helps prevent the sort of */
  50. /*  problems that arise when a DLL holds on to a pointer to an object in  */
  51. /*  an EXE that is no longer executing.                                   */
  52. /*                                                                        */
  53. /*  Note that under Windows THeapSelector is a __huge class. Using it     */
  54. /*  will force all derived classes to be __huge as well. Since exported   */
  55. /*  classes are automatically __huge this won't impose a penalty when     */
  56. /*  used as a base class for an exported class.                           */
  57. /*                                                                        */
  58. /*------------------------------------------------------------------------*/
  59.  
  60. class _BIDSFARCLASS _RTTI THeapSelector
  61. {
  62.  
  63. public:
  64.  
  65.     THeapSelector() {}
  66.  
  67.     friend void _BIDSFARDATA *operator new( size_t sz,
  68.                                         const THeapSelector _BIDSFARDATA &heap );
  69.     void operator delete( void _BIDSFARDATA *ptr );
  70. #if !defined(BI_NO_ARRAYNEW)
  71.     friend void _BIDSFARDATA *operator new[] ( size_t sz,
  72.                                         const THeapSelector _BIDSFARDATA &heap );
  73.     void operator delete[] ( void _BIDSFARDATA *ptr );
  74. #endif
  75.  
  76. // protected:   // workaround
  77.  
  78.     THeapSelector( const THeapSelector _BIDSFARDATA & ) {}
  79.     THeapSelector _BIDSFARDATA & operator = ( const THeapSelector _BIDSFARDATA & )
  80.         { return *this; }
  81.  
  82. private:
  83.  
  84. #if !defined(BI_PTR_O_32)
  85.  
  86.     void _BIDSFARDATA *Allocate( size_t ) const;
  87.     static void Free( void _BIDSFARDATA * );
  88.  
  89.     class HeapSetup
  90.     {
  91.     public:
  92.         HeapSetup( unsigned );
  93.         ~HeapSetup();
  94.     private:
  95.         unsigned SavedDS;
  96.     };
  97.  
  98. #endif
  99.  
  100. };
  101.  
  102. #if defined(BI_PTR_O_32)
  103.  
  104. inline void *operator new( size_t sz,
  105.                            const THeapSelector &heap )
  106. {
  107.     return ::operator new( sz );
  108. }
  109.  
  110. inline void THeapSelector::operator delete( void *ptr )
  111. {
  112.     ::operator delete( ptr );
  113. }
  114.  
  115. #if !defined(BI_NO_ARRAYNEW)
  116.  
  117. inline void *operator new [] ( size_t sz,
  118.                                const THeapSelector &heap )
  119. {
  120.     return ::operator new[]( sz );
  121. }
  122.  
  123. inline void THeapSelector::operator delete [] ( void *ptr )
  124. {
  125.     ::operator delete[]( ptr );
  126. }
  127.  
  128. #endif
  129.  
  130. #else   // 16-bit
  131.  
  132. inline void _BIDSFARDATA *operator new( size_t sz,
  133.                                     const THeapSelector _BIDSFARDATA &heap )
  134. {
  135.     return heap.Allocate( sz );
  136. }
  137.  
  138. inline void THeapSelector::operator delete( void _BIDSFARDATA *ptr )
  139. {
  140.     THeapSelector::Free( ptr );
  141. }
  142.  
  143. #if !defined(BI_NO_ARRAYNEW)
  144. inline void _BIDSFARDATA *operator new [] ( size_t sz,
  145.                                     const THeapSelector _BIDSFARDATA &heap )
  146. {
  147.     return heap.Allocate( sz );
  148. }
  149.  
  150. inline void THeapSelector::operator delete [] ( void _BIDSFARDATA *ptr )
  151. {
  152.     THeapSelector::Free( ptr );
  153. }
  154. #endif
  155.  
  156. inline THeapSelector::HeapSetup::HeapSetup( unsigned seg )
  157. {
  158.     SavedDS = _DS;
  159.     _DS = seg;
  160. }
  161.  
  162. inline THeapSelector::HeapSetup::~HeapSetup()
  163. {
  164.     _DS = SavedDS;
  165. }
  166.  
  167. #endif
  168.  
  169. #if defined(BI_NAMESPACE)
  170. }   // namespace ClassLib
  171. #endif
  172.  
  173. #if defined( BI_CLASSLIB_NO_po )
  174. # pragma option -po.
  175. #endif
  176.  
  177. #endif  // CLASSLIB_HEAPSEL_H
  178.