jemalloc_helper.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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__)
  7. // glibc's `posix_memalign()` declaration specifies `throw()` 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 `throw()` when built with clang.
  16. #include <mm_malloc.h>
  17. #endif
  18. #ifdef ROCKSDB_JEMALLOC
  19. #ifdef __FreeBSD__
  20. #include <malloc_np.h>
  21. #else
  22. #define JEMALLOC_MANGLE
  23. #include <jemalloc/jemalloc.h>
  24. #endif
  25. #ifndef JEMALLOC_CXX_THROW
  26. #define JEMALLOC_CXX_THROW
  27. #endif
  28. #if defined(OS_WIN) && defined(_MSC_VER)
  29. // MSVC does not have weak symbol support. As long as ROCKSDB_JEMALLOC is
  30. // defined, Jemalloc memory allocator is used.
  31. static inline bool HasJemalloc() { return true; }
  32. #else
  33. // Declare non-standard jemalloc APIs as weak symbols. We can null-check these
  34. // symbols to detect whether jemalloc is linked with the binary.
  35. extern "C" void* mallocx(size_t, int) __attribute__((__weak__));
  36. extern "C" void* rallocx(void*, size_t, int) __attribute__((__weak__));
  37. extern "C" size_t xallocx(void*, size_t, size_t, int) __attribute__((__weak__));
  38. extern "C" size_t sallocx(const void*, int) __attribute__((__weak__));
  39. extern "C" void dallocx(void*, int) __attribute__((__weak__));
  40. extern "C" void sdallocx(void*, size_t, int) __attribute__((__weak__));
  41. extern "C" size_t nallocx(size_t, int) __attribute__((__weak__));
  42. extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
  43. __attribute__((__weak__));
  44. extern "C" int mallctlnametomib(const char*, size_t*, size_t*)
  45. __attribute__((__weak__));
  46. extern "C" int mallctlbymib(const size_t*, size_t, void*, size_t*, void*,
  47. size_t) __attribute__((__weak__));
  48. extern "C" void malloc_stats_print(void (*)(void*, const char*), void*,
  49. const char*) __attribute__((__weak__));
  50. extern "C" size_t malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void*)
  51. JEMALLOC_CXX_THROW __attribute__((__weak__));
  52. // Check if Jemalloc is linked with the binary. Note the main program might be
  53. // using a different memory allocator even this method return true.
  54. // It is loosely based on folly::usingJEMalloc(), minus the check that actually
  55. // allocate memory and see if it is through jemalloc, to handle the dlopen()
  56. // case:
  57. // https://github.com/facebook/folly/blob/76cf8b5841fb33137cfbf8b224f0226437c855bc/folly/memory/Malloc.h#L147
  58. static inline bool HasJemalloc() {
  59. return mallocx != nullptr && rallocx != nullptr && xallocx != nullptr &&
  60. sallocx != nullptr && dallocx != nullptr && sdallocx != nullptr &&
  61. nallocx != nullptr && mallctl != nullptr &&
  62. mallctlnametomib != nullptr && mallctlbymib != nullptr &&
  63. malloc_stats_print != nullptr && malloc_usable_size != nullptr;
  64. }
  65. #endif
  66. #endif // ROCKSDB_JEMALLOC