Analysis of memory mapping files
Author: xrbeck
MAPPING map is an important part of Windows Memory Management and is also programmed.
A more advanced topic in technology. At present, there is a relatively small amount of information in this area, and in fact memory maps
The file is actually very important to our memory for Windows, and write the mind here.
Come out, discuss with everyone.
Memory space and mapping
I believe everyone is already known, the biggest difference between Win32 and 16 windows is Win32
Introduced the process-oriented independent virtual address, the addressing space of this address reaches 4GB (2 ^ 32), of course
This address is virtual. Each process has its own independent space, the address A address 0x10000000
And the address B address 0x10000000 has no smile contact (just in the user's process address space, excluding other
range). Speaking of this place may be strange, only 64m (or 128m, etc.) in my machine, how can there be such a big address space? How do the same addresses in the process A and process B be identified make not conflict?
Let us take a look at the memory space of Windows (Note: Here we are discussing with Win9x,
Of course, Win2K or WinNT and 9X will be in a certain aspect)
0x00000000 ---- 0x003FFFFFFFFFF 4m belongs to the system reserved area
0x00400000 --- 0x7FFFFFF 2G-4M Process Separate Address Space
0x80000000--0XBFFFFFF 1G Win32 shared space, used to store
Memory map, etc.
0xC0000000 --- 0xfffffffff 1G used to store VXD, etc.
With the above list, the user's program runs in the second address range, and the map file we use to discuss is in the third address range. And when we debug the program, we often have a pointer. Variable value
For how much, this value refers to the address in the virtual address space.
So how do Windows convert this virtual address space into the address of the RAM on the actual PC?
This involves the problem of mapping, that is, the page (PAGE) to achieve the corresponding two addresses. This is believed
I have learned in the course of the operating system, this is no longer repeated. In this problem, the address situation
It may be as follows:
Process a RAM process B
0x10000000
1 page
.........
1 page
0x10000000
In this way, I believe that everyone has clearly clear the relationship between the address space in the program and the address of the specific OS access. The size of the page, the different platforms have different values, Windows is 4K, we can use getSystemInfo DWPAGESIZE in the system_info returned by this API is known to the road)
(Remark 2: During the actual mapping process, it is aligned with 64K).
Virtual Memory
However, we know that RAM is valuable and scarce, which has been launched in the 16-bit Windows era.
The hard disk exchange file is provided to provide virtual memory, and Win32 is a page file on the hard disk to continue.
Support virtual memory. According to RICHTER: "The size of the system page determines how much application can be used
The most important factor in physical memory, RAM is just a small impact. "During the actual memory access, the system
First, I will find the information you need in RAM. If you can't find it, you will provide a page error to let OS on page.
Focus on the file, if you find the contents of the page file to RAM, otherwise you will report an error.
Tip "Invalid Page Error" (also our most frequently encountered program error). Here, we may wish to put the page
The file is understood as "Reserve RAM". (Windows provides the user to control the virtual memory method is
The system option in the control panel). So in this case, the main role of RAM just has
And the page files on the hard disk do data exchange, so there is Richter's statement.
If the user program wants to use virtual memory yourself, the first step is to process address
Reserved in space (in 4m-2g), then submit this space (commit)
Give real memory. Windows provides our operation interface for virtual memory is a set of VirtualAlloc and VirtualFree, so we can use the huge characteristics of virtual memory.
The general program is difficult to solve. For example, it is assumed to have a two-dimensional structure Item [300] [256]; each unit
For 200 bytes, now to modify a unit inside, you have to allocate such a big memory almost
It is impossible, and the score is difficult to reflect the simplicity of the two-dimensional structure directly with the subscript.
You can keep this huge structure first, then only submit some parts to be modified to actual memory, make
The final operation is simple and effective.
The function of retaining and submitting virtual memory for Windows is just one: Virtualalloc, but it is
The parameters are different. In the case of the above example, we can do this:
LPVOID PSTART = NULL;
LPVOID PITEM = NULL;
DWORD OFFSET = 0;
/ / Reserve the entire data structure in the default location of the system, return the preserved address
Pstart = Virtualalloc (NULL, 300 * 256 * 200, MEM_RESERVE, PAGE_READWRITE);
...
/ / Calculate the offset position to access
.
// only submit some of them to memory
Pitem = Virtualalloc (PStart Offset, 200, Mem_Rserve, Page_Readwrite);
// Directly modified
Pitem = ....
VirtualFree (PStart, 300 * 256 * 200, Page_Readwrite);
However, virtual memory will also bring inconvenient, suppose we have to run a program, follow the previous
The approach is to use the page file to be accessed as a virtual memory so that the system must preserve first.
The address space of the program, then submits the physical memory, next to the program files from the hard disk
Copy to the system's page file, last load operation. Such results must be loaded with an application
The time of the program is very long, so the real practice of the system is to put the program file (.exe) as a memory
Use, this is no longer allocated from the page file, making the load time increased.
This feature is undoubtedly very attractive, but you can directly take the file as memory, isn't it very convenient?
Yes. When the system loads the EXE file and the DLL file, the system is automatically handled. So if
Is it a general data file to use this feature? The answer is yes, that is, we wrapped around
The topic of the big circle is ultimately to say: memory map files.