Linux 2.6 Kernel Read notes memory management

Source: Internet
Author: User

July 29, 2014 Buddy allocation algorithm

The kernel needs to provide a robust and efficient allocation strategy for assigning a contiguous set of page boxes. Assigning a contiguous page box must resolve external fragmentation in memory management (external fragmentation). Frequent requests and the release of a set of contiguous page boxes of different sizes will inevitably result in the allocation of a block of page boxes to many small pieces of free page boxes that cannot be used for a large number of allocations.

The Linux kernel uses well-known partner system algorithms to solve external fragmentation problems. The core idea of the algorithm is to divide all the free page frames into 11 chain-block tables. The size of each chain table is 1,2,4,8,16,32,64,128,256,512 and 1024 consecutive page boxes.

Each block B can be divided into two B1 and B2, assuming that the index of the B1 is Bi2, and because the inside page box is contiguous, the BI1 can be compared with (1<<order (2^order = number of pages in Block B)) or the operation gets Bi2, Bi2 can also get bi1 in the same way.

The physical address of the first page box for each block should be an integer multiple of the block size. For example, a block with a size of 16 page boxes whose starting address should be a multiple of 16*2^12 (2^12 is a page box size, 4K). Suppose you want to request a block of 256 page boxes, the algorithm first checks the list of 256 page boxes for a free block. If not, find the next larger page box, which is a list of 512 page boxes. If such a block exists in the list of 512 page boxes, the block of 512 page boxes is divided into two points, one for satisfying the request, and one for inserting into the list of 256 page boxes. If there are no such blocks in the list of 512 page boxes, look in the list of 1024 page boxes. If there are such blocks in the list of 1024 page boxes, the 256 pages of the 1024 page boxes are used to satisfy the request, the 512 page box blocks in the remaining 768 page boxes are placed in a linked list of 512 page boxes, and the remaining 256 page box blocks are placed in the linked list of the 256 page boxes.   If there are no such blocks in the list of 1024 page boxes, the allocation fails and returns NULL. At the time of release, the partner's index is obtained by XOR the index of the page box, by removing the effect from the linked list in which it is currently located and the partner merge.

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.