Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef mozmemory_wrap_h |
michael@0 | 6 | #define mozmemory_wrap_h |
michael@0 | 7 | |
michael@0 | 8 | /* |
michael@0 | 9 | * This header contains #defines which tweak the names of various memory |
michael@0 | 10 | * allocation functions. |
michael@0 | 11 | * |
michael@0 | 12 | * There are several types of functions related to memory allocation |
michael@0 | 13 | * that are meant to be used publicly by the Gecko codebase: |
michael@0 | 14 | * |
michael@0 | 15 | * - malloc implementation functions: |
michael@0 | 16 | * - malloc |
michael@0 | 17 | * - posix_memalign |
michael@0 | 18 | * - aligned_alloc |
michael@0 | 19 | * - calloc |
michael@0 | 20 | * - realloc |
michael@0 | 21 | * - free |
michael@0 | 22 | * - memalign |
michael@0 | 23 | * - valloc |
michael@0 | 24 | * - malloc_usable_size |
michael@0 | 25 | * - malloc_good_size |
michael@0 | 26 | * Some of these functions are specific to some systems, but for |
michael@0 | 27 | * convenience, they are treated as being cross-platform, and available |
michael@0 | 28 | * as such. |
michael@0 | 29 | * |
michael@0 | 30 | * - duplication functions: |
michael@0 | 31 | * - strndup |
michael@0 | 32 | * - strdup |
michael@0 | 33 | * - wcsdup (Windows only) |
michael@0 | 34 | * |
michael@0 | 35 | * - jemalloc specific functions: |
michael@0 | 36 | * - jemalloc_stats |
michael@0 | 37 | * - jemalloc_purge_freed_pages |
michael@0 | 38 | * - jemalloc_free_dirty_pages |
michael@0 | 39 | * (these functions are native to mozjemalloc, and have compatibility |
michael@0 | 40 | * implementations for jemalloc3) |
michael@0 | 41 | * |
michael@0 | 42 | * These functions are all exported as part of libmozglue (see |
michael@0 | 43 | * $(topsrcdir)/mozglue/build/Makefile.in), with a few implementation |
michael@0 | 44 | * peculiarities: |
michael@0 | 45 | * |
michael@0 | 46 | * - On Windows, the malloc implementation functions are all prefixed with |
michael@0 | 47 | * "je_", the duplication functions are prefixed with "wrap_", and jemalloc |
michael@0 | 48 | * specific functions are left unprefixed. All these functions are however |
michael@0 | 49 | * aliased when exporting them, such that the resulting mozglue.dll exports |
michael@0 | 50 | * them unprefixed (see $(topsrcdir)/mozglue/build/mozglue.def.in). The |
michael@0 | 51 | * prefixed malloc implementation and duplication functions are not |
michael@0 | 52 | * exported. |
michael@0 | 53 | * |
michael@0 | 54 | * - On MacOSX, the system libc has a zone allocator, which allows us to |
michael@0 | 55 | * hook custom malloc implementation functions without exporting them. |
michael@0 | 56 | * The malloc implementation functions are all prefixed with "je_" and used |
michael@0 | 57 | * this way from the custom zone allocator. They are not exported. |
michael@0 | 58 | * Duplication functions are not included, since they will call the custom |
michael@0 | 59 | * zone allocator anyways. Jemalloc-specific functions are also left |
michael@0 | 60 | * unprefixed. |
michael@0 | 61 | * |
michael@0 | 62 | * - On Android, both malloc implementation and duplication functions are |
michael@0 | 63 | * prefixed with "__wrap_". Additionally, C++ allocation functions |
michael@0 | 64 | * (operator new/delete) are also exported and prefixed with "__wrap_". |
michael@0 | 65 | * Jemalloc specific functions are left unprefixed. |
michael@0 | 66 | * |
michael@0 | 67 | * - On Gonk, all functions are left unprefixed. Additionally, C++ allocation |
michael@0 | 68 | * functions (operator new/delete) are also exported and unprefixed. |
michael@0 | 69 | * |
michael@0 | 70 | * - On other systems (mostly Linux), all functions are left unprefixed. |
michael@0 | 71 | * |
michael@0 | 72 | * Only Android and Gonk add C++ allocation functions. |
michael@0 | 73 | * |
michael@0 | 74 | * Proper exporting of the various functions is done with the MOZ_MEMORY_API |
michael@0 | 75 | * and MOZ_JEMALLOC_API macros. MOZ_MEMORY_API is meant to be used for malloc |
michael@0 | 76 | * implementation and duplication functions, while MOZ_JEMALLOC_API is |
michael@0 | 77 | * dedicated to jemalloc specific functions. |
michael@0 | 78 | * |
michael@0 | 79 | * |
michael@0 | 80 | * All these functions are meant to be called with no prefix from Gecko code. |
michael@0 | 81 | * In most cases, this is because that's how they are available at runtime. |
michael@0 | 82 | * However, on Android, "__wrap_" prefixing is left to the build-time linker |
michael@0 | 83 | * (with -Wl,--wrap), or to the mozmemory.h header for malloc_good_size and |
michael@0 | 84 | * jemalloc specific functions. |
michael@0 | 85 | * |
michael@0 | 86 | * |
michael@0 | 87 | * Within libmozglue (when MOZ_MEMORY_IMPL is defined), all the functions |
michael@0 | 88 | * should be suffixed with "_impl" both for declarations and use. |
michael@0 | 89 | * That is, the implementation declaration for e.g. strdup would look like: |
michael@0 | 90 | * char* strdup_impl(const char *) |
michael@0 | 91 | * That implementation would call malloc by using "malloc_impl". |
michael@0 | 92 | * |
michael@0 | 93 | * While mozjemalloc uses these "_impl" suffixed helpers, jemalloc3, being |
michael@0 | 94 | * third-party code, doesn't, but instead has an elaborate way to mangle |
michael@0 | 95 | * individual functions. See under "Run jemalloc configure script" in |
michael@0 | 96 | * $(topsrcdir)/configure.in. |
michael@0 | 97 | * |
michael@0 | 98 | * |
michael@0 | 99 | * When building with replace-malloc support, the above still holds, but |
michael@0 | 100 | * the malloc implementation and jemalloc specific functions are the |
michael@0 | 101 | * replace-malloc functions from replace_malloc.c. |
michael@0 | 102 | * |
michael@0 | 103 | * The actual jemalloc/mozjemalloc implementation is prefixed with "je_". |
michael@0 | 104 | * |
michael@0 | 105 | * Thus, when MOZ_REPLACE_MALLOC is defined, the "_impl" suffixed macros |
michael@0 | 106 | * expand to "je_" prefixed function when building mozjemalloc or |
michael@0 | 107 | * jemalloc3/mozjemalloc_compat, where MOZ_JEMALLOC_IMPL is defined. |
michael@0 | 108 | * |
michael@0 | 109 | * In other cases, the "_impl" suffixed macros follow the original scheme, |
michael@0 | 110 | * except on Windows and MacOSX, where they would expand to "je_" prefixed |
michael@0 | 111 | * functions. Instead, they are left unmodified (malloc_impl expands to |
michael@0 | 112 | * malloc_impl). |
michael@0 | 113 | */ |
michael@0 | 114 | |
michael@0 | 115 | #ifndef MOZ_MEMORY |
michael@0 | 116 | # error Should only include mozmemory_wrap.h when MOZ_MEMORY is set. |
michael@0 | 117 | #endif |
michael@0 | 118 | |
michael@0 | 119 | #if defined(MOZ_JEMALLOC_IMPL) && !defined(MOZ_MEMORY_IMPL) |
michael@0 | 120 | # define MOZ_MEMORY_IMPL |
michael@0 | 121 | #endif |
michael@0 | 122 | #if defined(MOZ_MEMORY_IMPL) && !defined(IMPL_MFBT) |
michael@0 | 123 | # ifdef MFBT_API /* mozilla/Types.h was already included */ |
michael@0 | 124 | # error mozmemory_wrap.h has to be included before mozilla/Types.h when MOZ_MEMORY_IMPL is set and IMPL_MFBT is not. |
michael@0 | 125 | # endif |
michael@0 | 126 | # define IMPL_MFBT |
michael@0 | 127 | #endif |
michael@0 | 128 | |
michael@0 | 129 | #include "mozilla/Types.h" |
michael@0 | 130 | |
michael@0 | 131 | #if !defined(MOZ_NATIVE_JEMALLOC) |
michael@0 | 132 | # ifdef MOZ_MEMORY_IMPL |
michael@0 | 133 | # if defined(MOZ_JEMALLOC_IMPL) && defined(MOZ_REPLACE_MALLOC) |
michael@0 | 134 | # define mozmem_malloc_impl(a) je_ ## a |
michael@0 | 135 | # define mozmem_jemalloc_impl(a) je_ ## a |
michael@0 | 136 | # else |
michael@0 | 137 | # define MOZ_JEMALLOC_API MFBT_API |
michael@0 | 138 | # ifdef MOZ_REPLACE_JEMALLOC |
michael@0 | 139 | # define MOZ_MEMORY_API MFBT_API |
michael@0 | 140 | # define mozmem_malloc_impl(a) replace_ ## a |
michael@0 | 141 | # define mozmem_jemalloc_impl(a) replace_ ## a |
michael@0 | 142 | # elif (defined(XP_WIN) || defined(XP_DARWIN)) |
michael@0 | 143 | # if defined(MOZ_REPLACE_MALLOC) |
michael@0 | 144 | # define mozmem_malloc_impl(a) a ## _impl |
michael@0 | 145 | # else |
michael@0 | 146 | # define mozmem_malloc_impl(a) je_ ## a |
michael@0 | 147 | # endif |
michael@0 | 148 | # else |
michael@0 | 149 | # define MOZ_MEMORY_API MFBT_API |
michael@0 | 150 | # if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GONK) |
michael@0 | 151 | # define MOZ_WRAP_NEW_DELETE |
michael@0 | 152 | # endif |
michael@0 | 153 | # endif |
michael@0 | 154 | # endif |
michael@0 | 155 | # ifdef XP_WIN |
michael@0 | 156 | # define mozmem_dup_impl(a) wrap_ ## a |
michael@0 | 157 | # endif |
michael@0 | 158 | # endif |
michael@0 | 159 | |
michael@0 | 160 | # if defined(MOZ_WIDGET_ANDROID) |
michael@0 | 161 | # ifndef mozmem_malloc_impl |
michael@0 | 162 | # define mozmem_malloc_impl(a) __wrap_ ## a |
michael@0 | 163 | # endif |
michael@0 | 164 | # define mozmem_dup_impl(a) __wrap_ ## a |
michael@0 | 165 | # endif |
michael@0 | 166 | |
michael@0 | 167 | /* All other jemalloc3 functions are prefixed with "je_", except when |
michael@0 | 168 | * building against an unprefixed system jemalloc library */ |
michael@0 | 169 | # define je_(a) je_ ## a |
michael@0 | 170 | #else /* defined(MOZ_NATIVE_JEMALLOC) */ |
michael@0 | 171 | # define je_(a) a |
michael@0 | 172 | #endif |
michael@0 | 173 | |
michael@0 | 174 | #if !defined(MOZ_MEMORY_IMPL) || defined(MOZ_NATIVE_JEMALLOC) |
michael@0 | 175 | # define MOZ_MEMORY_API MFBT_API |
michael@0 | 176 | # define MOZ_JEMALLOC_API MFBT_API |
michael@0 | 177 | #endif |
michael@0 | 178 | |
michael@0 | 179 | #ifndef MOZ_MEMORY_API |
michael@0 | 180 | # define MOZ_MEMORY_API |
michael@0 | 181 | #endif |
michael@0 | 182 | #ifndef MOZ_JEMALLOC_API |
michael@0 | 183 | # define MOZ_JEMALLOC_API |
michael@0 | 184 | #endif |
michael@0 | 185 | |
michael@0 | 186 | #ifndef mozmem_malloc_impl |
michael@0 | 187 | # define mozmem_malloc_impl(a) a |
michael@0 | 188 | #endif |
michael@0 | 189 | #ifndef mozmem_dup_impl |
michael@0 | 190 | # define mozmem_dup_impl(a) a |
michael@0 | 191 | #endif |
michael@0 | 192 | #ifndef mozmem_jemalloc_impl |
michael@0 | 193 | # define mozmem_jemalloc_impl(a) a |
michael@0 | 194 | #endif |
michael@0 | 195 | |
michael@0 | 196 | /* Malloc implementation functions */ |
michael@0 | 197 | #define malloc_impl mozmem_malloc_impl(malloc) |
michael@0 | 198 | #define posix_memalign_impl mozmem_malloc_impl(posix_memalign) |
michael@0 | 199 | #define aligned_alloc_impl mozmem_malloc_impl(aligned_alloc) |
michael@0 | 200 | #define calloc_impl mozmem_malloc_impl(calloc) |
michael@0 | 201 | #define realloc_impl mozmem_malloc_impl(realloc) |
michael@0 | 202 | #define free_impl mozmem_malloc_impl(free) |
michael@0 | 203 | #define memalign_impl mozmem_malloc_impl(memalign) |
michael@0 | 204 | #define valloc_impl mozmem_malloc_impl(valloc) |
michael@0 | 205 | #define malloc_usable_size_impl mozmem_malloc_impl(malloc_usable_size) |
michael@0 | 206 | #define malloc_good_size_impl mozmem_malloc_impl(malloc_good_size) |
michael@0 | 207 | |
michael@0 | 208 | /* Duplication functions */ |
michael@0 | 209 | #define strndup_impl mozmem_dup_impl(strndup) |
michael@0 | 210 | #define strdup_impl mozmem_dup_impl(strdup) |
michael@0 | 211 | #ifdef XP_WIN |
michael@0 | 212 | # define wcsdup_impl mozmem_dup_impl(wcsdup) |
michael@0 | 213 | #endif |
michael@0 | 214 | |
michael@0 | 215 | /* String functions */ |
michael@0 | 216 | #ifdef ANDROID |
michael@0 | 217 | /* Bug 801571 and Bug 879668, libstagefright uses vasprintf, causing malloc()/ |
michael@0 | 218 | * free() to be mismatched between bionic and mozglue implementation. |
michael@0 | 219 | */ |
michael@0 | 220 | #define vasprintf_impl mozmem_dup_impl(vasprintf) |
michael@0 | 221 | #define asprintf_impl mozmem_dup_impl(asprintf) |
michael@0 | 222 | #endif |
michael@0 | 223 | |
michael@0 | 224 | /* Jemalloc specific function */ |
michael@0 | 225 | #define jemalloc_stats_impl mozmem_jemalloc_impl(jemalloc_stats) |
michael@0 | 226 | #define jemalloc_purge_freed_pages_impl mozmem_jemalloc_impl(jemalloc_purge_freed_pages) |
michael@0 | 227 | #define jemalloc_free_dirty_pages_impl mozmem_jemalloc_impl(jemalloc_free_dirty_pages) |
michael@0 | 228 | |
michael@0 | 229 | #endif /* mozmemory_wrap_h */ |