Allocating memory via Buddy Allocation I'm trying to learn buddy allocation and whilst I think I understand the basics of it, there is one problem exercise with which I'm struggling with.


So I have an 8K block and I'm using buddy allocation trying to always allocate to the leftmost free memory area.. and I need to allocate a 5K memory block.

Do I split the 4K block into two 2K blocks, and take (from the left) the 1k, 2k, 2k blocks, or do I split the middle 2k block into two 1k blocks and take the 4k and the first 1k block from the right? Or do i simply take the 2k and 4k blocks that I have now?

Or is this type of allocation simply not possible if I need to make use of the whole 8k block (since the first 1k block is being used)?
None of the above.  First, block sizes are rounded up to the nearest power of 2.  If you try to allocate an object of length 5K, it will first round that up to 8K.  Next, it will try to find a free block of size 8K.  In your example, you'll need to look at larger blocks -- the 1K, 2K, and 4K blocks are too small.  You'd better hope there is another 8K block (not shown) that is free, or an even larger block that is free and can be split up to create a 8K block.