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