From 610c11630a3cbccdc2be0a91b5a6c2f934af3f55 Mon Sep 17 00:00:00 2001 From: =?utf8?q?P=C3=A1draig=20Brady?= Date: Fri, 15 Sep 2017 00:56:25 -0700 Subject: [PATCH] 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 --- folly/Malloc.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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); }(); -- 2.34.1