tor-browser

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

mozmemory.h (2679B)


      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 #ifndef mozmemory_h
      8 #define mozmemory_h
      9 
     10 // This header is meant to be used when the following functions are
     11 // necessary:
     12 //   - malloc_good_size (used to be called je_malloc_usable_in_advance)
     13 //   - jemalloc_stats
     14 //   - jemalloc_stats_num_bins
     15 //   - jemalloc_purge_freed_pages
     16 //   - jemalloc_free_dirty_pages
     17 //   - jemalloc_thread_local_arena
     18 //   - jemalloc_ptr_info
     19 
     20 #ifdef MALLOC_H
     21 #  include MALLOC_H
     22 #endif
     23 
     24 #include "mozmemory_wrap.h"
     25 #include "mozilla/Types.h"
     26 #include "mozjemalloc_types.h"
     27 #include "malloc_decls.h"
     28 #include "stdbool.h"
     29 
     30 #ifdef MOZ_MEMORY
     31 // On OSX, malloc/malloc.h contains the declaration for malloc_good_size,
     32 // which will call back in jemalloc, through the zone allocator so just use it.
     33 #  ifndef XP_DARWIN
     34 MOZ_MEMORY_API size_t malloc_good_size_impl(size_t size);
     35 
     36 // Note: the MOZ_GLUE_IN_PROGRAM ifdef below is there to avoid -Werror turning
     37 // the protective if into errors. MOZ_GLUE_IN_PROGRAM is what triggers MFBT_API
     38 // to use weak imports.
     39 static inline size_t _malloc_good_size(size_t size) {
     40 #    if defined(MOZ_GLUE_IN_PROGRAM) && !defined(IMPL_MFBT)
     41  if (!malloc_good_size) return size;
     42 #    endif
     43  return malloc_good_size_impl(size);
     44 }
     45 
     46 #    define malloc_good_size _malloc_good_size
     47 #  endif
     48 
     49 #  define MALLOC_DECL(name, return_type, ...) \
     50    MOZ_JEMALLOC_API return_type name(__VA_ARGS__);
     51 #  define MALLOC_FUNCS MALLOC_FUNCS_JEMALLOC
     52 #  include "malloc_decls.h"
     53 
     54 // jemalloc_stats may only be called on the main thread so that it can access
     55 // main thread only arenas.
     56 #  ifdef __cplusplus
     57 static inline void jemalloc_stats(jemalloc_stats_t* aStats,
     58                                  jemalloc_bin_stats_t* aBinStats = nullptr) {
     59  jemalloc_stats_internal(aStats, aBinStats);
     60 }
     61 #  else
     62 static inline void jemalloc_stats(jemalloc_stats_t* aStats) {
     63  jemalloc_stats_internal(aStats, NULL);
     64 }
     65 #  endif
     66 
     67 #endif  // MOZ_MEMORY
     68 
     69 #define NOTHROW_MALLOC_DECL(name, return_type, ...) \
     70  MOZ_JEMALLOC_API return_type name(__VA_ARGS__) noexcept(true);
     71 #define MALLOC_DECL(name, return_type, ...) \
     72  MOZ_JEMALLOC_API return_type name(__VA_ARGS__);
     73 #define MALLOC_FUNCS MALLOC_FUNCS_ARENA
     74 #include "malloc_decls.h"
     75 
     76 #ifdef __cplusplus
     77 #  define moz_create_arena() moz_create_arena_with_params(nullptr)
     78 #else
     79 #  define moz_create_arena() moz_create_arena_with_params(NULL)
     80 #endif
     81 
     82 #endif  // mozmemory_h