15th. Using virtual memory in your application (1)

Source: Internet
Author: User

15.1 Reservation (reserve) address empty area

(1) virutalalloc (pvaddress,dwsize,fdwallocationtype,fdwprotect)

①pvoid pvaddress parameter : Memory address, which piece of address space to subscribe to.

A. This parameter also indicates that the function is the reason for the lower function, because we can specify at which address to allocate memory! There is also a limit, however, that memory can only be allocated in user-mode partitions, otherwise it will fail.

B. when booking a region , the system always allocates the area by the allocation granularity of the CPU. If the specified pvaddress is not an integer multiple of 64K, the system will take pvaddress down to an integer multiple of 64K as the starting address of the reservation area (also known as the base site of the region ). such as pvaddress=300x65536 + 8192, will be rounded to 300x65536. (However, it should be noted that at commit time , the start address is always (the same size as the area) and is always an integer multiple of the page size )

C. When this parameter is NULL, the system will automatically find a free area to book. However, the system does not guarantee that it will be allocated from the bottom of the address space, or must be allocated from the top of the address space.

D. Allocate space in high addresses whenever possible when fdwallocatiotype specifies Mem_top_down

②size_t dwsize parameter--used to specify the size of the booking area, in bytes

A. The area size is booked as an integer multiple of the CPU page size (typically 4KB).

B. If the specified dwsize is not an integer multiple of 4KB, it will be rounded up . If the booking size is 62KB, the resulting region size is 64KB.

③dword fdwallocationtype Parameters--to book or submit, etc.

Type

Note

Mem_reserve

A. The reserved zone does not allocate physical memory (physical memory refers to physical, paging, or file images), but only adds a data structure that describes the state of the process virtual address space used to record the area that has been booked.

B. because there is no real allocation of physical memory , This area cannot be accessed directly, otherwise it will cause "memory access violation". However, the "booking" operation is relatively fast .

Mem_commit

A. Submit the area and redeploy the reserved physical storage type to the reservation area. Note that the commit does not immediately allocate space from the physical memory, but instead submits it to the paging file!

B. Until the first time the area is accessed, the system throws a "page fault" and handles the error before actually mapping to physical memory. This strategy is called "demand-paging" strategy, which saves time and saves memory waste.

Mem_physical

A. Used only in address space extensions (AWE), this flag must and can only be used with Mem_reserve.

B. The page must have read-write properties (see the "AWE" section later in the use of the logo)

Mem_top_down

Pass NULL to Pvaddress, while Mem_top_down and Mem_reserve bitwise OR pass to Fdwallocationtype allows the system to subscribe to the region from the highest possible memory address. This prevents a reservation from the middle of the process address space, thus avoiding memory fragmentation

④dword fdwprotect parameter-- Specify the protection property for the zone

A. When booking a region, you can use page_noaccess, Page_readwrite, Page_readonly, page_execute_*, and other categories.

B. However, you cannot use Page_writecopy, Page_execute_writecopy, Page_guard, Page_nocache and Page_writecombine when booking , Because these flags are associated with physical memory.

15.2 Allocating (Commit) physical memory to a region

(1) when calling VirtualAlloc, the MEM_COMMIT flag is passed in, and when the page protection property is assigned to the physical memory, it can be consistent with the reservation or it can be completely different! But the final protective page is subject to the protection properties of the physical page (not the protection properties of the zone)

(2) In the area of the reservation, it is not necessary to allocate physical memory to the whole area at once, you can use Pvaddress and dwsize to specify the part to be submitted. (That is, which memory address, size, and x86 in the 4KB page size as the base unit)

(3) because the system is based on a full page (4KB size) to specify the protection properties, so the same physical storage page can not have different protection properties . But another page in the same region can be another protection property .

15.3 simultaneous booking and allocation of physical memory

(1) Call VirtualAlloc and pass in mem_reserve| Mem_commit logo

(2) Allocation of large page memory

① return large page allocation granularity: size_t getlargepageminimum (), returns 0 if the system does not support large pages

② incoming mem_serve| when calling VirtualAlloc Mem_commit| Mem_large_page, that is, the memory must be booked and committed at the same time, but not separated to achieve.

③ the memory block size (dwsize) to be allocated at the same time must be the integer multiple of the large page allocation granularity, and the protection property Fdwprotect of the page must also be specified as Page_readwrite.

Note

① the memory that is allocated to the large page is non-exchangeable, that is, it must reside in memory and not be swapped out to the page interchange file. Therefore, the physical memory page is required to be locked.

② by default, to use "Lock memory page" to be authorized first. (by adding a user or group in the control Panel → administrative tools → local security policy → local policies → user Rights assignment → lock memory pages)

15.4 when to allocate physical memory

(1) Pre-booking the area without committing physical memory (again, actually committing to the paging file) can save a lot of physical memory.

(2) When an application accesses an uncommitted memory address, an exception is thrown, and the system notifies our application. All we have to do is set up an exception handler for the application, where we can actually commit the physical memory, so that the memory is "on demand".

15.5 undo allocation of physical memory and release area

(1) virtualfree (Pvadress,dwsize,fdwfreetype)

Undo and release the entire area : Pvaddress is specified as the base address of the zone, which is the return value VirtualAlloc when the subscription area is booked. At the same time dwsize must pass in 0because the system knows the size of the area. The 3rd parameter is passed mem_release. (Note that you cannot undo only a subset of areas at this time. In addition, the committed memory is released and the virtual address space of the reservation is returned , which means that the mem_release cannot be used Mem_decommand together! )

undo Some physical memory (but keep the virtual address space): Specify Pvaddress and dwsize, and pass in Mem_decommit. As with the reservation and submission of physical storage, the undo is also based on the granularity of the page, i.e. the system undoes all pages that the address space is pvaddress to pvaddress+dwsize ( Note that The page is a 4KB space, that is, if pvaddress is in the middle of a page, the page is also undone as a whole.

(2) When to deallocate physical memory

① because the undo operation is based on page granularity, when we undo it, we may undo the memory of a variable that is currently in use.

② for a safe undo operation, add a flag bit for each variable or struct to record the situation in which they are being used. Undo can only be undone until all adjacent structures on the same page are not in use.

"Demandpaging program" uses structured anomaly mechanism for "on-demand" memory

#include <windows.h>#include<tchar.h>#include<locale.h>#definePagelimit 80//total number of request pageslpbyte lpnextpage=null;//page address of the next requestDWORD dwpagesize =0;//Size of the page (allocation granularity)DWORD dwpages =0;//number of pages currently requestedINT pagefaultexceptionfilter (DWORD dwcode) {lpvoid lpvresult; //if it is not a page fault, exit    if(Dwcode! =exception_access_violation) {_tprintf (_t ("error occurred [%d]\n"), Dwcode); returnException_execute_handler;//executes the contents of the except code block when an error occurs. } _tprintf (_t ("page access error,")); if(Dwpages >=pagelimit) {_tprintf (_t ("number of pages exceeded%d\n"), pagelimit); returnException_execute_handler;//executes the contents of the except code block when an error occurs. } lpvresult=VirtualAlloc ((LPVOID) lpnextpage, dwPageSize, Mem_commit, page_readwrite); if(Lpvresult = =NULL) {_tprintf (_t ("submitting a new page failed! \ n")); returnException_execute_handler; } Else{_tprintf (_t ("a new page will be resubmitted! \ n")); } dwpages++; Lpnextpage+=dwpagesize; returnException_continue_execution;//executes the contents of the except code block when an error occurs. }void errorexit (LPTSTR oops) {_tprintf (_t ("error!%s, error code%ld\n"), oops, GetLastError ()); Exit (0);}int_tmain () {_tsetlocale (Lc_all, _t ("CHS")); LPVOID Lpvbase; //the memory base address to testLPTSTR lpptr;//generic character PointerBOOL bsuccess;//System_info si;//System Information Structure BodyGetSystemInfo (&si); dwPageSize=si.dwpagesize; _tprintf (_t ("CPU Page size is%dkb.\n"), si.dwpagesize/1024x768); //to book a page in the virtual space of a processLpvbase = VirtualAlloc (NULL,//The system automatically selects the base addressPagelimit*dwpagesize,//size of AreaMem_reserve,//Booking (note not submitted)page_noaccess);//protect properties, not read and write        if(Lpvbase = =NULL) {Errorexit (_t ("Booking page Failed")); } lpptr=(LPTSTR) lpvbase; Lpnextpage=(LPBYTE) lpvbase;  for(DWORD i =0; I < pagelimit*dwpagesize/sizeof(TCHAR); i++) {__try{//Write MemoryLpptr[i] = _t ('a');//because the physical memory is not committed, an exception is thrown here        }        //If a page fault occurs, submit another page and try to continue__except (Pagefaultexceptionfilter (GetExceptionCode ())) {//The following code only fails when the next page is submitted in the filter function//before it is called .ExitProcess (GetLastError ()); }    }    //Release Areabsuccess =VirtualFree (Lpvbase,0,//when using mem_release, it must be 0Mem_release);//Undo the submitted area (this is not added mem_decommit)_tprintf (_t ("release Operation%s.\n"), bsuccess? _t ("Success"): _t ("failed")); return 0;}

Garbage collection of "Vmalloc Program" memory (Application of VirtualFree function)

15th. Using virtual memory in your application (1)

Related Article

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.