Bug 613131 - role nothing should allow recursive name calculation from children,...
[mozilla-central.git] / memory / mozalloc / mozalloc.cpp
blob5cd2a00c1d248db7609804e3d52fb6b447c8a7da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: sw=4 ts=4 et :
3 */
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is mozilla.org code.
19 * The Initial Developer of the Original Code is
20 * Mozilla Foundation
21 * Portions created by the Initial Developer are Copyright (C) 2009
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Chris Jones <jones.chris.g@gmail.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include <errno.h>
42 #include <new> // for std::bad_alloc
43 #include <string.h>
45 #if defined(MALLOC_H)
46 # include MALLOC_H // for memalign, valloc where available
47 #endif // if defined(MALLOC_H)
48 #include <stddef.h> // for size_t
49 #include <stdlib.h> // for malloc, free
50 #if defined(XP_UNIX)
51 # include <unistd.h> // for valloc on *BSD
52 #endif //if defined(XP_UNIX)
54 #if defined(MOZ_MEMORY)
55 // jemalloc.h doesn't redeclare symbols if they're provided by the OS
56 # include "jemalloc.h"
57 #endif
59 #if defined(XP_WIN) || (defined(XP_OS2) && defined(__declspec))
60 # define MOZALLOC_EXPORT __declspec(dllexport)
61 #endif
63 // Make sure that "malloc" et al. resolve to their libc variants.
64 #define MOZALLOC_DONT_DEFINE_MACRO_WRAPPERS
65 #include "mozilla/mozalloc.h"
66 #include "mozilla/mozalloc_oom.h" // for mozalloc_handle_oom
69 #if defined(__GNUC__) && (__GNUC__ > 2)
70 #define LIKELY(x) (__builtin_expect(!!(x), 1))
71 #define UNLIKELY(x) (__builtin_expect(!!(x), 0))
72 #else
73 #define LIKELY(x) (x)
74 #define UNLIKELY(x) (x)
75 #endif
77 #if defined(MOZ_MEMORY_ANDROID) || defined(WRAP_MALLOC_WITH_JEMALLOC)
78 #include "jemalloc.h"
79 #define malloc(a) je_malloc(a)
80 #define valloc(a) je_valloc(a)
81 #define calloc(a, b) je_calloc(a, b)
82 #define realloc(a, b) je_realloc(a, b)
83 #define free(a) je_free(a)
84 #define strdup(a) je_strdup(a)
85 #define strndup(a, b) je_strndup(a, b)
86 #define posix_memalign(a, b, c) je_posix_memalign(a, b, c)
87 #endif
89 void
90 moz_free(void* ptr)
92 free(ptr);
95 void*
96 moz_xmalloc(size_t size)
98 void* ptr = malloc(size);
99 if (UNLIKELY(!ptr)) {
100 mozalloc_handle_oom();
101 return moz_xmalloc(size);
103 return ptr;
105 void*
106 moz_malloc(size_t size)
108 return malloc(size);
111 void*
112 moz_xcalloc(size_t nmemb, size_t size)
114 void* ptr = calloc(nmemb, size);
115 if (UNLIKELY(!ptr)) {
116 mozalloc_handle_oom();
117 return moz_xcalloc(nmemb, size);
119 return ptr;
121 void*
122 moz_calloc(size_t nmemb, size_t size)
124 return calloc(nmemb, size);
127 void*
128 moz_xrealloc(void* ptr, size_t size)
130 void* newptr = realloc(ptr, size);
131 if (UNLIKELY(!newptr)) {
132 mozalloc_handle_oom();
133 return moz_xrealloc(ptr, size);
135 return newptr;
137 void*
138 moz_realloc(void* ptr, size_t size)
140 return realloc(ptr, size);
143 char*
144 moz_xstrdup(const char* str)
146 char* dup = strdup(str);
147 if (UNLIKELY(!dup)) {
148 mozalloc_handle_oom();
149 return moz_xstrdup(str);
151 return dup;
153 char*
154 moz_strdup(const char* str)
156 return strdup(str);
159 #if defined(HAVE_STRNDUP)
160 char*
161 moz_xstrndup(const char* str, size_t strsize)
163 char* dup = strndup(str, strsize);
164 if (UNLIKELY(!dup)) {
165 mozalloc_handle_oom();
166 return moz_xstrndup(str, strsize);
168 return dup;
170 char*
171 moz_strndup(const char* str, size_t strsize)
173 return strndup(str, strsize);
175 #endif // if defined(HAVE_STRNDUP)
177 #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_JEMALLOC_POSIX_MEMALIGN)
179 moz_xposix_memalign(void **ptr, size_t alignment, size_t size)
181 int err = posix_memalign(ptr, alignment, size);
182 if (UNLIKELY(err && ENOMEM == err)) {
183 mozalloc_handle_oom();
184 return moz_xposix_memalign(ptr, alignment, size);
186 // else: (0 == err) or (EINVAL == err)
187 return err;
190 moz_posix_memalign(void **ptr, size_t alignment, size_t size)
192 return posix_memalign(ptr, alignment, size);
194 #endif // if defined(HAVE_POSIX_MEMALIGN)
196 #if defined(HAVE_MEMALIGN) || defined(HAVE_JEMALLOC_MEMALIGN)
197 void*
198 moz_xmemalign(size_t boundary, size_t size)
200 void* ptr = memalign(boundary, size);
201 if (UNLIKELY(!ptr && EINVAL != errno)) {
202 mozalloc_handle_oom();
203 return moz_xmemalign(boundary, size);
205 // non-NULL ptr or errno == EINVAL
206 return ptr;
208 void*
209 moz_memalign(size_t boundary, size_t size)
211 return memalign(boundary, size);
213 #endif // if defined(HAVE_MEMALIGN)
215 #if defined(HAVE_VALLOC)
216 void*
217 moz_xvalloc(size_t size)
219 void* ptr = valloc(size);
220 if (UNLIKELY(!ptr)) {
221 mozalloc_handle_oom();
222 return moz_xvalloc(size);
224 return ptr;
226 void*
227 moz_valloc(size_t size)
229 return valloc(size);
231 #endif // if defined(HAVE_VALLOC)
233 size_t
234 moz_malloc_usable_size(void *ptr)
236 if (!ptr)
237 return 0;
239 #if defined(MOZ_MEMORY)
240 return malloc_usable_size(ptr);
241 #elif defined(XP_MACOSX)
242 return malloc_size(ptr);
243 #elif defined(XP_WIN)
244 return _msize(ptr);
245 #else
246 return 0;
247 #endif
250 namespace mozilla {
252 const fallible_t fallible = fallible_t();
254 } // namespace mozilla