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
pskyboy

Memory Manager

Post by pskyboy »

Hey Guys

I was wondering if any one had any code for a memory manager i could look at as i have read the books and looked at tutorials on doing the memory manager but am still a bit lost as to how to implement it. I was hoping that looking at a real solution might help.

Cheers
Peter
DarylD

Re:Memory Manager

Post by DarylD »

To be honest the best way of implementing one is to read books rather than read code. Most code you come across for memory management looks way more complex than it actually is!

But I think most implementations work similar. My kernel allocates a fixed number of pages to start with, in my case 8, this gives me 32K of heap. Then I have two linked list of c structs, one list is the free map and the other is the list of allocations.

Basically, you scan the free list starting at the front looking for a free space big enough for what has been requested. Then in this space put the allocation struct and return the address of the memory just after this allocation struct..this returned address is the same as malloc would return. Then update the free entry accordingly to shrink it, or remove it if it is zero.

Sure that is probably as clear as mud!!
pskyboy

Re:Memory Manager

Post by pskyboy »

Yeah you see this is what all the books say but i want to implement Demand Paged virtual memory so this is slightly different as i need to memory manages for a start one for physical one for virtual. Also i don't think i need to do any scanning for a memory space as i will just give the application another page if it needs more memory or allocate enough page sfor it when it is loading.

Peter
DarylD

Re:Memory Manager

Post by DarylD »

Ah!

We have our wires crossed, for example my system a memory manager which will allocate arbitrary sized memory blocks and also has a procedure which just spits outs free pages.

What you need is to build a free page stack (or something similar), this is a a list of all the addresses of physical pages that can be used, what you need to do is either return a physical page address from this routine (and pop it off the stack accordingly) or use a function to allocate virtual-physical pages.

I actually found this quite hard to get my head around, because you will need to make sure the page directory and page tables are updated accordingly (unless you simply use 4MB of physical memory for the page tables, then you won't need to be messing around with page tables, you just need to index into a uint32 * array of pages.

I am sure I am making this sound more complicated than it actually is, can anybody else explain it better?!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Memory Manager

Post by Pype.Clicker »

my own system (check it if you wish), the memory management is a two-shots feature:
1. you allocate (by the mean of kalloc) some addresses (i.e. virtual memory), but it's usually not granted with physical mem.
2. every page in the page table is marked with a special tag that says "not present, allocate a Zero-filled page on demand")
3. when a page fault is issued, the system just checks if the tag is "PG_ZERO", and if this is the case, a new page frame is allocated
and the frame number is set to the faulty virtual address.
Post Reply