]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/port/jemalloc_helper.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rocksdb / port / jemalloc_helper.h
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
6 #pragma once
7
8 #if defined(__clang__)
9 // glibc's `posix_memalign()` declaration specifies `throw()` while clang's
10 // declaration does not. There is a hack in clang to make its re-declaration
11 // compatible with glibc's if they are declared consecutively. That hack breaks
12 // if yet another `posix_memalign()` declaration comes between glibc's and
13 // clang's declarations. Include "mm_malloc.h" here ensures glibc's and clang's
14 // declarations both come before "jemalloc.h"'s `posix_memalign()` declaration.
15 //
16 // This problem could also be avoided if "jemalloc.h"'s `posix_memalign()`
17 // declaration did not specify `throw()` when built with clang.
18 #include <mm_malloc.h>
19 #endif
20
21 #ifdef ROCKSDB_JEMALLOC
22 #ifdef __FreeBSD__
23 #include <malloc_np.h>
24 #else
25 #define JEMALLOC_MANGLE
26 #include <jemalloc/jemalloc.h>
27 #endif
28
29 #ifndef JEMALLOC_CXX_THROW
30 #define JEMALLOC_CXX_THROW
31 #endif
32
33 #if defined(OS_WIN) && defined(_MSC_VER)
34
35 // MSVC does not have weak symbol support. As long as ROCKSDB_JEMALLOC is
36 // defined, Jemalloc memory allocator is used.
37 static inline bool HasJemalloc() { return true; }
38
39 #else
40
41 // Declare non-standard jemalloc APIs as weak symbols. We can null-check these
42 // symbols to detect whether jemalloc is linked with the binary.
43 extern "C" void* mallocx(size_t, int) __attribute__((__weak__));
44 extern "C" void* rallocx(void*, size_t, int) __attribute__((__weak__));
45 extern "C" size_t xallocx(void*, size_t, size_t, int) __attribute__((__weak__));
46 extern "C" size_t sallocx(const void*, int) __attribute__((__weak__));
47 extern "C" void dallocx(void*, int) __attribute__((__weak__));
48 extern "C" void sdallocx(void*, size_t, int) __attribute__((__weak__));
49 extern "C" size_t nallocx(size_t, int) __attribute__((__weak__));
50 extern "C" int mallctl(const char*, void*, size_t*, void*, size_t)
51 __attribute__((__weak__));
52 extern "C" int mallctlnametomib(const char*, size_t*, size_t*)
53 __attribute__((__weak__));
54 extern "C" int mallctlbymib(const size_t*, size_t, void*, size_t*, void*,
55 size_t) __attribute__((__weak__));
56 extern "C" void malloc_stats_print(void (*)(void*, const char*), void*,
57 const char*) __attribute__((__weak__));
58 extern "C" size_t malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void*)
59 JEMALLOC_CXX_THROW __attribute__((__weak__));
60
61 // Check if Jemalloc is linked with the binary. Note the main program might be
62 // using a different memory allocator even this method return true.
63 // It is loosely based on folly::usingJEMalloc(), minus the check that actually
64 // allocate memory and see if it is through jemalloc, to handle the dlopen()
65 // case:
66 // https://github.com/facebook/folly/blob/76cf8b5841fb33137cfbf8b224f0226437c855bc/folly/memory/Malloc.h#L147
67 static inline bool HasJemalloc() {
68 return mallocx != nullptr && rallocx != nullptr && xallocx != nullptr &&
69 sallocx != nullptr && dallocx != nullptr && sdallocx != nullptr &&
70 nallocx != nullptr && mallctl != nullptr &&
71 mallctlnametomib != nullptr && mallctlbymib != nullptr &&
72 malloc_stats_print != nullptr && malloc_usable_size != nullptr;
73 }
74
75 #endif
76
77 #endif // ROCKSDB_JEMALLOC