Memory management questions.

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
skwee
Member
Member
Posts: 73
Joined: Mon Jan 12, 2009 3:11 am

Memory management questions.

Post by skwee »

Hello there!
I want to ask few questions about memory management:
For now I got paging run and work: I mapped the whole area from 0 to _end (as defined in linker script) as the kernel directory. I used placement malloc as was described in James Molloy's tutorial, to allocate space for kernel needed stuff. Now I want to implant heap.
But I have a question, the heap is actually the memory manager? I mean if I do heap for kernel and heap for user I got memory management? Or I have to do something else?

Also I would like to ask about how to implement heap:
I tough about linked list, The list will hold pointers to free areas and their size, from the beginning the list will have only 1 pointer that points to the start of the kernel heap (I don't bother thinking about user heap, I have pretty long way till Ill reach the user space), and the size of the heap. When Ill want to allocate X bytes Ill go trough the list and find the first free block that is >= X, Ill put a header in front of that block that will contain data about this block, and at the end Ill put a footer (The approach is pretty similar to James Malloy's heap tutorial). IMHO this is pretty good method, but can be slow if I have a lot of free blocks referenced to different places in memory. So I tough also about binary search tree or even AVL tree, what do you think?


Thanks a lot!
TCP/IP: Connecting people...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Memory management questions.

Post by Brendan »

Hi,
skwo wrote:I want to ask few questions about memory management:
For now I got paging run and work: I mapped the whole area from 0 to _end (as defined in linker script) as the kernel directory. I used placement malloc as was described in James Molloy's tutorial, to allocate space for kernel needed stuff. Now I want to implant heap.
But I have a question, the heap is actually the memory manager? I mean if I do heap for kernel and heap for user I got memory management? Or I have to do something else?
In theory there's 3 completely different/separate memory managers: one for managing physical memory, one for managing linear memory, and one to manage the heap. However...

Some OSs try to use the same memory manager for different things. For example, if the entire physical address space could be identity mapped into the linear address space, then you could use the kernel's heap manager to allocate physical pages, and when the kernel allocates some physical pages they're already mapped into the (kernel's) linear address space. This is what Linux tries to do (with extra hacks and crud added when they realized that the entire physical address space can't be identity mapped into the linear address space, and no hope of sending kernel data to swap or supporting other linear memory manager tricks in kernel space).

IMHO this is like pretending you can use a hammer for nails, screws and applying glue - a hammer could be used for all those things but your results might vary, and three different tools designed for each specific job will probably give better results... ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
skwee
Member
Member
Posts: 73
Joined: Mon Jan 12, 2009 3:11 am

Re: Memory management questions.

Post by skwee »

Thanks for that answer.
However I want to clarify something:
By physical memory management you mean a manager that manages the real physical memory?
By linear memory management you mean a manager that manages the virtual memory (aka Paging)?
And by heap you mean dynamic memory allocation?
Also if someone would be able to comment on my first question about heap realization method.

Thanks again!
TCP/IP: Connecting people...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Memory management questions.

Post by Brendan »

Hi,
skwo wrote:By physical memory management you mean a manager that manages the real physical memory?
Yes, but it could include more than this. For example, it could be something that manages everything related to the physical address space, including allocating/freeing physical pages of RAM, sorting out MTRRs, finding good addresses for memory mapped PCI device BARs (and tracking these areas), handling hot-plug (hot-insert?) RAM, etc.
skwo wrote:By linear memory management you mean a manager that manages the virtual memory (aka Paging)?
Yes, possibly including allocating/freeing virtual address spaces, sending pages to/from some sort of swap manager, memory mapped files, shared memory, transactional memory, mapping memory mapped devices into the linear address space (including the PAT), monitoring page usage (e.g. least recently used algorithm for helping to decide which pages to send to swap), etc.
skwo wrote:And by heap you mean dynamic memory allocation?
Yes.

I should have mentioned that for processes dynamic memory allocation should be done by the process itself (e.g. using a library) so that each process can use whatever suits that process best. The kernel only needs to worry about it's own dynamic memory requirements.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
skwee
Member
Member
Posts: 73
Joined: Mon Jan 12, 2009 3:11 am

Re: Memory management questions.

Post by skwee »

Thanks again :)
I rewrote my mm, now I have PMM that handle blocks, and can allocate using placement malloc, and VMM that handles the paging stuff. However I still think it have poor design :\
TCP/IP: Connecting people...
Post Reply