Commit 78022b6e authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by facebook-github-bot-9

Fix usingJEMalloc with Clang

Summary: Clang is too clever and in some contexts optimizes away the `malloc`, but we rely on a side-effect. Declaring the variable as static forces it to call `malloc`. We could free the pointer relying on the fact that the lambda is guaranteed to be called only once, but I feel more comfortable just leaking it (LSan won't complain).

Reviewed By: philippv

Differential Revision: D2674769

fb-gh-sync-id: 1153a3ca226c6b7aa64c453bd61b036dcbf3ffcc
parent 2fa98577
...@@ -159,12 +159,14 @@ inline bool usingJEMalloc() noexcept { ...@@ -159,12 +159,14 @@ inline bool usingJEMalloc() noexcept {
uint64_t origAllocated = *counter; uint64_t origAllocated = *counter;
void* ptr = malloc(1); // Static because otherwise clever compilers will find out that
// the ptr is not used and does not escape the scope, so they will
// just optimize away the malloc.
static void* ptr = malloc(1);
if (!ptr) { if (!ptr) {
// wtf, failing to allocate 1 byte // wtf, failing to allocate 1 byte
return false; return false;
} }
free(ptr);
return (origAllocated != *counter); return (origAllocated != *counter);
}(); }();
......
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