tor-browser

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

mozjemalloc_types.h (9944B)


      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 // Portions of this file were originally under the following license:
      8 //
      9 // Copyright (C) 2006-2008 Jason Evans <jasone@FreeBSD.org>.
     10 // All rights reserved.
     11 //
     12 // Redistribution and use in source and binary forms, with or without
     13 // modification, are permitted provided that the following conditions
     14 // are met:
     15 // 1. Redistributions of source code must retain the above copyright
     16 //    notice(s), this list of conditions and the following disclaimer as
     17 //    the first lines of this file unmodified other than the possible
     18 //    addition of one or more copyright notices.
     19 // 2. Redistributions in binary form must reproduce the above copyright
     20 //    notice(s), this list of conditions and the following disclaimer in
     21 //    the documentation and/or other materials provided with the
     22 //    distribution.
     23 //
     24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
     25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27 // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
     28 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     31 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     32 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     33 // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     34 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35 
     36 #ifndef _JEMALLOC_TYPES_H_
     37 #define _JEMALLOC_TYPES_H_
     38 
     39 #include <stdint.h>
     40 
     41 // grab size_t
     42 #ifdef _MSC_VER
     43 #  include <crtdefs.h>
     44 #else
     45 #  include <stddef.h>
     46 #endif
     47 #include <stdbool.h>
     48 
     49 #ifdef __cplusplus
     50 extern "C" {
     51 #endif
     52 
     53 #ifndef MALLOC_USABLE_SIZE_CONST_PTR
     54 #  define MALLOC_USABLE_SIZE_CONST_PTR const
     55 #endif
     56 
     57 typedef MALLOC_USABLE_SIZE_CONST_PTR void* usable_ptr_t;
     58 
     59 typedef size_t arena_id_t;
     60 
     61 #define ARENA_FLAG_RANDOMIZE_SMALL_MASK 0x3
     62 #define ARENA_FLAG_RANDOMIZE_SMALL_DEFAULT 0
     63 #define ARENA_FLAG_RANDOMIZE_SMALL_ENABLED 1
     64 #define ARENA_FLAG_RANDOMIZE_SMALL_DISABLED 2
     65 
     66 // Arenas are usually protected by a lock (ARENA_FLAG_THREAD_SAFE) however some
     67 // arenas are accessed by only the main thread
     68 // (ARENA_FLAG_THREAD_MAIN_THREAD_ONLY) and their locking can be skipped.
     69 #define ARENA_FLAG_THREAD_MASK 0x4
     70 #define ARENA_FLAG_THREAD_MAIN_THREAD_ONLY 0x4
     71 #define ARENA_FLAG_THREAD_SAFE 0x0
     72 
     73 typedef struct arena_params_s {
     74  size_t mMaxDirty;
     75  // Arena specific modifiers which override the value passed to
     76  // moz_set_max_dirty_page_modifier. If value > 0 is passed to that function,
     77  // and mMaxDirtyIncreaseOverride != 0, mMaxDirtyIncreaseOverride will be used
     78  // instead, and similarly if value < 0 is passed and mMaxDirtyDecreaseOverride
     79  // != 0, mMaxDirtyDecreaseOverride will be used as the modifier.
     80  int32_t mMaxDirtyIncreaseOverride;
     81  int32_t mMaxDirtyDecreaseOverride;
     82 
     83  uint32_t mFlags;
     84 
     85  // The label will be copied into fixed-size storage (currently 128 bytes)
     86  // within the arena.  It may be null for unamed arenas
     87  const char* mLabel;
     88 
     89 #ifdef __cplusplus
     90  arena_params_s()
     91      : mMaxDirty(0),
     92        mMaxDirtyIncreaseOverride(0),
     93        mMaxDirtyDecreaseOverride(0),
     94        mFlags(0),
     95        mLabel(nullptr) {}
     96 #endif
     97 } arena_params_t;
     98 
     99 // jemalloc_stats() is not a stable interface.  When using jemalloc_stats_t, be
    100 // sure that the compiled results of mozjemalloc.cpp are in sync with this
    101 // header file.
    102 typedef struct {
    103  // Run-time configuration settings.
    104  bool opt_junk;             // Fill allocated memory with kAllocJunk?
    105  bool opt_randomize_small;  // Randomization of small allocations?
    106  bool opt_zero;             // Fill allocated memory with 0x0?
    107  size_t narenas;            // Number of arenas.
    108  size_t quantum;            // Allocation quantum.
    109  size_t quantum_max;        // Max quantum-spaced allocation size.
    110  size_t quantum_wide;       // Allocation quantum (QuantuWide).
    111  size_t quantum_wide_max;   // Max quantum-wide-spaced allocation size.
    112  size_t subpage_max;        // Max subpage allocation size.
    113  size_t large_max;          // Max sub-chunksize allocation size.
    114  size_t chunksize;          // Size of each virtual memory mapping.
    115  size_t page_size;          // Size of pages.
    116  size_t dirty_max;          // Max dirty pages per arena.
    117 
    118  // Current memory usage statistics.
    119  size_t mapped;          // Bytes mapped (not necessarily committed).
    120  size_t allocated;       // Bytes allocated (committed, in use by application).
    121  size_t waste;           // Bytes committed, not in use by the
    122                          // application, and not intentionally left
    123                          // unused (i.e., not dirty).
    124  size_t pages_dirty;     // Committed, unused pages kept around as a cache.
    125  size_t pages_fresh;     // Unused pages that have never been touched.
    126  size_t pages_madvised;  // Unsed pages we told the kernel we don't need.
    127  size_t bookkeeping;     // Committed bytes used internally by the
    128                          // allocator.
    129  size_t bin_unused;      // Bytes committed to a bin but currently unused.
    130 
    131  size_t num_operations;  // The number of malloc()+free() calls.  Note that
    132                          // realloc calls
    133                          // count as 0, 1 or 2 operations depending on internal
    134                          // operations.  Which internal operations (eg in place
    135                          // or move, or different size classes) require
    136                          // different internal operations is unspecified.
    137 } jemalloc_stats_t;
    138 
    139 typedef struct {
    140  size_t size;               // The size of objects in this bin, zero if this
    141                             // bin stats array entry is unused (no more bins).
    142  size_t num_non_full_runs;  // The number of non-full runs
    143  size_t num_runs;           // The number of runs in this bin
    144  size_t bytes_unused;       // The unallocated bytes across all these bins
    145  size_t bytes_total;        // The total storage area for runs in this bin,
    146  size_t bytes_per_run;      // The number of bytes per run, including headers.
    147  size_t regions_per_run;    // The number of regions (aka cells) per run.
    148 } jemalloc_bin_stats_t;
    149 
    150 // jemalloc_stats_lite() is not a stable interface.  When using
    151 // jemalloc_stats_lite_t, be sure that the compiled results of mozjemalloc.cpp
    152 // are in sync with this header file.
    153 typedef struct {
    154  size_t allocated_bytes;
    155 
    156  // The number of malloc()+free() calls.  realloc calls count as 0, 1 or 2
    157  // operations depending on whether they do nothing, resize in-place, or move
    158  // the memory.
    159  uint64_t num_operations;
    160 } jemalloc_stats_lite_t;
    161 
    162 enum PtrInfoTag {
    163  // The pointer is not currently known to the allocator.
    164  // 'addr', 'size', and 'arenaId' are always 0.
    165  TagUnknown,
    166 
    167  // The pointer is within a live allocation.
    168  // 'addr', 'size', and 'arenaId' describe the allocation.
    169  TagLiveAlloc,
    170 
    171  // The pointer is within a small freed allocation.
    172  // 'addr', 'size', and 'arenaId' describe the allocation.
    173  TagFreedAlloc,
    174 
    175  // The pointer is within a freed page. Details about the original
    176  // allocation, including its size, are not available.
    177  // 'addr', 'size', and 'arenaId' describe the page.
    178  TagFreedPage,
    179 };
    180 
    181 // The information in jemalloc_ptr_info_t could be represented in a variety of
    182 // ways. The chosen representation has the following properties.
    183 // - The number of fields is minimized.
    184 // - The 'tag' field unambiguously defines the meaning of the subsequent fields.
    185 // Helper functions are used to group together related categories of tags.
    186 typedef struct jemalloc_ptr_info_s {
    187  enum PtrInfoTag tag;
    188  void* addr;   // meaning depends on tag; see above
    189  size_t size;  // meaning depends on tag; see above
    190 
    191 #ifdef MOZ_DEBUG
    192  arena_id_t arenaId;  // meaning depends on tag; see above
    193 #endif
    194 
    195 #ifdef __cplusplus
    196  jemalloc_ptr_info_s() = default;
    197  jemalloc_ptr_info_s(enum PtrInfoTag aTag, void* aAddr, size_t aSize,
    198                      arena_id_t aArenaId)
    199      : tag(aTag),
    200        addr(aAddr),
    201        size(aSize)
    202 #  ifdef MOZ_DEBUG
    203        ,
    204        arenaId(aArenaId)
    205 #  endif
    206  {
    207  }
    208 #endif
    209 } jemalloc_ptr_info_t;
    210 
    211 static inline bool jemalloc_ptr_is_live(jemalloc_ptr_info_t* info) {
    212  return info->tag == TagLiveAlloc;
    213 }
    214 
    215 static inline bool jemalloc_ptr_is_freed(jemalloc_ptr_info_t* info) {
    216  return info->tag == TagFreedAlloc || info->tag == TagFreedPage;
    217 }
    218 
    219 static inline bool jemalloc_ptr_is_freed_page(jemalloc_ptr_info_t* info) {
    220  return info->tag == TagFreedPage;
    221 }
    222 
    223 // The result of purging memory from a sigle arena
    224 enum ArenaPurgeResult {
    225  // The stop threshold of dirty pages was reached or all the remaining chunks
    226  // that may have dirty pages are busy.  The allocator can't always tell the
    227  // difference between these conditions.
    228  ReachedThresholdOrBusy,
    229 
    230  // There's more chunks in this arena that could be purged.
    231  NotDone,
    232 
    233  // The arena needs to be destroyed by the caller.
    234  Dying,
    235 };
    236 
    237 // The result of calling moz_may_purge_now().
    238 enum may_purge_now_result_t {
    239  // Done: No more purge requests are pending.
    240  Done,
    241 
    242  // There may be one or more arenas whose reuse grace period expired and
    243  // needs purging asap.
    244  NeedsMore,
    245 
    246  // There is at least one arena that waits either for its reuse grace to
    247  // expire or for significant reuse to happen. As we cannot foresee the
    248  // future, whatever schedules the purges should come back later to check
    249  // if we need a purge.
    250  WantsLater,
    251 };
    252 
    253 #ifdef __cplusplus
    254 }  // extern "C"
    255 #endif
    256 
    257 #endif  // _JEMALLOC_TYPES_H_