# Buddy Memory Allocation Program | Set 2 (Deallocation)

Prerequisite – Buddy Allocation | Set 1
Question: Write a program to implement the buddy system of memory allocation and deallocation in Operating Systems.

Explanation –
As we already know from Set 1, the allocation is done via the usage of free lists. Now, for deallocation, we will maintain an extra data structure-a Map (unordered_set in C++, HashMap in Java) with the starting address of segment as key and size of the segment as value and update it whenever an allocation request comes. Now, when deallocation request comes, we will first check the map to see if it is a valid request. If so, we will then add the block to the free list tracking blocks of its sizes. Then, we will search the free list to see if it’s buddy is free-if so, we will merge the blocks and place them on the free list above them (which tracks blocks of double the size), else we will not coalesce and simply return after that.
How to know which block is a given block’s buddy?
Let us define two terms-buddyNumber and buddyAddress. The buddyNumber of a block is calculated by the formula:

We note that this is always an integer, as both numerator and denominator are powers of 2. Now, a block will be another block’s buddy if both of them were formed by the splitting of the same bigger block. For example, if 4 consecutive allocation requests of 16 bytes come, we will end up with blocks 0-15, 16-31, 32-47, 48-63 where blocks 0-15 and 16-31 are buddies (as they were formed by splitting block 0-32) but 0-15 and 32-47 aren’t. The buddyAddress of a block is the starting index of its buddy block, given by the formula:

block_starting_address+block_size (if buddyNumber is even)
block_starting_address-block_size (if buddyNumber is odd)

Thus, all we have to do is find this buddyAddress in the free list (by comparing with all the starting addresses in that particular list), and if present, coalescing can be done.

Examples:
Let us see how the algorithm proceeds by tracking a memory block of size 128 KB. Initially, the free list is: {}, {}, {}, {}, {}, {}, {}, { (0, 127) }

• Allocation Request: 16 bytes
No such block found, so we traverse up and split the 0-127 block into 0-63, 64-127; we add 64-127 to list tracking 64 byte blocks and pass 0-63 downwards; again it is split into 0-31 and 32-63; we add 32-63 to list tracking 32 byte blocks, passing 0-31 downwards; one more spli is done and 0-15 is returned to user while 16-31 is added to free list tracking 16 byte blocks.
List is: {}, {}, {}, {}, { (16, 31) }, { (32, 63) }, { (64, 127) }, {}
• Allocation Request: 16 bytes
Straight up memory segment 16-31 will be allocated as it already exists.
List is: {}, {}, {}, {}, {}, { (32, 63) }, { (64, 127) }, {}
• Allocation Request: 16 bytes
No such block found, so we will traverse up to block 32-63 and split it to blocks 32-47 and 48-63; we will add 48-63 to list tracking 16 byte blocks and return 32-47 to user.
List is: {}, {}, {}, {}, { (48, 63) }, {}, { (64, 127) }, {}
• Allocation Request: 16 bytes
Straight up memory segment 48-63 will be allocated as it already exists.
List is: {}, {}, {}, {}, {}, {}, { (64, 127) }, {}
• Deallocation Request: StartIndex = 0
Deallocation will be done but no coalesscing is possible as it’s buddyNumber is 0 and buddyAddress is 16 (via the formula), none of which is in the free list.
List is: {}, {}, {}, {}, { (0, 15) }, {}, { (64, 127) }, {}
• Deallocation Request: StartIndex = 9
Result: Invalid request, as this segmeent was never allocated.
List is: {}, {}, {}, {}, { (0, 15) }, {}, { (64, 127) }, {}
• Deallocation Request: StartIndex = 32
Deallocation will be done but no coalesscing is possible as the buddyNumber of the blocks are 0 and 2 buddyAddress of the blocks are 16 and 48, repectively, none of which is in the free list.
List is: {}, {}, {}, {}, { (0, 15), (32-47) }, {}, { (64, 127) }, {}
• Deallocation Request: StartIndex = 16
Deallocation will be done and coealsecing of the blocks 0-15 and 16-31 will also be done as the buddyAddress of block 16-31 is 0, which is present in the free list tracking 16 byte blocks.
List is: {}, {}, {}, {}, { (32-47) }, { (0, 31) }, { (64, 127) }, {}

Figure – Buddy algorithm-allocation and deallocation

Implementation –
Below is the complete program.

Output:
Memory from 0 to 15 allocated
Memory from 16 to 31 allocated
Memory from 32 to 47 allocated
Memory from 48 to 63 allocated
Memory block from 0 to 15 freed
Sorry, invalid free request
Memory block from 32 to 47 freed
Memory block from 16 to 31 freed
Coalescing of blocks starting at 0 and 16 was done

Time Complexity –
As already discussed in set 1, time complexity of allocation is O(log(n)). For deallocation, in the worst case, all the allocated blocks can be of size 1 unit, which will then require O(n) time to scan the list for coalescing. However, in practice, it is highly unlikely that such an allocation will happen so it is generally much faster than linear time.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

CSE B Tech 4th Year at IIT Bhubaneswar||Summer Intern 2018 IIT Roorkee||Summer Intern 2019 Goldman Sachs

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :