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.

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