Code for malloc and free
malloc()
should be in glibc.
Further Reading
Multithreading
C++ allocation tricks [Video]
Look in the appropriate release of glibc at the old release site1 or here. For example, if you are using glib 2.9, it is in this archive. Look for the file malloc/malloc.c
.
You will see that it is not a trivial piece of library code.
1 It looks like they changed the directory structure after glibc-2.9.
The POSIX interface of malloc is defined here.
If you want to find out how the C library in GNU/Linux (glibc) implements malloc
, go and get the source code from http://ftp.gnu.org/gnu/glibc/ or browse the git repository and look at the malloc/malloc.c
file.
There is also the base documentation of the Memory Allocator by Doug Lea that describes the theory of a m(emory)alloc(ator) (read this carrefully, and then decide if you really need to implement your own malloc).