Bug 1755973 - Try implied arr+[0] in GetUniformIndices. r=gfx-reviewers,bradwerth
[gecko.git] / memory / mozalloc / mozalloc.h
blob549f92eded0be03a7a7693a05a429f4af47a2c4b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 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/. */
8 #ifndef mozilla_mozalloc_h
9 #define mozilla_mozalloc_h
12 * https://bugzilla.mozilla.org/show_bug.cgi?id=427099
15 #if defined(__cplusplus)
16 # include <new>
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()
21 # include <cstdlib>
22 #else
23 # include <stdlib.h>
24 #endif
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"
39 #endif
41 #if defined(__cplusplus)
42 # include "mozilla/fallible.h"
43 # include "mozilla/mozalloc_abort.h"
44 # include "mozilla/TemplateLib.h"
45 #endif
46 #include "mozilla/Attributes.h"
47 #include "mozilla/Types.h"
49 MOZ_BEGIN_EXTERN_C
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
55 * ours.
57 #ifndef free_impl
58 # define free_impl free
59 # define free_impl_
60 #endif
61 #ifndef malloc_impl
62 # define malloc_impl malloc
63 # define malloc_impl_
64 #endif
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);
107 MOZ_END_EXTERN_C
109 #ifdef __cplusplus
111 /* NB: This is defined just to silence vacuous warnings about symbol
112 * visibility on OS X/gcc. These symbols are force-inline and not
113 * exported. */
114 # if defined(XP_MACOSX)
115 # define MOZALLOC_EXPORT_NEW MFBT_API MOZ_ALWAYS_INLINE_EVEN_DEBUG
116 # else
117 # define MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG
118 # endif
120 # include "mozilla/cxxalloc.h"
123 * This policy is identical to MallocAllocPolicy, except it uses
124 * moz_xmalloc/moz_xcalloc/moz_xrealloc instead of
125 * malloc/calloc/realloc.
127 class InfallibleAllocPolicy {
128 public:
129 template <typename T>
130 T* maybe_pod_malloc(size_t aNumElems) {
131 return pod_malloc<T>(aNumElems);
134 template <typename T>
135 T* maybe_pod_calloc(size_t aNumElems) {
136 return pod_calloc<T>(aNumElems);
139 template <typename T>
140 T* maybe_pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize) {
141 return pod_realloc<T>(aPtr, aOldSize, aNewSize);
144 template <typename T>
145 T* pod_malloc(size_t aNumElems) {
146 if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
147 reportAllocOverflow();
149 return static_cast<T*>(moz_xmalloc(aNumElems * sizeof(T)));
152 template <typename T>
153 T* pod_calloc(size_t aNumElems) {
154 return static_cast<T*>(moz_xcalloc(aNumElems, sizeof(T)));
157 template <typename T>
158 T* pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize) {
159 if (aNewSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
160 reportAllocOverflow();
162 return static_cast<T*>(moz_xrealloc(aPtr, aNewSize * sizeof(T)));
165 template <typename T>
166 void free_(T* aPtr, size_t aNumElems = 0) {
167 free_impl(aPtr);
170 void reportAllocOverflow() const { mozalloc_abort("alloc overflow"); }
172 bool checkSimulatedOOM() const { return true; }
175 #endif /* ifdef __cplusplus */
177 #ifdef malloc_impl_
178 # undef malloc_impl_
179 # undef malloc_impl
180 #endif
181 #ifdef free_impl_
182 # undef free_impl_
183 # undef free_impl
184 #endif
186 #endif /* ifndef mozilla_mozalloc_h */