tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

malloc_decls.h (9826B)


      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 // Some malloc operations require extra includes.  Before using this header with
     31 // MALLOC_FUNCS unset or containing MALLOC_FUNCS_JEMALLOC you must include
     32 // this header once, outside a struct/class definition and without MALLOC_DECL
     33 // set and it will include headers it may later need.
     34 #  if !defined(MALLOC_DECL) && defined(__cplusplus)
     35 #    include <functional>
     36 #    include "mozilla/Maybe.h"
     37 #  endif
     38 
     39 #endif  // malloc_decls_h
     40 
     41 #ifndef MALLOC_FUNCS
     42 #  define MALLOC_FUNCS MALLOC_FUNCS_ALL
     43 #endif
     44 
     45 #ifdef MALLOC_DECL
     46 // NOTHROW_MALLOC_DECL is intended for functions where the standard library
     47 // declares the functions in question as `throw()`.  Not all platforms
     48 // consistent declare certain functions as `throw()`, though.
     49 
     50 // Bionic and OS X don't seem to care about `throw()`ness.
     51 #  if defined(ANDROID) || defined(XP_DARWIN)
     52 #    undef NOTHROW_MALLOC_DECL
     53 #    define NOTHROW_MALLOC_DECL MALLOC_DECL
     54 // Some places don't care about the distinction.
     55 #  elif !defined(NOTHROW_MALLOC_DECL)
     56 #    define NOTHROW_MALLOC_DECL MALLOC_DECL
     57 #  endif
     58 
     59 #  if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_BASE
     60 MALLOC_DECL(malloc, void*, size_t)
     61 MALLOC_DECL(calloc, void*, size_t, size_t)
     62 MALLOC_DECL(realloc, void*, void*, size_t)
     63 NOTHROW_MALLOC_DECL(free, void, void*)
     64 NOTHROW_MALLOC_DECL(memalign, void*, size_t, size_t)
     65 #  endif
     66 #  if MALLOC_FUNCS & MALLOC_FUNCS_MALLOC_EXTRA
     67 NOTHROW_MALLOC_DECL(posix_memalign, int, void**, size_t, size_t)
     68 NOTHROW_MALLOC_DECL(aligned_alloc, void*, size_t, size_t)
     69 NOTHROW_MALLOC_DECL(valloc, void*, size_t)
     70 NOTHROW_MALLOC_DECL(malloc_usable_size, size_t, usable_ptr_t)
     71 MALLOC_DECL(malloc_good_size, size_t, size_t)
     72 #  endif
     73 
     74 #  if MALLOC_FUNCS & MALLOC_FUNCS_JEMALLOC
     75 // The 2nd argument points to an optional array exactly
     76 // jemalloc_stats_num_bins() long to be filled in (if non-null).
     77 // This must only be called on the main thread.
     78 MALLOC_DECL(jemalloc_stats_internal, void, jemalloc_stats_t*,
     79            jemalloc_bin_stats_t*)
     80 
     81 // Return the size of the jemalloc_bin_stats_t array.
     82 MALLOC_DECL(jemalloc_stats_num_bins, size_t)
     83 
     84 // Return some of the information that jemalloc_stats returns but works
     85 // off-main-thread and is faster.
     86 MALLOC_DECL(jemalloc_stats_lite, void, jemalloc_stats_lite_t*)
     87 
     88 // Tell jemalloc this is the main thread. jemalloc will use this to validate
     89 // that main thread only arenas are only used on the main thread.
     90 MALLOC_DECL(jemalloc_set_main_thread, void)
     91 
     92 // On some operating systems (Mac), we use madvise(MADV_FREE) to hand pages
     93 // back to the operating system.  On Mac, the operating system doesn't take
     94 // this memory back immediately; instead, the OS takes it back only when the
     95 // machine is running out of physical memory.
     96 //
     97 // This is great from the standpoint of efficiency, but it makes measuring our
     98 // actual RSS difficult, because pages which we've MADV_FREE'd shouldn't count
     99 // against our RSS.
    100 //
    101 // This function explicitly purges any MADV_FREE'd pages from physical memory,
    102 // causing our reported RSS match the amount of memory we're actually using.
    103 //
    104 // Note that this call is expensive in two ways.  First, it may be slow to
    105 // execute, because it may make a number of slow syscalls to free memory.  This
    106 // function holds the big jemalloc locks, so basically all threads are blocked
    107 // while this function runs.
    108 //
    109 // This function is also expensive in that the next time we go to access a page
    110 // which we've just explicitly decommitted, the operating system has to attach
    111 // to it a physical page!  If we hadn't run this function, the OS would have
    112 // less work to do.
    113 //
    114 // If MALLOC_DOUBLE_PURGE is not defined, this function does nothing.
    115 //
    116 // It may only be used from the main thread.
    117 MALLOC_DECL(jemalloc_purge_freed_pages, void)
    118 
    119 // Free all unused dirty pages in all arenas. Calling this function will slow
    120 // down subsequent allocations so it is recommended to use it only when
    121 // memory needs to be reclaimed at all costs (see bug 805855). This function
    122 // provides functionality similar to mallctl("arenas.purge") in jemalloc 3.
    123 // Note that if called on a different thread than the main thread, only arenas
    124 // that are not created with ARENA_FLAG_THREAD_MAIN_THREAD_ONLY will be purged.
    125 MALLOC_DECL(jemalloc_free_dirty_pages, void)
    126 
    127 // Set the default modifier for mMaxDirty. The value is the number of shifts
    128 // applied to the value. Positive value is handled as <<, negative >>.
    129 // Arenas may override the default modifier.
    130 MALLOC_DECL(moz_set_max_dirty_page_modifier, void, int32_t)
    131 
    132 // Enable or disable deferred purging. Returns the former state.
    133 // If enabled, jemalloc will not purge anything until either
    134 // jemalloc_free_[excess]_dirty_pages or moz_may_purge_now are called
    135 // explicitly. Disabling it may cause an immediate synchronous purge of all
    136 // arenas.
    137 // Must be called only on the main thread.
    138 // Parameters:
    139 // bool:            enable/disable
    140 MALLOC_DECL(moz_enable_deferred_purge, bool, bool)
    141 
    142 // Perform some purging.
    143 //
    144 // Returns a may_purge_now_result_t with the following meaning:
    145 // Done:       Purge has completed for all arenas.
    146 // NeedsMore:  There may be an arena that needs to be purged now.  The caller
    147 //             may call moz_may_purge_one_now again.
    148 // WantsLater: There is at least one arena that might want a purge later,
    149 //             according to aReuseGraceMS passed.  But none requesting purge
    150 //             now.
    151 //
    152 // Parameters:
    153 // aPeekOnly:     If true, it won't process any purge but just return if some is
    154 //                needed now or wanted later.
    155 // aReuseGraceMS: The time to wait after a significant re-use happened before
    156 //                purging memory in an arena.
    157 // aKeepGoing:    Used to determine if it should continue processing purge
    158 //                requests and may be used to implement a work budget.  It will
    159 //                exit if there's no more requests, if it finishes processing an
    160 //                arena or if this parameter returns false.
    161 //
    162 // The cost of calling this when there is no pending purge is: a mutex
    163 // lock/unlock and iterating the list of purges. The mutex is never held during
    164 // expensive operations.
    165 #    ifdef __cplusplus
    166 MALLOC_DECL(moz_may_purge_now, may_purge_now_result_t, bool, uint32_t,
    167            const mozilla::Maybe<std::function<bool()>>&)
    168 #    endif
    169 
    170 // Free dirty pages until the max dirty pages threshold is satisfied. Useful
    171 // after lowering the max dirty pages threshold to get RSS back to normal.
    172 // This behaves just like a synchronous purge on all arenas.
    173 // Note that if called on a different thread than the main thread, only arenas
    174 // that are not created with ARENA_FLAG_THREAD_MAIN_THREAD_ONLY will be purged.
    175 MALLOC_DECL(jemalloc_free_excess_dirty_pages, void)
    176 
    177 // Change the value of opt_randomize_small to control small allocation
    178 // randomization and maybe perform a reinitialization of the arena's PRNG.
    179 MALLOC_DECL(jemalloc_reset_small_alloc_randomization, void, bool)
    180 
    181 // Opt in or out of a thread local arena (bool argument is whether to opt-in
    182 // (true) or out (false)).
    183 MALLOC_DECL(jemalloc_thread_local_arena, void, bool)
    184 
    185 // Provide information about any allocation enclosing the given address.
    186 MALLOC_DECL(jemalloc_ptr_info, void, const void*, jemalloc_ptr_info_t*)
    187 #  endif
    188 
    189 #  if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_BASE
    190 // Creates a separate arena, and returns its id, valid to use with moz_arena_*
    191 // functions. A helper is provided in mozmemory.h that doesn't take any
    192 // arena_params_t: moz_create_arena.
    193 MALLOC_DECL(moz_create_arena_with_params, arena_id_t, arena_params_t*)
    194 
    195 // Dispose of the given arena. Subsequent uses of the arena will crash.
    196 // Passing an invalid id (inexistent or already disposed) to this function
    197 // will crash. The arena must be empty prior to calling this function.
    198 MALLOC_DECL(moz_dispose_arena, void, arena_id_t)
    199 #  endif
    200 
    201 #  if MALLOC_FUNCS & MALLOC_FUNCS_ARENA_ALLOC
    202 // Same as the functions without the moz_arena_ prefix, but using arenas
    203 // created with moz_create_arena.
    204 // The contract, even if not enforced at runtime in some configurations,
    205 // is that moz_arena_realloc and moz_arena_free will crash if the given
    206 // arena doesn't own the given pointer. All functions will crash if the
    207 // arena id is invalid.
    208 // Although discouraged, plain realloc and free can still be used on
    209 // pointers allocated with these functions. Realloc will properly keep
    210 // new pointers in the same arena as the original.
    211 MALLOC_DECL(moz_arena_malloc, void*, arena_id_t, size_t)
    212 MALLOC_DECL(moz_arena_calloc, void*, arena_id_t, size_t, size_t)
    213 MALLOC_DECL(moz_arena_realloc, void*, arena_id_t, void*, size_t)
    214 MALLOC_DECL(moz_arena_free, void, arena_id_t, void*)
    215 MALLOC_DECL(moz_arena_memalign, void*, arena_id_t, size_t, size_t)
    216 #  endif
    217 
    218 #endif  // MALLOC_DECL
    219 
    220 #undef NOTHROW_MALLOC_DECL
    221 #undef MALLOC_DECL
    222 #undef MALLOC_FUNCS