1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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
15 #if defined(__cplusplus)
17 // Since libstdc++ 6, including the C headers (e.g. stdlib.h) instead of the
18 // corresponding C++ header (e.g. cstdlib) can cause confusion in C++ code
19 // using things defined there. Specifically, with stdlib.h, the use of abs()
20 // in gfx/graphite2/src/inc/UtfCodec.h somehow ends up picking the wrong abs()
26 #if defined(MOZ_MEMORY) && defined(IMPL_MFBT)
27 # define MOZ_MEMORY_IMPL
28 # include "mozmemory_wrap.h"
29 # define MALLOC_FUNCS MALLOC_FUNCS_MALLOC
30 // See mozmemory_wrap.h for more details. Files that are part of libmozglue,
31 // need to use _impl suffixes, which is becoming cumbersome. We'll have to use
32 // something like a malloc.h wrapper and allow the use of the functions without
33 // a _impl suffix. In the meanwhile, this is enough to get by for C++ code.
34 # define NOTHROW_MALLOC_DECL(name, return_type, ...) \
35 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__) noexcept(true);
36 # define MALLOC_DECL(name, return_type, ...) \
37 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
38 # include "malloc_decls.h"
41 #if defined(__cplusplus)
42 # include "mozilla/fallible.h"
43 # include "mozilla/mozalloc_abort.h"
44 # include "mozilla/TemplateLib.h"
46 #include "mozilla/Attributes.h"
47 #include "mozilla/Types.h"
52 * We need to use malloc_impl and free_impl in this file when they are
53 * defined, because of how mozglue.dll is linked on Windows, where using
54 * malloc/free would end up using the symbols from the MSVCRT instead of
58 # define free_impl free
62 # define malloc_impl malloc
67 * Each declaration below is analogous to a "standard" allocation
68 * function, except that the out-of-memory handling is made explicit.
69 * The |moz_x| versions will never return a NULL pointer; if memory
70 * is exhausted, they abort. The |moz_| versions may return NULL
71 * pointers if memory is exhausted: their return value must be checked.
73 * All these allocation functions are *guaranteed* to return a pointer
74 * to memory allocated in such a way that that memory can be freed by
75 * passing that pointer to |free()|.
78 MFBT_API
void* moz_xmalloc(size_t size
) MOZ_INFALLIBLE_ALLOCATOR
;
80 MFBT_API
void* moz_xcalloc(size_t nmemb
, size_t size
) MOZ_INFALLIBLE_ALLOCATOR
;
82 MFBT_API
void* moz_xrealloc(void* ptr
, size_t size
) MOZ_INFALLIBLE_ALLOCATOR
;
84 MFBT_API
char* moz_xstrdup(const char* str
) MOZ_INFALLIBLE_ALLOCATOR
;
86 #if defined(HAVE_STRNDUP)
87 MFBT_API
char* moz_xstrndup(const char* str
,
88 size_t strsize
) MOZ_INFALLIBLE_ALLOCATOR
;
89 #endif /* if defined(HAVE_STRNDUP) */
91 MFBT_API
void* moz_xmemdup(const void* ptr
,
92 size_t size
) MOZ_INFALLIBLE_ALLOCATOR
;
94 MFBT_API
void* moz_xmemalign(size_t boundary
,
95 size_t size
) MOZ_INFALLIBLE_ALLOCATOR
;
97 MFBT_API
size_t moz_malloc_usable_size(void* ptr
);
99 MFBT_API
size_t moz_malloc_size_of(const void* ptr
);
102 * Like moz_malloc_size_of(), but works reliably with interior pointers, i.e.
103 * pointers into the middle of a live allocation.
105 MFBT_API
size_t moz_malloc_enclosing_size_of(const void* ptr
);
111 /* NB: This is defined with MFBT_API just to silence vacuous warnings
112 * about symbol visibility on OS X/gcc.
113 * These symbols are force-inline mainly for performance reasons, and
114 * not exported. While the standard doesn't allow that, we are in a
115 * controlled environment where the issues the standard tries to
116 * prevent don't apply, and we can't end up in situations where
117 * operator new and operator delete are inconsistent. */
119 # pragma clang diagnostic push
120 # pragma clang diagnostic ignored "-Winline-new-delete"
123 # if defined(XP_MACOSX)
124 # define MOZALLOC_EXPORT_NEW MFBT_API MOZ_ALWAYS_INLINE_EVEN_DEBUG
126 # define MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG
129 # include "mozilla/cxxalloc.h"
131 # pragma clang diagnostic pop
135 * This policy is identical to MallocAllocPolicy, except it uses
136 * moz_xmalloc/moz_xcalloc/moz_xrealloc instead of
137 * malloc/calloc/realloc.
139 class InfallibleAllocPolicy
{
141 template <typename T
>
142 T
* maybe_pod_malloc(size_t aNumElems
) {
143 return pod_malloc
<T
>(aNumElems
);
146 template <typename T
>
147 T
* maybe_pod_calloc(size_t aNumElems
) {
148 return pod_calloc
<T
>(aNumElems
);
151 template <typename T
>
152 T
* maybe_pod_realloc(T
* aPtr
, size_t aOldSize
, size_t aNewSize
) {
153 return pod_realloc
<T
>(aPtr
, aOldSize
, aNewSize
);
156 template <typename T
>
157 T
* pod_malloc(size_t aNumElems
) {
158 if (aNumElems
& mozilla::tl::MulOverflowMask
<sizeof(T
)>::value
) {
159 reportAllocOverflow();
161 return static_cast<T
*>(moz_xmalloc(aNumElems
* sizeof(T
)));
164 template <typename T
>
165 T
* pod_calloc(size_t aNumElems
) {
166 return static_cast<T
*>(moz_xcalloc(aNumElems
, sizeof(T
)));
169 template <typename T
>
170 T
* pod_realloc(T
* aPtr
, size_t aOldSize
, size_t aNewSize
) {
171 if (aNewSize
& mozilla::tl::MulOverflowMask
<sizeof(T
)>::value
) {
172 reportAllocOverflow();
174 return static_cast<T
*>(moz_xrealloc(aPtr
, aNewSize
* sizeof(T
)));
177 template <typename T
>
178 void free_(T
* aPtr
, size_t aNumElems
= 0) {
182 void reportAllocOverflow() const { mozalloc_abort("alloc overflow"); }
184 bool checkSimulatedOOM() const { return true; }
187 #endif /* ifdef __cplusplus */
198 #endif /* ifndef mozilla_mozalloc_h */