ReactOS  0.4.15-dev-2703-g05fb0f1
fxmemorybufferfromlookaside.cpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxMemoryBufferFromLookaside.cpp
8 
9 Abstract:
10 
11  This module implements a frameworks managed FxMemoryBufferFromLookaside
12 
13 Author:
14 
15 
16 Environment:
17 
18  kernel mode only
19 
20 Revision History:
21 
22 --*/
23 
24 #include "coreprivshared.hpp"
27 
29  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
31  __in size_t BufferSize
32  ) :
33  FxMemoryObject(FxDriverGlobals,
35  BufferSize),
36  m_pLookaside(Lookaside)
37 /*++
38 
39 Routine 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 
47 Arguments:
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 
53 Return 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 
71 Routine 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 
79 Arguments:
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 
87 Return Value:
88  None
89 
90  --*/
91 {
92  Init();
93 }
94 
95 VOID
97  VOID
98  )
99 {
100  m_pLookaside->ADDREF(this);
101 }
102 
103 
104 
106 /*++
107 
108 Routine Description:
109  Destructor for this object. This function does nothing, it lets
110  SelfDestruct do all the work.
111 
112 Arguments:
113 
114 Return Value:
115 
116  --*/
117 {
118 }
119 
121 PVOID
123  __in size_t Size,
124  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
125  __inout PVOID ValidMemory,
126  __in size_t BufferSize,
128  )
129 /*++
130 
131 Routine 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 
135 Arguments:
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 
147 Return 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,
176  Attributes,
178  );
179 }
180 
181 PVOID
183 {
184  //
185  // The buffer follows the struct itself
186  //
187  return ((PUCHAR) this) + COMPUTE_RAW_OBJECT_SIZE(sizeof(*this));
188 }
189 
190 VOID
192 /*++
193 
194 Routine 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 
200 Arguments:
201  None
202 
203 Return 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,
237  BufferSize,
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,
252  BufferSize,
253  ObjectSize),
254  m_Pool(Buffer)
255 {
256 }
257 
258 VOID
260  VOID
261  )
262 {
263  //
264  // Free the 2ndary allocation
265  //
266  ((FxLookasideListFromPool*) m_pLookaside)->ReclaimPool(m_Pool);
267 
268  //
269  // Free the object itself
270  //
272 }
273 
275 PVOID
277  __in size_t Size,
278  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
279  __inout PVOID ValidMemory,
281  )
282 {
283  size_t objectSize;
284 
286 
288 
289  objectSize = COMPUTE_OBJECT_SIZE(sizeof(FxMemoryBufferFromPoolLookaside), 0);
290 
292  FxDriverGlobals,
293  ValidMemory,
294  (USHORT) objectSize,
295  Attributes,
297  );
298 }
#define __in_bcount(x)
Definition: dbghelp.h:41
#define _Must_inspect_result_
Definition: no_sal2.h:62
_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:407
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList
#define COMPUTE_RAW_OBJECT_SIZE(_rawObjectSize)
Definition: fxhandle.h:100
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
unsigned char * PUCHAR
Definition: retypes.h:3
operator
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
virtual VOID Reclaim(__in FxMemoryBufferFromLookaside *Memory)=0
FxLookasideList * pLookaside
Definition: bufpool.h:45
#define COMPUTE_OBJECT_SIZE(_rawObjectSize, _extraSize)
Definition: fxhandle.h:107
#define ASSERT(a)
Definition: mode.c:44
FxMemoryBufferFromPoolLookaside(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __inout FxLookasideList *Lookaside, __in size_t BufferSize, __in_bcount(BufferSize) PVOID Buffer)
#define __inout
Definition: dbghelp.h:50
unsigned short USHORT
Definition: pedump.c:61
unsigned int ULONG
Definition: retypes.h:1
FxMemoryBufferFromLookaside(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __inout FxLookasideList *Lookaside, __in size_t BufferSize)
#define __in
Definition: dbghelp.h:35
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 BufferSize
Definition: mmc.h:75
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:251
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes