Bug 1283439 - Include OpusDecoder.h only for Rust MP4 parsing r=kinetik
[gecko.git] / memory / mozalloc / mozalloc.cpp
blob9fc5f219d18e34afa8958e776699cd90ec0d4642
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/. */
8 #include <stddef.h> // for size_t
10 // Building with USE_STATIC_LIBS = True sets -MT instead of -MD. -MT sets _MT,
11 // while -MD sets _MT and _DLL.
12 #if defined(_MT) && !defined(_DLL)
13 #define MOZ_STATIC_RUNTIME
14 #endif
16 #if defined(MOZ_MEMORY) && !defined(MOZ_STATIC_RUNTIME)
17 // mozalloc.cpp is part of the same library as mozmemory, thus MOZ_MEMORY_IMPL
18 // is needed.
19 #define MOZ_MEMORY_IMPL
20 #include "mozmemory_wrap.h"
22 #if defined(XP_DARWIN)
23 #include <malloc/malloc.h> // for malloc_size
24 #endif
26 // See mozmemory_wrap.h for more details. This file is part of libmozglue, so
27 // it needs to use _impl suffixes. However, with libmozglue growing, this is
28 // becoming cumbersome, so we will likely use a malloc.h wrapper of some sort
29 // and allow the use of the functions without a _impl suffix.
30 #define MALLOC_DECL(name, return_type, ...) \
31 extern "C" MOZ_MEMORY_API return_type name ## _impl(__VA_ARGS__);
32 #define MALLOC_FUNS MALLOC_FUNCS_MALLOC
33 #include "malloc_decls.h"
35 extern "C" MOZ_MEMORY_API char *strdup_impl(const char *);
36 extern "C" MOZ_MEMORY_API char *strndup_impl(const char *, size_t);
38 #else
39 // When jemalloc is disabled, or when building the static runtime variant,
40 // we need not to use the suffixes.
42 #if defined(MALLOC_H)
43 # include MALLOC_H // for memalign, valloc, malloc_size, malloc_us
44 #endif // if defined(MALLOC_H)
45 #include <stdlib.h> // for malloc, free
46 #if defined(XP_UNIX)
47 # include <unistd.h> // for valloc on *BSD
48 #endif //if defined(XP_UNIX)
50 #define malloc_impl malloc
51 #define posix_memalign_impl posix_memalign
52 #define calloc_impl calloc
53 #define realloc_impl realloc
54 #define free_impl free
55 #define memalign_impl memalign
56 #define valloc_impl valloc
57 #define malloc_usable_size_impl malloc_usable_size
58 #define strdup_impl strdup
59 #define strndup_impl strndup
61 #endif
63 #include <errno.h>
64 #include <new> // for std::bad_alloc
65 #include <string.h>
67 #include <sys/types.h>
69 #include "mozilla/mozalloc.h"
70 #include "mozilla/mozalloc_oom.h" // for mozalloc_handle_oom
72 #ifdef __GNUC__
73 #define LIKELY(x) (__builtin_expect(!!(x), 1))
74 #define UNLIKELY(x) (__builtin_expect(!!(x), 0))
75 #else
76 #define LIKELY(x) (x)
77 #define UNLIKELY(x) (x)
78 #endif
80 void*
81 moz_xmalloc(size_t size)
83 void* ptr = malloc_impl(size);
84 if (UNLIKELY(!ptr && size)) {
85 mozalloc_handle_oom(size);
86 return moz_xmalloc(size);
88 return ptr;
91 void*
92 moz_xcalloc(size_t nmemb, size_t size)
94 void* ptr = calloc_impl(nmemb, size);
95 if (UNLIKELY(!ptr && nmemb && size)) {
96 mozalloc_handle_oom(size);
97 return moz_xcalloc(nmemb, size);
99 return ptr;
102 void*
103 moz_xrealloc(void* ptr, size_t size)
105 void* newptr = realloc_impl(ptr, size);
106 if (UNLIKELY(!newptr && size)) {
107 mozalloc_handle_oom(size);
108 return moz_xrealloc(ptr, size);
110 return newptr;
113 char*
114 moz_xstrdup(const char* str)
116 char* dup = strdup_impl(str);
117 if (UNLIKELY(!dup)) {
118 mozalloc_handle_oom(0);
119 return moz_xstrdup(str);
121 return dup;
124 #if defined(HAVE_STRNDUP)
125 char*
126 moz_xstrndup(const char* str, size_t strsize)
128 char* dup = strndup_impl(str, strsize);
129 if (UNLIKELY(!dup)) {
130 mozalloc_handle_oom(strsize);
131 return moz_xstrndup(str, strsize);
133 return dup;
135 #endif // if defined(HAVE_STRNDUP)
137 #if defined(HAVE_POSIX_MEMALIGN)
139 moz_xposix_memalign(void **ptr, size_t alignment, size_t size)
141 int err = posix_memalign_impl(ptr, alignment, size);
142 if (UNLIKELY(err && ENOMEM == err)) {
143 mozalloc_handle_oom(size);
144 return moz_xposix_memalign(ptr, alignment, size);
146 // else: (0 == err) or (EINVAL == err)
147 return err;
150 moz_posix_memalign(void **ptr, size_t alignment, size_t size)
152 int code = posix_memalign_impl(ptr, alignment, size);
153 if (code)
154 return code;
156 #if defined(XP_DARWIN)
157 // Workaround faulty OSX posix_memalign, which provides memory with the
158 // incorrect alignment sometimes, but returns 0 as if nothing was wrong.
159 size_t mask = alignment - 1;
160 if (((size_t)(*ptr) & mask) != 0) {
161 void* old = *ptr;
162 code = moz_posix_memalign(ptr, alignment, size);
163 free(old);
165 #endif
167 return code;
170 #endif // if defined(HAVE_POSIX_MEMALIGN)
172 #if defined(HAVE_MEMALIGN)
173 void*
174 moz_xmemalign(size_t boundary, size_t size)
176 void* ptr = memalign_impl(boundary, size);
177 if (UNLIKELY(!ptr && EINVAL != errno)) {
178 mozalloc_handle_oom(size);
179 return moz_xmemalign(boundary, size);
181 // non-NULL ptr or errno == EINVAL
182 return ptr;
184 #endif // if defined(HAVE_MEMALIGN)
186 #if defined(HAVE_VALLOC)
187 void*
188 moz_xvalloc(size_t size)
190 void* ptr = valloc_impl(size);
191 if (UNLIKELY(!ptr)) {
192 mozalloc_handle_oom(size);
193 return moz_xvalloc(size);
195 return ptr;
197 #endif // if defined(HAVE_VALLOC)
199 #ifndef MOZ_STATIC_RUNTIME
200 size_t
201 moz_malloc_usable_size(void *ptr)
203 if (!ptr)
204 return 0;
206 #if defined(XP_DARWIN)
207 return malloc_size(ptr);
208 #elif defined(HAVE_MALLOC_USABLE_SIZE) || defined(MOZ_MEMORY)
209 return malloc_usable_size_impl(ptr);
210 #elif defined(XP_WIN)
211 return _msize(ptr);
212 #else
213 return 0;
214 #endif
217 size_t moz_malloc_size_of(const void *ptr)
219 return moz_malloc_usable_size((void *)ptr);
221 #endif