LPVOID VirtualAlloc(lpAddress, dwSize, dwAllocationType, dwProtect) | |||
LPVOID lpAddress; | |||
DWORD dwSize; | |||
DWORD dwAllocationType; | |||
DWORD dwProtect; |
A region of pages within the virtual address space of a calling process can be reserved and/or committed with the VirtualAlloc function:
lpAddress
If the initial value of this argument is not NULL and the memory is being reserved, then the region is allocated starting at the specified virtual address rounded down to the next 64K byte boundary. If the memory is already reserved and is being committed, this value is rounded down to a host-page-size boundary. If the initial value of this argument is NULL, then the operating system determines where to allocate the region.
dwSize
A number that specifies the size in bytes of the region. It will be rounded up to the next host-page-size boundary.
dwAllocationType
A set of flags that describes the type of allocation that is to be performed for the specified region of pages. One of MEM_COMMIT or MEM_RESERVED is required.
Value | Meaning |
MEM_COMMIT | ||
The specified region of pages is to be committed. | ||
MEM_RESERVE | ||
The specified region of pages is to be reserved. |
dwProtect
The protection desired for the committed region of pages.
Value | Meaning |
PAGE_NOACCESS | ||
No access to the committed region of pages is allowed. An attempt to read, write, or execute the committed region results in an access violation (a GP fault). | ||
PAGE_READONLY | ||
Read access to the committed region of pages is allowed. An attempt to write or execute the committed region results in an access violation. | ||
PAGE_READWRITE | ||
Read and write access to the committed region of pages is allowed. |
The return value is the base address of the allocated region of pages if the function is successful. Otherwise it is NULL, in which case extended error information is available from the GetLastError function.
This function can be used to commit a region of previously reserved pages (i.e., from a previous call to this function), to reserve a region of pages, or to reserve and commit a region of pages. This function also can be used to create a sparse population of committed pages.
If the value of the lpAddress parameter is NULL, then the operating system allocates a region of pages large enough to fulfill the specified allocation request from the virtual address space of the calling process. The base address of this region is returned as the value of this function.
Process address map entries are scanned from the base address upward until the entire range of pages can be allocated or a failure occurs. If the entire range cannot be allocated, an appropriate status value is returned and no pages are mapped.
Each page in the process virtual address space can be in one of three states:
1.Free – Not committed or reserved, and inaccessible
2.Committed – Allocated backing storage with access controlled by a protection code
3.Reserved – Reserved, not committed, and inaccessible
For each of these states, each page considered for allocation is handled as follows:
1.Free – A page that is free can be reserved and/or committed.
2.Committed – A page that is already committed cannot be reserved or committed a second time.
3.Reserved – A page that is reserved can be committed but cannot be reserved a second time.
If the desired type of allocation is allowed for the entire dwSize, then page attributes are established as necessary in the process address map, and the current length of the allocation region is updated. If the desired type of allocation cannot be performed, then an appropriate status value is returned and no changes are made.
Any protection value can be applied to committed pages. Reserved pages are given a protection value of no access.