Memory Manager - How to store the metadata?
Posted: Thu Apr 02, 2009 3:50 pm
Hi there,
I'm just trying to implement my kmalloc() in my memory manager.
I've got a workig page allocator which marks a page as used in a bitmap and I also have paging working.
At least for allocation.
Now I wanted to implement a first fit algorithm to start at an easy level.
I've currently got the following idea in my head floating around:
My kernel resides at (virtual) 0xC0000000 = 3GB and I currently assume that the kernel and the stack behind it has a size of 4MB.
After that the kernel heap starts, for now, at 0xC0400000 and can grow up to 0xFFBFFFFF. The last page is used for the identity paging.
Now if I want to allocate memory with my kmalloc() function for the first time, I allocate a page and map it into the heap-space.
Then I create a little structure for the metadata which contains the size of the allocated space, what type it is (free or used) and a pointer to the next structure which then resides after the just allocated memory with type "free" and the size of the rest of the space for the heap.
Before allocating space I always need to check wether a new page must be mapped into the space and do it if neccessary.
Freeing would also be easy: Just look at the neighbours if they are free and merge the entries in the linked list. But how can I find the predecessor of an entry? A double linked list would be much more prone to errors when messing around with all these pointers.
Would this be the right thing how to do it? Or should I store the metadata per allocated page in the heap?
Oh and some minor question about the stack: Should I locate the stack on some other place? So designate some own space for it?
Thanks for your suggestions,
Andreas
I'm just trying to implement my kmalloc() in my memory manager.
I've got a workig page allocator which marks a page as used in a bitmap and I also have paging working.
At least for allocation.
Now I wanted to implement a first fit algorithm to start at an easy level.
I've currently got the following idea in my head floating around:
My kernel resides at (virtual) 0xC0000000 = 3GB and I currently assume that the kernel and the stack behind it has a size of 4MB.
After that the kernel heap starts, for now, at 0xC0400000 and can grow up to 0xFFBFFFFF. The last page is used for the identity paging.
Now if I want to allocate memory with my kmalloc() function for the first time, I allocate a page and map it into the heap-space.
Then I create a little structure for the metadata which contains the size of the allocated space, what type it is (free or used) and a pointer to the next structure which then resides after the just allocated memory with type "free" and the size of the rest of the space for the heap.
Before allocating space I always need to check wether a new page must be mapped into the space and do it if neccessary.
Freeing would also be easy: Just look at the neighbours if they are free and merge the entries in the linked list. But how can I find the predecessor of an entry? A double linked list would be much more prone to errors when messing around with all these pointers.
Would this be the right thing how to do it? Or should I store the metadata per allocated page in the heap?
Oh and some minor question about the stack: Should I locate the stack on some other place? So designate some own space for it?
Thanks for your suggestions,
Andreas