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

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  HEAPSEL.H                                                             */
  4. /*                                                                        */
  5. /*  Copyright (c) 1993, 1994 Borland International                        */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __HEAPSEL_H )
  11. #define __HEAPSEL_H
  12.  
  13. #if !defined( __DOS_H )
  14. #include <dos.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. /*  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)char[17]; }             */
  40. /*      private:                                                          */
  41. /*          char *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.     friend void _BIDSFARDATA *operator new [] ( size_t sz,
  70.                                         const THeapSelector _BIDSFARDATA &heap );
  71.     void operator delete( void _BIDSFARDATA *ptr );
  72.     void operator delete [] ( void _BIDSFARDATA *ptr );
  73.  
  74. // protected:   // workaround
  75.  
  76.     THeapSelector( const THeapSelector _BIDSFARDATA & ) {};
  77.     THeapSelector _BIDSFARDATA & operator = ( const THeapSelector _BIDSFARDATA & )
  78.         { return *this; };
  79.  
  80. private:
  81.  
  82. #if !defined( __FLAT__ )
  83.  
  84.     void _BIDSFARDATA *Allocate( size_t ) const;
  85.     static void Free( void _BIDSFARDATA * );
  86.  
  87.     class HeapSetup
  88.     {
  89.     public:
  90.         HeapSetup( unsigned );
  91.         ~HeapSetup();
  92.     private:
  93.         unsigned SavedDS;
  94.     };
  95.  
  96. #endif
  97.  
  98. };
  99.  
  100. #if defined( __FLAT__ )
  101.  
  102. inline void *operator new( size_t sz,
  103.                            const THeapSelector &heap )
  104. {
  105.     return ::operator new( sz );
  106. }
  107.  
  108. inline void *operator new [] ( size_t sz,
  109.                                const THeapSelector &heap )
  110. {
  111.     return ::operator new[]( sz );
  112. }
  113.  
  114. inline void THeapSelector::operator delete( void *ptr )
  115. {
  116.     ::operator delete( ptr );
  117. }
  118.  
  119. inline void THeapSelector::operator delete [] ( void *ptr )
  120. {
  121.     ::operator delete[]( ptr );
  122. }
  123.  
  124. #else   // 16-bit
  125.  
  126. inline void _BIDSFARDATA *operator new( size_t sz,
  127.                                     const THeapSelector _BIDSFARDATA &heap )
  128. {
  129.     return heap.Allocate( sz );
  130. }
  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. inline void THeapSelector::operator delete [] ( void _BIDSFARDATA *ptr )
  144. {
  145.     THeapSelector::Free( ptr );
  146. }
  147.  
  148. inline THeapSelector::HeapSetup::HeapSetup( unsigned seg )
  149. {
  150.     SavedDS = _DS;
  151.     _DS = seg;
  152. }
  153.  
  154. inline THeapSelector::HeapSetup::~HeapSetup()
  155. {
  156.     _DS = SavedDS;
  157. }
  158.  
  159. #endif
  160.  
  161. #if defined( BI_CLASSLIB_NO_po )
  162. #pragma option -po.
  163. #endif
  164.  
  165. #endif  // __HEAPSEL_H
  166.  
  167.