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/. */
10 #include "mozjemalloc_types.h"
11 #include "mozilla/MacroArgs.h"
15 #define MACRO_CALL(a, b) a b
16 // Can't use macros recursively, so we need another one doing the same as above.
17 #define MACRO_CALL2(a, b) a b
19 #define ARGS_HELPER(name, ...) \
20 MACRO_CALL2(MOZ_PASTE_PREFIX_AND_ARG_COUNT(name, ##__VA_ARGS__), \
23 #define TYPED_ARGS1(t1) t1 arg1
24 #define TYPED_ARGS2(t1, t2) TYPED_ARGS1(t1), t2 arg2
25 #define TYPED_ARGS3(t1, t2, t3) TYPED_ARGS2(t1, t2), t3 arg3
28 #define ARGS1(t1) arg1
29 #define ARGS2(t1, t2) ARGS1(t1), arg2
30 #define ARGS3(t1, t2, t3) ARGS2(t1, t2), arg3
34 // Generic interface exposing the whole public allocator API
35 // This facilitates the implementation of things like replace-malloc.
36 // Note: compilers are expected to be able to optimize out `this`.
38 struct Allocator
: public T
{
39 # define MALLOC_DECL(name, return_type, ...) \
40 static return_type name(__VA_ARGS__);
41 # include "malloc_decls.h"
44 // The MozJemalloc allocator
45 struct MozJemallocBase
{};
46 typedef Allocator
<MozJemallocBase
> MozJemalloc
;
48 # ifdef MOZ_REPLACE_MALLOC
49 // The replace-malloc allocator
50 struct ReplaceMallocBase
{};
51 typedef Allocator
<ReplaceMallocBase
> ReplaceMalloc
;
53 typedef ReplaceMalloc DefaultMalloc
;
55 typedef MozJemalloc DefaultMalloc
;
60 // Dummy implementation of the moz_arena_* API, falling back to a given
61 // implementation of the base allocator.
63 struct DummyArenaAllocator
{
64 static arena_id_t
moz_create_arena_with_params(arena_params_t
*) { return 0; }
66 static void moz_dispose_arena(arena_id_t
) {}
68 static void moz_set_max_dirty_page_modifier(int32_t) {}
70 #define MALLOC_DECL(name, return_type, ...) \
71 static return_type moz_arena_##name( \
72 arena_id_t, ARGS_HELPER(TYPED_ARGS, ##__VA_ARGS__)) { \
73 return T::name(ARGS_HELPER(ARGS, ##__VA_ARGS__)); \
75 #define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
76 #include "malloc_decls.h"