1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7     FxPoolInlines.h
8 
9 Abstract:
10 
11     This module contains inline functions for pool
12 
13 Environment:
14 
15     kernel/user mode
16 
17 Revision History:
18 
19         Made it mode agnostic
20 
21 --*/
22 
23 #ifndef __FX_POOL_INLINES_HPP__
24 #define __FX_POOL_INLINES_HPP__
25 
26 extern "C" {
27 
28 #if defined(EVENT_TRACING)
29 #include "FxPoolInlines.hpp.tmh"
30 #endif
31 
32 }
33 
34 _Must_inspect_result_
35 NTSTATUS
36 __inline
FxPoolAddHeaderSize(__in PFX_DRIVER_GLOBALS FxDriverGlobals,__in size_t AllocationSize,__out size_t * NewSize)37 FxPoolAddHeaderSize(
38     __in    PFX_DRIVER_GLOBALS FxDriverGlobals,
39     __in    size_t AllocationSize,
40     __out   size_t* NewSize
41     )
42 {
43     NTSTATUS status;
44     size_t total;
45 
46     total = AllocationSize;
47 
48     //
49     // sizeof(FX_POOL_HEADER) is too large since it will contain enough memory
50     // for AllocationStart which we compute on our own.
51     //
52     status = RtlSizeTAdd(total, FX_POOL_HEADER_SIZE, &total);
53 
54     if (!NT_SUCCESS(status)) {
55         DoTraceLevelMessage(
56             FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGDEVICE,
57             "Size overflow, could not add pool header, %!STATUS!", status);
58 
59         return status;
60     }
61 
62     if (FxDriverGlobals->IsPoolTrackingOn())  {
63         status = RtlSizeTAdd(total, sizeof(FX_POOL_TRACKER), &total);
64 
65         if (!NT_SUCCESS(status)) {
66             DoTraceLevelMessage(
67                 FxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGDEVICE,
68                 "Size overflow, could not add pool tracker, %!STATUS!", status);
69             return status;
70         }
71     }
72 
73     *NewSize = total;
74 
75     return STATUS_SUCCESS;
76 }
77 
78 VOID
79 __inline
FxPoolInsertNonPagedAllocateTracker(__in PFX_POOL Pool,__in PFX_POOL_TRACKER Tracker,__in SIZE_T Size,__in ULONG Tag,__in PVOID Caller)80 FxPoolInsertNonPagedAllocateTracker(
81     __in PFX_POOL           Pool,
82     __in PFX_POOL_TRACKER   Tracker,
83     __in SIZE_T             Size,
84     __in ULONG              Tag,
85     __in PVOID              Caller
86     )
87 /*++
88 
89 Routine Description:
90 
91     Format and insert a Tracker for a NonPaged allocation.
92 
93 Arguments:
94 
95     Pool    - Pointer to FX_POOL structure
96 
97     Tracker - Pointer to raw FX_POOL_TRACKER structure.
98 
99     Size    - Size in bytes of the allocation
100 
101     Tag     - Caller specified additional tag value for debugging/tracing
102 
103     Caller  - Caller's address
104 
105 Returns:
106 
107     VOID
108 
109 --*/
110 {
111     KIRQL  irql;
112 
113     Tracker->Tag            = Tag;
114     Tracker->PoolType       = NonPagedPool;
115     Tracker->Pool           = Pool;
116     Tracker->Size           = Size;
117     Tracker->CallersAddress = Caller;
118 
119     Pool->NonPagedLock.Acquire(&irql);
120 
121     InsertTailList(&Pool->NonPagedHead, &Tracker->Link);
122 
123     Pool->NonPagedBytes += Size;
124     Pool->NonPagedAllocations++;
125 
126     if( Pool->NonPagedBytes > Pool->PeakNonPagedBytes ) {
127         Pool->PeakNonPagedBytes = Pool->NonPagedBytes;
128     }
129 
130     if( Pool->NonPagedAllocations > Pool->PeakNonPagedAllocations ) {
131         Pool->PeakNonPagedAllocations = Pool->NonPagedAllocations;
132     }
133 
134     Pool->NonPagedLock.Release(irql);
135 }
136 
137 VOID
138 __inline
FxPoolRemoveNonPagedAllocateTracker(__in PFX_POOL_TRACKER Tracker)139 FxPoolRemoveNonPagedAllocateTracker(
140     __in PFX_POOL_TRACKER  Tracker
141     )
142 /*++
143 
144 Routine Description:
145 
146     Decommission a Tracker for a NonPaged allocation.
147 
148 Arguments:
149 
150     Tracker - Pointer to the formatted FX_POOL_TRACKER structure.
151 
152 Returns:
153 
154     VOID
155 
156 --*/
157 {
158     KIRQL irql;
159 
160     Tracker->Pool->NonPagedLock.Acquire(&irql);
161 
162     RemoveEntryList(&Tracker->Link);
163 
164     Tracker->Pool->NonPagedBytes -= Tracker->Size;
165     Tracker->Pool->NonPagedAllocations--;
166 
167     Tracker->Pool->NonPagedLock.Release(irql);
168 }
169 
170 VOID
171 __inline
FxPoolInsertPagedAllocateTracker(__in PFX_POOL Pool,__in PFX_POOL_TRACKER Tracker,__in SIZE_T Size,__in ULONG Tag,__in PVOID Caller)172 FxPoolInsertPagedAllocateTracker(
173     __in PFX_POOL           Pool,
174     __in PFX_POOL_TRACKER    Tracker,
175     __in SIZE_T             Size,
176     __in ULONG              Tag,
177     __in PVOID              Caller
178     )
179 /*++
180 
181 Routine Description:
182 
183     Format and insert a Tracker for a Paged allocation.
184 
185 Arguments:
186 
187     Pool    - Pointer to FX_POOL structure
188 
189     Tracker - Pointer to raw FX_POOL_TRACKER structure.
190 
191     Size    - Size in bytes of the allocation
192 
193     Tag     - Caller specified additional tag value for debugging/tracing
194 
195     Caller  - Caller's address
196 
197 Returns:
198 
199     VOID
200 
201 --*/
202 {
203     Tracker->Tag            = Tag;
204     Tracker->PoolType       = PagedPool;
205     Tracker->Pool           = Pool;
206     Tracker->Size           = Size;
207     Tracker->CallersAddress = Caller;
208 
209     Pool->PagedLock.Acquire();
210 
211     InsertTailList(&Pool->PagedHead, &Tracker->Link);
212 
213     Pool->PagedBytes += Size;
214     Pool->PagedAllocations++;
215 
216     if( Pool->PagedBytes > Pool->PeakPagedBytes ) {
217         Pool->PeakPagedBytes = Pool->PagedBytes;
218     }
219 
220     if( Pool->PagedAllocations > Pool->PeakPagedAllocations ) {
221         Pool->PeakPagedAllocations = Pool->PagedAllocations;
222     }
223 
224     Pool->PagedLock.Release();
225 }
226 
227 VOID
228 __inline
FxPoolRemovePagedAllocateTracker(__in PFX_POOL_TRACKER Tracker)229 FxPoolRemovePagedAllocateTracker(
230     __in PFX_POOL_TRACKER  Tracker
231     )
232 /*++
233 
234 Routine Description:
235 
236     Decommission a Tracker for a Paged allocation.
237 
238 Arguments:
239 
240     Tracker - Pointer to the formatted FX_POOL_TRACKER structure.
241 
242 Returns:
243 
244     VOID
245 
246 --*/
247 {
248     Tracker->Pool->PagedLock.Acquire();
249 
250     RemoveEntryList(&Tracker->Link);
251 
252     Tracker->Pool->PagedBytes -= Tracker->Size;
253     Tracker->Pool->PagedAllocations--;
254 
255     Tracker->Pool->PagedLock.Release();
256 }
257 
258 #endif // __FX_POOL_INLINES_HPP__
259