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/. */
12 #include "mozjemalloc_types.h"
13 #include "mozilla/MacroArgs.h"
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__), \
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
30 #define ARGS1(t1) arg1
31 #define ARGS2(t1, t2) ARGS1(t1), arg2
32 #define ARGS3(t1, t2, t3) ARGS2(t1, t2), arg3
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
,
45 // alignment must be a power of two and a multiple of sizeof(void*)
46 if (((aAlignment
- 1) & aAlignment
) != 0 || aAlignment
< sizeof(void*)) {
50 // The 0-->1 size promotion is done in the memalign() call below
51 result
= memalign(aAlignment
, aSize
);
61 static inline void* aligned_alloc(size_t aAlignment
, size_t aSize
) {
62 if (aSize
% aAlignment
) {
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
77 // Note: compilers are expected to be able to optimize out `this`.
79 // The MozJemalloc allocator
81 # define MALLOC_DECL(name, return_type, ...) \
82 static inline return_type name(__VA_ARGS__);
83 # include "malloc_decls.h"
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
,
95 return AlignedAllocator
<memalign
>::posix_memalign(aMemPtr
, aAlignment
,
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_stats_lite(jemalloc_stats_lite_t
*);
113 static void jemalloc_ptr_info(const void*, jemalloc_ptr_info_t
*);
115 # define MALLOC_DECL(name, return_type, ...) \
116 static return_type name(__VA_ARGS__);
117 # define MALLOC_FUNCS MALLOC_FUNCS_ARENA_ALLOC
118 # include "malloc_decls.h"
122 # ifdef MOZ_REPLACE_MALLOC
123 // The replace-malloc allocator
124 struct ReplaceMalloc
{
125 # define MALLOC_DECL(name, return_type, ...) \
126 static return_type name(__VA_ARGS__);
127 # include "malloc_decls.h"
132 using CanonicalMalloc
= MozJemallocPHC
;
134 using CanonicalMalloc
= MozJemalloc
;
137 # ifdef MOZ_REPLACE_MALLOC
138 using DefaultMalloc
= ReplaceMalloc
;
140 using DefaultMalloc
= CanonicalMalloc
;
143 // Poison - write "poison" to cells upon deallocation.
144 constexpr uint8_t kAllocPoison
= 0xe5;
146 // Junk - write this junk value to freshly allocated cells.
147 constexpr uint8_t kAllocJunk
= 0xe4;
149 // Maximum size of L1 cache line. This is used to avoid cache line aliasing,
150 // so over-estimates are okay (up to a point), but under-estimates will
151 // negatively affect performance.
152 constexpr size_t kCacheLineSize
=
153 # if defined(XP_DARWIN) && defined(__aarch64__)
162 // Dummy implementation of the moz_arena_* API, falling back to a given
163 // implementation of the base allocator.
164 template <typename T
>
165 struct DummyArenaAllocator
{
166 static arena_id_t
moz_create_arena_with_params(arena_params_t
*) { return 0; }
168 static void moz_dispose_arena(arena_id_t
) {}
170 static void moz_set_max_dirty_page_modifier(int32_t) {}
172 #define MALLOC_DECL(name, return_type, ...) \
173 static return_type moz_arena_##name( \
174 arena_id_t, ARGS_HELPER(TYPED_ARGS, ##__VA_ARGS__)) { \
175 return T::name(ARGS_HELPER(ARGS, ##__VA_ARGS__)); \
177 #define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
178 #include "malloc_decls.h"