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 #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
16 #if defined(MOZ_MEMORY) && !defined(MOZ_STATIC_RUNTIME)
17 // mozalloc.cpp is part of the same library as mozmemory, thus MOZ_MEMORY_IMPL
19 #define MOZ_MEMORY_IMPL
20 #include "mozmemory_wrap.h"
22 #if defined(XP_DARWIN)
23 #include <malloc/malloc.h> // for malloc_size
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);
39 // When jemalloc is disabled, or when building the static runtime variant,
40 // we need not to use the suffixes.
43 # include MALLOC_H // for memalign, valloc, malloc_size, malloc_us
44 #endif // if defined(MALLOC_H)
45 #include <stdlib.h> // for malloc, free
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
64 #include <new> // for std::bad_alloc
67 #include <sys/types.h>
69 #include "mozilla/mozalloc.h"
70 #include "mozilla/mozalloc_oom.h" // for mozalloc_handle_oom
73 #define LIKELY(x) (__builtin_expect(!!(x), 1))
74 #define UNLIKELY(x) (__builtin_expect(!!(x), 0))
77 #define UNLIKELY(x) (x)
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
);
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
);
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
);
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
);
124 #if defined(HAVE_STRNDUP)
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
);
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)
150 moz_posix_memalign(void **ptr
, size_t alignment
, size_t size
)
152 int code
= posix_memalign_impl(ptr
, alignment
, size
);
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) {
162 code
= moz_posix_memalign(ptr
, alignment
, size
);
170 #endif // if defined(HAVE_POSIX_MEMALIGN)
172 #if defined(HAVE_MEMALIGN)
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
184 #endif // if defined(HAVE_MEMALIGN)
186 #if defined(HAVE_VALLOC)
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
);
197 #endif // if defined(HAVE_VALLOC)
199 #ifndef MOZ_STATIC_RUNTIME
201 moz_malloc_usable_size(void *ptr
)
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)
217 size_t moz_malloc_size_of(const void *ptr
)
219 return moz_malloc_usable_size((void *)ptr
);