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 // Helper header to declare all the supported malloc functions.
8 // MALLOC_DECL arguments are:
13 #ifndef malloc_decls_h
14 # define malloc_decls_h
16 # include "mozjemalloc_types.h"
18 # define MALLOC_FUNCS_MALLOC_BASE 1
19 # define MALLOC_FUNCS_MALLOC_EXTRA 2
20 # define MALLOC_FUNCS_MALLOC \
21 (MALLOC_FUNCS_MALLOC_BASE | MALLOC_FUNCS_MALLOC_EXTRA)
22 # define MALLOC_FUNCS_JEMALLOC 4
23 # define MALLOC_FUNCS_ARENA_BASE 8
24 # define MALLOC_FUNCS_ARENA_ALLOC 16
25 # define MALLOC_FUNCS_ARENA \
26 (MALLOC_FUNCS_ARENA_BASE | MALLOC_FUNCS_ARENA_ALLOC)
27 # define MALLOC_FUNCS_ALL \
28 (MALLOC_FUNCS_MALLOC | MALLOC_FUNCS_JEMALLOC | MALLOC_FUNCS_ARENA)
30 #endif // malloc_decls_h
33 # define MALLOC_FUNCS MALLOC_FUNCS_ALL
37 // NOTHROW_MALLOC_DECL is intended for functions where the standard library
38 // declares the functions in question as `throw()`. Not all platforms
39 // consistent declare certain functions as `throw()`, though.
41 // Bionic and OS X don't seem to care about `throw()`ness.
42 # if defined(ANDROID) || defined(XP_DARWIN)
43 # undef NOTHROW_MALLOC_DECL
44 # define NOTHROW_MALLOC_DECL MALLOC_DECL
45 // Some places don't care about the distinction.
46 # elif !defined(NOTHROW_MALLOC_DECL)
47 # define NOTHROW_MALLOC_DECL MALLOC_DECL
50 # if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_BASE
51 MALLOC_DECL(malloc
, void*, size_t)
52 MALLOC_DECL(calloc
, void*, size_t, size_t)
53 MALLOC_DECL(realloc
, void*, void*, size_t)
54 NOTHROW_MALLOC_DECL(free
, void, void*)
55 NOTHROW_MALLOC_DECL(memalign
, void*, size_t, size_t)
57 # if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_EXTRA
58 NOTHROW_MALLOC_DECL(posix_memalign
, int, void**, size_t, size_t)
59 NOTHROW_MALLOC_DECL(aligned_alloc
, void*, size_t, size_t)
60 NOTHROW_MALLOC_DECL(valloc
, void*, size_t)
61 NOTHROW_MALLOC_DECL(malloc_usable_size
, size_t, usable_ptr_t
)
62 MALLOC_DECL(malloc_good_size
, size_t, size_t)
65 # if MALLOC_FUNCS & MALLOC_FUNCS_JEMALLOC
66 // The 2nd argument points to an optional array exactly
67 // jemalloc_stats_num_bins() long to be filled in (if non-null).
68 // This must only be called on the main thread.
69 MALLOC_DECL(jemalloc_stats_internal
, void, jemalloc_stats_t
*,
70 jemalloc_bin_stats_t
*)
72 // Return the size of the jemalloc_bin_stats_t array.
73 MALLOC_DECL(jemalloc_stats_num_bins
, size_t)
75 // Tell jemalloc this is the main thread. jemalloc will use this to validate
76 // that main thread only arenas are only used on the main thread.
77 MALLOC_DECL(jemalloc_set_main_thread
, void)
79 // On some operating systems (Mac), we use madvise(MADV_FREE) to hand pages
80 // back to the operating system. On Mac, the operating system doesn't take
81 // this memory back immediately; instead, the OS takes it back only when the
82 // machine is running out of physical memory.
84 // This is great from the standpoint of efficiency, but it makes measuring our
85 // actual RSS difficult, because pages which we've MADV_FREE'd shouldn't count
88 // This function explicitly purges any MADV_FREE'd pages from physical memory,
89 // causing our reported RSS match the amount of memory we're actually using.
91 // Note that this call is expensive in two ways. First, it may be slow to
92 // execute, because it may make a number of slow syscalls to free memory. This
93 // function holds the big jemalloc locks, so basically all threads are blocked
94 // while this function runs.
96 // This function is also expensive in that the next time we go to access a page
97 // which we've just explicitly decommitted, the operating system has to attach
98 // to it a physical page! If we hadn't run this function, the OS would have
101 // If MALLOC_DOUBLE_PURGE is not defined, this function does nothing.
103 // It may only be used from the main thread.
104 MALLOC_DECL(jemalloc_purge_freed_pages
, void)
106 // Free all unused dirty pages in all arenas. Calling this function will slow
107 // down subsequent allocations so it is recommended to use it only when
108 // memory needs to be reclaimed at all costs (see bug 805855). This function
109 // provides functionality similar to mallctl("arenas.purge") in jemalloc 3.
110 // It may only be used from the main thread.
111 MALLOC_DECL(jemalloc_free_dirty_pages
, void)
113 // Opt in or out of a thread local arena (bool argument is whether to opt-in
114 // (true) or out (false)).
115 MALLOC_DECL(jemalloc_thread_local_arena
, void, bool)
117 // Provide information about any allocation enclosing the given address.
118 MALLOC_DECL(jemalloc_ptr_info
, void, const void*, jemalloc_ptr_info_t
*)
121 # if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_BASE
123 // Creates a separate arena, and returns its id, valid to use with moz_arena_*
124 // functions. A helper is provided in mozmemory.h that doesn't take any
125 // arena_params_t: moz_create_arena.
126 MALLOC_DECL(moz_create_arena_with_params
, arena_id_t
, arena_params_t
*)
128 // Dispose of the given arena. Subsequent uses of the arena will crash.
129 // Passing an invalid id (inexistent or already disposed) to this function
130 // will crash. The arena must be empty prior to calling this function.
131 MALLOC_DECL(moz_dispose_arena
, void, arena_id_t
)
133 // Set the default modifier for mMaxDirty. The value is the number of shifts
134 // applied to the value. Positive value is handled as <<, negative >>.
135 // Arenas may override the default modifier.
136 MALLOC_DECL(moz_set_max_dirty_page_modifier
, void, int32_t)
140 # if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_ALLOC
141 // Same as the functions without the moz_arena_ prefix, but using arenas
142 // created with moz_create_arena.
143 // The contract, even if not enforced at runtime in some configurations,
144 // is that moz_arena_realloc and moz_arena_free will crash if the given
145 // arena doesn't own the given pointer. All functions will crash if the
146 // arena id is invalid.
147 // Although discouraged, plain realloc and free can still be used on
148 // pointers allocated with these functions. Realloc will properly keep
149 // new pointers in the same arena as the original.
150 MALLOC_DECL(moz_arena_malloc
, void*, arena_id_t
, size_t)
151 MALLOC_DECL(moz_arena_calloc
, void*, arena_id_t
, size_t, size_t)
152 MALLOC_DECL(moz_arena_realloc
, void*, arena_id_t
, void*, size_t)
153 MALLOC_DECL(moz_arena_free
, void, arena_id_t
, void*)
154 MALLOC_DECL(moz_arena_memalign
, void*, arena_id_t
, size_t, size_t)
157 #endif // MALLOC_DECL
159 #undef NOTHROW_MALLOC_DECL