none
How to allocate memory for a structure or size less than PAGE_SIZE in kmdf? RRS feed

  • Question

  • does WdfMemoryCreate works here ? if it is then what pool type should I give to allocate less than PAGE_SIZE or just simply for size of structure? 
    Monday, December 23, 2013 1:36 PM

Answers

  • I would use ExAllocatePoolWithTag and have the list head in a WDF object, such as a device context.  Have a cleanup routine for the WDF object that walks the list cleaning up the items.   You might also look at ExAllocateFromNPagedLookasideList/ExAllocateFromPagedLookasideList depending on what you are doing.  This is optimized for allocating/freeing fixed size items.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Monday, December 23, 2013 3:05 PM

All replies

  • It really depends on what you are going to do with the memory.   WdfMemoryCreate allocates a buffer, with all the object properties of WDF objects, which means among other things that if the parent object of the memory is deleted it is.  This can be good in many instances.  

    The underlying call is ExAllocatePoolWithTag which you can think of as the kernels equivalent of malloc.  It does not have the object characteristics of the WdfMemoryCreate, including not having to have a handle to the object and a pointer to the memory.  But it does mean you have to clean this up yourself.

    On pool type, for either of the above it depends on how and when your memory is going to be accessed.  If your memory is going to be accessed at DISPATCH_LEVEL or higher, then it must be non-paged.  DISPATCH_LEVEL or higher comes about because the memory is accessed from an Interrupt Service Routine, or a DPC routine (WdfInterruptQueueDpcForIsr) or for functions such as KeAquireSpinLock that raise IRQL to DISPATCH.   For all other cases paged pool is fine, and should be used.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Monday, December 23, 2013 2:07 PM
  • In simple way, I want to create a link list in kmdf driver. I came across this api so I thought this might be the way to allocate memory for structure also. But now I think using  WdfMemoryCreate would be the wrong way to do this.?? and I should use ExAllocatePoolWithTag ?
    Monday, December 23, 2013 2:30 PM
  • I would use ExAllocatePoolWithTag and have the list head in a WDF object, such as a device context.  Have a cleanup routine for the WDF object that walks the list cleaning up the items.   You might also look at ExAllocateFromNPagedLookasideList/ExAllocateFromPagedLookasideList depending on what you are doing.  This is optimized for allocating/freeing fixed size items.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Monday, December 23, 2013 3:05 PM