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.

195 lines
3.6 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation
  6. //
  7. // File: kpmem.cxx
  8. //
  9. // Contents: Routines to wrap memory allocation, etc.
  10. //
  11. // History: 10-Jul-2001 t-ryanj Created
  12. //
  13. //------------------------------------------------------------------------
  14. #include "kpmem.h"
  15. HANDLE KpHeap = NULL;
  16. #ifdef DBG
  17. LONG KpAllocs = 0;
  18. #endif
  19. //+-------------------------------------------------------------------------
  20. //
  21. // Function: KpInitMem
  22. //
  23. // Synopsis: Does any initialization required before allocating memory
  24. // using KpAlloc/KpFree
  25. //
  26. // Effects:
  27. //
  28. // Arguments:
  29. //
  30. // Requires:
  31. //
  32. // Returns: Success value. If FALSE is returned, memory has not been
  33. // initialized, and KpAlloc/KpFree cannot be safely called.
  34. //
  35. // Notes:
  36. //
  37. //
  38. //--------------------------------------------------------------------------
  39. BOOL
  40. KpInitMem(
  41. VOID
  42. )
  43. {
  44. //
  45. // Create a private, growable heap
  46. //
  47. DsysAssert( KpHeap == NULL );
  48. KpHeap = HeapCreate( 0, 0, 0 );
  49. if( !KpHeap )
  50. {
  51. DebugLog( DEB_ERROR, "%s(%d): Could not create heap: 0x%x.\n", __FILE__, __LINE__, GetLastError() );
  52. goto Error;
  53. }
  54. return TRUE;
  55. Error:
  56. KpCleanupMem();
  57. return FALSE;
  58. }
  59. //+-------------------------------------------------------------------------
  60. //
  61. // Function: KpCleanupMem
  62. //
  63. // Synopsis: Undoes whatever initialization was done by KpInitMem
  64. //
  65. // Effects:
  66. //
  67. // Arguments:
  68. //
  69. // Requires:
  70. //
  71. // Returns:
  72. //
  73. // Notes:
  74. //
  75. //
  76. //--------------------------------------------------------------------------
  77. VOID
  78. KpCleanupMem(
  79. VOID
  80. )
  81. {
  82. #if DBG
  83. //
  84. // Assert that we freed all our memory.
  85. //
  86. DsysAssert( KpAllocs == 0 );
  87. #endif
  88. //
  89. // Destroy our private heap.
  90. //
  91. if( KpHeap )
  92. {
  93. HeapDestroy( KpHeap );
  94. KpHeap = NULL;
  95. }
  96. }
  97. //+-------------------------------------------------------------------------
  98. //
  99. // Function: KpAlloc
  100. //
  101. // Synopsis: Wrapper to be used for all alloc calls.
  102. //
  103. // Effects:
  104. //
  105. // Arguments:
  106. //
  107. // Requires:
  108. //
  109. // Returns:
  110. //
  111. // Notes:
  112. //
  113. //
  114. //--------------------------------------------------------------------------
  115. LPVOID
  116. KpAlloc(
  117. SIZE_T size
  118. )
  119. {
  120. PVOID pv;
  121. pv = HeapAlloc( KpHeap, 0, size );
  122. #ifdef DBG
  123. if( pv )
  124. InterlockedIncrement(&KpAllocs);
  125. #endif
  126. return pv;
  127. }
  128. //+-------------------------------------------------------------------------
  129. //
  130. // Function: KpFree
  131. //
  132. // Synopsis: Wrapper to be used for all free calls.
  133. //
  134. // Effects:
  135. //
  136. // Arguments:
  137. //
  138. // Requires:
  139. //
  140. // Returns:
  141. //
  142. // Notes:
  143. //
  144. //
  145. //--------------------------------------------------------------------------
  146. BOOL
  147. KpFree(
  148. LPVOID buffer
  149. )
  150. {
  151. #ifdef DBG
  152. InterlockedDecrement(&KpAllocs);
  153. #endif
  154. return HeapFree( KpHeap, 0, buffer );
  155. }
  156. //+-------------------------------------------------------------------------
  157. //
  158. // Function: KpReAlloc
  159. //
  160. // Synopsis: Wrapper to be used for realloc calls.
  161. //
  162. // Effects:
  163. //
  164. // Arguments: buffer - buffer to realloc
  165. // size - new size
  166. //
  167. // Requires:
  168. //
  169. // Returns: New pointer to buffer.
  170. //
  171. // Notes:
  172. //
  173. //--------------------------------------------------------------------------
  174. LPVOID
  175. KpReAlloc(
  176. LPVOID buffer,
  177. SIZE_T size
  178. )
  179. {
  180. return HeapReAlloc( KpHeap, 0, buffer, size );
  181. }