1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7     FxPool.h
8 
9 Abstract:
10 
11     This module contains private Pool package definitions
12 
13 Author:
14 
15 
16 
17 
18 Environment:
19 
20     Both kernel and user mode
21 
22 Revision History:
23 
24 
25         Made it mode agnostic
26 
27         New failure paths:
28             Initialization failures of paged/non-paged lock -
29 
30             Upon failure we disable pool tracking, rest of the behavior
31             remains unchanged, and FxPoolInitialize not being bubbled up
32             doesn't become an issue.
33 
34 --*/
35 
36 #ifndef _FXPOOL_H_
37 #define _FXPOOL_H_
38 
39 //
40 // Common pool header for small allocations (less than PAGE_SIZE)
41 //
42 struct FX_POOL_HEADER {
43 
44     PVOID                 Base;
45 
46     PFX_DRIVER_GLOBALS    FxDriverGlobals;
47 
48     DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT) ULONG AllocationStart[1];
49 };
50 
51 typedef FX_POOL_HEADER* PFX_POOL_HEADER;
52 
53 #define FX_POOL_HEADER_SIZE      FIELD_OFFSET(FX_POOL_HEADER, AllocationStart)
54 
55 
56 //
57 // This structure described an indivdually tracked pool.
58 //
59 // The frameworks tracks pool on behalf of the frameworks (global),
60 // and per driver.
61 //
62 
63 struct FX_POOL {
64     MxLockNoDynam       NonPagedLock;
65     LIST_ENTRY          NonPagedHead;
66 
67     MxPagedLockNoDynam  PagedLock;
68     LIST_ENTRY          PagedHead;
69 
70     // Current Pool Usage Information
71     SIZE_T      NonPagedBytes;
72     SIZE_T      PagedBytes;
73 
74     ULONG       NonPagedAllocations;
75     ULONG       PagedAllocations;
76 
77     // Peak Pool Usage Information
78     SIZE_T      PeakNonPagedBytes;
79     SIZE_T      PeakPagedBytes;
80 
81     ULONG       PeakNonPagedAllocations;
82     ULONG       PeakPagedAllocations;
83 };
84 
85 typedef FX_POOL *PFX_POOL;
86 
87 //
88 // This structure is allocated along with the pool item and
89 // is used to track it.
90 //
91 // Note: We would be messing up cache aligned if its greater
92 //       than 16.
93 //
94 //       Our struct is 7 DWORD's on an x86, and 11 DWORDS on 64 bit
95 //       machines.
96 //
97 //       This rounds up to 8 or 12 DWORDS.
98 //
99 //
DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT)100 struct DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT) FX_POOL_TRACKER {
101     LIST_ENTRY Link;
102     PFX_POOL   Pool;
103     ULONG      Tag;
104     SIZE_T     Size;
105     POOL_TYPE  PoolType;
106     PVOID      CallersAddress;
107 };
108 
109 typedef FX_POOL_TRACKER *PFX_POOL_TRACKER;
110 
111 /*++
112 
113 Routine Description:
114 
115     Initialize the FX_POOL tracking object
116 
117 Arguments:
118 
119     Pool    - FX_POOL object for tracking allocations
120 
121 Returns:
122 
123     status
124 
125 --*/
126 _Must_inspect_result_
127 NTSTATUS
128 FxPoolInitialize(
129     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
130     __in PFX_POOL Pool
131     );
132 
133 /*++
134 
135 Routine Description:
136 
137     Destory the FX_POOL tracking object
138 
139 Arguments:
140 
141     Pool    - FX_POOL object for tracking allocations
142 
143 --*/
144 VOID
145 FxPoolDestroy(
146     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
147     __in PFX_POOL  Pool
148     );
149 
150 
151 extern "C"
152 _Must_inspect_result_
153 PWDF_DRIVER_GLOBALS
154 FxAllocateDriverGlobals(
155     VOID
156     );
157 
158 extern "C"
159 VOID
160 FxFreeDriverGlobals(
161     __in PWDF_DRIVER_GLOBALS DriverGlobals
162     );
163 
164 BOOLEAN
165 FxIsPagedPoolType(
166     __in POOL_TYPE Type
167     );
168 
169 /*++
170 
171 Routine Description:
172 
173     Allocates system pool tracked in a FX_POOL tracking object.
174 
175 Arguments:
176 
177     Pool    - FX_POOL object for tracking allocations
178 
179     Type    - POOL_TYPE from ntddk.h
180 
181     Size    - Size in bytes of the allocation
182 
183     Tag     - Caller specified additional tag value for debugging/tracing
184 
185     PVOID   - Caller's address, usefull for finding who allocated the memory
186 
187 Returns:
188 
189     NULL - Could not allocate pool
190     !NULL - Pointer to pool of minimum Size bytes
191 
192 --*/
193 PVOID
194 FxPoolAllocator(
195     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
196     __in PFX_POOL  Pool,
197     __in POOL_TYPE Type,
198     __in SIZE_T    Size,
199     __in ULONG     Tag,
200     __in PVOID     CallersAddress
201     );
202 
203 /*++
204 
205 Routine Description:
206 
207     Release tracked pool
208 
209 Arguments:
210 
211     Pool - FX_POOL object allocation is tracked in
212 
213     ptr - Pointer to pool to release
214 
215 Returns:
216 
217 --*/
218 void
219 FxPoolFree(
220     __in_xcount(ptr is at an offset from AllocationStart)  PVOID ptr
221     );
222 
223 /*++
224 
225 Routine Description:
226 
227     Dump the FX_POOL tracking object
228 
229 Arguments:
230 
231     Pool    - FX_POOL object for tracking allocations
232 
233 Returns:
234 
235     STATUS_SUCCESS
236 
237 --*/
238 NTSTATUS
239 FxPoolDump(
240     __in PFX_DRIVER_GLOBALS FxDriverGlobals,
241     __in PFX_POOL  Pool
242     );
243 
244 /*++
245 
246 Routine Description:
247 
248     Initialize the pool support package at startup time.
249 
250     This must be called before the first allocation.
251 
252 Arguments:
253 
254 Returns:
255 
256     STATUS_SUCCESS
257 
258 --*/
259 _Must_inspect_result_
260 NTSTATUS
261 FxPoolPackageInitialize(
262     __in PFX_DRIVER_GLOBALS FxDriverGlobals
263     );
264 /*++
265 
266 Routine Description:
267 
268     Destroy the pool support package at unload time
269 
270     This must be after the last free
271 
272 Arguments:
273 
274 Returns:
275 
276     status
277 
278 --*/
279 VOID
280 FxPoolPackageDestroy(
281     __in PFX_DRIVER_GLOBALS FxDriverGlobals
282     );
283 
284 
285 
286 #endif // _FXPOOL_H_
287