Data structure - the buddy system memory management

Partner system itself is the difference between a dynamic memory management method, and a method of identifying a boundary that is: the use of storage system management partner, whether it is occupied or free block the block size is 2 to the power of n (n is a positive integer) .

For example, the entire memory space of the system 2 m words. Then carrying out several allocation and recovery, the available space table may contain only space is: 2 0 , 2 . 1 , 2 2 , ..., 2 m free blocks.
Word is a unit of measurement, consists of several bytes, the median number of bytes for the different machines, word included. For example, an 8-bit machine word of one byte; a 16-bit machine word consists of 2 bytes.

Available space in the node configuration table

Partner systems may be utilized in the node configuration table space shown in Figure 1:


16823531-456a289e4d7f99d3.png
A node configuration in FIG .png

header field is represented as the head node, composed of four parts:

  • llink and rlink pointer for the node type field, respectively, for direct and immediate predecessor point successor node.
  • tag values: a status flag memory block, is occupied block (represented by 1) or a free block (represented by 0)
  • kval: recording capacity of the storage block. Since each of the memory system block is a power of 2 m, the value of m kval recorded.

Code is expressed as:

typedef struct WORD_b{
    struct WORD_b *llink;//指向直接前驱
    int tag;//记录该块是占用块还是空闲块
    int kval;//记录该存储块容量大小为2的多少次幂
    struct WORD_b *rlink;//指向直接后继
    OtherType other;//记录结点的其它信息
}WORD_b,head;

In the partner system, since the system will continue to accept the request of the user application memory, it will have a lot of different sizes but all capacity of 2 m memory block, in order to facilitate distribution to find the time, the system uses the same size each build a list. For the initial capacity of 2 m in one piece of storage, the list is formed, there might m + 1 th, in order to better manage these lists, these systems linked lists of m + 1 is stored in the header array, similar to the structure of the adjacent table, as shown.

16823531-f474ce6ac6a88698.png
The initial state of the system of Figure 2 partner .png

Available space code table expressed as:

#define m 16//设定m的初始值
typedef struct HeadNode {
    int nodesize;//记录该链表中存储的空闲块的大小
    WORD_b * first;//相当于链表中的next指针的作用
}FreeList[m+1];//一维数组

Allocation algorithm

Buddy system allocation algorithm is simple. Assume that the user request to the system memory space of size n, if the 2 K-. 1 <n <= 2 K , then you need to view the available space in the table of size 2 K linked list node no space available:

  • If the list is not NULL, the method can be taken directly from a plug head from the head node, available to users;
  • If a size of 2 K of the linked list is NULL, it is necessary sequentially to see more than 2 K larger list, removed from the list after finding, taken space corresponding size to users, the remaining space, according to the size into the appropriate list.

For example, a user to apply the system to the spatial size of 7 words, and the total system memory is 24 words, at this time in accordance with a buddy system allocation algorithm derived: 2 2 <7 to <2 . 3 , so in this case should see the size of available space in table 2 . 3 whether there is an idle list of nodes:

  • If there is, in the list is removed from a node, assigned directly to users;
  • If not, successively view the need to 2 . 3 larger if each of the list there is an idle node. Suppose, in 2 Size . 4 has a linked list of free blocks, then removal of the free blocks assigned to the user 2 . 3 spatial word, the remaining two . 3 characters, the remaining free block to the size of 2 . 3 linked list.
16823531-c0e02f23c448d333.png
Before (A) assigned (B) after the dispensing process dispensing system of FIG. 3 partners .png

Collection algorithm

Whatever the memory management mechanism used on the issue of memory recovery will face a common problem: how to reclaim memory for effective integration, the buddy system is no exception.

When a memory block is no longer used in the user application, the system needs to recover this part of the memory block needs to judge whether or may be combined with other free blocks during recovery.

When looking at the object of merging, and different systems partners boundary identification method, a buddy system for each memory block has a respective "buddy", when the user releases the memory block only needs to determine whether the partner of the memory block is a free block, if that will merge, and then merge the new free block also need to determine integrate with its partners. Conversely direct memory block may be inserted into the space depending on the size of the table can be used.

When determining the position of a partner memory block, the method is employed: if the starting address of the memory block as p, the size of 2 K , then the start address of its partner is located:

16823531-38f552529450c813.png
image.png

For example, when the size of 2 . 8 , the start address is calculated starting address of the block is 512 partners:

Since the MOD 2 512 . 9 = 0, so that 512 + 2 . 8 = 768, and if the memory block is recovered, only the starting address to view the status of memory block 768, if the block is free to combine the two, and vice versa directly the recovered link to release the block size is 2 . 8 linked list.

Use of a buddy system for storage management process, when the user space applications, because of the different sizes of free blocks in different linked list, so the distribution is completed faster, the algorithm is relatively simple.

The recovery storage space for merging the free blocks, not depending on the state of the block adjacent to the free position of the block; it depends entirely on its partner block. So free block, but not because the relationship between the two partners, so it will not merge even if the memory block adjacent location. This is one of the drawbacks of this system: Because the only partner in the merger consideration, so prone to debris storage.

Guess you like

Origin blog.csdn.net/weixin_34087307/article/details/90966899