1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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/. */
8 #ifndef mozilla_mozalloc_h
9 #define mozilla_mozalloc_h
12 * https://bugzilla.mozilla.org/show_bug.cgi?id=427099
17 #if defined(__cplusplus)
20 #include "xpcom-config.h"
22 #if defined(__cplusplus)
23 #include "mozilla/fallible.h"
25 #include "mozilla/Attributes.h"
27 #define MOZALLOC_HAVE_XMALLOC
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) || (defined(XP_OS2) && defined(__declspec))
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")))
39 # define MOZALLOC_EXPORT
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
48 # define MOZALLOC_INLINE inline
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
59 #if defined(__cplusplus)
61 #endif /* ifdef __cplusplus */
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
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()|.
78 void moz_free(void* ptr
);
80 MOZALLOC_EXPORT
void* moz_xmalloc(size_t size
)
81 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
84 void* moz_malloc(size_t size
)
85 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
88 MOZALLOC_EXPORT
void* moz_xcalloc(size_t nmemb
, size_t size
)
89 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
91 MOZALLOC_EXPORT
void* moz_calloc(size_t nmemb
, size_t size
)
92 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
95 MOZALLOC_EXPORT
void* moz_xrealloc(void* ptr
, size_t size
)
96 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
98 MOZALLOC_EXPORT
void* moz_realloc(void* ptr
, size_t size
)
99 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
102 MOZALLOC_EXPORT
char* moz_xstrdup(const char* str
)
103 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
105 MOZALLOC_EXPORT
char* moz_strdup(const char* str
)
106 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
108 MOZALLOC_EXPORT
size_t moz_malloc_usable_size(void *ptr
);
110 MOZALLOC_EXPORT
size_t moz_malloc_size_of(const void *ptr
);
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
;
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) */
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
;
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) */
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
;
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) */
139 #if defined(HAVE_VALLOC)
140 MOZALLOC_EXPORT
void* moz_xvalloc(size_t size
)
141 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
143 MOZALLOC_EXPORT
void* moz_valloc(size_t size
)
144 NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT
;
145 #endif /* if defined(HAVE_VALLOC) */
150 #endif /* ifdef __cplusplus */
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
161 * Each operator new immediately below returns a pointer to memory
162 * that can be delete'd by any of
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)|
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.
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
177 #if defined(XP_MACOSX)
178 # define MOZALLOC_EXPORT_NEW MOZALLOC_EXPORT
180 # define MOZALLOC_EXPORT_NEW
183 #if defined(ANDROID) || defined(_MSC_VER)
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).
189 #define MOZALLOC_THROW_IF_HAS_EXCEPTIONS /**/
190 #define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS
192 #define MOZALLOC_THROW_IF_HAS_EXCEPTIONS throw()
193 #define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS throw(std::bad_alloc)
196 #define MOZALLOC_THROW_BAD_ALLOC MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS
198 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
199 void* operator new(size_t size
) MOZALLOC_THROW_BAD_ALLOC
201 return moz_xmalloc(size
);
204 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
205 void* operator new(size_t size
, const std::nothrow_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
207 return moz_malloc(size
);
210 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
211 void* operator new[](size_t size
) MOZALLOC_THROW_BAD_ALLOC
213 return moz_xmalloc(size
);
216 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
217 void* operator new[](size_t size
, const std::nothrow_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
219 return moz_malloc(size
);
222 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
223 void operator delete(void* ptr
) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
225 return moz_free(ptr
);
228 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
229 void operator delete(void* ptr
, const std::nothrow_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
231 return moz_free(ptr
);
234 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
235 void operator delete[](void* ptr
) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
237 return moz_free(ptr
);
240 MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
241 void operator delete[](void* ptr
, const std::nothrow_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
243 return moz_free(ptr
);
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
253 * Foo* f = new (mozilla::fallible) Foo(...);
255 * operator delete(fallible) is defined for completeness only.
257 * Each operator new below returns a pointer to memory that can be
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)|
267 void* operator new(size_t size
, const mozilla::fallible_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
269 return moz_malloc(size
);
273 void* operator new[](size_t size
, const mozilla::fallible_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
275 return moz_malloc(size
);
279 void operator delete(void* ptr
, const mozilla::fallible_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
285 void operator delete[](void* ptr
, const mozilla::fallible_t
&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
290 #endif /* ifdef __cplusplus */
293 #endif /* ifndef mozilla_mozalloc_h */