ReactOS  0.4.15-dev-3453-gff89651
fxpagedlookasidelist.cpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxPagedLookasideList.cpp
8 
9 Abstract:
10 
11  This module implements a frameworks managed FxPagedLookasideList
12 
13 Author:
14 
15 Environment:
16 
17  kernel mode only
18 
19 Revision History:
20 
21 
22 --*/
23 
24 #include "coreprivshared.hpp"
25 
26 #include "fxpagedlookasidelist.hpp"
28 
30  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
32  __in FxDeviceBase* DeviceBase,
33  __in FxDeviceBase* MemoryDeviceBase
34  ) : FxLookasideListFromPool(FxDriverGlobals, sizeof(*this), PoolTag),
35  m_RawBufferSize(0), m_MemoryDeviceBase(MemoryDeviceBase)
36 {
37  SetDeviceBase(DeviceBase);
38 
39  //
40  // Callbacks might be a bit excessive (passive dispose is what we are
41  // really after) because this object has no callbacks, but this is being
42  // proactive in case any callbacks are added. If they are added, this
43  // will take care of them w/out additional changes to object setup.
44  //
46 }
47 
49  VOID
50  )
51 {
52  if (m_MemoryObjectSize != 0) {
54  }
55 
56  if (m_RawBufferSize != 0) {
58  }
59 }
60 
64  __in size_t BufferSize,
66  )
67 {
68  size_t rawBufferSize;
70 
71  if (BufferSize >= PAGE_SIZE) {
72  //
73  // We don't want to burn extra entire pages for tracking information
74  // so we just use the size as is.
75  //
76  rawBufferSize = BufferSize;
77  }
78  else {
79  //
80  // Allocate extra space for tracking the allocation
81  //
83  BufferSize,
84  &rawBufferSize);
85 
86  if (!NT_SUCCESS(status)) {
87  //
88  // FxPoolAddHeaderSize logs to the IFR on error
89  //
90  return status;
91  }
92  }
93 
94  if (UsePagedBufferObject()) {
98  }
99  else {
103  }
104 
105  if (!NT_SUCCESS(status)) {
106  return status;
107  }
108 
109  //
110  // We use m_RawBufferSize == 0 as a condition not to delete the lookaside, so
111  // only assign it a value once we know success is guaranteed.
112  //
114  m_RawBufferSize = rawBufferSize;
115 
116  //
117  // Initialize a non paged pool with these characteristics. All FxObject
118  // derived objects must come from non paged pool.
119  //
121  NULL,
122  NULL,
123  0,
125  m_PoolTag,
126  0);
127 
128  //
129  // Initialize a paged pool with these characteristics.
130  //
131  // Free and Allocate are left intentionally NULL so that we use the Ex
132  // versions.
133  //
134  // bufferSize is used b/c it is full size of the object + pool requirements.
135  // m_BufferSize is the size the client wants the buffer to be.
136  //
138  NULL,
139  NULL,
140  0,
142  m_PoolTag,
143  0);
144 
145  return status;
146 }
147 
148 #pragma prefast(push)
149 
150 
151 
152 //This routine intentionally accesses the header of the allocated memory.
153 #pragma prefast(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_HIGH_PRIORITY)
154 PVOID
156  __out_bcount(this->m_RawBufferSize) PVOID Alloc
157  )
158 /*++
159 
160 Routine Description:
161  Initializes the object allocation so that it can be tracked and inserted
162  in this drivers POOL.
163 
164 Arguments:
165  Alloc - the raw allocation
166 
167 Return Value:
168  the start of where the object memory should be, not necessarily == Alloc
169 
170  --*/
171 {
174  PFX_POOL_TRACKER tracker;
175 
177 
179 
181  //
182  // PoolTracking is active, so format and insert
183  // a tracker in the NonPagedHeader list of the pool.
184  //
185  tracker = (PFX_POOL_TRACKER) Alloc;
187  sizeof(FX_POOL_TRACKER),
189 
190  pHeader->Base = Alloc;
191  pHeader->FxDriverGlobals = pFxDriverGlobals;
192 
195  tracker,
197  m_PoolTag,
198  _ReturnAddress());
199  }
200  else {
201  //
202  // PoolTracking is inactive, only format FX_POOL_HEADER area.
203  //
205  pHeader->Base = Alloc;
206  pHeader->FxDriverGlobals = pFxDriverGlobals;
207  }
208 
209  return &pHeader->AllocationStart[0];
210 }
211 #pragma prefast(pop)
212 
214 NTSTATUS
216  __out FxMemoryObject** PPMemory
217  )
218 {
220  PVOID pObj, pBuf;
221 
222  //
223  // Allocate the object which will contain the 2ndary allocation
224  //
226 
227  if (pObj == NULL) {
229  }
230 
231  pObj = InitObjectAlloc(pObj);
232 
233  //
234  // Create the 2ndary allocation (the one the driver writer uses), what will
235  // be FxMemoryBufferFromPoolLookaside::m_Pool below.
236  //
238  if (pBuf == NULL) {
239  //
240  // This case is safe because Reclaim doesn't treat the pointer as an
241  // object, rather it just performs pointer math and then frees the alloc
242  //
245  }
246 
247  if (m_BufferSize < PAGE_SIZE) {
248  //
249  // For allocations < PAGE, allocate a header, otherwise leave the
250  // allocation alone.
251  //
252  pBuf = InitPagedAlloc(pBuf);
253  }
254  else {
255  //
256  // There is no tracking info before the real allocation start since we
257  // don't want to burn an entire page.
258  //
259  DO_NOTHING();
260  }
261 
262  //
263  // Construct a new FxMemoryBufferFromPoolLookaside using the pool will allocated
264  // above.
265  //
266  // Both objects will know that the base object is one allocation and the
267  // buffer is another. FxMemoryPagedBufferFromPoolLookaside also knows to
268  // dispose itself on the owning FxDeviceBase* pointer.
269  //
270  if (UsePagedBufferObject()) {
273  this,
274  m_BufferSize,
275  pBuf,
277  }
278  else {
281  this,
282  m_BufferSize,
283  pBuf);
284  }
285 
286  //
287  // pBuffer might be displaced if there is a debug extension
288  //
289  ASSERT(_GetBase(pBuffer) == pObj);
290 
291  //
292  // Callbacks might be a bit excessive (passive dispose is what we are
293  // really after) because this object has no callbacks, but this is being
294  // proactive in case any callbacks are added. If they are added, this
295  // will take care of them w/out additional changes to object setup.
296  //
297  pBuffer->MarkPassiveCallbacks(ObjectDoNotLock);
298 
299  *PPMemory = pBuffer;
300 
301  return STATUS_SUCCESS;
302 }
303 
304 VOID
307  )
308 {
310 }
NPAGED_LOOKASIDE_LIST m_ObjectLookaside
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
PVOID InitObjectAlloc(__out_bcount(this->m_MemoryObjectSize) PVOID Alloc)
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList
static VOID _Reclaim(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __inout PNPAGED_LOOKASIDE_LIST List, __in FxMemoryBufferFromLookaside *Memory)
static PVOID _GetBase(__in FxObject *Object)
Definition: fxobject.hpp:418
_Must_inspect_result_ NTSTATUS InitializeLookaside(__in USHORT BufferSize, __in USHORT MemoryObjectSize, __in PWDF_OBJECT_ATTRIBUTES MemoryAttributes)
#define __out_bcount(x)
Definition: dbghelp.h:68
virtual _Must_inspect_result_ NTSTATUS Allocate(__out FxMemoryObject **PPMemory)
LONG NTSTATUS
Definition: precomp.h:26
VOID SetDeviceBase(__in CfxDeviceBase *DeviceBase)
Definition: fxobject.hpp:797
__inline PVOID FxAllocateFromNPagedLookasideList(_In_ PNPAGED_LOOKASIDE_LIST Lookaside, _In_opt_ size_t ElementSize=0)
Definition: fxglobalskm.h:565
static __inline VOID MxInitializeNPagedLookasideList(_Out_ PNPAGED_LOOKASIDE_LIST Lookaside, _In_opt_ PALLOCATE_FUNCTION Allocate, _In_opt_ PFREE_FUNCTION Free, _In_ ULONG Flags, _In_ SIZE_T Size, _In_ ULONG Tag, _In_ USHORT Depth)
Definition: mxgeneralkm.h:451
FX_POOL FxPoolFrameworks
Definition: fxglobals.h:388
BOOLEAN IsPoolTrackingOn(VOID)
Definition: fxglobals.h:215
PVOID InitPagedAlloc(__out_bcount(this->m_RawBufferSize) PVOID Alloc)
FxContextHeader * pHeader
Definition: handleapi.cpp:604
#define __out
Definition: dbghelp.h:62
virtual _Must_inspect_result_ NTSTATUS Initialize(__in size_t BufferSize, __in PWDF_OBJECT_ATTRIBUTES MemoryAttributes)
PVOID pBuffer
VOID MarkPassiveCallbacks(__in FxObjectLockState State=ObjectLock)
Definition: fxobject.hpp:972
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _Strict_type_match_ POOL_TYPE _In_opt_ ULONG _In_ _Out_ WDFMEMORY * Memory
Definition: wdfmemory.h:169
PFX_DRIVER_GLOBALS pFxDriverGlobals
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static __inline VOID MxInitializePagedLookasideList(_Out_ PPAGED_LOOKASIDE_LIST Lookaside, _In_opt_ PALLOCATE_FUNCTION Allocate, _In_opt_ PFREE_FUNCTION Free, _In_ ULONG Flags, _In_ SIZE_T Size, _In_ ULONG Tag, _In_ USHORT Depth)
Definition: mxgeneralkm.h:472
PVOID Alloc(IN DWORD dwFlags, IN SIZE_T dwBytes)
Definition: main.c:63
FX_POOL_HEADER * PFX_POOL_HEADER
Definition: fxpool.h:51
_Must_inspect_result_ NTSTATUS __inline FxPoolAddHeaderSize(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in size_t AllocationSize, __out size_t *NewSize)
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _Strict_type_match_ POOL_TYPE _In_opt_ ULONG PoolTag
Definition: wdfmemory.h:159
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _In_ _Strict_type_match_ POOL_TYPE _In_opt_ PWDF_OBJECT_ATTRIBUTES MemoryAttributes
Definition: wdfmemory.h:407
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define _Must_inspect_result_
Definition: ms_sal.h:558
static __inline VOID MxDeleteNPagedLookasideList(_In_ PNPAGED_LOOKASIDE_LIST LookasideList)
Definition: mxgeneralkm.h:433
WDF_OBJECT_ATTRIBUTES m_MemoryAttributes
__inline PFX_DRIVER_GLOBALS GetDriverGlobals(VOID)
Definition: fxobject.hpp:734
_Must_inspect_result_ __inline PVOID FxAllocateFromPagedLookasideList(__in PPAGED_LOOKASIDE_LIST Lookaside)
Definition: fxglobalskm.h:652
#define NULL
Definition: types.h:112
void * _ReturnAddress(void)
VOID __inline FxPoolInsertPagedAllocateTracker(__in PFX_POOL Pool, __in PFX_POOL_TRACKER Tracker, __in SIZE_T Size, __in ULONG Tag, __in PVOID Caller)
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
FxPagedLookasideListFromPool(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in ULONG PoolTag, __in FxDeviceBase *DeviceBase, __in FxDeviceBase *MemoryDeviceBase)
#define STATUS_SUCCESS
Definition: shellext.h:65
virtual VOID Reclaim(__in FxMemoryBufferFromLookaside *Memory)
static __inline VOID MxDeletePagedLookasideList(_In_ PPAGED_LOOKASIDE_LIST LookasideList)
Definition: mxgeneralkm.h:442
#define __in
Definition: dbghelp.h:35
static SERVICE_STATUS status
Definition: service.c:31
FX_POOL_TRACKER * PFX_POOL_TRACKER
Definition: fxpool.h:107
#define BufferSize
Definition: mmc.h:75
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:251
#define WDF_PTR_ADD_OFFSET_TYPE(_ptr, _offset, _type)
Definition: wdfcore.h:141
#define DO_NOTHING()
Definition: mxgeneral.h:32
Definition: ps.c:97