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)
64 # if MALLOC_FUNCS & MALLOC_FUNCS_JEMALLOC
65 MALLOC_DECL(jemalloc_stats
, void, jemalloc_stats_t
*)
67 // On some operating systems (Mac), we use madvise(MADV_FREE) to hand pages
68 // back to the operating system. On Mac, the operating system doesn't take
69 // this memory back immediately; instead, the OS takes it back only when the
70 // machine is running out of physical memory.
72 // This is great from the standpoint of efficiency, but it makes measuring our
73 // actual RSS difficult, because pages which we've MADV_FREE'd shouldn't count
76 // This function explicitly purges any MADV_FREE'd pages from physical memory,
77 // causing our reported RSS match the amount of memory we're actually using.
79 // Note that this call is expensive in two ways. First, it may be slow to
80 // execute, because it may make a number of slow syscalls to free memory. This
81 // function holds the big jemalloc locks, so basically all threads are blocked
82 // while this function runs.
84 // This function is also expensive in that the next time we go to access a page
85 // which we've just explicitly decommitted, the operating system has to attach
86 // to it a physical page! If we hadn't run this function, the OS would have
89 // If MALLOC_DOUBLE_PURGE is not defined, this function does nothing.
90 MALLOC_DECL(jemalloc_purge_freed_pages
, void)
92 // Free all unused dirty pages in all arenas. Calling this function will slow
93 // down subsequent allocations so it is recommended to use it only when
94 // memory needs to be reclaimed at all costs (see bug 805855). This function
95 // provides functionality similar to mallctl("arenas.purge") in jemalloc 3.
96 MALLOC_DECL(jemalloc_free_dirty_pages
, void)
98 // Opt in or out of a thread local arena (bool argument is whether to opt-in
99 // (true) or out (false)).
100 MALLOC_DECL(jemalloc_thread_local_arena
, void, bool)
102 // Provide information about any allocation enclosing the given address.
103 MALLOC_DECL(jemalloc_ptr_info
, void, const void*, jemalloc_ptr_info_t
*)
106 # if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_BASE
108 // Creates a separate arena, and returns its id, valid to use with moz_arena_*
109 // functions. A helper is provided in mozmemory.h that doesn't take any
110 // arena_params_t: moz_create_arena.
111 MALLOC_DECL(moz_create_arena_with_params
, arena_id_t
, arena_params_t
*)
113 // Dispose of the given arena. Subsequent uses of the arena will crash.
114 // Passing an invalid id (inexistent or already disposed) to this function
115 // will crash. The arena must be empty prior to calling this function.
116 MALLOC_DECL(moz_dispose_arena
, void, arena_id_t
)
119 # if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_ALLOC
120 // Same as the functions without the moz_arena_ prefix, but using arenas
121 // created with moz_create_arena.
122 // The contract, even if not enforced at runtime in some configurations,
123 // is that moz_arena_realloc and moz_arena_free will crash if the given
124 // arena doesn't own the given pointer. All functions will crash if the
125 // arena id is invalid.
126 // Although discouraged, plain realloc and free can still be used on
127 // pointers allocated with these functions. Realloc will properly keep
128 // new pointers in the same arena as the original.
129 MALLOC_DECL(moz_arena_malloc
, void*, arena_id_t
, size_t)
130 MALLOC_DECL(moz_arena_calloc
, void*, arena_id_t
, size_t, size_t)
131 MALLOC_DECL(moz_arena_realloc
, void*, arena_id_t
, void*, size_t)
132 MALLOC_DECL(moz_arena_free
, void, arena_id_t
, void*)
133 MALLOC_DECL(moz_arena_memalign
, void*, arena_id_t
, size_t, size_t)
136 #endif // MALLOC_DECL
138 #undef NOTHROW_MALLOC_DECL