2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // http://www.canonware.com/download/jemalloc/jemalloc-latest/doc/jemalloc.html
21 #include <folly/portability/Config.h>
23 #ifdef FOLLY_HAVE_LIBJEMALLOC
25 #include <folly/portability/SysMman.h>
26 #include <jemalloc/jemalloc.h>
28 #if (JEMALLOC_VERSION_MAJOR > 3) && defined(MADV_DONTDUMP)
29 #define FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED 1
32 #endif // FOLLY_HAVE_LIBJEMALLOC
39 * An allocator which uses Jemalloc to create an dedicated arena to allocate
40 * memory from. The only special property set on the allocated memory is that
41 * the memory is not dump-able.
43 * This is done by setting MADV_DONTDUMP using the `madvise` system call. A
44 * custom hook installed which is called when allocating a new chunk of memory.
45 * All it does is call the original jemalloc hook to allocate the memory and
46 * then set the advise on it before returning the pointer to the allocated
47 * memory. Jemalloc does not use allocated chunks across different arenas,
48 * without `munmap`-ing them first, and the advises are not sticky i.e. they are
49 * unset if `munmap` is done. Also this arena can't be used by any other part of
50 * the code by just calling `malloc`.
52 * If target system doesn't support MADV_DONTDUMP or jemalloc doesn't support
53 * custom arena hook, JemallocNodumpAllocator would fall back to using malloc /
54 * free. Such behavior can be identified by using
55 * !defined(FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED).
57 * Similarly, if binary isn't linked with jemalloc, the logic would fall back to
60 class JemallocNodumpAllocator {
67 // To be used as IOBuf::FreeFunction, userData should be set to
68 // reinterpret_cast<void*>(getFlags()).
69 static void deallocate(void* p, void* userData);
71 explicit JemallocNodumpAllocator(State state = State::ENABLED);
73 void* allocate(size_t size);
74 void* reallocate(void* p, size_t size);
75 void deallocate(void* p);
77 unsigned getArenaIndex() const { return arena_index_; }
78 int getFlags() const { return flags_; }
81 #ifdef FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
82 static chunk_alloc_t* original_chunk_alloc_;
83 static void* chunk_alloc(void* chunk,
89 #endif // FOLLY_JEMALLOC_NODUMP_ALLOCATOR_SUPPORTED
91 bool extend_and_setup_arena();
93 unsigned arena_index_{0};
98 * JemallocNodumpAllocator singleton.
100 JemallocNodumpAllocator& globalJemallocNodumpAllocator();