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/. */ 6 7 // Helper header to declare all the supported malloc functions. 8 // MALLOC_DECL arguments are: 9 // - function name 10 // - return type 11 // - argument types 12 13 #ifndef malloc_decls_h 14 # define malloc_decls_h 15 16 # include "mozjemalloc_types.h" 17 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) 29 30 #endif // malloc_decls_h 31 32 #ifndef MALLOC_FUNCS 33 # define MALLOC_FUNCS MALLOC_FUNCS_ALL 34 #endif 35 36 #ifdef MALLOC_DECL 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. 40 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 48 # endif 49 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) 56 # endif 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) 63 # endif 64 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 MALLOC_DECL(jemalloc_stats_internal, void, jemalloc_stats_t*, 69 jemalloc_bin_stats_t*) 70 71 // Return the size of the jemalloc_bin_stats_t array. 72 MALLOC_DECL(jemalloc_stats_num_bins, size_t) 73 74 // On some operating systems (Mac), we use madvise(MADV_FREE) to hand pages 75 // back to the operating system. On Mac, the operating system doesn't take 76 // this memory back immediately; instead, the OS takes it back only when the 77 // machine is running out of physical memory. 78 // 79 // This is great from the standpoint of efficiency, but it makes measuring our 80 // actual RSS difficult, because pages which we've MADV_FREE'd shouldn't count 81 // against our RSS. 82 // 83 // This function explicitly purges any MADV_FREE'd pages from physical memory, 84 // causing our reported RSS match the amount of memory we're actually using. 85 // 86 // Note that this call is expensive in two ways. First, it may be slow to 87 // execute, because it may make a number of slow syscalls to free memory. This 88 // function holds the big jemalloc locks, so basically all threads are blocked 89 // while this function runs. 90 // 91 // This function is also expensive in that the next time we go to access a page 92 // which we've just explicitly decommitted, the operating system has to attach 93 // to it a physical page! If we hadn't run this function, the OS would have 94 // less work to do. 95 // 96 // If MALLOC_DOUBLE_PURGE is not defined, this function does nothing. 97 MALLOC_DECL(jemalloc_purge_freed_pages, void) 98 99 // Free all unused dirty pages in all arenas. Calling this function will slow 100 // down subsequent allocations so it is recommended to use it only when 101 // memory needs to be reclaimed at all costs (see bug 805855). This function 102 // provides functionality similar to mallctl("arenas.purge") in jemalloc 3. 103 MALLOC_DECL(jemalloc_free_dirty_pages, void) 104 105 // Opt in or out of a thread local arena (bool argument is whether to opt-in 106 // (true) or out (false)). 107 MALLOC_DECL(jemalloc_thread_local_arena, void, bool) 108 109 // Provide information about any allocation enclosing the given address. 110 MALLOC_DECL(jemalloc_ptr_info, void, const void*, jemalloc_ptr_info_t*) 111 # endif 112 113 # if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_BASE 114 115 // Creates a separate arena, and returns its id, valid to use with moz_arena_* 116 // functions. A helper is provided in mozmemory.h that doesn't take any 117 // arena_params_t: moz_create_arena. 118 MALLOC_DECL(moz_create_arena_with_params, arena_id_t, arena_params_t*) 119 120 // Dispose of the given arena. Subsequent uses of the arena will crash. 121 // Passing an invalid id (inexistent or already disposed) to this function 122 // will crash. The arena must be empty prior to calling this function. 123 MALLOC_DECL(moz_dispose_arena, void, arena_id_t) 124 # endif 125 126 # if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_ALLOC 127 // Same as the functions without the moz_arena_ prefix, but using arenas 128 // created with moz_create_arena. 129 // The contract, even if not enforced at runtime in some configurations, 130 // is that moz_arena_realloc and moz_arena_free will crash if the given 131 // arena doesn't own the given pointer. All functions will crash if the 132 // arena id is invalid. 133 // Although discouraged, plain realloc and free can still be used on 134 // pointers allocated with these functions. Realloc will properly keep 135 // new pointers in the same arena as the original. 136 MALLOC_DECL(moz_arena_malloc, void*, arena_id_t, size_t) 137 MALLOC_DECL(moz_arena_calloc, void*, arena_id_t, size_t, size_t) 138 MALLOC_DECL(moz_arena_realloc, void*, arena_id_t, void*, size_t) 139 MALLOC_DECL(moz_arena_free, void, arena_id_t, void*) 140 MALLOC_DECL(moz_arena_memalign, void*, arena_id_t, size_t, size_t) 141 # endif 142 143 #endif // MALLOC_DECL 144 145 #undef NOTHROW_MALLOC_DECL 146 #undef MALLOC_DECL 147 #undef MALLOC_FUNCS 148