no bug - Correct some typos in the comments. a=typo-fix
[gecko.git] / memory / build / mozjemalloc.h
blobdf3fbc31b367dea948da07c1154600ffb4984e46
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozjemalloc_h
8 #define mozjemalloc_h
10 #include <errno.h>
12 #include "mozjemalloc_types.h"
13 #include "mozilla/MacroArgs.h"
15 // Macro helpers
17 #define MACRO_CALL(a, b) a b
18 // Can't use macros recursively, so we need another one doing the same as above.
19 #define MACRO_CALL2(a, b) a b
21 #define ARGS_HELPER(name, ...) \
22 MACRO_CALL2(MOZ_PASTE_PREFIX_AND_ARG_COUNT(name, ##__VA_ARGS__), \
23 (__VA_ARGS__))
24 #define TYPED_ARGS0()
25 #define TYPED_ARGS1(t1) t1 arg1
26 #define TYPED_ARGS2(t1, t2) TYPED_ARGS1(t1), t2 arg2
27 #define TYPED_ARGS3(t1, t2, t3) TYPED_ARGS2(t1, t2), t3 arg3
29 #define ARGS0()
30 #define ARGS1(t1) arg1
31 #define ARGS2(t1, t2) ARGS1(t1), arg2
32 #define ARGS3(t1, t2, t3) ARGS2(t1, t2), arg3
34 #ifdef MOZ_MEMORY
36 size_t GetKernelPageSize();
38 // Implement the set of alignment functions in terms of memalign.
39 template <void* (*memalign)(size_t, size_t)>
40 struct AlignedAllocator {
41 static inline int posix_memalign(void** aMemPtr, size_t aAlignment,
42 size_t aSize) {
43 void* result;
45 // alignment must be a power of two and a multiple of sizeof(void*)
46 if (((aAlignment - 1) & aAlignment) != 0 || aAlignment < sizeof(void*)) {
47 return EINVAL;
50 // The 0-->1 size promotion is done in the memalign() call below
51 result = memalign(aAlignment, aSize);
53 if (!result) {
54 return ENOMEM;
57 *aMemPtr = result;
58 return 0;
61 static inline void* aligned_alloc(size_t aAlignment, size_t aSize) {
62 if (aSize % aAlignment) {
63 return nullptr;
65 return memalign(aAlignment, aSize);
68 static inline void* valloc(size_t aSize) {
69 return memalign(GetKernelPageSize(), aSize);
73 // These classes each implement the same interface. Writing out the
74 // interface for each one rather than using inheritance makes things more
75 // explicit.
77 // Note: compilers are expected to be able to optimize out `this`.
79 // The MozJemalloc allocator
80 struct MozJemalloc {
81 # define MALLOC_DECL(name, return_type, ...) \
82 static inline return_type name(__VA_ARGS__);
83 # include "malloc_decls.h"
86 # ifdef MOZ_PHC
87 struct MozJemallocPHC : public MozJemalloc {
88 # define MALLOC_DECL(name, return_type, ...) \
89 static return_type name(__VA_ARGS__);
90 # define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
91 # include "malloc_decls.h"
93 static inline int posix_memalign(void** aMemPtr, size_t aAlignment,
94 size_t aSize) {
95 return AlignedAllocator<memalign>::posix_memalign(aMemPtr, aAlignment,
96 aSize);
99 static inline void* aligned_alloc(size_t aAlignment, size_t aSize) {
100 return AlignedAllocator<memalign>::aligned_alloc(aAlignment, aSize);
103 static inline void* valloc(size_t aSize) {
104 return AlignedAllocator<memalign>::valloc(aSize);
107 static size_t malloc_usable_size(usable_ptr_t);
109 static void jemalloc_stats_internal(jemalloc_stats_t*, jemalloc_bin_stats_t*);
111 static void jemalloc_ptr_info(const void*, jemalloc_ptr_info_t*);
113 # define MALLOC_DECL(name, return_type, ...) \
114 static return_type name(__VA_ARGS__);
115 # define MALLOC_FUNCS MALLOC_FUNCS_ARENA_ALLOC
116 # include "malloc_decls.h"
118 # endif
120 # ifdef MOZ_REPLACE_MALLOC
121 // The replace-malloc allocator
122 struct ReplaceMalloc {
123 # define MALLOC_DECL(name, return_type, ...) \
124 static return_type name(__VA_ARGS__);
125 # include "malloc_decls.h"
127 # endif
129 # ifdef MOZ_PHC
130 using CanonicalMalloc = MozJemallocPHC;
131 # else
132 using CanonicalMalloc = MozJemalloc;
133 # endif
135 # ifdef MOZ_REPLACE_MALLOC
136 using DefaultMalloc = ReplaceMalloc;
137 # else
138 using DefaultMalloc = CanonicalMalloc;
139 # endif
141 // Poison - write "poison" to cells upon deallocation.
142 constexpr uint8_t kAllocPoison = 0xe5;
144 // Junk - write this junk value to freshly allocated cells.
145 constexpr uint8_t kAllocJunk = 0xe4;
147 #endif // MOZ_MEMORY
149 // Dummy implementation of the moz_arena_* API, falling back to a given
150 // implementation of the base allocator.
151 template <typename T>
152 struct DummyArenaAllocator {
153 static arena_id_t moz_create_arena_with_params(arena_params_t*) { return 0; }
155 static void moz_dispose_arena(arena_id_t) {}
157 static void moz_set_max_dirty_page_modifier(int32_t) {}
159 #define MALLOC_DECL(name, return_type, ...) \
160 static return_type moz_arena_##name( \
161 arena_id_t, ARGS_HELPER(TYPED_ARGS, ##__VA_ARGS__)) { \
162 return T::name(ARGS_HELPER(ARGS, ##__VA_ARGS__)); \
164 #define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
165 #include "malloc_decls.h"
168 #endif