The VirtualAlloc function reserves or commits a region of pages in the virtual address space of the calling process. Memory allocated by this function is automatically initialized to zero, unless the MEM_RESET flag is set.
LPVOID VirtualAlloc(
LPVOID lpAddress, | // address of region to reserve or commit |
DWORD dwSize, | // size of region |
DWORD flAllocationType, | // type of allocation |
DWORD flProtect | // type of access protection |
); |
Flag | Meaning |
MEM_COMMIT | Allocates physical storage in memory or in the paging file on disk for the specified region of pages. |
An attempt to commit an already committed page will not cause the function to fail. This means that a range of committed or decommitted pages can be committed without having to worry about a failure. | |
The maximum amount of memory that can be committed is based solely on the maximum range of contiguous free or reserved addresses (but not a combination of both), regardless of the amount of physical memory available to the system. When memory is committed, physical pages of memory are allocated and space is reserved in a pagefile. That is, pages of committed memory always exist as either physical pages of memory or as pages that have been paged to the pagefile on disk. It is also possible that, while committing a chunk of memory, part or all of that memory will not reside in physical memory initially. Some pages of memory reside initially in the pagefile until accessed. Once pages of memory are committed, the virtual memory manager treats them like all other pages of memory in the system. | |
MEM_PHYSICAL | Allocate physical memory. This value is solely for use with Address Windowing Extensions (AWE) memory. See also: http://msdn.microsoft.com/library/psdk/winbase/memman_3mr7.htm |
MEM_RESERVE | Reserves a range of the processs virtual address space without allocating any physical storage. The reserved range cannot be used by any other allocation operations (the malloc function, the LocalAlloc function, and so on) until it is released. Reserved pages can be committed in subsequent calls to the VirtualAlloc function. |
MEM_RESET | Windows NT
only: Specifies that memory pages within the range specified by
lpAddress and dwSize will not be written to or read
from the paging file.
When you set the MEM_RESET flag, you are declaring that the contents of that range are no longer important. The range is going to be overwritten, and the application does not want the memory to migrate out to or in from the paging file. Setting this flag does not guarantee that the range operated on with MEM_RESET will contain zeroes. If you want the range to contain zeroes, decommit the memory and then recommit it. When you set the MEM_RESET flag, the VirtualAlloc function ignores the value of fProtect. However, you must still set fProtect to a valid protection value, such as PAGE_NOACCESS. VirtualAlloc returns an error if you set the MEM_RESET flag and the range of memory is mapped to a file. A shared view is only acceptable if it is mapped to a paging file. |
MEM_TOP_DOWN | Windows NT/2000: Allocates memory at the highest possible address. |
MEM_WRITE_WATCH | Windows 98: Causes the system to keep track of the pages that are written
to in the allocated region. If you specify this value, you must also specify
MEM_RESERVE. The write-tracking feature remains enabled for the memory region
until the region is freed.
To retrieve the addresses of the pages that have been written to since the region was allocated or the write-tracking state was reset, call the GetWriteWatch function. To reset the write-tracking state, call GetWriteWatch or ResetWriteWatch. |
Flag | Meaning |
PAGE_READONLY | Enables read access to the committed region of pages. An attempt to write to the committed region results in an access violation. If the system differentiates between read-only access and execute access, an attempt to execute code in the committed region results in an access violation. |
PAGE_READWRITE | Enables both read and write access to the committed region of pages. |
PAGE_EXECUTE | Enables execute access to the committed region of pages. An attempt to read or write to the committed region results in an access violation. |
PAGE_EXECUTE_READ | Enables execute and read access to the committed region of pages. An attempt to write to the committed region results in an access violation. |
PAGE_EXECUTE_READWRITE | Enables execute, read, and write access to the committed region of pages. |
PAGE_GUARD | Pages in the region become guard pages. Any attempt to read from or
write to a guard page causes the operating system to raise a STATUS_GUARD_PAGE
exception and turn off the guard page status. Guard pages thus act as a one-shot
access alarm.
The PAGE_GUARD flag is a page protection modifier. An application uses it with one of the other page protection flags, with one exception: It cannot be used with PAGE_NOACCESS. When an access attempt leads the operating system to turn off guard page status, the underlying page protection takes over. If a guard page exception occurs during a system service, the service typically returns a failure status indicator. |
PAGE_NOACCESS | Disables all access to the committed region of pages. An attempt to read from, write to, or execute in the committed region results in an access violation exception, called a general protection (GP) fault. |
PAGE_NOCACHE | Allows no caching of the committed regions of pages. The hardware attributes for the physical memory should be specified as no cache. This is not recommended for general usage. It is useful for device drivers; for example, mapping a video frame buffer with no caching. This flag is a page protection modifier, only valid when used with one of the page protections other than PAGE_NOACCESS. |
PAGE_WRITECOMBINE | Enables write combining, that is, coalescing writes from cache to main memory, where the hardware supports it. This is primarily for use with frame buffer memory. Normally a frame buffer is not cached. However, with this option, portions of the frame buffer can be cached. This means that writes to the same cache line are coalesced in cache and written out to the frame buffer once upon the first write to another cache line. This can greatly reduce writes across the bus to video memory. If the hardware does not support write combining, the value is ignored. This value is a page protection modifier, and it is valid only when used with one of the page protections other than PAGE_NOACCESS. |
If the function succeeds, the return value is the base address of the allocated region of pages.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
VirtualAlloc can perform the following operations:
You can use VirtualAlloc to reserve a block of pages and then make additional calls to VirtualAlloc to commit individual pages from the reserved block. This enables a process to reserve a range of its virtual address space without consuming physical storage until it is needed.
Each page in the processs virtual address space is in one of three states:
State | Meaning |
Free | The page is not committed or reserved and is not accessible to the process. VirtualAlloc can reserve, or simultaneously reserve and commit, a free page. |
Reserved | The range of addresses cannot be used by other allocation functions, but the page is not accessible and has no physical storage associated with it. VirtualAlloc can commit a reserved page, but it cannot reserve it a second time. The VirtualFree function can release a reserved page, making it a free page. |
Committed | Physical storage is allocated for the page, and access is controlled by a protection code. The system initializes and loads each committed page into physical memory only at the first attempt to read or write to that page. When the process terminates, the system releases the storage for committed pages. VirtualAlloc can commit an already committed page. This means that you can commit a range of pages, regardless of whether they have already been committed, and the function will not fail. VirtualFree can decommit a committed page, releasing the pages storage, or it can simultaneously decommit and release a committed page. |
If the lpAddress parameter is not NULL, the function uses the lpAddress and dwSize parameters to compute the region of pages to be allocated. The current state of the entire range of pages must be compatible with the type of allocation specified by the flAllocationType parameter. Otherwise, the function fails and none of the pages are allocated. This compatibility requirement does not preclude committing an already committed page; see the preceding list.
Windows NT/2000: The PAGE_GUARD protection modifier flag establishes guard pages. Guard pages act as one-shot access alarms. See Guard Pages.
GlobalAlloc, HeapAlloc, VirtualFree, VirtualLock, VirtualProtect, VirtualQuery
Comments:
file: /Techref/os/win/api/win32/func/src/f90_8.htm, 14KB, , updated: 2006/4/16 10:16, local time: 2024/11/28 23:37,
18.117.188.105:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://techref.massmind.org/Techref/os/win/api/win32/func/src/f90_8.htm"> VirtualAlloc</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to techref.massmind.org! |
.