blob: 9b8b52d275b8df85a49c834690b776eeff123771 [file] [log] [blame]
Jason Evanse476f8a2010-01-16 09:53:50 -08001#define JEMALLOC_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evans289053c2009-06-22 12:08:42 -07003
Jason Evans289053c2009-06-22 12:08:42 -07004/******************************************************************************/
Jason Evanse476f8a2010-01-16 09:53:50 -08005/* Data. */
Jason Evans289053c2009-06-22 12:08:42 -07006
Jason Evanscd9a1342012-03-21 18:33:03 -07007malloc_tsd_data(, arenas, arena_t *, NULL)
8malloc_tsd_data(, thread_allocated, thread_allocated_t,
9 THREAD_ALLOCATED_INITIALIZER)
Jason Evans289053c2009-06-22 12:08:42 -070010
Jason Evanse476f8a2010-01-16 09:53:50 -080011/* Runtime configuration options. */
Jason Evans0a5489e2012-03-01 17:19:20 -080012const char *je_malloc_conf JEMALLOC_ATTR(visibility("default"));
Jason Evansb7924f52009-06-23 19:01:18 -070013#ifdef JEMALLOC_DEBUG
Jason Evanse476f8a2010-01-16 09:53:50 -080014bool opt_abort = true;
Jason Evansb7924f52009-06-23 19:01:18 -070015# ifdef JEMALLOC_FILL
Jason Evanse476f8a2010-01-16 09:53:50 -080016bool opt_junk = true;
Jason Evans7372b152012-02-10 20:22:09 -080017# else
18bool opt_junk = false;
Jason Evansb7924f52009-06-23 19:01:18 -070019# endif
Jason Evans289053c2009-06-22 12:08:42 -070020#else
Jason Evanse476f8a2010-01-16 09:53:50 -080021bool opt_abort = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080022bool opt_junk = false;
Jason Evans289053c2009-06-22 12:08:42 -070023#endif
Jason Evans122449b2012-04-06 00:35:09 -070024size_t opt_quarantine = ZU(0);
Jason Evansd6abcbb2012-04-12 17:09:54 -070025bool opt_redzone = false;
Jason Evansb1476112012-04-05 13:36:17 -070026bool opt_utrace = false;
Jason Evans122449b2012-04-06 00:35:09 -070027bool opt_valgrind = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080028bool opt_xmalloc = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080029bool opt_zero = false;
Jason Evanse7339702010-10-23 18:37:06 -070030size_t opt_narenas = 0;
Jason Evans289053c2009-06-22 12:08:42 -070031
Jason Evanscd9a1342012-03-21 18:33:03 -070032unsigned ncpus;
33
34malloc_mutex_t arenas_lock;
35arena_t **arenas;
36unsigned narenas;
37
38/* Set to true once the allocator has been initialized. */
Jason Evans4eeb52f2012-04-02 01:46:25 -070039static bool malloc_initialized = false;
Jason Evanscd9a1342012-03-21 18:33:03 -070040
Jason Evans41b6afb2012-02-02 22:04:57 -080041#ifdef JEMALLOC_THREADED_INIT
Jason Evanscd9a1342012-03-21 18:33:03 -070042/* Used to let the initializing thread recursively allocate. */
Jason Evans02b23122012-04-05 11:06:23 -070043# define NO_INITIALIZER ((unsigned long)0)
Jason Evans41b6afb2012-02-02 22:04:57 -080044# define INITIALIZER pthread_self()
45# define IS_INITIALIZER (malloc_initializer == pthread_self())
Jason Evans02b23122012-04-05 11:06:23 -070046static pthread_t malloc_initializer = NO_INITIALIZER;
Jason Evans41b6afb2012-02-02 22:04:57 -080047#else
Jason Evans02b23122012-04-05 11:06:23 -070048# define NO_INITIALIZER false
Jason Evans41b6afb2012-02-02 22:04:57 -080049# define INITIALIZER true
50# define IS_INITIALIZER malloc_initializer
Jason Evans02b23122012-04-05 11:06:23 -070051static bool malloc_initializer = NO_INITIALIZER;
Jason Evans41b6afb2012-02-02 22:04:57 -080052#endif
Jason Evanscd9a1342012-03-21 18:33:03 -070053
54/* Used to avoid initialization races. */
55static malloc_mutex_t init_lock = MALLOC_MUTEX_INITIALIZER;
56
Jason Evansb1476112012-04-05 13:36:17 -070057typedef struct {
58 void *p; /* Input pointer (as in realloc(p, s)). */
59 size_t s; /* Request size. */
60 void *r; /* Result pointer. */
61} malloc_utrace_t;
62
63#ifdef JEMALLOC_UTRACE
64# define UTRACE(a, b, c) do { \
65 if (opt_utrace) { \
66 malloc_utrace_t ut; \
67 ut.p = (a); \
68 ut.s = (b); \
69 ut.r = (c); \
70 utrace(&ut, sizeof(ut)); \
71 } \
72} while (0)
73#else
74# define UTRACE(a, b, c)
75#endif
76
Jason Evans289053c2009-06-22 12:08:42 -070077/******************************************************************************/
Jason Evanse476f8a2010-01-16 09:53:50 -080078/* Function prototypes for non-inline static functions. */
Jason Evans289053c2009-06-22 12:08:42 -070079
Jason Evans03c22372010-01-03 12:10:42 -080080static void stats_print_atexit(void);
Jason Evansc9658dd2009-06-22 14:44:08 -070081static unsigned malloc_ncpus(void);
Jason Evanse7339702010-10-23 18:37:06 -070082static bool malloc_conf_next(char const **opts_p, char const **k_p,
83 size_t *klen_p, char const **v_p, size_t *vlen_p);
84static void malloc_conf_error(const char *msg, const char *k, size_t klen,
85 const char *v, size_t vlen);
86static void malloc_conf_init(void);
Jason Evans289053c2009-06-22 12:08:42 -070087static bool malloc_init_hard(void);
Jason Evans59656312012-02-28 21:37:38 -080088static int imemalign(void **memptr, size_t alignment, size_t size,
Jason Evans0a0bbf62012-03-13 12:55:21 -070089 size_t min_alignment);
Jason Evans289053c2009-06-22 12:08:42 -070090
Jason Evans289053c2009-06-22 12:08:42 -070091/******************************************************************************/
Jason Evansc9658dd2009-06-22 14:44:08 -070092/*
Jason Evanse476f8a2010-01-16 09:53:50 -080093 * Begin miscellaneous support functions.
Jason Evansb7924f52009-06-23 19:01:18 -070094 */
95
Jason Evanse476f8a2010-01-16 09:53:50 -080096/* Create a new arena and insert it into the arenas array at index ind. */
97arena_t *
98arenas_extend(unsigned ind)
Jason Evans289053c2009-06-22 12:08:42 -070099{
100 arena_t *ret;
101
Jason Evansb1726102012-02-28 16:50:47 -0800102 ret = (arena_t *)base_alloc(sizeof(arena_t));
Jason Evanse476f8a2010-01-16 09:53:50 -0800103 if (ret != NULL && arena_new(ret, ind) == false) {
104 arenas[ind] = ret;
105 return (ret);
Jason Evans289053c2009-06-22 12:08:42 -0700106 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800107 /* Only reached if there is an OOM error. */
Jason Evans289053c2009-06-22 12:08:42 -0700108
Jason Evanse476f8a2010-01-16 09:53:50 -0800109 /*
110 * OOM here is quite inconvenient to propagate, since dealing with it
111 * would require a check for failure in the fast path. Instead, punt
112 * by using arenas[0]. In practice, this is an extremely unlikely
113 * failure.
114 */
Jason Evans698805c2010-03-03 17:45:38 -0800115 malloc_write("<jemalloc>: Error initializing arena\n");
Jason Evanse476f8a2010-01-16 09:53:50 -0800116 if (opt_abort)
117 abort();
Jason Evans289053c2009-06-22 12:08:42 -0700118
Jason Evanse476f8a2010-01-16 09:53:50 -0800119 return (arenas[0]);
Jason Evans289053c2009-06-22 12:08:42 -0700120}
121
Jason Evans4c2faa82012-03-13 11:09:23 -0700122/* Slow path, called only by choose_arena(). */
Jason Evanse476f8a2010-01-16 09:53:50 -0800123arena_t *
Jason Evans289053c2009-06-22 12:08:42 -0700124choose_arena_hard(void)
125{
126 arena_t *ret;
127
Jason Evans289053c2009-06-22 12:08:42 -0700128 if (narenas > 1) {
Jason Evans597632b2011-03-18 13:41:33 -0700129 unsigned i, choose, first_null;
130
131 choose = 0;
132 first_null = narenas;
Jason Evans3ee7a5c2009-12-29 00:09:15 -0800133 malloc_mutex_lock(&arenas_lock);
Jason Evans0657f122011-03-18 17:56:14 -0700134 assert(arenas[0] != NULL);
Jason Evans597632b2011-03-18 13:41:33 -0700135 for (i = 1; i < narenas; i++) {
136 if (arenas[i] != NULL) {
137 /*
138 * Choose the first arena that has the lowest
139 * number of threads assigned to it.
140 */
141 if (arenas[i]->nthreads <
142 arenas[choose]->nthreads)
143 choose = i;
144 } else if (first_null == narenas) {
145 /*
146 * Record the index of the first uninitialized
147 * arena, in case all extant arenas are in use.
148 *
149 * NB: It is possible for there to be
150 * discontinuities in terms of initialized
151 * versus uninitialized arenas, due to the
152 * "thread.arena" mallctl.
153 */
154 first_null = i;
155 }
156 }
157
Jason Evans41b6afb2012-02-02 22:04:57 -0800158 if (arenas[choose]->nthreads == 0 || first_null == narenas) {
Jason Evans597632b2011-03-18 13:41:33 -0700159 /*
160 * Use an unloaded arena, or the least loaded arena if
161 * all arenas are already initialized.
162 */
163 ret = arenas[choose];
164 } else {
165 /* Initialize a new arena. */
166 ret = arenas_extend(first_null);
167 }
168 ret->nthreads++;
Jason Evans3ee7a5c2009-12-29 00:09:15 -0800169 malloc_mutex_unlock(&arenas_lock);
Jason Evans597632b2011-03-18 13:41:33 -0700170 } else {
Jason Evans289053c2009-06-22 12:08:42 -0700171 ret = arenas[0];
Jason Evans597632b2011-03-18 13:41:33 -0700172 malloc_mutex_lock(&arenas_lock);
173 ret->nthreads++;
174 malloc_mutex_unlock(&arenas_lock);
175 }
Jason Evans289053c2009-06-22 12:08:42 -0700176
Jason Evanscd9a1342012-03-21 18:33:03 -0700177 arenas_tsd_set(&ret);
Jason Evans289053c2009-06-22 12:08:42 -0700178
179 return (ret);
180}
Jason Evans289053c2009-06-22 12:08:42 -0700181
Jason Evans03c22372010-01-03 12:10:42 -0800182static void
183stats_print_atexit(void)
184{
185
Jason Evans7372b152012-02-10 20:22:09 -0800186 if (config_tcache && config_stats) {
187 unsigned i;
Jason Evans03c22372010-01-03 12:10:42 -0800188
Jason Evans7372b152012-02-10 20:22:09 -0800189 /*
190 * Merge stats from extant threads. This is racy, since
191 * individual threads do not lock when recording tcache stats
192 * events. As a consequence, the final stats may be slightly
193 * out of date by the time they are reported, if other threads
194 * continue to allocate.
195 */
196 for (i = 0; i < narenas; i++) {
197 arena_t *arena = arenas[i];
198 if (arena != NULL) {
199 tcache_t *tcache;
Jason Evans03c22372010-01-03 12:10:42 -0800200
Jason Evans7372b152012-02-10 20:22:09 -0800201 /*
202 * tcache_stats_merge() locks bins, so if any
203 * code is introduced that acquires both arena
204 * and bin locks in the opposite order,
205 * deadlocks may result.
206 */
207 malloc_mutex_lock(&arena->lock);
208 ql_foreach(tcache, &arena->tcache_ql, link) {
209 tcache_stats_merge(tcache, arena);
210 }
211 malloc_mutex_unlock(&arena->lock);
Jason Evans03c22372010-01-03 12:10:42 -0800212 }
Jason Evans03c22372010-01-03 12:10:42 -0800213 }
214 }
Jason Evans0a5489e2012-03-01 17:19:20 -0800215 je_malloc_stats_print(NULL, NULL, NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700216}
217
Jason Evans289053c2009-06-22 12:08:42 -0700218/*
Jason Evanse476f8a2010-01-16 09:53:50 -0800219 * End miscellaneous support functions.
Jason Evans289053c2009-06-22 12:08:42 -0700220 */
221/******************************************************************************/
222/*
Jason Evanse476f8a2010-01-16 09:53:50 -0800223 * Begin initialization functions.
Jason Evans289053c2009-06-22 12:08:42 -0700224 */
225
Jason Evansc9658dd2009-06-22 14:44:08 -0700226static unsigned
227malloc_ncpus(void)
228{
229 unsigned ret;
Jason Evansb7924f52009-06-23 19:01:18 -0700230 long result;
Jason Evansc9658dd2009-06-22 14:44:08 -0700231
Jason Evansb7924f52009-06-23 19:01:18 -0700232 result = sysconf(_SC_NPROCESSORS_ONLN);
233 if (result == -1) {
234 /* Error. */
235 ret = 1;
Jason Evansc9658dd2009-06-22 14:44:08 -0700236 }
Jason Evansb7924f52009-06-23 19:01:18 -0700237 ret = (unsigned)result;
Jason Evansc9658dd2009-06-22 14:44:08 -0700238
239 return (ret);
240}
Jason Evansb7924f52009-06-23 19:01:18 -0700241
Jason Evanscd9a1342012-03-21 18:33:03 -0700242void
Jason Evans597632b2011-03-18 13:41:33 -0700243arenas_cleanup(void *arg)
244{
Jason Evanscd9a1342012-03-21 18:33:03 -0700245 arena_t *arena = *(arena_t **)arg;
Jason Evans597632b2011-03-18 13:41:33 -0700246
247 malloc_mutex_lock(&arenas_lock);
248 arena->nthreads--;
249 malloc_mutex_unlock(&arenas_lock);
250}
251
Jason Evans289053c2009-06-22 12:08:42 -0700252static inline bool
253malloc_init(void)
254{
255
256 if (malloc_initialized == false)
257 return (malloc_init_hard());
258
259 return (false);
260}
261
262static bool
Jason Evanse7339702010-10-23 18:37:06 -0700263malloc_conf_next(char const **opts_p, char const **k_p, size_t *klen_p,
264 char const **v_p, size_t *vlen_p)
265{
266 bool accept;
267 const char *opts = *opts_p;
268
269 *k_p = opts;
270
271 for (accept = false; accept == false;) {
272 switch (*opts) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800273 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
274 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
275 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
276 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
277 case 'Y': case 'Z':
278 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
279 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
280 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
281 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
282 case 'y': case 'z':
283 case '0': case '1': case '2': case '3': case '4': case '5':
284 case '6': case '7': case '8': case '9':
285 case '_':
286 opts++;
287 break;
288 case ':':
289 opts++;
290 *klen_p = (uintptr_t)opts - 1 - (uintptr_t)*k_p;
291 *v_p = opts;
292 accept = true;
293 break;
294 case '\0':
295 if (opts != *opts_p) {
296 malloc_write("<jemalloc>: Conf string ends "
297 "with key\n");
298 }
299 return (true);
300 default:
301 malloc_write("<jemalloc>: Malformed conf string\n");
302 return (true);
Jason Evanse7339702010-10-23 18:37:06 -0700303 }
304 }
305
306 for (accept = false; accept == false;) {
307 switch (*opts) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800308 case ',':
309 opts++;
310 /*
311 * Look ahead one character here, because the next time
312 * this function is called, it will assume that end of
313 * input has been cleanly reached if no input remains,
314 * but we have optimistically already consumed the
315 * comma if one exists.
316 */
317 if (*opts == '\0') {
318 malloc_write("<jemalloc>: Conf string ends "
319 "with comma\n");
320 }
321 *vlen_p = (uintptr_t)opts - 1 - (uintptr_t)*v_p;
322 accept = true;
323 break;
324 case '\0':
325 *vlen_p = (uintptr_t)opts - (uintptr_t)*v_p;
326 accept = true;
327 break;
328 default:
329 opts++;
330 break;
Jason Evanse7339702010-10-23 18:37:06 -0700331 }
332 }
333
334 *opts_p = opts;
335 return (false);
336}
337
338static void
339malloc_conf_error(const char *msg, const char *k, size_t klen, const char *v,
340 size_t vlen)
341{
Jason Evanse7339702010-10-23 18:37:06 -0700342
Jason Evansd81e4bd2012-03-06 14:57:45 -0800343 malloc_printf("<jemalloc>: %s: %.*s:%.*s\n", msg, (int)klen, k,
344 (int)vlen, v);
Jason Evanse7339702010-10-23 18:37:06 -0700345}
346
347static void
348malloc_conf_init(void)
Jason Evans289053c2009-06-22 12:08:42 -0700349{
350 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -0700351 char buf[PATH_MAX + 1];
Jason Evanse7339702010-10-23 18:37:06 -0700352 const char *opts, *k, *v;
353 size_t klen, vlen;
354
355 for (i = 0; i < 3; i++) {
356 /* Get runtime configuration. */
357 switch (i) {
358 case 0:
Jason Evans0a5489e2012-03-01 17:19:20 -0800359 if (je_malloc_conf != NULL) {
Jason Evanse7339702010-10-23 18:37:06 -0700360 /*
361 * Use options that were compiled into the
362 * program.
363 */
Jason Evans0a5489e2012-03-01 17:19:20 -0800364 opts = je_malloc_conf;
Jason Evanse7339702010-10-23 18:37:06 -0700365 } else {
366 /* No configuration specified. */
367 buf[0] = '\0';
368 opts = buf;
369 }
370 break;
371 case 1: {
372 int linklen;
373 const char *linkname =
374#ifdef JEMALLOC_PREFIX
375 "/etc/"JEMALLOC_PREFIX"malloc.conf"
376#else
377 "/etc/malloc.conf"
378#endif
379 ;
380
381 if ((linklen = readlink(linkname, buf,
382 sizeof(buf) - 1)) != -1) {
383 /*
384 * Use the contents of the "/etc/malloc.conf"
385 * symbolic link's name.
386 */
387 buf[linklen] = '\0';
388 opts = buf;
389 } else {
390 /* No configuration specified. */
391 buf[0] = '\0';
392 opts = buf;
393 }
394 break;
Jason Evansd81e4bd2012-03-06 14:57:45 -0800395 } case 2: {
Jason Evanse7339702010-10-23 18:37:06 -0700396 const char *envname =
397#ifdef JEMALLOC_PREFIX
398 JEMALLOC_CPREFIX"MALLOC_CONF"
399#else
400 "MALLOC_CONF"
401#endif
402 ;
403
404 if ((opts = getenv(envname)) != NULL) {
405 /*
406 * Do nothing; opts is already initialized to
Jason Evans8ad0eac2010-12-17 18:07:53 -0800407 * the value of the MALLOC_CONF environment
408 * variable.
Jason Evanse7339702010-10-23 18:37:06 -0700409 */
410 } else {
411 /* No configuration specified. */
412 buf[0] = '\0';
413 opts = buf;
414 }
415 break;
Jason Evansd81e4bd2012-03-06 14:57:45 -0800416 } default:
Jason Evanse7339702010-10-23 18:37:06 -0700417 /* NOTREACHED */
418 assert(false);
419 buf[0] = '\0';
420 opts = buf;
421 }
422
423 while (*opts != '\0' && malloc_conf_next(&opts, &k, &klen, &v,
424 &vlen) == false) {
Jason Evans122449b2012-04-06 00:35:09 -0700425#define CONF_HANDLE_BOOL_HIT(o, n, hit) \
Jason Evanse7339702010-10-23 18:37:06 -0700426 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
427 klen) == 0) { \
428 if (strncmp("true", v, vlen) == 0 && \
429 vlen == sizeof("true")-1) \
Jason Evansd81e4bd2012-03-06 14:57:45 -0800430 o = true; \
Jason Evanse7339702010-10-23 18:37:06 -0700431 else if (strncmp("false", v, vlen) == \
432 0 && vlen == sizeof("false")-1) \
Jason Evansd81e4bd2012-03-06 14:57:45 -0800433 o = false; \
Jason Evanse7339702010-10-23 18:37:06 -0700434 else { \
435 malloc_conf_error( \
436 "Invalid conf value", \
437 k, klen, v, vlen); \
438 } \
Jason Evans122449b2012-04-06 00:35:09 -0700439 hit = true; \
440 } else \
441 hit = false;
442#define CONF_HANDLE_BOOL(o, n) { \
443 bool hit; \
444 CONF_HANDLE_BOOL_HIT(o, n, hit); \
445 if (hit) \
Jason Evanse7339702010-10-23 18:37:06 -0700446 continue; \
Jason Evans122449b2012-04-06 00:35:09 -0700447}
Jason Evansd81e4bd2012-03-06 14:57:45 -0800448#define CONF_HANDLE_SIZE_T(o, n, min, max) \
Jason Evanse7339702010-10-23 18:37:06 -0700449 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
450 klen) == 0) { \
Jason Evans122449b2012-04-06 00:35:09 -0700451 uintmax_t um; \
Jason Evanse7339702010-10-23 18:37:06 -0700452 char *end; \
453 \
454 errno = 0; \
Jason Evans41b6afb2012-02-02 22:04:57 -0800455 um = malloc_strtoumax(v, &end, 0); \
Jason Evanse7339702010-10-23 18:37:06 -0700456 if (errno != 0 || (uintptr_t)end - \
457 (uintptr_t)v != vlen) { \
458 malloc_conf_error( \
459 "Invalid conf value", \
460 k, klen, v, vlen); \
Jason Evans41b6afb2012-02-02 22:04:57 -0800461 } else if (um < min || um > max) { \
Jason Evanse7339702010-10-23 18:37:06 -0700462 malloc_conf_error( \
463 "Out-of-range conf value", \
464 k, klen, v, vlen); \
465 } else \
Jason Evans41b6afb2012-02-02 22:04:57 -0800466 o = um; \
Jason Evanse7339702010-10-23 18:37:06 -0700467 continue; \
468 }
Jason Evansd81e4bd2012-03-06 14:57:45 -0800469#define CONF_HANDLE_SSIZE_T(o, n, min, max) \
Jason Evanse7339702010-10-23 18:37:06 -0700470 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
471 klen) == 0) { \
472 long l; \
473 char *end; \
474 \
475 errno = 0; \
476 l = strtol(v, &end, 0); \
477 if (errno != 0 || (uintptr_t)end - \
478 (uintptr_t)v != vlen) { \
479 malloc_conf_error( \
480 "Invalid conf value", \
481 k, klen, v, vlen); \
482 } else if (l < (ssize_t)min || l > \
483 (ssize_t)max) { \
484 malloc_conf_error( \
485 "Out-of-range conf value", \
486 k, klen, v, vlen); \
487 } else \
Jason Evansd81e4bd2012-03-06 14:57:45 -0800488 o = l; \
Jason Evanse7339702010-10-23 18:37:06 -0700489 continue; \
490 }
Jason Evansd81e4bd2012-03-06 14:57:45 -0800491#define CONF_HANDLE_CHAR_P(o, n, d) \
Jason Evanse7339702010-10-23 18:37:06 -0700492 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
493 klen) == 0) { \
494 size_t cpylen = (vlen <= \
Jason Evansd81e4bd2012-03-06 14:57:45 -0800495 sizeof(o)-1) ? vlen : \
496 sizeof(o)-1; \
497 strncpy(o, v, cpylen); \
498 o[cpylen] = '\0'; \
Jason Evanse7339702010-10-23 18:37:06 -0700499 continue; \
500 }
501
Jason Evansd81e4bd2012-03-06 14:57:45 -0800502 CONF_HANDLE_BOOL(opt_abort, abort)
Jason Evanse7339702010-10-23 18:37:06 -0700503 /*
504 * Chunks always require at least one * header page,
505 * plus one data page.
506 */
Jason Evansae4c7b42012-04-02 07:04:34 -0700507 CONF_HANDLE_SIZE_T(opt_lg_chunk, lg_chunk, LG_PAGE+1,
Jason Evanse7339702010-10-23 18:37:06 -0700508 (sizeof(size_t) << 3) - 1)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800509 CONF_HANDLE_SIZE_T(opt_narenas, narenas, 1, SIZE_T_MAX)
510 CONF_HANDLE_SSIZE_T(opt_lg_dirty_mult, lg_dirty_mult,
511 -1, (sizeof(size_t) << 3) - 1)
512 CONF_HANDLE_BOOL(opt_stats_print, stats_print)
Jason Evans7372b152012-02-10 20:22:09 -0800513 if (config_fill) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800514 CONF_HANDLE_BOOL(opt_junk, junk)
Jason Evans122449b2012-04-06 00:35:09 -0700515 CONF_HANDLE_SIZE_T(opt_quarantine, quarantine,
516 0, SIZE_T_MAX)
517 CONF_HANDLE_BOOL(opt_redzone, redzone)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800518 CONF_HANDLE_BOOL(opt_zero, zero)
Jason Evans7372b152012-02-10 20:22:09 -0800519 }
Jason Evansb1476112012-04-05 13:36:17 -0700520 if (config_utrace) {
521 CONF_HANDLE_BOOL(opt_utrace, utrace)
522 }
Jason Evans122449b2012-04-06 00:35:09 -0700523 if (config_valgrind) {
524 bool hit;
525 CONF_HANDLE_BOOL_HIT(opt_valgrind,
526 valgrind, hit)
527 if (config_fill && opt_valgrind && hit) {
528 opt_junk = false;
529 opt_zero = false;
530 if (opt_quarantine == 0) {
531 opt_quarantine =
532 JEMALLOC_VALGRIND_QUARANTINE_DEFAULT;
533 }
534 opt_redzone = true;
535 }
536 if (hit)
537 continue;
538 }
Jason Evans7372b152012-02-10 20:22:09 -0800539 if (config_xmalloc) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800540 CONF_HANDLE_BOOL(opt_xmalloc, xmalloc)
Jason Evans7372b152012-02-10 20:22:09 -0800541 }
542 if (config_tcache) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800543 CONF_HANDLE_BOOL(opt_tcache, tcache)
544 CONF_HANDLE_SSIZE_T(opt_lg_tcache_max,
545 lg_tcache_max, -1,
Jason Evans7372b152012-02-10 20:22:09 -0800546 (sizeof(size_t) << 3) - 1)
547 }
548 if (config_prof) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800549 CONF_HANDLE_BOOL(opt_prof, prof)
550 CONF_HANDLE_CHAR_P(opt_prof_prefix, prof_prefix,
551 "jeprof")
552 CONF_HANDLE_BOOL(opt_prof_active, prof_active)
553 CONF_HANDLE_SSIZE_T(opt_lg_prof_sample,
554 lg_prof_sample, 0,
Jason Evans7372b152012-02-10 20:22:09 -0800555 (sizeof(uint64_t) << 3) - 1)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800556 CONF_HANDLE_BOOL(opt_prof_accum, prof_accum)
557 CONF_HANDLE_SSIZE_T(opt_lg_prof_interval,
558 lg_prof_interval, -1,
Jason Evans7372b152012-02-10 20:22:09 -0800559 (sizeof(uint64_t) << 3) - 1)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800560 CONF_HANDLE_BOOL(opt_prof_gdump, prof_gdump)
561 CONF_HANDLE_BOOL(opt_prof_leak, prof_leak)
Jason Evans7372b152012-02-10 20:22:09 -0800562 }
Jason Evanse7339702010-10-23 18:37:06 -0700563 malloc_conf_error("Invalid conf pair", k, klen, v,
564 vlen);
565#undef CONF_HANDLE_BOOL
566#undef CONF_HANDLE_SIZE_T
567#undef CONF_HANDLE_SSIZE_T
568#undef CONF_HANDLE_CHAR_P
569 }
Jason Evanse7339702010-10-23 18:37:06 -0700570 }
571}
572
573static bool
574malloc_init_hard(void)
575{
Jason Evansb7924f52009-06-23 19:01:18 -0700576 arena_t *init_arenas[1];
Jason Evans289053c2009-06-22 12:08:42 -0700577
578 malloc_mutex_lock(&init_lock);
Jason Evans41b6afb2012-02-02 22:04:57 -0800579 if (malloc_initialized || IS_INITIALIZER) {
Jason Evans289053c2009-06-22 12:08:42 -0700580 /*
581 * Another thread initialized the allocator before this one
Jason Evansa25d0a82009-11-09 14:57:38 -0800582 * acquired init_lock, or this thread is the initializing
583 * thread, and it is recursively allocating.
Jason Evans289053c2009-06-22 12:08:42 -0700584 */
585 malloc_mutex_unlock(&init_lock);
586 return (false);
587 }
Jason Evans41b6afb2012-02-02 22:04:57 -0800588#ifdef JEMALLOC_THREADED_INIT
Jason Evans02b23122012-04-05 11:06:23 -0700589 if (malloc_initializer != NO_INITIALIZER && IS_INITIALIZER == false) {
Jason Evansb7924f52009-06-23 19:01:18 -0700590 /* Busy-wait until the initializing thread completes. */
591 do {
592 malloc_mutex_unlock(&init_lock);
593 CPU_SPINWAIT;
594 malloc_mutex_lock(&init_lock);
595 } while (malloc_initialized == false);
Jason Evans2541e1b2010-07-22 11:35:59 -0700596 malloc_mutex_unlock(&init_lock);
Jason Evansb7924f52009-06-23 19:01:18 -0700597 return (false);
598 }
Jason Evans41b6afb2012-02-02 22:04:57 -0800599#endif
600 malloc_initializer = INITIALIZER;
Jason Evans289053c2009-06-22 12:08:42 -0700601
Jason Evanscd9a1342012-03-21 18:33:03 -0700602 malloc_tsd_boot();
Jason Evans7372b152012-02-10 20:22:09 -0800603 if (config_prof)
604 prof_boot0();
Jason Evans289053c2009-06-22 12:08:42 -0700605
Jason Evanse7339702010-10-23 18:37:06 -0700606 malloc_conf_init();
Jason Evans289053c2009-06-22 12:08:42 -0700607
Mike Hommeye77fa592012-03-28 09:53:16 +0200608#if (!defined(JEMALLOC_MUTEX_INIT_CB) && !defined(JEMALLOC_ZONE))
Jason Evansa0bf2422010-01-29 14:30:41 -0800609 /* Register fork handlers. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700610 if (pthread_atfork(jemalloc_prefork, jemalloc_postfork_parent,
611 jemalloc_postfork_child) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -0800612 malloc_write("<jemalloc>: Error in pthread_atfork()\n");
Jason Evansa0bf2422010-01-29 14:30:41 -0800613 if (opt_abort)
614 abort();
615 }
Jason Evans41b6afb2012-02-02 22:04:57 -0800616#endif
Jason Evans3c234352010-01-27 13:10:55 -0800617
Jason Evans03c22372010-01-03 12:10:42 -0800618 if (opt_stats_print) {
Jason Evans289053c2009-06-22 12:08:42 -0700619 /* Print statistics at exit. */
Jason Evansa0bf2422010-01-29 14:30:41 -0800620 if (atexit(stats_print_atexit) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -0800621 malloc_write("<jemalloc>: Error in atexit()\n");
Jason Evansa0bf2422010-01-29 14:30:41 -0800622 if (opt_abort)
623 abort();
624 }
Jason Evans289053c2009-06-22 12:08:42 -0700625 }
626
Mike Hommeyb8325f92012-04-12 15:15:35 +0200627 if (base_boot()) {
Jason Evansa0bf2422010-01-29 14:30:41 -0800628 malloc_mutex_unlock(&init_lock);
629 return (true);
630 }
Jason Evansc9658dd2009-06-22 14:44:08 -0700631
Mike Hommeyb8325f92012-04-12 15:15:35 +0200632 if (chunk_boot0()) {
Jason Evans3c234352010-01-27 13:10:55 -0800633 malloc_mutex_unlock(&init_lock);
634 return (true);
635 }
636
Jason Evans41b6afb2012-02-02 22:04:57 -0800637 if (ctl_boot()) {
638 malloc_mutex_unlock(&init_lock);
639 return (true);
640 }
641
Jason Evans7372b152012-02-10 20:22:09 -0800642 if (config_prof)
643 prof_boot1();
Jason Evans3383af62010-02-11 08:59:06 -0800644
Jason Evansb1726102012-02-28 16:50:47 -0800645 arena_boot();
Jason Evans289053c2009-06-22 12:08:42 -0700646
Jason Evanscd9a1342012-03-21 18:33:03 -0700647 if (config_tcache && tcache_boot0()) {
Jason Evans84c8eef2011-03-16 10:30:13 -0700648 malloc_mutex_unlock(&init_lock);
649 return (true);
650 }
Jason Evans84cbbcb2009-12-29 00:09:15 -0800651
Jason Evanse476f8a2010-01-16 09:53:50 -0800652 if (huge_boot()) {
Jason Evansc9658dd2009-06-22 14:44:08 -0700653 malloc_mutex_unlock(&init_lock);
654 return (true);
655 }
Jason Evans289053c2009-06-22 12:08:42 -0700656
Jason Evans8e6f8b42011-11-03 18:40:03 -0700657 if (malloc_mutex_init(&arenas_lock))
658 return (true);
659
Jason Evansb7924f52009-06-23 19:01:18 -0700660 /*
661 * Create enough scaffolding to allow recursive allocation in
662 * malloc_ncpus().
663 */
664 narenas = 1;
665 arenas = init_arenas;
666 memset(arenas, 0, sizeof(arena_t *) * narenas);
667
668 /*
669 * Initialize one arena here. The rest are lazily created in
670 * choose_arena_hard().
671 */
672 arenas_extend(0);
673 if (arenas[0] == NULL) {
674 malloc_mutex_unlock(&init_lock);
675 return (true);
676 }
677
Jason Evanscd9a1342012-03-21 18:33:03 -0700678 /* Initialize allocation counters before any allocations can occur. */
679 if (config_stats && thread_allocated_tsd_boot()) {
680 malloc_mutex_unlock(&init_lock);
681 return (true);
682 }
Jason Evansb7924f52009-06-23 19:01:18 -0700683
Jason Evanscd9a1342012-03-21 18:33:03 -0700684 if (arenas_tsd_boot()) {
685 malloc_mutex_unlock(&init_lock);
686 return (true);
687 }
688
689 if (config_tcache && tcache_boot1()) {
690 malloc_mutex_unlock(&init_lock);
691 return (true);
692 }
693
Jason Evans122449b2012-04-06 00:35:09 -0700694 if (config_fill && quarantine_boot()) {
695 malloc_mutex_unlock(&init_lock);
696 return (true);
697 }
698
Jason Evans6da54182012-03-23 18:05:51 -0700699 if (config_prof && prof_boot2()) {
700 malloc_mutex_unlock(&init_lock);
701 return (true);
702 }
703
Jason Evansb7924f52009-06-23 19:01:18 -0700704 /* Get number of CPUs. */
Jason Evansb7924f52009-06-23 19:01:18 -0700705 malloc_mutex_unlock(&init_lock);
706 ncpus = malloc_ncpus();
707 malloc_mutex_lock(&init_lock);
708
Jason Evanscd9a1342012-03-21 18:33:03 -0700709 if (chunk_boot1()) {
710 malloc_mutex_unlock(&init_lock);
711 return (true);
712 }
713
Jason Evans633aaff2012-04-03 08:47:07 -0700714 if (mutex_boot()) {
715 malloc_mutex_unlock(&init_lock);
716 return (true);
717 }
718
Jason Evanse7339702010-10-23 18:37:06 -0700719 if (opt_narenas == 0) {
Jason Evans289053c2009-06-22 12:08:42 -0700720 /*
Jason Evans5463a522009-12-29 00:09:15 -0800721 * For SMP systems, create more than one arena per CPU by
722 * default.
Jason Evans289053c2009-06-22 12:08:42 -0700723 */
Jason Evanse7339702010-10-23 18:37:06 -0700724 if (ncpus > 1)
725 opt_narenas = ncpus << 2;
726 else
727 opt_narenas = 1;
Jason Evans289053c2009-06-22 12:08:42 -0700728 }
Jason Evanse7339702010-10-23 18:37:06 -0700729 narenas = opt_narenas;
730 /*
731 * Make sure that the arenas array can be allocated. In practice, this
732 * limit is enough to allow the allocator to function, but the ctl
733 * machinery will fail to allocate memory at far lower limits.
734 */
735 if (narenas > chunksize / sizeof(arena_t *)) {
Jason Evanse7339702010-10-23 18:37:06 -0700736 narenas = chunksize / sizeof(arena_t *);
Jason Evansd81e4bd2012-03-06 14:57:45 -0800737 malloc_printf("<jemalloc>: Reducing narenas to limit (%d)\n",
738 narenas);
Jason Evans289053c2009-06-22 12:08:42 -0700739 }
Jason Evans289053c2009-06-22 12:08:42 -0700740
Jason Evans289053c2009-06-22 12:08:42 -0700741 /* Allocate and initialize arenas. */
742 arenas = (arena_t **)base_alloc(sizeof(arena_t *) * narenas);
743 if (arenas == NULL) {
744 malloc_mutex_unlock(&init_lock);
745 return (true);
746 }
747 /*
748 * Zero the array. In practice, this should always be pre-zeroed,
749 * since it was just mmap()ed, but let's be sure.
750 */
751 memset(arenas, 0, sizeof(arena_t *) * narenas);
Jason Evansb7924f52009-06-23 19:01:18 -0700752 /* Copy the pointer to the one arena that was already initialized. */
753 arenas[0] = init_arenas[0];
Jason Evans289053c2009-06-22 12:08:42 -0700754
755 malloc_initialized = true;
756 malloc_mutex_unlock(&init_lock);
757 return (false);
758}
759
760/*
Jason Evanse476f8a2010-01-16 09:53:50 -0800761 * End initialization functions.
Jason Evans289053c2009-06-22 12:08:42 -0700762 */
763/******************************************************************************/
764/*
765 * Begin malloc(3)-compatible functions.
766 */
767
Jason Evans9ad48232010-01-03 11:59:20 -0800768JEMALLOC_ATTR(malloc)
Jason Evanse476f8a2010-01-16 09:53:50 -0800769JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -0700770void *
Jason Evans0a5489e2012-03-01 17:19:20 -0800771je_malloc(size_t size)
Jason Evans289053c2009-06-22 12:08:42 -0700772{
773 void *ret;
Jason Evans7372b152012-02-10 20:22:09 -0800774 size_t usize;
Jason Evans9225a192012-03-23 15:39:07 -0700775 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700776
777 if (malloc_init()) {
778 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700779 goto label_oom;
Jason Evans289053c2009-06-22 12:08:42 -0700780 }
781
Jason Evansc90ad712012-02-28 20:31:37 -0800782 if (size == 0)
783 size = 1;
Jason Evans289053c2009-06-22 12:08:42 -0700784
Jason Evans7372b152012-02-10 20:22:09 -0800785 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -0700786 usize = s2u(size);
Jason Evansa5070042011-08-12 13:48:27 -0700787 PROF_ALLOC_PREP(1, usize, cnt);
788 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700789 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700790 goto label_oom;
Jason Evans0b270a92010-03-31 16:45:04 -0700791 }
Jason Evans93443682010-10-20 17:39:18 -0700792 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <=
Jason Evansb1726102012-02-28 16:50:47 -0800793 SMALL_MAXCLASS) {
794 ret = imalloc(SMALL_MAXCLASS+1);
Jason Evans0b270a92010-03-31 16:45:04 -0700795 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700796 arena_prof_promoted(ret, usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700797 } else
798 ret = imalloc(size);
Jason Evans7372b152012-02-10 20:22:09 -0800799 } else {
Jason Evans122449b2012-04-06 00:35:09 -0700800 if (config_stats || (config_valgrind && opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -0800801 usize = s2u(size);
Jason Evans0b270a92010-03-31 16:45:04 -0700802 ret = imalloc(size);
Jason Evans93443682010-10-20 17:39:18 -0700803 }
Jason Evans289053c2009-06-22 12:08:42 -0700804
Jason Evansa1ee7832012-04-10 15:07:44 -0700805label_oom:
Jason Evans289053c2009-06-22 12:08:42 -0700806 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -0800807 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -0800808 malloc_write("<jemalloc>: Error in malloc(): "
809 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -0700810 abort();
811 }
812 errno = ENOMEM;
813 }
Jason Evans7372b152012-02-10 20:22:09 -0800814 if (config_prof && opt_prof && ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700815 prof_malloc(ret, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -0800816 if (config_stats && ret != NULL) {
Jason Evans122449b2012-04-06 00:35:09 -0700817 assert(usize == isalloc(ret, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -0700818 thread_allocated_tsd_get()->allocated += usize;
Jason Evans93443682010-10-20 17:39:18 -0700819 }
Jason Evansb1476112012-04-05 13:36:17 -0700820 UTRACE(0, size, ret);
Jason Evans122449b2012-04-06 00:35:09 -0700821 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, usize, false);
Jason Evans289053c2009-06-22 12:08:42 -0700822 return (ret);
823}
824
Jason Evans9ad48232010-01-03 11:59:20 -0800825JEMALLOC_ATTR(nonnull(1))
Jason Evansa5070042011-08-12 13:48:27 -0700826#ifdef JEMALLOC_PROF
827/*
Jason Evans7372b152012-02-10 20:22:09 -0800828 * Avoid any uncertainty as to how many backtrace frames to ignore in
Jason Evansa5070042011-08-12 13:48:27 -0700829 * PROF_ALLOC_PREP().
830 */
831JEMALLOC_ATTR(noinline)
832#endif
833static int
Jason Evans59656312012-02-28 21:37:38 -0800834imemalign(void **memptr, size_t alignment, size_t size,
Jason Evans0a0bbf62012-03-13 12:55:21 -0700835 size_t min_alignment)
Jason Evans289053c2009-06-22 12:08:42 -0700836{
837 int ret;
Jason Evans7372b152012-02-10 20:22:09 -0800838 size_t usize;
Jason Evans38d92102011-03-23 00:37:29 -0700839 void *result;
Jason Evans9225a192012-03-23 15:39:07 -0700840 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700841
Jason Evans0a0bbf62012-03-13 12:55:21 -0700842 assert(min_alignment != 0);
843
Jason Evans289053c2009-06-22 12:08:42 -0700844 if (malloc_init())
845 result = NULL;
846 else {
Jason Evansc90ad712012-02-28 20:31:37 -0800847 if (size == 0)
848 size = 1;
Jason Evansf2518142009-12-29 00:09:15 -0800849
Jason Evans289053c2009-06-22 12:08:42 -0700850 /* Make sure that alignment is a large enough power of 2. */
851 if (((alignment - 1) & alignment) != 0
Jason Evans0a0bbf62012-03-13 12:55:21 -0700852 || (alignment < min_alignment)) {
Jason Evans7372b152012-02-10 20:22:09 -0800853 if (config_xmalloc && opt_xmalloc) {
Jason Evans0a0bbf62012-03-13 12:55:21 -0700854 malloc_write("<jemalloc>: Error allocating "
855 "aligned memory: invalid alignment\n");
Jason Evans289053c2009-06-22 12:08:42 -0700856 abort();
857 }
858 result = NULL;
859 ret = EINVAL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700860 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700861 }
862
Jason Evans5ff709c2012-04-11 18:13:45 -0700863 usize = sa2u(size, alignment);
Jason Evans38d92102011-03-23 00:37:29 -0700864 if (usize == 0) {
865 result = NULL;
866 ret = ENOMEM;
Jason Evansa1ee7832012-04-10 15:07:44 -0700867 goto label_return;
Jason Evans38d92102011-03-23 00:37:29 -0700868 }
869
Jason Evans7372b152012-02-10 20:22:09 -0800870 if (config_prof && opt_prof) {
Jason Evansa5070042011-08-12 13:48:27 -0700871 PROF_ALLOC_PREP(2, usize, cnt);
872 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700873 result = NULL;
874 ret = EINVAL;
875 } else {
876 if (prof_promote && (uintptr_t)cnt !=
Jason Evansb1726102012-02-28 16:50:47 -0800877 (uintptr_t)1U && usize <= SMALL_MAXCLASS) {
878 assert(sa2u(SMALL_MAXCLASS+1,
Jason Evans5ff709c2012-04-11 18:13:45 -0700879 alignment) != 0);
Jason Evansb1726102012-02-28 16:50:47 -0800880 result = ipalloc(sa2u(SMALL_MAXCLASS+1,
Jason Evans5ff709c2012-04-11 18:13:45 -0700881 alignment), alignment, false);
Jason Evans0b270a92010-03-31 16:45:04 -0700882 if (result != NULL) {
883 arena_prof_promoted(result,
Jason Evans93443682010-10-20 17:39:18 -0700884 usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700885 }
Jason Evans8e3c3c62010-09-17 15:46:18 -0700886 } else {
Jason Evans38d92102011-03-23 00:37:29 -0700887 result = ipalloc(usize, alignment,
Jason Evans8e3c3c62010-09-17 15:46:18 -0700888 false);
889 }
Jason Evans0b270a92010-03-31 16:45:04 -0700890 }
Jason Evans6109fe02010-02-10 10:37:56 -0800891 } else
Jason Evans38d92102011-03-23 00:37:29 -0700892 result = ipalloc(usize, alignment, false);
Jason Evans289053c2009-06-22 12:08:42 -0700893 }
894
895 if (result == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -0800896 if (config_xmalloc && opt_xmalloc) {
Jason Evans0a0bbf62012-03-13 12:55:21 -0700897 malloc_write("<jemalloc>: Error allocating aligned "
898 "memory: out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -0700899 abort();
900 }
901 ret = ENOMEM;
Jason Evansa1ee7832012-04-10 15:07:44 -0700902 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700903 }
904
905 *memptr = result;
906 ret = 0;
907
Jason Evansa1ee7832012-04-10 15:07:44 -0700908label_return:
Jason Evans7372b152012-02-10 20:22:09 -0800909 if (config_stats && result != NULL) {
Jason Evans122449b2012-04-06 00:35:09 -0700910 assert(usize == isalloc(result, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -0700911 thread_allocated_tsd_get()->allocated += usize;
Jason Evans93443682010-10-20 17:39:18 -0700912 }
Jason Evans7372b152012-02-10 20:22:09 -0800913 if (config_prof && opt_prof && result != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700914 prof_malloc(result, usize, cnt);
Jason Evansb1476112012-04-05 13:36:17 -0700915 UTRACE(0, size, result);
Jason Evans289053c2009-06-22 12:08:42 -0700916 return (ret);
917}
918
Jason Evansa5070042011-08-12 13:48:27 -0700919JEMALLOC_ATTR(nonnull(1))
920JEMALLOC_ATTR(visibility("default"))
921int
Jason Evans0a5489e2012-03-01 17:19:20 -0800922je_posix_memalign(void **memptr, size_t alignment, size_t size)
Jason Evansa5070042011-08-12 13:48:27 -0700923{
Jason Evans122449b2012-04-06 00:35:09 -0700924 int ret = imemalign(memptr, alignment, size, sizeof(void *));
925 JEMALLOC_VALGRIND_MALLOC(ret == 0, *memptr, isalloc(*memptr,
926 config_prof), false);
927 return (ret);
Jason Evans0a0bbf62012-03-13 12:55:21 -0700928}
929
930JEMALLOC_ATTR(malloc)
931JEMALLOC_ATTR(visibility("default"))
932void *
933je_aligned_alloc(size_t alignment, size_t size)
934{
935 void *ret;
936 int err;
937
938 if ((err = imemalign(&ret, alignment, size, 1)) != 0) {
939 ret = NULL;
940 errno = err;
941 }
Jason Evans122449b2012-04-06 00:35:09 -0700942 JEMALLOC_VALGRIND_MALLOC(err == 0, ret, isalloc(ret, config_prof),
943 false);
Jason Evans0a0bbf62012-03-13 12:55:21 -0700944 return (ret);
Jason Evansa5070042011-08-12 13:48:27 -0700945}
946
Jason Evans9ad48232010-01-03 11:59:20 -0800947JEMALLOC_ATTR(malloc)
Jason Evanse476f8a2010-01-16 09:53:50 -0800948JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -0700949void *
Jason Evans0a5489e2012-03-01 17:19:20 -0800950je_calloc(size_t num, size_t size)
Jason Evans289053c2009-06-22 12:08:42 -0700951{
952 void *ret;
953 size_t num_size;
Jason Evans7372b152012-02-10 20:22:09 -0800954 size_t usize;
Jason Evans9225a192012-03-23 15:39:07 -0700955 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700956
957 if (malloc_init()) {
958 num_size = 0;
959 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700960 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700961 }
962
963 num_size = num * size;
964 if (num_size == 0) {
Jason Evansc90ad712012-02-28 20:31:37 -0800965 if (num == 0 || size == 0)
Jason Evans289053c2009-06-22 12:08:42 -0700966 num_size = 1;
967 else {
968 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700969 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700970 }
971 /*
972 * Try to avoid division here. We know that it isn't possible to
973 * overflow during multiplication if neither operand uses any of the
974 * most significant half of the bits in a size_t.
975 */
976 } else if (((num | size) & (SIZE_T_MAX << (sizeof(size_t) << 2)))
977 && (num_size / size != num)) {
978 /* size_t overflow. */
979 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700980 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700981 }
982
Jason Evans7372b152012-02-10 20:22:09 -0800983 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -0700984 usize = s2u(num_size);
Jason Evansa5070042011-08-12 13:48:27 -0700985 PROF_ALLOC_PREP(1, usize, cnt);
986 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700987 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700988 goto label_return;
Jason Evans0b270a92010-03-31 16:45:04 -0700989 }
Jason Evans93443682010-10-20 17:39:18 -0700990 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize
Jason Evansb1726102012-02-28 16:50:47 -0800991 <= SMALL_MAXCLASS) {
992 ret = icalloc(SMALL_MAXCLASS+1);
Jason Evans0b270a92010-03-31 16:45:04 -0700993 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700994 arena_prof_promoted(ret, usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700995 } else
996 ret = icalloc(num_size);
Jason Evans7372b152012-02-10 20:22:09 -0800997 } else {
Jason Evans122449b2012-04-06 00:35:09 -0700998 if (config_stats || (config_valgrind && opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -0800999 usize = s2u(num_size);
Jason Evans0b270a92010-03-31 16:45:04 -07001000 ret = icalloc(num_size);
Jason Evans93443682010-10-20 17:39:18 -07001001 }
Jason Evans289053c2009-06-22 12:08:42 -07001002
Jason Evansa1ee7832012-04-10 15:07:44 -07001003label_return:
Jason Evans289053c2009-06-22 12:08:42 -07001004 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001005 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001006 malloc_write("<jemalloc>: Error in calloc(): out of "
1007 "memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001008 abort();
1009 }
1010 errno = ENOMEM;
1011 }
1012
Jason Evans7372b152012-02-10 20:22:09 -08001013 if (config_prof && opt_prof && ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001014 prof_malloc(ret, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -08001015 if (config_stats && ret != NULL) {
Jason Evans122449b2012-04-06 00:35:09 -07001016 assert(usize == isalloc(ret, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -07001017 thread_allocated_tsd_get()->allocated += usize;
Jason Evans93443682010-10-20 17:39:18 -07001018 }
Jason Evansb1476112012-04-05 13:36:17 -07001019 UTRACE(0, num_size, ret);
Jason Evans122449b2012-04-06 00:35:09 -07001020 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, usize, true);
Jason Evans289053c2009-06-22 12:08:42 -07001021 return (ret);
1022}
1023
Jason Evanse476f8a2010-01-16 09:53:50 -08001024JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001025void *
Jason Evans0a5489e2012-03-01 17:19:20 -08001026je_realloc(void *ptr, size_t size)
Jason Evans289053c2009-06-22 12:08:42 -07001027{
1028 void *ret;
Jason Evans7372b152012-02-10 20:22:09 -08001029 size_t usize;
Jason Evans93443682010-10-20 17:39:18 -07001030 size_t old_size = 0;
Jason Evans122449b2012-04-06 00:35:09 -07001031 size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evans9225a192012-03-23 15:39:07 -07001032 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
1033 prof_ctx_t *old_ctx JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans6109fe02010-02-10 10:37:56 -08001034
Jason Evans289053c2009-06-22 12:08:42 -07001035 if (size == 0) {
Jason Evansf081b882012-02-28 20:24:05 -08001036 if (ptr != NULL) {
1037 /* realloc(ptr, 0) is equivalent to free(p). */
Jason Evans122449b2012-04-06 00:35:09 -07001038 if (config_prof) {
1039 old_size = isalloc(ptr, true);
1040 if (config_valgrind && opt_valgrind)
1041 old_rzsize = p2rz(ptr);
1042 } else if (config_stats) {
1043 old_size = isalloc(ptr, false);
1044 if (config_valgrind && opt_valgrind)
1045 old_rzsize = u2rz(old_size);
1046 } else if (config_valgrind && opt_valgrind) {
1047 old_size = isalloc(ptr, false);
1048 old_rzsize = u2rz(old_size);
1049 }
Jason Evansf081b882012-02-28 20:24:05 -08001050 if (config_prof && opt_prof) {
1051 old_ctx = prof_ctx_get(ptr);
Jason Evans6109fe02010-02-10 10:37:56 -08001052 cnt = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001053 }
Jason Evans122449b2012-04-06 00:35:09 -07001054 iqalloc(ptr);
Jason Evans289053c2009-06-22 12:08:42 -07001055 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -07001056 goto label_return;
Jason Evansc90ad712012-02-28 20:31:37 -08001057 } else
1058 size = 1;
Jason Evans289053c2009-06-22 12:08:42 -07001059 }
1060
1061 if (ptr != NULL) {
Jason Evans41b6afb2012-02-02 22:04:57 -08001062 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans289053c2009-06-22 12:08:42 -07001063
Jason Evans122449b2012-04-06 00:35:09 -07001064 if (config_prof) {
1065 old_size = isalloc(ptr, true);
1066 if (config_valgrind && opt_valgrind)
1067 old_rzsize = p2rz(ptr);
1068 } else if (config_stats) {
1069 old_size = isalloc(ptr, false);
1070 if (config_valgrind && opt_valgrind)
1071 old_rzsize = u2rz(old_size);
1072 } else if (config_valgrind && opt_valgrind) {
1073 old_size = isalloc(ptr, false);
1074 old_rzsize = u2rz(old_size);
1075 }
Jason Evans7372b152012-02-10 20:22:09 -08001076 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001077 usize = s2u(size);
Jason Evans50651562010-04-13 16:13:54 -07001078 old_ctx = prof_ctx_get(ptr);
Jason Evansa5070042011-08-12 13:48:27 -07001079 PROF_ALLOC_PREP(1, usize, cnt);
1080 if (cnt == NULL) {
Jason Evans46405e62011-08-30 23:37:29 -07001081 old_ctx = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001082 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -07001083 goto label_oom;
Jason Evans6109fe02010-02-10 10:37:56 -08001084 }
Jason Evans0b270a92010-03-31 16:45:04 -07001085 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U &&
Jason Evansb1726102012-02-28 16:50:47 -08001086 usize <= SMALL_MAXCLASS) {
1087 ret = iralloc(ptr, SMALL_MAXCLASS+1, 0, 0,
Jason Evans8e3c3c62010-09-17 15:46:18 -07001088 false, false);
Jason Evans0b270a92010-03-31 16:45:04 -07001089 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001090 arena_prof_promoted(ret, usize);
Jason Evans46405e62011-08-30 23:37:29 -07001091 else
1092 old_ctx = NULL;
1093 } else {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001094 ret = iralloc(ptr, size, 0, 0, false, false);
Jason Evans46405e62011-08-30 23:37:29 -07001095 if (ret == NULL)
1096 old_ctx = NULL;
1097 }
Jason Evans7372b152012-02-10 20:22:09 -08001098 } else {
Jason Evans122449b2012-04-06 00:35:09 -07001099 if (config_stats || (config_valgrind && opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -08001100 usize = s2u(size);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001101 ret = iralloc(ptr, size, 0, 0, false, false);
Jason Evans93443682010-10-20 17:39:18 -07001102 }
Jason Evans289053c2009-06-22 12:08:42 -07001103
Jason Evansa1ee7832012-04-10 15:07:44 -07001104label_oom:
Jason Evans289053c2009-06-22 12:08:42 -07001105 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001106 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001107 malloc_write("<jemalloc>: Error in realloc(): "
1108 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001109 abort();
1110 }
1111 errno = ENOMEM;
1112 }
1113 } else {
Jason Evansf081b882012-02-28 20:24:05 -08001114 /* realloc(NULL, size) is equivalent to malloc(size). */
Jason Evans7372b152012-02-10 20:22:09 -08001115 if (config_prof && opt_prof)
Jason Evans50651562010-04-13 16:13:54 -07001116 old_ctx = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001117 if (malloc_init()) {
Jason Evans7372b152012-02-10 20:22:09 -08001118 if (config_prof && opt_prof)
Jason Evans6109fe02010-02-10 10:37:56 -08001119 cnt = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001120 ret = NULL;
1121 } else {
Jason Evans7372b152012-02-10 20:22:09 -08001122 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001123 usize = s2u(size);
Jason Evansa5070042011-08-12 13:48:27 -07001124 PROF_ALLOC_PREP(1, usize, cnt);
1125 if (cnt == NULL)
Jason Evans0b270a92010-03-31 16:45:04 -07001126 ret = NULL;
1127 else {
1128 if (prof_promote && (uintptr_t)cnt !=
Jason Evans93443682010-10-20 17:39:18 -07001129 (uintptr_t)1U && usize <=
Jason Evansb1726102012-02-28 16:50:47 -08001130 SMALL_MAXCLASS) {
1131 ret = imalloc(SMALL_MAXCLASS+1);
Jason Evans0b270a92010-03-31 16:45:04 -07001132 if (ret != NULL) {
1133 arena_prof_promoted(ret,
Jason Evans93443682010-10-20 17:39:18 -07001134 usize);
Jason Evans0b270a92010-03-31 16:45:04 -07001135 }
1136 } else
1137 ret = imalloc(size);
1138 }
Jason Evans7372b152012-02-10 20:22:09 -08001139 } else {
Jason Evans122449b2012-04-06 00:35:09 -07001140 if (config_stats || (config_valgrind &&
1141 opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -08001142 usize = s2u(size);
Jason Evans6109fe02010-02-10 10:37:56 -08001143 ret = imalloc(size);
Jason Evans93443682010-10-20 17:39:18 -07001144 }
Jason Evans6109fe02010-02-10 10:37:56 -08001145 }
Jason Evans569432c2009-12-29 00:09:15 -08001146
Jason Evans289053c2009-06-22 12:08:42 -07001147 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001148 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001149 malloc_write("<jemalloc>: Error in realloc(): "
1150 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001151 abort();
1152 }
1153 errno = ENOMEM;
1154 }
1155 }
1156
Jason Evansa1ee7832012-04-10 15:07:44 -07001157label_return:
Jason Evans7372b152012-02-10 20:22:09 -08001158 if (config_prof && opt_prof)
Jason Evanse4f78462010-10-22 10:45:59 -07001159 prof_realloc(ret, usize, cnt, old_size, old_ctx);
Jason Evans7372b152012-02-10 20:22:09 -08001160 if (config_stats && ret != NULL) {
Jason Evanscd9a1342012-03-21 18:33:03 -07001161 thread_allocated_t *ta;
Jason Evans122449b2012-04-06 00:35:09 -07001162 assert(usize == isalloc(ret, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -07001163 ta = thread_allocated_tsd_get();
1164 ta->allocated += usize;
1165 ta->deallocated += old_size;
Jason Evans93443682010-10-20 17:39:18 -07001166 }
Jason Evansb1476112012-04-05 13:36:17 -07001167 UTRACE(ptr, size, ret);
Jason Evans122449b2012-04-06 00:35:09 -07001168 JEMALLOC_VALGRIND_REALLOC(ret, usize, ptr, old_size, old_rzsize, false);
Jason Evans289053c2009-06-22 12:08:42 -07001169 return (ret);
1170}
1171
Jason Evanse476f8a2010-01-16 09:53:50 -08001172JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001173void
Jason Evans0a5489e2012-03-01 17:19:20 -08001174je_free(void *ptr)
Jason Evans289053c2009-06-22 12:08:42 -07001175{
1176
Jason Evansb1476112012-04-05 13:36:17 -07001177 UTRACE(ptr, 0, 0);
Jason Evansf0047372012-04-02 15:18:24 -07001178 if (ptr != NULL) {
1179 size_t usize;
Jason Evans122449b2012-04-06 00:35:09 -07001180 size_t rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evanse4f78462010-10-22 10:45:59 -07001181
Jason Evansf0047372012-04-02 15:18:24 -07001182 assert(malloc_initialized || IS_INITIALIZER);
1183
1184 if (config_prof && opt_prof) {
Jason Evans122449b2012-04-06 00:35:09 -07001185 usize = isalloc(ptr, config_prof);
Jason Evansf0047372012-04-02 15:18:24 -07001186 prof_free(ptr, usize);
Jason Evans122449b2012-04-06 00:35:09 -07001187 } else if (config_stats || config_valgrind)
1188 usize = isalloc(ptr, config_prof);
Jason Evansf0047372012-04-02 15:18:24 -07001189 if (config_stats)
1190 thread_allocated_tsd_get()->deallocated += usize;
Jason Evans122449b2012-04-06 00:35:09 -07001191 if (config_valgrind && opt_valgrind)
1192 rzsize = p2rz(ptr);
1193 iqalloc(ptr);
1194 JEMALLOC_VALGRIND_FREE(ptr, rzsize);
Jason Evansf0047372012-04-02 15:18:24 -07001195 }
Jason Evans289053c2009-06-22 12:08:42 -07001196}
1197
1198/*
1199 * End malloc(3)-compatible functions.
1200 */
1201/******************************************************************************/
1202/*
Jason Evans6a0d2912010-09-20 16:44:23 -07001203 * Begin non-standard override functions.
Jason Evans6a0d2912010-09-20 16:44:23 -07001204 */
Jason Evans6a0d2912010-09-20 16:44:23 -07001205
1206#ifdef JEMALLOC_OVERRIDE_MEMALIGN
1207JEMALLOC_ATTR(malloc)
1208JEMALLOC_ATTR(visibility("default"))
1209void *
Jason Evans0a5489e2012-03-01 17:19:20 -08001210je_memalign(size_t alignment, size_t size)
Jason Evans6a0d2912010-09-20 16:44:23 -07001211{
Jason Evans9225a192012-03-23 15:39:07 -07001212 void *ret JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans0a0bbf62012-03-13 12:55:21 -07001213 imemalign(&ret, alignment, size, 1);
Jason Evans122449b2012-04-06 00:35:09 -07001214 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, size, false);
Jason Evans6a0d2912010-09-20 16:44:23 -07001215 return (ret);
1216}
1217#endif
1218
1219#ifdef JEMALLOC_OVERRIDE_VALLOC
1220JEMALLOC_ATTR(malloc)
1221JEMALLOC_ATTR(visibility("default"))
1222void *
Jason Evans0a5489e2012-03-01 17:19:20 -08001223je_valloc(size_t size)
Jason Evans6a0d2912010-09-20 16:44:23 -07001224{
Jason Evans9225a192012-03-23 15:39:07 -07001225 void *ret JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evansae4c7b42012-04-02 07:04:34 -07001226 imemalign(&ret, PAGE, size, 1);
Jason Evans122449b2012-04-06 00:35:09 -07001227 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, size, false);
Jason Evans6a0d2912010-09-20 16:44:23 -07001228 return (ret);
1229}
1230#endif
1231
Mike Hommey5c89c502012-03-26 17:46:57 +02001232/*
1233 * is_malloc(je_malloc) is some macro magic to detect if jemalloc_defs.h has
1234 * #define je_malloc malloc
1235 */
1236#define malloc_is_malloc 1
1237#define is_malloc_(a) malloc_is_ ## a
1238#define is_malloc(a) is_malloc_(a)
1239
1240#if ((is_malloc(je_malloc) == 1) && defined(__GLIBC__) && !defined(__UCLIBC__))
Jason Evans4bb09832012-02-29 10:37:27 -08001241/*
1242 * glibc provides the RTLD_DEEPBIND flag for dlopen which can make it possible
1243 * to inconsistently reference libc's malloc(3)-compatible functions
1244 * (https://bugzilla.mozilla.org/show_bug.cgi?id=493541).
1245 *
Mike Hommey3c2ba0d2012-03-27 14:20:13 +02001246 * These definitions interpose hooks in glibc. The functions are actually
Jason Evans4bb09832012-02-29 10:37:27 -08001247 * passed an extra argument for the caller return address, which will be
1248 * ignored.
1249 */
1250JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001251void (* const __free_hook)(void *ptr) = je_free;
Jason Evans4bb09832012-02-29 10:37:27 -08001252
1253JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001254void *(* const __malloc_hook)(size_t size) = je_malloc;
Jason Evans4bb09832012-02-29 10:37:27 -08001255
1256JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001257void *(* const __realloc_hook)(void *ptr, size_t size) = je_realloc;
Jason Evans4bb09832012-02-29 10:37:27 -08001258
1259JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001260void *(* const __memalign_hook)(size_t alignment, size_t size) = je_memalign;
Jason Evans4bb09832012-02-29 10:37:27 -08001261#endif
1262
Jason Evans6a0d2912010-09-20 16:44:23 -07001263/*
1264 * End non-standard override functions.
1265 */
1266/******************************************************************************/
1267/*
Jason Evans289053c2009-06-22 12:08:42 -07001268 * Begin non-standard functions.
1269 */
1270
Jason Evanse476f8a2010-01-16 09:53:50 -08001271JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001272size_t
Jason Evans0a5489e2012-03-01 17:19:20 -08001273je_malloc_usable_size(const void *ptr)
Jason Evans289053c2009-06-22 12:08:42 -07001274{
Jason Evans569432c2009-12-29 00:09:15 -08001275 size_t ret;
Jason Evans289053c2009-06-22 12:08:42 -07001276
Jason Evans41b6afb2012-02-02 22:04:57 -08001277 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001278
Jason Evans7372b152012-02-10 20:22:09 -08001279 if (config_ivsalloc)
Jason Evans122449b2012-04-06 00:35:09 -07001280 ret = ivsalloc(ptr, config_prof);
Jason Evans2465bdf2012-03-26 13:13:55 -07001281 else
Jason Evans122449b2012-04-06 00:35:09 -07001282 ret = (ptr != NULL) ? isalloc(ptr, config_prof) : 0;
Jason Evans289053c2009-06-22 12:08:42 -07001283
Jason Evans569432c2009-12-29 00:09:15 -08001284 return (ret);
Jason Evans289053c2009-06-22 12:08:42 -07001285}
1286
Jason Evans4201af02010-01-24 02:53:40 -08001287JEMALLOC_ATTR(visibility("default"))
1288void
Jason Evans0a5489e2012-03-01 17:19:20 -08001289je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
1290 const char *opts)
Jason Evans4201af02010-01-24 02:53:40 -08001291{
1292
Jason Evans698805c2010-03-03 17:45:38 -08001293 stats_print(write_cb, cbopaque, opts);
Jason Evans4201af02010-01-24 02:53:40 -08001294}
1295
Jason Evans3c234352010-01-27 13:10:55 -08001296JEMALLOC_ATTR(visibility("default"))
1297int
Jason Evans0a5489e2012-03-01 17:19:20 -08001298je_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
Jason Evans3c234352010-01-27 13:10:55 -08001299 size_t newlen)
1300{
1301
Jason Evans95833312010-01-27 13:45:21 -08001302 if (malloc_init())
1303 return (EAGAIN);
1304
Jason Evans3c234352010-01-27 13:10:55 -08001305 return (ctl_byname(name, oldp, oldlenp, newp, newlen));
1306}
1307
1308JEMALLOC_ATTR(visibility("default"))
1309int
Jason Evans0a5489e2012-03-01 17:19:20 -08001310je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp)
Jason Evans3c234352010-01-27 13:10:55 -08001311{
1312
Jason Evans95833312010-01-27 13:45:21 -08001313 if (malloc_init())
1314 return (EAGAIN);
1315
Jason Evans3c234352010-01-27 13:10:55 -08001316 return (ctl_nametomib(name, mibp, miblenp));
1317}
1318
1319JEMALLOC_ATTR(visibility("default"))
1320int
Jason Evans0a5489e2012-03-01 17:19:20 -08001321je_mallctlbymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1322 void *newp, size_t newlen)
Jason Evans3c234352010-01-27 13:10:55 -08001323{
1324
Jason Evans95833312010-01-27 13:45:21 -08001325 if (malloc_init())
1326 return (EAGAIN);
1327
Jason Evans3c234352010-01-27 13:10:55 -08001328 return (ctl_bymib(mib, miblen, oldp, oldlenp, newp, newlen));
1329}
1330
Jason Evans7e77eaf2012-03-02 17:47:37 -08001331/*
1332 * End non-standard functions.
1333 */
1334/******************************************************************************/
1335/*
1336 * Begin experimental functions.
1337 */
1338#ifdef JEMALLOC_EXPERIMENTAL
1339
Jason Evans8e3c3c62010-09-17 15:46:18 -07001340JEMALLOC_INLINE void *
Jason Evans38d92102011-03-23 00:37:29 -07001341iallocm(size_t usize, size_t alignment, bool zero)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001342{
1343
Jason Evans5ff709c2012-04-11 18:13:45 -07001344 assert(usize == ((alignment == 0) ? s2u(usize) : sa2u(usize,
1345 alignment)));
Jason Evans38d92102011-03-23 00:37:29 -07001346
Jason Evans8e3c3c62010-09-17 15:46:18 -07001347 if (alignment != 0)
Jason Evans38d92102011-03-23 00:37:29 -07001348 return (ipalloc(usize, alignment, zero));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001349 else if (zero)
Jason Evans38d92102011-03-23 00:37:29 -07001350 return (icalloc(usize));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001351 else
Jason Evans38d92102011-03-23 00:37:29 -07001352 return (imalloc(usize));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001353}
1354
Jason Evans6a0d2912010-09-20 16:44:23 -07001355JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001356JEMALLOC_ATTR(visibility("default"))
1357int
Jason Evans0a5489e2012-03-01 17:19:20 -08001358je_allocm(void **ptr, size_t *rsize, size_t size, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001359{
1360 void *p;
Jason Evans93443682010-10-20 17:39:18 -07001361 size_t usize;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001362 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1363 & (SIZE_T_MAX-1));
1364 bool zero = flags & ALLOCM_ZERO;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001365 prof_thr_cnt_t *cnt;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001366
1367 assert(ptr != NULL);
1368 assert(size != 0);
1369
1370 if (malloc_init())
Jason Evansa1ee7832012-04-10 15:07:44 -07001371 goto label_oom;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001372
Jason Evans5ff709c2012-04-11 18:13:45 -07001373 usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment);
Jason Evans38d92102011-03-23 00:37:29 -07001374 if (usize == 0)
Jason Evansa1ee7832012-04-10 15:07:44 -07001375 goto label_oom;
Jason Evans38d92102011-03-23 00:37:29 -07001376
Jason Evans7372b152012-02-10 20:22:09 -08001377 if (config_prof && opt_prof) {
Jason Evansa5070042011-08-12 13:48:27 -07001378 PROF_ALLOC_PREP(1, usize, cnt);
1379 if (cnt == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001380 goto label_oom;
Jason Evans93443682010-10-20 17:39:18 -07001381 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <=
Jason Evansb1726102012-02-28 16:50:47 -08001382 SMALL_MAXCLASS) {
Jason Evans38d92102011-03-23 00:37:29 -07001383 size_t usize_promoted = (alignment == 0) ?
Jason Evansb1726102012-02-28 16:50:47 -08001384 s2u(SMALL_MAXCLASS+1) : sa2u(SMALL_MAXCLASS+1,
Jason Evans5ff709c2012-04-11 18:13:45 -07001385 alignment);
Jason Evans38d92102011-03-23 00:37:29 -07001386 assert(usize_promoted != 0);
1387 p = iallocm(usize_promoted, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001388 if (p == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001389 goto label_oom;
Jason Evans93443682010-10-20 17:39:18 -07001390 arena_prof_promoted(p, usize);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001391 } else {
Jason Evans38d92102011-03-23 00:37:29 -07001392 p = iallocm(usize, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001393 if (p == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001394 goto label_oom;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001395 }
Jason Evans749c2a02011-08-12 18:37:54 -07001396 prof_malloc(p, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -08001397 } else {
Jason Evans38d92102011-03-23 00:37:29 -07001398 p = iallocm(usize, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001399 if (p == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001400 goto label_oom;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001401 }
Jason Evans7372b152012-02-10 20:22:09 -08001402 if (rsize != NULL)
1403 *rsize = usize;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001404
1405 *ptr = p;
Jason Evans7372b152012-02-10 20:22:09 -08001406 if (config_stats) {
Jason Evans122449b2012-04-06 00:35:09 -07001407 assert(usize == isalloc(p, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -07001408 thread_allocated_tsd_get()->allocated += usize;
Jason Evans7372b152012-02-10 20:22:09 -08001409 }
Jason Evansb1476112012-04-05 13:36:17 -07001410 UTRACE(0, size, p);
Jason Evans122449b2012-04-06 00:35:09 -07001411 JEMALLOC_VALGRIND_MALLOC(true, p, usize, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001412 return (ALLOCM_SUCCESS);
Jason Evansa1ee7832012-04-10 15:07:44 -07001413label_oom:
Jason Evans7372b152012-02-10 20:22:09 -08001414 if (config_xmalloc && opt_xmalloc) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001415 malloc_write("<jemalloc>: Error in allocm(): "
1416 "out of memory\n");
1417 abort();
1418 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001419 *ptr = NULL;
Jason Evansb1476112012-04-05 13:36:17 -07001420 UTRACE(0, size, 0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001421 return (ALLOCM_ERR_OOM);
1422}
1423
Jason Evans6a0d2912010-09-20 16:44:23 -07001424JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001425JEMALLOC_ATTR(visibility("default"))
1426int
Jason Evans0a5489e2012-03-01 17:19:20 -08001427je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001428{
1429 void *p, *q;
Jason Evans93443682010-10-20 17:39:18 -07001430 size_t usize;
Jason Evans93443682010-10-20 17:39:18 -07001431 size_t old_size;
Jason Evans122449b2012-04-06 00:35:09 -07001432 size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001433 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1434 & (SIZE_T_MAX-1));
1435 bool zero = flags & ALLOCM_ZERO;
1436 bool no_move = flags & ALLOCM_NO_MOVE;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001437 prof_thr_cnt_t *cnt;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001438
1439 assert(ptr != NULL);
1440 assert(*ptr != NULL);
1441 assert(size != 0);
1442 assert(SIZE_T_MAX - size >= extra);
Jason Evans41b6afb2012-02-02 22:04:57 -08001443 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001444
1445 p = *ptr;
Jason Evans7372b152012-02-10 20:22:09 -08001446 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001447 /*
1448 * usize isn't knowable before iralloc() returns when extra is
1449 * non-zero. Therefore, compute its maximum possible value and
Jason Evansa5070042011-08-12 13:48:27 -07001450 * use that in PROF_ALLOC_PREP() to decide whether to capture a
Jason Evans93443682010-10-20 17:39:18 -07001451 * backtrace. prof_realloc() will use the actual usize to
1452 * decide whether to sample.
1453 */
1454 size_t max_usize = (alignment == 0) ? s2u(size+extra) :
Jason Evans5ff709c2012-04-11 18:13:45 -07001455 sa2u(size+extra, alignment);
Jason Evans46405e62011-08-30 23:37:29 -07001456 prof_ctx_t *old_ctx = prof_ctx_get(p);
Jason Evans122449b2012-04-06 00:35:09 -07001457 old_size = isalloc(p, true);
1458 if (config_valgrind && opt_valgrind)
1459 old_rzsize = p2rz(p);
Jason Evansa5070042011-08-12 13:48:27 -07001460 PROF_ALLOC_PREP(1, max_usize, cnt);
1461 if (cnt == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001462 goto label_oom;
Jason Evans183ba502011-08-11 22:51:00 -07001463 /*
1464 * Use minimum usize to determine whether promotion may happen.
1465 */
1466 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U
Jason Evans5ff709c2012-04-11 18:13:45 -07001467 && ((alignment == 0) ? s2u(size) : sa2u(size, alignment))
1468 <= SMALL_MAXCLASS) {
Jason Evansb1726102012-02-28 16:50:47 -08001469 q = iralloc(p, SMALL_MAXCLASS+1, (SMALL_MAXCLASS+1 >=
1470 size+extra) ? 0 : size+extra - (SMALL_MAXCLASS+1),
Jason Evans8e3c3c62010-09-17 15:46:18 -07001471 alignment, zero, no_move);
1472 if (q == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001473 goto label_err;
Jason Evansae4c7b42012-04-02 07:04:34 -07001474 if (max_usize < PAGE) {
Jason Evans183ba502011-08-11 22:51:00 -07001475 usize = max_usize;
1476 arena_prof_promoted(q, usize);
Jason Evansb493ce22011-08-12 11:28:47 -07001477 } else
Jason Evans122449b2012-04-06 00:35:09 -07001478 usize = isalloc(q, config_prof);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001479 } else {
1480 q = iralloc(p, size, extra, alignment, zero, no_move);
1481 if (q == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001482 goto label_err;
Jason Evans122449b2012-04-06 00:35:09 -07001483 usize = isalloc(q, config_prof);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001484 }
Jason Evanse4f78462010-10-22 10:45:59 -07001485 prof_realloc(q, usize, cnt, old_size, old_ctx);
Jason Evanseacb8962011-03-23 00:30:30 -07001486 if (rsize != NULL)
1487 *rsize = usize;
Jason Evans7372b152012-02-10 20:22:09 -08001488 } else {
Jason Evans122449b2012-04-06 00:35:09 -07001489 if (config_stats) {
1490 old_size = isalloc(p, false);
1491 if (config_valgrind && opt_valgrind)
1492 old_rzsize = u2rz(old_size);
1493 } else if (config_valgrind && opt_valgrind) {
1494 old_size = isalloc(p, false);
1495 old_rzsize = u2rz(old_size);
1496 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001497 q = iralloc(p, size, extra, alignment, zero, no_move);
1498 if (q == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001499 goto label_err;
Jason Evans7372b152012-02-10 20:22:09 -08001500 if (config_stats)
Jason Evans122449b2012-04-06 00:35:09 -07001501 usize = isalloc(q, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001502 if (rsize != NULL) {
1503 if (config_stats == false)
Jason Evans122449b2012-04-06 00:35:09 -07001504 usize = isalloc(q, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001505 *rsize = usize;
Jason Evans93443682010-10-20 17:39:18 -07001506 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001507 }
1508
1509 *ptr = q;
Jason Evanscd9a1342012-03-21 18:33:03 -07001510 if (config_stats) {
1511 thread_allocated_t *ta;
1512 ta = thread_allocated_tsd_get();
1513 ta->allocated += usize;
1514 ta->deallocated += old_size;
1515 }
Jason Evansb1476112012-04-05 13:36:17 -07001516 UTRACE(p, size, q);
Jason Evans122449b2012-04-06 00:35:09 -07001517 JEMALLOC_VALGRIND_REALLOC(q, usize, p, old_size, old_rzsize, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001518 return (ALLOCM_SUCCESS);
Jason Evansa1ee7832012-04-10 15:07:44 -07001519label_err:
Jason Evansb1476112012-04-05 13:36:17 -07001520 if (no_move) {
1521 UTRACE(p, size, q);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001522 return (ALLOCM_ERR_NOT_MOVED);
Jason Evansb1476112012-04-05 13:36:17 -07001523 }
Jason Evansa1ee7832012-04-10 15:07:44 -07001524label_oom:
Jason Evans7372b152012-02-10 20:22:09 -08001525 if (config_xmalloc && opt_xmalloc) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001526 malloc_write("<jemalloc>: Error in rallocm(): "
1527 "out of memory\n");
1528 abort();
1529 }
Jason Evansb1476112012-04-05 13:36:17 -07001530 UTRACE(p, size, 0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001531 return (ALLOCM_ERR_OOM);
1532}
1533
Jason Evans6a0d2912010-09-20 16:44:23 -07001534JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001535JEMALLOC_ATTR(visibility("default"))
1536int
Jason Evans0a5489e2012-03-01 17:19:20 -08001537je_sallocm(const void *ptr, size_t *rsize, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001538{
1539 size_t sz;
1540
Jason Evans41b6afb2012-02-02 22:04:57 -08001541 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001542
Jason Evans7372b152012-02-10 20:22:09 -08001543 if (config_ivsalloc)
Jason Evans122449b2012-04-06 00:35:09 -07001544 sz = ivsalloc(ptr, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001545 else {
1546 assert(ptr != NULL);
Jason Evans122449b2012-04-06 00:35:09 -07001547 sz = isalloc(ptr, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001548 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001549 assert(rsize != NULL);
1550 *rsize = sz;
1551
1552 return (ALLOCM_SUCCESS);
1553}
1554
Jason Evans6a0d2912010-09-20 16:44:23 -07001555JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001556JEMALLOC_ATTR(visibility("default"))
1557int
Jason Evans0a5489e2012-03-01 17:19:20 -08001558je_dallocm(void *ptr, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001559{
Jason Evanse4f78462010-10-22 10:45:59 -07001560 size_t usize;
Jason Evans122449b2012-04-06 00:35:09 -07001561 size_t rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001562
1563 assert(ptr != NULL);
Jason Evans41b6afb2012-02-02 22:04:57 -08001564 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001565
Jason Evansb1476112012-04-05 13:36:17 -07001566 UTRACE(ptr, 0, 0);
Jason Evans122449b2012-04-06 00:35:09 -07001567 if (config_stats || config_valgrind)
1568 usize = isalloc(ptr, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001569 if (config_prof && opt_prof) {
Jason Evans122449b2012-04-06 00:35:09 -07001570 if (config_stats == false && config_valgrind == false)
1571 usize = isalloc(ptr, config_prof);
Jason Evanse4f78462010-10-22 10:45:59 -07001572 prof_free(ptr, usize);
1573 }
Jason Evans7372b152012-02-10 20:22:09 -08001574 if (config_stats)
Jason Evanscd9a1342012-03-21 18:33:03 -07001575 thread_allocated_tsd_get()->deallocated += usize;
Jason Evans122449b2012-04-06 00:35:09 -07001576 if (config_valgrind && opt_valgrind)
1577 rzsize = p2rz(ptr);
1578 iqalloc(ptr);
1579 JEMALLOC_VALGRIND_FREE(ptr, rzsize);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001580
1581 return (ALLOCM_SUCCESS);
1582}
1583
Jason Evans7e15dab2012-02-29 12:56:37 -08001584JEMALLOC_ATTR(visibility("default"))
1585int
Jason Evans0a5489e2012-03-01 17:19:20 -08001586je_nallocm(size_t *rsize, size_t size, int flags)
Jason Evans7e15dab2012-02-29 12:56:37 -08001587{
1588 size_t usize;
1589 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1590 & (SIZE_T_MAX-1));
1591
1592 assert(size != 0);
1593
1594 if (malloc_init())
1595 return (ALLOCM_ERR_OOM);
1596
Jason Evans5ff709c2012-04-11 18:13:45 -07001597 usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment);
Jason Evans7e15dab2012-02-29 12:56:37 -08001598 if (usize == 0)
1599 return (ALLOCM_ERR_OOM);
1600
1601 if (rsize != NULL)
1602 *rsize = usize;
1603 return (ALLOCM_SUCCESS);
1604}
1605
Jason Evans7e77eaf2012-03-02 17:47:37 -08001606#endif
Jason Evans289053c2009-06-22 12:08:42 -07001607/*
Jason Evans7e77eaf2012-03-02 17:47:37 -08001608 * End experimental functions.
Jason Evans289053c2009-06-22 12:08:42 -07001609 */
1610/******************************************************************************/
Jason Evans289053c2009-06-22 12:08:42 -07001611/*
1612 * The following functions are used by threading libraries for protection of
Jason Evans28177d42010-09-20 11:24:24 -07001613 * malloc during fork().
Jason Evans289053c2009-06-22 12:08:42 -07001614 */
1615
Jason Evans41b6afb2012-02-02 22:04:57 -08001616#ifndef JEMALLOC_MUTEX_INIT_CB
Jason Evans2dbecf12010-09-05 10:35:13 -07001617void
Jason Evans804c9ec2009-06-22 17:44:33 -07001618jemalloc_prefork(void)
Jason Evans41b6afb2012-02-02 22:04:57 -08001619#else
1620void
1621_malloc_prefork(void)
1622#endif
Jason Evans289053c2009-06-22 12:08:42 -07001623{
Jason Evansfbbb6242010-01-24 17:56:48 -08001624 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -07001625
1626 /* Acquire all mutexes in a safe order. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001627 malloc_mutex_prefork(&arenas_lock);
Jason Evansfbbb6242010-01-24 17:56:48 -08001628 for (i = 0; i < narenas; i++) {
1629 if (arenas[i] != NULL)
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001630 arena_prefork(arenas[i]);
Jason Evansfbbb6242010-01-24 17:56:48 -08001631 }
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001632 base_prefork();
1633 huge_prefork();
1634 chunk_dss_prefork();
Jason Evans289053c2009-06-22 12:08:42 -07001635}
1636
Jason Evans41b6afb2012-02-02 22:04:57 -08001637#ifndef JEMALLOC_MUTEX_INIT_CB
Jason Evans2dbecf12010-09-05 10:35:13 -07001638void
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001639jemalloc_postfork_parent(void)
Jason Evans41b6afb2012-02-02 22:04:57 -08001640#else
1641void
1642_malloc_postfork(void)
1643#endif
Jason Evans289053c2009-06-22 12:08:42 -07001644{
1645 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -07001646
1647 /* Release all mutexes, now that fork() has completed. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001648 chunk_dss_postfork_parent();
1649 huge_postfork_parent();
1650 base_postfork_parent();
Jason Evans289053c2009-06-22 12:08:42 -07001651 for (i = 0; i < narenas; i++) {
Jason Evansfbbb6242010-01-24 17:56:48 -08001652 if (arenas[i] != NULL)
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001653 arena_postfork_parent(arenas[i]);
Jason Evans289053c2009-06-22 12:08:42 -07001654 }
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001655 malloc_mutex_postfork_parent(&arenas_lock);
1656}
1657
1658void
1659jemalloc_postfork_child(void)
1660{
1661 unsigned i;
1662
1663 /* Release all mutexes, now that fork() has completed. */
1664 chunk_dss_postfork_child();
1665 huge_postfork_child();
1666 base_postfork_child();
1667 for (i = 0; i < narenas; i++) {
1668 if (arenas[i] != NULL)
1669 arena_postfork_child(arenas[i]);
1670 }
1671 malloc_mutex_postfork_child(&arenas_lock);
Jason Evans289053c2009-06-22 12:08:42 -07001672}
Jason Evans2dbecf12010-09-05 10:35:13 -07001673
1674/******************************************************************************/
Jason Evans01b3fe52012-04-03 09:28:00 -07001675/*
1676 * The following functions are used for TLS allocation/deallocation in static
1677 * binaries on FreeBSD. The primary difference between these and i[mcd]alloc()
1678 * is that these avoid accessing TLS variables.
1679 */
1680
1681static void *
1682a0alloc(size_t size, bool zero)
1683{
1684
1685 if (malloc_init())
1686 return (NULL);
1687
1688 if (size == 0)
1689 size = 1;
1690
1691 if (size <= arena_maxclass)
1692 return (arena_malloc(arenas[0], size, zero, false));
1693 else
1694 return (huge_malloc(size, zero));
1695}
1696
1697void *
1698a0malloc(size_t size)
1699{
1700
1701 return (a0alloc(size, false));
1702}
1703
1704void *
1705a0calloc(size_t num, size_t size)
1706{
1707
1708 return (a0alloc(num * size, true));
1709}
1710
1711void
1712a0free(void *ptr)
1713{
1714 arena_chunk_t *chunk;
1715
1716 if (ptr == NULL)
1717 return;
1718
1719 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1720 if (chunk != ptr)
1721 arena_dalloc(chunk->arena, chunk, ptr, false);
1722 else
1723 huge_dalloc(ptr, true);
1724}
1725
1726/******************************************************************************/