Partner algorithm (Buddy)

Source: Internet
Author: User

 

Hi, Buddy!

Hedgehog @ http://blog.csdn.net/littlehedgehog

 

 

 

 

 

Bitmap

In the Linux kernel partner algorithm, the bitmap of each order indicates all idle blocks. For example, the memory of my computer is 256 MB (now I can compare the cards on the last qq homepage ), in theory, bitmap with order 0 contains 256 MB/(4 K * 2) blocks. Why divide by two? Because a bitmap corresponds to two partner blocks, 1 indicates that one of them is busy, and 0 indicates that both parts are idle.
Each time alloc or free requests an operating partner system, we need to perform an exclusive or operation. This is because the exclusive or operation means that the first two parts are idle at 0, later, one of them used an exception or 1 for a moment, and the other used an exception or 0 for a moment. Later, the previous one recycled an exception or 1 for a moment, later, the other part also recycles the difference or returns 0 for a moment,
In this way (if it is set to 1, it can be merged into a large block with the previous one.
The main purpose of bitmap is to indicate in the recycle algorithm whether it can be combined with the partner block. It is sufficient to search for the idle linked list during allocation. Of course, the corresponding bitwise must be different or omitted at the same time as the allocation, which serves to reclaim the algorithm.

 

 

Allocation Algorithm

Assume that in the initial stage, all the blocks with a size of 2 ^ 9 (MAX_ORDER is 10) and the serial numbers are 0,512,102 4 in sequence, in addition, the map bits of all areas are 0 (in fact, the operating system code occupies part of the space, but here is only an example). To allocate a 2 ^ 3 page block, you can perform the following actions:
1. search from the idle linked list of the area with order 3. If it is not found, search for the high-level area, and so on. According to the assumptions, the area with order 9 will be searched all the time, page 2 ^ 9 with the serial number 0 is found.
2. remove the 2 ^ 9 page block with the serial number 0 from the idle linked list of the area with the order 9 and set the 0th bits of the area (0> (1 + 9 )) an exception or 1.
3. split the 2 ^ 9 page blocks with numbers 0 into two 2 ^ 8 page blocks with numbers 0 and 256 respectively. The former is placed in the idle linked list of areas with order 8, and the 0th bits (0> (1 + 8) of the area are exclusive or 1 bit.
4. split the 2 ^ 8 page block with the serial number 256 into two 2 ^ 7 page blocks with the serial numbers 256 and 384 respectively. The former is placed in the idle linked list of area with the order of 7, the 1st-bit (256> (1 + 7) of the area is exclusive or 1 bit.
5. split the 2 ^ 7 page block with the serial number 384 into two 2 ^ 6 page blocks with the serial numbers 384 and 448 respectively. The former is placed in the idle linked list of area with the order of 6, the 3rd-bit (384> (1 + 6) of the area is exclusive or 1 bit.
6. split the 2 ^ 6 page blocks with serial numbers 448 into two 2 ^ 5 page blocks with serial numbers 448 and 480 respectively. The former is placed in the idle linked list of area with order 5, the 7th-bit (448> (1 + 5) of the area is exclusive or 1 bit.
7. split the 2 ^ 5 page block with the serial number 480 into two 2 ^ 4 Page blocks with the serial numbers 480 and 496 respectively. The former is placed in the idle linked list of area with the order of 4, the 15th-bit (480> (1 + 4) of the area is exclusive or 1 bit.
8. split the 2 ^ 4 Page block with the serial number 496 into two 2 ^ 3 page blocks with the serial numbers 496 and 504 respectively. The former is placed in the idle linked list of area with the order of 3, the 31st-bit (496> (1 + 3) of the area is exclusive or 1 bit.
9. The 2 ^ 3 page with the serial number 504 is the requested block.

 

About recycling Algorithms

1. when you recycle one page with order 4, first find the area with order 0 and add the page block to the idle linked list of the area, then, judge the status of the partner block (1 page block with serial number 5) and read the area (not other area !) 2nd bits (4> (1 + order) of the map. If the partner block is occupied, this bit is 0 (the first four blocks, 4 and 5 blocks are recycled ).
Are all busy), now it is an exception or 1, and will not be merged up.
2. when we recycle one page with the serial number 5, we first find the area with the order of 0 and add the page block to the idle linked list of the area, then, judge the status of the partner block (1 page block with the serial number 4), read the 2nd bits (5> (1 + order) of the map in the area )), at this time, this bit is 1 (4 items have been recycled). Now it is 0 for an exception or a moment, and it is merged up to sort the order.
The 1-page block with no. 4 and the 1-page block with no. 5 are removed from the free linked list of the area and merged into the 2-page block with No. 4, and put it in the idle linked list of area with order 1. Similarly, you have to judge the status of the merged peer block (2 pages of 6) and read the area (area with order 1, not other! (4> (1 + order) of the map. If the partner block has been recycled before, the bit is 1, and the current difference is 0, merge them up, and remove the 2-page block with the serial number 4 and the 2-page block with the serial number 6 from the idle linked list of the area with the order 1, merge them into 4-page segments and put them in the idle linked list of area with order 2. Then judge the status of the partner block.

 

 

 

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.