From: Pádraig Brady Date: Fri, 15 Sep 2017 07:56:25 +0000 (-0700) Subject: fix usingJEMalloc() with LTO X-Git-Tag: v2017.09.18.00~3 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=610c11630a3cbccdc2be0a91b5a6c2f934af3f55;p=folly.git fix usingJEMalloc() with LTO Summary: LTO has better optimization which results in the malloc() being optimized away in this function. Note we don't use folly::dontOptimizeAway() because that introduces a circular dependency between the malloc and benchmark targets. Reviewed By: marksantaniello, meyering, interwq Differential Revision: D5840883 fbshipit-source-id: 5dd9b152f70b7a49f125b6d79b4dfa40f4cdac59 --- diff --git a/folly/Malloc.h b/folly/Malloc.h index 42faee41..388a1667 100644 --- a/folly/Malloc.h +++ b/folly/Malloc.h @@ -183,15 +183,15 @@ FOLLY_MALLOC_NOINLINE inline bool usingJEMalloc() noexcept { uint64_t origAllocated = *counter; - // 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 const void* ptr = malloc(1); + const void* ptr = malloc(1); if (!ptr) { // wtf, failing to allocate 1 byte return false; } + /* Avoid optimizing away the malloc. */ + asm volatile("" ::"m"(ptr) : "memory"); + return (origAllocated != *counter); }();