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.

307 lines
6.0 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLMEM_H__
  11. #define __ATLMEM_H__
  12. #pragma once
  13. #include <atlbase.h>
  14. #include <limits.h>
  15. namespace ATL
  16. {
  17. template< typename N >
  18. inline N AtlAlignUp( N n, ULONG nAlign ) throw()
  19. {
  20. return( N( (n+(nAlign-1))&~(N( nAlign )-1) ) );
  21. }
  22. template< typename N >
  23. inline N AtlAlignDown( N n, ULONG nAlign ) throw()
  24. {
  25. return( N( n&~(N( nAlign )-1) ) );
  26. }
  27. __interface __declspec(uuid("654F7EF5-CFDF-4df9-A450-6C6A13C622C0")) IAtlMemMgr
  28. {
  29. public:
  30. void* Allocate( size_t nBytes ) throw();
  31. void Free( void* p ) throw();
  32. void* Reallocate( void* p, size_t nBytes ) throw();
  33. size_t GetSize( void* p ) throw();
  34. };
  35. #ifndef _ATL_MIN_CRT
  36. class CCRTHeap :
  37. public IAtlMemMgr
  38. {
  39. public:
  40. virtual void* Allocate( size_t nBytes ) throw()
  41. {
  42. return( malloc( nBytes ) );
  43. }
  44. virtual void Free( void* p ) throw()
  45. {
  46. free( p );
  47. }
  48. virtual void* Reallocate( void* p, size_t nBytes ) throw()
  49. {
  50. return( realloc( p, nBytes ) );
  51. }
  52. virtual size_t GetSize( void* p ) throw()
  53. {
  54. return( _msize( p ) );
  55. }
  56. public:
  57. };
  58. #endif //!_ATL_MIN_CRT
  59. class CWin32Heap :
  60. public IAtlMemMgr
  61. {
  62. public:
  63. CWin32Heap() throw() :
  64. m_hHeap( NULL ),
  65. m_bOwnHeap( false )
  66. {
  67. }
  68. CWin32Heap( HANDLE hHeap ) throw() :
  69. m_hHeap( hHeap ),
  70. m_bOwnHeap( false )
  71. {
  72. ATLASSERT( hHeap != NULL );
  73. }
  74. CWin32Heap( DWORD dwFlags, size_t nInitialSize, size_t nMaxSize = 0 ) :
  75. m_hHeap( NULL ),
  76. m_bOwnHeap( true )
  77. {
  78. ATLASSERT( !(dwFlags&HEAP_GENERATE_EXCEPTIONS) );
  79. m_hHeap = ::HeapCreate( dwFlags, nInitialSize, nMaxSize );
  80. if( m_hHeap == NULL )
  81. {
  82. AtlThrowLastWin32();
  83. }
  84. }
  85. ~CWin32Heap() throw()
  86. {
  87. if( m_bOwnHeap && (m_hHeap != NULL) )
  88. {
  89. BOOL bSuccess;
  90. bSuccess = ::HeapDestroy( m_hHeap );
  91. ATLASSERT( bSuccess );
  92. }
  93. }
  94. void Attach( HANDLE hHeap, bool bTakeOwnership ) throw()
  95. {
  96. ATLASSERT( hHeap != NULL );
  97. ATLASSERT( m_hHeap == NULL );
  98. m_hHeap = hHeap;
  99. m_bOwnHeap = bTakeOwnership;
  100. }
  101. HANDLE Detach() throw()
  102. {
  103. HANDLE hHeap;
  104. hHeap = m_hHeap;
  105. m_hHeap = NULL;
  106. m_bOwnHeap = false;
  107. return( hHeap );
  108. }
  109. // IAtlMemMgr
  110. virtual void* Allocate( size_t nBytes ) throw()
  111. {
  112. return( ::HeapAlloc( m_hHeap, 0, nBytes ) );
  113. }
  114. virtual void Free( void* p ) throw()
  115. {
  116. if( p != NULL )
  117. {
  118. BOOL bSuccess;
  119. bSuccess = ::HeapFree( m_hHeap, 0, p );
  120. ATLASSERT( bSuccess );
  121. }
  122. }
  123. virtual void* Reallocate( void* p, size_t nBytes ) throw()
  124. {
  125. if( p == NULL )
  126. {
  127. return( Allocate( nBytes ) );
  128. }
  129. else
  130. {
  131. return( ::HeapReAlloc( m_hHeap, 0, p, nBytes ) );
  132. }
  133. }
  134. virtual size_t GetSize( void* p ) throw()
  135. {
  136. return( ::HeapSize( m_hHeap, 0, p ) );
  137. }
  138. public:
  139. HANDLE m_hHeap;
  140. bool m_bOwnHeap;
  141. };
  142. class CComHeap :
  143. public IAtlMemMgr
  144. {
  145. // IAtlMemMgr
  146. public:
  147. virtual void* Allocate( size_t nBytes ) throw()
  148. {
  149. #ifdef _WIN64
  150. if( nBytes > INT_MAX )
  151. {
  152. return( NULL );
  153. }
  154. #endif
  155. return( ::CoTaskMemAlloc( ULONG( nBytes ) ) );
  156. }
  157. virtual void Free( void* p ) throw()
  158. {
  159. ::CoTaskMemFree( p );
  160. }
  161. virtual void* Reallocate( void* p, size_t nBytes ) throw()
  162. {
  163. #ifdef _WIN64
  164. if( nBytes > INT_MAX )
  165. {
  166. return( NULL );
  167. }
  168. #endif
  169. return( ::CoTaskMemRealloc( p, ULONG( nBytes ) ) );
  170. }
  171. virtual size_t GetSize( void* p ) throw()
  172. {
  173. CComPtr< IMalloc > pMalloc;
  174. ::CoGetMalloc( 1, &pMalloc );
  175. return( pMalloc->GetSize( p ) );
  176. }
  177. };
  178. class CLocalHeap :
  179. public IAtlMemMgr
  180. {
  181. // IAtlMemMgr
  182. public:
  183. virtual void* Allocate( size_t nBytes ) throw()
  184. {
  185. return( ::LocalAlloc( LMEM_FIXED, nBytes ) );
  186. }
  187. virtual void Free( void* p ) throw()
  188. {
  189. ::LocalFree( p );
  190. }
  191. virtual void* Reallocate( void* p, size_t nBytes ) throw()
  192. {
  193. return( ::LocalReAlloc( p, nBytes, 0 ) );
  194. }
  195. virtual size_t GetSize( void* p ) throw()
  196. {
  197. return( ::LocalSize( p ) );
  198. }
  199. };
  200. class CGlobalHeap :
  201. public IAtlMemMgr
  202. {
  203. // IAtlMemMgr
  204. public:
  205. virtual void* Allocate( size_t nBytes ) throw()
  206. {
  207. return( ::GlobalAlloc( LMEM_FIXED, nBytes ) );
  208. }
  209. virtual void Free( void* p ) throw()
  210. {
  211. ::GlobalFree( p );
  212. }
  213. virtual void* Reallocate( void* p, size_t nBytes ) throw()
  214. {
  215. return( ::GlobalReAlloc( p, nBytes, 0 ) );
  216. }
  217. virtual size_t GetSize( void* p ) throw()
  218. {
  219. return( ::GlobalSize( p ) );
  220. }
  221. };
  222. /////////////////////////////////////////////////////////////////////////////
  223. // OLE task memory allocation support
  224. inline LPWSTR AtlAllocTaskWideString(LPCWSTR lpszString) throw()
  225. {
  226. if (lpszString == NULL)
  227. return NULL;
  228. UINT nSize = (UINT)((wcslen(lpszString)+1) * sizeof(WCHAR));
  229. LPWSTR lpszResult = (LPWSTR)CoTaskMemAlloc(nSize);
  230. if (lpszResult != NULL)
  231. memcpy(lpszResult, lpszString, nSize);
  232. return lpszResult;
  233. }
  234. inline LPWSTR AtlAllocTaskWideString(LPCSTR lpszString) throw()
  235. {
  236. if (lpszString == NULL)
  237. return NULL;
  238. UINT nLen = lstrlenA(lpszString)+1;
  239. LPWSTR lpszResult = (LPWSTR)CoTaskMemAlloc(nLen*sizeof(WCHAR));
  240. if (lpszResult != NULL)
  241. ATLVERIFY(MultiByteToWideChar(CP_ACP, 0, lpszString, -1, lpszResult, nLen));
  242. return lpszResult;
  243. }
  244. inline LPSTR AtlAllocTaskAnsiString(LPCWSTR lpszString) throw()
  245. {
  246. if (lpszString == NULL)
  247. return NULL;
  248. UINT nBytes = (UINT)((wcslen(lpszString)+1)*2);
  249. LPSTR lpszResult = (LPSTR)CoTaskMemAlloc(nBytes);
  250. if (lpszResult != NULL)
  251. ATLVERIFY(WideCharToMultiByte(CP_ACP, 0, lpszString, -1, lpszResult, nBytes, NULL, NULL));
  252. return lpszResult;
  253. }
  254. inline LPSTR AtlAllocTaskAnsiString(LPCSTR lpszString) throw()
  255. {
  256. if (lpszString == NULL)
  257. return NULL;
  258. UINT nSize = lstrlenA(lpszString)+1;
  259. LPSTR lpszResult = (LPSTR)CoTaskMemAlloc(nSize);
  260. if (lpszResult != NULL)
  261. memcpy(lpszResult, lpszString, nSize);
  262. return lpszResult;
  263. }
  264. #ifdef _UNICODE
  265. #define AtlAllocTaskString(x) AtlAllocTaskWideString(x)
  266. #else
  267. #define AtlAllocTaskString(x) AtlAllocTaskAnsiString(x)
  268. #endif
  269. #define AtlAllocTaskOleString(x) AtlAllocTaskWideString(x)
  270. }; // namespace ATL
  271. #endif //__ATLMEM_H__