VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/buddy-memory-allocation-program-set-2-deallocation/

⇱ Buddy Memory Allocation Program | Set 2 (Deallocation) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Buddy Memory Allocation Program | Set 2 (Deallocation)

Last Updated : 11 Jul, 2025

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 a 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 their sizes. Then, we will search the free list to see if its 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: 

(base_address-starting_address_of_main_memory)/block_size

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 splits done and 0-15 is returned to the 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 was found, so we will traverse up to block 32-63 and split it into blocks 32-47 and 48-63; we will add 48-63 to list tracking 16-byte blocks and return 32-47 to a 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 coalescing is possible as its 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 segment was never allocated. 
    List is: {}, {}, {}, {}, { (0, 15) }, {}, { (64, 127) }, {}
  • Deallocation Request: StartIndex = 32 
    Deallocation will be done but no coalescing is possible as the buddyNumber of the blocks are 0 and 2 buddyAddress of the blocks are 16 and 48, respectively, 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 coalescing 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) }, {}


👁 Image

Figure - Buddy algorithm-allocation and deallocation


Implementation -
Below is the complete program. 


Output
Memory from 0 to 15 allocate
Memory from 16 to 31 allocated
Memory from 32 to 47 allocate
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, the 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.

Comment
Article Tags: