Page 1 of 1

could anyone please give me a "plug-in" malloc AND

Posted: Sat Jun 03, 2006 12:15 am
by earlz
I have tried at least 14 times to make a malloc and have looked at other code but it is very confusing

I got a page allocator so no worries there(that seems to be what everyone leads me to) just i can not figure out how to make a malloc with a free

Re:could anyone please give me a "plug-in" malloc

Posted: Sat Jun 03, 2006 1:42 am
by durand
You could always have a look at mine: http://www.djm.co.za/liballoc/

It's good enough to run firefox, openoffice, etc...and you just need to implement 4 functions and it will work in your system.

Re:could anyone please give me a "plug-in" malloc

Posted: Sat Jun 03, 2006 5:25 am
by paulbarker
There are hundreds of these things. For my kernel, kalloc() and kfree() are wrappers around bget. Other systems you might try are Doug Lea's malloc (dlmalloc) and the allocator in PDCLib. Take a look around, decide which one suits you best rather than jumping on the first.

Re:could anyone please give me a "plug-in" malloc

Posted: Sat Jun 03, 2006 11:15 am
by earlz
@durand
I haven't got it all implemented yet but your malloc and stuff seems to be very good thanks so much

edit:
liballoc works great! thanks again durand
plus thanks to using it i have discovered a big problem in my page allocator when allocating multiple pages >:( but anyway workign on getting that fixed now

Re:could anyone please give me a "plug-in" malloc

Posted: Sat Jun 03, 2006 4:33 pm
by durand
Good! I haven't had any feedback previously about liballoc but I hope it was easy to implement, clear and stuff. I've tried to make it so...

Re:could anyone please give me a "plug-in" malloc

Posted: Sat Jun 03, 2006 5:03 pm
by earlz
yes very easy to implement

Re:could anyone please give me a "plug-in" malloc

Posted: Sat Jun 03, 2006 6:06 pm
by Warrior
Wow that's a pretty looking allocator, I'll try to implement it into my OS tomorrow. I'll post results. (Although don't know what good they'll be, not like I have a firefox just lying around in my OS)

Re:could anyone please give me a "plug-in" malloc

Posted: Sun Jun 04, 2006 2:49 am
by mystran
Durand: your liballoc page says you don't know why people want 4 byte aligned allocations?

Well, they want that because it gives you 2 bits in each pointer for all kinds of h4x0ring (like dynamic typing), and because 4 byte aligned pointers are faster anyway. There are probably a few more reasons that I forgot.

Re:could anyone please give me a "plug-in" malloc

Posted: Sun Jun 04, 2006 3:26 am
by durand
Thanks, Mystran. I had to search the gtk source code to find out that it required aligned memory. It just seemed a little odd to me because running an application based on gtk then required a malloc/free implementation that knew to return aligned pointers and I wasn't aware that the standard ones generally did...

Re:could anyone please give me a "plug-in" malloc

Posted: Sun Jun 04, 2006 8:21 pm
by mystran
Actually, malloc aligning to only 4 bytes can be a pain. You take hit on performance if your doubles aren't aligned to 8 bytes. But that's not even bad. You need to use special unaligned moves if you load SSE registers from unaligned locations, and the alignment requirement is 16 bytes. Guess what? The unaligned move is so slow that you actually lose most of the benefits of SSE.

Technically, I believe malloc is supposed to return "suitably aligned" pointers, and whether or not you consider performance hits to be unsuitable, is a matter of opinion.

Ofcourse most of the time one relies on certain alignment, one should use something like posix_memalign instead. But malloc() exists everywhere...

Re:could anyone please give me a "plug-in" malloc

Posted: Sun Jun 04, 2006 8:23 pm
by mystran
btw, posix_memalign() manual on my Linux box says:

Code: Select all

       GNU libc malloc() always returns 8-byte aligned memory addresses, so these
       routines are only needed if you require larger alignment values.