Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

321 lines
7.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 2000.
  5. //
  6. // File: xolemem.hxx
  7. //
  8. // Contents: OLE memory allocation routines, useful for returning
  9. // memory to the client that the client can free.
  10. //
  11. // Templates: newOLE, deleteOLE, renewOLE, XArrayOLE
  12. //
  13. // History: 11-Jan-95 dlee Created
  14. //
  15. //----------------------------------------------------------------------------
  16. #pragma once
  17. //+-------------------------------------------------------------------------
  18. //
  19. // Class: newOLE
  20. //
  21. // Purpose: Does allocation through the OLE allocator
  22. //
  23. // History: 11-Jan-95 dlee Created
  24. //
  25. //--------------------------------------------------------------------------
  26. inline void * newOLE( unsigned cbAlloc )
  27. {
  28. void *pv = CoTaskMemAlloc( cbAlloc );
  29. if (0 == pv)
  30. THROW(CException(E_OUTOFMEMORY));
  31. return pv;
  32. } //newOle
  33. //+-------------------------------------------------------------------------
  34. //
  35. // Class: deleteOLE
  36. //
  37. // Purpose: Does freeing through the OLE allocator
  38. //
  39. // History: 11-Jan-95 dlee Created
  40. //
  41. //--------------------------------------------------------------------------
  42. template<class T> void deleteOLE( T *pt )
  43. {
  44. CoTaskMemFree(pt);
  45. } //deleteOLE
  46. //+-------------------------------------------------------------------------
  47. //
  48. // Class: renewOLE
  49. //
  50. // Purpose: Does reallocation through the OLE allocator
  51. //
  52. // History: 11-Jan-95 dlee Created
  53. //
  54. //--------------------------------------------------------------------------
  55. template<class T> T * renewOLE( T * ptOld, unsigned cOld, unsigned cNew )
  56. {
  57. if (cOld == cNew)
  58. {
  59. return ptOld;
  60. }
  61. else
  62. {
  63. T *ptNew = (T *) newOLE ( sizeof T * cNew );
  64. unsigned cMin = __min( cOld, cNew );
  65. RtlCopyMemory( ptNew, ptOld, cMin * sizeof T );
  66. deleteOLE( ptOld );
  67. return ptNew;
  68. }
  69. } //renewOLE
  70. //+-------------------------------------------------------------------------
  71. //
  72. // Class: XArrayOLE
  73. //
  74. // Purpose: Smart array template based on the OLE allocator used to
  75. // pass memory between query.dll and the client
  76. //
  77. // History: 11-Jan-95 dlee Created
  78. //
  79. //--------------------------------------------------------------------------
  80. template <class T> class XArrayOLE
  81. {
  82. public:
  83. XArrayOLE()
  84. : _cElems( 0 ), _pElems( 0 )
  85. {
  86. }
  87. XArrayOLE( unsigned cElems)
  88. : _cElems( cElems )
  89. {
  90. _pElems = (T *) newOLE( sizeof T * cElems );
  91. RtlZeroMemory( _pElems, sizeof T * cElems );
  92. }
  93. ~XArrayOLE(void)
  94. {
  95. deleteOLE(_pElems);
  96. }
  97. void Init( unsigned cElems )
  98. {
  99. Win4Assert( _pElems == 0 );
  100. _pElems = (T *) newOLE( sizeof T * cElems );
  101. RtlZeroMemory( _pElems, sizeof T * cElems );
  102. _cElems = cElems;
  103. }
  104. void InitNoThrow( unsigned cElems )
  105. {
  106. Win4Assert( _pElems == 0 );
  107. _pElems = (T *) CoTaskMemAlloc( sizeof T * cElems );
  108. if ( 0 != _pElems )
  109. {
  110. RtlZeroMemory( _pElems, sizeof T * cElems );
  111. _cElems = cElems;
  112. }
  113. }
  114. void Set( unsigned cElems, T * pElems )
  115. {
  116. Win4Assert( _pElems == 0 );
  117. _cElems = cElems;
  118. _pElems = pElems;
  119. }
  120. void GrowToSize( unsigned cElems )
  121. {
  122. Win4Assert( cElems > _cElems );
  123. unsigned newCount = (_cElems * 2) > cElems ? (_cElems * 2) : cElems;
  124. unsigned newSize = newCount * sizeof(T);
  125. unsigned oldSize = _cElems * sizeof(T);
  126. T * pNewElems = (T *) newOLE ( newSize );
  127. RtlCopyMemory( pNewElems, _pElems, oldSize );
  128. RtlZeroMemory( pNewElems + _cElems, newSize - oldSize );
  129. deleteOLE( _pElems );
  130. _cElems = newCount;
  131. _pElems = pNewElems;
  132. }
  133. unsigned SizeOf() { return sizeof T * _cElems; }
  134. T * Get() const { return _pElems; }
  135. T * GetPointer() const { return _pElems; }
  136. T * Acquire() { T * p = _pElems; _pElems = 0; _cElems = 0; return p; }
  137. void Free()
  138. {
  139. deleteOLE( Acquire() );
  140. }
  141. BOOL IsNull() const { return ( 0 == _pElems); }
  142. T & operator[](ULONG iElem) { return _pElems[iElem]; }
  143. T const & operator[](ULONG iElem) const { return _pElems[iElem]; }
  144. unsigned Count() const { return _cElems; }
  145. protected:
  146. T * _pElems;
  147. unsigned _cElems;
  148. };
  149. //+---------------------------------------------------------------------------
  150. //
  151. // Class: XArrayOLEInPlace
  152. //
  153. // Purpose: Calls destructors on the individual array elements.
  154. //
  155. // History: 1-15-97 srikants Created
  156. //
  157. // Notes:
  158. //
  159. //----------------------------------------------------------------------------
  160. template <class T> class XArrayOLEInPlace : public XArrayOLE<T>
  161. {
  162. public:
  163. XArrayOLEInPlace() : XArrayOLE<T>()
  164. {
  165. }
  166. XArrayOLEInPlace( unsigned cElems) : XArrayOLE<T>( cElems )
  167. {
  168. }
  169. ~XArrayOLEInPlace()
  170. {
  171. for ( unsigned i = 0; i < _cElems; i++ )
  172. {
  173. _pElems[i].T::~T();
  174. }
  175. }
  176. void Free()
  177. {
  178. for ( unsigned i = 0; i < _cElems; i++ )
  179. {
  180. _pElems[i].T::~T();
  181. }
  182. deleteOLE( Acquire() );
  183. }
  184. };
  185. //+-------------------------------------------------------------------------
  186. //
  187. // Class: XPtrOLE
  188. //
  189. // Purpose: Smart pointer template based on the OLE allocator used to
  190. // pass memory between query.dll and the client
  191. //
  192. // History: 3-May-95 BartoszM Created
  193. //
  194. //--------------------------------------------------------------------------
  195. template <class T> class XPtrOLE
  196. {
  197. public:
  198. XPtrOLE( T * p = 0 ) : _p ( p )
  199. {
  200. }
  201. XPtrOLE ( XPtrOLE<T> & x) : _p( x.Acquire() )
  202. {
  203. }
  204. ~XPtrOLE(void) { deleteOLE( _p ); }
  205. T* operator->() { return _p; }
  206. T const * operator->() const { return _p; }
  207. BOOL IsNull() const { return (0 == _p); }
  208. void Set ( T* p )
  209. {
  210. Win4Assert (0 == _p);
  211. _p = p;
  212. }
  213. T * Acquire()
  214. {
  215. T * pTemp = _p;
  216. _p = 0;
  217. return pTemp;
  218. }
  219. T & GetReference() const { return *_p; }
  220. T * GetPointer() const { return _p; }
  221. private:
  222. T * _p;
  223. };
  224. //+-------------------------------------------------------------------------
  225. //
  226. // Class: XCom
  227. //
  228. // Purpose: Manages CoInitialize and CoUninitialize
  229. //
  230. // History: 28-Aug-97 dlee Created
  231. //
  232. //--------------------------------------------------------------------------
  233. class XCom
  234. {
  235. public:
  236. XCom( BOOL fAllowAnyThreading = FALSE,
  237. DWORD flags = COINIT_MULTITHREADED ) :
  238. _fInit( FALSE )
  239. {
  240. SCODE sc = CoInitializeEx( 0, flags );
  241. if ( FAILED( sc ) )
  242. {
  243. if ( ! ( RPC_E_CHANGED_MODE == sc && fAllowAnyThreading ) )
  244. THROW( CException( sc ) );
  245. }
  246. else
  247. _fInit = TRUE;
  248. }
  249. ~XCom()
  250. {
  251. if ( _fInit )
  252. CoUninitialize();
  253. }
  254. private:
  255. BOOL _fInit;
  256. };