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.

299 lines
6.8 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. #include "precomp.h"
  8. #include "genutils.h"
  9. #include "arena.h"
  10. #include "sync.h"
  11. #include "reg.h"
  12. #include "arrtempl.h"
  13. #define STARTUP_HEAP_HINT_REGVAL_W L"Startup Heap Preallocation Size"
  14. static HANDLE g_hHeap = NULL;
  15. static class DefaultInitializer
  16. {
  17. public:
  18. DefaultInitializer()
  19. {
  20. CWin32DefaultArena::WbemHeapInitialize( GetProcessHeap() );
  21. }
  22. } g_hDefaultInitializer;
  23. BOOL CWin32DefaultArena::WbemHeapInitialize( HANDLE hHeap )
  24. {
  25. if ( g_hHeap != NULL )
  26. {
  27. return FALSE;
  28. }
  29. g_hHeap = hHeap;
  30. return TRUE;
  31. }
  32. void CWin32DefaultArena::WbemHeapFree()
  33. {
  34. if ( g_hHeap == NULL )
  35. {
  36. return;
  37. }
  38. if (g_hHeap != GetProcessHeap())
  39. HeapDestroy(g_hHeap);
  40. g_hHeap = NULL;
  41. return;
  42. }
  43. //
  44. //***************************************************************************
  45. LPVOID CWin32DefaultArena::WbemMemAlloc(SIZE_T dwBytes)
  46. {
  47. if ( g_hHeap == NULL )
  48. return NULL;
  49. return HeapAlloc( g_hHeap, 0, dwBytes);
  50. }
  51. //***************************************************************************
  52. //
  53. //***************************************************************************
  54. LPVOID CWin32DefaultArena::WbemMemReAlloc(LPVOID pOriginal, SIZE_T dwNewSize)
  55. {
  56. if ( g_hHeap == NULL )
  57. return NULL;
  58. return HeapReAlloc( g_hHeap, 0, pOriginal, dwNewSize);
  59. }
  60. //***************************************************************************
  61. //
  62. //***************************************************************************
  63. BOOL CWin32DefaultArena::WbemMemFree(LPVOID pBlock)
  64. {
  65. if ( g_hHeap == NULL )
  66. return FALSE;
  67. if (pBlock==0)
  68. return TRUE;
  69. return HeapFree( g_hHeap, 0, pBlock);
  70. }
  71. //***************************************************************************
  72. //
  73. //***************************************************************************
  74. SIZE_T CWin32DefaultArena::WbemMemSize(LPVOID pBlock)
  75. {
  76. if ( g_hHeap == NULL )
  77. return 0;
  78. return HeapSize( g_hHeap, 0, pBlock);
  79. }
  80. //***************************************************************************
  81. //
  82. //***************************************************************************
  83. BSTR CWin32DefaultArena::WbemSysAllocString(const wchar_t *wszString)
  84. {
  85. if ( g_hHeap == NULL )
  86. return NULL;
  87. BSTR pBuffer = SysAllocString(wszString);
  88. return pBuffer;
  89. }
  90. //***************************************************************************
  91. //
  92. //***************************************************************************
  93. BSTR CWin32DefaultArena::WbemSysAllocStringByteLen(const char *szString, UINT len)
  94. {
  95. if ( g_hHeap == NULL )
  96. return NULL;
  97. BSTR pBuffer = SysAllocStringByteLen(szString, len);
  98. return pBuffer;
  99. }
  100. //***************************************************************************
  101. //
  102. //****************************************************************************
  103. INT CWin32DefaultArena::WbemSysReAllocString(BSTR *bszString, const wchar_t *wszString)
  104. {
  105. if ( g_hHeap == NULL )
  106. return FALSE;
  107. INT nRet = SysReAllocString(bszString, wszString);
  108. return nRet;
  109. }
  110. //***************************************************************************
  111. //
  112. //***************************************************************************
  113. BSTR CWin32DefaultArena::WbemSysAllocStringLen(const wchar_t *wszString, UINT len)
  114. {
  115. if ( g_hHeap == NULL )
  116. return NULL;
  117. BSTR pBuffer = SysAllocStringLen(wszString, len);
  118. return pBuffer;
  119. }
  120. //***************************************************************************
  121. //
  122. //***************************************************************************
  123. int CWin32DefaultArena::WbemSysReAllocStringLen( BSTR *bszString,
  124. const wchar_t *wszString,
  125. UINT nLen)
  126. {
  127. if ( g_hHeap == NULL )
  128. return FALSE;
  129. INT nRet = SysReAllocStringLen(bszString, wszString, nLen);
  130. return nRet;
  131. }
  132. //***************************************************************************
  133. //
  134. //***************************************************************************
  135. BOOL CWin32DefaultArena::WbemOutOfMemory()
  136. {
  137. return FALSE;
  138. }
  139. BOOL CWin32DefaultArena::ValidateMemSize(BOOL bLargeValidation)
  140. {
  141. if ( g_hHeap == NULL )
  142. return FALSE;
  143. MEMORYSTATUS memBuffer;
  144. memset(&memBuffer, 0, sizeof(MEMORYSTATUS));
  145. memBuffer.dwLength = sizeof(MEMORYSTATUS);
  146. DWORD dwMemReq = 0;
  147. if (bLargeValidation)
  148. dwMemReq = 0x400000; //4MB
  149. else
  150. dwMemReq = 0x200000; //2MB
  151. GlobalMemoryStatus(&memBuffer);
  152. if (memBuffer.dwAvailPageFile >= dwMemReq)
  153. {
  154. return TRUE;
  155. }
  156. static CCritSec cs;
  157. try
  158. {
  159. cs.Enter();
  160. }
  161. catch(...)
  162. {
  163. return FALSE;
  164. }
  165. //THIS ABSOLUTELY HAS TO BE HeapAlloc, and not the WBEM Allocator!!!
  166. LPVOID pBuff = HeapAlloc( g_hHeap, 0, dwMemReq);
  167. //THIS ABSOLUTELY HAS TO BE HeapAlloc, and not the WBEM Allocator!!!
  168. if (pBuff == NULL)
  169. {
  170. cs.Leave();
  171. return FALSE;
  172. }
  173. HeapFree( g_hHeap, 0, pBuff);
  174. GlobalMemoryStatus(&memBuffer);
  175. cs.Leave();
  176. if (memBuffer.dwAvailPageFile >= dwMemReq)
  177. {
  178. return TRUE;
  179. }
  180. return FALSE;
  181. }
  182. HANDLE CWin32DefaultArena::GetArenaHeap()
  183. {
  184. return g_hHeap;
  185. }
  186. BOOL CWin32DefaultArena::WriteHeapHint()
  187. {
  188. if ( g_hHeap == NULL )
  189. return FALSE;
  190. //
  191. // Don't bother if not on NT. We will use internal NT APIs
  192. //
  193. if(!IsNT())
  194. return FALSE;
  195. //
  196. // Don't bother if running in a client --- only WinMgmt uses hints
  197. //
  198. if(!IsWinMgmt())
  199. return FALSE;
  200. //
  201. // In WinMgmt. Walk the heap to calculate total size
  202. //
  203. PROCESS_HEAP_ENTRY entry;
  204. entry.lpData = NULL;
  205. DWORD dwTotalAllocations = 0;
  206. while(HeapWalk( g_hHeap, &entry))
  207. {
  208. if(entry.wFlags & PROCESS_HEAP_ENTRY_BUSY)
  209. {
  210. //
  211. // Allocated block. Add both it's size and its overhead to the total
  212. // We want the overhead since it figures into the total required
  213. // commitment.
  214. //
  215. dwTotalAllocations += entry.cbData + entry.cbOverhead;
  216. }
  217. }
  218. //
  219. // Write the total to the registry. Note that we write this data even if
  220. // startup preallocations are disabled, since they may be enabled before
  221. // WinMgmt starts up the next time
  222. //
  223. HKEY hKey;
  224. long lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, WBEM_REG_WINMGMT, 0,
  225. KEY_SET_VALUE, &hKey);
  226. if(lRes)
  227. return FALSE;
  228. CRegCloseMe cm1(hKey);
  229. lRes = RegSetValueExW(hKey, STARTUP_HEAP_HINT_REGVAL_W, 0,
  230. REG_DWORD, (const BYTE*)&dwTotalAllocations, sizeof(DWORD));
  231. if(lRes)
  232. return FALSE;
  233. return TRUE;
  234. }