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.

311 lines
7.4 KiB

  1. /*++ BUILD Version: 0001 // Increment this if a change has global effects
  2. Copyright (c) 1989-1995 Microsoft Corporation
  3. Module Name:
  4. pool.h
  5. Abstract:
  6. Private executive data structures and prototypes for pool management.
  7. There are a number of different pool types:
  8. 1. NonPaged.
  9. 2. Paged.
  10. 3. Session (always paged, but virtualized per TS session).
  11. Author:
  12. Lou Perazzoli (loup) 23-Feb-1989
  13. Landy Wang (landyw) 02-June-1997
  14. Revision History:
  15. --*/
  16. #ifndef _POOL_
  17. #define _POOL_
  18. #if !DBG
  19. #define NO_POOL_CHECKS 1
  20. #endif
  21. #define POOL_CACHE_SUPPORTED 0
  22. #define POOL_CACHE_ALIGN 0
  23. #define NUMBER_OF_POOLS 2
  24. #if defined(NT_UP)
  25. #define NUMBER_OF_PAGED_POOLS 2
  26. #else
  27. #define NUMBER_OF_PAGED_POOLS 4
  28. #endif
  29. #define BASE_POOL_TYPE_MASK 1
  30. #define MUST_SUCCEED_POOL_TYPE_MASK 2
  31. #define CACHE_ALIGNED_POOL_TYPE_MASK 4
  32. #define SESSION_POOL_MASK 32
  33. #define POOL_VERIFIER_MASK 64
  34. #define POOL_DRIVER_MASK 128 // Note this cannot encode into a header.
  35. //
  36. // WARNING: POOL_QUOTA_MASK is overloaded by POOL_QUOTA_FAIL_INSTEAD_OF_RAISE
  37. // which is exported from ex.h.
  38. //
  39. // WARNING: POOL_RAISE_IF_ALLOCATION_FAILURE is exported from ex.h with a
  40. // value of 16.
  41. //
  42. // These definitions are used to control the raising of an exception as the
  43. // result of quota and allocation failures.
  44. //
  45. #define POOL_QUOTA_MASK 8
  46. #define POOL_TYPE_MASK (3)
  47. //
  48. // Size of a pool page.
  49. //
  50. // This must be greater than or equal to the page size.
  51. //
  52. #define POOL_PAGE_SIZE PAGE_SIZE
  53. //
  54. // The page size must be a multiple of the smallest pool block size.
  55. //
  56. // Define the block size.
  57. //
  58. #if (PAGE_SIZE == 0x4000)
  59. #define POOL_BLOCK_SHIFT 5
  60. #elif (PAGE_SIZE == 0x2000)
  61. #define POOL_BLOCK_SHIFT 4
  62. #else
  63. #if defined (_WIN64)
  64. #define POOL_BLOCK_SHIFT 4
  65. #else
  66. #define POOL_BLOCK_SHIFT 3
  67. #endif
  68. #endif
  69. #define POOL_LIST_HEADS (POOL_PAGE_SIZE / (1 << POOL_BLOCK_SHIFT))
  70. #define PAGE_ALIGNED(p) (!(((ULONG_PTR)p) & (POOL_PAGE_SIZE - 1)))
  71. //
  72. // Define page end macro.
  73. //
  74. #define PAGE_END(Address) (((ULONG_PTR)(Address) & (PAGE_SIZE - 1)) == 0)
  75. //
  76. // Define pool descriptor structure.
  77. //
  78. typedef struct _POOL_DESCRIPTOR {
  79. POOL_TYPE PoolType;
  80. ULONG PoolIndex;
  81. ULONG RunningAllocs;
  82. ULONG RunningDeAllocs;
  83. ULONG TotalPages;
  84. ULONG TotalBigPages;
  85. ULONG Threshold;
  86. PVOID LockAddress;
  87. PVOID PendingFrees;
  88. LONG PendingFreeDepth;
  89. LIST_ENTRY ListHeads[POOL_LIST_HEADS];
  90. } POOL_DESCRIPTOR, *PPOOL_DESCRIPTOR;
  91. //
  92. // Caveat Programmer:
  93. //
  94. // The pool header must be QWORD (8 byte) aligned in size. If it
  95. // is not, the pool allocation code will trash the allocated
  96. // buffer.
  97. //
  98. //
  99. //
  100. // The layout of the pool header is:
  101. //
  102. // 31 23 16 15 7 0
  103. // +----------------------------------------------------------+
  104. // | Current Size | PoolType+1 | Pool Index |Previous Size |
  105. // +----------------------------------------------------------+
  106. // | ProcessBilled (NULL if not allocated with quota) |
  107. // +----------------------------------------------------------+
  108. // | Zero or more longwords of pad such that the pool header |
  109. // | is on a cache line boundary and the pool body is also |
  110. // | on a cache line boundary. |
  111. // +----------------------------------------------------------+
  112. //
  113. // PoolBody:
  114. //
  115. // +----------------------------------------------------------+
  116. // | Used by allocator, or when free FLINK into sized list |
  117. // +----------------------------------------------------------+
  118. // | Used by allocator, or when free BLINK into sized list |
  119. // +----------------------------------------------------------+
  120. // ... rest of pool block...
  121. //
  122. //
  123. // N.B. The size fields of the pool header are expressed in units of the
  124. // smallest pool block size.
  125. //
  126. typedef struct _POOL_HEADER {
  127. union {
  128. struct {
  129. USHORT PreviousSize : 9;
  130. USHORT PoolIndex : 7;
  131. USHORT BlockSize : 9;
  132. USHORT PoolType : 7;
  133. };
  134. ULONG Ulong1; // used for InterlockedCompareExchange required by Alpha
  135. };
  136. #if defined (_WIN64)
  137. ULONG PoolTag;
  138. #endif
  139. union {
  140. EPROCESS *ProcessBilled;
  141. #if !defined (_WIN64)
  142. ULONG PoolTag;
  143. #endif
  144. struct {
  145. USHORT AllocatorBackTraceIndex;
  146. USHORT PoolTagHash;
  147. };
  148. };
  149. } POOL_HEADER, *PPOOL_HEADER;
  150. //
  151. // Define size of pool block overhead.
  152. //
  153. #define POOL_OVERHEAD ((LONG)sizeof(POOL_HEADER))
  154. //
  155. // Define size of pool block overhead when the block is on a freelist.
  156. //
  157. #define POOL_FREE_BLOCK_OVERHEAD (POOL_OVERHEAD + sizeof (LIST_ENTRY))
  158. //
  159. // Define dummy type so computation of pointers is simplified.
  160. //
  161. typedef struct _POOL_BLOCK {
  162. UCHAR Fill[1 << POOL_BLOCK_SHIFT];
  163. } POOL_BLOCK, *PPOOL_BLOCK;
  164. //
  165. // Define size of smallest pool block.
  166. //
  167. #define POOL_SMALLEST_BLOCK (sizeof(POOL_BLOCK))
  168. //
  169. // Define pool tracking information.
  170. //
  171. #define POOL_BACKTRACEINDEX_PRESENT 0x8000
  172. #if POOL_CACHE_SUPPORTED
  173. #define POOL_BUDDY_MAX PoolBuddyMax
  174. #else
  175. #define POOL_BUDDY_MAX \
  176. (POOL_PAGE_SIZE - (POOL_OVERHEAD + POOL_SMALLEST_BLOCK ))
  177. #endif
  178. //
  179. // Pool support routines are not for general consumption.
  180. // These are only used by the memory manager.
  181. //
  182. VOID
  183. ExInitializePoolDescriptor (
  184. IN PPOOL_DESCRIPTOR PoolDescriptor,
  185. IN POOL_TYPE PoolType,
  186. IN ULONG PoolIndex,
  187. IN ULONG Threshold,
  188. IN PVOID PoolLock
  189. );
  190. VOID
  191. ExDeferredFreePool (
  192. IN PPOOL_DESCRIPTOR PoolDesc
  193. );
  194. #define EX_CHECK_POOL_FREES_FOR_ACTIVE_TIMERS 0x1
  195. #define EX_CHECK_POOL_FREES_FOR_ACTIVE_WORKERS 0x2
  196. #define EX_CHECK_POOL_FREES_FOR_ACTIVE_RESOURCES 0x4
  197. #define EX_KERNEL_VERIFIER_ENABLED 0x8
  198. #define EX_VERIFIER_DEADLOCK_DETECTION_ENABLED 0x10
  199. #define EX_SPECIAL_POOL_ENABLED 0x20
  200. #define EX_PRINT_POOL_FAILURES 0x40
  201. #define EX_STOP_ON_POOL_FAILURES 0x80
  202. #define EX_SEPARATE_HOT_PAGES_DURING_BOOT 0x100
  203. #define EX_DELAY_POOL_FREES 0x200
  204. VOID
  205. ExSetPoolFlags (
  206. IN ULONG PoolFlag
  207. );
  208. //++
  209. //SIZE_T
  210. //EX_REAL_POOL_USAGE (
  211. // IN SIZE_T SizeInBytes
  212. // );
  213. //
  214. // Routine Description:
  215. //
  216. // This routine determines the real pool cost of the supplied allocation.
  217. //
  218. // Arguments
  219. //
  220. // SizeInBytes - Supplies the allocation size in bytes.
  221. //
  222. // Return Value:
  223. //
  224. // TRUE if unused segment trimming should be initiated, FALSE if not.
  225. //
  226. //--
  227. #define EX_REAL_POOL_USAGE(SizeInBytes) \
  228. (((SizeInBytes) > POOL_BUDDY_MAX) ? \
  229. (ROUND_TO_PAGES(SizeInBytes)) : \
  230. (((SizeInBytes) + POOL_OVERHEAD + (POOL_SMALLEST_BLOCK - 1)) & ~(POOL_SMALLEST_BLOCK - 1)))
  231. typedef struct _POOL_TRACKER_TABLE {
  232. ULONG Key;
  233. ULONG NonPagedAllocs;
  234. ULONG NonPagedFrees;
  235. SIZE_T NonPagedBytes;
  236. ULONG PagedAllocs;
  237. ULONG PagedFrees;
  238. SIZE_T PagedBytes;
  239. } POOL_TRACKER_TABLE, *PPOOL_TRACKER_TABLE;
  240. //
  241. // N.B. The last entry of the pool tracker table is used for all overflow
  242. // table entries.
  243. //
  244. extern PPOOL_TRACKER_TABLE PoolTrackTable;
  245. typedef struct _POOL_TRACKER_BIG_PAGES {
  246. PVOID Va;
  247. ULONG Key;
  248. ULONG NumberOfPages;
  249. } POOL_TRACKER_BIG_PAGES, *PPOOL_TRACKER_BIG_PAGES;
  250. #endif