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