malloc implementation

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

malloc implementation

Post by AlfaOmega08 »

Hi, I'm looking for a easy implementation of malloc, to be integrated in my os.
I found a link on the resources page on the wiki which points to http://gee.cs.oswego.edu/pub/misc/malloc.c.
Do you know what are the features needed by this implementation? I cannot get it to compile.

Or maybe do you know an easyer way of get malloc available on my os?
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: malloc implementation

Post by jal »

AlfaOmega08 wrote:Or maybe do you know an easyer way of get malloc available on my os?
Make one yourself? It's rather easy.


JAL
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: malloc implementation

Post by quok »

AlfaOmega08 wrote:Hi, I'm looking for a easy implementation of malloc, to be integrated in my os.
I found a link on the resources page on the wiki which points to http://gee.cs.oswego.edu/pub/misc/malloc.c.
Do you know what are the features needed by this implementation? I cannot get it to compile.

Or maybe do you know an easyer way of get malloc available on my os?
I know it's long, but did you bother to read through the comments at the top of that code? Doug Lea's malloc is actually pretty simple to get integrated with an OS code. Read the comments, it tells you exactly what supporting code you need in your OS, as well as what all the #defines in the code do, and you'll have to change some of them to get it to compile against your OS sources.

Now that being said, Doug Lea's malloc is also pretty complex. It is by no means the fastest, smallest, or best (and I don't think there IS a malloc implementation that could possibly be all three of those), but it is quite portable. I'd still recommend coding up your own implementation however. Even a basic first fit algorithm isn't too terribly complex and it'll help you see how everything works together. After that is there, you may decide to improve upon your own code, or you may still want to use dlmalloc, but either way you'll understand how it all fits a lot better.
tharkun
Member
Member
Posts: 51
Joined: Sat Mar 21, 2009 1:29 pm
Location: Ireland

Re: malloc implementation

Post by tharkun »

Well there is an easy way to create a malloc with free.

Malloc:
They way to go about it is to create a static pointer at the start of your malloc function and then increase it until you have found the right amount of free bytes + 4. The reason for the +4 is that you put a header specifying how many bytes until the end of this variables area of memory.

Free:
With free, you use the extra four bytes that we allocated to specify how many blocks to free.

Sorry if it's hard to understand, I'm not very good at explaining things.
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: malloc implementation

Post by AlfaOmega08 »

Ok. now something is more clear. I need one of morecore, sbrk or mmap/mmunmap. What do they do? Which is better? Which is easyer to implement?

Thanks
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: malloc implementation

Post by quok »

Once again a little bit of searching would enlighten you.

The forum has a search function. Searching the forums for 'sbrk' yields some useful posts:
http://forum.osdev.org/viewtopic.php?f= ... rk#p153047
http://forum.osdev.org/viewtopic.php?f= ... hilit=sbrk
http://forum.osdev.org/viewtopic.php?f= ... hilit=sbrk

And then there's the almighty Google as well. Please also read the rules. You've broken rule #1.

Now to be a little friendlier, since several of my last posts aren't much more than "did you bother to search first."

Obviously "which is easier to implement" is quite dependent on the design of your OS's virtual memory system. I'd have to say that memcore/sbrk is easier to implement than mmap, but mmap would probably be preferred and is more flexible. Implementing both can be rather useful as well, as then you could use sbrk for smaller allocations and mmap for something rather large. I believe dlmalloc has the ability to use both of them in precisely that manner.
Post Reply