Voix malloc.c rev 381.
Posted: Sun Apr 29, 2007 8:32 am
I finally got around to cleaning up, fixing, and generally polishing my good (not-so-)old malloc, so it's time to post a new version here, if only because I posted an early version earlier, and the current one is orders of magnitude better.
This is not the best malloc around. But it's not the worst either. It has some nice properties, being reasonably fast and reasonably space efficient, in almost all cases detecting any heap corruptions (buffer overflows, double frees) instantly, doesn't need monopoly over heap, and needs very little library support - for normal compile, all you need is morecore()/memset()/memcpy() and a panic(). Plus it's pretty simple.
When I say reasonably efficient, we are talking about full Voix kernel build needing about 10% more time, and a running Gnome-Vim taking more or less the same amount of memory as glibc stock malloc. For thread-safety, add implementations for malloc_lock() and malloc_unlock() which are no-ops as distributed.
It does much more sanity checking than one would need to detect the most common cases of heap corruptions. It doesn't do strict overflow checking though, so in cases where malloc returns a block slightly larger than was requested, it won't notice if you write into the excess part (since that won't corrupt heap). Stricter checking could be added, but I haven't bothered.
It's first-fit which should minimize heap fragmentation in theory, especially as it always splits/coalesces where possible. In practice best-fit might be preferable, but doing efficient best-fit is somewhat trickier and I wanted to avoid complexity and it performs quite fine as is. Maybe later. Realloc is still just a wrapper around malloc/free, without any attempt at trying to resize in-place, which I'll probably add next time I can be bothered to touch this code.
Why would anyone want to use this? I don't know. Maybe somebody needs a "simple" malloc for something, or just wants to see how such a thing could be written. It's reasonably simple to understand. Haven't tested it on 64-bits (don't have any available right now) but in theory it should work. If not, please tell and I'll fix it.
There's some printfs doing debug output but none of them gets compiled in unless __MALLOC_DEBUG__ is defined, which is only really useful when working with the malloc code. The panics on the other hand should be left there, since it's game-over if panic() is ever called (well maybe not, double-free's could be ignored safely, but I like to panic early).
Finally, if somebody wants to use it, but the license (which is MIT) is unacceptable, please tell, and we'll think of something.
This is not the best malloc around. But it's not the worst either. It has some nice properties, being reasonably fast and reasonably space efficient, in almost all cases detecting any heap corruptions (buffer overflows, double frees) instantly, doesn't need monopoly over heap, and needs very little library support - for normal compile, all you need is morecore()/memset()/memcpy() and a panic(). Plus it's pretty simple.
When I say reasonably efficient, we are talking about full Voix kernel build needing about 10% more time, and a running Gnome-Vim taking more or less the same amount of memory as glibc stock malloc. For thread-safety, add implementations for malloc_lock() and malloc_unlock() which are no-ops as distributed.
It does much more sanity checking than one would need to detect the most common cases of heap corruptions. It doesn't do strict overflow checking though, so in cases where malloc returns a block slightly larger than was requested, it won't notice if you write into the excess part (since that won't corrupt heap). Stricter checking could be added, but I haven't bothered.
It's first-fit which should minimize heap fragmentation in theory, especially as it always splits/coalesces where possible. In practice best-fit might be preferable, but doing efficient best-fit is somewhat trickier and I wanted to avoid complexity and it performs quite fine as is. Maybe later. Realloc is still just a wrapper around malloc/free, without any attempt at trying to resize in-place, which I'll probably add next time I can be bothered to touch this code.
Why would anyone want to use this? I don't know. Maybe somebody needs a "simple" malloc for something, or just wants to see how such a thing could be written. It's reasonably simple to understand. Haven't tested it on 64-bits (don't have any available right now) but in theory it should work. If not, please tell and I'll fix it.
There's some printfs doing debug output but none of them gets compiled in unless __MALLOC_DEBUG__ is defined, which is only really useful when working with the malloc code. The panics on the other hand should be left there, since it's game-over if panic() is ever called (well maybe not, double-free's could be ignored safely, but I like to panic early).
Finally, if somebody wants to use it, but the license (which is MIT) is unacceptable, please tell, and we'll think of something.