Windows core programming-Virtual Memory

Source: Internet
Author: User

Microsoft Windows provides three mechanisms for memory operations.
1. Heap --------- is most suitable for managing a large number of small objects.
2. Virtual Memory ----- It is most suitable for managing large object arrays or large Structured arrays.

3. Memory ing files-it is most suitable for managing large data streams (usually files) and sharing data among multiple processes running on the same machine.


Next I will introduce several functions used in virtual memory.
Lpvoid virtualalloc (
Lpvoid lpaddress, // address of the memory area to be allocated
DWORD dwsize, // allocated size
DWORD flallocationtype, // allocation type
DWORD flprotect // initial protection attribute of the memory
);
Function: book or submit some pages in the virtual address space of the calling process.
Lpvoid lpaddress: Address of the allocated memory area. When you use virtualalloc to submit a previously reserved memory block, the lpaddress parameter can be used to identify the previously reserved memory block. If this parameter is null, the system determines the location of the allocated memory area, and rounded up by 64-kb (Roundup ).
Size_t dwsize, the size of the region to be allocated or retained. This parameter is in bytes rather than pages. The system will distribute the parameter to the DWORD at the Bottom page according to the size.
Flallocationtype: Allocation type. Mem_reserve is the region of the reserved address space; mem_commit is used to allocate physical memory, that is, the actually allocated memory. It can also be scheduled and allocated at the same time.
DWORD flprotect specifies the access protection mode for the allocated area

Size_t winapi virtualquery (
_ In_opt _ lpcvoid lpaddress, // address of the Region of pages to be queried.
_ Out _ pmemory_basic_information lpbuffer,
// Address of Information Buffer
_ In _ size_t dwlength // size of Buffer
);
Function: view the page allocation of the process memory address space.

Bool virtualfree (
Lpvoid lpaddress, // region address
Size_t dwsize, // region size, in bytes
DWORD dwfreetype // type
);

Function: cancels or releases a region of the virtual address space page of the calling process.

Below is a simple example Program

# Include <windows. h> # include <tchar. h> # include <iostream> // reprinted please indicate the article from: http://blog.csdn.net/windows_nttypedef tchar * ptstr; void main () {tchar szappname [] = _ T ("mem_reset tester "); tchar sztestdata [] = _ T ("some text data"); // commit a page of storage and modify its contents. ptstr pszdata = (ptstr) virtualalloc (null, 1024, mem_reserve | mem_commit, page_readwrite); lstrcpy (pszdata, sztestdata); {printf (_ T ("modified data page. \ n "); // we want this page of storage to remain in our process but the // contents aren't important to us anymore. // tell the system that the data is not modified. // Note: Because mem_reset destroys data, virtualalloc rounds // The base address and size parameters to their safest range. // here is an example: // virtualalloc (pvdata, 5000, mem_reset, page_readwrite) // resets 0 pages on CPUs where the page size is greater than 4 kb // and resets 1 page on CPUs with a 4 kb page. so that our call to // virtualalloc to reset memory below always succeeds, virtualquery // is called first to get the exact region size. memory_basic_information MBI; virtualquery (pszdata, & MBI, sizeof (MBI); virtualalloc (pszdata, MBI. regionsize, mem_reset, page_readwrite) ;}{ printf (_ T ("clear data page. \ n "); // commit as much storage as there is physical Ram. memorystatus MST; globalmemorystatus (& MST); pvoid pvdummy = virtualalloc (null, MST. dwtotalphys, mem_reserve | mem_commit, page_readwrite); // touch all the pages in the dummy region so that any // modified pages in RAM are written to the paging file. zeromemory (pvdummy, MST. dwtotalphys );}}

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.