| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1 | #define JEMALLOC_HUGE_C_ |
| Jason Evans | 376b152 | 2010-02-11 14:45:59 -0800 | [diff] [blame] | 2 | #include "jemalloc/internal/jemalloc_internal.h" |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3 | |
| 4 | /******************************************************************************/ |
| 5 | /* Data. */ |
| 6 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 7 | uint64_t huge_nmalloc; |
| 8 | uint64_t huge_ndalloc; |
| 9 | size_t huge_allocated; |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 10 | |
| 11 | malloc_mutex_t huge_mtx; |
| 12 | |
| 13 | /******************************************************************************/ |
| 14 | |
| 15 | /* Tree of chunks that are stand-alone huge allocations. */ |
| 16 | static extent_tree_t huge; |
| 17 | |
| 18 | void * |
| 19 | huge_malloc(size_t size, bool zero) |
| 20 | { |
| Mike Hommey | eae2690 | 2012-04-10 19:50:33 +0200 | [diff] [blame] | 21 | |
| 22 | return (huge_palloc(size, chunksize, zero)); |
| 23 | } |
| 24 | |
| 25 | void * |
| 26 | huge_palloc(size_t size, size_t alignment, bool zero) |
| 27 | { |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 28 | void *ret; |
| 29 | size_t csize; |
| 30 | extent_node_t *node; |
| Jason Evans | 7ad54c1 | 2012-04-21 16:04:51 -0700 | [diff] [blame] | 31 | bool is_zeroed; |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 32 | |
| 33 | /* Allocate one or more contiguous chunks for this request. */ |
| 34 | |
| 35 | csize = CHUNK_CEILING(size); |
| 36 | if (csize == 0) { |
| 37 | /* size is large enough to cause size_t wrap-around. */ |
| 38 | return (NULL); |
| 39 | } |
| 40 | |
| 41 | /* Allocate an extent node with which to track the chunk. */ |
| 42 | node = base_node_alloc(); |
| 43 | if (node == NULL) |
| 44 | return (NULL); |
| 45 | |
| Jason Evans | 7ad54c1 | 2012-04-21 16:04:51 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Copy zero into is_zeroed and pass the copy to chunk_alloc(), so that |
| 48 | * it is possible to make correct junk/zero fill decisions below. |
| 49 | */ |
| 50 | is_zeroed = zero; |
| Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 51 | ret = chunk_alloc(csize, alignment, false, &is_zeroed, |
| 52 | chunk_dss_prec_get()); |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 53 | if (ret == NULL) { |
| 54 | base_node_dealloc(node); |
| 55 | return (NULL); |
| 56 | } |
| 57 | |
| 58 | /* Insert node into huge. */ |
| 59 | node->addr = ret; |
| 60 | node->size = csize; |
| 61 | |
| 62 | malloc_mutex_lock(&huge_mtx); |
| 63 | extent_tree_ad_insert(&huge, node); |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 64 | if (config_stats) { |
| 65 | stats_cactive_add(csize); |
| 66 | huge_nmalloc++; |
| 67 | huge_allocated += csize; |
| 68 | } |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 69 | malloc_mutex_unlock(&huge_mtx); |
| 70 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 71 | if (config_fill && zero == false) { |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 72 | if (opt_junk) |
| 73 | memset(ret, 0xa5, csize); |
| Jason Evans | 7ad54c1 | 2012-04-21 16:04:51 -0700 | [diff] [blame] | 74 | else if (opt_zero && is_zeroed == false) |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 75 | memset(ret, 0, csize); |
| 76 | } |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 77 | |
| 78 | return (ret); |
| 79 | } |
| 80 | |
| Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 81 | bool |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 82 | huge_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra) |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 83 | { |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 84 | |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Avoid moving the allocation if the size class can be left the same. |
| 87 | */ |
| 88 | if (oldsize > arena_maxclass |
| 89 | && CHUNK_CEILING(oldsize) >= CHUNK_CEILING(size) |
| 90 | && CHUNK_CEILING(oldsize) <= CHUNK_CEILING(size+extra)) { |
| 91 | assert(CHUNK_CEILING(oldsize) == oldsize); |
| Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 92 | return (false); |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 95 | /* Reallocation would require a move. */ |
| Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 96 | return (true); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 97 | } |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 98 | |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 99 | void * |
| 100 | huge_ralloc(void *ptr, size_t oldsize, size_t size, size_t extra, |
| Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 101 | size_t alignment, bool zero, bool try_tcache_dalloc) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 102 | { |
| 103 | void *ret; |
| 104 | size_t copysize; |
| 105 | |
| 106 | /* Try to avoid moving the allocation. */ |
| Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 107 | if (huge_ralloc_no_move(ptr, oldsize, size, extra) == false) |
| 108 | return (ptr); |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * size and oldsize are different enough that we need to use a |
| 112 | * different size class. In that case, fall back to allocating new |
| 113 | * space and copying. |
| 114 | */ |
| Jason Evans | 31bfb3e | 2011-01-31 19:58:22 -0800 | [diff] [blame] | 115 | if (alignment > chunksize) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 116 | ret = huge_palloc(size + extra, alignment, zero); |
| 117 | else |
| 118 | ret = huge_malloc(size + extra, zero); |
| 119 | |
| 120 | if (ret == NULL) { |
| 121 | if (extra == 0) |
| 122 | return (NULL); |
| 123 | /* Try again, this time without extra. */ |
| Jason Evans | 31bfb3e | 2011-01-31 19:58:22 -0800 | [diff] [blame] | 124 | if (alignment > chunksize) |
| Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 125 | ret = huge_palloc(size, alignment, zero); |
| 126 | else |
| 127 | ret = huge_malloc(size, zero); |
| 128 | |
| 129 | if (ret == NULL) |
| 130 | return (NULL); |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Copy at most size bytes (not size+extra), since the caller has no |
| 135 | * expectation that the extra bytes will be reliably preserved. |
| 136 | */ |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 137 | copysize = (size < oldsize) ? size : oldsize; |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 138 | |
| Jason Evans | 2e671ff | 2012-05-09 16:12:00 -0700 | [diff] [blame] | 139 | #ifdef JEMALLOC_MREMAP |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 140 | /* |
| 141 | * Use mremap(2) if this is a huge-->huge reallocation, and neither the |
| Jason Evans | 4162627 | 2012-02-13 10:56:17 -0800 | [diff] [blame] | 142 | * source nor the destination are in dss. |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 143 | */ |
| Jason Evans | 4162627 | 2012-02-13 10:56:17 -0800 | [diff] [blame] | 144 | if (oldsize >= chunksize && (config_dss == false || (chunk_in_dss(ptr) |
| 145 | == false && chunk_in_dss(ret) == false))) { |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 146 | size_t newsize = huge_salloc(ret); |
| 147 | |
| Jason Evans | fa351d9 | 2011-11-09 11:55:19 -0800 | [diff] [blame] | 148 | /* |
| 149 | * Remove ptr from the tree of huge allocations before |
| 150 | * performing the remap operation, in order to avoid the |
| 151 | * possibility of another thread acquiring that mapping before |
| 152 | * this one removes it from the tree. |
| 153 | */ |
| 154 | huge_dalloc(ptr, false); |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 155 | if (mremap(ptr, oldsize, newsize, MREMAP_MAYMOVE|MREMAP_FIXED, |
| 156 | ret) == MAP_FAILED) { |
| 157 | /* |
| 158 | * Assuming no chunk management bugs in the allocator, |
| 159 | * the only documented way an error can occur here is |
| 160 | * if the application changed the map type for a |
| 161 | * portion of the old allocation. This is firmly in |
| 162 | * undefined behavior territory, so write a diagnostic |
| 163 | * message, and optionally abort. |
| 164 | */ |
| 165 | char buf[BUFERROR_BUF]; |
| 166 | |
| Jason Evans | 2a83ed0 | 2013-12-08 20:52:21 -0800 | [diff] [blame] | 167 | buferror(get_errno(), buf, sizeof(buf)); |
| Jason Evans | d81e4bd | 2012-03-06 14:57:45 -0800 | [diff] [blame] | 168 | malloc_printf("<jemalloc>: Error in mremap(): %s\n", |
| 169 | buf); |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 170 | if (opt_abort) |
| 171 | abort(); |
| 172 | memcpy(ret, ptr, copysize); |
| Jason Evans | 12a4887 | 2011-11-11 14:41:59 -0800 | [diff] [blame] | 173 | chunk_dealloc_mmap(ptr, oldsize); |
| Jason Evans | 940fdfd | 2014-02-25 11:58:50 -0800 | [diff] [blame] | 174 | } else if (config_fill && zero == false && opt_junk && oldsize |
| 175 | < newsize) { |
| 176 | /* |
| 177 | * mremap(2) clobbers the original mapping, so |
| 178 | * junk/zero filling is not preserved. There is no |
| 179 | * need to zero fill here, since any trailing |
| 180 | * uninititialized memory is demand-zeroed by the |
| 181 | * kernel, but junk filling must be redone. |
| 182 | */ |
| 183 | memset(ret + oldsize, 0xa5, newsize - oldsize); |
| Jason Evans | fa351d9 | 2011-11-09 11:55:19 -0800 | [diff] [blame] | 184 | } |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 185 | } else |
| 186 | #endif |
| 187 | { |
| 188 | memcpy(ret, ptr, copysize); |
| Jason Evans | d82a5e6 | 2013-12-12 22:35:52 -0800 | [diff] [blame] | 189 | iqalloct(ptr, try_tcache_dalloc); |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 190 | } |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 191 | return (ret); |
| 192 | } |
| 193 | |
| Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 194 | #ifdef JEMALLOC_JET |
| 195 | #undef huge_dalloc_junk |
| 196 | #define huge_dalloc_junk JEMALLOC_N(huge_dalloc_junk_impl) |
| 197 | #endif |
| 198 | static void |
| 199 | huge_dalloc_junk(void *ptr, size_t usize) |
| 200 | { |
| 201 | |
| 202 | if (config_fill && config_dss && opt_junk) { |
| 203 | /* |
| 204 | * Only bother junk filling if the chunk isn't about to be |
| 205 | * unmapped. |
| 206 | */ |
| 207 | if (config_munmap == false || (config_dss && chunk_in_dss(ptr))) |
| 208 | memset(ptr, 0x5a, usize); |
| 209 | } |
| 210 | } |
| 211 | #ifdef JEMALLOC_JET |
| 212 | #undef huge_dalloc_junk |
| 213 | #define huge_dalloc_junk JEMALLOC_N(huge_dalloc_junk) |
| 214 | huge_dalloc_junk_t *huge_dalloc_junk = JEMALLOC_N(huge_dalloc_junk_impl); |
| 215 | #endif |
| 216 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 217 | void |
| Jason Evans | cfdc8cf | 2010-11-30 16:50:58 -0800 | [diff] [blame] | 218 | huge_dalloc(void *ptr, bool unmap) |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 219 | { |
| 220 | extent_node_t *node, key; |
| 221 | |
| 222 | malloc_mutex_lock(&huge_mtx); |
| 223 | |
| 224 | /* Extract from tree of huge allocations. */ |
| 225 | key.addr = ptr; |
| 226 | node = extent_tree_ad_search(&huge, &key); |
| 227 | assert(node != NULL); |
| 228 | assert(node->addr == ptr); |
| 229 | extent_tree_ad_remove(&huge, node); |
| 230 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 231 | if (config_stats) { |
| 232 | stats_cactive_sub(node->size); |
| 233 | huge_ndalloc++; |
| 234 | huge_allocated -= node->size; |
| 235 | } |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 236 | |
| 237 | malloc_mutex_unlock(&huge_mtx); |
| 238 | |
| Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 239 | if (unmap) |
| 240 | huge_dalloc_junk(node->addr, node->size); |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 241 | |
| Jason Evans | 12a4887 | 2011-11-11 14:41:59 -0800 | [diff] [blame] | 242 | chunk_dealloc(node->addr, node->size, unmap); |
| 243 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 244 | base_node_dealloc(node); |
| 245 | } |
| 246 | |
| 247 | size_t |
| 248 | huge_salloc(const void *ptr) |
| 249 | { |
| 250 | size_t ret; |
| 251 | extent_node_t *node, key; |
| 252 | |
| 253 | malloc_mutex_lock(&huge_mtx); |
| 254 | |
| 255 | /* Extract from tree of huge allocations. */ |
| 256 | key.addr = __DECONST(void *, ptr); |
| 257 | node = extent_tree_ad_search(&huge, &key); |
| 258 | assert(node != NULL); |
| 259 | |
| 260 | ret = node->size; |
| 261 | |
| 262 | malloc_mutex_unlock(&huge_mtx); |
| 263 | |
| 264 | return (ret); |
| 265 | } |
| 266 | |
| Jason Evans | 5065156 | 2010-04-13 16:13:54 -0700 | [diff] [blame] | 267 | prof_ctx_t * |
| 268 | huge_prof_ctx_get(const void *ptr) |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 269 | { |
| Jason Evans | 5065156 | 2010-04-13 16:13:54 -0700 | [diff] [blame] | 270 | prof_ctx_t *ret; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 271 | extent_node_t *node, key; |
| 272 | |
| 273 | malloc_mutex_lock(&huge_mtx); |
| 274 | |
| 275 | /* Extract from tree of huge allocations. */ |
| 276 | key.addr = __DECONST(void *, ptr); |
| 277 | node = extent_tree_ad_search(&huge, &key); |
| 278 | assert(node != NULL); |
| 279 | |
| Jason Evans | 5065156 | 2010-04-13 16:13:54 -0700 | [diff] [blame] | 280 | ret = node->prof_ctx; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 281 | |
| 282 | malloc_mutex_unlock(&huge_mtx); |
| 283 | |
| 284 | return (ret); |
| 285 | } |
| 286 | |
| 287 | void |
| Jason Evans | 5065156 | 2010-04-13 16:13:54 -0700 | [diff] [blame] | 288 | huge_prof_ctx_set(const void *ptr, prof_ctx_t *ctx) |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 289 | { |
| 290 | extent_node_t *node, key; |
| 291 | |
| 292 | malloc_mutex_lock(&huge_mtx); |
| 293 | |
| 294 | /* Extract from tree of huge allocations. */ |
| 295 | key.addr = __DECONST(void *, ptr); |
| 296 | node = extent_tree_ad_search(&huge, &key); |
| 297 | assert(node != NULL); |
| 298 | |
| Jason Evans | 5065156 | 2010-04-13 16:13:54 -0700 | [diff] [blame] | 299 | node->prof_ctx = ctx; |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 300 | |
| 301 | malloc_mutex_unlock(&huge_mtx); |
| 302 | } |
| Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 303 | |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 304 | bool |
| 305 | huge_boot(void) |
| 306 | { |
| 307 | |
| 308 | /* Initialize chunks data. */ |
| 309 | if (malloc_mutex_init(&huge_mtx)) |
| 310 | return (true); |
| 311 | extent_tree_ad_new(&huge); |
| 312 | |
| Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 313 | if (config_stats) { |
| 314 | huge_nmalloc = 0; |
| 315 | huge_ndalloc = 0; |
| 316 | huge_allocated = 0; |
| 317 | } |
| Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 318 | |
| 319 | return (false); |
| 320 | } |
| Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 321 | |
| 322 | void |
| 323 | huge_prefork(void) |
| 324 | { |
| 325 | |
| 326 | malloc_mutex_prefork(&huge_mtx); |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | huge_postfork_parent(void) |
| 331 | { |
| 332 | |
| 333 | malloc_mutex_postfork_parent(&huge_mtx); |
| 334 | } |
| 335 | |
| 336 | void |
| 337 | huge_postfork_child(void) |
| 338 | { |
| 339 | |
| 340 | malloc_mutex_postfork_child(&huge_mtx); |
| 341 | } |