blob: 6669c110b5745ab8257b9257a70cf2ab208ccad7 [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 /*
Jason Evans7ca0fdf2012-04-12 20:20:58 -0700504 * Chunks always require at least one header page, plus
505 * one data page in the absence of redzones, or three
506 * pages in the presence of redzones. In order to
507 * simplify options processing, fix the limit based on
508 * config_fill.
Jason Evanse7339702010-10-23 18:37:06 -0700509 */
Jason Evans7ca0fdf2012-04-12 20:20:58 -0700510 CONF_HANDLE_SIZE_T(opt_lg_chunk, lg_chunk, LG_PAGE +
511 (config_fill ? 2 : 1), (sizeof(size_t) << 3) - 1)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800512 CONF_HANDLE_SIZE_T(opt_narenas, narenas, 1, SIZE_T_MAX)
513 CONF_HANDLE_SSIZE_T(opt_lg_dirty_mult, lg_dirty_mult,
514 -1, (sizeof(size_t) << 3) - 1)
515 CONF_HANDLE_BOOL(opt_stats_print, stats_print)
Jason Evans7372b152012-02-10 20:22:09 -0800516 if (config_fill) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800517 CONF_HANDLE_BOOL(opt_junk, junk)
Jason Evans122449b2012-04-06 00:35:09 -0700518 CONF_HANDLE_SIZE_T(opt_quarantine, quarantine,
519 0, SIZE_T_MAX)
520 CONF_HANDLE_BOOL(opt_redzone, redzone)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800521 CONF_HANDLE_BOOL(opt_zero, zero)
Jason Evans7372b152012-02-10 20:22:09 -0800522 }
Jason Evansb1476112012-04-05 13:36:17 -0700523 if (config_utrace) {
524 CONF_HANDLE_BOOL(opt_utrace, utrace)
525 }
Jason Evans122449b2012-04-06 00:35:09 -0700526 if (config_valgrind) {
527 bool hit;
528 CONF_HANDLE_BOOL_HIT(opt_valgrind,
529 valgrind, hit)
530 if (config_fill && opt_valgrind && hit) {
531 opt_junk = false;
532 opt_zero = false;
533 if (opt_quarantine == 0) {
534 opt_quarantine =
535 JEMALLOC_VALGRIND_QUARANTINE_DEFAULT;
536 }
537 opt_redzone = true;
538 }
539 if (hit)
540 continue;
541 }
Jason Evans7372b152012-02-10 20:22:09 -0800542 if (config_xmalloc) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800543 CONF_HANDLE_BOOL(opt_xmalloc, xmalloc)
Jason Evans7372b152012-02-10 20:22:09 -0800544 }
545 if (config_tcache) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800546 CONF_HANDLE_BOOL(opt_tcache, tcache)
547 CONF_HANDLE_SSIZE_T(opt_lg_tcache_max,
548 lg_tcache_max, -1,
Jason Evans7372b152012-02-10 20:22:09 -0800549 (sizeof(size_t) << 3) - 1)
550 }
551 if (config_prof) {
Jason Evansd81e4bd2012-03-06 14:57:45 -0800552 CONF_HANDLE_BOOL(opt_prof, prof)
553 CONF_HANDLE_CHAR_P(opt_prof_prefix, prof_prefix,
554 "jeprof")
555 CONF_HANDLE_BOOL(opt_prof_active, prof_active)
556 CONF_HANDLE_SSIZE_T(opt_lg_prof_sample,
557 lg_prof_sample, 0,
Jason Evans7372b152012-02-10 20:22:09 -0800558 (sizeof(uint64_t) << 3) - 1)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800559 CONF_HANDLE_BOOL(opt_prof_accum, prof_accum)
560 CONF_HANDLE_SSIZE_T(opt_lg_prof_interval,
561 lg_prof_interval, -1,
Jason Evans7372b152012-02-10 20:22:09 -0800562 (sizeof(uint64_t) << 3) - 1)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800563 CONF_HANDLE_BOOL(opt_prof_gdump, prof_gdump)
Jason Evans0b25fe72012-04-17 16:39:33 -0700564 CONF_HANDLE_BOOL(opt_prof_final, prof_final)
Jason Evansd81e4bd2012-03-06 14:57:45 -0800565 CONF_HANDLE_BOOL(opt_prof_leak, prof_leak)
Jason Evans7372b152012-02-10 20:22:09 -0800566 }
Jason Evanse7339702010-10-23 18:37:06 -0700567 malloc_conf_error("Invalid conf pair", k, klen, v,
568 vlen);
569#undef CONF_HANDLE_BOOL
570#undef CONF_HANDLE_SIZE_T
571#undef CONF_HANDLE_SSIZE_T
572#undef CONF_HANDLE_CHAR_P
573 }
Jason Evanse7339702010-10-23 18:37:06 -0700574 }
575}
576
577static bool
578malloc_init_hard(void)
579{
Jason Evansb7924f52009-06-23 19:01:18 -0700580 arena_t *init_arenas[1];
Jason Evans289053c2009-06-22 12:08:42 -0700581
582 malloc_mutex_lock(&init_lock);
Jason Evans41b6afb2012-02-02 22:04:57 -0800583 if (malloc_initialized || IS_INITIALIZER) {
Jason Evans289053c2009-06-22 12:08:42 -0700584 /*
585 * Another thread initialized the allocator before this one
Jason Evansa25d0a82009-11-09 14:57:38 -0800586 * acquired init_lock, or this thread is the initializing
587 * thread, and it is recursively allocating.
Jason Evans289053c2009-06-22 12:08:42 -0700588 */
589 malloc_mutex_unlock(&init_lock);
590 return (false);
591 }
Jason Evans41b6afb2012-02-02 22:04:57 -0800592#ifdef JEMALLOC_THREADED_INIT
Jason Evans02b23122012-04-05 11:06:23 -0700593 if (malloc_initializer != NO_INITIALIZER && IS_INITIALIZER == false) {
Jason Evansb7924f52009-06-23 19:01:18 -0700594 /* Busy-wait until the initializing thread completes. */
595 do {
596 malloc_mutex_unlock(&init_lock);
597 CPU_SPINWAIT;
598 malloc_mutex_lock(&init_lock);
599 } while (malloc_initialized == false);
Jason Evans2541e1b2010-07-22 11:35:59 -0700600 malloc_mutex_unlock(&init_lock);
Jason Evansb7924f52009-06-23 19:01:18 -0700601 return (false);
602 }
Jason Evans41b6afb2012-02-02 22:04:57 -0800603#endif
604 malloc_initializer = INITIALIZER;
Jason Evans289053c2009-06-22 12:08:42 -0700605
Jason Evanscd9a1342012-03-21 18:33:03 -0700606 malloc_tsd_boot();
Jason Evans7372b152012-02-10 20:22:09 -0800607 if (config_prof)
608 prof_boot0();
Jason Evans289053c2009-06-22 12:08:42 -0700609
Jason Evanse7339702010-10-23 18:37:06 -0700610 malloc_conf_init();
Jason Evans289053c2009-06-22 12:08:42 -0700611
Mike Hommeye77fa592012-03-28 09:53:16 +0200612#if (!defined(JEMALLOC_MUTEX_INIT_CB) && !defined(JEMALLOC_ZONE))
Jason Evansa0bf2422010-01-29 14:30:41 -0800613 /* Register fork handlers. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -0700614 if (pthread_atfork(jemalloc_prefork, jemalloc_postfork_parent,
615 jemalloc_postfork_child) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -0800616 malloc_write("<jemalloc>: Error in pthread_atfork()\n");
Jason Evansa0bf2422010-01-29 14:30:41 -0800617 if (opt_abort)
618 abort();
619 }
Jason Evans41b6afb2012-02-02 22:04:57 -0800620#endif
Jason Evans3c234352010-01-27 13:10:55 -0800621
Jason Evans03c22372010-01-03 12:10:42 -0800622 if (opt_stats_print) {
Jason Evans289053c2009-06-22 12:08:42 -0700623 /* Print statistics at exit. */
Jason Evansa0bf2422010-01-29 14:30:41 -0800624 if (atexit(stats_print_atexit) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -0800625 malloc_write("<jemalloc>: Error in atexit()\n");
Jason Evansa0bf2422010-01-29 14:30:41 -0800626 if (opt_abort)
627 abort();
628 }
Jason Evans289053c2009-06-22 12:08:42 -0700629 }
630
Mike Hommeyb8325f92012-04-12 15:15:35 +0200631 if (base_boot()) {
Jason Evansa0bf2422010-01-29 14:30:41 -0800632 malloc_mutex_unlock(&init_lock);
633 return (true);
634 }
Jason Evansc9658dd2009-06-22 14:44:08 -0700635
Mike Hommeyb8325f92012-04-12 15:15:35 +0200636 if (chunk_boot0()) {
Jason Evans3c234352010-01-27 13:10:55 -0800637 malloc_mutex_unlock(&init_lock);
638 return (true);
639 }
640
Jason Evans41b6afb2012-02-02 22:04:57 -0800641 if (ctl_boot()) {
642 malloc_mutex_unlock(&init_lock);
643 return (true);
644 }
645
Jason Evans7372b152012-02-10 20:22:09 -0800646 if (config_prof)
647 prof_boot1();
Jason Evans3383af62010-02-11 08:59:06 -0800648
Jason Evansb1726102012-02-28 16:50:47 -0800649 arena_boot();
Jason Evans289053c2009-06-22 12:08:42 -0700650
Jason Evanscd9a1342012-03-21 18:33:03 -0700651 if (config_tcache && tcache_boot0()) {
Jason Evans84c8eef2011-03-16 10:30:13 -0700652 malloc_mutex_unlock(&init_lock);
653 return (true);
654 }
Jason Evans84cbbcb2009-12-29 00:09:15 -0800655
Jason Evanse476f8a2010-01-16 09:53:50 -0800656 if (huge_boot()) {
Jason Evansc9658dd2009-06-22 14:44:08 -0700657 malloc_mutex_unlock(&init_lock);
658 return (true);
659 }
Jason Evans289053c2009-06-22 12:08:42 -0700660
Jason Evans8e6f8b42011-11-03 18:40:03 -0700661 if (malloc_mutex_init(&arenas_lock))
662 return (true);
663
Jason Evansb7924f52009-06-23 19:01:18 -0700664 /*
665 * Create enough scaffolding to allow recursive allocation in
666 * malloc_ncpus().
667 */
668 narenas = 1;
669 arenas = init_arenas;
670 memset(arenas, 0, sizeof(arena_t *) * narenas);
671
672 /*
673 * Initialize one arena here. The rest are lazily created in
674 * choose_arena_hard().
675 */
676 arenas_extend(0);
677 if (arenas[0] == NULL) {
678 malloc_mutex_unlock(&init_lock);
679 return (true);
680 }
681
Jason Evanscd9a1342012-03-21 18:33:03 -0700682 /* Initialize allocation counters before any allocations can occur. */
683 if (config_stats && thread_allocated_tsd_boot()) {
684 malloc_mutex_unlock(&init_lock);
685 return (true);
686 }
Jason Evansb7924f52009-06-23 19:01:18 -0700687
Jason Evanscd9a1342012-03-21 18:33:03 -0700688 if (arenas_tsd_boot()) {
689 malloc_mutex_unlock(&init_lock);
690 return (true);
691 }
692
693 if (config_tcache && tcache_boot1()) {
694 malloc_mutex_unlock(&init_lock);
695 return (true);
696 }
697
Jason Evans122449b2012-04-06 00:35:09 -0700698 if (config_fill && quarantine_boot()) {
699 malloc_mutex_unlock(&init_lock);
700 return (true);
701 }
702
Jason Evans6da54182012-03-23 18:05:51 -0700703 if (config_prof && prof_boot2()) {
704 malloc_mutex_unlock(&init_lock);
705 return (true);
706 }
707
Jason Evansb7924f52009-06-23 19:01:18 -0700708 /* Get number of CPUs. */
Jason Evansb7924f52009-06-23 19:01:18 -0700709 malloc_mutex_unlock(&init_lock);
710 ncpus = malloc_ncpus();
711 malloc_mutex_lock(&init_lock);
712
Jason Evanscd9a1342012-03-21 18:33:03 -0700713 if (chunk_boot1()) {
714 malloc_mutex_unlock(&init_lock);
715 return (true);
716 }
717
Jason Evans633aaff2012-04-03 08:47:07 -0700718 if (mutex_boot()) {
719 malloc_mutex_unlock(&init_lock);
720 return (true);
721 }
722
Jason Evanse7339702010-10-23 18:37:06 -0700723 if (opt_narenas == 0) {
Jason Evans289053c2009-06-22 12:08:42 -0700724 /*
Jason Evans5463a522009-12-29 00:09:15 -0800725 * For SMP systems, create more than one arena per CPU by
726 * default.
Jason Evans289053c2009-06-22 12:08:42 -0700727 */
Jason Evanse7339702010-10-23 18:37:06 -0700728 if (ncpus > 1)
729 opt_narenas = ncpus << 2;
730 else
731 opt_narenas = 1;
Jason Evans289053c2009-06-22 12:08:42 -0700732 }
Jason Evanse7339702010-10-23 18:37:06 -0700733 narenas = opt_narenas;
734 /*
735 * Make sure that the arenas array can be allocated. In practice, this
736 * limit is enough to allow the allocator to function, but the ctl
737 * machinery will fail to allocate memory at far lower limits.
738 */
739 if (narenas > chunksize / sizeof(arena_t *)) {
Jason Evanse7339702010-10-23 18:37:06 -0700740 narenas = chunksize / sizeof(arena_t *);
Jason Evansd81e4bd2012-03-06 14:57:45 -0800741 malloc_printf("<jemalloc>: Reducing narenas to limit (%d)\n",
742 narenas);
Jason Evans289053c2009-06-22 12:08:42 -0700743 }
Jason Evans289053c2009-06-22 12:08:42 -0700744
Jason Evans289053c2009-06-22 12:08:42 -0700745 /* Allocate and initialize arenas. */
746 arenas = (arena_t **)base_alloc(sizeof(arena_t *) * narenas);
747 if (arenas == NULL) {
748 malloc_mutex_unlock(&init_lock);
749 return (true);
750 }
751 /*
752 * Zero the array. In practice, this should always be pre-zeroed,
753 * since it was just mmap()ed, but let's be sure.
754 */
755 memset(arenas, 0, sizeof(arena_t *) * narenas);
Jason Evansb7924f52009-06-23 19:01:18 -0700756 /* Copy the pointer to the one arena that was already initialized. */
757 arenas[0] = init_arenas[0];
Jason Evans289053c2009-06-22 12:08:42 -0700758
759 malloc_initialized = true;
760 malloc_mutex_unlock(&init_lock);
761 return (false);
762}
763
764/*
Jason Evanse476f8a2010-01-16 09:53:50 -0800765 * End initialization functions.
Jason Evans289053c2009-06-22 12:08:42 -0700766 */
767/******************************************************************************/
768/*
769 * Begin malloc(3)-compatible functions.
770 */
771
Jason Evans9ad48232010-01-03 11:59:20 -0800772JEMALLOC_ATTR(malloc)
Jason Evanse476f8a2010-01-16 09:53:50 -0800773JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -0700774void *
Jason Evans0a5489e2012-03-01 17:19:20 -0800775je_malloc(size_t size)
Jason Evans289053c2009-06-22 12:08:42 -0700776{
777 void *ret;
Jason Evans7372b152012-02-10 20:22:09 -0800778 size_t usize;
Jason Evans9225a192012-03-23 15:39:07 -0700779 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700780
781 if (malloc_init()) {
782 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700783 goto label_oom;
Jason Evans289053c2009-06-22 12:08:42 -0700784 }
785
Jason Evansc90ad712012-02-28 20:31:37 -0800786 if (size == 0)
787 size = 1;
Jason Evans289053c2009-06-22 12:08:42 -0700788
Jason Evans7372b152012-02-10 20:22:09 -0800789 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -0700790 usize = s2u(size);
Jason Evansa5070042011-08-12 13:48:27 -0700791 PROF_ALLOC_PREP(1, usize, cnt);
792 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700793 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700794 goto label_oom;
Jason Evans0b270a92010-03-31 16:45:04 -0700795 }
Jason Evans93443682010-10-20 17:39:18 -0700796 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <=
Jason Evansb1726102012-02-28 16:50:47 -0800797 SMALL_MAXCLASS) {
798 ret = imalloc(SMALL_MAXCLASS+1);
Jason Evans0b270a92010-03-31 16:45:04 -0700799 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700800 arena_prof_promoted(ret, usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700801 } else
802 ret = imalloc(size);
Jason Evans7372b152012-02-10 20:22:09 -0800803 } else {
Jason Evans122449b2012-04-06 00:35:09 -0700804 if (config_stats || (config_valgrind && opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -0800805 usize = s2u(size);
Jason Evans0b270a92010-03-31 16:45:04 -0700806 ret = imalloc(size);
Jason Evans93443682010-10-20 17:39:18 -0700807 }
Jason Evans289053c2009-06-22 12:08:42 -0700808
Jason Evansa1ee7832012-04-10 15:07:44 -0700809label_oom:
Jason Evans289053c2009-06-22 12:08:42 -0700810 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -0800811 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -0800812 malloc_write("<jemalloc>: Error in malloc(): "
813 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -0700814 abort();
815 }
816 errno = ENOMEM;
817 }
Jason Evans7372b152012-02-10 20:22:09 -0800818 if (config_prof && opt_prof && ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700819 prof_malloc(ret, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -0800820 if (config_stats && ret != NULL) {
Jason Evans122449b2012-04-06 00:35:09 -0700821 assert(usize == isalloc(ret, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -0700822 thread_allocated_tsd_get()->allocated += usize;
Jason Evans93443682010-10-20 17:39:18 -0700823 }
Jason Evansb1476112012-04-05 13:36:17 -0700824 UTRACE(0, size, ret);
Jason Evans122449b2012-04-06 00:35:09 -0700825 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, usize, false);
Jason Evans289053c2009-06-22 12:08:42 -0700826 return (ret);
827}
828
Jason Evans9ad48232010-01-03 11:59:20 -0800829JEMALLOC_ATTR(nonnull(1))
Jason Evansa5070042011-08-12 13:48:27 -0700830#ifdef JEMALLOC_PROF
831/*
Jason Evans7372b152012-02-10 20:22:09 -0800832 * Avoid any uncertainty as to how many backtrace frames to ignore in
Jason Evansa5070042011-08-12 13:48:27 -0700833 * PROF_ALLOC_PREP().
834 */
835JEMALLOC_ATTR(noinline)
836#endif
837static int
Jason Evans59656312012-02-28 21:37:38 -0800838imemalign(void **memptr, size_t alignment, size_t size,
Jason Evans0a0bbf62012-03-13 12:55:21 -0700839 size_t min_alignment)
Jason Evans289053c2009-06-22 12:08:42 -0700840{
841 int ret;
Jason Evans7372b152012-02-10 20:22:09 -0800842 size_t usize;
Jason Evans38d92102011-03-23 00:37:29 -0700843 void *result;
Jason Evans9225a192012-03-23 15:39:07 -0700844 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700845
Jason Evans0a0bbf62012-03-13 12:55:21 -0700846 assert(min_alignment != 0);
847
Jason Evans289053c2009-06-22 12:08:42 -0700848 if (malloc_init())
849 result = NULL;
850 else {
Jason Evansc90ad712012-02-28 20:31:37 -0800851 if (size == 0)
852 size = 1;
Jason Evansf2518142009-12-29 00:09:15 -0800853
Jason Evans289053c2009-06-22 12:08:42 -0700854 /* Make sure that alignment is a large enough power of 2. */
855 if (((alignment - 1) & alignment) != 0
Jason Evans0a0bbf62012-03-13 12:55:21 -0700856 || (alignment < min_alignment)) {
Jason Evans7372b152012-02-10 20:22:09 -0800857 if (config_xmalloc && opt_xmalloc) {
Jason Evans0a0bbf62012-03-13 12:55:21 -0700858 malloc_write("<jemalloc>: Error allocating "
859 "aligned memory: invalid alignment\n");
Jason Evans289053c2009-06-22 12:08:42 -0700860 abort();
861 }
862 result = NULL;
863 ret = EINVAL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700864 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700865 }
866
Jason Evans5ff709c2012-04-11 18:13:45 -0700867 usize = sa2u(size, alignment);
Jason Evans38d92102011-03-23 00:37:29 -0700868 if (usize == 0) {
869 result = NULL;
870 ret = ENOMEM;
Jason Evansa1ee7832012-04-10 15:07:44 -0700871 goto label_return;
Jason Evans38d92102011-03-23 00:37:29 -0700872 }
873
Jason Evans7372b152012-02-10 20:22:09 -0800874 if (config_prof && opt_prof) {
Jason Evansa5070042011-08-12 13:48:27 -0700875 PROF_ALLOC_PREP(2, usize, cnt);
876 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700877 result = NULL;
878 ret = EINVAL;
879 } else {
880 if (prof_promote && (uintptr_t)cnt !=
Jason Evansb1726102012-02-28 16:50:47 -0800881 (uintptr_t)1U && usize <= SMALL_MAXCLASS) {
882 assert(sa2u(SMALL_MAXCLASS+1,
Jason Evans5ff709c2012-04-11 18:13:45 -0700883 alignment) != 0);
Jason Evansb1726102012-02-28 16:50:47 -0800884 result = ipalloc(sa2u(SMALL_MAXCLASS+1,
Jason Evans5ff709c2012-04-11 18:13:45 -0700885 alignment), alignment, false);
Jason Evans0b270a92010-03-31 16:45:04 -0700886 if (result != NULL) {
887 arena_prof_promoted(result,
Jason Evans93443682010-10-20 17:39:18 -0700888 usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700889 }
Jason Evans8e3c3c62010-09-17 15:46:18 -0700890 } else {
Jason Evans38d92102011-03-23 00:37:29 -0700891 result = ipalloc(usize, alignment,
Jason Evans8e3c3c62010-09-17 15:46:18 -0700892 false);
893 }
Jason Evans0b270a92010-03-31 16:45:04 -0700894 }
Jason Evans6109fe02010-02-10 10:37:56 -0800895 } else
Jason Evans38d92102011-03-23 00:37:29 -0700896 result = ipalloc(usize, alignment, false);
Jason Evans289053c2009-06-22 12:08:42 -0700897 }
898
899 if (result == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -0800900 if (config_xmalloc && opt_xmalloc) {
Jason Evans0a0bbf62012-03-13 12:55:21 -0700901 malloc_write("<jemalloc>: Error allocating aligned "
902 "memory: out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -0700903 abort();
904 }
905 ret = ENOMEM;
Jason Evansa1ee7832012-04-10 15:07:44 -0700906 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700907 }
908
909 *memptr = result;
910 ret = 0;
911
Jason Evansa1ee7832012-04-10 15:07:44 -0700912label_return:
Jason Evans7372b152012-02-10 20:22:09 -0800913 if (config_stats && result != NULL) {
Jason Evans122449b2012-04-06 00:35:09 -0700914 assert(usize == isalloc(result, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -0700915 thread_allocated_tsd_get()->allocated += usize;
Jason Evans93443682010-10-20 17:39:18 -0700916 }
Jason Evans7372b152012-02-10 20:22:09 -0800917 if (config_prof && opt_prof && result != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700918 prof_malloc(result, usize, cnt);
Jason Evansb1476112012-04-05 13:36:17 -0700919 UTRACE(0, size, result);
Jason Evans289053c2009-06-22 12:08:42 -0700920 return (ret);
921}
922
Jason Evansa5070042011-08-12 13:48:27 -0700923JEMALLOC_ATTR(nonnull(1))
924JEMALLOC_ATTR(visibility("default"))
925int
Jason Evans0a5489e2012-03-01 17:19:20 -0800926je_posix_memalign(void **memptr, size_t alignment, size_t size)
Jason Evansa5070042011-08-12 13:48:27 -0700927{
Jason Evans122449b2012-04-06 00:35:09 -0700928 int ret = imemalign(memptr, alignment, size, sizeof(void *));
929 JEMALLOC_VALGRIND_MALLOC(ret == 0, *memptr, isalloc(*memptr,
930 config_prof), false);
931 return (ret);
Jason Evans0a0bbf62012-03-13 12:55:21 -0700932}
933
934JEMALLOC_ATTR(malloc)
935JEMALLOC_ATTR(visibility("default"))
936void *
937je_aligned_alloc(size_t alignment, size_t size)
938{
939 void *ret;
940 int err;
941
942 if ((err = imemalign(&ret, alignment, size, 1)) != 0) {
943 ret = NULL;
944 errno = err;
945 }
Jason Evans122449b2012-04-06 00:35:09 -0700946 JEMALLOC_VALGRIND_MALLOC(err == 0, ret, isalloc(ret, config_prof),
947 false);
Jason Evans0a0bbf62012-03-13 12:55:21 -0700948 return (ret);
Jason Evansa5070042011-08-12 13:48:27 -0700949}
950
Jason Evans9ad48232010-01-03 11:59:20 -0800951JEMALLOC_ATTR(malloc)
Jason Evanse476f8a2010-01-16 09:53:50 -0800952JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -0700953void *
Jason Evans0a5489e2012-03-01 17:19:20 -0800954je_calloc(size_t num, size_t size)
Jason Evans289053c2009-06-22 12:08:42 -0700955{
956 void *ret;
957 size_t num_size;
Jason Evans7372b152012-02-10 20:22:09 -0800958 size_t usize;
Jason Evans9225a192012-03-23 15:39:07 -0700959 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700960
961 if (malloc_init()) {
962 num_size = 0;
963 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700964 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700965 }
966
967 num_size = num * size;
968 if (num_size == 0) {
Jason Evansc90ad712012-02-28 20:31:37 -0800969 if (num == 0 || size == 0)
Jason Evans289053c2009-06-22 12:08:42 -0700970 num_size = 1;
971 else {
972 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700973 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700974 }
975 /*
976 * Try to avoid division here. We know that it isn't possible to
977 * overflow during multiplication if neither operand uses any of the
978 * most significant half of the bits in a size_t.
979 */
980 } else if (((num | size) & (SIZE_T_MAX << (sizeof(size_t) << 2)))
981 && (num_size / size != num)) {
982 /* size_t overflow. */
983 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700984 goto label_return;
Jason Evans289053c2009-06-22 12:08:42 -0700985 }
986
Jason Evans7372b152012-02-10 20:22:09 -0800987 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -0700988 usize = s2u(num_size);
Jason Evansa5070042011-08-12 13:48:27 -0700989 PROF_ALLOC_PREP(1, usize, cnt);
990 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700991 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -0700992 goto label_return;
Jason Evans0b270a92010-03-31 16:45:04 -0700993 }
Jason Evans93443682010-10-20 17:39:18 -0700994 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize
Jason Evansb1726102012-02-28 16:50:47 -0800995 <= SMALL_MAXCLASS) {
996 ret = icalloc(SMALL_MAXCLASS+1);
Jason Evans0b270a92010-03-31 16:45:04 -0700997 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700998 arena_prof_promoted(ret, usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700999 } else
1000 ret = icalloc(num_size);
Jason Evans7372b152012-02-10 20:22:09 -08001001 } else {
Jason Evans122449b2012-04-06 00:35:09 -07001002 if (config_stats || (config_valgrind && opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -08001003 usize = s2u(num_size);
Jason Evans0b270a92010-03-31 16:45:04 -07001004 ret = icalloc(num_size);
Jason Evans93443682010-10-20 17:39:18 -07001005 }
Jason Evans289053c2009-06-22 12:08:42 -07001006
Jason Evansa1ee7832012-04-10 15:07:44 -07001007label_return:
Jason Evans289053c2009-06-22 12:08:42 -07001008 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001009 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001010 malloc_write("<jemalloc>: Error in calloc(): out of "
1011 "memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001012 abort();
1013 }
1014 errno = ENOMEM;
1015 }
1016
Jason Evans7372b152012-02-10 20:22:09 -08001017 if (config_prof && opt_prof && ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001018 prof_malloc(ret, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -08001019 if (config_stats && ret != NULL) {
Jason Evans122449b2012-04-06 00:35:09 -07001020 assert(usize == isalloc(ret, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -07001021 thread_allocated_tsd_get()->allocated += usize;
Jason Evans93443682010-10-20 17:39:18 -07001022 }
Jason Evansb1476112012-04-05 13:36:17 -07001023 UTRACE(0, num_size, ret);
Jason Evans122449b2012-04-06 00:35:09 -07001024 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, usize, true);
Jason Evans289053c2009-06-22 12:08:42 -07001025 return (ret);
1026}
1027
Jason Evanse476f8a2010-01-16 09:53:50 -08001028JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001029void *
Jason Evans0a5489e2012-03-01 17:19:20 -08001030je_realloc(void *ptr, size_t size)
Jason Evans289053c2009-06-22 12:08:42 -07001031{
1032 void *ret;
Jason Evans7372b152012-02-10 20:22:09 -08001033 size_t usize;
Jason Evans93443682010-10-20 17:39:18 -07001034 size_t old_size = 0;
Jason Evans122449b2012-04-06 00:35:09 -07001035 size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evans9225a192012-03-23 15:39:07 -07001036 prof_thr_cnt_t *cnt JEMALLOC_CC_SILENCE_INIT(NULL);
1037 prof_ctx_t *old_ctx JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans6109fe02010-02-10 10:37:56 -08001038
Jason Evans289053c2009-06-22 12:08:42 -07001039 if (size == 0) {
Jason Evansf081b882012-02-28 20:24:05 -08001040 if (ptr != NULL) {
1041 /* realloc(ptr, 0) is equivalent to free(p). */
Jason Evans122449b2012-04-06 00:35:09 -07001042 if (config_prof) {
1043 old_size = isalloc(ptr, true);
1044 if (config_valgrind && opt_valgrind)
1045 old_rzsize = p2rz(ptr);
1046 } else if (config_stats) {
1047 old_size = isalloc(ptr, false);
1048 if (config_valgrind && opt_valgrind)
1049 old_rzsize = u2rz(old_size);
1050 } else if (config_valgrind && opt_valgrind) {
1051 old_size = isalloc(ptr, false);
1052 old_rzsize = u2rz(old_size);
1053 }
Jason Evansf081b882012-02-28 20:24:05 -08001054 if (config_prof && opt_prof) {
1055 old_ctx = prof_ctx_get(ptr);
Jason Evans6109fe02010-02-10 10:37:56 -08001056 cnt = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001057 }
Jason Evans122449b2012-04-06 00:35:09 -07001058 iqalloc(ptr);
Jason Evans289053c2009-06-22 12:08:42 -07001059 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -07001060 goto label_return;
Jason Evansc90ad712012-02-28 20:31:37 -08001061 } else
1062 size = 1;
Jason Evans289053c2009-06-22 12:08:42 -07001063 }
1064
1065 if (ptr != NULL) {
Jason Evans41b6afb2012-02-02 22:04:57 -08001066 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans289053c2009-06-22 12:08:42 -07001067
Jason Evans122449b2012-04-06 00:35:09 -07001068 if (config_prof) {
1069 old_size = isalloc(ptr, true);
1070 if (config_valgrind && opt_valgrind)
1071 old_rzsize = p2rz(ptr);
1072 } else if (config_stats) {
1073 old_size = isalloc(ptr, false);
1074 if (config_valgrind && opt_valgrind)
1075 old_rzsize = u2rz(old_size);
1076 } else if (config_valgrind && opt_valgrind) {
1077 old_size = isalloc(ptr, false);
1078 old_rzsize = u2rz(old_size);
1079 }
Jason Evans7372b152012-02-10 20:22:09 -08001080 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001081 usize = s2u(size);
Jason Evans50651562010-04-13 16:13:54 -07001082 old_ctx = prof_ctx_get(ptr);
Jason Evansa5070042011-08-12 13:48:27 -07001083 PROF_ALLOC_PREP(1, usize, cnt);
1084 if (cnt == NULL) {
Jason Evans46405e62011-08-30 23:37:29 -07001085 old_ctx = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001086 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -07001087 goto label_oom;
Jason Evans6109fe02010-02-10 10:37:56 -08001088 }
Jason Evans0b270a92010-03-31 16:45:04 -07001089 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U &&
Jason Evansb1726102012-02-28 16:50:47 -08001090 usize <= SMALL_MAXCLASS) {
1091 ret = iralloc(ptr, SMALL_MAXCLASS+1, 0, 0,
Jason Evans8e3c3c62010-09-17 15:46:18 -07001092 false, false);
Jason Evans0b270a92010-03-31 16:45:04 -07001093 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001094 arena_prof_promoted(ret, usize);
Jason Evans46405e62011-08-30 23:37:29 -07001095 else
1096 old_ctx = NULL;
1097 } else {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001098 ret = iralloc(ptr, size, 0, 0, false, false);
Jason Evans46405e62011-08-30 23:37:29 -07001099 if (ret == NULL)
1100 old_ctx = NULL;
1101 }
Jason Evans7372b152012-02-10 20:22:09 -08001102 } else {
Jason Evans122449b2012-04-06 00:35:09 -07001103 if (config_stats || (config_valgrind && opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -08001104 usize = s2u(size);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001105 ret = iralloc(ptr, size, 0, 0, false, false);
Jason Evans93443682010-10-20 17:39:18 -07001106 }
Jason Evans289053c2009-06-22 12:08:42 -07001107
Jason Evansa1ee7832012-04-10 15:07:44 -07001108label_oom:
Jason Evans289053c2009-06-22 12:08:42 -07001109 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001110 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001111 malloc_write("<jemalloc>: Error in realloc(): "
1112 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001113 abort();
1114 }
1115 errno = ENOMEM;
1116 }
1117 } else {
Jason Evansf081b882012-02-28 20:24:05 -08001118 /* realloc(NULL, size) is equivalent to malloc(size). */
Jason Evans7372b152012-02-10 20:22:09 -08001119 if (config_prof && opt_prof)
Jason Evans50651562010-04-13 16:13:54 -07001120 old_ctx = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001121 if (malloc_init()) {
Jason Evans7372b152012-02-10 20:22:09 -08001122 if (config_prof && opt_prof)
Jason Evans6109fe02010-02-10 10:37:56 -08001123 cnt = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001124 ret = NULL;
1125 } else {
Jason Evans7372b152012-02-10 20:22:09 -08001126 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001127 usize = s2u(size);
Jason Evansa5070042011-08-12 13:48:27 -07001128 PROF_ALLOC_PREP(1, usize, cnt);
1129 if (cnt == NULL)
Jason Evans0b270a92010-03-31 16:45:04 -07001130 ret = NULL;
1131 else {
1132 if (prof_promote && (uintptr_t)cnt !=
Jason Evans93443682010-10-20 17:39:18 -07001133 (uintptr_t)1U && usize <=
Jason Evansb1726102012-02-28 16:50:47 -08001134 SMALL_MAXCLASS) {
1135 ret = imalloc(SMALL_MAXCLASS+1);
Jason Evans0b270a92010-03-31 16:45:04 -07001136 if (ret != NULL) {
1137 arena_prof_promoted(ret,
Jason Evans93443682010-10-20 17:39:18 -07001138 usize);
Jason Evans0b270a92010-03-31 16:45:04 -07001139 }
1140 } else
1141 ret = imalloc(size);
1142 }
Jason Evans7372b152012-02-10 20:22:09 -08001143 } else {
Jason Evans122449b2012-04-06 00:35:09 -07001144 if (config_stats || (config_valgrind &&
1145 opt_valgrind))
Jason Evans7372b152012-02-10 20:22:09 -08001146 usize = s2u(size);
Jason Evans6109fe02010-02-10 10:37:56 -08001147 ret = imalloc(size);
Jason Evans93443682010-10-20 17:39:18 -07001148 }
Jason Evans6109fe02010-02-10 10:37:56 -08001149 }
Jason Evans569432c2009-12-29 00:09:15 -08001150
Jason Evans289053c2009-06-22 12:08:42 -07001151 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001152 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001153 malloc_write("<jemalloc>: Error in realloc(): "
1154 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001155 abort();
1156 }
1157 errno = ENOMEM;
1158 }
1159 }
1160
Jason Evansa1ee7832012-04-10 15:07:44 -07001161label_return:
Jason Evans7372b152012-02-10 20:22:09 -08001162 if (config_prof && opt_prof)
Jason Evanse4f78462010-10-22 10:45:59 -07001163 prof_realloc(ret, usize, cnt, old_size, old_ctx);
Jason Evans7372b152012-02-10 20:22:09 -08001164 if (config_stats && ret != NULL) {
Jason Evanscd9a1342012-03-21 18:33:03 -07001165 thread_allocated_t *ta;
Jason Evans122449b2012-04-06 00:35:09 -07001166 assert(usize == isalloc(ret, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -07001167 ta = thread_allocated_tsd_get();
1168 ta->allocated += usize;
1169 ta->deallocated += old_size;
Jason Evans93443682010-10-20 17:39:18 -07001170 }
Jason Evansb1476112012-04-05 13:36:17 -07001171 UTRACE(ptr, size, ret);
Jason Evans122449b2012-04-06 00:35:09 -07001172 JEMALLOC_VALGRIND_REALLOC(ret, usize, ptr, old_size, old_rzsize, false);
Jason Evans289053c2009-06-22 12:08:42 -07001173 return (ret);
1174}
1175
Jason Evanse476f8a2010-01-16 09:53:50 -08001176JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001177void
Jason Evans0a5489e2012-03-01 17:19:20 -08001178je_free(void *ptr)
Jason Evans289053c2009-06-22 12:08:42 -07001179{
1180
Jason Evansb1476112012-04-05 13:36:17 -07001181 UTRACE(ptr, 0, 0);
Jason Evansf0047372012-04-02 15:18:24 -07001182 if (ptr != NULL) {
1183 size_t usize;
Jason Evans122449b2012-04-06 00:35:09 -07001184 size_t rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evanse4f78462010-10-22 10:45:59 -07001185
Jason Evansf0047372012-04-02 15:18:24 -07001186 assert(malloc_initialized || IS_INITIALIZER);
1187
1188 if (config_prof && opt_prof) {
Jason Evans122449b2012-04-06 00:35:09 -07001189 usize = isalloc(ptr, config_prof);
Jason Evansf0047372012-04-02 15:18:24 -07001190 prof_free(ptr, usize);
Jason Evans122449b2012-04-06 00:35:09 -07001191 } else if (config_stats || config_valgrind)
1192 usize = isalloc(ptr, config_prof);
Jason Evansf0047372012-04-02 15:18:24 -07001193 if (config_stats)
1194 thread_allocated_tsd_get()->deallocated += usize;
Jason Evans122449b2012-04-06 00:35:09 -07001195 if (config_valgrind && opt_valgrind)
1196 rzsize = p2rz(ptr);
1197 iqalloc(ptr);
1198 JEMALLOC_VALGRIND_FREE(ptr, rzsize);
Jason Evansf0047372012-04-02 15:18:24 -07001199 }
Jason Evans289053c2009-06-22 12:08:42 -07001200}
1201
1202/*
1203 * End malloc(3)-compatible functions.
1204 */
1205/******************************************************************************/
1206/*
Jason Evans6a0d2912010-09-20 16:44:23 -07001207 * Begin non-standard override functions.
Jason Evans6a0d2912010-09-20 16:44:23 -07001208 */
Jason Evans6a0d2912010-09-20 16:44:23 -07001209
1210#ifdef JEMALLOC_OVERRIDE_MEMALIGN
1211JEMALLOC_ATTR(malloc)
1212JEMALLOC_ATTR(visibility("default"))
1213void *
Jason Evans0a5489e2012-03-01 17:19:20 -08001214je_memalign(size_t alignment, size_t size)
Jason Evans6a0d2912010-09-20 16:44:23 -07001215{
Jason Evans9225a192012-03-23 15:39:07 -07001216 void *ret JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evans0a0bbf62012-03-13 12:55:21 -07001217 imemalign(&ret, alignment, size, 1);
Jason Evans122449b2012-04-06 00:35:09 -07001218 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, size, false);
Jason Evans6a0d2912010-09-20 16:44:23 -07001219 return (ret);
1220}
1221#endif
1222
1223#ifdef JEMALLOC_OVERRIDE_VALLOC
1224JEMALLOC_ATTR(malloc)
1225JEMALLOC_ATTR(visibility("default"))
1226void *
Jason Evans0a5489e2012-03-01 17:19:20 -08001227je_valloc(size_t size)
Jason Evans6a0d2912010-09-20 16:44:23 -07001228{
Jason Evans9225a192012-03-23 15:39:07 -07001229 void *ret JEMALLOC_CC_SILENCE_INIT(NULL);
Jason Evansae4c7b42012-04-02 07:04:34 -07001230 imemalign(&ret, PAGE, size, 1);
Jason Evans122449b2012-04-06 00:35:09 -07001231 JEMALLOC_VALGRIND_MALLOC(ret != NULL, ret, size, false);
Jason Evans6a0d2912010-09-20 16:44:23 -07001232 return (ret);
1233}
1234#endif
1235
Mike Hommey5c89c502012-03-26 17:46:57 +02001236/*
1237 * is_malloc(je_malloc) is some macro magic to detect if jemalloc_defs.h has
1238 * #define je_malloc malloc
1239 */
1240#define malloc_is_malloc 1
1241#define is_malloc_(a) malloc_is_ ## a
1242#define is_malloc(a) is_malloc_(a)
1243
1244#if ((is_malloc(je_malloc) == 1) && defined(__GLIBC__) && !defined(__UCLIBC__))
Jason Evans4bb09832012-02-29 10:37:27 -08001245/*
1246 * glibc provides the RTLD_DEEPBIND flag for dlopen which can make it possible
1247 * to inconsistently reference libc's malloc(3)-compatible functions
1248 * (https://bugzilla.mozilla.org/show_bug.cgi?id=493541).
1249 *
Mike Hommey3c2ba0d2012-03-27 14:20:13 +02001250 * These definitions interpose hooks in glibc. The functions are actually
Jason Evans4bb09832012-02-29 10:37:27 -08001251 * passed an extra argument for the caller return address, which will be
1252 * ignored.
1253 */
1254JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001255void (* const __free_hook)(void *ptr) = je_free;
Jason Evans4bb09832012-02-29 10:37:27 -08001256
1257JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001258void *(* const __malloc_hook)(size_t size) = je_malloc;
Jason Evans4bb09832012-02-29 10:37:27 -08001259
1260JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001261void *(* const __realloc_hook)(void *ptr, size_t size) = je_realloc;
Jason Evans4bb09832012-02-29 10:37:27 -08001262
1263JEMALLOC_ATTR(visibility("default"))
Jason Evans0a5489e2012-03-01 17:19:20 -08001264void *(* const __memalign_hook)(size_t alignment, size_t size) = je_memalign;
Jason Evans4bb09832012-02-29 10:37:27 -08001265#endif
1266
Jason Evans6a0d2912010-09-20 16:44:23 -07001267/*
1268 * End non-standard override functions.
1269 */
1270/******************************************************************************/
1271/*
Jason Evans289053c2009-06-22 12:08:42 -07001272 * Begin non-standard functions.
1273 */
1274
Jason Evanse476f8a2010-01-16 09:53:50 -08001275JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001276size_t
Jason Evans0a5489e2012-03-01 17:19:20 -08001277je_malloc_usable_size(const void *ptr)
Jason Evans289053c2009-06-22 12:08:42 -07001278{
Jason Evans569432c2009-12-29 00:09:15 -08001279 size_t ret;
Jason Evans289053c2009-06-22 12:08:42 -07001280
Jason Evans41b6afb2012-02-02 22:04:57 -08001281 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001282
Jason Evans7372b152012-02-10 20:22:09 -08001283 if (config_ivsalloc)
Jason Evans122449b2012-04-06 00:35:09 -07001284 ret = ivsalloc(ptr, config_prof);
Jason Evans2465bdf2012-03-26 13:13:55 -07001285 else
Jason Evans122449b2012-04-06 00:35:09 -07001286 ret = (ptr != NULL) ? isalloc(ptr, config_prof) : 0;
Jason Evans289053c2009-06-22 12:08:42 -07001287
Jason Evans569432c2009-12-29 00:09:15 -08001288 return (ret);
Jason Evans289053c2009-06-22 12:08:42 -07001289}
1290
Jason Evans4201af02010-01-24 02:53:40 -08001291JEMALLOC_ATTR(visibility("default"))
1292void
Jason Evans0a5489e2012-03-01 17:19:20 -08001293je_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
1294 const char *opts)
Jason Evans4201af02010-01-24 02:53:40 -08001295{
1296
Jason Evans698805c2010-03-03 17:45:38 -08001297 stats_print(write_cb, cbopaque, opts);
Jason Evans4201af02010-01-24 02:53:40 -08001298}
1299
Jason Evans3c234352010-01-27 13:10:55 -08001300JEMALLOC_ATTR(visibility("default"))
1301int
Jason Evans0a5489e2012-03-01 17:19:20 -08001302je_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
Jason Evans3c234352010-01-27 13:10:55 -08001303 size_t newlen)
1304{
1305
Jason Evans95833312010-01-27 13:45:21 -08001306 if (malloc_init())
1307 return (EAGAIN);
1308
Jason Evans3c234352010-01-27 13:10:55 -08001309 return (ctl_byname(name, oldp, oldlenp, newp, newlen));
1310}
1311
1312JEMALLOC_ATTR(visibility("default"))
1313int
Jason Evans0a5489e2012-03-01 17:19:20 -08001314je_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp)
Jason Evans3c234352010-01-27 13:10:55 -08001315{
1316
Jason Evans95833312010-01-27 13:45:21 -08001317 if (malloc_init())
1318 return (EAGAIN);
1319
Jason Evans3c234352010-01-27 13:10:55 -08001320 return (ctl_nametomib(name, mibp, miblenp));
1321}
1322
1323JEMALLOC_ATTR(visibility("default"))
1324int
Jason Evans0a5489e2012-03-01 17:19:20 -08001325je_mallctlbymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1326 void *newp, size_t newlen)
Jason Evans3c234352010-01-27 13:10:55 -08001327{
1328
Jason Evans95833312010-01-27 13:45:21 -08001329 if (malloc_init())
1330 return (EAGAIN);
1331
Jason Evans3c234352010-01-27 13:10:55 -08001332 return (ctl_bymib(mib, miblen, oldp, oldlenp, newp, newlen));
1333}
1334
Jason Evans7e77eaf2012-03-02 17:47:37 -08001335/*
1336 * End non-standard functions.
1337 */
1338/******************************************************************************/
1339/*
1340 * Begin experimental functions.
1341 */
1342#ifdef JEMALLOC_EXPERIMENTAL
1343
Jason Evans8e3c3c62010-09-17 15:46:18 -07001344JEMALLOC_INLINE void *
Jason Evans38d92102011-03-23 00:37:29 -07001345iallocm(size_t usize, size_t alignment, bool zero)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001346{
1347
Jason Evans5ff709c2012-04-11 18:13:45 -07001348 assert(usize == ((alignment == 0) ? s2u(usize) : sa2u(usize,
1349 alignment)));
Jason Evans38d92102011-03-23 00:37:29 -07001350
Jason Evans8e3c3c62010-09-17 15:46:18 -07001351 if (alignment != 0)
Jason Evans38d92102011-03-23 00:37:29 -07001352 return (ipalloc(usize, alignment, zero));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001353 else if (zero)
Jason Evans38d92102011-03-23 00:37:29 -07001354 return (icalloc(usize));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001355 else
Jason Evans38d92102011-03-23 00:37:29 -07001356 return (imalloc(usize));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001357}
1358
Jason Evans6a0d2912010-09-20 16:44:23 -07001359JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001360JEMALLOC_ATTR(visibility("default"))
1361int
Jason Evans0a5489e2012-03-01 17:19:20 -08001362je_allocm(void **ptr, size_t *rsize, size_t size, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001363{
1364 void *p;
Jason Evans93443682010-10-20 17:39:18 -07001365 size_t usize;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001366 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1367 & (SIZE_T_MAX-1));
1368 bool zero = flags & ALLOCM_ZERO;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001369 prof_thr_cnt_t *cnt;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001370
1371 assert(ptr != NULL);
1372 assert(size != 0);
1373
1374 if (malloc_init())
Jason Evansa1ee7832012-04-10 15:07:44 -07001375 goto label_oom;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001376
Jason Evans5ff709c2012-04-11 18:13:45 -07001377 usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment);
Jason Evans38d92102011-03-23 00:37:29 -07001378 if (usize == 0)
Jason Evansa1ee7832012-04-10 15:07:44 -07001379 goto label_oom;
Jason Evans38d92102011-03-23 00:37:29 -07001380
Jason Evans7372b152012-02-10 20:22:09 -08001381 if (config_prof && opt_prof) {
Jason Evansa5070042011-08-12 13:48:27 -07001382 PROF_ALLOC_PREP(1, usize, cnt);
1383 if (cnt == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001384 goto label_oom;
Jason Evans93443682010-10-20 17:39:18 -07001385 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <=
Jason Evansb1726102012-02-28 16:50:47 -08001386 SMALL_MAXCLASS) {
Jason Evans38d92102011-03-23 00:37:29 -07001387 size_t usize_promoted = (alignment == 0) ?
Jason Evansb1726102012-02-28 16:50:47 -08001388 s2u(SMALL_MAXCLASS+1) : sa2u(SMALL_MAXCLASS+1,
Jason Evans5ff709c2012-04-11 18:13:45 -07001389 alignment);
Jason Evans38d92102011-03-23 00:37:29 -07001390 assert(usize_promoted != 0);
1391 p = iallocm(usize_promoted, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001392 if (p == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001393 goto label_oom;
Jason Evans93443682010-10-20 17:39:18 -07001394 arena_prof_promoted(p, usize);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001395 } else {
Jason Evans38d92102011-03-23 00:37:29 -07001396 p = iallocm(usize, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001397 if (p == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001398 goto label_oom;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001399 }
Jason Evans749c2a02011-08-12 18:37:54 -07001400 prof_malloc(p, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -08001401 } else {
Jason Evans38d92102011-03-23 00:37:29 -07001402 p = iallocm(usize, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001403 if (p == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001404 goto label_oom;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001405 }
Jason Evans7372b152012-02-10 20:22:09 -08001406 if (rsize != NULL)
1407 *rsize = usize;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001408
1409 *ptr = p;
Jason Evans7372b152012-02-10 20:22:09 -08001410 if (config_stats) {
Jason Evans122449b2012-04-06 00:35:09 -07001411 assert(usize == isalloc(p, config_prof));
Jason Evanscd9a1342012-03-21 18:33:03 -07001412 thread_allocated_tsd_get()->allocated += usize;
Jason Evans7372b152012-02-10 20:22:09 -08001413 }
Jason Evansb1476112012-04-05 13:36:17 -07001414 UTRACE(0, size, p);
Jason Evans122449b2012-04-06 00:35:09 -07001415 JEMALLOC_VALGRIND_MALLOC(true, p, usize, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001416 return (ALLOCM_SUCCESS);
Jason Evansa1ee7832012-04-10 15:07:44 -07001417label_oom:
Jason Evans7372b152012-02-10 20:22:09 -08001418 if (config_xmalloc && opt_xmalloc) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001419 malloc_write("<jemalloc>: Error in allocm(): "
1420 "out of memory\n");
1421 abort();
1422 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001423 *ptr = NULL;
Jason Evansb1476112012-04-05 13:36:17 -07001424 UTRACE(0, size, 0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001425 return (ALLOCM_ERR_OOM);
1426}
1427
Jason Evans6a0d2912010-09-20 16:44:23 -07001428JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001429JEMALLOC_ATTR(visibility("default"))
1430int
Jason Evans0a5489e2012-03-01 17:19:20 -08001431je_rallocm(void **ptr, size_t *rsize, size_t size, size_t extra, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001432{
1433 void *p, *q;
Jason Evans93443682010-10-20 17:39:18 -07001434 size_t usize;
Jason Evans93443682010-10-20 17:39:18 -07001435 size_t old_size;
Jason Evans122449b2012-04-06 00:35:09 -07001436 size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001437 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1438 & (SIZE_T_MAX-1));
1439 bool zero = flags & ALLOCM_ZERO;
1440 bool no_move = flags & ALLOCM_NO_MOVE;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001441 prof_thr_cnt_t *cnt;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001442
1443 assert(ptr != NULL);
1444 assert(*ptr != NULL);
1445 assert(size != 0);
1446 assert(SIZE_T_MAX - size >= extra);
Jason Evans41b6afb2012-02-02 22:04:57 -08001447 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001448
1449 p = *ptr;
Jason Evans7372b152012-02-10 20:22:09 -08001450 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001451 /*
1452 * usize isn't knowable before iralloc() returns when extra is
1453 * non-zero. Therefore, compute its maximum possible value and
Jason Evansa5070042011-08-12 13:48:27 -07001454 * use that in PROF_ALLOC_PREP() to decide whether to capture a
Jason Evans93443682010-10-20 17:39:18 -07001455 * backtrace. prof_realloc() will use the actual usize to
1456 * decide whether to sample.
1457 */
1458 size_t max_usize = (alignment == 0) ? s2u(size+extra) :
Jason Evans5ff709c2012-04-11 18:13:45 -07001459 sa2u(size+extra, alignment);
Jason Evans46405e62011-08-30 23:37:29 -07001460 prof_ctx_t *old_ctx = prof_ctx_get(p);
Jason Evans122449b2012-04-06 00:35:09 -07001461 old_size = isalloc(p, true);
1462 if (config_valgrind && opt_valgrind)
1463 old_rzsize = p2rz(p);
Jason Evansa5070042011-08-12 13:48:27 -07001464 PROF_ALLOC_PREP(1, max_usize, cnt);
1465 if (cnt == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001466 goto label_oom;
Jason Evans183ba502011-08-11 22:51:00 -07001467 /*
1468 * Use minimum usize to determine whether promotion may happen.
1469 */
1470 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U
Jason Evans5ff709c2012-04-11 18:13:45 -07001471 && ((alignment == 0) ? s2u(size) : sa2u(size, alignment))
1472 <= SMALL_MAXCLASS) {
Jason Evansb1726102012-02-28 16:50:47 -08001473 q = iralloc(p, SMALL_MAXCLASS+1, (SMALL_MAXCLASS+1 >=
1474 size+extra) ? 0 : size+extra - (SMALL_MAXCLASS+1),
Jason Evans8e3c3c62010-09-17 15:46:18 -07001475 alignment, zero, no_move);
1476 if (q == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001477 goto label_err;
Jason Evansae4c7b42012-04-02 07:04:34 -07001478 if (max_usize < PAGE) {
Jason Evans183ba502011-08-11 22:51:00 -07001479 usize = max_usize;
1480 arena_prof_promoted(q, usize);
Jason Evansb493ce22011-08-12 11:28:47 -07001481 } else
Jason Evans122449b2012-04-06 00:35:09 -07001482 usize = isalloc(q, config_prof);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001483 } else {
1484 q = iralloc(p, size, extra, alignment, zero, no_move);
1485 if (q == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001486 goto label_err;
Jason Evans122449b2012-04-06 00:35:09 -07001487 usize = isalloc(q, config_prof);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001488 }
Jason Evanse4f78462010-10-22 10:45:59 -07001489 prof_realloc(q, usize, cnt, old_size, old_ctx);
Jason Evanseacb8962011-03-23 00:30:30 -07001490 if (rsize != NULL)
1491 *rsize = usize;
Jason Evans7372b152012-02-10 20:22:09 -08001492 } else {
Jason Evans122449b2012-04-06 00:35:09 -07001493 if (config_stats) {
1494 old_size = isalloc(p, false);
1495 if (config_valgrind && opt_valgrind)
1496 old_rzsize = u2rz(old_size);
1497 } else if (config_valgrind && opt_valgrind) {
1498 old_size = isalloc(p, false);
1499 old_rzsize = u2rz(old_size);
1500 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001501 q = iralloc(p, size, extra, alignment, zero, no_move);
1502 if (q == NULL)
Jason Evansa1ee7832012-04-10 15:07:44 -07001503 goto label_err;
Jason Evans7372b152012-02-10 20:22:09 -08001504 if (config_stats)
Jason Evans122449b2012-04-06 00:35:09 -07001505 usize = isalloc(q, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001506 if (rsize != NULL) {
1507 if (config_stats == false)
Jason Evans122449b2012-04-06 00:35:09 -07001508 usize = isalloc(q, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001509 *rsize = usize;
Jason Evans93443682010-10-20 17:39:18 -07001510 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001511 }
1512
1513 *ptr = q;
Jason Evanscd9a1342012-03-21 18:33:03 -07001514 if (config_stats) {
1515 thread_allocated_t *ta;
1516 ta = thread_allocated_tsd_get();
1517 ta->allocated += usize;
1518 ta->deallocated += old_size;
1519 }
Jason Evansb1476112012-04-05 13:36:17 -07001520 UTRACE(p, size, q);
Jason Evans122449b2012-04-06 00:35:09 -07001521 JEMALLOC_VALGRIND_REALLOC(q, usize, p, old_size, old_rzsize, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001522 return (ALLOCM_SUCCESS);
Jason Evansa1ee7832012-04-10 15:07:44 -07001523label_err:
Jason Evansb1476112012-04-05 13:36:17 -07001524 if (no_move) {
1525 UTRACE(p, size, q);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001526 return (ALLOCM_ERR_NOT_MOVED);
Jason Evansb1476112012-04-05 13:36:17 -07001527 }
Jason Evansa1ee7832012-04-10 15:07:44 -07001528label_oom:
Jason Evans7372b152012-02-10 20:22:09 -08001529 if (config_xmalloc && opt_xmalloc) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001530 malloc_write("<jemalloc>: Error in rallocm(): "
1531 "out of memory\n");
1532 abort();
1533 }
Jason Evansb1476112012-04-05 13:36:17 -07001534 UTRACE(p, size, 0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001535 return (ALLOCM_ERR_OOM);
1536}
1537
Jason Evans6a0d2912010-09-20 16:44:23 -07001538JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001539JEMALLOC_ATTR(visibility("default"))
1540int
Jason Evans0a5489e2012-03-01 17:19:20 -08001541je_sallocm(const void *ptr, size_t *rsize, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001542{
1543 size_t sz;
1544
Jason Evans41b6afb2012-02-02 22:04:57 -08001545 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001546
Jason Evans7372b152012-02-10 20:22:09 -08001547 if (config_ivsalloc)
Jason Evans122449b2012-04-06 00:35:09 -07001548 sz = ivsalloc(ptr, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001549 else {
1550 assert(ptr != NULL);
Jason Evans122449b2012-04-06 00:35:09 -07001551 sz = isalloc(ptr, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001552 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001553 assert(rsize != NULL);
1554 *rsize = sz;
1555
1556 return (ALLOCM_SUCCESS);
1557}
1558
Jason Evans6a0d2912010-09-20 16:44:23 -07001559JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001560JEMALLOC_ATTR(visibility("default"))
1561int
Jason Evans0a5489e2012-03-01 17:19:20 -08001562je_dallocm(void *ptr, int flags)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001563{
Jason Evanse4f78462010-10-22 10:45:59 -07001564 size_t usize;
Jason Evans122449b2012-04-06 00:35:09 -07001565 size_t rzsize JEMALLOC_CC_SILENCE_INIT(0);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001566
1567 assert(ptr != NULL);
Jason Evans41b6afb2012-02-02 22:04:57 -08001568 assert(malloc_initialized || IS_INITIALIZER);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001569
Jason Evansb1476112012-04-05 13:36:17 -07001570 UTRACE(ptr, 0, 0);
Jason Evans122449b2012-04-06 00:35:09 -07001571 if (config_stats || config_valgrind)
1572 usize = isalloc(ptr, config_prof);
Jason Evans7372b152012-02-10 20:22:09 -08001573 if (config_prof && opt_prof) {
Jason Evans122449b2012-04-06 00:35:09 -07001574 if (config_stats == false && config_valgrind == false)
1575 usize = isalloc(ptr, config_prof);
Jason Evanse4f78462010-10-22 10:45:59 -07001576 prof_free(ptr, usize);
1577 }
Jason Evans7372b152012-02-10 20:22:09 -08001578 if (config_stats)
Jason Evanscd9a1342012-03-21 18:33:03 -07001579 thread_allocated_tsd_get()->deallocated += usize;
Jason Evans122449b2012-04-06 00:35:09 -07001580 if (config_valgrind && opt_valgrind)
1581 rzsize = p2rz(ptr);
1582 iqalloc(ptr);
1583 JEMALLOC_VALGRIND_FREE(ptr, rzsize);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001584
1585 return (ALLOCM_SUCCESS);
1586}
1587
Jason Evans7e15dab2012-02-29 12:56:37 -08001588JEMALLOC_ATTR(visibility("default"))
1589int
Jason Evans0a5489e2012-03-01 17:19:20 -08001590je_nallocm(size_t *rsize, size_t size, int flags)
Jason Evans7e15dab2012-02-29 12:56:37 -08001591{
1592 size_t usize;
1593 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1594 & (SIZE_T_MAX-1));
1595
1596 assert(size != 0);
1597
1598 if (malloc_init())
1599 return (ALLOCM_ERR_OOM);
1600
Jason Evans5ff709c2012-04-11 18:13:45 -07001601 usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment);
Jason Evans7e15dab2012-02-29 12:56:37 -08001602 if (usize == 0)
1603 return (ALLOCM_ERR_OOM);
1604
1605 if (rsize != NULL)
1606 *rsize = usize;
1607 return (ALLOCM_SUCCESS);
1608}
1609
Jason Evans7e77eaf2012-03-02 17:47:37 -08001610#endif
Jason Evans289053c2009-06-22 12:08:42 -07001611/*
Jason Evans7e77eaf2012-03-02 17:47:37 -08001612 * End experimental functions.
Jason Evans289053c2009-06-22 12:08:42 -07001613 */
1614/******************************************************************************/
Jason Evans289053c2009-06-22 12:08:42 -07001615/*
1616 * The following functions are used by threading libraries for protection of
Jason Evans28177d42010-09-20 11:24:24 -07001617 * malloc during fork().
Jason Evans289053c2009-06-22 12:08:42 -07001618 */
1619
Jason Evans41b6afb2012-02-02 22:04:57 -08001620#ifndef JEMALLOC_MUTEX_INIT_CB
Jason Evans2dbecf12010-09-05 10:35:13 -07001621void
Jason Evans804c9ec2009-06-22 17:44:33 -07001622jemalloc_prefork(void)
Jason Evans41b6afb2012-02-02 22:04:57 -08001623#else
Jason Evans86e58582012-04-18 19:01:00 -07001624JEMALLOC_ATTR(visibility("default"))
Jason Evans41b6afb2012-02-02 22:04:57 -08001625void
1626_malloc_prefork(void)
1627#endif
Jason Evans289053c2009-06-22 12:08:42 -07001628{
Jason Evansfbbb6242010-01-24 17:56:48 -08001629 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -07001630
1631 /* Acquire all mutexes in a safe order. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001632 malloc_mutex_prefork(&arenas_lock);
Jason Evansfbbb6242010-01-24 17:56:48 -08001633 for (i = 0; i < narenas; i++) {
1634 if (arenas[i] != NULL)
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001635 arena_prefork(arenas[i]);
Jason Evansfbbb6242010-01-24 17:56:48 -08001636 }
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001637 base_prefork();
1638 huge_prefork();
1639 chunk_dss_prefork();
Jason Evans289053c2009-06-22 12:08:42 -07001640}
1641
Jason Evans41b6afb2012-02-02 22:04:57 -08001642#ifndef JEMALLOC_MUTEX_INIT_CB
Jason Evans2dbecf12010-09-05 10:35:13 -07001643void
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001644jemalloc_postfork_parent(void)
Jason Evans41b6afb2012-02-02 22:04:57 -08001645#else
Jason Evans86e58582012-04-18 19:01:00 -07001646JEMALLOC_ATTR(visibility("default"))
Jason Evans41b6afb2012-02-02 22:04:57 -08001647void
1648_malloc_postfork(void)
1649#endif
Jason Evans289053c2009-06-22 12:08:42 -07001650{
1651 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -07001652
1653 /* Release all mutexes, now that fork() has completed. */
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001654 chunk_dss_postfork_parent();
1655 huge_postfork_parent();
1656 base_postfork_parent();
Jason Evans289053c2009-06-22 12:08:42 -07001657 for (i = 0; i < narenas; i++) {
Jason Evansfbbb6242010-01-24 17:56:48 -08001658 if (arenas[i] != NULL)
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001659 arena_postfork_parent(arenas[i]);
Jason Evans289053c2009-06-22 12:08:42 -07001660 }
Jason Evans4e2e3dd2012-03-13 16:31:41 -07001661 malloc_mutex_postfork_parent(&arenas_lock);
1662}
1663
1664void
1665jemalloc_postfork_child(void)
1666{
1667 unsigned i;
1668
1669 /* Release all mutexes, now that fork() has completed. */
1670 chunk_dss_postfork_child();
1671 huge_postfork_child();
1672 base_postfork_child();
1673 for (i = 0; i < narenas; i++) {
1674 if (arenas[i] != NULL)
1675 arena_postfork_child(arenas[i]);
1676 }
1677 malloc_mutex_postfork_child(&arenas_lock);
Jason Evans289053c2009-06-22 12:08:42 -07001678}
Jason Evans2dbecf12010-09-05 10:35:13 -07001679
1680/******************************************************************************/
Jason Evans01b3fe52012-04-03 09:28:00 -07001681/*
1682 * The following functions are used for TLS allocation/deallocation in static
1683 * binaries on FreeBSD. The primary difference between these and i[mcd]alloc()
1684 * is that these avoid accessing TLS variables.
1685 */
1686
1687static void *
1688a0alloc(size_t size, bool zero)
1689{
1690
1691 if (malloc_init())
1692 return (NULL);
1693
1694 if (size == 0)
1695 size = 1;
1696
1697 if (size <= arena_maxclass)
1698 return (arena_malloc(arenas[0], size, zero, false));
1699 else
1700 return (huge_malloc(size, zero));
1701}
1702
1703void *
1704a0malloc(size_t size)
1705{
1706
1707 return (a0alloc(size, false));
1708}
1709
1710void *
1711a0calloc(size_t num, size_t size)
1712{
1713
1714 return (a0alloc(num * size, true));
1715}
1716
1717void
1718a0free(void *ptr)
1719{
1720 arena_chunk_t *chunk;
1721
1722 if (ptr == NULL)
1723 return;
1724
1725 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1726 if (chunk != ptr)
1727 arena_dalloc(chunk->arena, chunk, ptr, false);
1728 else
1729 huge_dalloc(ptr, true);
1730}
1731
1732/******************************************************************************/