ReactOS 0.4.15-dev-8131-g4988de4
fxmemorybufferfromlookaside.cpp
Go to the documentation of this file.
1/*++
2
3Copyright (c) Microsoft Corporation
4
5Module Name:
6
7 FxMemoryBufferFromLookaside.cpp
8
9Abstract:
10
11 This module implements a frameworks managed FxMemoryBufferFromLookaside
12
13Author:
14
15
16Environment:
17
18 kernel mode only
19
20Revision History:
21
22--*/
23
24#include "coreprivshared.hpp"
27
29 __in PFX_DRIVER_GLOBALS FxDriverGlobals,
31 __in size_t BufferSize
32 ) :
33 FxMemoryObject(FxDriverGlobals,
36 m_pLookaside(Lookaside)
37/*++
38
39Routine Description:
40 Constructor for this object. Remembers which lookaside list this object
41 was allocated from
42
43 We round up the object size (via COMPUTE_OBJECT_SIZE) because the object,
44 the context and the memory it returns via GetBuffer() are all in one allocation
45 and the returned memory needs to match the alignment that raw pool follows.
46
47Arguments:
48 Lookaside - The lookaside list which this object will return itself to when
49 its last reference is removed
50
51 BufferSize - The buffer size associated with this object
52
53Return Value:
54 None
55
56 --*/
57{
58 Init();
59}
60
62 __in PFX_DRIVER_GLOBALS FxDriverGlobals,
64 __in size_t BufferSize,
65 __in USHORT ObjectSize
66 ) :
67 FxMemoryObject(FxDriverGlobals, ObjectSize, BufferSize),
68 m_pLookaside(Lookaside)
69/*++
70
71Routine Description:
72 Constructor for this object. Remembers which lookaside list this object
73 was allocated from
74
75 There is no round up of the ObjectSize because the derived class may not
76 embed the buffer in the same allocation as the object. If it were to do so,
77 the derived object should do the round up on its own.
78
79Arguments:
80 Lookaside - The lookaside list which this object will return itself to when
81 its last reference is removed
82
83 BufferSize - The buffer size associated with this object
84
85 ObjectSize - The size of this object.
86
87Return Value:
88 None
89
90 --*/
91{
92 Init();
93}
94
95VOID
97 VOID
98 )
99{
100 m_pLookaside->ADDREF(this);
101}
102
103
104
106/*++
107
108Routine Description:
109 Destructor for this object. This function does nothing, it lets
110 SelfDestruct do all the work.
111
112Arguments:
113
114Return Value:
115
116 --*/
117{
118}
119
121PVOID
122FxMemoryBufferFromLookaside::operator new(
123 __in size_t Size,
124 __in PFX_DRIVER_GLOBALS FxDriverGlobals,
125 __inout PVOID ValidMemory,
126 __in size_t BufferSize,
128 )
129/*++
130
131Routine Description:
132 Displacement new operator overload. Since the lookaside list actually
133 allocates the memory block, we just return the block given to us.
134
135Arguments:
136 Size - Compiler supplied parameter indicating the
137 sizeof(FxMemoryBufferFromLookaside)
138
139 FxDriverGlobals - Driver's globals.
140
141 ValidMemory - Previously allocated block
142
143 BufferSize - The buffer size associated with this object
144
145 Attributes - Description of context.
146
147Return Value:
148 ValidMemory pointer value
149
150 --*/
151{
152 size_t objectSize;
153
155
157
158 //
159 // We round up the object size (via COMPUTE_OBJECT_SIZE) because the object,
160 // and the buffer are all in one allocation and the returned memory needs to
161 // match the alignment that raw pool follows.
162 //
163 // Note that FxMemoryBufferFromLookaside is used for buffer less than a page
164 // size so downcasting to USHORT (via COMPUTE_OBJECT_SIZE) is safe because
165 // size of object plus buffer (<page size) would be smaller than 64K that
166 // could fit in USHORT. See comments in FxMemoryObject.hpp for info on
167 // various fx memory objects.
168 //
170 (ULONG) BufferSize);
171
173 FxDriverGlobals,
174 ValidMemory,
175 (USHORT) objectSize,
178 );
179}
180
181PVOID
183{
184 //
185 // The buffer follows the struct itself
186 //
187 return ((PUCHAR) this) + COMPUTE_RAW_OBJECT_SIZE(sizeof(*this));
188}
189
190VOID
192/*++
193
194Routine Description:
195 Self destruction routine called when the last reference has been removed
196 on this object. This functions behaves as if "delete this" has been called
197 without calling operator delete. It manually calls the destructor and then
198 returns the memory to the lookaside.
199
200Arguments:
201 None
202
203Return Value:
204 None
205
206 --*/
207{
209
210 //
211 // Call the destructor first.
212 //
213 // Since we were allocated from a lookaside list, return ourself to this
214 // list and then free the reference we have on it. We can't call this from
215 // within the destructor b/c then all parent objects would be destructing on
216 // freed pool.
217 //
219
220 //
221 // After FxLookaside::Reclaim, this no longer points to valid memory so we
222 // must stash off m_pLookaside first so we can use it after the reclaim.
223 //
224 pLookaside = m_pLookaside;
225 pLookaside->Reclaim(this);
226 pLookaside->RELEASE(this);
227}
228
230 __in PFX_DRIVER_GLOBALS FxDriverGlobals,
232 __in size_t BufferSize,
234 ) :
235 FxMemoryBufferFromLookaside(FxDriverGlobals,
236 Lookaside,
238 sizeof(*this)),
239 m_Pool(Buffer)
240{
241}
242
244 __in PFX_DRIVER_GLOBALS FxDriverGlobals,
246 __in size_t BufferSize,
248 __in USHORT ObjectSize
249 ) :
250 FxMemoryBufferFromLookaside(FxDriverGlobals,
251 Lookaside,
253 ObjectSize),
254 m_Pool(Buffer)
255{
256}
257
258VOID
260 VOID
261 )
262{
263 //
264 // Free the 2ndary allocation
265 //
267
268 //
269 // Free the object itself
270 //
272}
273
275PVOID
276FxMemoryBufferFromPoolLookaside::operator new(
277 __in size_t Size,
278 __in PFX_DRIVER_GLOBALS FxDriverGlobals,
279 __inout PVOID ValidMemory,
281 )
282{
283 size_t objectSize;
284
286
288
290
292 FxDriverGlobals,
293 ValidMemory,
294 (USHORT) objectSize,
297 );
298}
Definition: bufpool.h:45
virtual VOID Reclaim(__in FxMemoryBufferFromLookaside *Memory)=0
FxMemoryBufferFromLookaside(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __inout FxLookasideList *Lookaside, __in size_t BufferSize)
FxMemoryBufferFromPoolLookaside(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __inout FxLookasideList *Lookaside, __in size_t BufferSize, __in_bcount(BufferSize) PVOID Buffer)
#define __in
Definition: dbghelp.h:35
#define __in_bcount(x)
Definition: dbghelp.h:41
#define __inout
Definition: dbghelp.h:50
#define BufferSize
Definition: mmc.h:75
PVOID FxObjectAndHandleHeaderInit(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in PVOID AllocationStart, __in USHORT ObjectSize, __in_opt PWDF_OBJECT_ATTRIBUTES Attributes, __in FxObjectType ObjectType)
Definition: handleapi.cpp:292
#define COMPUTE_RAW_OBJECT_SIZE(_rawObjectSize)
Definition: fxhandle.h:100
#define COMPUTE_OBJECT_SIZE(_rawObjectSize, _extraSize)
Definition: fxhandle.h:107
FxLookasideList * pLookaside
@ FxObjectTypeExternal
Definition: fxobject.hpp:120
#define ASSERT(a)
Definition: mode.c:44
#define _Must_inspect_result_
Definition: ms_sal.h:558
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
unsigned short USHORT
Definition: pedump.c:61
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _In_ _Strict_type_match_ POOL_TYPE _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_opt_ ULONG _Out_ WDFLOOKASIDE * Lookaside
Definition: wdfmemory.h:414
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList