Reserving a Region and Committing Storage Simultaneously

[Previous] [Next]

At times, you'll want to reserve a region and commit storage to it simultaneously. You can do this by placing a single call to VirtualAlloc as follows:

 PVOID pvMem = VirtualAlloc(NULL, 99 * 1024, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); 

This call is a request to reserve a 99-KB region and commit 99 KB of physical storage to the region. When the system processes this call, it first searches your process's address space to find a contiguous area of unreserved address space large enough to hold 100 KB (on a 4-KB page machine) or 104 KB (on an 8-KB page machine).

The system searches the address space because I specified NULL as the pvAddress parameter. If I had specified a memory address for pvAddress, the system would see whether there was enough unreserved address space at that memory address. If the system could not find enough unreserved address space, VirtualAlloc would return NULL.

If a suitable region can be reserved, the system commits physical storage to the entire region. Both the region and the committed storage will be assigned PAGE_READWRITE protection.

Finally, VirtualAlloc returns the virtual address of the reserved and committed region, which is then saved in the pvMem variable. If the system couldn't find a large enough address space or commit the physical storage, VirtualAlloc returns NULL.

It is certainly possible when reserving a region and committing physical storage this way to pass a specific address as the pvAddress parameter to VirtualAlloc. Or you might need to have the system select a suitable region toward the top of your process's address space by ORing the MEM_TOP_DOWN flag to the fdwAllocationType parameter and passing NULL for the pvAddress parameter.



Programming Applications for Microsoft Windows
Programming Applications for Microsoft Windows (Microsoft Programming Series)
ISBN: 1572319968
EAN: 2147483647
Year: 1999
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net