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?
malloc implementation
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
malloc implementation
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...
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...
Re: malloc implementation
Make one yourself? It's rather easy.AlfaOmega08 wrote:Or maybe do you know an easyer way of get malloc available on my os?
JAL
Re: malloc implementation
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.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?
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.
Re: malloc implementation
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.
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.
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
Re: malloc implementation
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
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...
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...
Re: malloc implementation
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.
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.