Linux partner algorithm (BuddyAllocator)

Source: Internet
Author: User
Linux partner algorithm (BuddyAllocator)-Linux general technology-Linux programming and kernel information. The following is a detailed description. Author: WANG Chun in the snow

This algorithm is used to manage Linux physical memory. The application of this algorithm determines that the unit of memory allocation must be two N power pages each time, such as pages 1, 2, 4, 8, and 32. After the Memory Manager is initialized, the memory allocation is very simple. My question is, how can we achieve high efficiency when releasing the memory? I think very sb:

Release a power (order) as k memory. First, find whether there are adjacent blocks in the linked list corresponding to k, if any, merged. After merging, continue to search for adjacent blocks in the linked list with order k + 1 (this is a new block.

If this is an interview question, I told the interviewer that he might think I really understand it! Actually, I don't understand it at all. Why? The problem lies in the bold words above: search. In my mind, the search here means that we can find adjacent items through the judgment of the address in the traversal chain table until we find them. In the kernel, if someone had to use this sb behavior, someone would have rewritten it with a binary tree or hash algorithm!

Therefore, read the _ free_pages_ OK () function in mm/page_alloc.c of linux source code and find out how to run it once. The list_add macro is called only once. According to my above ideas, list_add must be called frequently. Amazing! So I read it more carefully. It repeatedly called the list_del macro in while, and the rest of the Code did not read it carefully. I suddenly understood it. Oh, I am stupid! My thinking is limited to the operation of the free_list linked list, and I forgot, the page search can also be based on mem_map! When I put my thinking on mem_map, I found everything very simple: Isn't it linear search for adjacent areas? Given an area and knowing its length, let you find adjacent areas with the same length before and after it. Is that simple?

It is inseparable from the elaborate design of the list_head structure to complete this task so elegantly. What impressed me most is the two simple macros INIT_LIST_HEAD and list_empty:
QUOTE:
Related Article

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.