Tolga wrote:But there is a problem. When a created any dynamic object, i will create a LinkedList struct in heap. To store this LinkedList informations, i will create another LinkedList. To store ...
Hi,
Currently, my MM is very simple and I think it needs to be this way to start with - you don't want MM problems i when you are debugging other parts of your system - it makes tracking errors much more difficult!
What I was suggesting was as follows:
1. Set up a *single* linked list, storing just free space - to get the ball rolling, you just need 1 item, which you can assign statically. This linked list may be something like:
Code: Select all
struct mylinkedlist
{
base;
limit;
*next;
(*prev; - if you want a doubly-linked list)
};
So, the base may be set at 0xD0000000 and the limit at 0xDFFFFFFF, for example.
2. When I request
n bytes, your allocation routine actually assigns n+4 bytes. To do this, simply increase 'base' by n+4, thus removing the space from the linked list.
3. At 0xD0000000, place a double word containing how many bytes you just assigned.
4. Your kmalloc(bytes) function returns 0xD0000004.
5. Your kfree(void*ptr) function simply has do do a (ptr-4) to find out how many bytes it needs to free.
*This* is where it gets a little (but not much) more complex!
If You have assigned more free space, you cannot simply amend the 'base' of your heap linked list to free the space you have assigned, and you will need another structure in your linked list. All you have to do is a :
Code: Select all
kmalloc(sizeof(struct mylinkedlist));
and fill the values with the base and limit of your newly freed area.
This is not as chicken and egg as it first seems as your kmalloc will *never* need to create more new structures - you are simply amending an existing structure (until you start worrying about alignment and fragmentation - but that comes much later...).
That's how my basic MM works. I thought I was creating an algorithm to see me through some of the earlier stages of kernel development but it has proven more robust than I imagined!
[edit]Sorry - I didn't fully read the previous post which says kind of the same thing, but I'll leave this here as an alternative answer anyway
[/edit]
Cheers,
Adam