blob: a32ce1a7c1a65183df5f3f5ddca3e08f1e4354b0 [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 Evans3c234352010-01-27 13:10:55 -08007malloc_mutex_t arenas_lock;
Jason Evanse476f8a2010-01-16 09:53:50 -08008arena_t **arenas;
9unsigned narenas;
Jason Evanse476f8a2010-01-16 09:53:50 -080010
Jason Evans597632b2011-03-18 13:41:33 -070011pthread_key_t arenas_tsd;
Jason Evanse476f8a2010-01-16 09:53:50 -080012#ifndef NO_TLS
Jason Evans2dbecf12010-09-05 10:35:13 -070013__thread arena_t *arenas_tls JEMALLOC_ATTR(tls_model("initial-exec"));
Jason Evanse476f8a2010-01-16 09:53:50 -080014#endif
Jason Evans289053c2009-06-22 12:08:42 -070015
Jason Evans7372b152012-02-10 20:22:09 -080016#ifndef NO_TLS
Jason Evans93443682010-10-20 17:39:18 -070017__thread thread_allocated_t thread_allocated_tls;
Jason Evans93443682010-10-20 17:39:18 -070018#endif
Jason Evans7372b152012-02-10 20:22:09 -080019pthread_key_t thread_allocated_tsd;
Jason Evans93443682010-10-20 17:39:18 -070020
Jason Evans289053c2009-06-22 12:08:42 -070021/* Set to true once the allocator has been initialized. */
Jason Evans93443682010-10-20 17:39:18 -070022static bool malloc_initialized = false;
Jason Evans289053c2009-06-22 12:08:42 -070023
Jason Evansb7924f52009-06-23 19:01:18 -070024/* Used to let the initializing thread recursively allocate. */
Jason Evans93443682010-10-20 17:39:18 -070025static pthread_t malloc_initializer = (unsigned long)0;
Jason Evansb7924f52009-06-23 19:01:18 -070026
Jason Evans289053c2009-06-22 12:08:42 -070027/* Used to avoid initialization races. */
Jason Evans7372b152012-02-10 20:22:09 -080028static malloc_mutex_t init_lock = MALLOC_MUTEX_INITIALIZER;
Jason Evans289053c2009-06-22 12:08:42 -070029
Jason Evansb7924f52009-06-23 19:01:18 -070030#ifdef DYNAMIC_PAGE_SHIFT
Jason Evanse476f8a2010-01-16 09:53:50 -080031size_t pagesize;
32size_t pagesize_mask;
33size_t lg_pagesize;
Jason Evansb7924f52009-06-23 19:01:18 -070034#endif
35
Jason Evanse476f8a2010-01-16 09:53:50 -080036unsigned ncpus;
Jason Evans289053c2009-06-22 12:08:42 -070037
Jason Evanse476f8a2010-01-16 09:53:50 -080038/* Runtime configuration options. */
Jason Evanse7339702010-10-23 18:37:06 -070039const char *JEMALLOC_P(malloc_conf) JEMALLOC_ATTR(visibility("default"));
Jason Evansb7924f52009-06-23 19:01:18 -070040#ifdef JEMALLOC_DEBUG
Jason Evanse476f8a2010-01-16 09:53:50 -080041bool opt_abort = true;
Jason Evansb7924f52009-06-23 19:01:18 -070042# ifdef JEMALLOC_FILL
Jason Evanse476f8a2010-01-16 09:53:50 -080043bool opt_junk = true;
Jason Evans7372b152012-02-10 20:22:09 -080044# else
45bool opt_junk = false;
Jason Evansb7924f52009-06-23 19:01:18 -070046# endif
Jason Evans289053c2009-06-22 12:08:42 -070047#else
Jason Evanse476f8a2010-01-16 09:53:50 -080048bool opt_abort = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080049bool opt_junk = false;
Jason Evans289053c2009-06-22 12:08:42 -070050#endif
Jason Evanse476f8a2010-01-16 09:53:50 -080051bool opt_sysv = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080052bool opt_xmalloc = false;
Jason Evanse476f8a2010-01-16 09:53:50 -080053bool opt_zero = false;
Jason Evanse7339702010-10-23 18:37:06 -070054size_t opt_narenas = 0;
Jason Evans289053c2009-06-22 12:08:42 -070055
Jason Evans289053c2009-06-22 12:08:42 -070056/******************************************************************************/
Jason Evanse476f8a2010-01-16 09:53:50 -080057/* Function prototypes for non-inline static functions. */
Jason Evans289053c2009-06-22 12:08:42 -070058
Jason Evans698805c2010-03-03 17:45:38 -080059static void wrtmessage(void *cbopaque, const char *s);
Jason Evans03c22372010-01-03 12:10:42 -080060static void stats_print_atexit(void);
Jason Evansc9658dd2009-06-22 14:44:08 -070061static unsigned malloc_ncpus(void);
Jason Evans597632b2011-03-18 13:41:33 -070062static void arenas_cleanup(void *arg);
Jason Evans7372b152012-02-10 20:22:09 -080063#ifdef NO_TLS
Jason Evans93443682010-10-20 17:39:18 -070064static void thread_allocated_cleanup(void *arg);
65#endif
Jason Evanse7339702010-10-23 18:37:06 -070066static bool malloc_conf_next(char const **opts_p, char const **k_p,
67 size_t *klen_p, char const **v_p, size_t *vlen_p);
68static void malloc_conf_error(const char *msg, const char *k, size_t klen,
69 const char *v, size_t vlen);
70static void malloc_conf_init(void);
Jason Evans289053c2009-06-22 12:08:42 -070071static bool malloc_init_hard(void);
Jason Evansa5070042011-08-12 13:48:27 -070072static int imemalign(void **memptr, size_t alignment, size_t size);
Jason Evans289053c2009-06-22 12:08:42 -070073
Jason Evans289053c2009-06-22 12:08:42 -070074/******************************************************************************/
Jason Evanse476f8a2010-01-16 09:53:50 -080075/* malloc_message() setup. */
Jason Evans289053c2009-06-22 12:08:42 -070076
Jason Evans7372b152012-02-10 20:22:09 -080077JEMALLOC_CATTR(visibility("hidden"), static)
Jason Evanse476f8a2010-01-16 09:53:50 -080078void
Jason Evans698805c2010-03-03 17:45:38 -080079wrtmessage(void *cbopaque, const char *s)
Jason Evansc9658dd2009-06-22 14:44:08 -070080{
Jason Evans7372b152012-02-10 20:22:09 -080081 UNUSED int result = write(STDERR_FILENO, s, strlen(s));
Jason Evansc9658dd2009-06-22 14:44:08 -070082}
83
Jason Evans698805c2010-03-03 17:45:38 -080084void (*JEMALLOC_P(malloc_message))(void *, const char *s)
85 JEMALLOC_ATTR(visibility("default")) = wrtmessage;
Jason Evansc9658dd2009-06-22 14:44:08 -070086
87/******************************************************************************/
88/*
Jason Evanse476f8a2010-01-16 09:53:50 -080089 * Begin miscellaneous support functions.
Jason Evansb7924f52009-06-23 19:01:18 -070090 */
91
Jason Evanse476f8a2010-01-16 09:53:50 -080092/* Create a new arena and insert it into the arenas array at index ind. */
93arena_t *
94arenas_extend(unsigned ind)
Jason Evans289053c2009-06-22 12:08:42 -070095{
96 arena_t *ret;
97
Jason Evanse476f8a2010-01-16 09:53:50 -080098 /* Allocate enough space for trailing bins. */
Jason Evansc2fc8c82010-10-01 18:02:43 -070099 ret = (arena_t *)base_alloc(offsetof(arena_t, bins)
100 + (sizeof(arena_bin_t) * nbins));
Jason Evanse476f8a2010-01-16 09:53:50 -0800101 if (ret != NULL && arena_new(ret, ind) == false) {
102 arenas[ind] = ret;
103 return (ret);
Jason Evans289053c2009-06-22 12:08:42 -0700104 }
Jason Evanse476f8a2010-01-16 09:53:50 -0800105 /* Only reached if there is an OOM error. */
Jason Evans289053c2009-06-22 12:08:42 -0700106
Jason Evanse476f8a2010-01-16 09:53:50 -0800107 /*
108 * OOM here is quite inconvenient to propagate, since dealing with it
109 * would require a check for failure in the fast path. Instead, punt
110 * by using arenas[0]. In practice, this is an extremely unlikely
111 * failure.
112 */
Jason Evans698805c2010-03-03 17:45:38 -0800113 malloc_write("<jemalloc>: Error initializing arena\n");
Jason Evanse476f8a2010-01-16 09:53:50 -0800114 if (opt_abort)
115 abort();
Jason Evans289053c2009-06-22 12:08:42 -0700116
Jason Evanse476f8a2010-01-16 09:53:50 -0800117 return (arenas[0]);
Jason Evans289053c2009-06-22 12:08:42 -0700118}
119
Jason Evans289053c2009-06-22 12:08:42 -0700120/*
121 * Choose an arena based on a per-thread value (slow-path code only, called
122 * only by choose_arena()).
123 */
Jason Evanse476f8a2010-01-16 09:53:50 -0800124arena_t *
Jason Evans289053c2009-06-22 12:08:42 -0700125choose_arena_hard(void)
126{
127 arena_t *ret;
128
Jason Evans289053c2009-06-22 12:08:42 -0700129 if (narenas > 1) {
Jason Evans597632b2011-03-18 13:41:33 -0700130 unsigned i, choose, first_null;
131
132 choose = 0;
133 first_null = narenas;
Jason Evans3ee7a5c2009-12-29 00:09:15 -0800134 malloc_mutex_lock(&arenas_lock);
Jason Evans0657f122011-03-18 17:56:14 -0700135 assert(arenas[0] != NULL);
Jason Evans597632b2011-03-18 13:41:33 -0700136 for (i = 1; i < narenas; i++) {
137 if (arenas[i] != NULL) {
138 /*
139 * Choose the first arena that has the lowest
140 * number of threads assigned to it.
141 */
142 if (arenas[i]->nthreads <
143 arenas[choose]->nthreads)
144 choose = i;
145 } else if (first_null == narenas) {
146 /*
147 * Record the index of the first uninitialized
148 * arena, in case all extant arenas are in use.
149 *
150 * NB: It is possible for there to be
151 * discontinuities in terms of initialized
152 * versus uninitialized arenas, due to the
153 * "thread.arena" mallctl.
154 */
155 first_null = i;
156 }
157 }
158
159 if (arenas[choose] == 0 || first_null == narenas) {
160 /*
161 * Use an unloaded arena, or the least loaded arena if
162 * all arenas are already initialized.
163 */
164 ret = arenas[choose];
165 } else {
166 /* Initialize a new arena. */
167 ret = arenas_extend(first_null);
168 }
169 ret->nthreads++;
Jason Evans3ee7a5c2009-12-29 00:09:15 -0800170 malloc_mutex_unlock(&arenas_lock);
Jason Evans597632b2011-03-18 13:41:33 -0700171 } else {
Jason Evans289053c2009-06-22 12:08:42 -0700172 ret = arenas[0];
Jason Evans597632b2011-03-18 13:41:33 -0700173 malloc_mutex_lock(&arenas_lock);
174 ret->nthreads++;
175 malloc_mutex_unlock(&arenas_lock);
176 }
Jason Evans289053c2009-06-22 12:08:42 -0700177
Jason Evans2dbecf12010-09-05 10:35:13 -0700178 ARENA_SET(ret);
Jason Evans289053c2009-06-22 12:08:42 -0700179
180 return (ret);
181}
Jason Evans289053c2009-06-22 12:08:42 -0700182
Jason Evansa09f55c2010-09-20 16:05:41 -0700183/*
184 * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so
185 * provide a wrapper.
186 */
187int
188buferror(int errnum, char *buf, size_t buflen)
189{
190#ifdef _GNU_SOURCE
191 char *b = strerror_r(errno, buf, buflen);
192 if (b != buf) {
193 strncpy(buf, b, buflen);
194 buf[buflen-1] = '\0';
195 }
196 return (0);
197#else
198 return (strerror_r(errno, buf, buflen));
199#endif
200}
201
Jason Evans03c22372010-01-03 12:10:42 -0800202static void
203stats_print_atexit(void)
204{
205
Jason Evans7372b152012-02-10 20:22:09 -0800206 if (config_tcache && config_stats) {
207 unsigned i;
Jason Evans03c22372010-01-03 12:10:42 -0800208
Jason Evans7372b152012-02-10 20:22:09 -0800209 /*
210 * Merge stats from extant threads. This is racy, since
211 * individual threads do not lock when recording tcache stats
212 * events. As a consequence, the final stats may be slightly
213 * out of date by the time they are reported, if other threads
214 * continue to allocate.
215 */
216 for (i = 0; i < narenas; i++) {
217 arena_t *arena = arenas[i];
218 if (arena != NULL) {
219 tcache_t *tcache;
Jason Evans03c22372010-01-03 12:10:42 -0800220
Jason Evans7372b152012-02-10 20:22:09 -0800221 /*
222 * tcache_stats_merge() locks bins, so if any
223 * code is introduced that acquires both arena
224 * and bin locks in the opposite order,
225 * deadlocks may result.
226 */
227 malloc_mutex_lock(&arena->lock);
228 ql_foreach(tcache, &arena->tcache_ql, link) {
229 tcache_stats_merge(tcache, arena);
230 }
231 malloc_mutex_unlock(&arena->lock);
Jason Evans03c22372010-01-03 12:10:42 -0800232 }
Jason Evans03c22372010-01-03 12:10:42 -0800233 }
234 }
Jason Evansed1bf452010-01-19 12:11:25 -0800235 JEMALLOC_P(malloc_stats_print)(NULL, NULL, NULL);
Jason Evans289053c2009-06-22 12:08:42 -0700236}
237
Jason Evans9dcad2d2011-02-13 18:11:54 -0800238thread_allocated_t *
239thread_allocated_get_hard(void)
240{
241 thread_allocated_t *thread_allocated = (thread_allocated_t *)
242 imalloc(sizeof(thread_allocated_t));
243 if (thread_allocated == NULL) {
244 static thread_allocated_t static_thread_allocated = {0, 0};
245 malloc_write("<jemalloc>: Error allocating TSD;"
246 " mallctl(\"thread.{de,}allocated[p]\", ...)"
247 " will be inaccurate\n");
248 if (opt_abort)
249 abort();
250 return (&static_thread_allocated);
251 }
252 pthread_setspecific(thread_allocated_tsd, thread_allocated);
253 thread_allocated->allocated = 0;
254 thread_allocated->deallocated = 0;
255 return (thread_allocated);
256}
Jason Evans9dcad2d2011-02-13 18:11:54 -0800257
Jason Evans289053c2009-06-22 12:08:42 -0700258/*
Jason Evanse476f8a2010-01-16 09:53:50 -0800259 * End miscellaneous support functions.
Jason Evans289053c2009-06-22 12:08:42 -0700260 */
261/******************************************************************************/
262/*
Jason Evanse476f8a2010-01-16 09:53:50 -0800263 * Begin initialization functions.
Jason Evans289053c2009-06-22 12:08:42 -0700264 */
265
Jason Evansc9658dd2009-06-22 14:44:08 -0700266static unsigned
267malloc_ncpus(void)
268{
269 unsigned ret;
Jason Evansb7924f52009-06-23 19:01:18 -0700270 long result;
Jason Evansc9658dd2009-06-22 14:44:08 -0700271
Jason Evansb7924f52009-06-23 19:01:18 -0700272 result = sysconf(_SC_NPROCESSORS_ONLN);
273 if (result == -1) {
274 /* Error. */
275 ret = 1;
Jason Evansc9658dd2009-06-22 14:44:08 -0700276 }
Jason Evansb7924f52009-06-23 19:01:18 -0700277 ret = (unsigned)result;
Jason Evansc9658dd2009-06-22 14:44:08 -0700278
279 return (ret);
280}
Jason Evansb7924f52009-06-23 19:01:18 -0700281
Jason Evans597632b2011-03-18 13:41:33 -0700282static void
283arenas_cleanup(void *arg)
284{
285 arena_t *arena = (arena_t *)arg;
286
287 malloc_mutex_lock(&arenas_lock);
288 arena->nthreads--;
289 malloc_mutex_unlock(&arenas_lock);
290}
291
Jason Evans7372b152012-02-10 20:22:09 -0800292#ifdef NO_TLS
Jason Evans93443682010-10-20 17:39:18 -0700293static void
294thread_allocated_cleanup(void *arg)
295{
296 uint64_t *allocated = (uint64_t *)arg;
297
298 if (allocated != NULL)
299 idalloc(allocated);
300}
301#endif
302
Jason Evans289053c2009-06-22 12:08:42 -0700303/*
304 * FreeBSD's pthreads implementation calls malloc(3), so the malloc
305 * implementation has to take pains to avoid infinite recursion during
306 * initialization.
307 */
308static inline bool
309malloc_init(void)
310{
311
312 if (malloc_initialized == false)
313 return (malloc_init_hard());
314
315 return (false);
316}
317
318static bool
Jason Evanse7339702010-10-23 18:37:06 -0700319malloc_conf_next(char const **opts_p, char const **k_p, size_t *klen_p,
320 char const **v_p, size_t *vlen_p)
321{
322 bool accept;
323 const char *opts = *opts_p;
324
325 *k_p = opts;
326
327 for (accept = false; accept == false;) {
328 switch (*opts) {
329 case 'A': case 'B': case 'C': case 'D': case 'E':
330 case 'F': case 'G': case 'H': case 'I': case 'J':
331 case 'K': case 'L': case 'M': case 'N': case 'O':
332 case 'P': case 'Q': case 'R': case 'S': case 'T':
333 case 'U': case 'V': case 'W': case 'X': case 'Y':
334 case 'Z':
335 case 'a': case 'b': case 'c': case 'd': case 'e':
336 case 'f': case 'g': case 'h': case 'i': case 'j':
337 case 'k': case 'l': case 'm': case 'n': case 'o':
338 case 'p': case 'q': case 'r': case 's': case 't':
339 case 'u': case 'v': case 'w': case 'x': case 'y':
340 case 'z':
341 case '0': case '1': case '2': case '3': case '4':
342 case '5': case '6': case '7': case '8': case '9':
343 case '_':
344 opts++;
345 break;
346 case ':':
347 opts++;
348 *klen_p = (uintptr_t)opts - 1 - (uintptr_t)*k_p;
349 *v_p = opts;
350 accept = true;
351 break;
352 case '\0':
353 if (opts != *opts_p) {
354 malloc_write("<jemalloc>: Conf string "
355 "ends with key\n");
356 }
357 return (true);
358 default:
359 malloc_write("<jemalloc>: Malformed conf "
360 "string\n");
361 return (true);
362 }
363 }
364
365 for (accept = false; accept == false;) {
366 switch (*opts) {
367 case ',':
368 opts++;
369 /*
370 * Look ahead one character here, because the
371 * next time this function is called, it will
372 * assume that end of input has been cleanly
373 * reached if no input remains, but we have
374 * optimistically already consumed the comma if
375 * one exists.
376 */
377 if (*opts == '\0') {
378 malloc_write("<jemalloc>: Conf string "
379 "ends with comma\n");
380 }
381 *vlen_p = (uintptr_t)opts - 1 - (uintptr_t)*v_p;
382 accept = true;
383 break;
384 case '\0':
385 *vlen_p = (uintptr_t)opts - (uintptr_t)*v_p;
386 accept = true;
387 break;
388 default:
389 opts++;
390 break;
391 }
392 }
393
394 *opts_p = opts;
395 return (false);
396}
397
398static void
399malloc_conf_error(const char *msg, const char *k, size_t klen, const char *v,
400 size_t vlen)
401{
402 char buf[PATH_MAX + 1];
403
404 malloc_write("<jemalloc>: ");
405 malloc_write(msg);
406 malloc_write(": ");
407 memcpy(buf, k, klen);
408 memcpy(&buf[klen], ":", 1);
409 memcpy(&buf[klen+1], v, vlen);
410 buf[klen+1+vlen] = '\0';
411 malloc_write(buf);
412 malloc_write("\n");
413}
414
415static void
416malloc_conf_init(void)
Jason Evans289053c2009-06-22 12:08:42 -0700417{
418 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -0700419 char buf[PATH_MAX + 1];
Jason Evanse7339702010-10-23 18:37:06 -0700420 const char *opts, *k, *v;
421 size_t klen, vlen;
422
423 for (i = 0; i < 3; i++) {
424 /* Get runtime configuration. */
425 switch (i) {
426 case 0:
427 if (JEMALLOC_P(malloc_conf) != NULL) {
428 /*
429 * Use options that were compiled into the
430 * program.
431 */
432 opts = JEMALLOC_P(malloc_conf);
433 } else {
434 /* No configuration specified. */
435 buf[0] = '\0';
436 opts = buf;
437 }
438 break;
439 case 1: {
440 int linklen;
441 const char *linkname =
442#ifdef JEMALLOC_PREFIX
443 "/etc/"JEMALLOC_PREFIX"malloc.conf"
444#else
445 "/etc/malloc.conf"
446#endif
447 ;
448
449 if ((linklen = readlink(linkname, buf,
450 sizeof(buf) - 1)) != -1) {
451 /*
452 * Use the contents of the "/etc/malloc.conf"
453 * symbolic link's name.
454 */
455 buf[linklen] = '\0';
456 opts = buf;
457 } else {
458 /* No configuration specified. */
459 buf[0] = '\0';
460 opts = buf;
461 }
462 break;
463 }
464 case 2: {
465 const char *envname =
466#ifdef JEMALLOC_PREFIX
467 JEMALLOC_CPREFIX"MALLOC_CONF"
468#else
469 "MALLOC_CONF"
470#endif
471 ;
472
473 if ((opts = getenv(envname)) != NULL) {
474 /*
475 * Do nothing; opts is already initialized to
Jason Evans8ad0eac2010-12-17 18:07:53 -0800476 * the value of the MALLOC_CONF environment
477 * variable.
Jason Evanse7339702010-10-23 18:37:06 -0700478 */
479 } else {
480 /* No configuration specified. */
481 buf[0] = '\0';
482 opts = buf;
483 }
484 break;
485 }
486 default:
487 /* NOTREACHED */
488 assert(false);
489 buf[0] = '\0';
490 opts = buf;
491 }
492
493 while (*opts != '\0' && malloc_conf_next(&opts, &k, &klen, &v,
494 &vlen) == false) {
495#define CONF_HANDLE_BOOL(n) \
496 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
497 klen) == 0) { \
498 if (strncmp("true", v, vlen) == 0 && \
499 vlen == sizeof("true")-1) \
500 opt_##n = true; \
501 else if (strncmp("false", v, vlen) == \
502 0 && vlen == sizeof("false")-1) \
503 opt_##n = false; \
504 else { \
505 malloc_conf_error( \
506 "Invalid conf value", \
507 k, klen, v, vlen); \
508 } \
509 continue; \
510 }
511#define CONF_HANDLE_SIZE_T(n, min, max) \
512 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
513 klen) == 0) { \
514 unsigned long ul; \
515 char *end; \
516 \
517 errno = 0; \
518 ul = strtoul(v, &end, 0); \
519 if (errno != 0 || (uintptr_t)end - \
520 (uintptr_t)v != vlen) { \
521 malloc_conf_error( \
522 "Invalid conf value", \
523 k, klen, v, vlen); \
524 } else if (ul < min || ul > max) { \
525 malloc_conf_error( \
526 "Out-of-range conf value", \
527 k, klen, v, vlen); \
528 } else \
529 opt_##n = ul; \
530 continue; \
531 }
532#define CONF_HANDLE_SSIZE_T(n, min, max) \
533 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
534 klen) == 0) { \
535 long l; \
536 char *end; \
537 \
538 errno = 0; \
539 l = strtol(v, &end, 0); \
540 if (errno != 0 || (uintptr_t)end - \
541 (uintptr_t)v != vlen) { \
542 malloc_conf_error( \
543 "Invalid conf value", \
544 k, klen, v, vlen); \
545 } else if (l < (ssize_t)min || l > \
546 (ssize_t)max) { \
547 malloc_conf_error( \
548 "Out-of-range conf value", \
549 k, klen, v, vlen); \
550 } else \
551 opt_##n = l; \
552 continue; \
553 }
554#define CONF_HANDLE_CHAR_P(n, d) \
555 if (sizeof(#n)-1 == klen && strncmp(#n, k, \
556 klen) == 0) { \
557 size_t cpylen = (vlen <= \
558 sizeof(opt_##n)-1) ? vlen : \
559 sizeof(opt_##n)-1; \
560 strncpy(opt_##n, v, cpylen); \
561 opt_##n[cpylen] = '\0'; \
562 continue; \
563 }
564
565 CONF_HANDLE_BOOL(abort)
566 CONF_HANDLE_SIZE_T(lg_qspace_max, LG_QUANTUM,
567 PAGE_SHIFT-1)
568 CONF_HANDLE_SIZE_T(lg_cspace_max, LG_QUANTUM,
569 PAGE_SHIFT-1)
570 /*
571 * Chunks always require at least one * header page,
572 * plus one data page.
573 */
574 CONF_HANDLE_SIZE_T(lg_chunk, PAGE_SHIFT+1,
575 (sizeof(size_t) << 3) - 1)
576 CONF_HANDLE_SIZE_T(narenas, 1, SIZE_T_MAX)
577 CONF_HANDLE_SSIZE_T(lg_dirty_mult, -1,
578 (sizeof(size_t) << 3) - 1)
579 CONF_HANDLE_BOOL(stats_print)
Jason Evans7372b152012-02-10 20:22:09 -0800580 if (config_fill) {
581 CONF_HANDLE_BOOL(junk)
582 CONF_HANDLE_BOOL(zero)
583 }
584 if (config_sysv) {
585 CONF_HANDLE_BOOL(sysv)
586 }
587 if (config_xmalloc) {
588 CONF_HANDLE_BOOL(xmalloc)
589 }
590 if (config_tcache) {
591 CONF_HANDLE_BOOL(tcache)
592 CONF_HANDLE_SSIZE_T(lg_tcache_gc_sweep, -1,
593 (sizeof(size_t) << 3) - 1)
594 CONF_HANDLE_SSIZE_T(lg_tcache_max, -1,
595 (sizeof(size_t) << 3) - 1)
596 }
597 if (config_prof) {
598 CONF_HANDLE_BOOL(prof)
599 CONF_HANDLE_CHAR_P(prof_prefix, "jeprof")
600 CONF_HANDLE_SIZE_T(lg_prof_bt_max, 0,
601 LG_PROF_BT_MAX)
602 CONF_HANDLE_BOOL(prof_active)
603 CONF_HANDLE_SSIZE_T(lg_prof_sample, 0,
604 (sizeof(uint64_t) << 3) - 1)
605 CONF_HANDLE_BOOL(prof_accum)
606 CONF_HANDLE_SSIZE_T(lg_prof_tcmax, -1,
607 (sizeof(size_t) << 3) - 1)
608 CONF_HANDLE_SSIZE_T(lg_prof_interval, -1,
609 (sizeof(uint64_t) << 3) - 1)
610 CONF_HANDLE_BOOL(prof_gdump)
611 CONF_HANDLE_BOOL(prof_leak)
612 }
Jason Evanse7339702010-10-23 18:37:06 -0700613 malloc_conf_error("Invalid conf pair", k, klen, v,
614 vlen);
615#undef CONF_HANDLE_BOOL
616#undef CONF_HANDLE_SIZE_T
617#undef CONF_HANDLE_SSIZE_T
618#undef CONF_HANDLE_CHAR_P
619 }
620
621 /* Validate configuration of options that are inter-related. */
622 if (opt_lg_qspace_max+1 >= opt_lg_cspace_max) {
623 malloc_write("<jemalloc>: Invalid lg_[qc]space_max "
624 "relationship; restoring defaults\n");
625 opt_lg_qspace_max = LG_QSPACE_MAX_DEFAULT;
626 opt_lg_cspace_max = LG_CSPACE_MAX_DEFAULT;
627 }
628 }
629}
630
631static bool
632malloc_init_hard(void)
633{
Jason Evansb7924f52009-06-23 19:01:18 -0700634 arena_t *init_arenas[1];
Jason Evans289053c2009-06-22 12:08:42 -0700635
636 malloc_mutex_lock(&init_lock);
Jason Evansb7924f52009-06-23 19:01:18 -0700637 if (malloc_initialized || malloc_initializer == pthread_self()) {
Jason Evans289053c2009-06-22 12:08:42 -0700638 /*
639 * Another thread initialized the allocator before this one
Jason Evansa25d0a82009-11-09 14:57:38 -0800640 * acquired init_lock, or this thread is the initializing
641 * thread, and it is recursively allocating.
Jason Evans289053c2009-06-22 12:08:42 -0700642 */
643 malloc_mutex_unlock(&init_lock);
644 return (false);
645 }
Jason Evansb7924f52009-06-23 19:01:18 -0700646 if (malloc_initializer != (unsigned long)0) {
647 /* Busy-wait until the initializing thread completes. */
648 do {
649 malloc_mutex_unlock(&init_lock);
650 CPU_SPINWAIT;
651 malloc_mutex_lock(&init_lock);
652 } while (malloc_initialized == false);
Jason Evans2541e1b2010-07-22 11:35:59 -0700653 malloc_mutex_unlock(&init_lock);
Jason Evansb7924f52009-06-23 19:01:18 -0700654 return (false);
655 }
Jason Evans289053c2009-06-22 12:08:42 -0700656
Jason Evansb7924f52009-06-23 19:01:18 -0700657#ifdef DYNAMIC_PAGE_SHIFT
Jason Evansc9658dd2009-06-22 14:44:08 -0700658 /* Get page size. */
659 {
660 long result;
661
662 result = sysconf(_SC_PAGESIZE);
663 assert(result != -1);
Jason Evans30fbef82011-11-05 21:06:55 -0700664 pagesize = (size_t)result;
Jason Evansb7924f52009-06-23 19:01:18 -0700665
666 /*
667 * We assume that pagesize is a power of 2 when calculating
Jason Evans94ad2b52009-12-29 00:09:15 -0800668 * pagesize_mask and lg_pagesize.
Jason Evansb7924f52009-06-23 19:01:18 -0700669 */
670 assert(((result - 1) & result) == 0);
671 pagesize_mask = result - 1;
Jason Evans94ad2b52009-12-29 00:09:15 -0800672 lg_pagesize = ffs((int)result) - 1;
Jason Evans289053c2009-06-22 12:08:42 -0700673 }
Jason Evansc9658dd2009-06-22 14:44:08 -0700674#endif
Jason Evans289053c2009-06-22 12:08:42 -0700675
Jason Evans7372b152012-02-10 20:22:09 -0800676 if (config_prof)
677 prof_boot0();
Jason Evans289053c2009-06-22 12:08:42 -0700678
Jason Evanse7339702010-10-23 18:37:06 -0700679 malloc_conf_init();
Jason Evans289053c2009-06-22 12:08:42 -0700680
Jason Evansa0bf2422010-01-29 14:30:41 -0800681 /* Register fork handlers. */
682 if (pthread_atfork(jemalloc_prefork, jemalloc_postfork,
683 jemalloc_postfork) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -0800684 malloc_write("<jemalloc>: Error in pthread_atfork()\n");
Jason Evansa0bf2422010-01-29 14:30:41 -0800685 if (opt_abort)
686 abort();
687 }
688
Jason Evans3c234352010-01-27 13:10:55 -0800689 if (ctl_boot()) {
690 malloc_mutex_unlock(&init_lock);
691 return (true);
692 }
693
Jason Evans03c22372010-01-03 12:10:42 -0800694 if (opt_stats_print) {
Jason Evans289053c2009-06-22 12:08:42 -0700695 /* Print statistics at exit. */
Jason Evansa0bf2422010-01-29 14:30:41 -0800696 if (atexit(stats_print_atexit) != 0) {
Jason Evans698805c2010-03-03 17:45:38 -0800697 malloc_write("<jemalloc>: Error in atexit()\n");
Jason Evansa0bf2422010-01-29 14:30:41 -0800698 if (opt_abort)
699 abort();
700 }
Jason Evans289053c2009-06-22 12:08:42 -0700701 }
702
Jason Evansa0bf2422010-01-29 14:30:41 -0800703 if (chunk_boot()) {
704 malloc_mutex_unlock(&init_lock);
705 return (true);
706 }
Jason Evansc9658dd2009-06-22 14:44:08 -0700707
Jason Evans3c234352010-01-27 13:10:55 -0800708 if (base_boot()) {
709 malloc_mutex_unlock(&init_lock);
710 return (true);
711 }
712
Jason Evans7372b152012-02-10 20:22:09 -0800713 if (config_prof)
714 prof_boot1();
Jason Evans3383af62010-02-11 08:59:06 -0800715
Jason Evansa0bf2422010-01-29 14:30:41 -0800716 if (arena_boot()) {
Jason Evans289053c2009-06-22 12:08:42 -0700717 malloc_mutex_unlock(&init_lock);
718 return (true);
719 }
720
Jason Evans7372b152012-02-10 20:22:09 -0800721 if (config_tcache && tcache_boot()) {
Jason Evans84c8eef2011-03-16 10:30:13 -0700722 malloc_mutex_unlock(&init_lock);
723 return (true);
724 }
Jason Evans84cbbcb2009-12-29 00:09:15 -0800725
Jason Evanse476f8a2010-01-16 09:53:50 -0800726 if (huge_boot()) {
Jason Evansc9658dd2009-06-22 14:44:08 -0700727 malloc_mutex_unlock(&init_lock);
728 return (true);
729 }
Jason Evans289053c2009-06-22 12:08:42 -0700730
Jason Evans7372b152012-02-10 20:22:09 -0800731#ifdef NO_TLS
Jason Evans93443682010-10-20 17:39:18 -0700732 /* Initialize allocation counters before any allocations can occur. */
Jason Evans7372b152012-02-10 20:22:09 -0800733 if (config_stats && pthread_key_create(&thread_allocated_tsd,
734 thread_allocated_cleanup) != 0) {
Jason Evans93443682010-10-20 17:39:18 -0700735 malloc_mutex_unlock(&init_lock);
736 return (true);
737 }
738#endif
739
Jason Evans8e6f8b42011-11-03 18:40:03 -0700740 if (malloc_mutex_init(&arenas_lock))
741 return (true);
742
743 if (pthread_key_create(&arenas_tsd, arenas_cleanup) != 0) {
744 malloc_mutex_unlock(&init_lock);
745 return (true);
746 }
747
Jason Evansb7924f52009-06-23 19:01:18 -0700748 /*
749 * Create enough scaffolding to allow recursive allocation in
750 * malloc_ncpus().
751 */
752 narenas = 1;
753 arenas = init_arenas;
754 memset(arenas, 0, sizeof(arena_t *) * narenas);
755
756 /*
757 * Initialize one arena here. The rest are lazily created in
758 * choose_arena_hard().
759 */
760 arenas_extend(0);
761 if (arenas[0] == NULL) {
762 malloc_mutex_unlock(&init_lock);
763 return (true);
764 }
765
Jason Evansb7924f52009-06-23 19:01:18 -0700766 /*
767 * Assign the initial arena to the initial thread, in order to avoid
768 * spurious creation of an extra arena if the application switches to
769 * threaded mode.
770 */
Jason Evans2dbecf12010-09-05 10:35:13 -0700771 ARENA_SET(arenas[0]);
Jason Evans597632b2011-03-18 13:41:33 -0700772 arenas[0]->nthreads++;
Jason Evansb7924f52009-06-23 19:01:18 -0700773
Jason Evans7372b152012-02-10 20:22:09 -0800774 if (config_prof && prof_boot2()) {
Jason Evans3383af62010-02-11 08:59:06 -0800775 malloc_mutex_unlock(&init_lock);
776 return (true);
777 }
Jason Evans3383af62010-02-11 08:59:06 -0800778
Jason Evansb7924f52009-06-23 19:01:18 -0700779 /* Get number of CPUs. */
780 malloc_initializer = pthread_self();
781 malloc_mutex_unlock(&init_lock);
782 ncpus = malloc_ncpus();
783 malloc_mutex_lock(&init_lock);
784
Jason Evanse7339702010-10-23 18:37:06 -0700785 if (opt_narenas == 0) {
Jason Evans289053c2009-06-22 12:08:42 -0700786 /*
Jason Evans5463a522009-12-29 00:09:15 -0800787 * For SMP systems, create more than one arena per CPU by
788 * default.
Jason Evans289053c2009-06-22 12:08:42 -0700789 */
Jason Evanse7339702010-10-23 18:37:06 -0700790 if (ncpus > 1)
791 opt_narenas = ncpus << 2;
792 else
793 opt_narenas = 1;
Jason Evans289053c2009-06-22 12:08:42 -0700794 }
Jason Evanse7339702010-10-23 18:37:06 -0700795 narenas = opt_narenas;
796 /*
797 * Make sure that the arenas array can be allocated. In practice, this
798 * limit is enough to allow the allocator to function, but the ctl
799 * machinery will fail to allocate memory at far lower limits.
800 */
801 if (narenas > chunksize / sizeof(arena_t *)) {
802 char buf[UMAX2S_BUFSIZE];
Jason Evans289053c2009-06-22 12:08:42 -0700803
Jason Evanse7339702010-10-23 18:37:06 -0700804 narenas = chunksize / sizeof(arena_t *);
805 malloc_write("<jemalloc>: Reducing narenas to limit (");
806 malloc_write(u2s(narenas, 10, buf));
807 malloc_write(")\n");
Jason Evans289053c2009-06-22 12:08:42 -0700808 }
Jason Evans289053c2009-06-22 12:08:42 -0700809
Jason Evans289053c2009-06-22 12:08:42 -0700810 /* Allocate and initialize arenas. */
811 arenas = (arena_t **)base_alloc(sizeof(arena_t *) * narenas);
812 if (arenas == NULL) {
813 malloc_mutex_unlock(&init_lock);
814 return (true);
815 }
816 /*
817 * Zero the array. In practice, this should always be pre-zeroed,
818 * since it was just mmap()ed, but let's be sure.
819 */
820 memset(arenas, 0, sizeof(arena_t *) * narenas);
Jason Evansb7924f52009-06-23 19:01:18 -0700821 /* Copy the pointer to the one arena that was already initialized. */
822 arenas[0] = init_arenas[0];
Jason Evans289053c2009-06-22 12:08:42 -0700823
Jason Evans2dbecf12010-09-05 10:35:13 -0700824#ifdef JEMALLOC_ZONE
825 /* Register the custom zone. */
826 malloc_zone_register(create_zone());
827
828 /*
829 * Convert the default szone to an "overlay zone" that is capable of
830 * deallocating szone-allocated objects, but allocating new objects
831 * from jemalloc.
832 */
833 szone2ozone(malloc_default_zone());
834#endif
835
Jason Evans289053c2009-06-22 12:08:42 -0700836 malloc_initialized = true;
837 malloc_mutex_unlock(&init_lock);
838 return (false);
839}
840
Jason Evans2dbecf12010-09-05 10:35:13 -0700841#ifdef JEMALLOC_ZONE
842JEMALLOC_ATTR(constructor)
843void
844jemalloc_darwin_init(void)
845{
846
847 if (malloc_init_hard())
848 abort();
849}
850#endif
851
Jason Evans289053c2009-06-22 12:08:42 -0700852/*
Jason Evanse476f8a2010-01-16 09:53:50 -0800853 * End initialization functions.
Jason Evans289053c2009-06-22 12:08:42 -0700854 */
855/******************************************************************************/
856/*
857 * Begin malloc(3)-compatible functions.
858 */
859
Jason Evans9ad48232010-01-03 11:59:20 -0800860JEMALLOC_ATTR(malloc)
Jason Evanse476f8a2010-01-16 09:53:50 -0800861JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -0700862void *
Jason Evanse476f8a2010-01-16 09:53:50 -0800863JEMALLOC_P(malloc)(size_t size)
Jason Evans289053c2009-06-22 12:08:42 -0700864{
865 void *ret;
Jason Evans7372b152012-02-10 20:22:09 -0800866 size_t usize;
867 prof_thr_cnt_t *cnt;
Jason Evans289053c2009-06-22 12:08:42 -0700868
869 if (malloc_init()) {
870 ret = NULL;
Jason Evansf2518142009-12-29 00:09:15 -0800871 goto OOM;
Jason Evans289053c2009-06-22 12:08:42 -0700872 }
873
874 if (size == 0) {
Jason Evans7372b152012-02-10 20:22:09 -0800875 if (config_sysv == false || opt_sysv == false)
Jason Evans289053c2009-06-22 12:08:42 -0700876 size = 1;
877 else {
Jason Evans7372b152012-02-10 20:22:09 -0800878 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -0800879 malloc_write("<jemalloc>: Error in malloc(): "
880 "invalid size 0\n");
Jason Evansf2518142009-12-29 00:09:15 -0800881 abort();
882 }
Jason Evans289053c2009-06-22 12:08:42 -0700883 ret = NULL;
884 goto RETURN;
885 }
886 }
887
Jason Evans7372b152012-02-10 20:22:09 -0800888 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -0700889 usize = s2u(size);
Jason Evansa5070042011-08-12 13:48:27 -0700890 PROF_ALLOC_PREP(1, usize, cnt);
891 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700892 ret = NULL;
893 goto OOM;
894 }
Jason Evans93443682010-10-20 17:39:18 -0700895 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <=
Jason Evans0b270a92010-03-31 16:45:04 -0700896 small_maxclass) {
897 ret = imalloc(small_maxclass+1);
898 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700899 arena_prof_promoted(ret, usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700900 } else
901 ret = imalloc(size);
Jason Evans7372b152012-02-10 20:22:09 -0800902 } else {
903 if (config_stats)
904 usize = s2u(size);
Jason Evans0b270a92010-03-31 16:45:04 -0700905 ret = imalloc(size);
Jason Evans93443682010-10-20 17:39:18 -0700906 }
Jason Evans289053c2009-06-22 12:08:42 -0700907
Jason Evansf2518142009-12-29 00:09:15 -0800908OOM:
Jason Evans289053c2009-06-22 12:08:42 -0700909 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -0800910 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -0800911 malloc_write("<jemalloc>: Error in malloc(): "
912 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -0700913 abort();
914 }
915 errno = ENOMEM;
916 }
917
Jason Evansf2518142009-12-29 00:09:15 -0800918RETURN:
Jason Evans7372b152012-02-10 20:22:09 -0800919 if (config_prof && opt_prof && ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -0700920 prof_malloc(ret, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -0800921 if (config_stats && ret != NULL) {
Jason Evans93443682010-10-20 17:39:18 -0700922 assert(usize == isalloc(ret));
923 ALLOCATED_ADD(usize, 0);
924 }
Jason Evans289053c2009-06-22 12:08:42 -0700925 return (ret);
926}
927
Jason Evans9ad48232010-01-03 11:59:20 -0800928JEMALLOC_ATTR(nonnull(1))
Jason Evansa5070042011-08-12 13:48:27 -0700929#ifdef JEMALLOC_PROF
930/*
Jason Evans7372b152012-02-10 20:22:09 -0800931 * Avoid any uncertainty as to how many backtrace frames to ignore in
Jason Evansa5070042011-08-12 13:48:27 -0700932 * PROF_ALLOC_PREP().
933 */
934JEMALLOC_ATTR(noinline)
935#endif
936static int
937imemalign(void **memptr, size_t alignment, size_t size)
Jason Evans289053c2009-06-22 12:08:42 -0700938{
939 int ret;
Jason Evans7372b152012-02-10 20:22:09 -0800940 size_t usize;
Jason Evans38d92102011-03-23 00:37:29 -0700941 void *result;
Jason Evans7372b152012-02-10 20:22:09 -0800942 prof_thr_cnt_t *cnt;
Jason Evans289053c2009-06-22 12:08:42 -0700943
944 if (malloc_init())
945 result = NULL;
946 else {
Jason Evansf2518142009-12-29 00:09:15 -0800947 if (size == 0) {
Jason Evans7372b152012-02-10 20:22:09 -0800948 if (config_sysv == false || opt_sysv == false)
Jason Evansf2518142009-12-29 00:09:15 -0800949 size = 1;
Jason Evansf2518142009-12-29 00:09:15 -0800950 else {
Jason Evans7372b152012-02-10 20:22:09 -0800951 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -0800952 malloc_write("<jemalloc>: Error in "
953 "posix_memalign(): invalid size "
954 "0\n");
Jason Evansf2518142009-12-29 00:09:15 -0800955 abort();
956 }
Jason Evansf2518142009-12-29 00:09:15 -0800957 result = NULL;
958 *memptr = NULL;
959 ret = 0;
960 goto RETURN;
961 }
Jason Evansf2518142009-12-29 00:09:15 -0800962 }
963
Jason Evans289053c2009-06-22 12:08:42 -0700964 /* Make sure that alignment is a large enough power of 2. */
965 if (((alignment - 1) & alignment) != 0
966 || alignment < sizeof(void *)) {
Jason Evans7372b152012-02-10 20:22:09 -0800967 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -0800968 malloc_write("<jemalloc>: Error in "
969 "posix_memalign(): invalid alignment\n");
Jason Evans289053c2009-06-22 12:08:42 -0700970 abort();
971 }
972 result = NULL;
973 ret = EINVAL;
974 goto RETURN;
975 }
976
Jason Evans38d92102011-03-23 00:37:29 -0700977 usize = sa2u(size, alignment, NULL);
978 if (usize == 0) {
979 result = NULL;
980 ret = ENOMEM;
981 goto RETURN;
982 }
983
Jason Evans7372b152012-02-10 20:22:09 -0800984 if (config_prof && opt_prof) {
Jason Evansa5070042011-08-12 13:48:27 -0700985 PROF_ALLOC_PREP(2, usize, cnt);
986 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -0700987 result = NULL;
988 ret = EINVAL;
989 } else {
990 if (prof_promote && (uintptr_t)cnt !=
Jason Evans93443682010-10-20 17:39:18 -0700991 (uintptr_t)1U && usize <= small_maxclass) {
Jason Evans38d92102011-03-23 00:37:29 -0700992 assert(sa2u(small_maxclass+1,
993 alignment, NULL) != 0);
994 result = ipalloc(sa2u(small_maxclass+1,
995 alignment, NULL), alignment, false);
Jason Evans0b270a92010-03-31 16:45:04 -0700996 if (result != NULL) {
997 arena_prof_promoted(result,
Jason Evans93443682010-10-20 17:39:18 -0700998 usize);
Jason Evans0b270a92010-03-31 16:45:04 -0700999 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001000 } else {
Jason Evans38d92102011-03-23 00:37:29 -07001001 result = ipalloc(usize, alignment,
Jason Evans8e3c3c62010-09-17 15:46:18 -07001002 false);
1003 }
Jason Evans0b270a92010-03-31 16:45:04 -07001004 }
Jason Evans6109fe02010-02-10 10:37:56 -08001005 } else
Jason Evans38d92102011-03-23 00:37:29 -07001006 result = ipalloc(usize, alignment, false);
Jason Evans289053c2009-06-22 12:08:42 -07001007 }
1008
1009 if (result == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001010 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001011 malloc_write("<jemalloc>: Error in posix_memalign(): "
1012 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001013 abort();
1014 }
1015 ret = ENOMEM;
1016 goto RETURN;
1017 }
1018
1019 *memptr = result;
1020 ret = 0;
1021
1022RETURN:
Jason Evans7372b152012-02-10 20:22:09 -08001023 if (config_stats && result != NULL) {
Jason Evans93443682010-10-20 17:39:18 -07001024 assert(usize == isalloc(result));
1025 ALLOCATED_ADD(usize, 0);
1026 }
Jason Evans7372b152012-02-10 20:22:09 -08001027 if (config_prof && opt_prof && result != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001028 prof_malloc(result, usize, cnt);
Jason Evans289053c2009-06-22 12:08:42 -07001029 return (ret);
1030}
1031
Jason Evansa5070042011-08-12 13:48:27 -07001032JEMALLOC_ATTR(nonnull(1))
1033JEMALLOC_ATTR(visibility("default"))
1034int
1035JEMALLOC_P(posix_memalign)(void **memptr, size_t alignment, size_t size)
1036{
1037
1038 return imemalign(memptr, alignment, size);
1039}
1040
Jason Evans9ad48232010-01-03 11:59:20 -08001041JEMALLOC_ATTR(malloc)
Jason Evanse476f8a2010-01-16 09:53:50 -08001042JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001043void *
Jason Evanse476f8a2010-01-16 09:53:50 -08001044JEMALLOC_P(calloc)(size_t num, size_t size)
Jason Evans289053c2009-06-22 12:08:42 -07001045{
1046 void *ret;
1047 size_t num_size;
Jason Evans7372b152012-02-10 20:22:09 -08001048 size_t usize;
1049 prof_thr_cnt_t *cnt;
Jason Evans289053c2009-06-22 12:08:42 -07001050
1051 if (malloc_init()) {
1052 num_size = 0;
1053 ret = NULL;
1054 goto RETURN;
1055 }
1056
1057 num_size = num * size;
1058 if (num_size == 0) {
Jason Evans7372b152012-02-10 20:22:09 -08001059 if ((config_sysv == false || opt_sysv == false)
1060 && ((num == 0) || (size == 0)))
Jason Evans289053c2009-06-22 12:08:42 -07001061 num_size = 1;
1062 else {
1063 ret = NULL;
1064 goto RETURN;
1065 }
1066 /*
1067 * Try to avoid division here. We know that it isn't possible to
1068 * overflow during multiplication if neither operand uses any of the
1069 * most significant half of the bits in a size_t.
1070 */
1071 } else if (((num | size) & (SIZE_T_MAX << (sizeof(size_t) << 2)))
1072 && (num_size / size != num)) {
1073 /* size_t overflow. */
1074 ret = NULL;
1075 goto RETURN;
1076 }
1077
Jason Evans7372b152012-02-10 20:22:09 -08001078 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001079 usize = s2u(num_size);
Jason Evansa5070042011-08-12 13:48:27 -07001080 PROF_ALLOC_PREP(1, usize, cnt);
1081 if (cnt == NULL) {
Jason Evans0b270a92010-03-31 16:45:04 -07001082 ret = NULL;
1083 goto RETURN;
1084 }
Jason Evans93443682010-10-20 17:39:18 -07001085 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize
Jason Evans0b270a92010-03-31 16:45:04 -07001086 <= small_maxclass) {
1087 ret = icalloc(small_maxclass+1);
1088 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001089 arena_prof_promoted(ret, usize);
Jason Evans0b270a92010-03-31 16:45:04 -07001090 } else
1091 ret = icalloc(num_size);
Jason Evans7372b152012-02-10 20:22:09 -08001092 } else {
1093 if (config_stats)
1094 usize = s2u(num_size);
Jason Evans0b270a92010-03-31 16:45:04 -07001095 ret = icalloc(num_size);
Jason Evans93443682010-10-20 17:39:18 -07001096 }
Jason Evans289053c2009-06-22 12:08:42 -07001097
1098RETURN:
1099 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001100 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001101 malloc_write("<jemalloc>: Error in calloc(): out of "
1102 "memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001103 abort();
1104 }
1105 errno = ENOMEM;
1106 }
1107
Jason Evans7372b152012-02-10 20:22:09 -08001108 if (config_prof && opt_prof && ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001109 prof_malloc(ret, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -08001110 if (config_stats && ret != NULL) {
Jason Evans93443682010-10-20 17:39:18 -07001111 assert(usize == isalloc(ret));
1112 ALLOCATED_ADD(usize, 0);
1113 }
Jason Evans289053c2009-06-22 12:08:42 -07001114 return (ret);
1115}
1116
Jason Evanse476f8a2010-01-16 09:53:50 -08001117JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001118void *
Jason Evanse476f8a2010-01-16 09:53:50 -08001119JEMALLOC_P(realloc)(void *ptr, size_t size)
Jason Evans289053c2009-06-22 12:08:42 -07001120{
1121 void *ret;
Jason Evans7372b152012-02-10 20:22:09 -08001122 size_t usize;
Jason Evans93443682010-10-20 17:39:18 -07001123 size_t old_size = 0;
Jason Evans7372b152012-02-10 20:22:09 -08001124 prof_thr_cnt_t *cnt;
1125 prof_ctx_t *old_ctx;
Jason Evans6109fe02010-02-10 10:37:56 -08001126
Jason Evans289053c2009-06-22 12:08:42 -07001127 if (size == 0) {
Jason Evans7372b152012-02-10 20:22:09 -08001128 if (config_sysv == false || opt_sysv == false)
Jason Evans289053c2009-06-22 12:08:42 -07001129 size = 1;
1130 else {
Jason Evanse476f8a2010-01-16 09:53:50 -08001131 if (ptr != NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001132 if (config_prof || config_stats)
1133 old_size = isalloc(ptr);
1134 if (config_prof && opt_prof) {
Jason Evans50651562010-04-13 16:13:54 -07001135 old_ctx = prof_ctx_get(ptr);
Jason Evans6109fe02010-02-10 10:37:56 -08001136 cnt = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001137 }
Jason Evans289053c2009-06-22 12:08:42 -07001138 idalloc(ptr);
Jason Evans7372b152012-02-10 20:22:09 -08001139 } else if (config_prof && opt_prof) {
Jason Evans50651562010-04-13 16:13:54 -07001140 old_ctx = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001141 cnt = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001142 }
Jason Evans289053c2009-06-22 12:08:42 -07001143 ret = NULL;
1144 goto RETURN;
1145 }
1146 }
1147
1148 if (ptr != NULL) {
Jason Evansa25d0a82009-11-09 14:57:38 -08001149 assert(malloc_initialized || malloc_initializer ==
1150 pthread_self());
Jason Evans289053c2009-06-22 12:08:42 -07001151
Jason Evans7372b152012-02-10 20:22:09 -08001152 if (config_prof || config_stats)
1153 old_size = isalloc(ptr);
1154 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001155 usize = s2u(size);
Jason Evans50651562010-04-13 16:13:54 -07001156 old_ctx = prof_ctx_get(ptr);
Jason Evansa5070042011-08-12 13:48:27 -07001157 PROF_ALLOC_PREP(1, usize, cnt);
1158 if (cnt == NULL) {
Jason Evans46405e62011-08-30 23:37:29 -07001159 old_ctx = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001160 ret = NULL;
1161 goto OOM;
1162 }
Jason Evans0b270a92010-03-31 16:45:04 -07001163 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U &&
Jason Evans93443682010-10-20 17:39:18 -07001164 usize <= small_maxclass) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001165 ret = iralloc(ptr, small_maxclass+1, 0, 0,
1166 false, false);
Jason Evans0b270a92010-03-31 16:45:04 -07001167 if (ret != NULL)
Jason Evans93443682010-10-20 17:39:18 -07001168 arena_prof_promoted(ret, usize);
Jason Evans46405e62011-08-30 23:37:29 -07001169 else
1170 old_ctx = NULL;
1171 } else {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001172 ret = iralloc(ptr, size, 0, 0, false, false);
Jason Evans46405e62011-08-30 23:37:29 -07001173 if (ret == NULL)
1174 old_ctx = NULL;
1175 }
Jason Evans7372b152012-02-10 20:22:09 -08001176 } else {
1177 if (config_stats)
1178 usize = s2u(size);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001179 ret = iralloc(ptr, size, 0, 0, false, false);
Jason Evans93443682010-10-20 17:39:18 -07001180 }
Jason Evans289053c2009-06-22 12:08:42 -07001181
Jason Evans6109fe02010-02-10 10:37:56 -08001182OOM:
Jason Evans289053c2009-06-22 12:08:42 -07001183 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001184 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001185 malloc_write("<jemalloc>: Error in realloc(): "
1186 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001187 abort();
1188 }
1189 errno = ENOMEM;
1190 }
1191 } else {
Jason Evans7372b152012-02-10 20:22:09 -08001192 if (config_prof && opt_prof)
Jason Evans50651562010-04-13 16:13:54 -07001193 old_ctx = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001194 if (malloc_init()) {
Jason Evans7372b152012-02-10 20:22:09 -08001195 if (config_prof && opt_prof)
Jason Evans6109fe02010-02-10 10:37:56 -08001196 cnt = NULL;
Jason Evans6109fe02010-02-10 10:37:56 -08001197 ret = NULL;
1198 } else {
Jason Evans7372b152012-02-10 20:22:09 -08001199 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001200 usize = s2u(size);
Jason Evansa5070042011-08-12 13:48:27 -07001201 PROF_ALLOC_PREP(1, usize, cnt);
1202 if (cnt == NULL)
Jason Evans0b270a92010-03-31 16:45:04 -07001203 ret = NULL;
1204 else {
1205 if (prof_promote && (uintptr_t)cnt !=
Jason Evans93443682010-10-20 17:39:18 -07001206 (uintptr_t)1U && usize <=
Jason Evans0b270a92010-03-31 16:45:04 -07001207 small_maxclass) {
1208 ret = imalloc(small_maxclass+1);
1209 if (ret != NULL) {
1210 arena_prof_promoted(ret,
Jason Evans93443682010-10-20 17:39:18 -07001211 usize);
Jason Evans0b270a92010-03-31 16:45:04 -07001212 }
1213 } else
1214 ret = imalloc(size);
1215 }
Jason Evans7372b152012-02-10 20:22:09 -08001216 } else {
1217 if (config_stats)
1218 usize = s2u(size);
Jason Evans6109fe02010-02-10 10:37:56 -08001219 ret = imalloc(size);
Jason Evans93443682010-10-20 17:39:18 -07001220 }
Jason Evans6109fe02010-02-10 10:37:56 -08001221 }
Jason Evans569432c2009-12-29 00:09:15 -08001222
Jason Evans289053c2009-06-22 12:08:42 -07001223 if (ret == NULL) {
Jason Evans7372b152012-02-10 20:22:09 -08001224 if (config_xmalloc && opt_xmalloc) {
Jason Evans698805c2010-03-03 17:45:38 -08001225 malloc_write("<jemalloc>: Error in realloc(): "
1226 "out of memory\n");
Jason Evans289053c2009-06-22 12:08:42 -07001227 abort();
1228 }
1229 errno = ENOMEM;
1230 }
1231 }
1232
1233RETURN:
Jason Evans7372b152012-02-10 20:22:09 -08001234 if (config_prof && opt_prof)
Jason Evanse4f78462010-10-22 10:45:59 -07001235 prof_realloc(ret, usize, cnt, old_size, old_ctx);
Jason Evans7372b152012-02-10 20:22:09 -08001236 if (config_stats && ret != NULL) {
Jason Evans93443682010-10-20 17:39:18 -07001237 assert(usize == isalloc(ret));
1238 ALLOCATED_ADD(usize, old_size);
1239 }
Jason Evans289053c2009-06-22 12:08:42 -07001240 return (ret);
1241}
1242
Jason Evanse476f8a2010-01-16 09:53:50 -08001243JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001244void
Jason Evanse476f8a2010-01-16 09:53:50 -08001245JEMALLOC_P(free)(void *ptr)
Jason Evans289053c2009-06-22 12:08:42 -07001246{
1247
Jason Evans289053c2009-06-22 12:08:42 -07001248 if (ptr != NULL) {
Jason Evanse4f78462010-10-22 10:45:59 -07001249 size_t usize;
Jason Evanse4f78462010-10-22 10:45:59 -07001250
Jason Evansa25d0a82009-11-09 14:57:38 -08001251 assert(malloc_initialized || malloc_initializer ==
1252 pthread_self());
Jason Evans289053c2009-06-22 12:08:42 -07001253
Jason Evans7372b152012-02-10 20:22:09 -08001254 if (config_prof && opt_prof) {
Jason Evanse4f78462010-10-22 10:45:59 -07001255 usize = isalloc(ptr);
Jason Evanse4f78462010-10-22 10:45:59 -07001256 prof_free(ptr, usize);
Jason Evans7372b152012-02-10 20:22:09 -08001257 } else if (config_stats) {
1258 usize = isalloc(ptr);
Jason Evanse4f78462010-10-22 10:45:59 -07001259 }
Jason Evans7372b152012-02-10 20:22:09 -08001260 if (config_stats)
1261 ALLOCATED_ADD(0, usize);
Jason Evans289053c2009-06-22 12:08:42 -07001262 idalloc(ptr);
1263 }
1264}
1265
1266/*
1267 * End malloc(3)-compatible functions.
1268 */
1269/******************************************************************************/
1270/*
Jason Evans6a0d2912010-09-20 16:44:23 -07001271 * Begin non-standard override functions.
1272 *
1273 * These overrides are omitted if the JEMALLOC_PREFIX is defined, since the
1274 * entire point is to avoid accidental mixed allocator usage.
1275 */
1276#ifndef JEMALLOC_PREFIX
1277
1278#ifdef JEMALLOC_OVERRIDE_MEMALIGN
1279JEMALLOC_ATTR(malloc)
1280JEMALLOC_ATTR(visibility("default"))
1281void *
1282JEMALLOC_P(memalign)(size_t alignment, size_t size)
1283{
Jason Evans7372b152012-02-10 20:22:09 -08001284 void *ret
Jason Evans355b4382010-09-20 19:20:48 -07001285#ifdef JEMALLOC_CC_SILENCE
Jason Evans7372b152012-02-10 20:22:09 -08001286 = NULL
Jason Evans355b4382010-09-20 19:20:48 -07001287#endif
Jason Evans7372b152012-02-10 20:22:09 -08001288 ;
1289 imemalign(&ret, alignment, size);
Jason Evans6a0d2912010-09-20 16:44:23 -07001290 return (ret);
1291}
1292#endif
1293
1294#ifdef JEMALLOC_OVERRIDE_VALLOC
1295JEMALLOC_ATTR(malloc)
1296JEMALLOC_ATTR(visibility("default"))
1297void *
1298JEMALLOC_P(valloc)(size_t size)
1299{
Jason Evans7372b152012-02-10 20:22:09 -08001300 void *ret
Jason Evans355b4382010-09-20 19:20:48 -07001301#ifdef JEMALLOC_CC_SILENCE
Jason Evans7372b152012-02-10 20:22:09 -08001302 = NULL
Jason Evans355b4382010-09-20 19:20:48 -07001303#endif
Jason Evans7372b152012-02-10 20:22:09 -08001304 ;
1305 imemalign(&ret, PAGE_SIZE, size);
Jason Evans6a0d2912010-09-20 16:44:23 -07001306 return (ret);
1307}
1308#endif
1309
1310#endif /* JEMALLOC_PREFIX */
1311/*
1312 * End non-standard override functions.
1313 */
1314/******************************************************************************/
1315/*
Jason Evans289053c2009-06-22 12:08:42 -07001316 * Begin non-standard functions.
1317 */
1318
Jason Evanse476f8a2010-01-16 09:53:50 -08001319JEMALLOC_ATTR(visibility("default"))
Jason Evans289053c2009-06-22 12:08:42 -07001320size_t
Jason Evanse476f8a2010-01-16 09:53:50 -08001321JEMALLOC_P(malloc_usable_size)(const void *ptr)
Jason Evans289053c2009-06-22 12:08:42 -07001322{
Jason Evans569432c2009-12-29 00:09:15 -08001323 size_t ret;
Jason Evans289053c2009-06-22 12:08:42 -07001324
Jason Evans8e3c3c62010-09-17 15:46:18 -07001325 assert(malloc_initialized || malloc_initializer == pthread_self());
1326
Jason Evans7372b152012-02-10 20:22:09 -08001327 if (config_ivsalloc)
1328 ret = ivsalloc(ptr);
1329 else {
1330 assert(ptr != NULL);
1331 ret = isalloc(ptr);
1332 }
Jason Evans289053c2009-06-22 12:08:42 -07001333
Jason Evans569432c2009-12-29 00:09:15 -08001334 return (ret);
Jason Evans289053c2009-06-22 12:08:42 -07001335}
1336
Jason Evans4201af02010-01-24 02:53:40 -08001337JEMALLOC_ATTR(visibility("default"))
1338void
Jason Evans698805c2010-03-03 17:45:38 -08001339JEMALLOC_P(malloc_stats_print)(void (*write_cb)(void *, const char *),
1340 void *cbopaque, const char *opts)
Jason Evans4201af02010-01-24 02:53:40 -08001341{
1342
Jason Evans698805c2010-03-03 17:45:38 -08001343 stats_print(write_cb, cbopaque, opts);
Jason Evans4201af02010-01-24 02:53:40 -08001344}
1345
Jason Evans3c234352010-01-27 13:10:55 -08001346JEMALLOC_ATTR(visibility("default"))
1347int
1348JEMALLOC_P(mallctl)(const char *name, void *oldp, size_t *oldlenp, void *newp,
1349 size_t newlen)
1350{
1351
Jason Evans95833312010-01-27 13:45:21 -08001352 if (malloc_init())
1353 return (EAGAIN);
1354
Jason Evans3c234352010-01-27 13:10:55 -08001355 return (ctl_byname(name, oldp, oldlenp, newp, newlen));
1356}
1357
1358JEMALLOC_ATTR(visibility("default"))
1359int
1360JEMALLOC_P(mallctlnametomib)(const char *name, size_t *mibp, size_t *miblenp)
1361{
1362
Jason Evans95833312010-01-27 13:45:21 -08001363 if (malloc_init())
1364 return (EAGAIN);
1365
Jason Evans3c234352010-01-27 13:10:55 -08001366 return (ctl_nametomib(name, mibp, miblenp));
1367}
1368
1369JEMALLOC_ATTR(visibility("default"))
1370int
1371JEMALLOC_P(mallctlbymib)(const size_t *mib, size_t miblen, void *oldp,
1372 size_t *oldlenp, void *newp, size_t newlen)
1373{
1374
Jason Evans95833312010-01-27 13:45:21 -08001375 if (malloc_init())
1376 return (EAGAIN);
1377
Jason Evans3c234352010-01-27 13:10:55 -08001378 return (ctl_bymib(mib, miblen, oldp, oldlenp, newp, newlen));
1379}
1380
Jason Evans8e3c3c62010-09-17 15:46:18 -07001381JEMALLOC_INLINE void *
Jason Evans38d92102011-03-23 00:37:29 -07001382iallocm(size_t usize, size_t alignment, bool zero)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001383{
1384
Jason Evans38d92102011-03-23 00:37:29 -07001385 assert(usize == ((alignment == 0) ? s2u(usize) : sa2u(usize, alignment,
1386 NULL)));
1387
Jason Evans8e3c3c62010-09-17 15:46:18 -07001388 if (alignment != 0)
Jason Evans38d92102011-03-23 00:37:29 -07001389 return (ipalloc(usize, alignment, zero));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001390 else if (zero)
Jason Evans38d92102011-03-23 00:37:29 -07001391 return (icalloc(usize));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001392 else
Jason Evans38d92102011-03-23 00:37:29 -07001393 return (imalloc(usize));
Jason Evans8e3c3c62010-09-17 15:46:18 -07001394}
1395
Jason Evans6a0d2912010-09-20 16:44:23 -07001396JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001397JEMALLOC_ATTR(visibility("default"))
1398int
1399JEMALLOC_P(allocm)(void **ptr, size_t *rsize, size_t size, int flags)
1400{
1401 void *p;
Jason Evans93443682010-10-20 17:39:18 -07001402 size_t usize;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001403 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1404 & (SIZE_T_MAX-1));
1405 bool zero = flags & ALLOCM_ZERO;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001406 prof_thr_cnt_t *cnt;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001407
1408 assert(ptr != NULL);
1409 assert(size != 0);
1410
1411 if (malloc_init())
1412 goto OOM;
1413
Jason Evans749c2a02011-08-12 18:37:54 -07001414 usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment, NULL);
Jason Evans38d92102011-03-23 00:37:29 -07001415 if (usize == 0)
1416 goto OOM;
1417
Jason Evans7372b152012-02-10 20:22:09 -08001418 if (config_prof && opt_prof) {
Jason Evansa5070042011-08-12 13:48:27 -07001419 PROF_ALLOC_PREP(1, usize, cnt);
1420 if (cnt == NULL)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001421 goto OOM;
Jason Evans93443682010-10-20 17:39:18 -07001422 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <=
Jason Evans8e3c3c62010-09-17 15:46:18 -07001423 small_maxclass) {
Jason Evans38d92102011-03-23 00:37:29 -07001424 size_t usize_promoted = (alignment == 0) ?
1425 s2u(small_maxclass+1) : sa2u(small_maxclass+1,
1426 alignment, NULL);
1427 assert(usize_promoted != 0);
1428 p = iallocm(usize_promoted, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001429 if (p == NULL)
1430 goto OOM;
Jason Evans93443682010-10-20 17:39:18 -07001431 arena_prof_promoted(p, usize);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001432 } else {
Jason Evans38d92102011-03-23 00:37:29 -07001433 p = iallocm(usize, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001434 if (p == NULL)
1435 goto OOM;
1436 }
Jason Evans749c2a02011-08-12 18:37:54 -07001437 prof_malloc(p, usize, cnt);
Jason Evans7372b152012-02-10 20:22:09 -08001438 } else {
Jason Evans38d92102011-03-23 00:37:29 -07001439 p = iallocm(usize, alignment, zero);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001440 if (p == NULL)
1441 goto OOM;
1442 }
Jason Evans7372b152012-02-10 20:22:09 -08001443 if (rsize != NULL)
1444 *rsize = usize;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001445
1446 *ptr = p;
Jason Evans7372b152012-02-10 20:22:09 -08001447 if (config_stats) {
1448 assert(usize == isalloc(p));
1449 ALLOCATED_ADD(usize, 0);
1450 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001451 return (ALLOCM_SUCCESS);
1452OOM:
Jason Evans7372b152012-02-10 20:22:09 -08001453 if (config_xmalloc && opt_xmalloc) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001454 malloc_write("<jemalloc>: Error in allocm(): "
1455 "out of memory\n");
1456 abort();
1457 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001458 *ptr = NULL;
1459 return (ALLOCM_ERR_OOM);
1460}
1461
Jason Evans6a0d2912010-09-20 16:44:23 -07001462JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001463JEMALLOC_ATTR(visibility("default"))
1464int
1465JEMALLOC_P(rallocm)(void **ptr, size_t *rsize, size_t size, size_t extra,
1466 int flags)
1467{
1468 void *p, *q;
Jason Evans93443682010-10-20 17:39:18 -07001469 size_t usize;
Jason Evans93443682010-10-20 17:39:18 -07001470 size_t old_size;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001471 size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK)
1472 & (SIZE_T_MAX-1));
1473 bool zero = flags & ALLOCM_ZERO;
1474 bool no_move = flags & ALLOCM_NO_MOVE;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001475 prof_thr_cnt_t *cnt;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001476
1477 assert(ptr != NULL);
1478 assert(*ptr != NULL);
1479 assert(size != 0);
1480 assert(SIZE_T_MAX - size >= extra);
1481 assert(malloc_initialized || malloc_initializer == pthread_self());
1482
1483 p = *ptr;
Jason Evans7372b152012-02-10 20:22:09 -08001484 if (config_prof && opt_prof) {
Jason Evans93443682010-10-20 17:39:18 -07001485 /*
1486 * usize isn't knowable before iralloc() returns when extra is
1487 * non-zero. Therefore, compute its maximum possible value and
Jason Evansa5070042011-08-12 13:48:27 -07001488 * use that in PROF_ALLOC_PREP() to decide whether to capture a
Jason Evans93443682010-10-20 17:39:18 -07001489 * backtrace. prof_realloc() will use the actual usize to
1490 * decide whether to sample.
1491 */
1492 size_t max_usize = (alignment == 0) ? s2u(size+extra) :
1493 sa2u(size+extra, alignment, NULL);
Jason Evans46405e62011-08-30 23:37:29 -07001494 prof_ctx_t *old_ctx = prof_ctx_get(p);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001495 old_size = isalloc(p);
Jason Evansa5070042011-08-12 13:48:27 -07001496 PROF_ALLOC_PREP(1, max_usize, cnt);
1497 if (cnt == NULL)
Jason Evans8e3c3c62010-09-17 15:46:18 -07001498 goto OOM;
Jason Evans183ba502011-08-11 22:51:00 -07001499 /*
1500 * Use minimum usize to determine whether promotion may happen.
1501 */
1502 if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U
1503 && ((alignment == 0) ? s2u(size) : sa2u(size,
1504 alignment, NULL)) <= small_maxclass) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001505 q = iralloc(p, small_maxclass+1, (small_maxclass+1 >=
1506 size+extra) ? 0 : size+extra - (small_maxclass+1),
1507 alignment, zero, no_move);
1508 if (q == NULL)
1509 goto ERR;
Jason Evans183ba502011-08-11 22:51:00 -07001510 if (max_usize < PAGE_SIZE) {
1511 usize = max_usize;
1512 arena_prof_promoted(q, usize);
Jason Evansb493ce22011-08-12 11:28:47 -07001513 } else
1514 usize = isalloc(q);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001515 } else {
1516 q = iralloc(p, size, extra, alignment, zero, no_move);
1517 if (q == NULL)
1518 goto ERR;
Jason Evans93443682010-10-20 17:39:18 -07001519 usize = isalloc(q);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001520 }
Jason Evanse4f78462010-10-22 10:45:59 -07001521 prof_realloc(q, usize, cnt, old_size, old_ctx);
Jason Evanseacb8962011-03-23 00:30:30 -07001522 if (rsize != NULL)
1523 *rsize = usize;
Jason Evans7372b152012-02-10 20:22:09 -08001524 } else {
1525 if (config_stats)
1526 old_size = isalloc(p);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001527 q = iralloc(p, size, extra, alignment, zero, no_move);
1528 if (q == NULL)
1529 goto ERR;
Jason Evans7372b152012-02-10 20:22:09 -08001530 if (config_stats)
Jason Evans93443682010-10-20 17:39:18 -07001531 usize = isalloc(q);
Jason Evans7372b152012-02-10 20:22:09 -08001532 if (rsize != NULL) {
1533 if (config_stats == false)
1534 usize = isalloc(q);
1535 *rsize = usize;
Jason Evans93443682010-10-20 17:39:18 -07001536 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001537 }
1538
1539 *ptr = q;
Jason Evans7372b152012-02-10 20:22:09 -08001540 if (config_stats)
1541 ALLOCATED_ADD(usize, old_size);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001542 return (ALLOCM_SUCCESS);
1543ERR:
1544 if (no_move)
1545 return (ALLOCM_ERR_NOT_MOVED);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001546OOM:
Jason Evans7372b152012-02-10 20:22:09 -08001547 if (config_xmalloc && opt_xmalloc) {
Jason Evans8e3c3c62010-09-17 15:46:18 -07001548 malloc_write("<jemalloc>: Error in rallocm(): "
1549 "out of memory\n");
1550 abort();
1551 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001552 return (ALLOCM_ERR_OOM);
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
1558JEMALLOC_P(sallocm)(const void *ptr, size_t *rsize, int flags)
1559{
1560 size_t sz;
1561
1562 assert(malloc_initialized || malloc_initializer == pthread_self());
1563
Jason Evans7372b152012-02-10 20:22:09 -08001564 if (config_ivsalloc)
1565 sz = ivsalloc(ptr);
1566 else {
1567 assert(ptr != NULL);
1568 sz = isalloc(ptr);
1569 }
Jason Evans8e3c3c62010-09-17 15:46:18 -07001570 assert(rsize != NULL);
1571 *rsize = sz;
1572
1573 return (ALLOCM_SUCCESS);
1574}
1575
Jason Evans6a0d2912010-09-20 16:44:23 -07001576JEMALLOC_ATTR(nonnull(1))
Jason Evans8e3c3c62010-09-17 15:46:18 -07001577JEMALLOC_ATTR(visibility("default"))
1578int
1579JEMALLOC_P(dallocm)(void *ptr, int flags)
1580{
Jason Evanse4f78462010-10-22 10:45:59 -07001581 size_t usize;
Jason Evans8e3c3c62010-09-17 15:46:18 -07001582
1583 assert(ptr != NULL);
1584 assert(malloc_initialized || malloc_initializer == pthread_self());
1585
Jason Evans7372b152012-02-10 20:22:09 -08001586 if (config_stats)
Jason Evanse4f78462010-10-22 10:45:59 -07001587 usize = isalloc(ptr);
Jason Evans7372b152012-02-10 20:22:09 -08001588 if (config_prof && opt_prof) {
1589 if (config_stats == false)
1590 usize = isalloc(ptr);
Jason Evanse4f78462010-10-22 10:45:59 -07001591 prof_free(ptr, usize);
1592 }
Jason Evans7372b152012-02-10 20:22:09 -08001593 if (config_stats)
1594 ALLOCATED_ADD(0, usize);
Jason Evans8e3c3c62010-09-17 15:46:18 -07001595 idalloc(ptr);
1596
1597 return (ALLOCM_SUCCESS);
1598}
1599
Jason Evans289053c2009-06-22 12:08:42 -07001600/*
1601 * End non-standard functions.
1602 */
1603/******************************************************************************/
Jason Evans289053c2009-06-22 12:08:42 -07001604
Jason Evans289053c2009-06-22 12:08:42 -07001605/*
1606 * The following functions are used by threading libraries for protection of
Jason Evans28177d42010-09-20 11:24:24 -07001607 * malloc during fork().
Jason Evans289053c2009-06-22 12:08:42 -07001608 */
1609
Jason Evans2dbecf12010-09-05 10:35:13 -07001610void
Jason Evans804c9ec2009-06-22 17:44:33 -07001611jemalloc_prefork(void)
Jason Evans289053c2009-06-22 12:08:42 -07001612{
Jason Evansfbbb6242010-01-24 17:56:48 -08001613 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -07001614
1615 /* Acquire all mutexes in a safe order. */
1616
Jason Evansfbbb6242010-01-24 17:56:48 -08001617 malloc_mutex_lock(&arenas_lock);
1618 for (i = 0; i < narenas; i++) {
1619 if (arenas[i] != NULL)
1620 malloc_mutex_lock(&arenas[i]->lock);
1621 }
Jason Evans289053c2009-06-22 12:08:42 -07001622
1623 malloc_mutex_lock(&base_mtx);
1624
1625 malloc_mutex_lock(&huge_mtx);
1626
Jason Evans7372b152012-02-10 20:22:09 -08001627 if (config_dss)
1628 malloc_mutex_lock(&dss_mtx);
Jason Evans289053c2009-06-22 12:08:42 -07001629}
1630
Jason Evans2dbecf12010-09-05 10:35:13 -07001631void
Jason Evans804c9ec2009-06-22 17:44:33 -07001632jemalloc_postfork(void)
Jason Evans289053c2009-06-22 12:08:42 -07001633{
1634 unsigned i;
Jason Evans289053c2009-06-22 12:08:42 -07001635
1636 /* Release all mutexes, now that fork() has completed. */
1637
Jason Evans7372b152012-02-10 20:22:09 -08001638 if (config_dss)
1639 malloc_mutex_unlock(&dss_mtx);
Jason Evans289053c2009-06-22 12:08:42 -07001640
1641 malloc_mutex_unlock(&huge_mtx);
1642
1643 malloc_mutex_unlock(&base_mtx);
1644
Jason Evans289053c2009-06-22 12:08:42 -07001645 for (i = 0; i < narenas; i++) {
Jason Evansfbbb6242010-01-24 17:56:48 -08001646 if (arenas[i] != NULL)
1647 malloc_mutex_unlock(&arenas[i]->lock);
Jason Evans289053c2009-06-22 12:08:42 -07001648 }
Jason Evansfbbb6242010-01-24 17:56:48 -08001649 malloc_mutex_unlock(&arenas_lock);
Jason Evans289053c2009-06-22 12:08:42 -07001650}
Jason Evans2dbecf12010-09-05 10:35:13 -07001651
1652/******************************************************************************/