Memory Management algorithm--buddy partner algorithm "Go"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/orange_os/article/details/7392986

Advantages and disadvantages of the buddy algorithm:

1) Although the partner memory algorithm has done quite well on the memory fragmentation issue, but in this algorithm, a very small block will often hinder a large chunk of the merger, a system, the allocation of memory blocks, the size is random, a piece of memory only a small block of memory is not released, next to the two large can not be merged.

2) There is a certain waste in the algorithm, the partner algorithm is the power of 2 to allocate memory block, of course, this is a reason, that is, in order to avoid the large block of memory is too broken, more important is to make the allocation and release process quickly. But he also brought a disadvantage, if the required memory size is not 2 power, there will be some of the page wasted. Sometimes it's serious. For example, the original is 1024 blocks, applied for 16 blocks, and then apply for 600 blocks on the application is not, because has been divided.

3) Additional splits and merges involve more linked lists and bitmap operations, and the overhead is relatively large.

Buddy (definition of partner):

This gives the concept of a partner, which meets the following three criteria called a partner:
1) Two blocks of the same size;
2) two block address continuous;
3) Two blocks must be separated from the same chunk;

The allocation principle of the buddy algorithm:

If the system needs 4 (2*2) page-size memory blocks, the algorithm will be found in the free_area[2], if there are free blocks in the list, it is directly removed from and allocated. If not, the algorithm will follow the array up to find free_area[3], if there is free block in free_area[3], then it is removed from the linked list, divided into two parts of equal size, the first four pages as a block inserted free_area[2], the last 4 pages allocated, Free_ AREA[3] also did not, and then look up, if free_area[4] there, will be 16 (2*2*2*2) pages, etc. into two parts, the first half of the linked list such as Free_area[3], the second half of the 8 pages divided into two equal, the first half hung free_area[ 2]
In the linked list, the latter half is allocated. If FREE_AREA[4] is not there, repeat the process above, knowing that the Free_area array is reached at the end, and if not, discard the assignment.


The release principle of the Buddy algorithm:

The release of memory is the inverse of the allocation process and can also be seen as the merging process of the partner. When releasing a block, first in its corresponding linked list to check whether a partner exists, if there is no partner block, the block to be released directly into the linked list, if any, then take off from the list of partners, combined into a large chunk, and then continue to investigate the merged block in the larger list of whether there are partners exist, Until you cannot merge or have merged to the largest block (2*2*2*2*2*2*2*2*2 pages).


Throughout the process, the bitmap plays an important role, as shown in 2, one of the bitmaps corresponds to two mutual partners of the block, 1 means that one of the pieces has been allocated, for 0 means both are idle. The partner either allocates or releases is only relative to the bitmap for the XOR operation. When allocating memory to a bitmap
is to release the process service, the release process depends on the bitmap to determine whether the partner exists, if the corresponding bit XOR 1, then no partner can be merged, if the XOR or operation is 0, merge, and continue to merge the partners in this way until it cannot be merged.


Buddy implementation of memory management:

When it comes to Buddy, I think of the management of the physical memory under Linux, the buddy system implemented on the memory pool.

Unlike the buddy system on Linux, which is implemented by page, he does the block size by 2 of the bytes.

The main structure of the implementation mechanism is as follows:

the structure of the entire buddy system :

struct mem_pool_table

{

#define Mem_pool_table_init_cookie (0x62756479)

UInt32 Initialized_cookie; /* The Cookie indicates that the memory has been initialized after the magic number if it has been initialized set to 0x62756479*/

Uint8 *mem_pool_ptr; /* point to Memory pool address */

UInt32 mem_pool_size; /* The size of the entire pool, below is the entire Max block sizes */

UInt32 max_block_size;/* Must be 2 of the n-th squareThat represents the size of the largest block in the pool*/
Boolean Assert_on_empty;/* If the value is set to True, the memory allocation request returns and outputs an error message when it is not completed*/
UInt32 mem_remaining;/* bytes of memory remaining in the current memory pool*/
UInt32 Max_free_list_index;/* Subscript for maximum freelist,*/
struct mem_free_hdr_type     * Free_lists[max_levels];< Span style= "color: #00cc;" >/* This is the level array of the partner system */

#ifdef feature_mem_check
UInt32 max_block_requested;
  UInt32 min_free_mem; /*/
#endif /* Feature_oncrpc_mem_check*/
}

This structure is included in the structure of free node or alloc node:

Where check and fill are set to a pattern
To check the legitimacy of that node.
#define Mem_hdr_check_pattern ((uint16) 0X3CA4)
#define Mem_hdr_fill_pattern ((uint8) 0x5C)


typedef struct TAGBUDDYMEMBLOCKHEADTYPE

{

Mem_pool_type Pool; /* Point back to Memory pool */

UInt16 check;

Uint8 State; /* bits 0-3 put that node belongs to that 1-level bit 7 if set 1, indicating that it has been allocated (not free)

Uint8 fill;

} Buddy_mem_block_head_type;


This structure is the structure of the free header that contains the node type structure:

typedef struct TAGBUDDYMEMHEADTYPE

{

Mem_node_hdr_type HDR;

struct Mem_free_hdr_type * PNEXT; /* Next,prev, bidirectional list*/for connecting the free header

struct Mem_free_hdr_type * PPREV;

} Mem_free_hdr_type;

This structure is the structure of the Alloc header that contains the node type structure:
The allocated Mem node is represented in memory.

    1. typedef struct MEM_ALLOC_HDR_TYPE
    2. {
    3. Mem_node_hdr_type HDR;

    4. #ifdef Feature_mem_check_overwrite
    5. UInt32 in_use_size;
    6. #endif

    7. } Mem_alloc_hdr_type;

Which uses in_use_size to indicate how much is actually used on the level of the requested allocated size.
For example, to apply for size=2000bytes, press size to level should be 2048, actual in_use_size
Is 2000, the remaining 48byte is all populated for a certain value, then at a later time free is available for check
Is there a value for Overwite to 48byte, generally for speed, only 8 to 16byte is checked

And why not put the remaining 48byte in the other level of the freelist, which may
Because the disadvantage of the original buddy system is that it is prone to fragmentation, which makes it even more brittle.

About free or alloc node:

Assume

The minimum block is 2^4=16, which is determined by the Mem_alloc_hdr_type (12byte) and is actually assignable to 4byte

If the maximum max_block_size = 1024 is assumed,

If the pool has a mem_free_hdr_type[0], two blocks of 1024 block node are hung

is free node, purple is Alloc node


The next major buddy system operations include Pool init, mem alloc, mem free

Pool init:
1. Remove the size of the actual pool mem_pool_table the size of the structure and place it in the
Mem_pool_size, and modify the actual mem_pool_ptr pointing forward mem_pool_table
Address of the structure size
2. Next, mainly mem_pool_size size of memory, the largest block to hang on free_lists
The level is 0 on the list, and then the size portion is less than the extent of the block, continuing to hang the next
level, loop to complete processing (feel the size actually used for pool, should be minus
The size of the mem_pool_table is then aligned with the largest block of sizes, which is better,
But not actually tested)


Mem Alloc:
This part is quite simple, first according to the size of the request mem, the actual allocation needs to add Mem_alloc_hdr_type
This 12byte, and then according to the resized size, the calculation should actually be assigned at that level, if there are corresponding levels
Very simple, direct return, if not, a first-level round-robin lookup, found after putting the saved part in the downward level
The first level is inserted at the corresponding level of the Freelist

MEM Free:
Where the address of free, minus 12 can get mem_alloc_hdr_type structure
Then determine if the buddy is in front of the free block, or back, and then merge buddy,
Loop to find the upper level of the buddy, and then merge, only to the maximum block size level



About this algorithm, in <<the Art of Computer programming>> Vol 1, the
Dynamic storage allocations are described, and the algorithm is useful for small systems with only osal

Memory Management algorithm--buddy partner algorithm "Go"

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.