Committing Storage in a Reserved Region

[Previous] [Next]

After you have reserved a region, you will need to commit physical storage to the region before you can access the memory addresses contained within it. The system allocates physical storage committed to a region from the system's paging file. Physical storage is always committed on page boundaries and in page-size chunks.

To commit physical storage, you must call VirtualAlloc again. This time, however, you'll pass the MEM_COMMIT identifier instead of the MEM_RESERVE identifier for the fdwAllocationType parameter. You usually pass the same page protection attribute (most often PAGE_READWRITE) that was used when VirtualAlloc was called to reserve the region, although you can specify a different protection attribute.

From within the reserved region, you must tell VirtualAlloc where you want to commit physical storage and how much physical storage to commit. You do this by specifying the desired memory address in the pvAddress parameter and the amount of physical storage, in bytes, in the dwSize parameter. Note that you don't have to commit physical storage to the entire region at once.

Let's look at an example of how to commit storage. Say your application is running on an x86 CPU and the application reserves a 512-KB region starting at address 5,242,880. You would like your application to commit storage to the 6-KB portion of the reserved region starting 2 KB into the reserved region's address space. To do this, call VirtualAlloc using the MEM_COMMIT flag as follows:

 VirtualAlloc((PVOID) (5242880 + (2 * 1024)), 6 * 1024, MEM_COMMIT, PAGE_READWRITE); 

In this case, the system must commit 8 KB of physical storage, spanning the address range 5,242,880 through 5,251,071 (5,242,880 + 8 KB - 1 byte). Both of these committed pages have a protection attribute of PAGE_READWRITE. Protection attributes are assigned on a whole-page basis only. It is not possible to use different protection attributes for portions of the same page of storage. However, it is possible for one page in a region to have one protection attribute (such as PAGE_READWRITE) and for another page in the same region to have a different protection attribute (such as PAGE_READONLY).



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