Construct Later with exception_ptr
[folly.git] / folly / Malloc.h
index 8310a9aafddac67b7d2bd789701dbbd71b9183fb..59745b5fd0c47615a72a6739f40000697c59b864 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 // includes and uses fbstring.
 #if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING)
 
+#include <folly/detail/Malloc.h>
+
 #include <string>
+
 namespace folly {
   using std::goodMallocSize;
   using std::jemallocMinInPlaceExpandable;
@@ -41,11 +44,24 @@ namespace folly {
 
 #ifdef _LIBSTDCXX_FBSTRING
 #pragma GCC system_header
+
+/**
+ * Declare rallocm(), allocm(), and mallctl() as weak symbols. These will be
+ * provided by jemalloc if we are using jemalloc, or will be NULL if we are
+ * using another malloc implementation.
+ */
+extern "C" int rallocm(void**, size_t*, size_t, size_t, int)
+__attribute__((weak));
+extern "C" int allocm(void**, size_t*, size_t, int)
+__attribute__((weak));
+extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
+__attribute__((weak));
+
 #include <bits/functexcept.h>
 #define FOLLY_HAVE_MALLOC_H 1
 #else
-#include "folly/Portability.h"
-#include <stdexcept>
+#include <folly/detail/Malloc.h>
+#include <folly/Portability.h>
 #endif
 
 // for malloc_usable_size
@@ -87,16 +103,6 @@ namespace folly {
 
 #endif /* ALLOCM_SUCCESS */
 
-/**
- * Declare rallocm() and malloc_usable_size() as weak symbols.  It
- * will be provided by jemalloc if we are using jemalloc, or it will
- * be NULL if we are using another malloc implementation.
- */
-extern "C" int rallocm(void**, size_t*, size_t, size_t, int)
-__attribute__((weak));
-extern "C" int allocm(void**, size_t*, size_t, int)
-__attribute__((weak));
-
 #ifdef _LIBSTDCXX_FBSTRING
 namespace std _GLIBCXX_VISIBILITY(default) {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -104,12 +110,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 namespace folly {
 #endif
 
+bool usingJEMallocSlow();
 
 /**
  * Determine if we are using jemalloc or not.
  */
 inline bool usingJEMalloc() {
-  return rallocm != NULL;
+  // Checking for rallocm != NULL is not sufficient; we may be in a dlopen()ed
+  // module that depends on libjemalloc, so rallocm is resolved, but the main
+  // program might be using a different memory allocator. Look at the
+  // implementation of usingJEMallocSlow() for the (hacky) details.
+  static bool result = usingJEMallocSlow();
+  return result;
 }
 
 /**
@@ -157,31 +169,19 @@ static const size_t jemallocMinInPlaceExpandable = 4096;
  */
 inline void* checkedMalloc(size_t size) {
   void* p = malloc(size);
-#ifdef _LIBSTDCXX_FBSTRING
   if (!p) std::__throw_bad_alloc();
-#else
-  if (!p) throw std::bad_alloc();
-#endif
   return p;
 }
 
 inline void* checkedCalloc(size_t n, size_t size) {
   void* p = calloc(n, size);
-#ifdef _LIBSTDCXX_FBSTRING
   if (!p) std::__throw_bad_alloc();
-#else
-  if (!p) throw std::bad_alloc();
-#endif
   return p;
 }
 
 inline void* checkedRealloc(void* ptr, size_t size) {
   void* p = realloc(ptr, size);
-#ifdef _LIBSTDCXX_FBSTRING
   if (!p) std::__throw_bad_alloc();
-#else
-  if (!p) throw std::bad_alloc();
-#endif
   return p;
 }
 
@@ -209,7 +209,7 @@ inline void* smartRealloc(void* p,
     // using jemalloc's API. Don't forget that jemalloc can never grow
     // in place blocks smaller than 4096 bytes.
     if (currentCapacity >= jemallocMinInPlaceExpandable &&
-        rallocm(&p, NULL, newCapacity, 0, ALLOCM_NO_MOVE) == ALLOCM_SUCCESS) {
+        rallocm(&p, nullptr, newCapacity, 0, ALLOCM_NO_MOVE) == ALLOCM_SUCCESS) {
       // Managed to expand in place
       return p;
     }