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.

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