memory/build/mozmemory_wrap.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/memory/build/mozmemory_wrap.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,229 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef mozmemory_wrap_h
     1.9 +#define mozmemory_wrap_h
    1.10 +
    1.11 +/*
    1.12 + * This header contains #defines which tweak the names of various memory
    1.13 + * allocation functions.
    1.14 + *
    1.15 + * There are several types of functions related to memory allocation
    1.16 + * that are meant to be used publicly by the Gecko codebase:
    1.17 + *
    1.18 + * - malloc implementation functions:
    1.19 + *   - malloc
    1.20 + *   - posix_memalign
    1.21 + *   - aligned_alloc
    1.22 + *   - calloc
    1.23 + *   - realloc
    1.24 + *   - free
    1.25 + *   - memalign
    1.26 + *   - valloc
    1.27 + *   - malloc_usable_size
    1.28 + *   - malloc_good_size
    1.29 + *   Some of these functions are specific to some systems, but for
    1.30 + *   convenience, they are treated as being cross-platform, and available
    1.31 + *   as such.
    1.32 + *
    1.33 + * - duplication functions:
    1.34 + *   - strndup
    1.35 + *   - strdup
    1.36 + *   - wcsdup (Windows only)
    1.37 + *
    1.38 + * - jemalloc specific functions:
    1.39 + *   - jemalloc_stats
    1.40 + *   - jemalloc_purge_freed_pages
    1.41 + *   - jemalloc_free_dirty_pages
    1.42 + *   (these functions are native to mozjemalloc, and have compatibility
    1.43 + *   implementations for jemalloc3)
    1.44 + *
    1.45 + * These functions are all exported as part of libmozglue (see
    1.46 + * $(topsrcdir)/mozglue/build/Makefile.in), with a few implementation
    1.47 + * peculiarities:
    1.48 + *
    1.49 + * - On Windows, the malloc implementation functions are all prefixed with
    1.50 + *   "je_", the duplication functions are prefixed with "wrap_", and jemalloc
    1.51 + *   specific functions are left unprefixed. All these functions are however
    1.52 + *   aliased when exporting them, such that the resulting mozglue.dll exports
    1.53 + *   them unprefixed (see $(topsrcdir)/mozglue/build/mozglue.def.in). The
    1.54 + *   prefixed malloc implementation and duplication functions are not
    1.55 + *   exported.
    1.56 + *
    1.57 + * - On MacOSX, the system libc has a zone allocator, which allows us to
    1.58 + *   hook custom malloc implementation functions without exporting them.
    1.59 + *   The malloc implementation functions are all prefixed with "je_" and used
    1.60 + *   this way from the custom zone allocator. They are not exported.
    1.61 + *   Duplication functions are not included, since they will call the custom
    1.62 + *   zone allocator anyways. Jemalloc-specific functions are also left
    1.63 + *   unprefixed.
    1.64 + *
    1.65 + * - On Android, both malloc implementation and duplication functions are
    1.66 + *   prefixed with "__wrap_". Additionally, C++ allocation functions
    1.67 + *   (operator new/delete) are also exported and prefixed with "__wrap_".
    1.68 + *   Jemalloc specific functions are left unprefixed.
    1.69 + *
    1.70 + * - On Gonk, all functions are left unprefixed. Additionally, C++ allocation
    1.71 + *   functions (operator new/delete) are also exported and unprefixed.
    1.72 + *
    1.73 + * - On other systems (mostly Linux), all functions are left unprefixed.
    1.74 + *
    1.75 + * Only Android and Gonk add C++ allocation functions.
    1.76 + *
    1.77 + * Proper exporting of the various functions is done with the MOZ_MEMORY_API
    1.78 + * and MOZ_JEMALLOC_API macros. MOZ_MEMORY_API is meant to be used for malloc
    1.79 + * implementation and duplication functions, while MOZ_JEMALLOC_API is
    1.80 + * dedicated to jemalloc specific functions.
    1.81 + *
    1.82 + *
    1.83 + * All these functions are meant to be called with no prefix from Gecko code.
    1.84 + * In most cases, this is because that's how they are available at runtime.
    1.85 + * However, on Android, "__wrap_" prefixing is left to the build-time linker
    1.86 + * (with -Wl,--wrap), or to the mozmemory.h header for malloc_good_size and
    1.87 + * jemalloc specific functions.
    1.88 + *
    1.89 + *
    1.90 + * Within libmozglue (when MOZ_MEMORY_IMPL is defined), all the functions
    1.91 + * should be suffixed with "_impl" both for declarations and use.
    1.92 + * That is, the implementation declaration for e.g. strdup would look like:
    1.93 + *   char* strdup_impl(const char *)
    1.94 + * That implementation would call malloc by using "malloc_impl".
    1.95 + *
    1.96 + * While mozjemalloc uses these "_impl" suffixed helpers, jemalloc3, being
    1.97 + * third-party code, doesn't, but instead has an elaborate way to mangle
    1.98 + * individual functions. See under "Run jemalloc configure script" in
    1.99 + * $(topsrcdir)/configure.in.
   1.100 + *
   1.101 + *
   1.102 + * When building with replace-malloc support, the above still holds, but
   1.103 + * the malloc implementation and jemalloc specific functions are the
   1.104 + * replace-malloc functions from replace_malloc.c.
   1.105 + *
   1.106 + * The actual jemalloc/mozjemalloc implementation is prefixed with "je_".
   1.107 + *
   1.108 + * Thus, when MOZ_REPLACE_MALLOC is defined, the "_impl" suffixed macros
   1.109 + * expand to "je_" prefixed function when building mozjemalloc or
   1.110 + * jemalloc3/mozjemalloc_compat, where MOZ_JEMALLOC_IMPL is defined.
   1.111 + *
   1.112 + * In other cases, the "_impl" suffixed macros follow the original scheme,
   1.113 + * except on Windows and MacOSX, where they would expand to "je_" prefixed
   1.114 + * functions. Instead, they are left unmodified (malloc_impl expands to
   1.115 + * malloc_impl).
   1.116 + */
   1.117 +
   1.118 +#ifndef MOZ_MEMORY
   1.119 +#  error Should only include mozmemory_wrap.h when MOZ_MEMORY is set.
   1.120 +#endif
   1.121 +
   1.122 +#if defined(MOZ_JEMALLOC_IMPL) && !defined(MOZ_MEMORY_IMPL)
   1.123 +#  define MOZ_MEMORY_IMPL
   1.124 +#endif
   1.125 +#if defined(MOZ_MEMORY_IMPL) && !defined(IMPL_MFBT)
   1.126 +#  ifdef MFBT_API /* mozilla/Types.h was already included */
   1.127 +#    error mozmemory_wrap.h has to be included before mozilla/Types.h when MOZ_MEMORY_IMPL is set and IMPL_MFBT is not.
   1.128 +#  endif
   1.129 +#  define IMPL_MFBT
   1.130 +#endif
   1.131 +
   1.132 +#include "mozilla/Types.h"
   1.133 +
   1.134 +#if !defined(MOZ_NATIVE_JEMALLOC)
   1.135 +#  ifdef MOZ_MEMORY_IMPL
   1.136 +#    if defined(MOZ_JEMALLOC_IMPL) && defined(MOZ_REPLACE_MALLOC)
   1.137 +#      define mozmem_malloc_impl(a)     je_ ## a
   1.138 +#      define mozmem_jemalloc_impl(a)   je_ ## a
   1.139 +#    else
   1.140 +#      define MOZ_JEMALLOC_API MFBT_API
   1.141 +#      ifdef MOZ_REPLACE_JEMALLOC
   1.142 +#        define MOZ_MEMORY_API MFBT_API
   1.143 +#        define mozmem_malloc_impl(a)     replace_ ## a
   1.144 +#        define mozmem_jemalloc_impl(a)   replace_ ## a
   1.145 +#      elif (defined(XP_WIN) || defined(XP_DARWIN))
   1.146 +#        if defined(MOZ_REPLACE_MALLOC)
   1.147 +#          define mozmem_malloc_impl(a)   a ## _impl
   1.148 +#        else
   1.149 +#          define mozmem_malloc_impl(a)   je_ ## a
   1.150 +#        endif
   1.151 +#      else
   1.152 +#        define MOZ_MEMORY_API MFBT_API
   1.153 +#        if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GONK)
   1.154 +#          define MOZ_WRAP_NEW_DELETE
   1.155 +#        endif
   1.156 +#      endif
   1.157 +#    endif
   1.158 +#    ifdef XP_WIN
   1.159 +#      define mozmem_dup_impl(a)      wrap_ ## a
   1.160 +#    endif
   1.161 +#  endif
   1.162 +
   1.163 +#  if defined(MOZ_WIDGET_ANDROID)
   1.164 +#    ifndef mozmem_malloc_impl
   1.165 +#      define mozmem_malloc_impl(a)   __wrap_ ## a
   1.166 +#    endif
   1.167 +#    define mozmem_dup_impl(a)      __wrap_ ## a
   1.168 +#  endif
   1.169 +
   1.170 +/* All other jemalloc3 functions are prefixed with "je_", except when
   1.171 + * building against an unprefixed system jemalloc library */
   1.172 +#  define je_(a) je_ ## a
   1.173 +#else /* defined(MOZ_NATIVE_JEMALLOC) */
   1.174 +#  define je_(a) a
   1.175 +#endif
   1.176 +
   1.177 +#if !defined(MOZ_MEMORY_IMPL) || defined(MOZ_NATIVE_JEMALLOC)
   1.178 +#  define MOZ_MEMORY_API MFBT_API
   1.179 +#  define MOZ_JEMALLOC_API MFBT_API
   1.180 +#endif
   1.181 +
   1.182 +#ifndef MOZ_MEMORY_API
   1.183 +#  define MOZ_MEMORY_API
   1.184 +#endif
   1.185 +#ifndef MOZ_JEMALLOC_API
   1.186 +#  define MOZ_JEMALLOC_API
   1.187 +#endif
   1.188 +
   1.189 +#ifndef mozmem_malloc_impl
   1.190 +#  define mozmem_malloc_impl(a)   a
   1.191 +#endif
   1.192 +#ifndef mozmem_dup_impl
   1.193 +#  define mozmem_dup_impl(a)      a
   1.194 +#endif
   1.195 +#ifndef mozmem_jemalloc_impl
   1.196 +#  define mozmem_jemalloc_impl(a) a
   1.197 +#endif
   1.198 +
   1.199 +/* Malloc implementation functions */
   1.200 +#define malloc_impl              mozmem_malloc_impl(malloc)
   1.201 +#define posix_memalign_impl      mozmem_malloc_impl(posix_memalign)
   1.202 +#define aligned_alloc_impl       mozmem_malloc_impl(aligned_alloc)
   1.203 +#define calloc_impl              mozmem_malloc_impl(calloc)
   1.204 +#define realloc_impl             mozmem_malloc_impl(realloc)
   1.205 +#define free_impl                mozmem_malloc_impl(free)
   1.206 +#define memalign_impl            mozmem_malloc_impl(memalign)
   1.207 +#define valloc_impl              mozmem_malloc_impl(valloc)
   1.208 +#define malloc_usable_size_impl  mozmem_malloc_impl(malloc_usable_size)
   1.209 +#define malloc_good_size_impl    mozmem_malloc_impl(malloc_good_size)
   1.210 +
   1.211 +/* Duplication functions */
   1.212 +#define strndup_impl   mozmem_dup_impl(strndup)
   1.213 +#define strdup_impl    mozmem_dup_impl(strdup)
   1.214 +#ifdef XP_WIN
   1.215 +#  define wcsdup_impl  mozmem_dup_impl(wcsdup)
   1.216 +#endif
   1.217 +
   1.218 +/* String functions */
   1.219 +#ifdef ANDROID
   1.220 +/* Bug 801571 and Bug 879668, libstagefright uses vasprintf, causing malloc()/
   1.221 + * free() to be mismatched between bionic and mozglue implementation.
   1.222 + */
   1.223 +#define vasprintf_impl  mozmem_dup_impl(vasprintf)
   1.224 +#define asprintf_impl   mozmem_dup_impl(asprintf)
   1.225 +#endif
   1.226 +
   1.227 +/* Jemalloc specific function */
   1.228 +#define jemalloc_stats_impl              mozmem_jemalloc_impl(jemalloc_stats)
   1.229 +#define jemalloc_purge_freed_pages_impl  mozmem_jemalloc_impl(jemalloc_purge_freed_pages)
   1.230 +#define jemalloc_free_dirty_pages_impl   mozmem_jemalloc_impl(jemalloc_free_dirty_pages)
   1.231 +
   1.232 +#endif /* mozmemory_wrap_h */

mercurial