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.

237 lines
6.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1994 - 1988, Microsoft Corporation
  4. //
  5. // File: tsmem.hxx
  6. //
  7. // Contents: Safe array allocation templates.
  8. //
  9. // Templates: XArray, XArrayVirtual
  10. //
  11. // History: 31-Aug-94 dlee Created
  12. //
  13. //----------------------------------------------------------------------------
  14. #pragma once
  15. //+-------------------------------------------------------------------------
  16. //
  17. // Class: XArray
  18. //
  19. // Purpose: Smart array template based on the new/delete allocator
  20. //
  21. // History: 31-Aug-94 dlee Created
  22. //
  23. //--------------------------------------------------------------------------
  24. template <class T> class XArray
  25. {
  26. public:
  27. XArray() : _cElems( 0 ), _pElems( 0 )
  28. {
  29. }
  30. XArray( unsigned cElems ) : _cElems( cElems )
  31. {
  32. _pElems = new T[cElems];
  33. }
  34. XArray( XArray<T> & src )
  35. {
  36. // don't do this in initializers -- _pElems is declared first
  37. // so the old array is acquired before the count is copied
  38. _cElems = src._cElems;
  39. _pElems = src.Acquire();
  40. }
  41. ~XArray(void) { delete [] _pElems; }
  42. void Init( unsigned cElems )
  43. {
  44. Win4Assert( _pElems == 0 );
  45. _cElems = cElems;
  46. _pElems = new T[cElems];
  47. }
  48. void Init( XArray<T> const & src )
  49. {
  50. Win4Assert( _pElems == 0 );
  51. _cElems = src._cElems;
  52. _pElems = new T[_cElems];
  53. RtlCopyMemory( _pElems, src._pElems, _cElems * sizeof T );
  54. }
  55. void Set( unsigned cElems, T * pElems )
  56. {
  57. Win4Assert( _pElems == 0 );
  58. _cElems = cElems;
  59. _pElems = pElems;
  60. }
  61. T * Get() const { return _pElems; }
  62. T * GetPointer() const { return _pElems; }
  63. T * Acquire() { T * p = _pElems; _pElems = 0; _cElems = 0; return p; }
  64. BOOL IsNull() const { return ( 0 == _pElems); }
  65. T & operator[](ULONG_PTR iElem) { return _pElems[iElem]; }
  66. T const & operator[](ULONG_PTR iElem) const { return _pElems[iElem]; }
  67. unsigned Count() const { return _cElems; }
  68. unsigned SizeOf() const { return _cElems * sizeof T; }
  69. void Free() { delete [] Acquire(); }
  70. void ReSize( unsigned cElems )
  71. {
  72. T * pNew = new T[cElems];
  73. RtlCopyMemory( pNew, _pElems, sizeof T * __min( cElems, _cElems ) );
  74. delete [] _pElems;
  75. _pElems = pNew;
  76. _cElems = cElems;
  77. }
  78. private:
  79. T * _pElems;
  80. unsigned _cElems;
  81. };
  82. //+-------------------------------------------------------------------------
  83. //
  84. // Class: XArrayVirtual
  85. //
  86. // Purpose: Smart array template based on Win32's VirtualAlloc()
  87. //
  88. // History: 2-Aug-95 dlee Created
  89. //
  90. //--------------------------------------------------------------------------
  91. template <class T> class XArrayVirtual
  92. {
  93. public:
  94. XArrayVirtual() : _cElems( 0 ), _pElems( 0 ), _cbReserved(0)
  95. {
  96. }
  97. XArrayVirtual(unsigned cElems, size_t cbReserve = 0) :
  98. _cElems( cElems ),
  99. _cbReserved(cbReserve)
  100. {
  101. if (_cbReserved)
  102. {
  103. Win4Assert( sizeof T * cElems <= _cbReserved );
  104. _pElems = (T *) VirtualAlloc( 0,
  105. _cbReserved,
  106. MEM_RESERVE,
  107. PAGE_READWRITE );
  108. if (_pElems != 0 && cElems > 0)
  109. {
  110. void * pb = VirtualAlloc( _pElems,
  111. sizeof T * cElems,
  112. MEM_COMMIT,
  113. PAGE_READWRITE );
  114. if (0 == pb)
  115. {
  116. VirtualFree( _pElems, 0, MEM_RELEASE );
  117. THROW(CException(E_OUTOFMEMORY));
  118. }
  119. }
  120. }
  121. else
  122. {
  123. _pElems = (T *) VirtualAlloc( 0,
  124. sizeof T * cElems,
  125. MEM_COMMIT,
  126. PAGE_READWRITE );
  127. }
  128. if (0 == _pElems)
  129. THROW(CException(E_OUTOFMEMORY));
  130. }
  131. ~XArrayVirtual(void)
  132. {
  133. if ( 0 != _pElems )
  134. {
  135. VirtualFree( _pElems, sizeof T * _cElems, MEM_DECOMMIT);
  136. VirtualFree( _pElems, 0, MEM_RELEASE );
  137. }
  138. }
  139. void Init( unsigned cElems )
  140. {
  141. Win4Assert( _pElems == 0 );
  142. _cElems = cElems;
  143. _pElems = (T *) VirtualAlloc( 0,
  144. sizeof T * cElems,
  145. MEM_COMMIT,
  146. PAGE_READWRITE );
  147. if (0 == _pElems)
  148. THROW(CException(E_OUTOFMEMORY));
  149. }
  150. void ReInit( unsigned cElems )
  151. {
  152. if ( 0 != _pElems )
  153. VirtualFree( _pElems, 0, MEM_RELEASE );
  154. _cbReserved = _cElems = 0;
  155. _pElems = 0;
  156. Init( cElems );
  157. }
  158. void Resize( unsigned cElems )
  159. {
  160. size_t cbOldSize = sizeof T * _cElems;
  161. size_t cbNewSize = sizeof T * cElems;
  162. Win4Assert( cbNewSize <= _cbReserved );
  163. Win4Assert( _pElems != 0 );
  164. if (cbNewSize > cbOldSize)
  165. {
  166. if (0 == VirtualAlloc(_pElems + cbOldSize,
  167. cbNewSize - cbOldSize,
  168. MEM_COMMIT,
  169. PAGE_READWRITE))
  170. THROW(CException(E_OUTOFMEMORY));
  171. }
  172. else
  173. {
  174. VirtualFree(_pElems + cbNewSize, cbOldSize-cbNewSize, MEM_DECOMMIT);
  175. }
  176. _cElems = cElems;
  177. }
  178. void Set( unsigned cElems, T * pElems )
  179. {
  180. Win4Assert( _pElems == 0 );
  181. _cElems = cElems;
  182. _pElems = pElems;
  183. }
  184. T * Get() const { return _pElems; }
  185. T * GetPointer() const { return _pElems; }
  186. T * Acquire() { T * p = _pElems; _pElems = 0; return p; }
  187. BOOL IsNull() const { return ( 0 == _pElems); }
  188. T & operator[](ULONG iElem) { return _pElems[iElem]; }
  189. T const & operator[](ULONG iElem) const { return _pElems[iElem]; }
  190. unsigned Count() const { return _cElems; }
  191. private:
  192. size_t _cbReserved; // reserved size
  193. T * _pElems;
  194. unsigned _cElems;
  195. };