Use #if rather than #ifdef for FOLLY_HAVE_WEAK_SYMBOLS
[folly.git] / folly / Malloc.h
index 7aadfc8fc9558d38cf56eff76d9dd1e44f4a52cc..1e02b764ba3835e80effbc01da8781dd2db35e2e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 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.
@@ -26,6 +26,8 @@
 // includes and uses fbstring.
 #if defined(_GLIBCXX_USE_FB) && !defined(_LIBSTDCXX_FBSTRING)
 
+#include "folly/detail/Malloc.h"
+
 #include <string>
 namespace folly {
   using std::goodMallocSize;
@@ -41,15 +43,28 @@ namespace folly {
 
 #ifdef _LIBSTDCXX_FBSTRING
 #pragma GCC system_header
+
+/**
+ * Declare rallocm() and allocm() 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));
+
+#include <bits/functexcept.h>
 #define FOLLY_HAVE_MALLOC_H 1
 #else
-#include "folly-config.h"
+#include "folly/detail/Malloc.h"
+#include "folly/Portability.h"
 #endif
 
 // for malloc_usable_size
 // NOTE: FreeBSD 9 doesn't have malloc.h.  It's defitions
 // are found in stdlib.h.
-#ifdef FOLLY_HAVE_MALLOC_H
+#if FOLLY_HAVE_MALLOC_H
 #include <malloc.h>
 #else
 #include <stdlib.h>
@@ -63,27 +78,28 @@ namespace folly {
 #include <new>
 
 /**
- * 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));
-
-/**
- * Define the ALLOCM_SUCCESS, ALLOCM_ZERO, and ALLOCM_NO_MOVE constants
- * normally provided by jemalloc.  We define them so that we don't have to
- * include jemalloc.h, in case the program is built without jemalloc support.
+ * Define various ALLOCM_* macros normally provided by jemalloc.  We define
+ * them so that we don't have to include jemalloc.h, in case the program is
+ * built without jemalloc support.
  */
 #ifndef ALLOCM_SUCCESS
+
 #define ALLOCM_SUCCESS 0
 #define ALLOCM_ERR_OOM 1
 #define ALLOCM_ERR_NOT_MOVED 2
 
 #define ALLOCM_ZERO    64
 #define ALLOCM_NO_MOVE 128
+
+#define ALLOCM_LG_ALIGN(la) (la)
+
+#if defined(JEMALLOC_MANGLE) && defined(JEMALLOC_EXPERIMENTAL)
+#define rallocm je_rallocm
+#define allocm je_allocm
 #endif
 
+#endif /* ALLOCM_SUCCESS */
+
 #ifdef _LIBSTDCXX_FBSTRING
 namespace std _GLIBCXX_VISIBILITY(default) {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -144,19 +160,19 @@ static const size_t jemallocMinInPlaceExpandable = 4096;
  */
 inline void* checkedMalloc(size_t size) {
   void* p = malloc(size);
-  if (!p) throw std::bad_alloc();
+  if (!p) std::__throw_bad_alloc();
   return p;
 }
 
 inline void* checkedCalloc(size_t n, size_t size) {
   void* p = calloc(n, size);
-  if (!p) throw std::bad_alloc();
+  if (!p) std::__throw_bad_alloc();
   return p;
 }
 
 inline void* checkedRealloc(void* ptr, size_t size) {
   void* p = realloc(ptr, size);
-  if (!p) throw std::bad_alloc();
+  if (!p) std::__throw_bad_alloc();
   return p;
 }