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.

430 lines
12 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. heap.h
  5. Abstract:
  6. This is the header file that describes the constants and data
  7. structures used by the user mode heap manager, exported by ntdll.dll
  8. and ntrtl.lib
  9. Procedure prototypes are defined in ntrtl.h
  10. Author:
  11. Steve Wood (stevewo) 21-Aug-1992
  12. Revision History:
  13. --*/
  14. #ifndef _RTL_HEAP_
  15. #define _RTL_HEAP_
  16. #include "trace.h"
  17. #define HEAP_LARGE_TAG_MASK 0xFF000000
  18. #define ROUND_UP_TO_POWER2( x, n ) (((ULONG)(x) + ((n)-1)) & ~((n)-1))
  19. #define ROUND_DOWN_TO_POWER2( x, n ) ((ULONG)(x) & ~((n)-1))
  20. typedef struct _HEAP_ENTRY {
  21. //
  22. // This field gives the size of the current block in allocation
  23. // granularity units. (i.e. Size << HEAP_GRANULARITY_SHIFT
  24. // equals the size in bytes).
  25. //
  26. USHORT Size;
  27. //
  28. // This field gives the size of the previous block in allocation
  29. // granularity units. (i.e. PreviousSize << HEAP_GRANULARITY_SHIFT
  30. // equals the size of the previous block in bytes).
  31. //
  32. USHORT PreviousSize;
  33. //
  34. // This field contains the index into the segment that controls
  35. // the memory for this block.
  36. //
  37. UCHAR SegmentIndex;
  38. //
  39. // This field contains various flag bits associated with this block.
  40. // Currently these are:
  41. //
  42. // 0x01 - HEAP_ENTRY_BUSY
  43. // 0x02 - HEAP_ENTRY_EXTRA_PRESENT
  44. // 0x04 - HEAP_ENTRY_FILL_PATTERN
  45. // 0x08 - HEAP_ENTRY_VIRTUAL_ALLOC
  46. // 0x10 - HEAP_ENTRY_LAST_ENTRY
  47. // 0x20 - HEAP_ENTRY_SETTABLE_FLAG1
  48. // 0x40 - HEAP_ENTRY_SETTABLE_FLAG2
  49. // 0x80 - HEAP_ENTRY_SETTABLE_FLAG3
  50. //
  51. UCHAR Flags;
  52. //
  53. // This field contains the number of unused bytes at the end of this
  54. // block that were not actually allocated. Used to compute exact
  55. // size requested prior to rounding requested size to allocation
  56. // granularity. Also used for tail checking purposes.
  57. //
  58. UCHAR UnusedBytes;
  59. //
  60. // Small (8 bit) tag indexes can go here.
  61. //
  62. UCHAR SmallTagIndex;
  63. } HEAP_ENTRY, *PHEAP_ENTRY;
  64. //
  65. // This block describes extra information that might be at the end of a
  66. // busy block.
  67. //
  68. typedef struct _HEAP_ENTRY_EXTRA {
  69. union {
  70. struct {
  71. //
  72. // This field is for debugging purposes. It will normally contain a
  73. // stack back trace index of the allocator for x86 systems.
  74. //
  75. USHORT AllocatorBackTraceIndex;
  76. //
  77. // This field is currently unused, but is intended for storing
  78. // any encoded value that will give the that gives the type of object
  79. // allocated.
  80. //
  81. USHORT TagIndex;
  82. //
  83. // This field is a 32-bit settable value that a higher level heap package
  84. // can use. The Win32 heap manager stores handle values in this field.
  85. //
  86. ULONG Settable;
  87. };
  88. ULONGLONG ZeroInit;
  89. };
  90. } HEAP_ENTRY_EXTRA, *PHEAP_ENTRY_EXTRA;
  91. //
  92. // This structure is present at the end of a free block if HEAP_ENTRY_EXTRA_PRESENT
  93. // is set in the Flags field of a HEAP_FREE_ENTRY structure. It is used to save the
  94. // tag index that was associated with the allocated block after it has been freed.
  95. // Works best when coalesce on free is disabled, along with decommitment.
  96. //
  97. typedef struct _HEAP_FREE_ENTRY_EXTRA {
  98. USHORT TagIndex;
  99. USHORT FreeBackTraceIndex;
  100. } HEAP_FREE_ENTRY_EXTRA, *PHEAP_FREE_ENTRY_EXTRA;
  101. //
  102. // This structure describes a block that lies outside normal heap memory
  103. // as it was allocated with NtAllocateVirtualMemory and has the
  104. // HEAP_ENTRY_VIRTUAL_ALLOC flag set.
  105. //
  106. typedef struct _HEAP_VIRTUAL_ALLOC_ENTRY {
  107. LIST_ENTRY Entry;
  108. HEAP_ENTRY_EXTRA ExtraStuff;
  109. ULONG CommitSize;
  110. ULONG ReserveSize;
  111. HEAP_ENTRY BusyBlock;
  112. } HEAP_VIRTUAL_ALLOC_ENTRY, *PHEAP_VIRTUAL_ALLOC_ENTRY;
  113. typedef struct _HEAP_FREE_ENTRY {
  114. //
  115. // This field gives the size of the current block in allocation
  116. // granularity units. (i.e. Size << HEAP_GRANULARITY_SHIFT
  117. // equals the size in bytes).
  118. //
  119. USHORT Size;
  120. //
  121. // This field gives the size of the previous block in allocation
  122. // granularity units. (i.e. PreviousSize << HEAP_GRANULARITY_SHIFT
  123. // equals the size of the previous block in bytes).
  124. //
  125. USHORT PreviousSize;
  126. //
  127. // This field contains the index into the segment that controls
  128. // the memory for this block.
  129. //
  130. UCHAR SegmentIndex;
  131. //
  132. // This field contains various flag bits associated with this block.
  133. // Currently for free blocks these can be:
  134. //
  135. // 0x02 - HEAP_ENTRY_EXTRA_PRESENT
  136. // 0x04 - HEAP_ENTRY_FILL_PATTERN
  137. // 0x10 - HEAP_ENTRY_LAST_ENTRY
  138. //
  139. UCHAR Flags;
  140. //
  141. // Two fields to encode the location of the bit in FreeListsInUse
  142. // array in HEAP_SEGMENT for blocks of this size.
  143. //
  144. UCHAR Index;
  145. UCHAR Mask;
  146. //
  147. // Free blocks use these two words for linking together free blocks
  148. // of the same size on a doubly linked list.
  149. //
  150. LIST_ENTRY FreeList;
  151. } HEAP_FREE_ENTRY, *PHEAP_FREE_ENTRY;
  152. #define HEAP_GRANULARITY (sizeof( HEAP_ENTRY ))
  153. #define HEAP_GRANULARITY_SHIFT 3 // Log2( HEAP_GRANULARITY )
  154. #define HEAP_MAXIMUM_BLOCK_SIZE (USHORT)(((0x10000 << HEAP_GRANULARITY_SHIFT) - PAGE_SIZE) >> HEAP_GRANULARITY_SHIFT)
  155. #define HEAP_MAXIMUM_FREELISTS 128
  156. #define HEAP_MAXIMUM_SEGMENTS 16
  157. #define HEAP_ENTRY_BUSY 0x01
  158. #define HEAP_ENTRY_EXTRA_PRESENT 0x02
  159. #define HEAP_ENTRY_FILL_PATTERN 0x04
  160. #define HEAP_ENTRY_VIRTUAL_ALLOC 0x08
  161. #define HEAP_ENTRY_LAST_ENTRY 0x10
  162. #define HEAP_ENTRY_SETTABLE_FLAG1 0x20
  163. #define HEAP_ENTRY_SETTABLE_FLAG2 0x40
  164. #define HEAP_ENTRY_SETTABLE_FLAG3 0x80
  165. #define HEAP_ENTRY_SETTABLE_FLAGS 0xE0
  166. //
  167. // HEAP_SEGMENT defines the structure used to describe a range of
  168. // contiguous virtual memory that has been set aside for use by
  169. // a heap.
  170. //
  171. typedef struct _HEAP_UNCOMMMTTED_RANGE {
  172. struct _HEAP_UNCOMMMTTED_RANGE *Next;
  173. ULONG Address;
  174. ULONG Size;
  175. ULONG filler;
  176. } HEAP_UNCOMMMTTED_RANGE, *PHEAP_UNCOMMMTTED_RANGE;
  177. typedef struct _HEAP_SEGMENT {
  178. HEAP_ENTRY Entry;
  179. ULONG Signature;
  180. ULONG Flags;
  181. struct _HEAP *Heap;
  182. ULONG LargestUnCommittedRange;
  183. PVOID BaseAddress;
  184. ULONG NumberOfPages;
  185. PHEAP_ENTRY FirstEntry;
  186. PHEAP_ENTRY LastValidEntry;
  187. ULONG NumberOfUnCommittedPages;
  188. ULONG NumberOfUnCommittedRanges;
  189. PHEAP_UNCOMMMTTED_RANGE UnCommittedRanges;
  190. USHORT AllocatorBackTraceIndex;
  191. USHORT Reserved;
  192. PHEAP_ENTRY LastEntryInSegment;
  193. } HEAP_SEGMENT, *PHEAP_SEGMENT;
  194. #define HEAP_SEGMENT_SIGNATURE 0xFFEEFFEE
  195. #define HEAP_SEGMENT_USER_ALLOCATED (ULONG)0x00000001
  196. //
  197. // HEAP defines the header for a heap.
  198. //
  199. typedef struct _HEAP_LOCK {
  200. union {
  201. RTL_CRITICAL_SECTION CriticalSection;
  202. ERESOURCE Resource;
  203. } Lock;
  204. } HEAP_LOCK, *PHEAP_LOCK;
  205. typedef struct _HEAP_UCR_SEGMENT {
  206. struct _HEAP_UCR_SEGMENT *Next;
  207. ULONG ReservedSize;
  208. ULONG CommittedSize;
  209. ULONG filler;
  210. } HEAP_UCR_SEGMENT, *PHEAP_UCR_SEGMENT;
  211. typedef struct _HEAP_TAG_ENTRY {
  212. ULONG Allocs;
  213. ULONG Frees;
  214. ULONG Size;
  215. USHORT TagIndex;
  216. USHORT CreatorBackTraceIndex;
  217. WCHAR TagName[ 24 ];
  218. } HEAP_TAG_ENTRY, *PHEAP_TAG_ENTRY; // sizeof( HEAP_TAG_ENTRY ) must divide page size evenly
  219. typedef struct _HEAP_PSEUDO_TAG_ENTRY {
  220. ULONG Allocs;
  221. ULONG Frees;
  222. ULONG Size;
  223. } HEAP_PSEUDO_TAG_ENTRY, *PHEAP_PSEUDO_TAG_ENTRY;
  224. typedef struct _HEAP {
  225. HEAP_ENTRY Entry;
  226. ULONG Signature;
  227. ULONG Flags;
  228. ULONG ForceFlags;
  229. ULONG VirtualMemoryThreshold;
  230. ULONG SegmentReserve;
  231. ULONG SegmentCommit;
  232. ULONG DeCommitFreeBlockThreshold;
  233. ULONG DeCommitTotalFreeThreshold;
  234. ULONG TotalFreeSize;
  235. ULONG MaximumAllocationSize;
  236. USHORT ProcessHeapsListIndex;
  237. USHORT HeaderValidateLength;
  238. PVOID HeaderValidateCopy;
  239. USHORT NextAvailableTagIndex;
  240. USHORT MaximumTagIndex;
  241. PHEAP_TAG_ENTRY TagEntries;
  242. PHEAP_UCR_SEGMENT UCRSegments;
  243. PHEAP_UNCOMMMTTED_RANGE UnusedUnCommittedRanges;
  244. ULONG AlignRound;
  245. ULONG AlignMask;
  246. LIST_ENTRY VirtualAllocdBlocks;
  247. PHEAP_SEGMENT Segments[ HEAP_MAXIMUM_SEGMENTS ];
  248. union {
  249. ULONG FreeListsInUseUlong[ HEAP_MAXIMUM_FREELISTS / 32 ];
  250. UCHAR FreeListsInUseBytes[ HEAP_MAXIMUM_FREELISTS / 8 ];
  251. } u;
  252. USHORT FreeListsInUseTerminate;
  253. USHORT AllocatorBackTraceIndex;
  254. PRTL_TRACE_BUFFER TraceBuffer;
  255. ULONG EventLogMask;
  256. PHEAP_PSEUDO_TAG_ENTRY PseudoTagEntries;
  257. LIST_ENTRY FreeLists[ HEAP_MAXIMUM_FREELISTS ];
  258. PHEAP_LOCK LockVariable;
  259. PRTL_HEAP_COMMIT_ROUTINE CommitRoutine;
  260. PVOID Lookaside;
  261. ULONG Reserved[ 1 ];
  262. } HEAP, *PHEAP;
  263. #define HEAP_SIGNATURE (ULONG)0xEEFFEEFF
  264. #define HEAP_LOCK_USER_ALLOCATED (ULONG)0x80000000
  265. #define HEAP_VALIDATE_PARAMETERS_ENABLED (ULONG)0x40000000
  266. #define HEAP_VALIDATE_ALL_ENABLED (ULONG)0x20000000
  267. #define HEAP_SKIP_VALIDATION_CHECKS (ULONG)0x10000000
  268. #define CHECK_HEAP_TAIL_SIZE HEAP_GRANULARITY
  269. #define CHECK_HEAP_TAIL_FILL 0xAB
  270. #define FREE_HEAP_FILL 0xFEEEFEEE
  271. #define ALLOC_HEAP_FILL 0xBAADF00D
  272. #define HEAP_MAXIMUM_SMALL_TAG 0xFF
  273. #define HEAP_SMALL_TAG_MASK (HEAP_MAXIMUM_SMALL_TAG << HEAP_TAG_SHIFT)
  274. #define HEAP_NEED_EXTRA_FLAGS ((HEAP_TAG_MASK ^ HEAP_SMALL_TAG_MASK) | HEAP_SETTABLE_USER_VALUE)
  275. #define HEAP_NUMBER_OF_PSEUDO_TAG (HEAP_MAXIMUM_FREELISTS+1)
  276. #if (HEAP_ENTRY_SETTABLE_FLAG1 ^ \
  277. HEAP_ENTRY_SETTABLE_FLAG2 ^ \
  278. HEAP_ENTRY_SETTABLE_FLAG3 ^ \
  279. HEAP_ENTRY_SETTABLE_FLAGS \
  280. )
  281. #error Invalid HEAP_ENTRY_SETTABLE_FLAGS
  282. #endif
  283. #if ((HEAP_ENTRY_BUSY ^ \
  284. HEAP_ENTRY_EXTRA_PRESENT ^ \
  285. HEAP_ENTRY_FILL_PATTERN ^ \
  286. HEAP_ENTRY_VIRTUAL_ALLOC ^ \
  287. HEAP_ENTRY_LAST_ENTRY ^ \
  288. HEAP_ENTRY_SETTABLE_FLAGS \
  289. ) != \
  290. (HEAP_ENTRY_BUSY | \
  291. HEAP_ENTRY_EXTRA_PRESENT | \
  292. HEAP_ENTRY_FILL_PATTERN | \
  293. HEAP_ENTRY_VIRTUAL_ALLOC | \
  294. HEAP_ENTRY_LAST_ENTRY | \
  295. HEAP_ENTRY_SETTABLE_FLAGS \
  296. ) \
  297. )
  298. #error Conflicting HEAP_ENTRY flags
  299. #endif
  300. #if ((HEAP_SETTABLE_USER_FLAGS >> 4) ^ HEAP_ENTRY_SETTABLE_FLAGS)
  301. #error HEAP_SETTABLE_USER_FLAGS in ntrtl.h conflicts with HEAP_ENTRY_SETTABLE_FLAGS in heap.h
  302. #endif
  303. typedef struct _HEAP_STOP_ON_TAG {
  304. union {
  305. ULONG HeapAndTagIndex;
  306. struct {
  307. USHORT TagIndex;
  308. USHORT HeapIndex;
  309. };
  310. };
  311. } HEAP_STOP_ON_TAG, *PHEAP_STOP_ON_TAG;
  312. typedef struct _HEAP_STOP_ON_VALUES {
  313. ULONG AllocAddress;
  314. HEAP_STOP_ON_TAG AllocTag;
  315. ULONG ReAllocAddress;
  316. HEAP_STOP_ON_TAG ReAllocTag;
  317. ULONG FreeAddress;
  318. HEAP_STOP_ON_TAG FreeTag;
  319. } HEAP_STOP_ON_VALUES, *PHEAP_STOP_ON_VALUES;
  320. #ifndef NTOS_KERNEL_RUNTIME
  321. extern BOOLEAN RtlpDebugHeap;
  322. extern BOOLEAN RtlpDebugPageHeap;
  323. extern BOOLEAN RtlpValidateHeapHdrsEnable; // Set to TRUE if headers are being corrupted
  324. extern BOOLEAN RtlpValidateHeapTagsEnable; // Set to TRUE if tag counts are off and you want to know why
  325. extern HEAP RtlpGlobalTagHeap;
  326. extern HEAP_STOP_ON_VALUES RtlpHeapStopOn;
  327. BOOLEAN
  328. RtlpHeapIsLocked(
  329. IN PVOID HeapHandle
  330. );
  331. #endif // NTOS_KERNEL_RUNTIME
  332. #endif // _RTL_HEAP_