| 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_wrap_h |
| 8 |
#define mozmemory_wrap_h |
| 9 |
|
| 10 |
// This header contains #defines which tweak the names of various memory |
| 11 |
// allocation functions. |
| 12 |
// |
| 13 |
// There are several types of functions related to memory allocation |
| 14 |
// that are meant to be used publicly by the Gecko codebase: |
| 15 |
// |
| 16 |
// - malloc implementation functions: |
| 17 |
// - malloc |
| 18 |
// - posix_memalign |
| 19 |
// - aligned_alloc |
| 20 |
// - calloc |
| 21 |
// - realloc |
| 22 |
// - free |
| 23 |
// - memalign |
| 24 |
// - valloc |
| 25 |
// - malloc_usable_size |
| 26 |
// - malloc_good_size |
| 27 |
// Some of these functions are specific to some systems, but for |
| 28 |
// convenience, they are treated as being cross-platform, and available |
| 29 |
// as such. |
| 30 |
// |
| 31 |
// - duplication functions: |
| 32 |
// - strndup |
| 33 |
// - strdup |
| 34 |
// - wcsdup (Windows only) |
| 35 |
// |
| 36 |
// - jemalloc specific functions: |
| 37 |
// - jemalloc_stats |
| 38 |
// - jemalloc_stats_num_bins |
| 39 |
// - jemalloc_purge_freed_pages |
| 40 |
// - jemalloc_free_dirty_pages |
| 41 |
// - jemalloc_thread_local_arena |
| 42 |
// - jemalloc_ptr_info |
| 43 |
// (these functions are native to mozjemalloc) |
| 44 |
// |
| 45 |
// These functions are all exported as part of libmozglue (see |
| 46 |
// $(topsrcdir)/mozglue/build/Makefile.in), with a few implementation |
| 47 |
// peculiarities: |
| 48 |
// |
| 49 |
// - On Windows, the malloc implementation functions are all prefixed with |
| 50 |
// "je_", the duplication functions are prefixed with "wrap_", and jemalloc |
| 51 |
// specific functions are left unprefixed. All these functions are however |
| 52 |
// aliased when exporting them, such that the resulting mozglue.dll exports |
| 53 |
// them unprefixed (see $(topsrcdir)/mozglue/build/mozglue.def.in). The |
| 54 |
// prefixed malloc implementation and duplication functions are not |
| 55 |
// exported. |
| 56 |
// |
| 57 |
// - On MacOSX, the system libc has a zone allocator, which allows us to |
| 58 |
// hook custom malloc implementation functions without exporting them. |
| 59 |
// However, since we want things in Firefox to skip the system zone |
| 60 |
// allocator, the malloc implementation functions are all exported |
| 61 |
// unprefixed, as well as duplication functions. |
| 62 |
// Jemalloc-specific functions are also left unprefixed. |
| 63 |
// |
| 64 |
// - On Android all functions are left unprefixed. |
| 65 |
// |
| 66 |
// - On other systems (mostly Linux), all functions are left unprefixed. |
| 67 |
// |
| 68 |
// On all platforms, C++ allocation functions are also exported. |
| 69 |
// |
| 70 |
// Proper exporting of the various functions is done with the MOZ_MEMORY_API |
| 71 |
// and MOZ_JEMALLOC_API macros. MOZ_MEMORY_API is meant to be used for malloc |
| 72 |
// implementation and duplication functions, while MOZ_JEMALLOC_API is |
| 73 |
// dedicated to jemalloc specific functions. |
| 74 |
// |
| 75 |
// |
| 76 |
// All these functions are meant to be called with no prefix from Gecko code. |
| 77 |
// In most cases, this is because that's how they are available at runtime. |
| 78 |
// However, on Android, this relies on faulty.lib (the custom dynamic linker) |
| 79 |
// resolving mozglue symbols before libc symbols, which is guaranteed by the |
| 80 |
// way faulty.lib works (it respects the DT_NEEDED order, and libc always |
| 81 |
// appears after mozglue ; which we double check when building anyways) |
| 82 |
// |
| 83 |
// |
| 84 |
// Within libmozglue (when MOZ_MEMORY_IMPL is defined), all the functions |
| 85 |
// should be suffixed with "_impl" both for declarations and use. |
| 86 |
// That is, the implementation declaration for e.g. strdup would look like: |
| 87 |
// char* strdup_impl(const char *) |
| 88 |
// That implementation would call malloc by using "malloc_impl". |
| 89 |
|
| 90 |
#if defined(MOZ_MEMORY_IMPL) && !defined(IMPL_MFBT) |
| 91 |
# ifdef MFBT_API // mozilla/Types.h was already included |
| 92 |
# error mozmemory_wrap.h has to be included before mozilla/Types.h when MOZ_MEMORY_IMPL is set and IMPL_MFBT is not. |
| 93 |
# endif |
| 94 |
# define IMPL_MFBT |
| 95 |
#endif |
| 96 |
|
| 97 |
#include "mozilla/Types.h" |
| 98 |
|
| 99 |
#ifndef MOZ_EXTERN_C |
| 100 |
# ifdef __cplusplus |
| 101 |
# define MOZ_EXTERN_C extern "C" |
| 102 |
# else |
| 103 |
# define MOZ_EXTERN_C |
| 104 |
# endif |
| 105 |
#endif |
| 106 |
|
| 107 |
#ifdef MOZ_MEMORY_IMPL |
| 108 |
# define MOZ_JEMALLOC_API MOZ_EXTERN_C MFBT_API |
| 109 |
# define MOZ_JEMALLOC_API_NODISCARD MOZ_EXTERN_C [[nodiscard]] MFBT_API |
| 110 |
# if defined(XP_WIN) |
| 111 |
# define mozmem_malloc_impl(a) je_##a |
| 112 |
# else |
| 113 |
# define MOZ_MEMORY_API MOZ_EXTERN_C MFBT_API |
| 114 |
# endif |
| 115 |
#endif |
| 116 |
#ifdef XP_WIN |
| 117 |
# define mozmem_dup_impl(a) wrap_##a |
| 118 |
#endif |
| 119 |
|
| 120 |
#if !defined(MOZ_MEMORY_IMPL) |
| 121 |
# define MOZ_MEMORY_API MOZ_EXTERN_C MFBT_API |
| 122 |
# define MOZ_JEMALLOC_API MOZ_EXTERN_C MFBT_API |
| 123 |
# define MOZ_JEMALLOC_API_NODISCARD MOZ_EXTERN_C [[nodiscard]] MFBT_API |
| 124 |
#endif |
| 125 |
|
| 126 |
#ifndef MOZ_MEMORY_API |
| 127 |
# define MOZ_MEMORY_API MOZ_EXTERN_C |
| 128 |
#endif |
| 129 |
#ifndef MOZ_JEMALLOC_API |
| 130 |
# define MOZ_JEMALLOC_API MOZ_EXTERN_C |
| 131 |
# define MOZ_JEMALLOC_API_NODISCARD MOZ_EXTERN_C [[nodiscard]] |
| 132 |
#endif |
| 133 |
|
| 134 |
#ifndef mozmem_malloc_impl |
| 135 |
# define mozmem_malloc_impl(a) a |
| 136 |
#endif |
| 137 |
#ifndef mozmem_dup_impl |
| 138 |
# define mozmem_dup_impl(a) a |
| 139 |
#endif |
| 140 |
|
| 141 |
// Malloc implementation functions |
| 142 |
#define malloc_impl mozmem_malloc_impl(malloc) |
| 143 |
#define posix_memalign_impl mozmem_malloc_impl(posix_memalign) |
| 144 |
#define aligned_alloc_impl mozmem_malloc_impl(aligned_alloc) |
| 145 |
#define calloc_impl mozmem_malloc_impl(calloc) |
| 146 |
#define realloc_impl mozmem_malloc_impl(realloc) |
| 147 |
#define free_impl mozmem_malloc_impl(free) |
| 148 |
#define memalign_impl mozmem_malloc_impl(memalign) |
| 149 |
#define valloc_impl mozmem_malloc_impl(valloc) |
| 150 |
#define malloc_usable_size_impl mozmem_malloc_impl(malloc_usable_size) |
| 151 |
#define malloc_good_size_impl mozmem_malloc_impl(malloc_good_size) |
| 152 |
|
| 153 |
// Duplication functions |
| 154 |
#define strndup_impl mozmem_dup_impl(strndup) |
| 155 |
#define strdup_impl mozmem_dup_impl(strdup) |
| 156 |
#ifdef XP_WIN |
| 157 |
# define wcsdup_impl mozmem_dup_impl(wcsdup) |
| 158 |
# define _aligned_malloc_impl mozmem_dup_impl(_aligned_malloc) |
| 159 |
#endif |
| 160 |
|
| 161 |
// String functions |
| 162 |
#ifdef ANDROID |
| 163 |
// Bug 801571 and Bug 879668, libstagefright uses vasprintf, causing malloc()/ |
| 164 |
// free() to be mismatched between bionic and mozglue implementation. |
| 165 |
# define vasprintf_impl mozmem_dup_impl(vasprintf) |
| 166 |
# define asprintf_impl mozmem_dup_impl(asprintf) |
| 167 |
#endif |
| 168 |
|
| 169 |
#endif // mozmemory_wrap_h |