Commit d72b38e4 authored by Mark Santaniello's avatar Mark Santaniello Committed by Facebook Github Bot

new tcmalloc

Summary: Support the brand-new TCMalloc version that was just released.

Reviewed By: luciang

Differential Revision: D19943852

fbshipit-source-id: 10ae07051d2bc56a502ee0d5e5767035cf53794f
parent 80774861
......@@ -132,13 +132,69 @@ FOLLY_NOINLINE inline bool usingJEMalloc() noexcept {
}
#endif
inline bool getTCMallocNumericProperty(const char* name, size_t* out) noexcept {
return MallocExtension_Internal_GetNumericProperty(name, strlen(name), out);
}
#if defined(FOLLY_ASSUME_NO_TCMALLOC) || FOLLY_SANITIZE
inline bool usingTCMalloc() noexcept {
return false;
}
#elif defined(USE_TCMALLOC) && !FOLLY_SANITIZE
inline bool usingTCMalloc() noexcept {
return true;
}
#else
FOLLY_NOINLINE inline bool usingTCMalloc() noexcept {
static const bool result = []() noexcept {
// Some platforms (*cough* OSX *cough*) require weak symbol checks to be
// in the form if (mallctl != nullptr). Not if (mallctl) or if (!mallctl)
// (!!). http://goo.gl/xpmctm
if (MallocExtension_Internal_GetNumericProperty == nullptr ||
sdallocx == nullptr || nallocx == nullptr) {
return false;
}
static const char kAllocBytes[] = "generic.current_allocated_bytes";
size_t before_bytes = 0;
getTCMallocNumericProperty(kAllocBytes, &before_bytes);
static void* volatile ptr = malloc(1);
if (!ptr) {
// wtf, failing to allocate 1 byte
return false;
}
size_t after_bytes = 0;
getTCMallocNumericProperty(kAllocBytes, &after_bytes);
free(ptr);
return (before_bytes != after_bytes);
}
();
return result;
}
#endif
FOLLY_NOINLINE inline bool canSdallocx() noexcept {
static bool rv = usingJEMalloc() || usingTCMalloc();
return rv;
}
FOLLY_NOINLINE inline bool canNallocx() noexcept {
static bool rv = usingJEMalloc() || usingTCMalloc();
return rv;
}
inline size_t goodMallocSize(size_t minSize) noexcept {
if (minSize == 0) {
return 0;
}
if (!usingJEMalloc()) {
// Not using jemalloc - no smarts
if (!canNallocx()) {
// No nallocx - no smarts
return minSize;
}
......@@ -183,7 +239,7 @@ inline void* checkedRealloc(void* ptr, size_t size) {
}
inline void sizedFree(void* ptr, size_t size) {
if (usingJEMalloc()) {
if (canSdallocx()) {
sdallocx(ptr, size, 0);
} else {
free(ptr);
......
......@@ -31,6 +31,7 @@ const char* nallocxWeak = nullptr;
const char* mallctlWeak = nullptr;
const char* mallctlnametomibWeak = nullptr;
const char* mallctlbymibWeak = nullptr;
const char* MallocExtension_Internal_GetNumericPropertyWeak = nullptr;
#elif !FOLLY_HAVE_WEAK_SYMBOLS
void* (*mallocx)(size_t, int) = nullptr;
void* (*rallocx)(void*, size_t, int) = nullptr;
......@@ -43,5 +44,9 @@ int (*mallctl)(const char*, void*, size_t*, void*, size_t) = nullptr;
int (*mallctlnametomib)(const char*, size_t*, size_t*) = nullptr;
int (*mallctlbymib)(const size_t*, size_t, void*, size_t*, void*, size_t) =
nullptr;
bool (*MallocExtension_Internal_GetNumericProperty)(
const char*,
size_t,
size_t*) = nullptr;
#endif
}
......@@ -38,6 +38,8 @@ int mallctlnametomib(const char*, size_t*, size_t*)
__attribute__((__nothrow__, __weak__));
int mallctlbymib(const size_t*, size_t, void*, size_t*, void*, size_t)
__attribute__((__nothrow__, __weak__));
bool MallocExtension_Internal_GetNumericProperty(const char*, size_t, size_t*)
__attribute__((__weak__));
#endif
#else
extern void* (*mallocx)(size_t, int);
......@@ -51,6 +53,8 @@ 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);
extern bool (
*MallocExtension_Internal_GetNumericProperty)(const char*, size_t, size_t*);
#ifdef _MSC_VER
// We emulate weak linkage for MSVC. The symbols we're
// aliasing to are hiding in MallocImpl.cpp
......@@ -66,6 +70,9 @@ extern int (
#pragma comment( \
linker, "/alternatename:_mallctlnametomib=_mallctlnametomibWeak")
#pragma comment(linker, "/alternatename:_mallctlbymib=_mallctlbymibWeak")
#pragma comment( \
linker, \
"/alternatename:_MallocExtension_Internal_GetNumericProperty=_MallocExtension_Internal_GetNumericPropertyWeak")
#else
#pragma comment(linker, "/alternatename:mallocx=mallocxWeak")
#pragma comment(linker, "/alternatename:rallocx=rallocxWeak")
......@@ -77,6 +84,9 @@ extern int (
#pragma comment(linker, "/alternatename:mallctl=mallctlWeak")
#pragma comment(linker, "/alternatename:mallctlnametomib=mallctlnametomibWeak")
#pragma comment(linker, "/alternatename:mallctlbymib=mallctlbymibWeak")
#pragma comment( \
linker, \
"/alternatename:MallocExtension_Internal_GetNumericProperty=MallocExtension_Internal_GetNumericPropertyWeak")
#endif
#endif
#endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment