Line data Source code
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/. */
7 :
8 : #include <stddef.h> // for size_t
9 :
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
15 :
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"
21 :
22 : #if defined(XP_DARWIN)
23 : #include <malloc/malloc.h> // for malloc_size
24 : #endif
25 :
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 : MOZ_MEMORY_API return_type name ## _impl(__VA_ARGS__);
32 : #define MALLOC_FUNCS MALLOC_FUNCS_MALLOC
33 : #include "malloc_decls.h"
34 :
35 : MOZ_MEMORY_API char *strdup_impl(const char *);
36 : MOZ_MEMORY_API char *strndup_impl(const char *, size_t);
37 :
38 : #else
39 : // When jemalloc is disabled, or when building the static runtime variant,
40 : // we need not to use the suffixes.
41 :
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)
49 :
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
60 :
61 : #endif
62 :
63 : #include <errno.h>
64 : #include <new> // for std::bad_alloc
65 : #include <string.h>
66 :
67 : #include <sys/types.h>
68 :
69 : #include "mozilla/mozalloc.h"
70 : #include "mozilla/mozalloc_oom.h" // for mozalloc_handle_oom
71 :
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
79 :
80 : void*
81 231383 : moz_xmalloc(size_t size)
82 : {
83 231383 : void* ptr = malloc_impl(size);
84 231383 : if (UNLIKELY(!ptr && size)) {
85 0 : mozalloc_handle_oom(size);
86 0 : return moz_xmalloc(size);
87 : }
88 231383 : return ptr;
89 : }
90 :
91 : void*
92 485 : moz_xcalloc(size_t nmemb, size_t size)
93 : {
94 485 : void* ptr = calloc_impl(nmemb, size);
95 485 : if (UNLIKELY(!ptr && nmemb && size)) {
96 0 : mozalloc_handle_oom(size);
97 0 : return moz_xcalloc(nmemb, size);
98 : }
99 485 : return ptr;
100 : }
101 :
102 : void*
103 16388 : moz_xrealloc(void* ptr, size_t size)
104 : {
105 16388 : void* newptr = realloc_impl(ptr, size);
106 16388 : if (UNLIKELY(!newptr && size)) {
107 0 : mozalloc_handle_oom(size);
108 0 : return moz_xrealloc(ptr, size);
109 : }
110 16388 : return newptr;
111 : }
112 :
113 : char*
114 0 : moz_xstrdup(const char* str)
115 : {
116 0 : char* dup = strdup_impl(str);
117 0 : if (UNLIKELY(!dup)) {
118 0 : mozalloc_handle_oom(0);
119 0 : return moz_xstrdup(str);
120 : }
121 0 : return dup;
122 : }
123 :
124 : #if defined(HAVE_STRNDUP)
125 : char*
126 0 : moz_xstrndup(const char* str, size_t strsize)
127 : {
128 0 : char* dup = strndup_impl(str, strsize);
129 0 : if (UNLIKELY(!dup)) {
130 0 : mozalloc_handle_oom(strsize);
131 0 : return moz_xstrndup(str, strsize);
132 : }
133 0 : return dup;
134 : }
135 : #endif // if defined(HAVE_STRNDUP)
136 :
137 : #if defined(HAVE_POSIX_MEMALIGN)
138 : int
139 0 : moz_xposix_memalign(void **ptr, size_t alignment, size_t size)
140 : {
141 0 : int err = posix_memalign_impl(ptr, alignment, size);
142 0 : if (UNLIKELY(err && ENOMEM == err)) {
143 0 : mozalloc_handle_oom(size);
144 0 : return moz_xposix_memalign(ptr, alignment, size);
145 : }
146 : // else: (0 == err) or (EINVAL == err)
147 0 : return err;
148 : }
149 : int
150 3 : moz_posix_memalign(void **ptr, size_t alignment, size_t size)
151 : {
152 3 : int code = posix_memalign_impl(ptr, alignment, size);
153 3 : if (code)
154 0 : return code;
155 :
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);
164 : }
165 : #endif
166 :
167 3 : return code;
168 :
169 : }
170 : #endif // if defined(HAVE_POSIX_MEMALIGN)
171 :
172 : #if defined(HAVE_MEMALIGN)
173 : void*
174 0 : moz_xmemalign(size_t boundary, size_t size)
175 : {
176 0 : void* ptr = memalign_impl(boundary, size);
177 0 : if (UNLIKELY(!ptr && EINVAL != errno)) {
178 0 : mozalloc_handle_oom(size);
179 0 : return moz_xmemalign(boundary, size);
180 : }
181 : // non-NULL ptr or errno == EINVAL
182 0 : return ptr;
183 : }
184 : #endif // if defined(HAVE_MEMALIGN)
185 :
186 : #if defined(HAVE_VALLOC)
187 : void*
188 0 : moz_xvalloc(size_t size)
189 : {
190 0 : void* ptr = valloc_impl(size);
191 0 : if (UNLIKELY(!ptr)) {
192 0 : mozalloc_handle_oom(size);
193 0 : return moz_xvalloc(size);
194 : }
195 0 : return ptr;
196 : }
197 : #endif // if defined(HAVE_VALLOC)
198 :
199 : #ifndef MOZ_STATIC_RUNTIME
200 : size_t
201 18128 : moz_malloc_usable_size(void *ptr)
202 : {
203 18128 : if (!ptr)
204 128 : return 0;
205 :
206 : #if defined(XP_DARWIN)
207 : return malloc_size(ptr);
208 : #elif defined(HAVE_MALLOC_USABLE_SIZE) || defined(MOZ_MEMORY)
209 18000 : return malloc_usable_size_impl(ptr);
210 : #elif defined(XP_WIN)
211 : return _msize(ptr);
212 : #else
213 : return 0;
214 : #endif
215 : }
216 :
217 1920 : size_t moz_malloc_size_of(const void *ptr)
218 : {
219 1920 : return moz_malloc_usable_size((void *)ptr);
220 : }
221 : #endif
|