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.

249 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. dbgalloc.cxx
  6. Abstract:
  7. This file contains a heap implementation that is strictly
  8. used for internal allocations used by this debug library.
  9. The concept is to not fill native NT leak detention logs with
  10. allocations from the debug library. When the debug library is
  11. initialized we allocate a single large memory block that the
  12. library then sub allocated from.
  13. Author:
  14. Steve Kiraly (SteveKi) 24-May-1998
  15. Revision History:
  16. --*/
  17. #include "precomp.hxx"
  18. #pragma hdrstop
  19. #include "dbgheap.hxx"
  20. /*++
  21. Title:
  22. DebugLibraryInitializeHeap
  23. Routine Description:
  24. This routine initialize the internal debug library
  25. heap. This routine must be called be for the library
  26. critical section has been created.
  27. Arguments:
  28. None
  29. Return Value:
  30. TRUE debug library heap is initialized
  31. FALSE debug library heap failed internalization
  32. --*/
  33. BOOL
  34. DEBUG_NS::
  35. DebugLibraryInitializeHeap(
  36. VOID
  37. )
  38. {
  39. //
  40. // Hold the critical section while we access the heap.
  41. //
  42. TDebugCriticalSection::TLock CS(GlobalCriticalSection);
  43. //
  44. // Initialize the debug heap.
  45. //
  46. GlobalInternalDebugHeap.Initialize();
  47. //
  48. // Return success or failure based on the debug head state.
  49. //
  50. return GlobalInternalDebugHeap.Valid();
  51. }
  52. /*++
  53. Title:
  54. DebugLibraryDestroyHeap
  55. Routine Description:
  56. This routine releases the internal debug library
  57. heap.
  58. Arguments:
  59. None
  60. Return Value:
  61. TRUE integral debug library heap was destroyed successfully.
  62. FALSE error occurred releasing the internal debug library heap.
  63. --*/
  64. BOOL
  65. DEBUG_NS::
  66. DebugLibraryDestroyHeap(
  67. VOID
  68. )
  69. {
  70. //
  71. // Hold the critical section while we access the heap.
  72. //
  73. TDebugCriticalSection::TLock CS( GlobalCriticalSection );
  74. //
  75. // Destroy the debug heap.
  76. //
  77. GlobalInternalDebugHeap.Destroy();
  78. //
  79. // Return success or failure based on the debug head state.
  80. //
  81. return !GlobalInternalDebugHeap.Valid();
  82. }
  83. /*++
  84. Title:
  85. DebugLibraryWalkHeap
  86. Routine Description:
  87. Walks all the allocated nodes in the debug library heap.
  88. Arguments:
  89. None
  90. Return Value:
  91. TRUE if heap was walked successfully, FALSE if error occurred.
  92. --*/
  93. BOOL
  94. DEBUG_NS::
  95. DebugLibraryWalkHeap(
  96. VOID
  97. )
  98. {
  99. //
  100. // Hold the critical section while we access the heap.
  101. //
  102. TDebugCriticalSection::TLock CS( GlobalCriticalSection );
  103. return GlobalInternalDebugHeap.Walk( NULL, NULL );
  104. }
  105. /*++
  106. Title:
  107. DebugLibraryMalloc
  108. Routine Description:
  109. Allocates memory from the internal debug library heap.
  110. Arguments:
  111. Size - size in bytes of requested memory block.
  112. pVoid - pointer to memory block when called by placement new.
  113. pszFile - file name where allocation was made.
  114. uLine - line number in file where allocation was made.
  115. Return Value:
  116. Pointer to newly allocated block on success,
  117. NULL if memory failed to be allocated.
  118. --*/
  119. PVOID
  120. DEBUG_NS::
  121. DebugLibraryMalloc(
  122. IN SIZE_T Size,
  123. IN PVOID pVoid,
  124. IN LPCTSTR pszFile,
  125. IN UINT uLine
  126. )
  127. {
  128. //
  129. // If we are passed a NULL then we are called from the
  130. // placement new operator, just return the passed in pointer.
  131. //
  132. if (!pVoid)
  133. {
  134. //
  135. // Initialize the debug library, it not already initialized.
  136. //
  137. DebugLibraryInitialize();
  138. //
  139. // Hold the critical section while we access the heap.
  140. //
  141. TDebugCriticalSection::TLock CS(GlobalCriticalSection);
  142. //
  143. // Allocate data from the heap.
  144. //
  145. pVoid = GlobalInternalDebugHeap.Malloc(Size);
  146. }
  147. return pVoid;
  148. }
  149. /*++
  150. Title:
  151. DebugLibraryFree
  152. Routine Description:
  153. Release memory which was allocated from the internal debug library heap.
  154. Arguments:
  155. pData - pointer to previously allocated memory.
  156. Return Value:
  157. None
  158. --*/
  159. VOID
  160. DEBUG_NS::
  161. DebugLibraryFree(
  162. IN PVOID pData
  163. )
  164. {
  165. //
  166. // Initialize the debug library, it not already initialized.
  167. //
  168. DebugLibraryInitialize();
  169. //
  170. // Hold the critical section while we access the heap.
  171. //
  172. TDebugCriticalSection::TLock CS(GlobalCriticalSection);
  173. //
  174. // Release the heap data.
  175. //
  176. GlobalInternalDebugHeap.Free(pData);
  177. }