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
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);
}();