tcmalloc: how can I get my malloc calls overridden when compiling statically?

▼魔方 西西 提交于 2019-11-30 09:14:11

Symbols are resolved on a first match basis. You need to make sure that libtcmalloc.a is searched before libc.a by the linker. I assume that you are not explicitly linking libc.a since you do not normally need to do so. The solution is to specify -nostdlibs, and then explicitly link all necessary libraries in the order you want them to be searched. Usually something like:

-nostdlibs -llibtcmalloc -llibm -llibc -llibgcc

Another solution which may be simpler, is to link the object file(s) needed to resolve tcmalloc rather than the static library, since object files take precedence over libraries in resolving symbols.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!