Click to Rate and Give Feedback
MSDN
MSDN Library
Windows Driver Kit
Reference
 WdfMemoryCreate
Windows Driver Kit: Kernel-Mode Driver Framework
WdfMemoryCreate

The WdfMemoryCreate method creates a framework memory object and allocates a memory buffer of a specified size.

NTSTATUS
  WdfMemoryCreate(
    IN OPTIONAL PWDF_OBJECT_ATTRIBUTES  Attributes,
    IN POOL_TYPE  PoolType,
    IN OPTIONAL ULONG  PoolTag,
    IN size_t  BufferSize,
    OUT WDFMEMORY*  Memory,
    OUT OPTIONAL PVOID*  Buffer
    );

Parameters

Attributes
A pointer to a WDF_OBJECT_ATTRIBUTES structure that contains object attributes for the new memory object. This parameter is optional and can be WDF_NO_OBJECT_ATTRIBUTES.
PoolType
A POOL_TYPE-typed value that specifies the type of memory to be allocated.
PoolTag
A driver-defined pool tag for the allocated memory. Debuggers display this tag. Drivers typically specify a character string of up to four characters, delimited by single quotation marks, in reverse order (for example, 'dcba'). The ASCII value of each character in the tag must be between 0 and 127. Debugging your driver is easier if each pool tag is unique.

If PoolTag is zero, the framework provides a default pool tag that uses the first four characters of your driver's kernel-mode service name. If the service name begins with "WDF" (the name is not case sensitive and does not include the quotation marks), the next four characters are used. If fewer than four characters are available, "FxDr" is used.

For KMDF versions 1.5 and later, your driver can use the DriverPoolTag member of the WDF_DRIVER_CONFIG structure to specify a default pool tag.

BufferSize
The nonzero specified size, in bytes, of the buffer.
Memory
A pointer to a location that receives a handle to the new memory object.
Buffer
A pointer to a location that receives a pointer to the buffer that is associated with the new memory object. This parameter is optional and can be NULL.

Return Value

WdfMemoryCreate returns STATUS_SUCCESS if the operation succeeds. Otherwise, this method might return one of the following values:

STATUS_INVALID_PARAMETER
An invalid parameter was detected.
STATUS_INSUFFICIENT_RESOURCES
There was insufficient memory.

For a list of other return values that the WdfMemoryCreate method might return, see Framework Object Creation Errors.

This method might also return other NTSTATUS values.

Comments

The WdfMemoryCreate method allocates a buffer of the size that the BufferSize parameter specifies and creates a framework memory object that represents the buffer.

To obtain the buffer's address, your driver can supply a non-NULL value for the WdfMemoryCreate function's Buffer parameter, or the driver can call WdfMemoryGetBuffer.

The default parent object for each memory object is the framework driver object that represents the driver that called WdfMemoryCreate. If your driver creates a memory object that it uses with a specific device object, request object, or other framework object, it should set the memory object's parent appropriately. The memory object and its buffer will be deleted when the parent object is deleted. If you do not change the default parent object, the memory object and its buffer will remain until the I/O manager unloads your driver.

A driver can also delete a memory object and its buffer by calling WdfObjectDelete.

If BufferSize is less than PAGE_SIZE, the operating system gives the caller exactly the number of requested bytes of memory. The buffer is not necessarily page-aligned, but it is aligned by the number of bytes that the MEMORY_ALLOCATION_ALIGNMENT constant specifies in Ntdef.h. Buffers of PAGE_SIZE or less do not cross page boundaries.

If BufferSize is PAGE_SIZE or greater, the system allocates a page-aligned buffer. If the PoolType parameter is NonPagedPool, the system allocates the number of pages that are necessary to hold all of the bytes. Any unused bytes on the last-allocated page are essentially wasted.

For more information about framework memory objects, see Using Memory Buffers.

If your driver specifies PagedPool for PoolType, the WdfMemoryCreate method must be called at IRQL <= APC_LEVEL. Otherwise, the method can be called at IRQL <= DISPATCH_LEVEL.

Example

The following code example creates a framework memory object and allocates a buffer whose size is WRITE_BUFFER_SIZE. The memory object's parent is a request object.

NTSTATUS  status;
WDF_OBJECT_ATTRIBUTES  attributes;
WDFMEMORY  writeBufferMemHandle;
PVOID  writeBufferPointer;

WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = requestHandle;
status = WdfMemoryCreate(
                         &attributes,
                         NonPagedPool,
                         0,
                         WRITE_BUFFER_SIZE,
                         &writeBufferMemHandle,
                         &writeBufferPointer
                         );

Requirements

Versions: The WdfMemoryCreate method is available in version 1.0 and later versions of KMDF.

Headers: Declared in WdfMemory.h. Include Wdf.h.

See Also

POOL_TYPE, WDF_OBJECT_ATTRIBUTES, WDF_OBJECT_ATTRIBUTES_INIT, WdfMemoryCreatePreallocated, WdfMemoryCreateFromLookaside, WdfMemoryGetBuffer, WdfObjectDelete

APIScan Requirements

Header: Wdfmemory.h, Wdf.h
Function: WdfMemoryCreate
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker