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.

251 lines
4.9 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. BOOL
  107. IncreaseCapacity(
  108. LPHEAP_ENTRY_LIST pList
  109. );
  110. //*************************************************
  111. //
  112. // Cleaning up the datastrcuture HEAP_ENTRY_LIST.
  113. //
  114. //*************************************************
  115. VOID
  116. DestroyList(
  117. LPHEAP_ENTRY_LIST pList
  118. );
  119. //*************************************************
  120. //
  121. // Extracting Maximum Block Sizes.
  122. //
  123. //*************************************************
  124. ULONG
  125. GetMaxBlockSize(
  126. LPHEAP_ENTRY_LIST pList,
  127. BLOCK_STATE BlockState
  128. );
  129. ULONG
  130. GetMaxFreeBlockSize(
  131. LPHEAP_ENTRY_LIST pList
  132. );
  133. ULONG
  134. GetMaxAllocBlockSize(
  135. LPHEAP_ENTRY_LIST pList
  136. );
  137. //*************************************************
  138. //
  139. // Extracting Top N Entries.
  140. //
  141. //*************************************************
  142. BOOL
  143. GetTopNentries(
  144. BLOCK_STATE BlockState,
  145. LPHEAP_ENTRY_LIST pList,
  146. LPHEAP_ENTRY_INFO pArray,
  147. UINT Entries
  148. );
  149. BOOL
  150. GetTopNfreeEntries(
  151. LPHEAP_ENTRY_LIST pList,
  152. LPHEAP_ENTRY_INFO pHeapEntries,
  153. UINT Entries
  154. );
  155. BOOL
  156. GetTopNallocEntries(
  157. LPHEAP_ENTRY_LIST pList,
  158. LPHEAP_ENTRY_INFO pHeapEntries,
  159. UINT Entries
  160. );
  161. //*************************************************
  162. //
  163. // Modifying the heap with Insertions & Deletions.
  164. //
  165. //*************************************************
  166. UINT
  167. InsertHeapEntry(
  168. LPHEAP_ENTRY_LIST pList,
  169. LPHEAP_ENTRY_INFO pHeapEntry
  170. );
  171. UINT
  172. DeleteHeapEntry(
  173. LPHEAP_ENTRY_LIST pList,
  174. LPHEAP_ENTRY_INFO pHeapEntry
  175. );
  176. UINT
  177. FindMatch(
  178. LPHEAP_ENTRY_LIST pList,
  179. LPHEAP_ENTRY_INFO pHeapEntry
  180. );
  181. //*************************************************
  182. //
  183. // Sorting the heap list.
  184. //
  185. //*************************************************
  186. VOID
  187. SortHeapEntries(
  188. LPHEAP_ENTRY_LIST pList
  189. );
  190. static int __cdecl
  191. SortByBlockSize(
  192. const void * arg1,
  193. const void * arg2
  194. );
  195. //*************************************************
  196. //
  197. // Display functions for HEAP_ENTRY_LIST.
  198. //
  199. //*************************************************
  200. VOID
  201. DisplayHeapFragStatistics(
  202. FILE * File,
  203. PVOID HeapAddress,
  204. LPHEAP_ENTRY_LIST pList
  205. );
  206. VOID
  207. PrintList(
  208. FILE * File,
  209. LPHEAP_ENTRY_LIST pList,
  210. BLOCK_STATE BlockState
  211. );
  212. #endif