|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: sw=4 ts=4 et : |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #ifndef mozilla_mozalloc_h |
|
9 #define mozilla_mozalloc_h |
|
10 |
|
11 /* |
|
12 * https://bugzilla.mozilla.org/show_bug.cgi?id=427099 |
|
13 */ |
|
14 |
|
15 #include <stdlib.h> |
|
16 #include <string.h> |
|
17 #if defined(__cplusplus) |
|
18 # include <new> |
|
19 #endif |
|
20 #include "xpcom-config.h" |
|
21 |
|
22 #if defined(__cplusplus) |
|
23 #include "mozilla/fallible.h" |
|
24 #endif |
|
25 #include "mozilla/Attributes.h" |
|
26 |
|
27 #define MOZALLOC_HAVE_XMALLOC |
|
28 |
|
29 #if defined(MOZALLOC_EXPORT) |
|
30 /* do nothing: it's been defined to __declspec(dllexport) by |
|
31 * mozalloc*.cpp on platforms where that's required. */ |
|
32 #elif defined(XP_WIN) |
|
33 # define MOZALLOC_EXPORT __declspec(dllimport) |
|
34 #elif defined(HAVE_VISIBILITY_ATTRIBUTE) |
|
35 /* Make sure symbols are still exported even if we're wrapped in a |
|
36 * |visibility push(hidden)| blanket. */ |
|
37 # define MOZALLOC_EXPORT __attribute__ ((visibility ("default"))) |
|
38 #else |
|
39 # define MOZALLOC_EXPORT |
|
40 #endif |
|
41 |
|
42 |
|
43 #if defined(MOZ_ALWAYS_INLINE_EVEN_DEBUG) |
|
44 # define MOZALLOC_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG |
|
45 #elif defined(HAVE_FORCEINLINE) |
|
46 # define MOZALLOC_INLINE __forceinline |
|
47 #else |
|
48 # define MOZALLOC_INLINE inline |
|
49 #endif |
|
50 |
|
51 /* Workaround build problem with Sun Studio 12 */ |
|
52 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC) |
|
53 # undef NS_WARN_UNUSED_RESULT |
|
54 # define NS_WARN_UNUSED_RESULT |
|
55 # undef NS_ATTR_MALLOC |
|
56 # define NS_ATTR_MALLOC |
|
57 #endif |
|
58 |
|
59 #if defined(__cplusplus) |
|
60 extern "C" { |
|
61 #endif /* ifdef __cplusplus */ |
|
62 |
|
63 |
|
64 /* |
|
65 * Each pair of declarations below is analogous to a "standard" |
|
66 * allocation function, except that the out-of-memory handling is made |
|
67 * explicit. The |moz_x| versions will never return a NULL pointer; |
|
68 * if memory is exhausted, they abort. The |moz_| versions may return |
|
69 * NULL pointers if memory is exhausted: their return value must be |
|
70 * checked. |
|
71 * |
|
72 * All these allocation functions are *guaranteed* to return a pointer |
|
73 * to memory allocated in such a way that that memory can be freed by |
|
74 * passing that pointer to |moz_free()|. |
|
75 */ |
|
76 |
|
77 MOZALLOC_EXPORT |
|
78 void moz_free(void* ptr); |
|
79 |
|
80 MOZALLOC_EXPORT void* moz_xmalloc(size_t size) |
|
81 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
82 |
|
83 MOZALLOC_EXPORT |
|
84 void* moz_malloc(size_t size) |
|
85 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
86 |
|
87 |
|
88 MOZALLOC_EXPORT void* moz_xcalloc(size_t nmemb, size_t size) |
|
89 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
90 |
|
91 MOZALLOC_EXPORT void* moz_calloc(size_t nmemb, size_t size) |
|
92 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
93 |
|
94 |
|
95 MOZALLOC_EXPORT void* moz_xrealloc(void* ptr, size_t size) |
|
96 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
97 |
|
98 MOZALLOC_EXPORT void* moz_realloc(void* ptr, size_t size) |
|
99 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
100 |
|
101 |
|
102 MOZALLOC_EXPORT char* moz_xstrdup(const char* str) |
|
103 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
104 |
|
105 MOZALLOC_EXPORT char* moz_strdup(const char* str) |
|
106 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
107 |
|
108 MOZALLOC_EXPORT size_t moz_malloc_usable_size(void *ptr); |
|
109 |
|
110 MOZALLOC_EXPORT size_t moz_malloc_size_of(const void *ptr); |
|
111 |
|
112 #if defined(HAVE_STRNDUP) |
|
113 MOZALLOC_EXPORT char* moz_xstrndup(const char* str, size_t strsize) |
|
114 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
115 |
|
116 MOZALLOC_EXPORT char* moz_strndup(const char* str, size_t strsize) |
|
117 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
118 #endif /* if defined(HAVE_STRNDUP) */ |
|
119 |
|
120 |
|
121 #if defined(HAVE_POSIX_MEMALIGN) |
|
122 MOZALLOC_EXPORT int moz_xposix_memalign(void **ptr, size_t alignment, size_t size) |
|
123 NS_WARN_UNUSED_RESULT; |
|
124 |
|
125 MOZALLOC_EXPORT int moz_posix_memalign(void **ptr, size_t alignment, size_t size) |
|
126 NS_WARN_UNUSED_RESULT; |
|
127 #endif /* if defined(HAVE_POSIX_MEMALIGN) */ |
|
128 |
|
129 |
|
130 #if defined(HAVE_MEMALIGN) |
|
131 MOZALLOC_EXPORT void* moz_xmemalign(size_t boundary, size_t size) |
|
132 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
133 |
|
134 MOZALLOC_EXPORT void* moz_memalign(size_t boundary, size_t size) |
|
135 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
136 #endif /* if defined(HAVE_MEMALIGN) */ |
|
137 |
|
138 |
|
139 #if defined(HAVE_VALLOC) |
|
140 MOZALLOC_EXPORT void* moz_xvalloc(size_t size) |
|
141 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
142 |
|
143 MOZALLOC_EXPORT void* moz_valloc(size_t size) |
|
144 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; |
|
145 #endif /* if defined(HAVE_VALLOC) */ |
|
146 |
|
147 |
|
148 #ifdef __cplusplus |
|
149 } /* extern "C" */ |
|
150 #endif /* ifdef __cplusplus */ |
|
151 |
|
152 |
|
153 #ifdef __cplusplus |
|
154 |
|
155 /* |
|
156 * We implement the default operators new/delete as part of |
|
157 * libmozalloc, replacing their definitions in libstdc++. The |
|
158 * operator new* definitions in libmozalloc will never return a NULL |
|
159 * pointer. |
|
160 * |
|
161 * Each operator new immediately below returns a pointer to memory |
|
162 * that can be delete'd by any of |
|
163 * |
|
164 * (1) the matching infallible operator delete immediately below |
|
165 * (2) the matching "fallible" operator delete further below |
|
166 * (3) the matching system |operator delete(void*, std::nothrow)| |
|
167 * (4) the matching system |operator delete(void*) throw(std::bad_alloc)| |
|
168 * |
|
169 * NB: these are declared |throw(std::bad_alloc)|, though they will never |
|
170 * throw that exception. This declaration is consistent with the rule |
|
171 * that |::operator new() throw(std::bad_alloc)| will never return NULL. |
|
172 */ |
|
173 |
|
174 /* NB: This is defined just to silence vacuous warnings about symbol |
|
175 * visibility on OS X/gcc. These symbols are force-inline and not |
|
176 * exported. */ |
|
177 #if defined(XP_MACOSX) |
|
178 # define MOZALLOC_EXPORT_NEW MOZALLOC_EXPORT |
|
179 #else |
|
180 # define MOZALLOC_EXPORT_NEW |
|
181 #endif |
|
182 |
|
183 #if defined(ANDROID) || defined(_MSC_VER) |
|
184 /* |
|
185 * Android doesn't fully support exceptions, so its <new> header |
|
186 * has operators that don't specify throw() at all. Also include MSVC |
|
187 * to suppress build warning spam (bug 578546). |
|
188 */ |
|
189 #define MOZALLOC_THROW_IF_HAS_EXCEPTIONS /**/ |
|
190 #define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS |
|
191 #else |
|
192 #define MOZALLOC_THROW_IF_HAS_EXCEPTIONS throw() |
|
193 #define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS throw(std::bad_alloc) |
|
194 #endif |
|
195 |
|
196 #define MOZALLOC_THROW_BAD_ALLOC MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS |
|
197 |
|
198 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
199 void* operator new(size_t size) MOZALLOC_THROW_BAD_ALLOC |
|
200 { |
|
201 return moz_xmalloc(size); |
|
202 } |
|
203 |
|
204 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
205 void* operator new(size_t size, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
206 { |
|
207 return moz_malloc(size); |
|
208 } |
|
209 |
|
210 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
211 void* operator new[](size_t size) MOZALLOC_THROW_BAD_ALLOC |
|
212 { |
|
213 return moz_xmalloc(size); |
|
214 } |
|
215 |
|
216 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
217 void* operator new[](size_t size, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
218 { |
|
219 return moz_malloc(size); |
|
220 } |
|
221 |
|
222 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
223 void operator delete(void* ptr) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
224 { |
|
225 return moz_free(ptr); |
|
226 } |
|
227 |
|
228 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
229 void operator delete(void* ptr, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
230 { |
|
231 return moz_free(ptr); |
|
232 } |
|
233 |
|
234 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
235 void operator delete[](void* ptr) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
236 { |
|
237 return moz_free(ptr); |
|
238 } |
|
239 |
|
240 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE |
|
241 void operator delete[](void* ptr, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
242 { |
|
243 return moz_free(ptr); |
|
244 } |
|
245 |
|
246 |
|
247 /* |
|
248 * We also add a new allocator variant: "fallible operator new." |
|
249 * Unlike libmozalloc's implementations of the standard nofail |
|
250 * allocators, this allocator is allowed to return NULL. It can be used |
|
251 * as follows |
|
252 * |
|
253 * Foo* f = new (mozilla::fallible) Foo(...); |
|
254 * |
|
255 * operator delete(fallible) is defined for completeness only. |
|
256 * |
|
257 * Each operator new below returns a pointer to memory that can be |
|
258 * delete'd by any of |
|
259 * |
|
260 * (1) the matching "fallible" operator delete below |
|
261 * (2) the matching infallible operator delete above |
|
262 * (3) the matching system |operator delete(void*, std::nothrow)| |
|
263 * (4) the matching system |operator delete(void*) throw(std::bad_alloc)| |
|
264 */ |
|
265 |
|
266 MOZALLOC_INLINE |
|
267 void* operator new(size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
268 { |
|
269 return moz_malloc(size); |
|
270 } |
|
271 |
|
272 MOZALLOC_INLINE |
|
273 void* operator new[](size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
274 { |
|
275 return moz_malloc(size); |
|
276 } |
|
277 |
|
278 MOZALLOC_INLINE |
|
279 void operator delete(void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
280 { |
|
281 moz_free(ptr); |
|
282 } |
|
283 |
|
284 MOZALLOC_INLINE |
|
285 void operator delete[](void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS |
|
286 { |
|
287 moz_free(ptr); |
|
288 } |
|
289 |
|
290 #endif /* ifdef __cplusplus */ |
|
291 |
|
292 |
|
293 #endif /* ifndef mozilla_mozalloc_h */ |