memory/build/mozmemory_wrap.h

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

mercurial