Divide the block into two parts. These two are called buddies. Go to 2, having the

J.E.D.I

7.6.4 Buddy-System Methods

In buddy-system methods, blocks are allocated in quantum sizes. Several avail lists, one for each allowable size, are maintained. There are two buddy system methods: • Binary buddy-system method – blocks are allocated in sizes based on the powers of two: 1, 2, 4, 8, …, 2 k words • Fibonacci buddy-system method – blocks are allocated in sizes based on the Fibonacci number sequence: 1, 2, 3, 5, 8, 13, … k-1+k-2 words In this section, we will cover binary buddy-system method only.

7.6.4.1 Binary Buddy-System Method

Figure 1.137 Buddies in the Binary Buddy-System

7.6.4.2 Reservation

Given a block with size 2 k , the following is the algorithm for reserving a block for a request for n words: 1. If the current block’s size n: • If the current block is the largest size, return: No sufficiently large block is available. • Else go to the avail list of the block next in size. Go to 1. • Else, go to 2. 2. If the block’s size is the smallest multiple of 2 ≥ n, then reserve the block for the requesting task. Return. Else go to 3.

3. Divide the block into two parts. These two are called buddies. Go to 2, having the

upper half of the newly cut buddies as the current block. For example, reserve space for the requests A 7K, B 3K, C 16K, D 8K, and E Data Structures 152 J.E.D.I 15K from an unused memory pool of size 64K. Data Structures 153 J.E.D.I Data Structures 154 J.E.D.I Data Structures 155 J.E.D.I Figure 1.138 Binary Buddy System Method Reservation Example Data Structures 156 J.E.D.I Liberation When a block is freed by a task and if the buddy of the block being freed is free, collapsing of the buddies is needed. When the newly collapsed block’s buddy is also free, collapsing is performed again. This is done repeatedly until no buddies can be collapsed anymore. Locating the buddy is a crucial step in the liberation operation and is done by computation: Let β k: α = address of the buddy of the block of size 2 k at address α β k: α = α + 2 k if α mod 2 k+1 = 0 β k: α = α - 2 k otherwise If the located buddy happens to be free, it can be collapsed with the newly-freed block. For the buddy-system method to be efficient, there is a need to maintain one avail list for each allowable size. The following is the algorithm for reservation using binary buddy system method: 1. If a request for n words is made and the avail list for blocks of size 2 k , where k =  log 2 n  , is not empty, then we get a block from the avail list. Otherwise, go to 2. 2. Get a block from the avail list of size 2 p where p is the smallest integer greater than k for which the list is not empty. 3. Split the block p-k times, inserting unused blocks in their respective avail lists. Using the previous allocation as our example, liberate the blocks with reservation B 3K, D 8K, and A 7K in the order given. Data Structures 157 J.E.D.I Figure 1.139 Binary Buddy System Method Liberation Example Figure 1.140 Data Structures 158 J.E.D.I In the implementation, the following node structure, a doubly-linked list, will be used for the avail lists: LLINK TAG KVAL RLINK TAG = 0 if free block 1 if reserved block KVAL = k if block has size 2 k To initialize the memory pool for the buddy-system method, assume that it is of size 2 m . There is a need to maintain m+1 lists. The pointers avail0:m to the lists are stored in an array of size m+1.

7.6.5 External and Internal Fragmentation in DMA