Example page allocator & virtual 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
melodyrs
Posts: 2
Joined: Fri Jan 10, 2025 3:07 am
Libera.chat IRC: melodyrs

Example page allocator & virtual memory manager?

Post by melodyrs »

Hiya

I'm working on a hobby x86_64 OS for fun and I'm at the point where I need to add a memory allocator. I've done a lot of reading up on paging and I get the gist of it but I really need some code to use as a reference (or for porting)
As I understand it you need a page allocator to allocate physical memory and a virtual memory manager to actually assign those pages to regions of virtual memory

Writing both of these sound like really daunting tasks and I'm unsure where to start, which is why I'm thinking reading some actual code would help
Any recommendations? Doesn't have to be any language in particular
Last edited by melodyrs on Fri Jan 10, 2025 8:34 am, edited 2 times in total.
sebihepp
Member
Member
Posts: 194
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: Example page allocator & virtual memory manager?

Post by sebihepp »

Hi Tysm,

for me personally implementing a malloc routine is much more of a daunting task. With PageFrame allocators you have one big advantage: The Size that will be allocated is always of the same size - a complete page.

Think of the interface first. What should a physical page frame allocator can do? An initialisation routine would be good, which can handle a memory map and generates all needed data structures. Then you want to allocate one page and free one page. It doesn't need to do much more. The same goes for the virtual page frame allocator.

A bit more work is implementing paging methods. You need to map a virtual address to a physical address and you should be able to get the physical address for a given virtual address. The trickiest part is to get your mind to understand paging. After you really understood it, its not as hard to implement these methods, as one might think.
You can have a look into my code. Its still in development, lacks some comment and I don't guarantee it works in every situation, but you wanted code so you will get code: https://github.com/sebihepp/HeppOS
Please use the sebihepp branch for checkout. In subdirectory kernel/src/memory you will find paging.h and paging.cpp

malloc is much harder, as it needs to allocate virtual page frames, physical ones, then map them and then handle objects with different sizes in an efficient way. It needs a dynamic structure itself to keep track of the allocated memory, but cannot use malloc itself.

Hope this helped.

Best regards
Sebi
melodyrs
Posts: 2
Joined: Fri Jan 10, 2025 3:07 am
Libera.chat IRC: melodyrs

Re: Example page allocator & virtual memory manager?

Post by melodyrs »

Thanks!
Also I go by melody, didn't mean to imply tysm was what I go by (oops)
sebihepp
Member
Member
Posts: 194
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: Example page allocator & virtual memory manager?

Post by sebihepp »

Oh, I am sorry melody. Somehow I saw Tysm anywhere while replying and thought it was your name.
AnotherIdiot
Posts: 13
Joined: Sat Sep 28, 2024 8:00 pm
GitHub: https://github.com/ThatOSDeveloper

Re: Example page allocator & virtual memory manager?

Post by AnotherIdiot »

For psychical memory management I would try using liballoc (with some mods) that is all I really know on the memory management front.
You know the drill what people put in here https://github.com/SlugOS/SlugOS
sebihepp
Member
Member
Posts: 194
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: Example page allocator & virtual memory manager?

Post by sebihepp »

AnotherIdiot wrote: Sun Jan 12, 2025 8:40 pm For psychical memory management I would try using liballoc (with some mods) that is all I really know on the memory management front.
liballoc first requires a physical and a virtual memory manager and implements malloc and free. So he still needs to get around paging, physical and virtual memory.
AnotherIdiot
Posts: 13
Joined: Sat Sep 28, 2024 8:00 pm
GitHub: https://github.com/ThatOSDeveloper

Re: Example page allocator & virtual memory manager?

Post by AnotherIdiot »

sebihepp wrote: Mon Jan 13, 2025 2:01 am
AnotherIdiot wrote: Sun Jan 12, 2025 8:40 pm For psychical memory management I would try using liballoc (with some mods) that is all I really know on the memory management front.
liballoc first requires a physical and a virtual memory manager and implements malloc and free. So he still needs to get around paging, physical and virtual memory.

Code: Select all

int   liballoc_lock();
int   liballoc_unlock();
void* liballoc_alloc(int);
int   liballoc_free(void*,int);
from the liballoc github page https://github.com/blanham/liballoc

No it does not require those, it just requires you to make the above functions, which may do what you said or may not, my method is only a psychical memory manager for now and I use liballoc, and it works just fine without a virtual memory manager.
You know the drill what people put in here https://github.com/SlugOS/SlugOS
sebihepp
Member
Member
Posts: 194
Joined: Tue Aug 26, 2008 11:24 am
GitHub: https://github.com/sebihepp

Re: Example page allocator & virtual memory manager?

Post by sebihepp »

AnotherIdiot wrote: Tue Jan 14, 2025 7:19 pm

Code: Select all

int   liballoc_lock();
int   liballoc_unlock();
void* liballoc_alloc(int);
int   liballoc_free(void*,int);
from the liballoc github page https://github.com/blanham/liballoc

No it does not require those, it just requires you to make the above functions, which may do what you said or may not, my method is only a psychical memory manager for now and I use liballoc, and it works just fine without a virtual memory manager.
Thats right. But from what he said I assumed he wants to support paging in his OS. But at least a physical page frame allocator is needed. And thats where he requested assistance.
AnotherIdiot
Posts: 13
Joined: Sat Sep 28, 2024 8:00 pm
GitHub: https://github.com/ThatOSDeveloper

Re: Example page allocator & virtual memory manager?

Post by AnotherIdiot »

sebihepp wrote: Thu Jan 16, 2025 1:41 am
AnotherIdiot wrote: Tue Jan 14, 2025 7:19 pm

Code: Select all

int   liballoc_lock();
int   liballoc_unlock();
void* liballoc_alloc(int);
int   liballoc_free(void*,int);
from the liballoc github page https://github.com/blanham/liballoc

No it does not require those, it just requires you to make the above functions, which may do what you said or may not, my method is only a psychical memory manager for now and I use liballoc, and it works just fine without a virtual memory manager.
Thats right. But from what he said I assumed he wants to support paging in his OS. But at least a physical page frame allocator is needed. And thats where he requested assistance.
Oops, I thought when you wrote that you meant that liballoc requires it (which it does not) which kinda confused me, my bad.
You know the drill what people put in here https://github.com/SlugOS/SlugOS
Post Reply