Windows core programming-Virtual Memory

Source: Internet
Author: User
Tags intel pentium

Wi n d o w s provides three memory management methods:
• Virtual memory, which is most suitable for managing large objects or arrays of structures.

• Memory ing files are most suitable for managing large data streams (usually from files) and sharing data among multiple processes running on a single computer.

• Memory stack, which is most suitable for managing a large number of small objects.

Virtual Memory status
Wi n d o w s function g l o B a l m e m o r y s t a t u s can be used to retrieve dynamic information about the current memory status:
Void globalmemorystatus (lpmemorystatus pmst );
If you want an application to run on a computer with a memory greater than 4g B, or the total size of swap files is greater than 4g B, then we can use the new g l o B a l m e m o r y s t a t u s e x function:
Bool globalmemorystatusex (lpmemorystatusex pmst );

Determine the address space status
Wi n d o w s provides a function that can be used to query some information about the memory address in the address space (such as the size, memory type, and protection attribute ).
This function is called vi r t u a l q u e r y:
DWORD virtualquery (
Lpcvoid pvaddress,
Pmemory_basic_information pmbi,
DWORD dwlength );
Wi n d o w s also provides another function, which enables a process to query the memory information of another process:
DWORD virtualqueryex (
Handle hprocess,
Lpcvoid pvaddress,
Pmemory_basic_information pmbi,
DWORD dwlength );
These two functions are basically the same. The difference is that when vi r t u a l q u e r y e x is used, the Process Handle that can pass the information about the address space you want to query. Debugging programs and other utilities use this function most. Almost all applications only need to call the vi r t u a l q u e r y function.
To obtain complete memory information, I created a function, namely v M Q U E R Y:
Bool vmquery (
Handle hprocess,
Pvoid pvaddress,
Pvmquery pvmq );
System Information

The value of many operating systems depends on the host, such as the page size and distribution granularity. These values should never be hard-coded into your source code. Instead, you should always retrieve these values during process initialization and use them in your source code. The g e t s y s t e m I n f o function will be used to retrieve host-related values:

 

Void getsysteminfo (lpsystem_info psinf );

The address of the s y s t e m _ I n f O structure must be passed to this function. This function initializes all structure members and returns them. The following is the data structure of s y s t e m _ I n f o.

 

Typedef struct _ system_info
{
Union
{
DWORD dwoemid; // obsolete, do not use
Struct
{
Word wprocessorarchitecture;
Word wreserved;
};
};
DWORD dwpagesize;
Lpvoid lpminimumapplicationaddress;
Lpvoid lpmaximumapplicationaddress;
Dword_ptr dwactiveprocessormask;
DWORD dwnumberofprocessors;
DWORD dwprocessortype;
DWORD dwallocationgranularity;
Word wprocessorlevel;
Word wprocessorrevision;
} System_info, * lpsystem_info;

When the system directs, it determines the value of these members. These values are always the same for any established system, so you never need to call this function multiple times for any established process.

Table 14-1 memory-related member functions

Member name Description
D w p a G E S I Z E Displays the page size of the c p u. On an x86 CPU, the value is 4 0 9 6 bytes. On the Alpha CPU, the value is 8 1 9 2 bytes. On I a-6 4, this value is 8 1 9 2 bytes
L p m I n I m u m a p L I c a t I o n a D R E S The minimum memory address used to give the available address space of each process. On Windows 98, the value is 4 194 304, or 0x0 4 0 0 0 0 0 0, because 4 m B in the address space of each process is not available. In Windows 2000, the value is 65 536 or 0x0 0 0 1 0 0 0 0, because the starting 6 4 k B in the address space of each process is always idle.
L p m a x I m u m a p L I c a t I o n a D R E S The maximum memory address used to give the available address space for each process. On Windows 98, the address is 2 147 483 647 or 0x7 f, because the shared memory ing file area and the shared operating system code are included in the above 2 GB partition. On Windows 2000, this address is the address starting from the kernel mode memory, which is less than 6 4 K B
D w a l o c a t I o n g r a n u l a r I t y Shows the allocation granularity of the reserved address space area. By the time this book was written, this value was 65536 on all wi n d o w s platforms.

Other members of this structure have nothing to do with memory management. For the sake of completeness, they are also described below (see Table 1 4-2 ).

 

Table 14-2 memory-independent member functions

Member name Description
D w o e m I d Expired, not referenced
W r e d e r v e d Reserved for future use, not referenced
D w n u m B e r o f P R o C E S o r s Used to specify the number of c p u in the computer
D w a C t I V E P R o C E S o r m A S K One-bit blocking is used to specify which c p u is active (the running thread is allowed)
D w p r o c e s o r ty P E Used only for Windows 98, not Windows 2000, indicating the type of the processor, such as Intel 386, 4 8 6, or P e n t I u m
W p r o c e s o r a r c h I t e c t u r e It is only used for Windows 2000, not for Windows 98, and is used to specify the processing structure, such as I n t E L, A L p H a, Intel 64-bit or alpha 64-bit
W p r o c e s o r l e v e l It is only used for Windows 2000, not for Windows 98, and is used to further segment the processor structure, for example, to set the Intel Pentium Pro or Pentium II
W p r o c e s o r e v I s I o n It is only used for Windows 2000, not for Windows 98, and is used to further segment the processor level

Reserve a region in the address space

By calling the vi r t u a l a l o C function, you can reserve a region in the address space of the process:

 

PVOID VirtualAlloc(   PVOID pvAddress,   SIZE_T dwSize,   DWORD fdwAllocationType,   DWORD fdwProtect);

The first parameter p v a d r e s contains a memory address, which is used to set the location where the address space is reserved. In most cases, you pass m u l for this parameter. It tells vi r t u a l o C that the system that stores a record of an idle address region should keep the region wherever it deems appropriate. The system can reserve a region from any location of the address space of the process, because it cannot guarantee that the system can allocate each region from the bottom of the address space up or from the top to the bottom. The m e m _ to P _ d o W n flag can be used to describe the allocation method. If the vi r t u a l a l o C function can meet your requirements, it returns a value indicating the base address of the reserved region. If a specific address is passed as the p v a d r e s parameter of vi r t u a l o C, the returned value is the same as the value passed to vi r t u a l a l o C, and is rounded to (if needed) the boundary value of 6 4 k B.

The second parameter of the vi r t u a l o C function is d w s I Z E, which is used to set the size of the region to be retained (measured in bytes ). Because the system must always retain a region that spans 6 2 kb B, the result is that an area spanning 6 4 kb B is generated on a computer that uses a 4 kb, 8 KB, or 16 KB page.

The third parameter of the vi r t u a l o C function is f d w a l o c a t I o n ty P E, it can tell the system whether you want to retain a region or submit physical storage (this distinction is necessary, because the vi r t u a l a l o C function can also be used to submit physical storage ). To retain an address space area, the m e m _ r e s e RV e identifier must be passed as the value of the f d w a l o c a t I o n ty P E parameter.

The last parameter is f d w p r o t e c T, which specifies the protection attribute that should be granted to the address space area. The protection attribute associated with the region does not affect the committed memory mapped to the region. No matter what the protection attribute is, if no physical storage is submitted, any attempt to access the memory address in the range will cause the thread to initiate an access violation.

Submit storage in the reserved Region

After a region is retained, you must submit the physical memory to the region before accessing the memory address contained in the region. The system allocates submitted physical storage to a region from its page file. Physical storage is always submitted by page boundary and page size blocks.

To submit the physical storage, you must call the vi r t u a l o C function again. However, this time, for the f d w a l o c a t I o n ty p e parameter, the m e m _ c o m I t mark is passed, instead of the m e m _ r e s e RV e sign. The passed page protection attributes are usually the same as those used when vi r t u a l o C is called to retain the region (in most cases, pa g e _ r e d w r I t e ), however, you can also set a different protection attribute.

In the reserved region, you must tell the vi r t u a l o C function where you want to submit the physical storage and how much physical storage to submit. To do this, you can set the memory address in the p v a d r e s parameter, set the number of physical memory in the parameter d w s I Z E (measured in bytes ). Note that you do not need to submit the physical storage to the entire region immediately.
Both region reservation and memory submission

Sometimes you may want to submit the physical storage to the region while retaining it. You only need to call the vi r t u a l a l o C function once to perform this operation, as shown below:

 

Pvoid pvmem = virtualalloc (null, 99*1024, mem_reserve | mem_commit, page_readwrite );

This function call requests to keep a 99 kb region and submit the 99 KB physical storage to it. When the system processes this function call, it first needs to search for the address space of your process and find an address continuous area in the unretained address space. It must be large enough, it can store 100 KB (on a computer with a 4 kb page) or 104 KB (on a computer with an 8 KB page ).

The reason for the system to search for the address space is that the parameter p v a d r e s has been set to n u L. If the memory address is set for p v a d r e s, the system needs to check whether there is sufficient unretained address space on the memory address. If the system cannot find enough unreserved address space, vi r t u a l o C will return n u l,

If a suitable region can be retained, the system submits the physical memory to the entire region. The pa g e _ r e a d w r I t e protection attribute will be assigned to both the region and the submitted memory.

Note that vi r t u a l a l o C returns the virtual address of the reserved and submitted regions, the virtual address is then stored in the p v m e m variable. If the system cannot find enough address space or submit the physical storage, vi r t u a l o C will return n u L.
Reclaim virtual memory and release the address space area

To reclaim physical storage mapped to a region or release this address space area, you can call the vi r t u a l f r e function:

 

BOOL VirtualFree(   LPVOID pvAddress,   SIZE_T dwSize,   DWORD fdwFreeType);

Change Protection attributes

Although this is rarely done in practice, you can change the protection attributes of one or more pages of the submitted physical storage.

To change the protection attribute of the Memory Page, call the vi r t u a l p r o t e c t function:

 

Bool virtualprotect (
Pvoid pvaddress,
Size_t dwsize,
DWORD flnewprotect,
Pdword pfloldprotect );

Of course, the Protection attribute is associated with the entire page of the memory, rather than each byte of the memory. Therefore, if you want to use the following code to call the vi r t u a l p r o t e c t function on a computer on a 4 kb page, the result is to grant the pa g e _ n o a c e s protection attribute to two pages in the memory:

 

VirtualProtect(pvRgnBase + (3 * 1024), 2 * 1024,   PAGE_NOACCESS, &flOldProtect);

Clears the content of the physical storage.

To demonstrate that the memory content has been cleared, we must put forward a lot of usage requirements for the system's r a M. To perform this operation, you can perform the following three steps:

1) Call the g l o B a l m e m o r y s t a T U S function to obtain the total capacity of R A m in the computer.

2) Call the vi r t u a l a l o C function and submit the amount of memory. This operation runs very fast because the system does not actually allocate r a m to the memory before the process tries to access the page.

3) Call the z e r o m e m o r y function to make the newly submitted page accessible. This puts a heavy burden on the system's r a m, causing some pages in r a m to be written into page files.

If the user specifies that the data will be accessed later, the data will not be cleared and will be transferred to r a m when the data is accessed later. However, if the user specifies that the data will not be accessed in the future, the data will be cleared and the system will not write the data to the page file, which improves the running performance of the application.

 

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.