TCMalloc overrides all allocation/deallocation function calls, including all variants of New/Delete, and C API (malloc/free/calloc/realloc/valloc/pvalloc/mem_aligned/malloc_usable_size)
For gcc based platforms, it achieves the override using alias directive.
I'm using lots of C++ new etc, so just #defining malloc to tcmalloc won't work
In TCMalloc headers, malloc is already aliased to tc_malloc, so this has no effect. For example:
#define ALIAS(tc_fn) __attribute__ ((alias (#tc_fn), used))
void* malloc(size_t size) __THROW ALIAS(tc_malloc)
As for New, please note that unlike glibc and other implementation of New (windows), that just wrapps malloc, tcmalloc's New does not call malloc.
TCMalloc New is aliased to tc_new and tc_newarray, which will call TCMalloc's "magical" memory manager, and in some cases, as libc malloc, will initiate a sbrk/brk system call.
Another thing you need to do is make sure gcc does not link with libc's malloc variants. For this, please add to C++ Flags in the Makefile the following:
-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free
Also, don't forget to specify TCMalloc static library:
LIB_TCMALLOC = $(TCMALLOC_LIB_DIR)/libtcmalloc_minimal.a
LIB_DIR := .... -L$(TCMALLOC_LIB_DIR) ...
LIBS := ... -static $(LIB_TCMALLOC) ...
Possibly I have to use malloc_hook myself, but I would have thought I could get tcmalloc to do it for me, since it clearly is doing it when linking dynamically
TCMalloc does not use malloc_hooks, which is by now considered deprecated due to thread safety issues. It simply uses the fact that memory-allocation methods are weak symbols. It overrides those symbols either using aliases (in gcc) __attribute__((alias))
of by function calls.
Please refer to:
https://github.com/gperftools/gperftools/blob/master/README