Are there any good memory management tutorials?

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
meh
Member
Member
Posts: 52
Joined: Sun Oct 21, 2007 4:30 pm

Are there any good memory management tutorials?

Post by meh »

Where can I find good C++ memory management tutorials?
LordMage
Member
Member
Posts: 115
Joined: Sat Sep 22, 2007 7:26 am
Contact:

Post by LordMage »

Well, I am looking for the exact same thing. The problem is that everyone is going to say that memory management is something that you should really develop based on your own design. I understand why, but that makes it hard to grasp a real good understanding of everything. I am a visual learner and so text on a paper doesn't help me too much. I will suggest reading the memory management tutorials on OSdever but they will only provide you with enough code to start up paging and not really any memory handling code. They also suggest that you start with a simple malloc routine for allocating memory inside the kernel. which they don't provide. I am stuck right there for now. I have side tracked and worked on my console design as a way to put off getting in the nitty gritty of calculating how much memory is available to my kernel and how much is left after my kernel is done. James Malloy has a tutorial that shows memory management tutorials for paging with the most basic kmalloc. It will probably work if you want fast results but I am really starting to agree with everyone that says you should design your own system. The only problem is that means delving into theory and I hate theory.

Do check out both his tutorial and the OSDever site also. both are good resources. Malloy is a member of the site so just find a post by him. there is a link to his site in his sig and the OSDever site is easy enough to find in google or a link somewhere in the forums.
Getting back in the game.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Have a look at http://g.oswego.edu/dl/html/malloc.html.

Although my kernel is in C++, my memory allocation routines are very much a 'flat' C-style. The only concession to C++ is that once my malloc() routine was written, I added the new and delete operators, which basically just point to malloc() and free() functions anyway.

If you want physical page frame allocation routines, I think there are quite a few of these about in GPL'ed OS code and tutorials, but http://www.osdev.org/wiki/Page_Frame_Allocation is a good starting point.

Cheers,
Adam
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

My own memory management (heap) routines are exactly the same to the ones I wrote in my tutorials:

http://www.jamesmolloy.co.uk/tutorial_h ... 0Heap.html

The only difference between that and my own kernel is I wrapped it in a C++ "Heap" class.

There is also a physical frame allocation tut there.
maverick777
Member
Member
Posts: 65
Joined: Wed Nov 14, 2007 3:19 pm

Post by maverick777 »

Hiya I also will hopefully soon be at memmory management , added your tutorial to my favourites James :-) , I get a 404 page cannot be found when clicking on Adams hobbyOS , Maybe thats by intention? but figured I would mention it just in case :-)
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

I wrote my own page manager, then ported liballoc (it only needs 4 hooks) to both kernel and user space and wrap new/delete around that :) I remembered when I first started writing my kernel and words like "allocate" use to scare me.

But it's really quite simple, and even page management isn't as hard as it sounds, it's simply dividing the memory into (most commonly) 4kb chunks and keeping a list of used/free pages.

Liballoc (and most other malloc implementations) basically hook around your paging manager. Malloc will request, say, 16 pages, and your page manager will scan through the list of pages and find 16 free pages next to each other, marked them as used, and then return the first address of the first page.

Thing's get a little more complicated when you use processor paging, e.g. you need to set up page tables/directories, but essentially a page table is a list of pointers to pages to say what lot of memory goes where, and a page directory is a list of pointers to page tables. To change to another process's "memory" you simply change the processor's page directory address to the new one. To initialise paging it's really only two steps:
- Pass the address to a page directory to the processor
- Change one bit in a register.

The real headache is how you should generate the page tables for each process when you associate/unassociate a page to a process, but this is better left to a tutorial.

Having memory management set up means you can use cool things, like strings. I no longer have to use:

Code: Select all

char buffer[100];
console.GetString(buffer, 100);
Which limits my input to 100 chars, I can now do:

Code: Select all

String buffer;
buffer = console.GetString();
Which dynamically resizes its internal buffer as you add/remove characters.
My OS is Perception.
Post Reply