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.