How is malloc() implemented?

How is malloc() implemented? I want to create my own version of it, but that would be much easier if I had its source code. I am using MinGW.
Get the linux kernel, then you can browse through the source code and change it so that your version is used. How is it implemented? Well, for MS that is a secret, for linux you find a description here (for any other system: use google):
http://www.ibm.com/developerworks/linux/library/l-memmod/
For more general (and easy-to-read) information on how mm works see A. Tanenbaum: "Modern Operating Systems", chapter 4.
It is fairly complicated and you'll need to download glibc, not the linux kernel if you
are using Linux.

http://rpmfind.net/linux/rpm2html/search.php?query=glibc&submit=Search+...

Look in /usr/include/malloc.h for __malloc_hook and _free_hook (probably near
the bottom). These provide ways for you to define your own malloc and free functions
without having to change the library.
Well, I pretty much doubt that you will find any monolithic kernel in which the memory management is not done in the kernel. You can write *hook* functions in glibc. But the kernel won't tell you the areas of free memory without other system calls very much like malloc, which is, as I understand it, not what nate wants.
The hooks are only intended e.g. to trace the calls of malloc and free in order to find memory-leaks etc. and call malloc() themselves. For a usable malloc "from scratch" you would have to implement paging, segmentation etc. which is, as jsmith stated, far from trivial (the memory management subsystem from my linux version is 35639 lines of ugly C code, minus some lines of comments...)
closed account (z05DSL3A)
Have a look at section 8.7 Example - A Storage Allocator (p.149) in K&R C
http://www.oberon2005.ru/paper/kr_c.pdf
Topic archived. No new replies allowed.