Bug 1842773 - Part 28: Enable Atomics with resizable buffers. r=sfink
[gecko.git] / memory / build / mozmemory_wrap.h
blob92d0a1176a63d64f3805f6cdaf1ceec5f4ce7979
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/. */
7 #ifndef mozmemory_wrap_h
8 #define mozmemory_wrap_h
10 // This header contains #defines which tweak the names of various memory
11 // allocation functions.
13 // There are several types of functions related to memory allocation
14 // that are meant to be used publicly by the Gecko codebase:
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.
31 // - duplication functions:
32 // - strndup
33 // - strdup
34 // - wcsdup (Windows only)
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)
45 // These functions are all exported as part of libmozglue (see
46 // $(topsrcdir)/mozglue/build/Makefile.in), with a few implementation
47 // peculiarities:
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.
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.
64 // - On Android all functions are left unprefixed.
66 // - On other systems (mostly Linux), all functions are left unprefixed.
68 // On all platforms, C++ allocation functions are also exported.
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.
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)
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".
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
97 #include "mozilla/Types.h"
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
107 #ifdef MOZ_MEMORY_IMPL
108 # define MOZ_JEMALLOC_API MOZ_EXTERN_C MFBT_API
109 # if defined(XP_WIN)
110 # define mozmem_malloc_impl(a) je_##a
111 # else
112 # define MOZ_MEMORY_API MOZ_EXTERN_C MFBT_API
113 # endif
114 #endif
115 #ifdef XP_WIN
116 # define mozmem_dup_impl(a) wrap_##a
117 #endif
119 #if !defined(MOZ_MEMORY_IMPL)
120 # define MOZ_MEMORY_API MOZ_EXTERN_C MFBT_API
121 # define MOZ_JEMALLOC_API MOZ_EXTERN_C MFBT_API
122 #endif
124 #ifndef MOZ_MEMORY_API
125 # define MOZ_MEMORY_API MOZ_EXTERN_C
126 #endif
127 #ifndef MOZ_JEMALLOC_API
128 # define MOZ_JEMALLOC_API MOZ_EXTERN_C
129 #endif
131 #ifndef mozmem_malloc_impl
132 # define mozmem_malloc_impl(a) a
133 #endif
134 #ifndef mozmem_dup_impl
135 # define mozmem_dup_impl(a) a
136 #endif
138 // Malloc implementation functions
139 #define malloc_impl mozmem_malloc_impl(malloc)
140 #define posix_memalign_impl mozmem_malloc_impl(posix_memalign)
141 #define aligned_alloc_impl mozmem_malloc_impl(aligned_alloc)
142 #define calloc_impl mozmem_malloc_impl(calloc)
143 #define realloc_impl mozmem_malloc_impl(realloc)
144 #define free_impl mozmem_malloc_impl(free)
145 #define memalign_impl mozmem_malloc_impl(memalign)
146 #define valloc_impl mozmem_malloc_impl(valloc)
147 #define malloc_usable_size_impl mozmem_malloc_impl(malloc_usable_size)
148 #define malloc_good_size_impl mozmem_malloc_impl(malloc_good_size)
150 // Duplication functions
151 #define strndup_impl mozmem_dup_impl(strndup)
152 #define strdup_impl mozmem_dup_impl(strdup)
153 #ifdef XP_WIN
154 # define wcsdup_impl mozmem_dup_impl(wcsdup)
155 # define _aligned_malloc_impl mozmem_dup_impl(_aligned_malloc)
156 #endif
158 // String functions
159 #ifdef ANDROID
160 // Bug 801571 and Bug 879668, libstagefright uses vasprintf, causing malloc()/
161 // free() to be mismatched between bionic and mozglue implementation.
162 # define vasprintf_impl mozmem_dup_impl(vasprintf)
163 # define asprintf_impl mozmem_dup_impl(asprintf)
164 #endif
166 #endif // mozmemory_wrap_h