VirtualAlloc of Windows API (not finished)

Source: Internet
Author: User

Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process. Memory allocated by this function is automatically initialized to zero.

LPVOID WINAPI VirtualAlloc (  _in_opt_ lpvoid lpaddress,  _in_     size_t dwsize,  _in_     DWORD  Flallocationtype,  _in_     DWORD  flprotect);

In order to understand VirtualAlloc better, we need to understand the principle of virtual memory technology first.

the memory structure of Windows is the key to understanding how the Windows operating system works, with a clear understanding of the memory structure, such as the sharing of data between processes, the efficient management of memory, and so on. This enables the program to run in a more efficient manner during program design . There are many different ways in which memory can be managed by the Windows operating system, where virtual memory is managed to manage large arrays of objects and structures.

In a Windows system, any process is given its own virtual address space, which covers a fairly large range, and for a 32-bit process, its address space is 2^32=4,294,967,296 Byte, This allows a pointer to use any one of the values from 0x00000000 to the 4GB range of 0xFFFFFFFF. Although each 32-bit process can use 4GB of address space, it does not mean that each process actually has a 4GB physical address space, the address space is just a virtual address space, this virtual address space is only a range of memory addresses. The actual physical memory that the process can get is much smaller than its virtual address space. The virtual address space for a process is private to each process, and the threads that run within the process are restricted to the calling process and cannot access memory space that belongs to other processes. In this way, pointers to the same address can be used in different processes to point to content that belongs to the respective calling process without causing confusion.

When a process is created and assigned an address space, its virtual address space is not allocated and is idle. In this case, the memory in the address space is not available, you must first allocate its respective regions through the VirtualAlloc () function to preserve it.

Its parameter lpaddress contains a memory address that defines the first address of the area to be allocated. This parameter can usually be set to NULL, which is determined by the system to determine the unreserved address space that satisfies the condition by searching the address space. At this point the system can retain an area from anywhere in the address space, and can also indicate that memory is allocated at the highest possible address by setting the mem_top_down flag to the parameter flallocationtype. If you do not want the system to automatically complete the allocation of memory areas for lpaddress and set a memory address (you must ensure that it is always in the user-mode partition of the process, otherwise it will cause the allocation to fail), then the system will first check if there is enough unreserved space on the memory address before allocating it. If there is an idle area that is large enough, the system will retain this zone and return the virtual address of this reserved area, otherwise it will cause the allocation to fail and return null. In particular, it is important to note that when specifying the memory address of a lpaddress, you must ensure that it starts at the boundary of a distribution granularity.

"Retention" and "Commit" of memory:

WIN32 provides a separate, 2 GB user address space for each application (process) in the system. For an application, it would seem that there is 2 GB of available memory (which is actually slightly different from the memory allocation on windows95/98,nt,win2000 Advanced server/enterprise Server), regardless of the actual available physical memory Amount If an application requires more memory than is available, Win32 satisfies this requirement by paging non-critical memory (paging) to a page file from this and/or other process and releasing the physical memory pages .

At any given time, each address in the process can be considered to be free, reserved, or committed . at the beginning of the process, all addresses are free, meaning they are free space and can be committed to memory, or reserved for future use, but they cannot be accessed (read/write). Before any free address can be used, it must first be assigned as reserved or committed .

When an address is persisted in a process, no physical memory page is committed, and, perhaps more importantly, there is no space reserved in the page file to back up the memory. Also, reserving an address range will not guarantee that there will be available physical memory to submit to these addresses in the future. In fact, it simply saves a specified free address address until it needs to be used, and it blocks other requests that are assigned to that segment of the address . without this type of protection, the routine operation (routine   operations), such as loading a DLL or resource, may occupy the specified address and jeopardize its use later .   to use a reserved address, the memory must first be presented to that address . Committing memory to an address is similar to preserving memory. Call VirtualAlloc, and set the dwallocation parameter equal to Mem_commit when called. At this point, the resource is submitted to the address. Each time, the memory can be submitted as a page size. The maximum memory value that can be committed depends only on the maximum range of consecutive free or reserved addresses (but not the combination of the two), regardless of the size of the system's available physical memory.  

When memory is committed, the memory physical page is assigned, and the space is retained in a page file. That is, the committed memory page always exists in the form of a physical memory page or a page file on a disk that has been paged. When committing a large chunk of memory, it is also possible that some or all of its memory does not reside in physical memory during the initial phase. Some memory pages begin to reside in the page file until it is accessed. In the system, once the memory pages have been committed, the virtual Memory manager treats them as if they were all other memory pages.
In the WIN32 virtual memory system, page tables is used to access physical memory pages. Each page table itself is also a memory page, just like a submitted page. Occasionally, when you commit memory, you must also assign additional pages to the page table. Therefore, a request to submit a page of memory might need to assign a page to the page table, assign a page to the requested page, and require two pages of space in the page file to back up each page in the page. Therefore, the time required for VirtualAlloc to complete a memory submission request varies greatly, depending on the state of the system and the size of the requested space.

Memory-Commit Size: The amount of virtual memory that is reserved for use by a process.
For submitted pages, the system dispatches them according to the total memory usage. When physical memory is tight, the system chooses some pages, swapping them out into memory files, and then swapping them back for the next use. Typically, applications do not need to intervene in the system's paging mechanism. In some special cases, the application can also use the VirtualLock function to lock the submitted page so that they always remain in physical memory, and then call the Virtualunlock function to end the lock. ”

Key words:

Memory Reserved Memory Commit

Reference:

Https://msdn.microsoft.com/en-us/library/windows/desktop/aa366887%28v=vs.85%29.aspx

Http://www.yesky.com/67/1753067.shtml

http://super-man-woman.blog.163.com/blog/static/3789803820098532317144/

VirtualAlloc of Windows API (not finished)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.