Heap management in the NT kernel (1): Key Structure

Source: Internet
Author: User

Strictly speaking, both the user State and the kernel state have content related to heap management, and both use the same copy.Code, Which are separated by macros in some different places. Write c on WindowsProgramThere will be more than one "heap manager" involved, such as malloc, free using the heap manager in the C producer. It seems quite confusing. In contrast, there is no so-called "heap manager" in the Linux kernel, and all heap-related operations are implemented in glibc (C Runtime on Linux). Dynamic Application (small block) is required in the kernel) memory. The kmalloc function is used and the applied physical memory is used.Gussing.cnblogs.com

The essence of heap management is that the system stores a large amount of memory at a time and then retail it to applications. The memory released by the application is not directly returned to the system's virtual storage management system. Instead, the heap manager is given to the system, and the heap manager is returned at a time when the memory size is enough. Four important queues are responsible for the wholesale and retail: Segment queue, lookaside queue group, freelist queue group, and virtualallocatedblock queue.Gussing.cnblogs.com

The segment queue is a static array with a size of 64 and acts as a wholesaler. The memory reserved by other queues is actually in the segment array. They are just referenced.Gussing.cnblogs.com

A freelist queue group is a buffer area of a memory block, with 128 queues. The memory released by the application will be retained in freelist first, and then the application will request the memory to be retained in freelist first. For example, if a program releases 6 units of memory, it will be inserted into the freelist [6] queue. Next time the program applies for 6 more memory units, you can directly use the memory block in the freelist [6] queue. Of course, the actual situation is a little more complicated than this, because when you apply for 6 units of memory, freelist [6] may be empty. At this time, you need to follow freelist [7], freelist [8] finds the appropriate block (assuming 9. The heap manager then splits the block into two parts, one of which is 6 units of size and returns to the program; the other is 3 units of extra memory. Originally, all three units can be used directly. Now, the first unit is requisitioned as a block header for management and inserted into freelist [3. In a freelist queue group, queue 0th is special because the memory blocks saved in the heap manager contain a management domain of 1 unit, and the minimum size is 1, it cannot be 0, so the queue No. 0 is empty. Taking into account that freelist has only 128 arrays, there is no place for memory blocks greater than 128, just as freelist [0] is empty, put it there. Therefore, freelist [0] retains memory blocks greater than 128 and smaller than a certain threshold.Gussing.cnblogs.com

The lookaside queue leader is similar to the freelist queue group and also has 128 queues. However, the difference is that the lookaside queue will not accommodate applications, and the program will apply for memory in this queue. If there is a suitable queue, you will get it, if it is not suitable, you can pull it down. Do you want him to split it into a big one for you? Sorry, you cannot do it. This can alleviate the problem of memory fragmentation: if a program requests to release small pieces of memory many times, all the memory stored in freelist is basically small pieces of memory, at this time, you need to apply for a large one. Even if there is enough memory, the heap manager cannot get the right one. The lookaside queue does not separate you. Even if you apply to release a small memory many times, it will not affect the large memory.Gussing.cnblogs.com

The memory size of each application operation is usually very small, dozens of bytes, and hundreds of K are the most common, but you cannot stop people from applying for huge memory, for example, if someone wants to apply for 20 mb of memory, can you leave it to him? With such a large memory block, freelist and lookaside have no place to save it for him (the reserved value in freelist [0] Must be smaller than a certain threshold value, the heap manager simply bypasses all the queue containers and directly applies for virtual storage. The applied memory is placed in the virtualallocatedblock queue.Gussing.cnblogs.com

These are the key data structures of the heap manager. We will discuss the specific operations on these structures later.Gussing.cnblogs.com

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.