11. Windows API Memory Management (1)

Source: Internet
Author: User

1. Related Concepts

1. Memory primary storageProgramMachines required for runningCodeAnd data.

The minimum memory unit is byte. Each byte in the memory has an address. In a 32-bit system, the 32-bit number is used to represent the memory address, so a total of 232(4294967296-4 GB) bytes.

2. Address

(1) address space.

The set of all available memory addresses in the system is called address space. For example, if you can use 4 GB memory, the address space is 0x00000000 ~ 0 xffffffff.

(2) physical memory.

The real storage space in the hardware system is called the physical memory. The physical memory is accessed through the hardware system bus.

But not every 32-bit machine has a 4 GB physical address space, so the physical address space is not necessarily 0x00000000 ~ 0 xffffffff. For example, in a system with 1 GB physical memory, only 0x00000000 ~ 0x3fffffff can be used.

(3) virtual address space.

To ensure unified and convenient access to memory, the operating system allows programs running on the operating system to access all addresses in the 4 GB memory space. Therefore, the operating system must perform some necessary address translation, convert the address accessed by the program to the real physical address in the physical memory, and then access the data. After the OS is switched, the address space used by the program is called the virtual address space. On a 32-bit system, the available virtual address space is 4 GB.

Of course, because the virtual address space may be larger than the real physical address space, the system will convert some addresses in the virtual address space into data in the hard disk, when necessary, the data in the physical memory is exchanged with the data in the hard disk.

This address translation and data exchange are implemented through paging and segmentation mechanisms..

(4) process memory space: User memory space and kernel memory space.

In Windows, each process has its own virtual address space. The 32-bit window operating system divides the 4 GB virtual memory (up to 8 TB on the 64-bit System) into two parts. The process uses 2 GB, which is called the user process space; the kernel uses 2 GB, which is called the system address space or the kernel address space (it can also be set to 3 GB for the user's process space and 1 GB for the kernel. Here we only discuss the situation of 2 GB for each ), the address range of the user space is Zero X 00000000 ~ 0 X7fffffff. The address range of the kernel space is 0 X80000000 ~ 0 Xffffffff. The virtual address space is closed in the process, and the process can only access its own address space. To access the address space of other processes, a special mechanism is required.

3. Paging and segmented memory management, memory ing and address conversion

The top 10 digits of the 32-bit address are called the page Directory Index and the next 10 digits are called the page table index. The remaining 12 digits are offset ). If only the 20 bits of the first two parts are taken and the last bits are filled with 0, the addresses that it may point to are all 4 kb (212) alignment, that is, the base address of the page. Therefore, the first 20 of the 32 bits position is the base address (PFn) of the page, and the last 12 bits are the offset of the address in the page.

It is equivalent to selecting the book first, then the page number of the book, and then the row on the middle page.

In the process of address translation, a key system register (S3. High 12 ~ The 32-bit address of the page Directory Index in the physical memory is saved.

The address translation process is as follows.

(1) From 12 ~ The 32-bit extracted address. The last bit is supplemented with 0 to form a 32-bit address. This address is the base address of the physical memory where the page Directory is located (therefore, the page Directory must be 4 kb aligned in memory ).

(2) use the data in the top 10 page Directory Index (directory) in the linear address as the offset, and add the base address of the physical memory where the page Directory obtained in the previous step is located, find the corresponding page Directory item (PDE) in the page Directory ).

(3) The page Directory includes the base address of the page table. The page table index (table) in the linear address is the offset in the page table, and the page table item (PTE) is found ).

(4) The page table item (PTE) stores the address of the page in the physical address space (PFn). If the page to which the PFN points is invalid (not in the physical memory ), in this case, page errors will occur in the system kernel. The System Kernel performs page scheduling to switch pages temporarily unavailable in the physical memory to the hard disk, swap the pages to be accessed in the hard disk to the physical memory. The pages directed to by PFN are valid.

(5) The base address of the page where the physical address corresponding to the linear address is located has been found.

(6) based on the physical page address and the offset of the last 12 digits of the linear address, the correspondence between the linear address and the physical address can be obtained.

It can be seen that the address conversion is in the unit of pages, that is, the linear address and the corresponding physical address Page Base Address (PFn) will change, the offset address on the page does not change.

Each page Directory item corresponds to a page table, with a total of 2 10 Page Directory. Each page table item corresponds to a memory page, and each page table can represent a maximum of 2 10 Page tables. A maximum of 2 20 Pages.

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.