jemalloc_helper.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #pragma once
  6. #if defined(__clang__) && defined(__GLIBC__)
  7. // glibc's `posix_memalign()` declaration specifies `noexcept` while clang's
  8. // declaration does not. There is a hack in clang to make its re-declaration
  9. // compatible with glibc's if they are declared consecutively. That hack breaks
  10. // if yet another `posix_memalign()` declaration comes between glibc's and
  11. // clang's declarations. Include "mm_malloc.h" here ensures glibc's and clang's
  12. // declarations both come before "jemalloc.h"'s `posix_memalign()` declaration.
  13. //
  14. // This problem could also be avoided if "jemalloc.h"'s `posix_memalign()`
  15. // declaration did not specify `noexcept` when built with clang.
  16. #include <mm_malloc.h>
  17. #endif
  18. #ifdef ROCKSDB_JEMALLOC
  19. #ifdef __FreeBSD__
  20. #include <malloc_np.h>
  21. #define JEMALLOC_USABLE_SIZE_CONST const
  22. #else
  23. #define JEMALLOC_MANGLE
  24. #include <jemalloc/jemalloc.h>
  25. #endif
  26. #ifndef JEMALLOC_CXX_THROW
  27. #define JEMALLOC_CXX_THROW
  28. #endif
  29. #if defined(OS_WIN) && defined(_MSC_VER)
  30. // MSVC does not have weak symbol support. As long as ROCKSDB_JEMALLOC is
  31. // defined, Jemalloc memory allocator is used.
  32. static inline bool HasJemalloc() { return true; }
  33. #else
  34. // definitions for compatibility with older versions of jemalloc
  35. #if !defined(JEMALLOC_ALLOCATOR)
  36. #define JEMALLOC_ALLOCATOR
  37. #endif
  38. #if !defined(JEMALLOC_RESTRICT_RETURN)
  39. #define JEMALLOC_RESTRICT_RETURN
  40. #endif
  41. #if !defined(JEMALLOC_NOTHROW)
  42. #define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)
  43. #endif
  44. #if !defined(JEMALLOC_ALLOC_SIZE)
  45. #ifdef JEMALLOC_HAVE_ATTR_ALLOC_SIZE
  46. #define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))
  47. #else
  48. #define JEMALLOC_ALLOC_SIZE(s)
  49. #endif
  50. #endif
  51. // Declare non-standard jemalloc APIs as weak symbols. We can null-check these
  52. // symbols to detect whether jemalloc is linked with the binary.
  53. extern "C" JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN void JEMALLOC_NOTHROW *
  54. mallocx(size_t, int) JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1)
  55. __attribute__((__weak__));
  56. extern "C" JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN void JEMALLOC_NOTHROW *
  57. rallocx(void *, size_t, int) JEMALLOC_ALLOC_SIZE(2) __attribute__((__weak__));
  58. extern "C" size_t JEMALLOC_NOTHROW xallocx(void *, size_t, size_t, int)
  59. __attribute__((__weak__));
  60. extern "C" size_t JEMALLOC_NOTHROW sallocx(const void *, int)
  61. JEMALLOC_ATTR(pure) __attribute__((__weak__));
  62. extern "C" void JEMALLOC_NOTHROW dallocx(void *, int) __attribute__((__weak__));
  63. extern "C" void JEMALLOC_NOTHROW sdallocx(void *, size_t, int)
  64. __attribute__((__weak__));
  65. extern "C" size_t JEMALLOC_NOTHROW nallocx(size_t, int) JEMALLOC_ATTR(pure)
  66. __attribute__((__weak__));
  67. extern "C" int JEMALLOC_NOTHROW mallctl(const char *, void *, size_t *, void *,
  68. size_t) __attribute__((__weak__));
  69. extern "C" int JEMALLOC_NOTHROW mallctlnametomib(const char *, size_t *,
  70. size_t *)
  71. __attribute__((__weak__));
  72. extern "C" int JEMALLOC_NOTHROW mallctlbymib(const size_t *, size_t, void *,
  73. size_t *, void *, size_t)
  74. __attribute__((__weak__));
  75. extern "C" void JEMALLOC_NOTHROW
  76. malloc_stats_print(void (*)(void *, const char *), void *, const char *)
  77. __attribute__((__weak__));
  78. extern "C" size_t JEMALLOC_NOTHROW
  79. malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *) JEMALLOC_CXX_THROW
  80. __attribute__((__weak__));
  81. // Check if Jemalloc is linked with the binary. Note the main program might be
  82. // using a different memory allocator even this method return true.
  83. // It is loosely based on folly::usingJEMalloc(), minus the check that actually
  84. // allocate memory and see if it is through jemalloc, to handle the dlopen()
  85. // case:
  86. // https://github.com/facebook/folly/blob/76cf8b5841fb33137cfbf8b224f0226437c855bc/folly/memory/Malloc.h#L147
  87. static inline bool HasJemalloc() {
  88. return mallocx != nullptr && rallocx != nullptr && xallocx != nullptr &&
  89. sallocx != nullptr && dallocx != nullptr && sdallocx != nullptr &&
  90. nallocx != nullptr && mallctl != nullptr &&
  91. mallctlnametomib != nullptr && mallctlbymib != nullptr &&
  92. malloc_stats_print != nullptr && malloc_usable_size != nullptr;
  93. }
  94. #endif
  95. #endif // ROCKSDB_JEMALLOC