TCMalloc static lib in vs2010

I discovered that in MS VS 2010, it is sufficient to:
1) compile a static .lib with overridden functions (#define WIN32_OVERRIDE_ALLOCATORS in config.h). Make sure that 'C++ | Code generation' is set to MT, not MD (not multi-threaded DLL)
2) in your application: set 'C++ | Code generation' to /MT (not /MD!), then put the compiled .lib at the beginning of library list at 'Linker | Input' page.

(Sometimes setting /MT instead of /MD is not perceived by MS VS. You can easily check if it worked: preprocessor symbol '_DLL' must not be defined.)

There is no need to edit default libraries or even to ignore them.
The only patch I had to make is in tcmalloc itself: under Windows, 'getenv' method calls 'malloc' which leads to a deadlock upon TCMalloc initialization. So just don't call 'getenv' in common.cc:

@@ -49,7 +49,12 @@
static inline void InitTCMallocTransferNumObjects()
{
if (UNLIKELY(FLAGS_tcmalloc_transfer_num_objects == 0)) {
+// getenv on MSVC calls malloc which leads to deadlock or stack overflow when linking tcmalloc statically -- V.Makhnychev
+#if !defined(_WIN32) || !defined(WIN32_OVERRIDE_ALLOCATORS)
const char *envval = getenv("TCMALLOC_TRANSFER_NUM_OBJ");
+#else
+ const char *envval = NULL;
+#endif
FLAGS_tcmalloc_transfer_num_objects = !envval ? kDefaultTransferNumObjecs :
strtol(envval, NULL, 10);
}

/FORCE:MULTIPLE


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值