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.

259 lines
5.3 KiB

  1. //
  2. // Copyright (c) 2000 Microsoft Corporation
  3. //
  4. // Module Name
  5. //
  6. // heapwalk.h
  7. //
  8. // Abstract
  9. //
  10. // Contains function prototypes that create/modify/update the
  11. // datastructure HEAP_ENTRY_LIST. HEAP_ENTRY_LIST maintains
  12. // miminum amount of data for a HEAP Object.
  13. //
  14. // These functions are defined in heapwalk.c
  15. //
  16. // Author
  17. //
  18. // Narayana Batchu (nbatchu) [May 11, 2001]
  19. //
  20. #ifndef _HEAPWALK_HPP_
  21. #define _HEAPWALK_HPP_
  22. #include <windows.h>
  23. #include <stdio.h>
  24. //
  25. // NO_MATCH Constant used to initialize the Index into
  26. // HEAP_ENTRY_LIST
  27. //
  28. #define NO_MATCH -1
  29. //
  30. // INITIAL_CAPACITY Initial array size for HEAP_ENTRY_LIST.
  31. //
  32. #define INITIAL_CAPACITY 512
  33. //
  34. // BLOCK_STATE Enumeration
  35. //
  36. // Enumeration of all the possible states a heap
  37. // block exists.
  38. //
  39. // Possible States
  40. //
  41. // HEAP_BLOCK_FREE - The block is free
  42. // HEAP_BLOCK_BUSY - The block is busy (allocated).
  43. //
  44. typedef enum _BLOCK_STATE
  45. {
  46. HEAP_BLOCK_FREE = 0,
  47. HEAP_BLOCK_BUSY = 1
  48. } BLOCK_STATE ;
  49. //
  50. // HEAP_ENTRY_INFO structure.
  51. //
  52. // This structure represents a group of heap blocks whose SIZE
  53. // and STATUS are same.
  54. //
  55. // BlockSize - Holds the size of the allocated/free blocks of the
  56. // heap
  57. //
  58. // BlockCount - Holds the number of blocks whose status and size are
  59. // same.
  60. //
  61. // BlockState - Holds the status of the collection of blocks. They
  62. // can be either allocated (HEAP_ENTRY_BUSY) or free
  63. // (HEAP_ENTRY_FREE).
  64. //
  65. typedef struct _HEAP_ENTRY_INFO
  66. {
  67. ULONG BlockSize;
  68. UINT BlockCount;
  69. BLOCK_STATE BlockState;
  70. } HEAP_ENTRY_INFO, *LPHEAP_ENTRY_INFO;
  71. //
  72. // HEAP_ENTRY_LIST structure
  73. //
  74. // This structure represents a heap (with only minimum amount of
  75. // date collected for each block, such as size and status).
  76. //
  77. // pHeapEntries - Pointer to an array of HEAP_ENTRY_INFO
  78. // structure.
  79. //
  80. // HeapEntryCount - Holds the count of HEAP_ENTRY_INFO structures
  81. // stored in the array 'pHeapEntries'
  82. //
  83. // PresentCapacity - Represents the number of HEAP_ENTRY_INFO structs
  84. // that can be possibly stored with the memory
  85. // allocated.
  86. //
  87. // ListSorted - Boolean that says whether the list is sorted in
  88. // its present state.
  89. //
  90. typedef struct _HEAP_ENTRY_LIST
  91. {
  92. LPHEAP_ENTRY_INFO pHeapEntries;
  93. UINT HeapEntryCount;
  94. UINT PresentCapacity;
  95. BOOL ListSorted;
  96. } HEAP_ENTRY_LIST, *LPHEAP_ENTRY_LIST;
  97. //*************************************************
  98. //
  99. // Allocating memory for heap list.
  100. //
  101. //*************************************************
  102. VOID
  103. Initialize(
  104. LPHEAP_ENTRY_LIST pList
  105. );
  106. VOID
  107. SetHeapEntry(
  108. LPHEAP_ENTRY_INFO HeapEntryInfo,
  109. USHORT Status,
  110. ULONG Size
  111. );
  112. BOOL
  113. IncreaseCapacity(
  114. LPHEAP_ENTRY_LIST pList
  115. );
  116. //*************************************************
  117. //
  118. // Cleaning up the datastrcuture HEAP_ENTRY_LIST.
  119. //
  120. //*************************************************
  121. VOID
  122. DestroyList(
  123. LPHEAP_ENTRY_LIST pList
  124. );
  125. //*************************************************
  126. //
  127. // Extracting Maximum Block Sizes.
  128. //
  129. //*************************************************
  130. ULONG
  131. GetMaxBlockSize(
  132. LPHEAP_ENTRY_LIST pList,
  133. BLOCK_STATE BlockState
  134. );
  135. ULONG
  136. GetMaxFreeBlockSize(
  137. LPHEAP_ENTRY_LIST pList
  138. );
  139. ULONG
  140. GetMaxAllocBlockSize(
  141. LPHEAP_ENTRY_LIST pList
  142. );
  143. //*************************************************
  144. //
  145. // Extracting Top N Entries.
  146. //
  147. //*************************************************
  148. BOOL
  149. GetTopNentries(
  150. BLOCK_STATE BlockState,
  151. LPHEAP_ENTRY_LIST pList,
  152. LPHEAP_ENTRY_INFO pArray,
  153. UINT Entries
  154. );
  155. BOOL
  156. GetTopNfreeEntries(
  157. LPHEAP_ENTRY_LIST pList,
  158. LPHEAP_ENTRY_INFO pHeapEntries,
  159. UINT Entries
  160. );
  161. BOOL
  162. GetTopNallocEntries(
  163. LPHEAP_ENTRY_LIST pList,
  164. LPHEAP_ENTRY_INFO pHeapEntries,
  165. UINT Entries
  166. );
  167. //*************************************************
  168. //
  169. // Modifying the heap with Insertions & Deletions.
  170. //
  171. //*************************************************
  172. UINT
  173. InsertHeapEntry(
  174. LPHEAP_ENTRY_LIST pList,
  175. LPHEAP_ENTRY_INFO pHeapEntry
  176. );
  177. UINT
  178. DeleteHeapEntry(
  179. LPHEAP_ENTRY_LIST pList,
  180. LPHEAP_ENTRY_INFO pHeapEntry
  181. );
  182. UINT
  183. FindMatch(
  184. LPHEAP_ENTRY_LIST pList,
  185. LPHEAP_ENTRY_INFO pHeapEntry
  186. );
  187. //*************************************************
  188. //
  189. // Sorting the heap list.
  190. //
  191. //*************************************************
  192. VOID
  193. SortHeapEntries(
  194. LPHEAP_ENTRY_LIST pList
  195. );
  196. static int __cdecl
  197. SortByBlockSize(
  198. const void * arg1,
  199. const void * arg2
  200. );
  201. //*************************************************
  202. //
  203. // Display functions for HEAP_ENTRY_LIST.
  204. //
  205. //*************************************************
  206. VOID
  207. DisplayHeapFragStatistics(
  208. FILE * File,
  209. PVOID HeapAddress,
  210. LPHEAP_ENTRY_LIST pList
  211. );
  212. VOID
  213. PrintList(
  214. FILE * File,
  215. LPHEAP_ENTRY_LIST pList,
  216. BLOCK_STATE BlockState
  217. );
  218. #endif