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.

187 lines
3.4 KiB

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