thx a lot, for the feedback, it really helps a lot when other ppl give us suggestions and recommendations.
JamesM wrote: 2) I assume this implementation is not written for your own OS? I'm confused by the #include <stdio.h>, <stdlib.h> stuff...
you're right, a don't really need them, but at first i was only using this malloc inside my OS in bochs, so i really couldn't test the performance. ( my OS isn't ready for me to boot in a real hardware

).
i've google "malloc tester", and get some file called "mallocer.c" which was a simple code to test malloc speed, that's why those headers are included.
JamesM wrote: 4) Consider the following worst-case scenario:
Code: Select all
for(int i = 1; i < 0xFFFFFF; i++)
{
kfree(kmalloc(i));
}
How would your heap manage this? As far as looking at the code can tell me, you would allocate a different linked-list structure for each allocation, because although they all get freed, the next allocation will always be bigger than the last. I.e. your algorithm doesn't reclaim and amalgamate space back into bigger holes. [EDIT] and thus would eventually run out of memory.
JamesM
hmm..very good question, i've questioned myself about this too, and i'm having a few ideas how to handle it. so far this is what i've thought.
my allocator allways align size, for instance, if i need 11 bytes, it will allocate a block of 16 bytes, if 12, again 16, if i need 21 it will be 32, and so on (they are always powers of 2), so with your suggested test code it will create 24 linked lists and thats a lot :p
I was thinking of this structure:
Code: Select all
typedef struct block_header
{
int free
int size;
struct block_header *next_blk; /* this are used for linking in the free list */
struct block_header *prev_blk; /* same */
struct block_header *next; /* this will never change is the "real " physical neighbors */
struct block_header *prev; /* same */
} block_header_t;
This could make possible for me to check if the neighbors were free, and join them.
But i'm thinking of a way to test this quick and not do this test every time we free or alloc other block, i think it will overhead the code.
again, thx for the suggestions, it really helps a lot.