| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1 | #define JEMALLOC_C_ |
| Jason Evans | 376b152 | 2010-02-11 14:45:59 -0800 | [diff] [blame] | 2 | #include "jemalloc/internal/jemalloc_internal.h" |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 3 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 4 | /******************************************************************************/ |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 5 | /* Data. */ |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 6 | |
| Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 7 | malloc_mutex_t arenas_lock; |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 8 | arena_t **arenas; |
| 9 | unsigned narenas; |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 10 | |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 11 | pthread_key_t arenas_tsd; |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 12 | #ifndef NO_TLS |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 13 | __thread arena_t *arenas_tls JEMALLOC_ATTR(tls_model("initial-exec")); |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 14 | #endif |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 15 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 16 | #ifndef NO_TLS |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 17 | __thread thread_allocated_t thread_allocated_tls; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 18 | #endif |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 19 | pthread_key_t thread_allocated_tsd; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 20 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 21 | /* Set to true once the allocator has been initialized. */ |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 22 | static bool malloc_initialized = false; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 23 | |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 24 | /* Used to let the initializing thread recursively allocate. */ |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 25 | static pthread_t malloc_initializer = (unsigned long)0; |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 26 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 27 | /* Used to avoid initialization races. */ |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 28 | static malloc_mutex_t init_lock = MALLOC_MUTEX_INITIALIZER; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 29 | |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 30 | #ifdef DYNAMIC_PAGE_SHIFT |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 31 | size_t pagesize; |
| 32 | size_t pagesize_mask; |
| 33 | size_t lg_pagesize; |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 34 | #endif |
| 35 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 36 | unsigned ncpus; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 37 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 38 | /* Runtime configuration options. */ |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 39 | const char *JEMALLOC_P(malloc_conf) JEMALLOC_ATTR(visibility("default")); |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 40 | #ifdef JEMALLOC_DEBUG |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 41 | bool opt_abort = true; |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 42 | # ifdef JEMALLOC_FILL |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 43 | bool opt_junk = true; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 44 | # else |
| 45 | bool opt_junk = false; |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 46 | # endif |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 47 | #else |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 48 | bool opt_abort = false; |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 49 | bool opt_junk = false; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 50 | #endif |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 51 | bool opt_xmalloc = false; |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 52 | bool opt_zero = false; |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 53 | size_t opt_narenas = 0; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 54 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 55 | /******************************************************************************/ |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 56 | /* Function prototypes for non-inline static functions. */ |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 57 | |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 58 | static void wrtmessage(void *cbopaque, const char *s); |
| Jason Evans | 03c2237 | 2010-01-03 12:10:42 -0800 | [diff] [blame] | 59 | static void stats_print_atexit(void); |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 60 | static unsigned malloc_ncpus(void); |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 61 | static void arenas_cleanup(void *arg); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 62 | #ifdef NO_TLS |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 63 | static void thread_allocated_cleanup(void *arg); |
| 64 | #endif |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 65 | static bool malloc_conf_next(char const **opts_p, char const **k_p, |
| 66 | size_t *klen_p, char const **v_p, size_t *vlen_p); |
| 67 | static void malloc_conf_error(const char *msg, const char *k, size_t klen, |
| 68 | const char *v, size_t vlen); |
| 69 | static void malloc_conf_init(void); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 70 | static bool malloc_init_hard(void); |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 71 | static int imemalign(void **memptr, size_t alignment, size_t size); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 72 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 73 | /******************************************************************************/ |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 74 | /* malloc_message() setup. */ |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 75 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 76 | JEMALLOC_CATTR(visibility("hidden"), static) |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 77 | void |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 78 | wrtmessage(void *cbopaque, const char *s) |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 79 | { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 80 | UNUSED int result = write(STDERR_FILENO, s, strlen(s)); |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 83 | void (*JEMALLOC_P(malloc_message))(void *, const char *s) |
| 84 | JEMALLOC_ATTR(visibility("default")) = wrtmessage; |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 85 | |
| 86 | /******************************************************************************/ |
| 87 | /* |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 88 | * Begin miscellaneous support functions. |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 89 | */ |
| 90 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 91 | /* Create a new arena and insert it into the arenas array at index ind. */ |
| 92 | arena_t * |
| 93 | arenas_extend(unsigned ind) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 94 | { |
| 95 | arena_t *ret; |
| 96 | |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 97 | ret = (arena_t *)base_alloc(sizeof(arena_t)); |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 98 | if (ret != NULL && arena_new(ret, ind) == false) { |
| 99 | arenas[ind] = ret; |
| 100 | return (ret); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 101 | } |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 102 | /* Only reached if there is an OOM error. */ |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 103 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 104 | /* |
| 105 | * OOM here is quite inconvenient to propagate, since dealing with it |
| 106 | * would require a check for failure in the fast path. Instead, punt |
| 107 | * by using arenas[0]. In practice, this is an extremely unlikely |
| 108 | * failure. |
| 109 | */ |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 110 | malloc_write("<jemalloc>: Error initializing arena\n"); |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 111 | if (opt_abort) |
| 112 | abort(); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 113 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 114 | return (arenas[0]); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 117 | /* |
| 118 | * Choose an arena based on a per-thread value (slow-path code only, called |
| 119 | * only by choose_arena()). |
| 120 | */ |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 121 | arena_t * |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 122 | choose_arena_hard(void) |
| 123 | { |
| 124 | arena_t *ret; |
| 125 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 126 | if (narenas > 1) { |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 127 | unsigned i, choose, first_null; |
| 128 | |
| 129 | choose = 0; |
| 130 | first_null = narenas; |
| Jason Evans | 3ee7a5c | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 131 | malloc_mutex_lock(&arenas_lock); |
| Jason Evans | 0657f12 | 2011-03-18 17:56:14 -0700 | [diff] [blame] | 132 | assert(arenas[0] != NULL); |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 133 | for (i = 1; i < narenas; i++) { |
| 134 | if (arenas[i] != NULL) { |
| 135 | /* |
| 136 | * Choose the first arena that has the lowest |
| 137 | * number of threads assigned to it. |
| 138 | */ |
| 139 | if (arenas[i]->nthreads < |
| 140 | arenas[choose]->nthreads) |
| 141 | choose = i; |
| 142 | } else if (first_null == narenas) { |
| 143 | /* |
| 144 | * Record the index of the first uninitialized |
| 145 | * arena, in case all extant arenas are in use. |
| 146 | * |
| 147 | * NB: It is possible for there to be |
| 148 | * discontinuities in terms of initialized |
| 149 | * versus uninitialized arenas, due to the |
| 150 | * "thread.arena" mallctl. |
| 151 | */ |
| 152 | first_null = i; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (arenas[choose] == 0 || first_null == narenas) { |
| 157 | /* |
| 158 | * Use an unloaded arena, or the least loaded arena if |
| 159 | * all arenas are already initialized. |
| 160 | */ |
| 161 | ret = arenas[choose]; |
| 162 | } else { |
| 163 | /* Initialize a new arena. */ |
| 164 | ret = arenas_extend(first_null); |
| 165 | } |
| 166 | ret->nthreads++; |
| Jason Evans | 3ee7a5c | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 167 | malloc_mutex_unlock(&arenas_lock); |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 168 | } else { |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 169 | ret = arenas[0]; |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 170 | malloc_mutex_lock(&arenas_lock); |
| 171 | ret->nthreads++; |
| 172 | malloc_mutex_unlock(&arenas_lock); |
| 173 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 174 | |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 175 | ARENA_SET(ret); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 176 | |
| 177 | return (ret); |
| 178 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 179 | |
| Jason Evans | a09f55c | 2010-09-20 16:05:41 -0700 | [diff] [blame] | 180 | /* |
| 181 | * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so |
| 182 | * provide a wrapper. |
| 183 | */ |
| 184 | int |
| 185 | buferror(int errnum, char *buf, size_t buflen) |
| 186 | { |
| 187 | #ifdef _GNU_SOURCE |
| 188 | char *b = strerror_r(errno, buf, buflen); |
| 189 | if (b != buf) { |
| 190 | strncpy(buf, b, buflen); |
| 191 | buf[buflen-1] = '\0'; |
| 192 | } |
| 193 | return (0); |
| 194 | #else |
| 195 | return (strerror_r(errno, buf, buflen)); |
| 196 | #endif |
| 197 | } |
| 198 | |
| Jason Evans | 03c2237 | 2010-01-03 12:10:42 -0800 | [diff] [blame] | 199 | static void |
| 200 | stats_print_atexit(void) |
| 201 | { |
| 202 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 203 | if (config_tcache && config_stats) { |
| 204 | unsigned i; |
| Jason Evans | 03c2237 | 2010-01-03 12:10:42 -0800 | [diff] [blame] | 205 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 206 | /* |
| 207 | * Merge stats from extant threads. This is racy, since |
| 208 | * individual threads do not lock when recording tcache stats |
| 209 | * events. As a consequence, the final stats may be slightly |
| 210 | * out of date by the time they are reported, if other threads |
| 211 | * continue to allocate. |
| 212 | */ |
| 213 | for (i = 0; i < narenas; i++) { |
| 214 | arena_t *arena = arenas[i]; |
| 215 | if (arena != NULL) { |
| 216 | tcache_t *tcache; |
| Jason Evans | 03c2237 | 2010-01-03 12:10:42 -0800 | [diff] [blame] | 217 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 218 | /* |
| 219 | * tcache_stats_merge() locks bins, so if any |
| 220 | * code is introduced that acquires both arena |
| 221 | * and bin locks in the opposite order, |
| 222 | * deadlocks may result. |
| 223 | */ |
| 224 | malloc_mutex_lock(&arena->lock); |
| 225 | ql_foreach(tcache, &arena->tcache_ql, link) { |
| 226 | tcache_stats_merge(tcache, arena); |
| 227 | } |
| 228 | malloc_mutex_unlock(&arena->lock); |
| Jason Evans | 03c2237 | 2010-01-03 12:10:42 -0800 | [diff] [blame] | 229 | } |
| Jason Evans | 03c2237 | 2010-01-03 12:10:42 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| Jason Evans | ed1bf45 | 2010-01-19 12:11:25 -0800 | [diff] [blame] | 232 | JEMALLOC_P(malloc_stats_print)(NULL, NULL, NULL); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| Jason Evans | 9dcad2d | 2011-02-13 18:11:54 -0800 | [diff] [blame] | 235 | thread_allocated_t * |
| 236 | thread_allocated_get_hard(void) |
| 237 | { |
| 238 | thread_allocated_t *thread_allocated = (thread_allocated_t *) |
| 239 | imalloc(sizeof(thread_allocated_t)); |
| 240 | if (thread_allocated == NULL) { |
| 241 | static thread_allocated_t static_thread_allocated = {0, 0}; |
| 242 | malloc_write("<jemalloc>: Error allocating TSD;" |
| 243 | " mallctl(\"thread.{de,}allocated[p]\", ...)" |
| 244 | " will be inaccurate\n"); |
| 245 | if (opt_abort) |
| 246 | abort(); |
| 247 | return (&static_thread_allocated); |
| 248 | } |
| 249 | pthread_setspecific(thread_allocated_tsd, thread_allocated); |
| 250 | thread_allocated->allocated = 0; |
| 251 | thread_allocated->deallocated = 0; |
| 252 | return (thread_allocated); |
| 253 | } |
| Jason Evans | 9dcad2d | 2011-02-13 18:11:54 -0800 | [diff] [blame] | 254 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 255 | /* |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 256 | * End miscellaneous support functions. |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 257 | */ |
| 258 | /******************************************************************************/ |
| 259 | /* |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 260 | * Begin initialization functions. |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 261 | */ |
| 262 | |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 263 | static unsigned |
| 264 | malloc_ncpus(void) |
| 265 | { |
| 266 | unsigned ret; |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 267 | long result; |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 268 | |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 269 | result = sysconf(_SC_NPROCESSORS_ONLN); |
| 270 | if (result == -1) { |
| 271 | /* Error. */ |
| 272 | ret = 1; |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 273 | } |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 274 | ret = (unsigned)result; |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 275 | |
| 276 | return (ret); |
| 277 | } |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 278 | |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 279 | static void |
| 280 | arenas_cleanup(void *arg) |
| 281 | { |
| 282 | arena_t *arena = (arena_t *)arg; |
| 283 | |
| 284 | malloc_mutex_lock(&arenas_lock); |
| 285 | arena->nthreads--; |
| 286 | malloc_mutex_unlock(&arenas_lock); |
| 287 | } |
| 288 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 289 | #ifdef NO_TLS |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 290 | static void |
| 291 | thread_allocated_cleanup(void *arg) |
| 292 | { |
| 293 | uint64_t *allocated = (uint64_t *)arg; |
| 294 | |
| 295 | if (allocated != NULL) |
| 296 | idalloc(allocated); |
| 297 | } |
| 298 | #endif |
| 299 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 300 | /* |
| 301 | * FreeBSD's pthreads implementation calls malloc(3), so the malloc |
| 302 | * implementation has to take pains to avoid infinite recursion during |
| 303 | * initialization. |
| 304 | */ |
| 305 | static inline bool |
| 306 | malloc_init(void) |
| 307 | { |
| 308 | |
| 309 | if (malloc_initialized == false) |
| 310 | return (malloc_init_hard()); |
| 311 | |
| 312 | return (false); |
| 313 | } |
| 314 | |
| 315 | static bool |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 316 | malloc_conf_next(char const **opts_p, char const **k_p, size_t *klen_p, |
| 317 | char const **v_p, size_t *vlen_p) |
| 318 | { |
| 319 | bool accept; |
| 320 | const char *opts = *opts_p; |
| 321 | |
| 322 | *k_p = opts; |
| 323 | |
| 324 | for (accept = false; accept == false;) { |
| 325 | switch (*opts) { |
| 326 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 327 | case 'F': case 'G': case 'H': case 'I': case 'J': |
| 328 | case 'K': case 'L': case 'M': case 'N': case 'O': |
| 329 | case 'P': case 'Q': case 'R': case 'S': case 'T': |
| 330 | case 'U': case 'V': case 'W': case 'X': case 'Y': |
| 331 | case 'Z': |
| 332 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 333 | case 'f': case 'g': case 'h': case 'i': case 'j': |
| 334 | case 'k': case 'l': case 'm': case 'n': case 'o': |
| 335 | case 'p': case 'q': case 'r': case 's': case 't': |
| 336 | case 'u': case 'v': case 'w': case 'x': case 'y': |
| 337 | case 'z': |
| 338 | case '0': case '1': case '2': case '3': case '4': |
| 339 | case '5': case '6': case '7': case '8': case '9': |
| 340 | case '_': |
| 341 | opts++; |
| 342 | break; |
| 343 | case ':': |
| 344 | opts++; |
| 345 | *klen_p = (uintptr_t)opts - 1 - (uintptr_t)*k_p; |
| 346 | *v_p = opts; |
| 347 | accept = true; |
| 348 | break; |
| 349 | case '\0': |
| 350 | if (opts != *opts_p) { |
| 351 | malloc_write("<jemalloc>: Conf string " |
| 352 | "ends with key\n"); |
| 353 | } |
| 354 | return (true); |
| 355 | default: |
| 356 | malloc_write("<jemalloc>: Malformed conf " |
| 357 | "string\n"); |
| 358 | return (true); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | for (accept = false; accept == false;) { |
| 363 | switch (*opts) { |
| 364 | case ',': |
| 365 | opts++; |
| 366 | /* |
| 367 | * Look ahead one character here, because the |
| 368 | * next time this function is called, it will |
| 369 | * assume that end of input has been cleanly |
| 370 | * reached if no input remains, but we have |
| 371 | * optimistically already consumed the comma if |
| 372 | * one exists. |
| 373 | */ |
| 374 | if (*opts == '\0') { |
| 375 | malloc_write("<jemalloc>: Conf string " |
| 376 | "ends with comma\n"); |
| 377 | } |
| 378 | *vlen_p = (uintptr_t)opts - 1 - (uintptr_t)*v_p; |
| 379 | accept = true; |
| 380 | break; |
| 381 | case '\0': |
| 382 | *vlen_p = (uintptr_t)opts - (uintptr_t)*v_p; |
| 383 | accept = true; |
| 384 | break; |
| 385 | default: |
| 386 | opts++; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | *opts_p = opts; |
| 392 | return (false); |
| 393 | } |
| 394 | |
| 395 | static void |
| 396 | malloc_conf_error(const char *msg, const char *k, size_t klen, const char *v, |
| 397 | size_t vlen) |
| 398 | { |
| 399 | char buf[PATH_MAX + 1]; |
| 400 | |
| 401 | malloc_write("<jemalloc>: "); |
| 402 | malloc_write(msg); |
| 403 | malloc_write(": "); |
| 404 | memcpy(buf, k, klen); |
| 405 | memcpy(&buf[klen], ":", 1); |
| 406 | memcpy(&buf[klen+1], v, vlen); |
| 407 | buf[klen+1+vlen] = '\0'; |
| 408 | malloc_write(buf); |
| 409 | malloc_write("\n"); |
| 410 | } |
| 411 | |
| 412 | static void |
| 413 | malloc_conf_init(void) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 414 | { |
| 415 | unsigned i; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 416 | char buf[PATH_MAX + 1]; |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 417 | const char *opts, *k, *v; |
| 418 | size_t klen, vlen; |
| 419 | |
| 420 | for (i = 0; i < 3; i++) { |
| 421 | /* Get runtime configuration. */ |
| 422 | switch (i) { |
| 423 | case 0: |
| 424 | if (JEMALLOC_P(malloc_conf) != NULL) { |
| 425 | /* |
| 426 | * Use options that were compiled into the |
| 427 | * program. |
| 428 | */ |
| 429 | opts = JEMALLOC_P(malloc_conf); |
| 430 | } else { |
| 431 | /* No configuration specified. */ |
| 432 | buf[0] = '\0'; |
| 433 | opts = buf; |
| 434 | } |
| 435 | break; |
| 436 | case 1: { |
| 437 | int linklen; |
| 438 | const char *linkname = |
| 439 | #ifdef JEMALLOC_PREFIX |
| 440 | "/etc/"JEMALLOC_PREFIX"malloc.conf" |
| 441 | #else |
| 442 | "/etc/malloc.conf" |
| 443 | #endif |
| 444 | ; |
| 445 | |
| 446 | if ((linklen = readlink(linkname, buf, |
| 447 | sizeof(buf) - 1)) != -1) { |
| 448 | /* |
| 449 | * Use the contents of the "/etc/malloc.conf" |
| 450 | * symbolic link's name. |
| 451 | */ |
| 452 | buf[linklen] = '\0'; |
| 453 | opts = buf; |
| 454 | } else { |
| 455 | /* No configuration specified. */ |
| 456 | buf[0] = '\0'; |
| 457 | opts = buf; |
| 458 | } |
| 459 | break; |
| 460 | } |
| 461 | case 2: { |
| 462 | const char *envname = |
| 463 | #ifdef JEMALLOC_PREFIX |
| 464 | JEMALLOC_CPREFIX"MALLOC_CONF" |
| 465 | #else |
| 466 | "MALLOC_CONF" |
| 467 | #endif |
| 468 | ; |
| 469 | |
| 470 | if ((opts = getenv(envname)) != NULL) { |
| 471 | /* |
| 472 | * Do nothing; opts is already initialized to |
| Jason Evans | 8ad0eac | 2010-12-17 18:07:53 -0800 | [diff] [blame] | 473 | * the value of the MALLOC_CONF environment |
| 474 | * variable. |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 475 | */ |
| 476 | } else { |
| 477 | /* No configuration specified. */ |
| 478 | buf[0] = '\0'; |
| 479 | opts = buf; |
| 480 | } |
| 481 | break; |
| 482 | } |
| 483 | default: |
| 484 | /* NOTREACHED */ |
| 485 | assert(false); |
| 486 | buf[0] = '\0'; |
| 487 | opts = buf; |
| 488 | } |
| 489 | |
| 490 | while (*opts != '\0' && malloc_conf_next(&opts, &k, &klen, &v, |
| 491 | &vlen) == false) { |
| 492 | #define CONF_HANDLE_BOOL(n) \ |
| 493 | if (sizeof(#n)-1 == klen && strncmp(#n, k, \ |
| 494 | klen) == 0) { \ |
| 495 | if (strncmp("true", v, vlen) == 0 && \ |
| 496 | vlen == sizeof("true")-1) \ |
| 497 | opt_##n = true; \ |
| 498 | else if (strncmp("false", v, vlen) == \ |
| 499 | 0 && vlen == sizeof("false")-1) \ |
| 500 | opt_##n = false; \ |
| 501 | else { \ |
| 502 | malloc_conf_error( \ |
| 503 | "Invalid conf value", \ |
| 504 | k, klen, v, vlen); \ |
| 505 | } \ |
| 506 | continue; \ |
| 507 | } |
| 508 | #define CONF_HANDLE_SIZE_T(n, min, max) \ |
| 509 | if (sizeof(#n)-1 == klen && strncmp(#n, k, \ |
| 510 | klen) == 0) { \ |
| 511 | unsigned long ul; \ |
| 512 | char *end; \ |
| 513 | \ |
| 514 | errno = 0; \ |
| 515 | ul = strtoul(v, &end, 0); \ |
| 516 | if (errno != 0 || (uintptr_t)end - \ |
| 517 | (uintptr_t)v != vlen) { \ |
| 518 | malloc_conf_error( \ |
| 519 | "Invalid conf value", \ |
| 520 | k, klen, v, vlen); \ |
| 521 | } else if (ul < min || ul > max) { \ |
| 522 | malloc_conf_error( \ |
| 523 | "Out-of-range conf value", \ |
| 524 | k, klen, v, vlen); \ |
| 525 | } else \ |
| 526 | opt_##n = ul; \ |
| 527 | continue; \ |
| 528 | } |
| 529 | #define CONF_HANDLE_SSIZE_T(n, min, max) \ |
| 530 | if (sizeof(#n)-1 == klen && strncmp(#n, k, \ |
| 531 | klen) == 0) { \ |
| 532 | long l; \ |
| 533 | char *end; \ |
| 534 | \ |
| 535 | errno = 0; \ |
| 536 | l = strtol(v, &end, 0); \ |
| 537 | if (errno != 0 || (uintptr_t)end - \ |
| 538 | (uintptr_t)v != vlen) { \ |
| 539 | malloc_conf_error( \ |
| 540 | "Invalid conf value", \ |
| 541 | k, klen, v, vlen); \ |
| 542 | } else if (l < (ssize_t)min || l > \ |
| 543 | (ssize_t)max) { \ |
| 544 | malloc_conf_error( \ |
| 545 | "Out-of-range conf value", \ |
| 546 | k, klen, v, vlen); \ |
| 547 | } else \ |
| 548 | opt_##n = l; \ |
| 549 | continue; \ |
| 550 | } |
| 551 | #define CONF_HANDLE_CHAR_P(n, d) \ |
| 552 | if (sizeof(#n)-1 == klen && strncmp(#n, k, \ |
| 553 | klen) == 0) { \ |
| 554 | size_t cpylen = (vlen <= \ |
| 555 | sizeof(opt_##n)-1) ? vlen : \ |
| 556 | sizeof(opt_##n)-1; \ |
| 557 | strncpy(opt_##n, v, cpylen); \ |
| 558 | opt_##n[cpylen] = '\0'; \ |
| 559 | continue; \ |
| 560 | } |
| 561 | |
| 562 | CONF_HANDLE_BOOL(abort) |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 563 | /* |
| 564 | * Chunks always require at least one * header page, |
| 565 | * plus one data page. |
| 566 | */ |
| 567 | CONF_HANDLE_SIZE_T(lg_chunk, PAGE_SHIFT+1, |
| 568 | (sizeof(size_t) << 3) - 1) |
| 569 | CONF_HANDLE_SIZE_T(narenas, 1, SIZE_T_MAX) |
| 570 | CONF_HANDLE_SSIZE_T(lg_dirty_mult, -1, |
| 571 | (sizeof(size_t) << 3) - 1) |
| 572 | CONF_HANDLE_BOOL(stats_print) |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 573 | if (config_fill) { |
| 574 | CONF_HANDLE_BOOL(junk) |
| 575 | CONF_HANDLE_BOOL(zero) |
| 576 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 577 | if (config_xmalloc) { |
| 578 | CONF_HANDLE_BOOL(xmalloc) |
| 579 | } |
| 580 | if (config_tcache) { |
| 581 | CONF_HANDLE_BOOL(tcache) |
| 582 | CONF_HANDLE_SSIZE_T(lg_tcache_gc_sweep, -1, |
| 583 | (sizeof(size_t) << 3) - 1) |
| 584 | CONF_HANDLE_SSIZE_T(lg_tcache_max, -1, |
| 585 | (sizeof(size_t) << 3) - 1) |
| 586 | } |
| 587 | if (config_prof) { |
| 588 | CONF_HANDLE_BOOL(prof) |
| 589 | CONF_HANDLE_CHAR_P(prof_prefix, "jeprof") |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 590 | CONF_HANDLE_BOOL(prof_active) |
| 591 | CONF_HANDLE_SSIZE_T(lg_prof_sample, 0, |
| 592 | (sizeof(uint64_t) << 3) - 1) |
| 593 | CONF_HANDLE_BOOL(prof_accum) |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 594 | CONF_HANDLE_SSIZE_T(lg_prof_interval, -1, |
| 595 | (sizeof(uint64_t) << 3) - 1) |
| 596 | CONF_HANDLE_BOOL(prof_gdump) |
| 597 | CONF_HANDLE_BOOL(prof_leak) |
| 598 | } |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 599 | malloc_conf_error("Invalid conf pair", k, klen, v, |
| 600 | vlen); |
| 601 | #undef CONF_HANDLE_BOOL |
| 602 | #undef CONF_HANDLE_SIZE_T |
| 603 | #undef CONF_HANDLE_SSIZE_T |
| 604 | #undef CONF_HANDLE_CHAR_P |
| 605 | } |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | static bool |
| 610 | malloc_init_hard(void) |
| 611 | { |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 612 | arena_t *init_arenas[1]; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 613 | |
| 614 | malloc_mutex_lock(&init_lock); |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 615 | if (malloc_initialized || malloc_initializer == pthread_self()) { |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 616 | /* |
| 617 | * Another thread initialized the allocator before this one |
| Jason Evans | a25d0a8 | 2009-11-09 14:57:38 -0800 | [diff] [blame] | 618 | * acquired init_lock, or this thread is the initializing |
| 619 | * thread, and it is recursively allocating. |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 620 | */ |
| 621 | malloc_mutex_unlock(&init_lock); |
| 622 | return (false); |
| 623 | } |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 624 | if (malloc_initializer != (unsigned long)0) { |
| 625 | /* Busy-wait until the initializing thread completes. */ |
| 626 | do { |
| 627 | malloc_mutex_unlock(&init_lock); |
| 628 | CPU_SPINWAIT; |
| 629 | malloc_mutex_lock(&init_lock); |
| 630 | } while (malloc_initialized == false); |
| Jason Evans | 2541e1b | 2010-07-22 11:35:59 -0700 | [diff] [blame] | 631 | malloc_mutex_unlock(&init_lock); |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 632 | return (false); |
| 633 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 634 | |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 635 | #ifdef DYNAMIC_PAGE_SHIFT |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 636 | /* Get page size. */ |
| 637 | { |
| 638 | long result; |
| 639 | |
| 640 | result = sysconf(_SC_PAGESIZE); |
| 641 | assert(result != -1); |
| Jason Evans | 30fbef8 | 2011-11-05 21:06:55 -0700 | [diff] [blame] | 642 | pagesize = (size_t)result; |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * We assume that pagesize is a power of 2 when calculating |
| Jason Evans | 94ad2b5 | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 646 | * pagesize_mask and lg_pagesize. |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 647 | */ |
| 648 | assert(((result - 1) & result) == 0); |
| 649 | pagesize_mask = result - 1; |
| Jason Evans | 94ad2b5 | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 650 | lg_pagesize = ffs((int)result) - 1; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 651 | } |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 652 | #endif |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 653 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 654 | if (config_prof) |
| 655 | prof_boot0(); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 656 | |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 657 | malloc_conf_init(); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 658 | |
| Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 659 | /* Register fork handlers. */ |
| 660 | if (pthread_atfork(jemalloc_prefork, jemalloc_postfork, |
| 661 | jemalloc_postfork) != 0) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 662 | malloc_write("<jemalloc>: Error in pthread_atfork()\n"); |
| Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 663 | if (opt_abort) |
| 664 | abort(); |
| 665 | } |
| 666 | |
| Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 667 | if (ctl_boot()) { |
| 668 | malloc_mutex_unlock(&init_lock); |
| 669 | return (true); |
| 670 | } |
| 671 | |
| Jason Evans | 03c2237 | 2010-01-03 12:10:42 -0800 | [diff] [blame] | 672 | if (opt_stats_print) { |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 673 | /* Print statistics at exit. */ |
| Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 674 | if (atexit(stats_print_atexit) != 0) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 675 | malloc_write("<jemalloc>: Error in atexit()\n"); |
| Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 676 | if (opt_abort) |
| 677 | abort(); |
| 678 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 681 | if (chunk_boot()) { |
| 682 | malloc_mutex_unlock(&init_lock); |
| 683 | return (true); |
| 684 | } |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 685 | |
| Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 686 | if (base_boot()) { |
| 687 | malloc_mutex_unlock(&init_lock); |
| 688 | return (true); |
| 689 | } |
| 690 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 691 | if (config_prof) |
| 692 | prof_boot1(); |
| Jason Evans | 3383af6 | 2010-02-11 08:59:06 -0800 | [diff] [blame] | 693 | |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 694 | arena_boot(); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 695 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 696 | if (config_tcache && tcache_boot()) { |
| Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 697 | malloc_mutex_unlock(&init_lock); |
| 698 | return (true); |
| 699 | } |
| Jason Evans | 84cbbcb | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 700 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 701 | if (huge_boot()) { |
| Jason Evans | c9658dd | 2009-06-22 14:44:08 -0700 | [diff] [blame] | 702 | malloc_mutex_unlock(&init_lock); |
| 703 | return (true); |
| 704 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 705 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 706 | #ifdef NO_TLS |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 707 | /* Initialize allocation counters before any allocations can occur. */ |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 708 | if (config_stats && pthread_key_create(&thread_allocated_tsd, |
| 709 | thread_allocated_cleanup) != 0) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 710 | malloc_mutex_unlock(&init_lock); |
| 711 | return (true); |
| 712 | } |
| 713 | #endif |
| 714 | |
| Jason Evans | 8e6f8b4 | 2011-11-03 18:40:03 -0700 | [diff] [blame] | 715 | if (malloc_mutex_init(&arenas_lock)) |
| 716 | return (true); |
| 717 | |
| 718 | if (pthread_key_create(&arenas_tsd, arenas_cleanup) != 0) { |
| 719 | malloc_mutex_unlock(&init_lock); |
| 720 | return (true); |
| 721 | } |
| 722 | |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 723 | /* |
| 724 | * Create enough scaffolding to allow recursive allocation in |
| 725 | * malloc_ncpus(). |
| 726 | */ |
| 727 | narenas = 1; |
| 728 | arenas = init_arenas; |
| 729 | memset(arenas, 0, sizeof(arena_t *) * narenas); |
| 730 | |
| 731 | /* |
| 732 | * Initialize one arena here. The rest are lazily created in |
| 733 | * choose_arena_hard(). |
| 734 | */ |
| 735 | arenas_extend(0); |
| 736 | if (arenas[0] == NULL) { |
| 737 | malloc_mutex_unlock(&init_lock); |
| 738 | return (true); |
| 739 | } |
| 740 | |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 741 | /* |
| 742 | * Assign the initial arena to the initial thread, in order to avoid |
| 743 | * spurious creation of an extra arena if the application switches to |
| 744 | * threaded mode. |
| 745 | */ |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 746 | ARENA_SET(arenas[0]); |
| Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 747 | arenas[0]->nthreads++; |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 748 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 749 | if (config_prof && prof_boot2()) { |
| Jason Evans | 3383af6 | 2010-02-11 08:59:06 -0800 | [diff] [blame] | 750 | malloc_mutex_unlock(&init_lock); |
| 751 | return (true); |
| 752 | } |
| Jason Evans | 3383af6 | 2010-02-11 08:59:06 -0800 | [diff] [blame] | 753 | |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 754 | /* Get number of CPUs. */ |
| 755 | malloc_initializer = pthread_self(); |
| 756 | malloc_mutex_unlock(&init_lock); |
| 757 | ncpus = malloc_ncpus(); |
| 758 | malloc_mutex_lock(&init_lock); |
| 759 | |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 760 | if (opt_narenas == 0) { |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 761 | /* |
| Jason Evans | 5463a52 | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 762 | * For SMP systems, create more than one arena per CPU by |
| 763 | * default. |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 764 | */ |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 765 | if (ncpus > 1) |
| 766 | opt_narenas = ncpus << 2; |
| 767 | else |
| 768 | opt_narenas = 1; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 769 | } |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 770 | narenas = opt_narenas; |
| 771 | /* |
| 772 | * Make sure that the arenas array can be allocated. In practice, this |
| 773 | * limit is enough to allow the allocator to function, but the ctl |
| 774 | * machinery will fail to allocate memory at far lower limits. |
| 775 | */ |
| 776 | if (narenas > chunksize / sizeof(arena_t *)) { |
| 777 | char buf[UMAX2S_BUFSIZE]; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 778 | |
| Jason Evans | e733970 | 2010-10-23 18:37:06 -0700 | [diff] [blame] | 779 | narenas = chunksize / sizeof(arena_t *); |
| 780 | malloc_write("<jemalloc>: Reducing narenas to limit ("); |
| 781 | malloc_write(u2s(narenas, 10, buf)); |
| 782 | malloc_write(")\n"); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 783 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 784 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 785 | /* Allocate and initialize arenas. */ |
| 786 | arenas = (arena_t **)base_alloc(sizeof(arena_t *) * narenas); |
| 787 | if (arenas == NULL) { |
| 788 | malloc_mutex_unlock(&init_lock); |
| 789 | return (true); |
| 790 | } |
| 791 | /* |
| 792 | * Zero the array. In practice, this should always be pre-zeroed, |
| 793 | * since it was just mmap()ed, but let's be sure. |
| 794 | */ |
| 795 | memset(arenas, 0, sizeof(arena_t *) * narenas); |
| Jason Evans | b7924f5 | 2009-06-23 19:01:18 -0700 | [diff] [blame] | 796 | /* Copy the pointer to the one arena that was already initialized. */ |
| 797 | arenas[0] = init_arenas[0]; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 798 | |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 799 | #ifdef JEMALLOC_ZONE |
| 800 | /* Register the custom zone. */ |
| 801 | malloc_zone_register(create_zone()); |
| 802 | |
| 803 | /* |
| 804 | * Convert the default szone to an "overlay zone" that is capable of |
| 805 | * deallocating szone-allocated objects, but allocating new objects |
| 806 | * from jemalloc. |
| 807 | */ |
| 808 | szone2ozone(malloc_default_zone()); |
| 809 | #endif |
| 810 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 811 | malloc_initialized = true; |
| 812 | malloc_mutex_unlock(&init_lock); |
| 813 | return (false); |
| 814 | } |
| 815 | |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 816 | #ifdef JEMALLOC_ZONE |
| 817 | JEMALLOC_ATTR(constructor) |
| 818 | void |
| 819 | jemalloc_darwin_init(void) |
| 820 | { |
| 821 | |
| 822 | if (malloc_init_hard()) |
| 823 | abort(); |
| 824 | } |
| 825 | #endif |
| 826 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 827 | /* |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 828 | * End initialization functions. |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 829 | */ |
| 830 | /******************************************************************************/ |
| 831 | /* |
| 832 | * Begin malloc(3)-compatible functions. |
| 833 | */ |
| 834 | |
| Jason Evans | 9ad4823 | 2010-01-03 11:59:20 -0800 | [diff] [blame] | 835 | JEMALLOC_ATTR(malloc) |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 836 | JEMALLOC_ATTR(visibility("default")) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 837 | void * |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 838 | JEMALLOC_P(malloc)(size_t size) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 839 | { |
| 840 | void *ret; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 841 | size_t usize; |
| Jason Evans | 6ffbbeb | 2012-02-13 12:31:30 -0800 | [diff] [blame] | 842 | prof_thr_cnt_t *cnt |
| 843 | #ifdef JEMALLOC_CC_SILENCE |
| 844 | = NULL |
| 845 | #endif |
| 846 | ; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 847 | |
| 848 | if (malloc_init()) { |
| 849 | ret = NULL; |
| Jason Evans | f251814 | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 850 | goto OOM; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| Jason Evans | c90ad71 | 2012-02-28 20:31:37 -0800 | [diff] [blame] | 853 | if (size == 0) |
| 854 | size = 1; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 855 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 856 | if (config_prof && opt_prof) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 857 | usize = s2u(size); |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 858 | PROF_ALLOC_PREP(1, usize, cnt); |
| 859 | if (cnt == NULL) { |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 860 | ret = NULL; |
| 861 | goto OOM; |
| 862 | } |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 863 | if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <= |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 864 | SMALL_MAXCLASS) { |
| 865 | ret = imalloc(SMALL_MAXCLASS+1); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 866 | if (ret != NULL) |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 867 | arena_prof_promoted(ret, usize); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 868 | } else |
| 869 | ret = imalloc(size); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 870 | } else { |
| 871 | if (config_stats) |
| 872 | usize = s2u(size); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 873 | ret = imalloc(size); |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 874 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 875 | |
| Jason Evans | f251814 | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 876 | OOM: |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 877 | if (ret == NULL) { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 878 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 879 | malloc_write("<jemalloc>: Error in malloc(): " |
| 880 | "out of memory\n"); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 881 | abort(); |
| 882 | } |
| 883 | errno = ENOMEM; |
| 884 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 885 | if (config_prof && opt_prof && ret != NULL) |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 886 | prof_malloc(ret, usize, cnt); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 887 | if (config_stats && ret != NULL) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 888 | assert(usize == isalloc(ret)); |
| 889 | ALLOCATED_ADD(usize, 0); |
| 890 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 891 | return (ret); |
| 892 | } |
| 893 | |
| Jason Evans | 9ad4823 | 2010-01-03 11:59:20 -0800 | [diff] [blame] | 894 | JEMALLOC_ATTR(nonnull(1)) |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 895 | #ifdef JEMALLOC_PROF |
| 896 | /* |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 897 | * Avoid any uncertainty as to how many backtrace frames to ignore in |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 898 | * PROF_ALLOC_PREP(). |
| 899 | */ |
| 900 | JEMALLOC_ATTR(noinline) |
| 901 | #endif |
| 902 | static int |
| 903 | imemalign(void **memptr, size_t alignment, size_t size) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 904 | { |
| 905 | int ret; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 906 | size_t usize; |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 907 | void *result; |
| Jason Evans | 6ffbbeb | 2012-02-13 12:31:30 -0800 | [diff] [blame] | 908 | prof_thr_cnt_t *cnt |
| 909 | #ifdef JEMALLOC_CC_SILENCE |
| 910 | = NULL |
| 911 | #endif |
| 912 | ; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 913 | |
| 914 | if (malloc_init()) |
| 915 | result = NULL; |
| 916 | else { |
| Jason Evans | c90ad71 | 2012-02-28 20:31:37 -0800 | [diff] [blame] | 917 | if (size == 0) |
| 918 | size = 1; |
| Jason Evans | f251814 | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 919 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 920 | /* Make sure that alignment is a large enough power of 2. */ |
| 921 | if (((alignment - 1) & alignment) != 0 |
| 922 | || alignment < sizeof(void *)) { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 923 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 924 | malloc_write("<jemalloc>: Error in " |
| 925 | "posix_memalign(): invalid alignment\n"); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 926 | abort(); |
| 927 | } |
| 928 | result = NULL; |
| 929 | ret = EINVAL; |
| 930 | goto RETURN; |
| 931 | } |
| 932 | |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 933 | usize = sa2u(size, alignment, NULL); |
| 934 | if (usize == 0) { |
| 935 | result = NULL; |
| 936 | ret = ENOMEM; |
| 937 | goto RETURN; |
| 938 | } |
| 939 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 940 | if (config_prof && opt_prof) { |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 941 | PROF_ALLOC_PREP(2, usize, cnt); |
| 942 | if (cnt == NULL) { |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 943 | result = NULL; |
| 944 | ret = EINVAL; |
| 945 | } else { |
| 946 | if (prof_promote && (uintptr_t)cnt != |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 947 | (uintptr_t)1U && usize <= SMALL_MAXCLASS) { |
| 948 | assert(sa2u(SMALL_MAXCLASS+1, |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 949 | alignment, NULL) != 0); |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 950 | result = ipalloc(sa2u(SMALL_MAXCLASS+1, |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 951 | alignment, NULL), alignment, false); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 952 | if (result != NULL) { |
| 953 | arena_prof_promoted(result, |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 954 | usize); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 955 | } |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 956 | } else { |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 957 | result = ipalloc(usize, alignment, |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 958 | false); |
| 959 | } |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 960 | } |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 961 | } else |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 962 | result = ipalloc(usize, alignment, false); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | if (result == NULL) { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 966 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 967 | malloc_write("<jemalloc>: Error in posix_memalign(): " |
| 968 | "out of memory\n"); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 969 | abort(); |
| 970 | } |
| 971 | ret = ENOMEM; |
| 972 | goto RETURN; |
| 973 | } |
| 974 | |
| 975 | *memptr = result; |
| 976 | ret = 0; |
| 977 | |
| 978 | RETURN: |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 979 | if (config_stats && result != NULL) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 980 | assert(usize == isalloc(result)); |
| 981 | ALLOCATED_ADD(usize, 0); |
| 982 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 983 | if (config_prof && opt_prof && result != NULL) |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 984 | prof_malloc(result, usize, cnt); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 985 | return (ret); |
| 986 | } |
| 987 | |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 988 | JEMALLOC_ATTR(nonnull(1)) |
| 989 | JEMALLOC_ATTR(visibility("default")) |
| 990 | int |
| 991 | JEMALLOC_P(posix_memalign)(void **memptr, size_t alignment, size_t size) |
| 992 | { |
| 993 | |
| 994 | return imemalign(memptr, alignment, size); |
| 995 | } |
| 996 | |
| Jason Evans | 9ad4823 | 2010-01-03 11:59:20 -0800 | [diff] [blame] | 997 | JEMALLOC_ATTR(malloc) |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 998 | JEMALLOC_ATTR(visibility("default")) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 999 | void * |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1000 | JEMALLOC_P(calloc)(size_t num, size_t size) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1001 | { |
| 1002 | void *ret; |
| 1003 | size_t num_size; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1004 | size_t usize; |
| Jason Evans | 6ffbbeb | 2012-02-13 12:31:30 -0800 | [diff] [blame] | 1005 | prof_thr_cnt_t *cnt |
| 1006 | #ifdef JEMALLOC_CC_SILENCE |
| 1007 | = NULL |
| 1008 | #endif |
| 1009 | ; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1010 | |
| 1011 | if (malloc_init()) { |
| 1012 | num_size = 0; |
| 1013 | ret = NULL; |
| 1014 | goto RETURN; |
| 1015 | } |
| 1016 | |
| 1017 | num_size = num * size; |
| 1018 | if (num_size == 0) { |
| Jason Evans | c90ad71 | 2012-02-28 20:31:37 -0800 | [diff] [blame] | 1019 | if (num == 0 || size == 0) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1020 | num_size = 1; |
| 1021 | else { |
| 1022 | ret = NULL; |
| 1023 | goto RETURN; |
| 1024 | } |
| 1025 | /* |
| 1026 | * Try to avoid division here. We know that it isn't possible to |
| 1027 | * overflow during multiplication if neither operand uses any of the |
| 1028 | * most significant half of the bits in a size_t. |
| 1029 | */ |
| 1030 | } else if (((num | size) & (SIZE_T_MAX << (sizeof(size_t) << 2))) |
| 1031 | && (num_size / size != num)) { |
| 1032 | /* size_t overflow. */ |
| 1033 | ret = NULL; |
| 1034 | goto RETURN; |
| 1035 | } |
| 1036 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1037 | if (config_prof && opt_prof) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1038 | usize = s2u(num_size); |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 1039 | PROF_ALLOC_PREP(1, usize, cnt); |
| 1040 | if (cnt == NULL) { |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1041 | ret = NULL; |
| 1042 | goto RETURN; |
| 1043 | } |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1044 | if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1045 | <= SMALL_MAXCLASS) { |
| 1046 | ret = icalloc(SMALL_MAXCLASS+1); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1047 | if (ret != NULL) |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1048 | arena_prof_promoted(ret, usize); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1049 | } else |
| 1050 | ret = icalloc(num_size); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1051 | } else { |
| 1052 | if (config_stats) |
| 1053 | usize = s2u(num_size); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1054 | ret = icalloc(num_size); |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1055 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1056 | |
| 1057 | RETURN: |
| 1058 | if (ret == NULL) { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1059 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 1060 | malloc_write("<jemalloc>: Error in calloc(): out of " |
| 1061 | "memory\n"); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1062 | abort(); |
| 1063 | } |
| 1064 | errno = ENOMEM; |
| 1065 | } |
| 1066 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1067 | if (config_prof && opt_prof && ret != NULL) |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1068 | prof_malloc(ret, usize, cnt); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1069 | if (config_stats && ret != NULL) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1070 | assert(usize == isalloc(ret)); |
| 1071 | ALLOCATED_ADD(usize, 0); |
| 1072 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1073 | return (ret); |
| 1074 | } |
| 1075 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1076 | JEMALLOC_ATTR(visibility("default")) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1077 | void * |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1078 | JEMALLOC_P(realloc)(void *ptr, size_t size) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1079 | { |
| 1080 | void *ret; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1081 | size_t usize; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1082 | size_t old_size = 0; |
| Jason Evans | 6ffbbeb | 2012-02-13 12:31:30 -0800 | [diff] [blame] | 1083 | prof_thr_cnt_t *cnt |
| 1084 | #ifdef JEMALLOC_CC_SILENCE |
| 1085 | = NULL |
| 1086 | #endif |
| 1087 | ; |
| 1088 | prof_ctx_t *old_ctx |
| 1089 | #ifdef JEMALLOC_CC_SILENCE |
| 1090 | = NULL |
| 1091 | #endif |
| 1092 | ; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1093 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1094 | if (size == 0) { |
| Jason Evans | f081b88 | 2012-02-28 20:24:05 -0800 | [diff] [blame] | 1095 | if (ptr != NULL) { |
| 1096 | /* realloc(ptr, 0) is equivalent to free(p). */ |
| 1097 | if (config_prof || config_stats) |
| 1098 | old_size = isalloc(ptr); |
| 1099 | if (config_prof && opt_prof) { |
| 1100 | old_ctx = prof_ctx_get(ptr); |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1101 | cnt = NULL; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1102 | } |
| Jason Evans | f081b88 | 2012-02-28 20:24:05 -0800 | [diff] [blame] | 1103 | idalloc(ptr); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1104 | ret = NULL; |
| 1105 | goto RETURN; |
| Jason Evans | c90ad71 | 2012-02-28 20:31:37 -0800 | [diff] [blame] | 1106 | } else |
| 1107 | size = 1; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | if (ptr != NULL) { |
| Jason Evans | a25d0a8 | 2009-11-09 14:57:38 -0800 | [diff] [blame] | 1111 | assert(malloc_initialized || malloc_initializer == |
| 1112 | pthread_self()); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1113 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1114 | if (config_prof || config_stats) |
| 1115 | old_size = isalloc(ptr); |
| 1116 | if (config_prof && opt_prof) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1117 | usize = s2u(size); |
| Jason Evans | 5065156 | 2010-04-13 16:13:54 -0700 | [diff] [blame] | 1118 | old_ctx = prof_ctx_get(ptr); |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 1119 | PROF_ALLOC_PREP(1, usize, cnt); |
| 1120 | if (cnt == NULL) { |
| Jason Evans | 46405e6 | 2011-08-30 23:37:29 -0700 | [diff] [blame] | 1121 | old_ctx = NULL; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1122 | ret = NULL; |
| 1123 | goto OOM; |
| 1124 | } |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1125 | if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1126 | usize <= SMALL_MAXCLASS) { |
| 1127 | ret = iralloc(ptr, SMALL_MAXCLASS+1, 0, 0, |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1128 | false, false); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1129 | if (ret != NULL) |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1130 | arena_prof_promoted(ret, usize); |
| Jason Evans | 46405e6 | 2011-08-30 23:37:29 -0700 | [diff] [blame] | 1131 | else |
| 1132 | old_ctx = NULL; |
| 1133 | } else { |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1134 | ret = iralloc(ptr, size, 0, 0, false, false); |
| Jason Evans | 46405e6 | 2011-08-30 23:37:29 -0700 | [diff] [blame] | 1135 | if (ret == NULL) |
| 1136 | old_ctx = NULL; |
| 1137 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1138 | } else { |
| 1139 | if (config_stats) |
| 1140 | usize = s2u(size); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1141 | ret = iralloc(ptr, size, 0, 0, false, false); |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1142 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1143 | |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1144 | OOM: |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1145 | if (ret == NULL) { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1146 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 1147 | malloc_write("<jemalloc>: Error in realloc(): " |
| 1148 | "out of memory\n"); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1149 | abort(); |
| 1150 | } |
| 1151 | errno = ENOMEM; |
| 1152 | } |
| 1153 | } else { |
| Jason Evans | f081b88 | 2012-02-28 20:24:05 -0800 | [diff] [blame] | 1154 | /* realloc(NULL, size) is equivalent to malloc(size). */ |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1155 | if (config_prof && opt_prof) |
| Jason Evans | 5065156 | 2010-04-13 16:13:54 -0700 | [diff] [blame] | 1156 | old_ctx = NULL; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1157 | if (malloc_init()) { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1158 | if (config_prof && opt_prof) |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1159 | cnt = NULL; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1160 | ret = NULL; |
| 1161 | } else { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1162 | if (config_prof && opt_prof) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1163 | usize = s2u(size); |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 1164 | PROF_ALLOC_PREP(1, usize, cnt); |
| 1165 | if (cnt == NULL) |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1166 | ret = NULL; |
| 1167 | else { |
| 1168 | if (prof_promote && (uintptr_t)cnt != |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1169 | (uintptr_t)1U && usize <= |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1170 | SMALL_MAXCLASS) { |
| 1171 | ret = imalloc(SMALL_MAXCLASS+1); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1172 | if (ret != NULL) { |
| 1173 | arena_prof_promoted(ret, |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1174 | usize); |
| Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } else |
| 1177 | ret = imalloc(size); |
| 1178 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1179 | } else { |
| 1180 | if (config_stats) |
| 1181 | usize = s2u(size); |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1182 | ret = imalloc(size); |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1183 | } |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1184 | } |
| Jason Evans | 569432c | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 1185 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1186 | if (ret == NULL) { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1187 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 1188 | malloc_write("<jemalloc>: Error in realloc(): " |
| 1189 | "out of memory\n"); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1190 | abort(); |
| 1191 | } |
| 1192 | errno = ENOMEM; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | RETURN: |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1197 | if (config_prof && opt_prof) |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1198 | prof_realloc(ret, usize, cnt, old_size, old_ctx); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1199 | if (config_stats && ret != NULL) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1200 | assert(usize == isalloc(ret)); |
| 1201 | ALLOCATED_ADD(usize, old_size); |
| 1202 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1203 | return (ret); |
| 1204 | } |
| 1205 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1206 | JEMALLOC_ATTR(visibility("default")) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1207 | void |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1208 | JEMALLOC_P(free)(void *ptr) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1209 | { |
| 1210 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1211 | if (ptr != NULL) { |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1212 | size_t usize; |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1213 | |
| Jason Evans | a25d0a8 | 2009-11-09 14:57:38 -0800 | [diff] [blame] | 1214 | assert(malloc_initialized || malloc_initializer == |
| 1215 | pthread_self()); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1216 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1217 | if (config_prof && opt_prof) { |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1218 | usize = isalloc(ptr); |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1219 | prof_free(ptr, usize); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1220 | } else if (config_stats) { |
| 1221 | usize = isalloc(ptr); |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1222 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1223 | if (config_stats) |
| 1224 | ALLOCATED_ADD(0, usize); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1225 | idalloc(ptr); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * End malloc(3)-compatible functions. |
| 1231 | */ |
| 1232 | /******************************************************************************/ |
| 1233 | /* |
| Jason Evans | 6a0d291 | 2010-09-20 16:44:23 -0700 | [diff] [blame] | 1234 | * Begin non-standard override functions. |
| 1235 | * |
| 1236 | * These overrides are omitted if the JEMALLOC_PREFIX is defined, since the |
| 1237 | * entire point is to avoid accidental mixed allocator usage. |
| 1238 | */ |
| 1239 | #ifndef JEMALLOC_PREFIX |
| 1240 | |
| 1241 | #ifdef JEMALLOC_OVERRIDE_MEMALIGN |
| 1242 | JEMALLOC_ATTR(malloc) |
| 1243 | JEMALLOC_ATTR(visibility("default")) |
| 1244 | void * |
| 1245 | JEMALLOC_P(memalign)(size_t alignment, size_t size) |
| 1246 | { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1247 | void *ret |
| Jason Evans | 355b438 | 2010-09-20 19:20:48 -0700 | [diff] [blame] | 1248 | #ifdef JEMALLOC_CC_SILENCE |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1249 | = NULL |
| Jason Evans | 355b438 | 2010-09-20 19:20:48 -0700 | [diff] [blame] | 1250 | #endif |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1251 | ; |
| 1252 | imemalign(&ret, alignment, size); |
| Jason Evans | 6a0d291 | 2010-09-20 16:44:23 -0700 | [diff] [blame] | 1253 | return (ret); |
| 1254 | } |
| 1255 | #endif |
| 1256 | |
| 1257 | #ifdef JEMALLOC_OVERRIDE_VALLOC |
| 1258 | JEMALLOC_ATTR(malloc) |
| 1259 | JEMALLOC_ATTR(visibility("default")) |
| 1260 | void * |
| 1261 | JEMALLOC_P(valloc)(size_t size) |
| 1262 | { |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1263 | void *ret |
| Jason Evans | 355b438 | 2010-09-20 19:20:48 -0700 | [diff] [blame] | 1264 | #ifdef JEMALLOC_CC_SILENCE |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1265 | = NULL |
| Jason Evans | 355b438 | 2010-09-20 19:20:48 -0700 | [diff] [blame] | 1266 | #endif |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1267 | ; |
| 1268 | imemalign(&ret, PAGE_SIZE, size); |
| Jason Evans | 6a0d291 | 2010-09-20 16:44:23 -0700 | [diff] [blame] | 1269 | return (ret); |
| 1270 | } |
| 1271 | #endif |
| 1272 | |
| 1273 | #endif /* JEMALLOC_PREFIX */ |
| 1274 | /* |
| 1275 | * End non-standard override functions. |
| 1276 | */ |
| 1277 | /******************************************************************************/ |
| 1278 | /* |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1279 | * Begin non-standard functions. |
| 1280 | */ |
| 1281 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1282 | JEMALLOC_ATTR(visibility("default")) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1283 | size_t |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1284 | JEMALLOC_P(malloc_usable_size)(const void *ptr) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1285 | { |
| Jason Evans | 569432c | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 1286 | size_t ret; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1287 | |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1288 | assert(malloc_initialized || malloc_initializer == pthread_self()); |
| 1289 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1290 | if (config_ivsalloc) |
| 1291 | ret = ivsalloc(ptr); |
| 1292 | else { |
| 1293 | assert(ptr != NULL); |
| 1294 | ret = isalloc(ptr); |
| 1295 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1296 | |
| Jason Evans | 569432c | 2009-12-29 00:09:15 -0800 | [diff] [blame] | 1297 | return (ret); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
| Jason Evans | 4201af0 | 2010-01-24 02:53:40 -0800 | [diff] [blame] | 1300 | JEMALLOC_ATTR(visibility("default")) |
| 1301 | void |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 1302 | JEMALLOC_P(malloc_stats_print)(void (*write_cb)(void *, const char *), |
| 1303 | void *cbopaque, const char *opts) |
| Jason Evans | 4201af0 | 2010-01-24 02:53:40 -0800 | [diff] [blame] | 1304 | { |
| 1305 | |
| Jason Evans | 698805c | 2010-03-03 17:45:38 -0800 | [diff] [blame] | 1306 | stats_print(write_cb, cbopaque, opts); |
| Jason Evans | 4201af0 | 2010-01-24 02:53:40 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 1309 | JEMALLOC_ATTR(visibility("default")) |
| 1310 | int |
| 1311 | JEMALLOC_P(mallctl)(const char *name, void *oldp, size_t *oldlenp, void *newp, |
| 1312 | size_t newlen) |
| 1313 | { |
| 1314 | |
| Jason Evans | 9583331 | 2010-01-27 13:45:21 -0800 | [diff] [blame] | 1315 | if (malloc_init()) |
| 1316 | return (EAGAIN); |
| 1317 | |
| Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 1318 | return (ctl_byname(name, oldp, oldlenp, newp, newlen)); |
| 1319 | } |
| 1320 | |
| 1321 | JEMALLOC_ATTR(visibility("default")) |
| 1322 | int |
| 1323 | JEMALLOC_P(mallctlnametomib)(const char *name, size_t *mibp, size_t *miblenp) |
| 1324 | { |
| 1325 | |
| Jason Evans | 9583331 | 2010-01-27 13:45:21 -0800 | [diff] [blame] | 1326 | if (malloc_init()) |
| 1327 | return (EAGAIN); |
| 1328 | |
| Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 1329 | return (ctl_nametomib(name, mibp, miblenp)); |
| 1330 | } |
| 1331 | |
| 1332 | JEMALLOC_ATTR(visibility("default")) |
| 1333 | int |
| 1334 | JEMALLOC_P(mallctlbymib)(const size_t *mib, size_t miblen, void *oldp, |
| 1335 | size_t *oldlenp, void *newp, size_t newlen) |
| 1336 | { |
| 1337 | |
| Jason Evans | 9583331 | 2010-01-27 13:45:21 -0800 | [diff] [blame] | 1338 | if (malloc_init()) |
| 1339 | return (EAGAIN); |
| 1340 | |
| Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 1341 | return (ctl_bymib(mib, miblen, oldp, oldlenp, newp, newlen)); |
| 1342 | } |
| 1343 | |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1344 | JEMALLOC_INLINE void * |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1345 | iallocm(size_t usize, size_t alignment, bool zero) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1346 | { |
| 1347 | |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1348 | assert(usize == ((alignment == 0) ? s2u(usize) : sa2u(usize, alignment, |
| 1349 | NULL))); |
| 1350 | |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1351 | if (alignment != 0) |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1352 | return (ipalloc(usize, alignment, zero)); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1353 | else if (zero) |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1354 | return (icalloc(usize)); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1355 | else |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1356 | return (imalloc(usize)); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
| Jason Evans | 6a0d291 | 2010-09-20 16:44:23 -0700 | [diff] [blame] | 1359 | JEMALLOC_ATTR(nonnull(1)) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1360 | JEMALLOC_ATTR(visibility("default")) |
| 1361 | int |
| 1362 | JEMALLOC_P(allocm)(void **ptr, size_t *rsize, size_t size, int flags) |
| 1363 | { |
| 1364 | void *p; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1365 | size_t usize; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1366 | size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK) |
| 1367 | & (SIZE_T_MAX-1)); |
| 1368 | bool zero = flags & ALLOCM_ZERO; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1369 | prof_thr_cnt_t *cnt; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1370 | |
| 1371 | assert(ptr != NULL); |
| 1372 | assert(size != 0); |
| 1373 | |
| 1374 | if (malloc_init()) |
| 1375 | goto OOM; |
| 1376 | |
| Jason Evans | 749c2a0 | 2011-08-12 18:37:54 -0700 | [diff] [blame] | 1377 | usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment, NULL); |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1378 | if (usize == 0) |
| 1379 | goto OOM; |
| 1380 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1381 | if (config_prof && opt_prof) { |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 1382 | PROF_ALLOC_PREP(1, usize, cnt); |
| 1383 | if (cnt == NULL) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1384 | goto OOM; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1385 | if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U && usize <= |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1386 | SMALL_MAXCLASS) { |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1387 | size_t usize_promoted = (alignment == 0) ? |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1388 | s2u(SMALL_MAXCLASS+1) : sa2u(SMALL_MAXCLASS+1, |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1389 | alignment, NULL); |
| 1390 | assert(usize_promoted != 0); |
| 1391 | p = iallocm(usize_promoted, alignment, zero); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1392 | if (p == NULL) |
| 1393 | goto OOM; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1394 | arena_prof_promoted(p, usize); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1395 | } else { |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1396 | p = iallocm(usize, alignment, zero); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1397 | if (p == NULL) |
| 1398 | goto OOM; |
| 1399 | } |
| Jason Evans | 749c2a0 | 2011-08-12 18:37:54 -0700 | [diff] [blame] | 1400 | prof_malloc(p, usize, cnt); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1401 | } else { |
| Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1402 | p = iallocm(usize, alignment, zero); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1403 | if (p == NULL) |
| 1404 | goto OOM; |
| 1405 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1406 | if (rsize != NULL) |
| 1407 | *rsize = usize; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1408 | |
| 1409 | *ptr = p; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1410 | if (config_stats) { |
| 1411 | assert(usize == isalloc(p)); |
| 1412 | ALLOCATED_ADD(usize, 0); |
| 1413 | } |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1414 | return (ALLOCM_SUCCESS); |
| 1415 | OOM: |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1416 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1417 | malloc_write("<jemalloc>: Error in allocm(): " |
| 1418 | "out of memory\n"); |
| 1419 | abort(); |
| 1420 | } |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1421 | *ptr = NULL; |
| 1422 | return (ALLOCM_ERR_OOM); |
| 1423 | } |
| 1424 | |
| Jason Evans | 6a0d291 | 2010-09-20 16:44:23 -0700 | [diff] [blame] | 1425 | JEMALLOC_ATTR(nonnull(1)) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1426 | JEMALLOC_ATTR(visibility("default")) |
| 1427 | int |
| 1428 | JEMALLOC_P(rallocm)(void **ptr, size_t *rsize, size_t size, size_t extra, |
| 1429 | int flags) |
| 1430 | { |
| 1431 | void *p, *q; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1432 | size_t usize; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1433 | size_t old_size; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1434 | size_t alignment = (ZU(1) << (flags & ALLOCM_LG_ALIGN_MASK) |
| 1435 | & (SIZE_T_MAX-1)); |
| 1436 | bool zero = flags & ALLOCM_ZERO; |
| 1437 | bool no_move = flags & ALLOCM_NO_MOVE; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1438 | prof_thr_cnt_t *cnt; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1439 | |
| 1440 | assert(ptr != NULL); |
| 1441 | assert(*ptr != NULL); |
| 1442 | assert(size != 0); |
| 1443 | assert(SIZE_T_MAX - size >= extra); |
| 1444 | assert(malloc_initialized || malloc_initializer == pthread_self()); |
| 1445 | |
| 1446 | p = *ptr; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1447 | if (config_prof && opt_prof) { |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1448 | /* |
| 1449 | * usize isn't knowable before iralloc() returns when extra is |
| 1450 | * non-zero. Therefore, compute its maximum possible value and |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 1451 | * use that in PROF_ALLOC_PREP() to decide whether to capture a |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1452 | * backtrace. prof_realloc() will use the actual usize to |
| 1453 | * decide whether to sample. |
| 1454 | */ |
| 1455 | size_t max_usize = (alignment == 0) ? s2u(size+extra) : |
| 1456 | sa2u(size+extra, alignment, NULL); |
| Jason Evans | 46405e6 | 2011-08-30 23:37:29 -0700 | [diff] [blame] | 1457 | prof_ctx_t *old_ctx = prof_ctx_get(p); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1458 | old_size = isalloc(p); |
| Jason Evans | a507004 | 2011-08-12 13:48:27 -0700 | [diff] [blame] | 1459 | PROF_ALLOC_PREP(1, max_usize, cnt); |
| 1460 | if (cnt == NULL) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1461 | goto OOM; |
| Jason Evans | 183ba50 | 2011-08-11 22:51:00 -0700 | [diff] [blame] | 1462 | /* |
| 1463 | * Use minimum usize to determine whether promotion may happen. |
| 1464 | */ |
| 1465 | if (prof_promote && (uintptr_t)cnt != (uintptr_t)1U |
| 1466 | && ((alignment == 0) ? s2u(size) : sa2u(size, |
| Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1467 | alignment, NULL)) <= SMALL_MAXCLASS) { |
| 1468 | q = iralloc(p, SMALL_MAXCLASS+1, (SMALL_MAXCLASS+1 >= |
| 1469 | size+extra) ? 0 : size+extra - (SMALL_MAXCLASS+1), |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1470 | alignment, zero, no_move); |
| 1471 | if (q == NULL) |
| 1472 | goto ERR; |
| Jason Evans | 183ba50 | 2011-08-11 22:51:00 -0700 | [diff] [blame] | 1473 | if (max_usize < PAGE_SIZE) { |
| 1474 | usize = max_usize; |
| 1475 | arena_prof_promoted(q, usize); |
| Jason Evans | b493ce2 | 2011-08-12 11:28:47 -0700 | [diff] [blame] | 1476 | } else |
| 1477 | usize = isalloc(q); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1478 | } else { |
| 1479 | q = iralloc(p, size, extra, alignment, zero, no_move); |
| 1480 | if (q == NULL) |
| 1481 | goto ERR; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1482 | usize = isalloc(q); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1483 | } |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1484 | prof_realloc(q, usize, cnt, old_size, old_ctx); |
| Jason Evans | eacb896 | 2011-03-23 00:30:30 -0700 | [diff] [blame] | 1485 | if (rsize != NULL) |
| 1486 | *rsize = usize; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1487 | } else { |
| 1488 | if (config_stats) |
| 1489 | old_size = isalloc(p); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1490 | q = iralloc(p, size, extra, alignment, zero, no_move); |
| 1491 | if (q == NULL) |
| 1492 | goto ERR; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1493 | if (config_stats) |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1494 | usize = isalloc(q); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1495 | if (rsize != NULL) { |
| 1496 | if (config_stats == false) |
| 1497 | usize = isalloc(q); |
| 1498 | *rsize = usize; |
| Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1499 | } |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | *ptr = q; |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1503 | if (config_stats) |
| 1504 | ALLOCATED_ADD(usize, old_size); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1505 | return (ALLOCM_SUCCESS); |
| 1506 | ERR: |
| 1507 | if (no_move) |
| 1508 | return (ALLOCM_ERR_NOT_MOVED); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1509 | OOM: |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1510 | if (config_xmalloc && opt_xmalloc) { |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1511 | malloc_write("<jemalloc>: Error in rallocm(): " |
| 1512 | "out of memory\n"); |
| 1513 | abort(); |
| 1514 | } |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1515 | return (ALLOCM_ERR_OOM); |
| 1516 | } |
| 1517 | |
| Jason Evans | 6a0d291 | 2010-09-20 16:44:23 -0700 | [diff] [blame] | 1518 | JEMALLOC_ATTR(nonnull(1)) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1519 | JEMALLOC_ATTR(visibility("default")) |
| 1520 | int |
| 1521 | JEMALLOC_P(sallocm)(const void *ptr, size_t *rsize, int flags) |
| 1522 | { |
| 1523 | size_t sz; |
| 1524 | |
| 1525 | assert(malloc_initialized || malloc_initializer == pthread_self()); |
| 1526 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1527 | if (config_ivsalloc) |
| 1528 | sz = ivsalloc(ptr); |
| 1529 | else { |
| 1530 | assert(ptr != NULL); |
| 1531 | sz = isalloc(ptr); |
| 1532 | } |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1533 | assert(rsize != NULL); |
| 1534 | *rsize = sz; |
| 1535 | |
| 1536 | return (ALLOCM_SUCCESS); |
| 1537 | } |
| 1538 | |
| Jason Evans | 6a0d291 | 2010-09-20 16:44:23 -0700 | [diff] [blame] | 1539 | JEMALLOC_ATTR(nonnull(1)) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1540 | JEMALLOC_ATTR(visibility("default")) |
| 1541 | int |
| 1542 | JEMALLOC_P(dallocm)(void *ptr, int flags) |
| 1543 | { |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1544 | size_t usize; |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1545 | |
| 1546 | assert(ptr != NULL); |
| 1547 | assert(malloc_initialized || malloc_initializer == pthread_self()); |
| 1548 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1549 | if (config_stats) |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1550 | usize = isalloc(ptr); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1551 | if (config_prof && opt_prof) { |
| 1552 | if (config_stats == false) |
| 1553 | usize = isalloc(ptr); |
| Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 1554 | prof_free(ptr, usize); |
| 1555 | } |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1556 | if (config_stats) |
| 1557 | ALLOCATED_ADD(0, usize); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1558 | idalloc(ptr); |
| 1559 | |
| 1560 | return (ALLOCM_SUCCESS); |
| 1561 | } |
| 1562 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1563 | /* |
| 1564 | * End non-standard functions. |
| 1565 | */ |
| 1566 | /******************************************************************************/ |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1567 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1568 | /* |
| 1569 | * The following functions are used by threading libraries for protection of |
| Jason Evans | 28177d4 | 2010-09-20 11:24:24 -0700 | [diff] [blame] | 1570 | * malloc during fork(). |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1571 | */ |
| 1572 | |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 1573 | void |
| Jason Evans | 804c9ec | 2009-06-22 17:44:33 -0700 | [diff] [blame] | 1574 | jemalloc_prefork(void) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1575 | { |
| Jason Evans | fbbb624 | 2010-01-24 17:56:48 -0800 | [diff] [blame] | 1576 | unsigned i; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1577 | |
| 1578 | /* Acquire all mutexes in a safe order. */ |
| 1579 | |
| Jason Evans | fbbb624 | 2010-01-24 17:56:48 -0800 | [diff] [blame] | 1580 | malloc_mutex_lock(&arenas_lock); |
| 1581 | for (i = 0; i < narenas; i++) { |
| 1582 | if (arenas[i] != NULL) |
| 1583 | malloc_mutex_lock(&arenas[i]->lock); |
| 1584 | } |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1585 | |
| 1586 | malloc_mutex_lock(&base_mtx); |
| 1587 | |
| 1588 | malloc_mutex_lock(&huge_mtx); |
| 1589 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1590 | if (config_dss) |
| 1591 | malloc_mutex_lock(&dss_mtx); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 1594 | void |
| Jason Evans | 804c9ec | 2009-06-22 17:44:33 -0700 | [diff] [blame] | 1595 | jemalloc_postfork(void) |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1596 | { |
| 1597 | unsigned i; |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1598 | |
| 1599 | /* Release all mutexes, now that fork() has completed. */ |
| 1600 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1601 | if (config_dss) |
| 1602 | malloc_mutex_unlock(&dss_mtx); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1603 | |
| 1604 | malloc_mutex_unlock(&huge_mtx); |
| 1605 | |
| 1606 | malloc_mutex_unlock(&base_mtx); |
| 1607 | |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1608 | for (i = 0; i < narenas; i++) { |
| Jason Evans | fbbb624 | 2010-01-24 17:56:48 -0800 | [diff] [blame] | 1609 | if (arenas[i] != NULL) |
| 1610 | malloc_mutex_unlock(&arenas[i]->lock); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1611 | } |
| Jason Evans | fbbb624 | 2010-01-24 17:56:48 -0800 | [diff] [blame] | 1612 | malloc_mutex_unlock(&arenas_lock); |
| Jason Evans | 289053c | 2009-06-22 12:08:42 -0700 | [diff] [blame] | 1613 | } |
| Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 1614 | |
| 1615 | /******************************************************************************/ |