I want to replace default malloc and add some statistics as well as leak detections and other behavious to malloc functions. I have seen some other imlementations like gperftool and jemlloc. They can replace the default malloc by linking with their static libraries. How can they do that? I would like to implement my custom malloc functions like that.
Share
You can wrap around the original malloc.
But don’t forget you must also wrap around calloc and realloc probably. And there are also less commonly used functions in the libc to allocate memory.
To wrap calloc you need to do a dirty hack because dlsym tries to allocate memory using calloc but doesn’t really need it.
and in the init function add this: