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