Are there any good memory management tutorials?
Are there any good memory management tutorials?
Where can I find good C++ memory management tutorials?
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.
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.
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
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
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.
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.
-
- Member
- Posts: 65
- Joined: Wed Nov 14, 2007 3:19 pm
- AndrewAPrice
- Member
- Posts: 2309
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
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:
Which limits my input to 100 chars, I can now do:
Which dynamically resizes its internal buffer as you add/remove characters.
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);
Code: Select all
String buffer;
buffer = console.GetString();
My OS is Perception.