unify FOLLY_SANITIZE existing uses and add a few new
[folly.git] / folly / memory / Malloc.h
index 2726c9d8f12575637966d1abb4b88c997a1c2257..c8aeaddd4573bacbf9e1f244be326f150d789eaa 100644 (file)
@@ -19,6 +19,7 @@
 
 #pragma once
 
+#include <folly/CPortability.h>
 #include <folly/portability/Config.h>
 
 /**
@@ -26,7 +27,7 @@
  * them so that we don't have to include jemalloc.h, in case the program is
  * built without jemalloc support.
  */
-#if defined(USE_JEMALLOC) || defined(FOLLY_USE_JEMALLOC)
+#if (defined(USE_JEMALLOC) || defined(FOLLY_USE_JEMALLOC)) && !FOLLY_SANITIZE
 // We have JEMalloc, so use it.
 # include <jemalloc/jemalloc.h>
 #else
@@ -144,9 +145,15 @@ namespace folly {
 #define FOLLY_MALLOC_CHECKED_MALLOC
 #endif
 
+#include <folly/CPortability.h>
 /**
  * Determine if we are using jemalloc or not.
  */
+#if defined(USE_JEMALLOC) && !FOLLY_SANITIZE
+  inline bool usingJEMalloc() noexcept {
+    return true;
+  }
+#else
 FOLLY_MALLOC_NOINLINE inline bool usingJEMalloc() noexcept {
   // Checking for rallocx != nullptr is not sufficient; we may be in a
   // dlopen()ed module that depends on libjemalloc, so rallocx is resolved, but
@@ -183,20 +190,18 @@ FOLLY_MALLOC_NOINLINE inline bool usingJEMalloc() noexcept {
 
     uint64_t origAllocated = *counter;
 
-    const void* ptr = malloc(1);
+    static const void* volatile 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);
   }();
 
   return result;
 }
+#endif
 
 inline size_t goodMallocSize(size_t minSize) noexcept {
   if (minSize == 0) {