ReactOS  0.4.15-dev-3173-g40ee59d
fxmemorybufferapi.cpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxMemoryBufferApi.cpp
8 
9 Abstract:
10 
11  This modules implements the C API's for the FxMemoryBuffer.
12 
13 Author:
14 
15 
16 Environment:
17 
18  kernel mode only
19 
20 Revision History:
21 
22 --*/
23 
24 #include "coreprivshared.hpp"
25 #include "fxmemorybuffer.hpp"
26 
27 extern "C" {
28 // #include "FxMemoryBufferAPI.tmh"
29 }
30 
31 extern "C" {
32 
36 WDFAPI
38 STDCALL
39 WDFEXPORT(WdfMemoryCreate)(
40  __in
42  __in_opt
44  __in
45  __drv_strictTypeMatch(__drv_typeExpr)
47  __in_opt
49  __in
51  size_t BufferSize,
52  __out
53  WDFMEMORY* Memory,
54  __out_opt
55  PVOID* Buffer
56  )
57 /*++
58 
59 Routine Description:
60  Creates a WDFMEMORY handle based on the caller's specifications
61 
62 Arguments:
63  Attributes - Attributes associated with this object
64 
65  PoolType - The type of pool created
66 
67  PoolTag - Tag to use when allocating the memory. If 0, then the frameworks
68  tag is used
69 
70  BufferSize - The size of the buffer represented by the returned handle
71 
72  Memory - The returned handle to the caller
73 
74  Buffer - (opt) Pointer to the associated memory buffer.
75 
76 Return Value:
77  STATUS_INVALID_PARAMETER - any required parameters are not present
78 
79  STATUS_INSUFFICIENT_RESOURCES - could not allocated the object that backs
80  the handle
81 
82  STATUS_SUCCESS - success
83 
84  --*/
85 {
86  DDI_ENTRY();
87 
90  WDFMEMORY hMemory;
92 
94 
95  //
96  // Get the parent's globals if it is present
97  //
99  Attributes))) {
100  FxObject* pParent;
101 
103  Attributes->ParentObject,
105  (PVOID*)&pParent,
107  }
108 
110 
113  if (!NT_SUCCESS(status)) {
114  return status;
115  }
116  }
117 
118  if (BufferSize == 0) {
121  "BufferSize == 0 not allowed, %!STATUS!", status);
122  return status;
123  }
124 
125  *Memory = NULL;
126 
129  return status;
130  }
131 
132  if (PoolTag == 0) {
134  }
135 
137 
140  Attributes,
141  PoolType,
142  PoolTag,
143  BufferSize,
144  &pBuffer);
145 
146  if (!NT_SUCCESS(status)) {
147  return status;
148  }
149 
151 
153  *Memory = hMemory;
154  if (Buffer != NULL) {
155  *Buffer = pBuffer->GetBuffer();
156  }
157  }
158  else {
160  }
161 
162  return status;
163 }
164 
166 PVOID
167 WDFAPI
168 STDCALL
169 WDFEXPORT(WdfMemoryGetBuffer)(
170  __in
172  __in
173  WDFMEMORY Memory,
174  __out_opt
175  size_t* BufferSize
176  )
177 /*++
178 
179 Routine Description:
180  Retrieves the raw pointers associated with WDFMEMORY handle
181 
182 Arguments:
183  Memory - handle to the WDFMEMORY
184 
185  BufferSize - the size / length of the buffer
186 
187 Return Value:
188  raw buffer
189 
190  --*/
191 {
192  DDI_ENTRY();
193 
195 
197  Memory,
199  (PVOID*)&pMemory);
200 
201  if (BufferSize != NULL) {
203  }
204 
205  return pMemory->GetBuffer();
206 }
207 
210 NTSTATUS
211 WDFAPI
212 STDCALL
213 WDFEXPORT(WdfMemoryCopyToBuffer)(
214  __in
216  __in
217  WDFMEMORY SourceMemory,
218  __in
219  size_t SourceOffset,
220  __out_bcount( NumBytesToCopyTo)
221  PVOID Buffer,
222  __in
223  __drv_when(NumBytesToCopyTo == 0, __drv_reportError(NumBytesToCopyTo cannot be zero))
224  size_t NumBytesToCopyTo
225  )
226 /*++
227 
228 Routine Description:
229  Copies memory from a WDFMEMORY handle to a raw pointer
230 
231 Arguments:
232  SourceMemory - Memory handle whose contents we are copying from.
233 
234  SourceOffset - Offset into SourceMemory from which the copy starts.
235 
236  Buffer - Memory whose contents we are copying into
237 
238  NumBytesToCopyTo - Number of bytes to copy into buffer.
239 
240 Return Value:
241 
242  STATUS_BUFFER_TOO_SMALL - SourceMemory is smaller than the requested number
243  of bytes to be copied.
244 
245  NTSTATUS
246 
247  --*/
248 {
249  DDI_ENTRY();
250 
252  IFxMemory* pSource;
253  WDFMEMORY_OFFSET srcOffsets;
254  WDFMEMORY_OFFSET dstOffsets;
256 
258  SourceMemory,
260  (PVOID*) &pSource);
261 
262  pFxDriverGlobals = pSource->GetDriverGlobals();
263 
265 
266  if (NumBytesToCopyTo == 0) {
269  "Zero bytes to copy not allowed, %!STATUS!", status);
270  return status;
271  }
272 
273  RtlZeroMemory(&srcOffsets, sizeof(srcOffsets));
274  srcOffsets.BufferLength = NumBytesToCopyTo;
275  srcOffsets.BufferOffset = SourceOffset;
276 
277  RtlZeroMemory(&dstOffsets, sizeof(dstOffsets));
278  dstOffsets.BufferLength = NumBytesToCopyTo;
279  dstOffsets.BufferOffset = 0;
280 
281  return pSource->CopyToPtr(&srcOffsets,
282  Buffer,
283  NumBytesToCopyTo,
284  &dstOffsets);
285 }
286 
289 NTSTATUS
290 WDFAPI
291 STDCALL
292 WDFEXPORT(WdfMemoryCopyFromBuffer)(
293  __in
295  __in
296  WDFMEMORY DestinationMemory,
297  __in
298  size_t DestinationOffset,
299  __in
300  PVOID Buffer,
301  __in
302  __drv_when(NumBytesToCopyFrom == 0, __drv_reportError(NumBytesToCopyFrom cannot be zero))
303  size_t NumBytesToCopyFrom
304  )
305 /*++
306 
307 Routine Description:
308  Copies memory from a raw pointer into a WDFMEMORY handle
309 
310 Arguments:
311  DestinationMemory - Memory handle whose contents we are copying into
312 
313  DestinationOffset - Offset into DestinationMemory from which the copy
314  starts.
315 
316  Buffer - Buffer whose context we are copying from
317 
318  NumBytesToCopyFrom - Number of bytes to copy from
319 
320 Return Value:
321 
322  STATUS_INVALID_BUFFER_SIZE - DestinationMemory is not large enough to contain
323  the number of bytes requested to be copied
324 
325  NTSATUS
326 
327  --*/
328 
329 {
330  DDI_ENTRY();
331 
333  WDFMEMORY_OFFSET srcOffsets;
334  WDFMEMORY_OFFSET dstOffsets;
335  IFxMemory* pDest;
337 
341  (PVOID*) &pDest);
342 
343  pFxDriverGlobals = pDest->GetDriverGlobals();
344 
346 
347  if (NumBytesToCopyFrom == 0) {
350  "Zero bytes to copy not allowed, %!STATUS!", status);
351  return status;
352  }
353 
354  RtlZeroMemory(&srcOffsets, sizeof(srcOffsets));
355  srcOffsets.BufferLength = NumBytesToCopyFrom;
356  srcOffsets.BufferOffset = 0;
357 
358  RtlZeroMemory(&dstOffsets, sizeof(dstOffsets));
359  dstOffsets.BufferLength = NumBytesToCopyFrom;
360  dstOffsets.BufferOffset = DestinationOffset;
361 
362  return pDest->CopyFromPtr(&dstOffsets,
363  Buffer,
364  NumBytesToCopyFrom,
365  &srcOffsets);
366 }
367 
368 } // extern "C"
BOOLEAN FxIsPagedPoolType(__in POOL_TYPE Type)
Definition: wdfpool.cpp:43
#define DDI_ENTRY()
Definition: fxglobalskm.h:56
#define __drv_reportError(why)
Definition: driverspecs.h:319
_Must_inspect_result_ __in_opt PWDF_OBJECT_ATTRIBUTES Attributes
_Must_inspect_result_ NTSTATUS Commit(__in_opt PWDF_OBJECT_ATTRIBUTES Attributes, __out_opt WDFOBJECT *ObjectHandle, __in_opt FxObject *Parent=NULL, __in BOOLEAN AssignDriverAsDefaultParent=TRUE)
Definition: fxobject.cpp:904
FxMemoryObject * pBuffer
IFxMemory * pMemory
#define __in_opt
Definition: dbghelp.h:38
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ __drv_when(PoolType==1||PoolType==257, __drv_maxIRQL(APC_LEVEL)) __drv_when(PoolType
FxPointerNotNull(pFxDriverGlobals, Memory)
LONG NTSTATUS
Definition: precomp.h:26
WDFMEMORY hMemory
_Must_inspect_result_ __in WDFMEMORY SourceMemory
_Must_inspect_result_ _In_ WDFMEMORY SourceMemory
Definition: wdfmemory.h:318
_Must_inspect_result_ __drv_maxIRQL(DISPATCH_LEVEL)) WDFAPI NTSTATUS STDCALL WDFEXPORT(WdfMemoryCreate)(__in PWDF_DRIVER_GLOBALS DriverGlobals
NTSTATUS status
DriverGlobals
__in WDFMEMORY __out_opt size_t * BufferSize
#define __out_opt
Definition: dbghelp.h:65
#define WDFEXPORT(a)
Definition: fxmacros.hpp:157
size_t BufferOffset
Definition: wdfmemory.h:65
#define WDFAPI
Definition: wdftypes.h:53
_Must_inspect_result_ NTSTATUS __inline FxValidateObjectAttributesForParentHandle(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in PWDF_OBJECT_ATTRIBUTES Attributes, __in ULONG Flags=FX_VALIDATE_OPTION_NONE_SPECIFIED)
#define __out
Definition: dbghelp.h:62
int zero
Definition: sehframes.cpp:29
Definition: bufpool.h:45
_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
__inline NTSTATUS FxVerifierCheckIrqlLevel(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in KIRQL Irql)
Definition: fxverifier.h:158
#define TRACINGDEVICE
Definition: dbgtrace.h:58
FxObjectHandleGetPtr(GetFxDriverGlobals(DriverGlobals), Memory, IFX_TYPE_MEMORY,(PVOID *)&pMemory)
#define STDCALL
Definition: wdf.h:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
INT POOL_TYPE
Definition: typedefs.h:78
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _Strict_type_match_ POOL_TYPE _In_opt_ ULONG PoolTag
Definition: wdfmemory.h:159
PFX_DRIVER_GLOBALS pFxDriverGlobals
size_t BufferLength
Definition: wdfmemory.h:72
_Must_inspect_result_ NTSTATUS FxValidateObjectAttributes(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in PWDF_OBJECT_ATTRIBUTES Attributes, __in ULONG Flags=FX_VALIDATE_OPTION_NONE_SPECIFIED)
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define _Must_inspect_result_
Definition: ms_sal.h:558
#define TRACE_LEVEL_ERROR
Definition: storswtr.h:27
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
_Must_inspect_result_ __in WDFMEMORY __in size_t SourceOffset
FxVerifierCheckNxPoolType(pFxDriverGlobals, PoolType, PoolTag)
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGPNP, "Enter, WDFDEVICE %p", Device)
_Must_inspect_result_ __in_opt PWDF_OBJECT_ATTRIBUTES __in __in_opt ULONG __in __out WDFMEMORY * Memory
_Must_inspect_result_ __in WDFMEMORY __in size_t DestinationOffset
#define NULL
Definition: types.h:112
_Must_inspect_result_ __in_opt PWDF_OBJECT_ATTRIBUTES __in __drv_strictTypeMatch(__drv_typeExpr) POOL_TYPE PoolType
_Must_inspect_result_ __in WDFMEMORY DestinationMemory
_Must_inspect_result_ __in WDFMEMORY __in size_t __out_bcount(NumBytesToCopyTo) PVOID Buffer
VOID DeleteFromFailedCreate(VOID)
Definition: fxobject.cpp:391
_Must_inspect_result_ _In_ WDFMEMORY DestinationMemory
Definition: wdfmemory.h:359
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ _Strict_type_match_ POOL_TYPE PoolType
Definition: wdfdevice.h:3810
unsigned int ULONG
Definition: retypes.h:1
FxObject * pParent
Definition: fxdpcapi.cpp:86
FxObjectHandleGetPtrAndGlobals(GetFxDriverGlobals(DriverGlobals), Device, FX_TYPE_DEVICE,(PVOID *)&pDevice, &pFxDriverGlobals)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
__inline PFX_DRIVER_GLOBALS GetFxDriverGlobals(__in PWDF_DRIVER_GLOBALS DriverGlobals)
Definition: fxglobals.h:597
_Must_inspect_result_ __in_opt PWDF_OBJECT_ATTRIBUTES __in __in_opt ULONG PoolTag
#define __in
Definition: dbghelp.h:35
_Must_inspect_result_ __in_opt PWDF_OBJECT_ATTRIBUTES __in __in_opt ULONG __in __out WDFMEMORY __out_opt PVOID * Buffer
#define APC_LEVEL
Definition: env_spec_w32.h:695
virtual size_t GetBufferSize(VOID)=0
virtual PVOID GetBuffer(VOID)=0
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:251
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
static _Must_inspect_result_ NTSTATUS _Create(__in PFX_DRIVER_GLOBALS DriverGlobals, __in_opt PWDF_OBJECT_ATTRIBUTES Attributes, __in POOL_TYPE PoolType, __in ULONG PoolTag, __in size_t BufferSize, __out FxMemoryObject **Object)
Definition: ps.c:97