Possible Duplicate:
Code for malloc and free
If not, can somebody explain to me how malloc() works (what algorithms/data structures it uses). I want to compare the standard library's implementation with my own implementation of malloc().
Possible Duplicate:
Code for malloc and free
If not, can somebody explain to me how malloc() works (what algorithms/data structures it uses). I want to compare the standard library's implementation with my own implementation of malloc().
there are many implementations: http://freebsd.active-venture.com/FreeBSD-srctree/newsrc/i386/boot/cdboot/malloc.c.html
The canonical implementation is dlmalloc
:
http://g.oswego.edu/dl/html/malloc.html
Any implementation that's not pure junk uses the same basic bin algorithms, but may have things like thread-local caches, arenas, etc. that might help performance but make it a lot harder to understand, so I would just read dlmalloc
and save the rest for when you feel like making yourself suffer. As a major plus, dlmalloc
's algorithms are very well-documented; see the link I provided.
The standard library functions are implemented per platform, so the implementation for any given platform may take any of several approaches. Terms to search for include: fixed block allocators, buddy block allocators, slab allocators etc.