Buddy Allocation

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
jensvdb
Posts: 3
Joined: Sun Jun 01, 2008 6:47 am

Buddy Allocation

Post by jensvdb »

Hello,

Working on my own OS (written in C++), I've come to a point to write a memory manager, and I've decided to go for buddy allocation. I only have one issue with this principle; imagine the following situation:

(# = used block; . = free block; xxK = block size)

Code: Select all

4K  ########
8K  # # # #
16K #   #
32K #
Let's say we want to free 20K (=5 blocks of 4K) starting on with the second block; we now have this situation:

Code: Select all

4K  #.....##
8K  # # # #
16K #   #
32K #
Now one would say a block of 16K has been made available, while actually, there isn't (because there is no sequence of 4 free blocks on a 16K boundary)!
I could move other 4K blocks (or larger pieces of memory) around; but in my opinion this would cause problems, for example:

Code: Select all

Object *obj = (Object *)alloc(sizeof(Object));
where 'alloc' returns the starting address of the allocated memory. If I move used chunks of memory around, then 'obj' won't be pointing to the actual object anymore!

Am I missing something or is this a negative side-effect of buddy allocation?

jensvdb
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Buddy Allocation

Post by Combuster »

People use Paging so that the addresses used does not need to match the physical address.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
jensvdb
Posts: 3
Joined: Sun Jun 01, 2008 6:47 am

Re: Buddy Allocation

Post by jensvdb »

Yes, I do understand the principle of paging, however then (with your explanation) I don't really see the benefits of using a buddy allocator.
The way I understand the wiki is like this:

* Paging splits the system RAM into "blocks" of memory (4K or 4MB or even 1G pages)
* This way, one can set up a virtual address space for each process (yes, this isn't position dependent anymore)
* how do we allocate pages? (I read the wiki page on "Page Frame Allocation") --> here comes my buddy allocator :)

Thus I made my allocator like this:

For the "4K" chunks of memory:
at physical memory address 0x0000 0000 is the first block; sized 0x1000 (4K; 1 page)
at physical memory address 0x0000 1000 is the second block; sized 0x1000 [...]

For the "8K" chunks of memory, it goes like this:
first block @ 0x0000 0000 physical
second block @ 0x0000 2000 physical [...]

And so on for 16K, 32K, 64K, 128K, 256K and 512K (like the Linux kernel did/does).

However, and here is my problem, when you PHYSICALLY move a page from one physical address to another; pointers and stuff like that get messed up?

It could be of course that I'm mixing "virtual" and "physical" memory, in which case: shame on me! But the way I understood the wiki is that a page frame allocator manages the PHYSICAL frames (thus pages) in memory...
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Buddy Allocation

Post by NickJohnson »

jensvdb wrote:It could be of course that I'm mixing "virtual" and "physical" memory, in which case: shame on me! But the way I understood the wiki is that a page frame allocator manages the PHYSICAL frames (thus pages) in memory..
I guess shame on you then - that is the flaw in your reasoning. When paging is enabled, pointers refer to virtual addresses, not physical ones (which is basically the definition of paging). You can't move frames from one physical address to another either (without physically moving memory chips around). You can however map physical frames to different pages, which moves them around within a virtual address space.
jensvdb
Posts: 3
Joined: Sun Jun 01, 2008 6:47 am

Re: Buddy Allocation

Post by jensvdb »

NickJohnson wrote:
jensvdb wrote:It could be of course that I'm mixing "virtual" and "physical" memory, in which case: shame on me! But the way I understood the wiki is that a page frame allocator manages the PHYSICAL frames (thus pages) in memory..
I guess shame on you then - that is the flaw in your reasoning. When paging is enabled, pointers refer to virtual addresses, not physical ones (which is basically the definition of paging). You can't move frames from one physical address to another either (without physically moving memory chips around). You can however map physical frames to different pages, which moves them around within a virtual address space.
:oops: I think I got it now ;)

Thank you!
Post Reply