Leaked source code of windows server 2003
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.

223 lines
5.3 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. static HANDLE g_hHeap = NULL;
  14. static class DefaultInitializer
  15. {
  16. public:
  17. DefaultInitializer()
  18. {
  19. CWin32DefaultArena::WbemHeapInitialize( GetProcessHeap() );
  20. }
  21. } g_hDefaultInitializer;
  22. BOOL CWin32DefaultArena::WbemHeapInitialize( HANDLE hHeap )
  23. {
  24. if ( g_hHeap != NULL )
  25. {
  26. return FALSE;
  27. }
  28. g_hHeap = hHeap;
  29. return TRUE;
  30. }
  31. void CWin32DefaultArena::WbemHeapFree()
  32. {
  33. if ( g_hHeap == NULL )
  34. {
  35. return;
  36. }
  37. if (g_hHeap != GetProcessHeap())
  38. HeapDestroy(g_hHeap);
  39. g_hHeap = NULL;
  40. return;
  41. }
  42. //
  43. //***************************************************************************
  44. LPVOID CWin32DefaultArena::WbemMemAlloc(SIZE_T dwBytes)
  45. {
  46. if ( g_hHeap == NULL )
  47. return NULL;
  48. return HeapAlloc( g_hHeap, 0, dwBytes);
  49. }
  50. //***************************************************************************
  51. //
  52. //***************************************************************************
  53. LPVOID CWin32DefaultArena::WbemMemReAlloc(LPVOID pOriginal, SIZE_T dwNewSize)
  54. {
  55. if ( g_hHeap == NULL )
  56. return NULL;
  57. return HeapReAlloc( g_hHeap, 0, pOriginal, dwNewSize);
  58. }
  59. //***************************************************************************
  60. //
  61. //***************************************************************************
  62. BOOL CWin32DefaultArena::WbemMemFree(LPVOID pBlock)
  63. {
  64. if ( g_hHeap == NULL )
  65. return FALSE;
  66. if (pBlock==0)
  67. return TRUE;
  68. return HeapFree( g_hHeap, 0, pBlock);
  69. }
  70. //***************************************************************************
  71. //
  72. //***************************************************************************
  73. SIZE_T CWin32DefaultArena::WbemMemSize(LPVOID pBlock)
  74. {
  75. if ( g_hHeap == NULL )
  76. return 0;
  77. return HeapSize( g_hHeap, 0, pBlock);
  78. }
  79. //***************************************************************************
  80. //
  81. //***************************************************************************
  82. BSTR CWin32DefaultArena::WbemSysAllocString(const wchar_t *wszString)
  83. {
  84. if ( g_hHeap == NULL )
  85. return NULL;
  86. BSTR pBuffer = SysAllocString(wszString);
  87. return pBuffer;
  88. }
  89. //***************************************************************************
  90. //
  91. //***************************************************************************
  92. BSTR CWin32DefaultArena::WbemSysAllocStringByteLen(const char *szString, UINT len)
  93. {
  94. if ( g_hHeap == NULL )
  95. return NULL;
  96. BSTR pBuffer = SysAllocStringByteLen(szString, len);
  97. return pBuffer;
  98. }
  99. //***************************************************************************
  100. //
  101. //****************************************************************************
  102. INT CWin32DefaultArena::WbemSysReAllocString(BSTR *bszString, const wchar_t *wszString)
  103. {
  104. if ( g_hHeap == NULL )
  105. return FALSE;
  106. INT nRet = SysReAllocString(bszString, wszString);
  107. return nRet;
  108. }
  109. //***************************************************************************
  110. //
  111. //***************************************************************************
  112. BSTR CWin32DefaultArena::WbemSysAllocStringLen(const wchar_t *wszString, UINT len)
  113. {
  114. if ( g_hHeap == NULL )
  115. return NULL;
  116. BSTR pBuffer = SysAllocStringLen(wszString, len);
  117. return pBuffer;
  118. }
  119. //***************************************************************************
  120. //
  121. //***************************************************************************
  122. int CWin32DefaultArena::WbemSysReAllocStringLen( BSTR *bszString,
  123. const wchar_t *wszString,
  124. UINT nLen)
  125. {
  126. if ( g_hHeap == NULL )
  127. return FALSE;
  128. INT nRet = SysReAllocStringLen(bszString, wszString, nLen);
  129. return nRet;
  130. }
  131. //***************************************************************************
  132. //
  133. //***************************************************************************
  134. BOOL CWin32DefaultArena::WbemOutOfMemory()
  135. {
  136. return FALSE;
  137. }
  138. BOOL CWin32DefaultArena::ValidateMemSize(BOOL bLargeValidation)
  139. {
  140. if ( g_hHeap == NULL )
  141. return FALSE;
  142. MEMORYSTATUS memBuffer;
  143. memset(&memBuffer, 0, sizeof(MEMORYSTATUS));
  144. memBuffer.dwLength = sizeof(MEMORYSTATUS);
  145. DWORD dwMemReq = 0;
  146. if (bLargeValidation)
  147. dwMemReq = 0x400000; //4MB
  148. else
  149. dwMemReq = 0x200000; //2MB
  150. GlobalMemoryStatus(&memBuffer);
  151. if (memBuffer.dwAvailPageFile >= dwMemReq)
  152. {
  153. return TRUE;
  154. }
  155. //THIS ABSOLUTELY HAS TO BE HeapAlloc, and not the WBEM Allocator!!!
  156. LPVOID pBuff = HeapAlloc( g_hHeap, 0, dwMemReq);
  157. //THIS ABSOLUTELY HAS TO BE HeapAlloc, and not the WBEM Allocator!!!
  158. if (pBuff == NULL)
  159. {
  160. return FALSE;
  161. }
  162. HeapFree( g_hHeap, 0, pBuff);
  163. GlobalMemoryStatus(&memBuffer);
  164. if (memBuffer.dwAvailPageFile >= dwMemReq)
  165. {
  166. return TRUE;
  167. }
  168. return FALSE;
  169. }
  170. HANDLE CWin32DefaultArena::GetArenaHeap()
  171. {
  172. return g_hHeap;
  173. }