Fix the weak linking of JEMalloc under MSVC
authorChristopher Dykes <cdykes@fb.com>
Tue, 26 Jul 2016 22:53:34 +0000 (15:53 -0700)
committerFacebook Github Bot 1 <facebook-github-bot-1-bot@fb.com>
Tue, 26 Jul 2016 23:08:32 +0000 (16:08 -0700)
Summary: While the previous version did compile, it wouldn't actually work at link time.

Reviewed By: meyering

Differential Revision: D3614424

fbshipit-source-id: 578677b65edca720330319594e1163d214e1400d

folly/detail/Malloc.h
folly/detail/MallocImpl.cpp

index 2b658f2a55c731708bcef7292f33cc7d4c966196..46704c799e503fbc70f6eaa55b3cadcba74f413c 100644 (file)
@@ -47,6 +47,20 @@ extern int (*mallctl)(const char*, void*, size_t*, void*, size_t);
 extern int (*mallctlnametomib)(const char*, size_t*, size_t*);
 extern int (*mallctlbymib)(const size_t*, size_t, void*, size_t*, void*,
                            size_t);
+#ifdef _MSC_VER
+// We emulate weak linkage for MSVC. The symbols we're
+// aliasing to are hiding in MallocImpl.cpp
+#pragma comment(linker, "/alternatename:mallocx=mallocxWeak")
+#pragma comment(linker, "/alternatename:rallocx=rallocxWeak")
+#pragma comment(linker, "/alternatename:xallocx=xallocxWeak")
+#pragma comment(linker, "/alternatename:sallocx=sallocxWeak")
+#pragma comment(linker, "/alternatename:dallocx=dallocxWeak")
+#pragma comment(linker, "/alternatename:sdallocx=sdallocxWeak")
+#pragma comment(linker, "/alternatename:nallocx=nallocxWeak")
+#pragma comment(linker, "/alternatename:mallctl=mallctlWeak")
+#pragma comment(linker, "/alternatename:mallctlnametomib=mallctlnametomibWeak")
+#pragma comment(linker, "/alternatename:mallctlbymib=mallctlbymibWeak")
+#endif
 #endif
 
 }
index 36d70e974bae2b618462fe9149a59503bb23dd40..1fbfe530eba2ffa063ac661ffeb0a53d78a67b81 100644 (file)
@@ -20,27 +20,17 @@ extern "C" {
 
 #ifdef _MSC_VER
 // MSVC doesn't have weak symbols, so do some linker magic
-// to emulate them.
+// to emulate them. (the magic is in the header)
 const char* mallocxWeak = nullptr;
-#pragma comment(linker, "/alternatename:_mallocx=_mallocxWeak")
 const char* rallocxWeak = nullptr;
-#pragma comment(linker, "/alternatename:_rallocx=_rallocxWeak")
 const char* xallocxWeak = nullptr;
-#pragma comment(linker, "/alternatename:_xallocx=_xallocxWeak")
 const char* sallocxWeak = nullptr;
-#pragma comment(linker, "/alternatename:_sallocx=_sallocxWeak")
 const char* dallocxWeak = nullptr;
-#pragma comment(linker, "/alternatename:_dallocx=_dallocxWeak")
 const char* sdallocxWeak = nullptr;
-#pragma comment(linker, "/alternatename:_sdallocx=_sdallocxWeak")
 const char* nallocxWeak = nullptr;
-#pragma comment(linker, "/alternatename:_nallocx=_nallocxWeak")
 const char* mallctlWeak = nullptr;
-#pragma comment(linker, "/alternatename:_mallctl=_mallctlWeak")
 const char* mallctlnametomibWeak = nullptr;
-#pragma comment(linker, "/alternatename:_mallctlnametomib=_mallctlnametomibWeak")
 const char* mallctlbymibWeak = nullptr;
-#pragma comment(linker, "/alternatename:_mallctlbymib=_mallctlbymibWeak")
 #elif !FOLLY_HAVE_WEAK_SYMBOLS
 void* (*mallocx)(size_t, int) = nullptr;
 void* (*rallocx)(void*, size_t, int) = nullptr;