How to implement a Physical Memory Manager?

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
User avatar
slashx
Posts: 4
Joined: Thu Jul 30, 2020 3:19 pm

How to implement a Physical Memory Manager?

Post by slashx »

There are couple ways I know of - bitmap, linked list, array.

Using a bitmap is the simplest approach. 1 bit in the bitmap is one memory block of physical memory. The address of the block depends on index of the bit set.
The allocation is fast as it's just search for 1 or n number of bits indicating a free block.
The problem I can see is that it can get quite big. I know that today's memory is cheap but 128 KiB is a bit big (4 GiB memory, 1 bit - 4096 B block).

In a linked list, a single entry can span multiple blocks. So instead of spending 160B to cover 20 blocks, I can just add one entry that is 8B and covers that 20 blocks.
One entry points to another. The allocation is slower since we need to jump around the memory.

Array is like the merge of the two previous approaches.
Fast access, single entry can cover multiple entries. The problem is in keeping it sorted and possibly making it resizable.

I've considered a tree, but it's rather too complex for just keeping track of allocated physical memory.

What's is a good approach on this?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to implement a Physical Memory Manager?

Post by iansjack »

128KB is 0.003% of 4GB - it's not really a large amount.
User avatar
pvc
Member
Member
Posts: 201
Joined: Mon Jan 15, 2018 2:27 pm

Re: How to implement a Physical Memory Manager?

Post by pvc »

My memory manager uses 1 32 bit value for each frame, and it's perfectly fine. The reasoning behind is, that COW (copy-on-write) implementation is very simple, using reference counting. It takes 4 MiB to cover 4 GiB of memory. It is still just about 0.1% overhead. But in the long run, memory and time savings are absolutely worth it.
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: How to implement a Physical Memory Manager?

Post by Octacone »

I personally use a bitmap. It is simple to write and fast with minimal overhead.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
slashx
Posts: 4
Joined: Thu Jul 30, 2020 3:19 pm

Re: How to implement a Physical Memory Manager?

Post by slashx »

Came back to this topic after a break and yeah, maybe i am just a bit too paranoid. :oops:
This is rather from my Softwaredev that whenever i deal with data the size of couple kilobytes, a red light lights up in my head.
Post Reply