Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1 | #define JEMALLOC_ARENA_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 | ssize_t opt_lg_dirty_mult = LG_DIRTY_MULT_DEFAULT; |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 8 | static ssize_t lg_dirty_mult_default; |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 9 | arena_bin_info_t arena_bin_info[NBINS]; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 10 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 11 | size_t map_bias; |
| 12 | size_t map_misc_offset; |
| 13 | size_t arena_maxrun; /* Max run size for arenas. */ |
| 14 | size_t arena_maxclass; /* Max size class for arenas. */ |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 15 | static size_t small_maxrun; /* Max run size used for small size classes. */ |
| 16 | static bool *small_run_tab; /* Valid small run page multiples. */ |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 17 | unsigned nlclasses; /* Number of large size classes. */ |
| 18 | unsigned nhclasses; /* Number of huge size classes. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 19 | |
| 20 | /******************************************************************************/ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 21 | /* |
| 22 | * Function prototypes for static functions that are referenced prior to |
| 23 | * definition. |
| 24 | */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 25 | |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 26 | static void arena_purge(arena_t *arena, bool all); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 27 | static void arena_run_dalloc(arena_t *arena, arena_run_t *run, bool dirty, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 28 | bool cleaned, bool decommitted); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 29 | static void arena_dalloc_bin_run(arena_t *arena, arena_chunk_t *chunk, |
| 30 | arena_run_t *run, arena_bin_t *bin); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 31 | static void arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk, |
| 32 | arena_run_t *run, arena_bin_t *bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 33 | |
| 34 | /******************************************************************************/ |
| 35 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 36 | #define CHUNK_MAP_KEY ((uintptr_t)0x1U) |
Ben Maurer | f9ff603 | 2014-04-06 13:24:16 -0700 | [diff] [blame] | 37 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 38 | JEMALLOC_INLINE_C arena_chunk_map_misc_t * |
| 39 | arena_miscelm_key_create(size_t size) |
| 40 | { |
| 41 | |
| 42 | return ((arena_chunk_map_misc_t *)((size << CHUNK_MAP_SIZE_SHIFT) | |
| 43 | CHUNK_MAP_KEY)); |
| 44 | } |
| 45 | |
| 46 | JEMALLOC_INLINE_C bool |
| 47 | arena_miscelm_is_key(const arena_chunk_map_misc_t *miscelm) |
| 48 | { |
| 49 | |
| 50 | return (((uintptr_t)miscelm & CHUNK_MAP_KEY) != 0); |
| 51 | } |
| 52 | |
| 53 | #undef CHUNK_MAP_KEY |
| 54 | |
| 55 | JEMALLOC_INLINE_C size_t |
| 56 | arena_miscelm_key_size_get(const arena_chunk_map_misc_t *miscelm) |
| 57 | { |
| 58 | |
| 59 | assert(arena_miscelm_is_key(miscelm)); |
| 60 | |
| 61 | return (((uintptr_t)miscelm & CHUNK_MAP_SIZE_MASK) >> |
| 62 | CHUNK_MAP_SIZE_SHIFT); |
| 63 | } |
| 64 | |
| 65 | JEMALLOC_INLINE_C size_t |
| 66 | arena_miscelm_size_get(arena_chunk_map_misc_t *miscelm) |
| 67 | { |
| 68 | arena_chunk_t *chunk; |
| 69 | size_t pageind, mapbits; |
| 70 | |
| 71 | assert(!arena_miscelm_is_key(miscelm)); |
| 72 | |
| 73 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(miscelm); |
| 74 | pageind = arena_miscelm_to_pageind(miscelm); |
| 75 | mapbits = arena_mapbits_get(chunk, pageind); |
| 76 | return ((mapbits & CHUNK_MAP_SIZE_MASK) >> CHUNK_MAP_SIZE_SHIFT); |
Ben Maurer | f9ff603 | 2014-04-06 13:24:16 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 79 | JEMALLOC_INLINE_C int |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 80 | arena_run_comp(arena_chunk_map_misc_t *a, arena_chunk_map_misc_t *b) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 81 | { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 82 | uintptr_t a_miscelm = (uintptr_t)a; |
| 83 | uintptr_t b_miscelm = (uintptr_t)b; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 84 | |
| 85 | assert(a != NULL); |
| 86 | assert(b != NULL); |
| 87 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 88 | return ((a_miscelm > b_miscelm) - (a_miscelm < b_miscelm)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Jason Evans | f3ff752 | 2010-02-28 15:00:18 -0800 | [diff] [blame] | 91 | /* Generate red-black tree functions. */ |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 92 | rb_gen(static UNUSED, arena_run_tree_, arena_run_tree_t, arena_chunk_map_misc_t, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 93 | rb_link, arena_run_comp) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 94 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 95 | static size_t |
| 96 | run_quantize(size_t size) |
| 97 | { |
| 98 | size_t qsize; |
| 99 | |
| 100 | assert(size != 0); |
| 101 | assert(size == PAGE_CEILING(size)); |
| 102 | |
| 103 | /* Don't change sizes that are valid small run sizes. */ |
| 104 | if (size <= small_maxrun && small_run_tab[size >> LG_PAGE]) |
| 105 | return (size); |
| 106 | |
| 107 | /* |
| 108 | * Round down to the nearest run size that can actually be requested |
| 109 | * during normal large allocation. Add large_pad so that cache index |
| 110 | * randomization can offset the allocation from the page boundary. |
| 111 | */ |
| 112 | qsize = index2size(size2index(size - large_pad + 1) - 1) + large_pad; |
| 113 | if (qsize <= SMALL_MAXCLASS + large_pad) |
| 114 | return (run_quantize(size - large_pad)); |
| 115 | assert(qsize <= size); |
| 116 | return (qsize); |
| 117 | } |
| 118 | |
| 119 | static size_t |
| 120 | run_quantize_next(size_t size) |
| 121 | { |
| 122 | size_t large_run_size_next; |
| 123 | |
| 124 | assert(size != 0); |
| 125 | assert(size == PAGE_CEILING(size)); |
| 126 | |
| 127 | /* |
| 128 | * Return the next quantized size greater than the input size. |
| 129 | * Quantized sizes comprise the union of run sizes that back small |
| 130 | * region runs, and run sizes that back large regions with no explicit |
| 131 | * alignment constraints. |
| 132 | */ |
| 133 | |
| 134 | if (size > SMALL_MAXCLASS) { |
| 135 | large_run_size_next = PAGE_CEILING(index2size(size2index(size - |
| 136 | large_pad) + 1) + large_pad); |
| 137 | } else |
| 138 | large_run_size_next = SIZE_T_MAX; |
| 139 | if (size >= small_maxrun) |
| 140 | return (large_run_size_next); |
| 141 | |
| 142 | while (true) { |
| 143 | size += PAGE; |
| 144 | assert(size <= small_maxrun); |
| 145 | if (small_run_tab[size >> LG_PAGE]) { |
| 146 | if (large_run_size_next < size) |
| 147 | return (large_run_size_next); |
| 148 | return (size); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static size_t |
| 154 | run_quantize_first(size_t size) |
| 155 | { |
| 156 | size_t qsize = run_quantize(size); |
| 157 | |
| 158 | if (qsize < size) { |
| 159 | /* |
| 160 | * Skip a quantization that may have an adequately large run, |
| 161 | * because under-sized runs may be mixed in. This only happens |
| 162 | * when an unusual size is requested, i.e. for aligned |
| 163 | * allocation, and is just one of several places where linear |
| 164 | * search would potentially find sufficiently aligned available |
| 165 | * memory somewhere lower. |
| 166 | */ |
| 167 | qsize = run_quantize_next(size); |
| 168 | } |
| 169 | return (qsize); |
| 170 | } |
| 171 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 172 | JEMALLOC_INLINE_C int |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 173 | arena_avail_comp(arena_chunk_map_misc_t *a, arena_chunk_map_misc_t *b) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 174 | { |
| 175 | int ret; |
Jason Evans | 5707d6f | 2015-03-06 17:14:05 -0800 | [diff] [blame] | 176 | uintptr_t a_miscelm = (uintptr_t)a; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 177 | size_t a_qsize = run_quantize(arena_miscelm_is_key(a) ? |
| 178 | arena_miscelm_key_size_get(a) : arena_miscelm_size_get(a)); |
| 179 | size_t b_qsize = run_quantize(arena_miscelm_size_get(b)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 180 | |
Jason Evans | 5707d6f | 2015-03-06 17:14:05 -0800 | [diff] [blame] | 181 | /* |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 182 | * Compare based on quantized size rather than size, in order to sort |
| 183 | * equally useful runs only by address. |
Jason Evans | 5707d6f | 2015-03-06 17:14:05 -0800 | [diff] [blame] | 184 | */ |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 185 | ret = (a_qsize > b_qsize) - (a_qsize < b_qsize); |
Qinfan Wu | 55c9aa1 | 2014-08-06 16:10:08 -0700 | [diff] [blame] | 186 | if (ret == 0) { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 187 | if (!arena_miscelm_is_key(a)) { |
Jason Evans | 5707d6f | 2015-03-06 17:14:05 -0800 | [diff] [blame] | 188 | uintptr_t b_miscelm = (uintptr_t)b; |
| 189 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 190 | ret = (a_miscelm > b_miscelm) - (a_miscelm < b_miscelm); |
Jason Evans | 5707d6f | 2015-03-06 17:14:05 -0800 | [diff] [blame] | 191 | } else { |
Qinfan Wu | ea73eb8 | 2014-08-06 16:43:01 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Treat keys as if they are lower than anything else. |
| 194 | */ |
Qinfan Wu | 55c9aa1 | 2014-08-06 16:10:08 -0700 | [diff] [blame] | 195 | ret = -1; |
Qinfan Wu | ea73eb8 | 2014-08-06 16:43:01 -0700 | [diff] [blame] | 196 | } |
Qinfan Wu | 55c9aa1 | 2014-08-06 16:10:08 -0700 | [diff] [blame] | 197 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 198 | |
| 199 | return (ret); |
| 200 | } |
| 201 | |
Jason Evans | f3ff752 | 2010-02-28 15:00:18 -0800 | [diff] [blame] | 202 | /* Generate red-black tree functions. */ |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 203 | rb_gen(static UNUSED, arena_avail_tree_, arena_avail_tree_t, |
| 204 | arena_chunk_map_misc_t, rb_link, arena_avail_comp) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 205 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 206 | static void |
| 207 | arena_avail_insert(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 208 | size_t npages) |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 209 | { |
| 210 | |
| 211 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 212 | LG_PAGE)); |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 213 | arena_avail_tree_insert(&arena->runs_avail, arena_miscelm_get(chunk, |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 214 | pageind)); |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | arena_avail_remove(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 219 | size_t npages) |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 220 | { |
| 221 | |
| 222 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 223 | LG_PAGE)); |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 224 | arena_avail_tree_remove(&arena->runs_avail, arena_miscelm_get(chunk, |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 225 | pageind)); |
| 226 | } |
| 227 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 228 | static void |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 229 | arena_run_dirty_insert(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 230 | size_t npages) |
| 231 | { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 232 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get(chunk, pageind); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 233 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 234 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 235 | LG_PAGE)); |
| 236 | assert(arena_mapbits_dirty_get(chunk, pageind) == CHUNK_MAP_DIRTY); |
| 237 | assert(arena_mapbits_dirty_get(chunk, pageind+npages-1) == |
| 238 | CHUNK_MAP_DIRTY); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 239 | |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 240 | qr_new(&miscelm->rd, rd_link); |
| 241 | qr_meld(&arena->runs_dirty, &miscelm->rd, rd_link); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 242 | arena->ndirty += npages; |
| 243 | } |
| 244 | |
| 245 | static void |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 246 | arena_run_dirty_remove(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 247 | size_t npages) |
| 248 | { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 249 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get(chunk, pageind); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 250 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 251 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 252 | LG_PAGE)); |
| 253 | assert(arena_mapbits_dirty_get(chunk, pageind) == CHUNK_MAP_DIRTY); |
| 254 | assert(arena_mapbits_dirty_get(chunk, pageind+npages-1) == |
| 255 | CHUNK_MAP_DIRTY); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 256 | |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 257 | qr_remove(&miscelm->rd, rd_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 258 | assert(arena->ndirty >= npages); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 259 | arena->ndirty -= npages; |
| 260 | } |
| 261 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 262 | static size_t |
| 263 | arena_chunk_dirty_npages(const extent_node_t *node) |
| 264 | { |
| 265 | |
| 266 | return (extent_node_size_get(node) >> LG_PAGE); |
| 267 | } |
| 268 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 269 | void |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 270 | arena_chunk_cache_maybe_insert(arena_t *arena, extent_node_t *node, bool cache) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 271 | { |
| 272 | |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 273 | if (cache) { |
Jason Evans | 47701b2 | 2015-02-17 22:23:10 -0800 | [diff] [blame] | 274 | extent_node_dirty_linkage_init(node); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 275 | extent_node_dirty_insert(node, &arena->runs_dirty, |
| 276 | &arena->chunks_cache); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 277 | arena->ndirty += arena_chunk_dirty_npages(node); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 282 | arena_chunk_cache_maybe_remove(arena_t *arena, extent_node_t *node, bool dirty) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 283 | { |
| 284 | |
| 285 | if (dirty) { |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 286 | extent_node_dirty_remove(node); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 287 | assert(arena->ndirty >= arena_chunk_dirty_npages(node)); |
| 288 | arena->ndirty -= arena_chunk_dirty_npages(node); |
| 289 | } |
| 290 | } |
| 291 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 292 | JEMALLOC_INLINE_C void * |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 293 | arena_run_reg_alloc(arena_run_t *run, arena_bin_info_t *bin_info) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 294 | { |
| 295 | void *ret; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 296 | unsigned regind; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 297 | arena_chunk_map_misc_t *miscelm; |
| 298 | void *rpages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 299 | |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 300 | assert(run->nfree > 0); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 301 | assert(!bitmap_full(run->bitmap, &bin_info->bitmap_info)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 302 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 303 | regind = bitmap_sfu(run->bitmap, &bin_info->bitmap_info); |
| 304 | miscelm = arena_run_to_miscelm(run); |
| 305 | rpages = arena_miscelm_to_rpages(miscelm); |
| 306 | ret = (void *)((uintptr_t)rpages + (uintptr_t)bin_info->reg0_offset + |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 307 | (uintptr_t)(bin_info->reg_interval * regind)); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 308 | run->nfree--; |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 309 | return (ret); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 312 | JEMALLOC_INLINE_C void |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 313 | arena_run_reg_dalloc(arena_run_t *run, void *ptr) |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 314 | { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 315 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 316 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
| 317 | size_t mapbits = arena_mapbits_get(chunk, pageind); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 318 | index_t binind = arena_ptr_small_binind_get(ptr, mapbits); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 319 | arena_bin_info_t *bin_info = &arena_bin_info[binind]; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 320 | unsigned regind = arena_run_regind(run, bin_info, ptr); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 321 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 322 | assert(run->nfree < bin_info->nregs); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 323 | /* Freeing an interior pointer can cause assertion failure. */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 324 | assert(((uintptr_t)ptr - |
| 325 | ((uintptr_t)arena_miscelm_to_rpages(arena_run_to_miscelm(run)) + |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 326 | (uintptr_t)bin_info->reg0_offset)) % |
| 327 | (uintptr_t)bin_info->reg_interval == 0); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 328 | assert((uintptr_t)ptr >= |
| 329 | (uintptr_t)arena_miscelm_to_rpages(arena_run_to_miscelm(run)) + |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 330 | (uintptr_t)bin_info->reg0_offset); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 331 | /* Freeing an unallocated pointer can cause assertion failure. */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 332 | assert(bitmap_get(run->bitmap, &bin_info->bitmap_info, regind)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 333 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 334 | bitmap_unset(run->bitmap, &bin_info->bitmap_info, regind); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 335 | run->nfree++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 338 | JEMALLOC_INLINE_C void |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 339 | arena_run_zero(arena_chunk_t *chunk, size_t run_ind, size_t npages) |
| 340 | { |
| 341 | |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 342 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk + |
| 343 | (run_ind << LG_PAGE)), (npages << LG_PAGE)); |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 344 | memset((void *)((uintptr_t)chunk + (run_ind << LG_PAGE)), 0, |
| 345 | (npages << LG_PAGE)); |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 348 | JEMALLOC_INLINE_C void |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 349 | arena_run_page_mark_zeroed(arena_chunk_t *chunk, size_t run_ind) |
| 350 | { |
| 351 | |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 352 | JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void *)((uintptr_t)chunk + (run_ind |
| 353 | << LG_PAGE)), PAGE); |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 356 | JEMALLOC_INLINE_C void |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 357 | arena_run_page_validate_zeroed(arena_chunk_t *chunk, size_t run_ind) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 358 | { |
Jason Evans | d4bab21 | 2010-10-24 20:08:37 -0700 | [diff] [blame] | 359 | size_t i; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 360 | UNUSED size_t *p = (size_t *)((uintptr_t)chunk + (run_ind << LG_PAGE)); |
Jason Evans | d4bab21 | 2010-10-24 20:08:37 -0700 | [diff] [blame] | 361 | |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 362 | arena_run_page_mark_zeroed(chunk, run_ind); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 363 | for (i = 0; i < PAGE / sizeof(size_t); i++) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 364 | assert(p[i] == 0); |
| 365 | } |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 366 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 367 | static void |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 368 | arena_cactive_update(arena_t *arena, size_t add_pages, size_t sub_pages) |
| 369 | { |
| 370 | |
| 371 | if (config_stats) { |
Jason Evans | 1522937 | 2014-08-06 23:38:39 -0700 | [diff] [blame] | 372 | ssize_t cactive_diff = CHUNK_CEILING((arena->nactive + add_pages |
| 373 | - sub_pages) << LG_PAGE) - CHUNK_CEILING(arena->nactive << |
| 374 | LG_PAGE); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 375 | if (cactive_diff != 0) |
| 376 | stats_cactive_add(cactive_diff); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | static void |
| 381 | arena_run_split_remove(arena_t *arena, arena_chunk_t *chunk, size_t run_ind, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 382 | size_t flag_dirty, size_t flag_decommitted, size_t need_pages) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 383 | { |
| 384 | size_t total_pages, rem_pages; |
| 385 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 386 | assert(flag_dirty == 0 || flag_decommitted == 0); |
| 387 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 388 | total_pages = arena_mapbits_unallocated_size_get(chunk, run_ind) >> |
| 389 | LG_PAGE; |
| 390 | assert(arena_mapbits_dirty_get(chunk, run_ind+total_pages-1) == |
| 391 | flag_dirty); |
| 392 | assert(need_pages <= total_pages); |
| 393 | rem_pages = total_pages - need_pages; |
| 394 | |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 395 | arena_avail_remove(arena, chunk, run_ind, total_pages); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 396 | if (flag_dirty != 0) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 397 | arena_run_dirty_remove(arena, chunk, run_ind, total_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 398 | arena_cactive_update(arena, need_pages, 0); |
| 399 | arena->nactive += need_pages; |
| 400 | |
| 401 | /* Keep track of trailing unused pages for later use. */ |
| 402 | if (rem_pages > 0) { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 403 | size_t flags = flag_dirty | flag_decommitted; |
| 404 | |
| 405 | if (flags != 0) { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 406 | arena_mapbits_unallocated_set(chunk, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 407 | run_ind+need_pages, (rem_pages << LG_PAGE), flags); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 408 | arena_mapbits_unallocated_set(chunk, |
| 409 | run_ind+total_pages-1, (rem_pages << LG_PAGE), |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 410 | flags); |
| 411 | if (flag_dirty != 0) { |
| 412 | arena_run_dirty_insert(arena, chunk, |
| 413 | run_ind+need_pages, rem_pages); |
| 414 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 415 | } else { |
| 416 | arena_mapbits_unallocated_set(chunk, run_ind+need_pages, |
| 417 | (rem_pages << LG_PAGE), |
| 418 | arena_mapbits_unzeroed_get(chunk, |
| 419 | run_ind+need_pages)); |
| 420 | arena_mapbits_unallocated_set(chunk, |
| 421 | run_ind+total_pages-1, (rem_pages << LG_PAGE), |
| 422 | arena_mapbits_unzeroed_get(chunk, |
| 423 | run_ind+total_pages-1)); |
| 424 | } |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 425 | arena_avail_insert(arena, chunk, run_ind+need_pages, rem_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 429 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 430 | arena_run_split_large_helper(arena_t *arena, arena_run_t *run, size_t size, |
| 431 | bool remove, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 432 | { |
| 433 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 434 | arena_chunk_map_misc_t *miscelm; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 435 | size_t flag_dirty, flag_decommitted, run_ind, need_pages, i; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 436 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 437 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 438 | miscelm = arena_run_to_miscelm(run); |
| 439 | run_ind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 440 | flag_dirty = arena_mapbits_dirty_get(chunk, run_ind); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 441 | flag_decommitted = arena_mapbits_decommitted_get(chunk, run_ind); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 442 | need_pages = (size >> LG_PAGE); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 443 | assert(need_pages > 0); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 444 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 445 | if (flag_decommitted != 0 && arena->chunk_hooks.commit(chunk, chunksize, |
| 446 | run_ind << LG_PAGE, size, arena->ind)) |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 447 | return (true); |
| 448 | |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 449 | if (remove) { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 450 | arena_run_split_remove(arena, chunk, run_ind, flag_dirty, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 451 | flag_decommitted, need_pages); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 454 | if (zero) { |
| 455 | if (flag_dirty == 0) { |
| 456 | /* |
| 457 | * The run is clean, so some pages may be zeroed (i.e. |
| 458 | * never before touched). |
| 459 | */ |
| 460 | for (i = 0; i < need_pages; i++) { |
| 461 | if (arena_mapbits_unzeroed_get(chunk, run_ind+i) |
| 462 | != 0) |
| 463 | arena_run_zero(chunk, run_ind+i, 1); |
| 464 | else if (config_debug) { |
| 465 | arena_run_page_validate_zeroed(chunk, |
| 466 | run_ind+i); |
| 467 | } else { |
| 468 | arena_run_page_mark_zeroed(chunk, |
| 469 | run_ind+i); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 470 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 471 | } |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 472 | } else { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 473 | /* The run is dirty, so all pages must be zeroed. */ |
| 474 | arena_run_zero(chunk, run_ind, need_pages); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 475 | } |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 476 | } else { |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 477 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk + |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 478 | (run_ind << LG_PAGE)), (need_pages << LG_PAGE)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 479 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Set the last element first, in case the run only contains one page |
| 483 | * (i.e. both statements set the same element). |
| 484 | */ |
| 485 | arena_mapbits_large_set(chunk, run_ind+need_pages-1, 0, flag_dirty); |
| 486 | arena_mapbits_large_set(chunk, run_ind, size, flag_dirty); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 487 | return (false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 490 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 491 | arena_run_split_large(arena_t *arena, arena_run_t *run, size_t size, bool zero) |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 492 | { |
| 493 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 494 | return (arena_run_split_large_helper(arena, run, size, true, zero)); |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 497 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 498 | arena_run_init_large(arena_t *arena, arena_run_t *run, size_t size, bool zero) |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 499 | { |
| 500 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 501 | return (arena_run_split_large_helper(arena, run, size, false, zero)); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 504 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 505 | arena_run_split_small(arena_t *arena, arena_run_t *run, size_t size, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 506 | index_t binind) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 507 | { |
| 508 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 509 | arena_chunk_map_misc_t *miscelm; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 510 | size_t flag_dirty, flag_decommitted, run_ind, need_pages, i; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 511 | |
| 512 | assert(binind != BININD_INVALID); |
| 513 | |
| 514 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 515 | miscelm = arena_run_to_miscelm(run); |
| 516 | run_ind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 517 | flag_dirty = arena_mapbits_dirty_get(chunk, run_ind); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 518 | flag_decommitted = arena_mapbits_decommitted_get(chunk, run_ind); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 519 | need_pages = (size >> LG_PAGE); |
| 520 | assert(need_pages > 0); |
| 521 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 522 | if (flag_decommitted != 0 && arena->chunk_hooks.commit(chunk, chunksize, |
| 523 | run_ind << LG_PAGE, size, arena->ind)) |
| 524 | return (true); |
| 525 | |
| 526 | arena_run_split_remove(arena, chunk, run_ind, flag_dirty, |
| 527 | flag_decommitted, need_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 528 | |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 529 | for (i = 0; i < need_pages; i++) { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 530 | arena_mapbits_small_set(chunk, run_ind+i, i, binind, 0); |
| 531 | if (config_debug && flag_dirty == 0 && |
| 532 | arena_mapbits_unzeroed_get(chunk, run_ind+i) == 0) |
| 533 | arena_run_page_validate_zeroed(chunk, run_ind+i); |
| 534 | } |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 535 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk + |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 536 | (run_ind << LG_PAGE)), (need_pages << LG_PAGE)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 537 | return (false); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | static arena_chunk_t * |
| 541 | arena_chunk_init_spare(arena_t *arena) |
| 542 | { |
| 543 | arena_chunk_t *chunk; |
| 544 | |
| 545 | assert(arena->spare != NULL); |
| 546 | |
| 547 | chunk = arena->spare; |
| 548 | arena->spare = NULL; |
| 549 | |
| 550 | assert(arena_mapbits_allocated_get(chunk, map_bias) == 0); |
| 551 | assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0); |
| 552 | assert(arena_mapbits_unallocated_size_get(chunk, map_bias) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 553 | arena_maxrun); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 554 | assert(arena_mapbits_unallocated_size_get(chunk, chunk_npages-1) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 555 | arena_maxrun); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 556 | assert(arena_mapbits_dirty_get(chunk, map_bias) == |
| 557 | arena_mapbits_dirty_get(chunk, chunk_npages-1)); |
| 558 | |
| 559 | return (chunk); |
| 560 | } |
| 561 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 562 | static bool |
| 563 | arena_chunk_register(arena_t *arena, arena_chunk_t *chunk, bool zero) |
| 564 | { |
| 565 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 566 | /* |
| 567 | * The extent node notion of "committed" doesn't directly apply to |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 568 | * arena chunks. Arbitrarily mark them as committed. The commit state |
| 569 | * of runs is tracked individually, and upon chunk deallocation the |
| 570 | * entire chunk is in a consistent commit state. |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 571 | */ |
| 572 | extent_node_init(&chunk->node, arena, chunk, chunksize, zero, true); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 573 | extent_node_achunk_set(&chunk->node, true); |
| 574 | return (chunk_register(chunk, &chunk->node)); |
| 575 | } |
| 576 | |
| 577 | static arena_chunk_t * |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 578 | arena_chunk_alloc_internal_hard(arena_t *arena, chunk_hooks_t *chunk_hooks, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 579 | bool *zero, bool *commit) |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 580 | { |
| 581 | arena_chunk_t *chunk; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 582 | |
| 583 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 584 | |
| 585 | chunk = (arena_chunk_t *)chunk_alloc_wrapper(arena, chunk_hooks, NULL, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 586 | chunksize, chunksize, zero, commit); |
| 587 | if (chunk != NULL && !*commit) { |
| 588 | /* Commit header. */ |
| 589 | if (chunk_hooks->commit(chunk, chunksize, 0, map_bias << |
| 590 | LG_PAGE, arena->ind)) { |
| 591 | chunk_dalloc_wrapper(arena, chunk_hooks, |
| 592 | (void *)chunk, chunksize, *commit); |
| 593 | chunk = NULL; |
| 594 | } |
| 595 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 596 | if (chunk != NULL && arena_chunk_register(arena, chunk, *zero)) { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 597 | if (!*commit) { |
| 598 | /* Undo commit of header. */ |
| 599 | chunk_hooks->decommit(chunk, chunksize, 0, map_bias << |
| 600 | LG_PAGE, arena->ind); |
| 601 | } |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 602 | chunk_dalloc_wrapper(arena, chunk_hooks, (void *)chunk, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 603 | chunksize, *commit); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 604 | chunk = NULL; |
| 605 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 606 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 607 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 608 | return (chunk); |
| 609 | } |
| 610 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 611 | static arena_chunk_t * |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 612 | arena_chunk_alloc_internal(arena_t *arena, bool *zero, bool *commit) |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 613 | { |
| 614 | arena_chunk_t *chunk; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 615 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 616 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 617 | chunk = chunk_alloc_cache(arena, &chunk_hooks, NULL, chunksize, |
| 618 | chunksize, zero, true); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 619 | if (chunk != NULL) { |
| 620 | if (arena_chunk_register(arena, chunk, *zero)) { |
| 621 | chunk_dalloc_cache(arena, &chunk_hooks, chunk, |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 622 | chunksize, true); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 623 | return (NULL); |
| 624 | } |
| 625 | *commit = true; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 626 | } |
| 627 | if (chunk == NULL) { |
| 628 | chunk = arena_chunk_alloc_internal_hard(arena, &chunk_hooks, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 629 | zero, commit); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 630 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 631 | |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 632 | if (config_stats && chunk != NULL) { |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 633 | arena->stats.mapped += chunksize; |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 634 | arena->stats.metadata_mapped += (map_bias << LG_PAGE); |
| 635 | } |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 636 | |
| 637 | return (chunk); |
| 638 | } |
| 639 | |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 640 | static arena_chunk_t * |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 641 | arena_chunk_init_hard(arena_t *arena) |
| 642 | { |
| 643 | arena_chunk_t *chunk; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 644 | bool zero, commit; |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 645 | size_t flag_unzeroed, flag_decommitted, i; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 646 | |
| 647 | assert(arena->spare == NULL); |
| 648 | |
| 649 | zero = false; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 650 | commit = false; |
| 651 | chunk = arena_chunk_alloc_internal(arena, &zero, &commit); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 652 | if (chunk == NULL) |
| 653 | return (NULL); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 654 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 655 | /* |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 656 | * Initialize the map to contain one maximal free untouched run. Mark |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 657 | * the pages as zeroed if chunk_alloc() returned a zeroed or decommitted |
| 658 | * chunk. |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 659 | */ |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 660 | flag_unzeroed = (zero || !commit) ? 0 : CHUNK_MAP_UNZEROED; |
| 661 | flag_decommitted = commit ? 0 : CHUNK_MAP_DECOMMITTED; |
| 662 | arena_mapbits_unallocated_set(chunk, map_bias, arena_maxrun, |
| 663 | flag_unzeroed | flag_decommitted); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 664 | /* |
| 665 | * There is no need to initialize the internal page map entries unless |
| 666 | * the chunk is not zeroed. |
| 667 | */ |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 668 | if (!zero) { |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 669 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED( |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 670 | (void *)arena_bitselm_get(chunk, map_bias+1), |
| 671 | (size_t)((uintptr_t) arena_bitselm_get(chunk, |
| 672 | chunk_npages-1) - (uintptr_t)arena_bitselm_get(chunk, |
| 673 | map_bias+1))); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 674 | for (i = map_bias+1; i < chunk_npages-1; i++) |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 675 | arena_mapbits_internal_set(chunk, i, flag_unzeroed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 676 | } else { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 677 | JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void |
| 678 | *)arena_bitselm_get(chunk, map_bias+1), (size_t)((uintptr_t) |
| 679 | arena_bitselm_get(chunk, chunk_npages-1) - |
| 680 | (uintptr_t)arena_bitselm_get(chunk, map_bias+1))); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 681 | if (config_debug) { |
| 682 | for (i = map_bias+1; i < chunk_npages-1; i++) { |
| 683 | assert(arena_mapbits_unzeroed_get(chunk, i) == |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 684 | flag_unzeroed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | } |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 688 | arena_mapbits_unallocated_set(chunk, chunk_npages-1, arena_maxrun, |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 689 | flag_unzeroed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 690 | |
| 691 | return (chunk); |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 694 | static arena_chunk_t * |
| 695 | arena_chunk_alloc(arena_t *arena) |
| 696 | { |
| 697 | arena_chunk_t *chunk; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 698 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 699 | if (arena->spare != NULL) |
| 700 | chunk = arena_chunk_init_spare(arena); |
Chris Pride | 20a8c78 | 2014-03-25 22:36:05 -0700 | [diff] [blame] | 701 | else { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 702 | chunk = arena_chunk_init_hard(arena); |
Chris Pride | 20a8c78 | 2014-03-25 22:36:05 -0700 | [diff] [blame] | 703 | if (chunk == NULL) |
| 704 | return (NULL); |
| 705 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 706 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 707 | /* Insert the run into the runs_avail tree. */ |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 708 | arena_avail_insert(arena, chunk, map_bias, chunk_npages-map_bias); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 709 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 710 | return (chunk); |
| 711 | } |
| 712 | |
| 713 | static void |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 714 | arena_chunk_dalloc(arena_t *arena, arena_chunk_t *chunk) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 715 | { |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 716 | |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 717 | assert(arena_mapbits_allocated_get(chunk, map_bias) == 0); |
| 718 | assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0); |
| 719 | assert(arena_mapbits_unallocated_size_get(chunk, map_bias) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 720 | arena_maxrun); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 721 | assert(arena_mapbits_unallocated_size_get(chunk, chunk_npages-1) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 722 | arena_maxrun); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 723 | assert(arena_mapbits_dirty_get(chunk, map_bias) == |
| 724 | arena_mapbits_dirty_get(chunk, chunk_npages-1)); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 725 | assert(arena_mapbits_decommitted_get(chunk, map_bias) == |
| 726 | arena_mapbits_decommitted_get(chunk, chunk_npages-1)); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 727 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 728 | /* |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 729 | * Remove run from the runs_avail tree, so that the arena does not use |
| 730 | * it. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 731 | */ |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 732 | arena_avail_remove(arena, chunk, map_bias, chunk_npages-map_bias); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 733 | |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 734 | if (arena->spare != NULL) { |
| 735 | arena_chunk_t *spare = arena->spare; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 736 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 737 | bool committed; |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 738 | |
| 739 | arena->spare = chunk; |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 740 | if (arena_mapbits_dirty_get(spare, map_bias) != 0) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 741 | arena_run_dirty_remove(arena, spare, map_bias, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 742 | chunk_npages-map_bias); |
| 743 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 744 | |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 745 | chunk_deregister(spare, &spare->node); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 746 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 747 | committed = (arena_mapbits_decommitted_get(spare, map_bias) == |
| 748 | 0); |
| 749 | if (!committed) { |
| 750 | /* |
| 751 | * Decommit the header. Mark the chunk as decommitted |
| 752 | * even if header decommit fails, since treating a |
| 753 | * partially committed chunk as committed has a high |
| 754 | * potential for causing later access of decommitted |
| 755 | * memory. |
| 756 | */ |
| 757 | chunk_hooks = chunk_hooks_get(arena); |
| 758 | chunk_hooks.decommit(spare, chunksize, 0, map_bias << |
| 759 | LG_PAGE, arena->ind); |
| 760 | } |
| 761 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 762 | chunk_dalloc_cache(arena, &chunk_hooks, (void *)spare, |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 763 | chunksize, committed); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 764 | |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 765 | if (config_stats) { |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 766 | arena->stats.mapped -= chunksize; |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 767 | arena->stats.metadata_mapped -= (map_bias << LG_PAGE); |
| 768 | } |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 769 | } else |
| 770 | arena->spare = chunk; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 773 | static void |
| 774 | arena_huge_malloc_stats_update(arena_t *arena, size_t usize) |
| 775 | { |
| 776 | index_t index = size2index(usize) - nlclasses - NBINS; |
| 777 | |
| 778 | cassert(config_stats); |
| 779 | |
| 780 | arena->stats.nmalloc_huge++; |
| 781 | arena->stats.allocated_huge += usize; |
| 782 | arena->stats.hstats[index].nmalloc++; |
| 783 | arena->stats.hstats[index].curhchunks++; |
| 784 | } |
| 785 | |
| 786 | static void |
| 787 | arena_huge_malloc_stats_update_undo(arena_t *arena, size_t usize) |
| 788 | { |
| 789 | index_t index = size2index(usize) - nlclasses - NBINS; |
| 790 | |
| 791 | cassert(config_stats); |
| 792 | |
| 793 | arena->stats.nmalloc_huge--; |
| 794 | arena->stats.allocated_huge -= usize; |
| 795 | arena->stats.hstats[index].nmalloc--; |
| 796 | arena->stats.hstats[index].curhchunks--; |
| 797 | } |
| 798 | |
| 799 | static void |
| 800 | arena_huge_dalloc_stats_update(arena_t *arena, size_t usize) |
| 801 | { |
| 802 | index_t index = size2index(usize) - nlclasses - NBINS; |
| 803 | |
| 804 | cassert(config_stats); |
| 805 | |
| 806 | arena->stats.ndalloc_huge++; |
| 807 | arena->stats.allocated_huge -= usize; |
| 808 | arena->stats.hstats[index].ndalloc++; |
| 809 | arena->stats.hstats[index].curhchunks--; |
| 810 | } |
| 811 | |
| 812 | static void |
| 813 | arena_huge_dalloc_stats_update_undo(arena_t *arena, size_t usize) |
| 814 | { |
| 815 | index_t index = size2index(usize) - nlclasses - NBINS; |
| 816 | |
| 817 | cassert(config_stats); |
| 818 | |
| 819 | arena->stats.ndalloc_huge--; |
| 820 | arena->stats.allocated_huge += usize; |
| 821 | arena->stats.hstats[index].ndalloc--; |
| 822 | arena->stats.hstats[index].curhchunks++; |
| 823 | } |
| 824 | |
| 825 | static void |
| 826 | arena_huge_ralloc_stats_update(arena_t *arena, size_t oldsize, size_t usize) |
| 827 | { |
| 828 | |
| 829 | arena_huge_dalloc_stats_update(arena, oldsize); |
| 830 | arena_huge_malloc_stats_update(arena, usize); |
| 831 | } |
| 832 | |
| 833 | static void |
| 834 | arena_huge_ralloc_stats_update_undo(arena_t *arena, size_t oldsize, |
| 835 | size_t usize) |
| 836 | { |
| 837 | |
| 838 | arena_huge_dalloc_stats_update_undo(arena, oldsize); |
| 839 | arena_huge_malloc_stats_update_undo(arena, usize); |
| 840 | } |
| 841 | |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 842 | extent_node_t * |
| 843 | arena_node_alloc(arena_t *arena) |
| 844 | { |
| 845 | extent_node_t *node; |
| 846 | |
| 847 | malloc_mutex_lock(&arena->node_cache_mtx); |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 848 | node = ql_last(&arena->node_cache, ql_link); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 849 | if (node == NULL) { |
| 850 | malloc_mutex_unlock(&arena->node_cache_mtx); |
| 851 | return (base_alloc(sizeof(extent_node_t))); |
| 852 | } |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 853 | ql_tail_remove(&arena->node_cache, extent_node_t, ql_link); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 854 | malloc_mutex_unlock(&arena->node_cache_mtx); |
| 855 | return (node); |
| 856 | } |
| 857 | |
| 858 | void |
| 859 | arena_node_dalloc(arena_t *arena, extent_node_t *node) |
| 860 | { |
| 861 | |
| 862 | malloc_mutex_lock(&arena->node_cache_mtx); |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 863 | ql_elm_new(node, ql_link); |
| 864 | ql_tail_insert(&arena->node_cache, node, ql_link); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 865 | malloc_mutex_unlock(&arena->node_cache_mtx); |
| 866 | } |
| 867 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 868 | static void * |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 869 | arena_chunk_alloc_huge_hard(arena_t *arena, chunk_hooks_t *chunk_hooks, |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 870 | size_t usize, size_t alignment, bool *zero, size_t csize) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 871 | { |
| 872 | void *ret; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 873 | bool commit = true; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 874 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 875 | ret = chunk_alloc_wrapper(arena, chunk_hooks, NULL, csize, alignment, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 876 | zero, &commit); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 877 | if (ret == NULL) { |
| 878 | /* Revert optimistic stats updates. */ |
| 879 | malloc_mutex_lock(&arena->lock); |
| 880 | if (config_stats) { |
| 881 | arena_huge_malloc_stats_update_undo(arena, usize); |
| 882 | arena->stats.mapped -= usize; |
| 883 | } |
| 884 | arena->nactive -= (usize >> LG_PAGE); |
| 885 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 888 | return (ret); |
| 889 | } |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 890 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 891 | void * |
| 892 | arena_chunk_alloc_huge(arena_t *arena, size_t usize, size_t alignment, |
| 893 | bool *zero) |
| 894 | { |
| 895 | void *ret; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 896 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 897 | size_t csize = CHUNK_CEILING(usize); |
| 898 | |
| 899 | malloc_mutex_lock(&arena->lock); |
| 900 | |
| 901 | /* Optimistically update stats. */ |
| 902 | if (config_stats) { |
| 903 | arena_huge_malloc_stats_update(arena, usize); |
| 904 | arena->stats.mapped += usize; |
| 905 | } |
| 906 | arena->nactive += (usize >> LG_PAGE); |
| 907 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 908 | ret = chunk_alloc_cache(arena, &chunk_hooks, NULL, csize, alignment, |
| 909 | zero, true); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 910 | malloc_mutex_unlock(&arena->lock); |
| 911 | if (ret == NULL) { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 912 | ret = arena_chunk_alloc_huge_hard(arena, &chunk_hooks, usize, |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 913 | alignment, zero, csize); |
| 914 | } |
| 915 | |
| 916 | if (config_stats && ret != NULL) |
| 917 | stats_cactive_add(usize); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 918 | return (ret); |
| 919 | } |
| 920 | |
| 921 | void |
| 922 | arena_chunk_dalloc_huge(arena_t *arena, void *chunk, size_t usize) |
| 923 | { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 924 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 925 | size_t csize; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 926 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 927 | csize = CHUNK_CEILING(usize); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 928 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 929 | if (config_stats) { |
| 930 | arena_huge_dalloc_stats_update(arena, usize); |
| 931 | arena->stats.mapped -= usize; |
| 932 | stats_cactive_sub(usize); |
| 933 | } |
| 934 | arena->nactive -= (usize >> LG_PAGE); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 935 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 936 | chunk_dalloc_cache(arena, &chunk_hooks, chunk, csize, true); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 937 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | void |
| 941 | arena_chunk_ralloc_huge_similar(arena_t *arena, void *chunk, size_t oldsize, |
| 942 | size_t usize) |
| 943 | { |
| 944 | |
| 945 | assert(CHUNK_CEILING(oldsize) == CHUNK_CEILING(usize)); |
| 946 | assert(oldsize != usize); |
| 947 | |
| 948 | malloc_mutex_lock(&arena->lock); |
| 949 | if (config_stats) |
| 950 | arena_huge_ralloc_stats_update(arena, oldsize, usize); |
| 951 | if (oldsize < usize) { |
| 952 | size_t udiff = usize - oldsize; |
| 953 | arena->nactive += udiff >> LG_PAGE; |
| 954 | if (config_stats) |
| 955 | stats_cactive_add(udiff); |
| 956 | } else { |
| 957 | size_t udiff = oldsize - usize; |
| 958 | arena->nactive -= udiff >> LG_PAGE; |
| 959 | if (config_stats) |
| 960 | stats_cactive_sub(udiff); |
| 961 | } |
| 962 | malloc_mutex_unlock(&arena->lock); |
| 963 | } |
| 964 | |
| 965 | void |
| 966 | arena_chunk_ralloc_huge_shrink(arena_t *arena, void *chunk, size_t oldsize, |
| 967 | size_t usize) |
| 968 | { |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 969 | size_t udiff = oldsize - usize; |
| 970 | size_t cdiff = CHUNK_CEILING(oldsize) - CHUNK_CEILING(usize); |
| 971 | |
| 972 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 973 | if (config_stats) { |
| 974 | arena_huge_ralloc_stats_update(arena, oldsize, usize); |
| 975 | if (cdiff != 0) { |
| 976 | arena->stats.mapped -= cdiff; |
| 977 | stats_cactive_sub(udiff); |
| 978 | } |
| 979 | } |
| 980 | arena->nactive -= udiff >> LG_PAGE; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 981 | |
Jason Evans | 2012d5a | 2014-11-17 09:54:49 -0800 | [diff] [blame] | 982 | if (cdiff != 0) { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 983 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 984 | void *nchunk = (void *)((uintptr_t)chunk + |
| 985 | CHUNK_CEILING(usize)); |
| 986 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 987 | chunk_dalloc_cache(arena, &chunk_hooks, nchunk, cdiff, true); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 988 | } |
| 989 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 992 | static bool |
| 993 | arena_chunk_ralloc_huge_expand_hard(arena_t *arena, chunk_hooks_t *chunk_hooks, |
| 994 | void *chunk, size_t oldsize, size_t usize, bool *zero, void *nchunk, |
| 995 | size_t udiff, size_t cdiff) |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 996 | { |
| 997 | bool err; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 998 | bool commit = true; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 999 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1000 | err = (chunk_alloc_wrapper(arena, chunk_hooks, nchunk, cdiff, chunksize, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1001 | zero, &commit) == NULL); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1002 | if (err) { |
| 1003 | /* Revert optimistic stats updates. */ |
| 1004 | malloc_mutex_lock(&arena->lock); |
| 1005 | if (config_stats) { |
| 1006 | arena_huge_ralloc_stats_update_undo(arena, oldsize, |
| 1007 | usize); |
| 1008 | arena->stats.mapped -= cdiff; |
| 1009 | } |
| 1010 | arena->nactive -= (udiff >> LG_PAGE); |
| 1011 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1012 | } else if (chunk_hooks->merge(chunk, CHUNK_CEILING(oldsize), nchunk, |
| 1013 | cdiff, true, arena->ind)) { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1014 | chunk_dalloc_arena(arena, chunk_hooks, nchunk, cdiff, *zero, |
| 1015 | true); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1016 | err = true; |
Jason Evans | 2012d5a | 2014-11-17 09:54:49 -0800 | [diff] [blame] | 1017 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1018 | return (err); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | bool |
| 1022 | arena_chunk_ralloc_huge_expand(arena_t *arena, void *chunk, size_t oldsize, |
| 1023 | size_t usize, bool *zero) |
| 1024 | { |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1025 | bool err; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1026 | chunk_hooks_t chunk_hooks = chunk_hooks_get(arena); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1027 | void *nchunk = (void *)((uintptr_t)chunk + CHUNK_CEILING(oldsize)); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1028 | size_t udiff = usize - oldsize; |
| 1029 | size_t cdiff = CHUNK_CEILING(usize) - CHUNK_CEILING(oldsize); |
| 1030 | |
| 1031 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1032 | |
| 1033 | /* Optimistically update stats. */ |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1034 | if (config_stats) { |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1035 | arena_huge_ralloc_stats_update(arena, oldsize, usize); |
| 1036 | arena->stats.mapped += cdiff; |
| 1037 | } |
| 1038 | arena->nactive += (udiff >> LG_PAGE); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1039 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1040 | err = (chunk_alloc_cache(arena, &arena->chunk_hooks, nchunk, cdiff, |
| 1041 | chunksize, zero, true) == NULL); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1042 | malloc_mutex_unlock(&arena->lock); |
| 1043 | if (err) { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1044 | err = arena_chunk_ralloc_huge_expand_hard(arena, &chunk_hooks, |
| 1045 | chunk, oldsize, usize, zero, nchunk, udiff, |
| 1046 | cdiff); |
| 1047 | } else if (chunk_hooks.merge(chunk, CHUNK_CEILING(oldsize), nchunk, |
| 1048 | cdiff, true, arena->ind)) { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1049 | chunk_dalloc_arena(arena, &chunk_hooks, nchunk, cdiff, *zero, |
| 1050 | true); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1051 | err = true; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1054 | if (config_stats && !err) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1055 | stats_cactive_add(udiff); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1056 | return (err); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1059 | /* |
| 1060 | * Do first-best-fit run selection, i.e. select the lowest run that best fits. |
| 1061 | * Run sizes are quantized, so not all candidate runs are necessarily exactly |
| 1062 | * the same size. |
| 1063 | */ |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1064 | static arena_run_t * |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1065 | arena_run_first_best_fit(arena_t *arena, size_t size) |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1066 | { |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1067 | size_t search_size = run_quantize_first(size); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1068 | arena_chunk_map_misc_t *key = arena_miscelm_key_create(search_size); |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1069 | arena_chunk_map_misc_t *miscelm = |
| 1070 | arena_avail_tree_nsearch(&arena->runs_avail, key); |
| 1071 | if (miscelm == NULL) |
| 1072 | return (NULL); |
| 1073 | return (&miscelm->run); |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1074 | } |
| 1075 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1076 | static arena_run_t * |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1077 | arena_run_alloc_large_helper(arena_t *arena, size_t size, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1078 | { |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1079 | arena_run_t *run = arena_run_first_best_fit(arena, s2u(size)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1080 | if (run != NULL) { |
| 1081 | if (arena_run_split_large(arena, run, size, zero)) |
| 1082 | run = NULL; |
| 1083 | } |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1084 | return (run); |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | static arena_run_t * |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1088 | arena_run_alloc_large(arena_t *arena, size_t size, bool zero) |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1089 | { |
| 1090 | arena_chunk_t *chunk; |
| 1091 | arena_run_t *run; |
| 1092 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 1093 | assert(size <= arena_maxrun); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 1094 | assert(size == PAGE_CEILING(size)); |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1095 | |
| 1096 | /* Search the arena's chunks for the lowest best fit. */ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1097 | run = arena_run_alloc_large_helper(arena, size, zero); |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1098 | if (run != NULL) |
| 1099 | return (run); |
| 1100 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1101 | /* |
| 1102 | * No usable runs. Create a new chunk from which to allocate the run. |
| 1103 | */ |
| 1104 | chunk = arena_chunk_alloc(arena); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1105 | if (chunk != NULL) { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1106 | run = &arena_miscelm_get(chunk, map_bias)->run; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1107 | if (arena_run_split_large(arena, run, size, zero)) |
| 1108 | run = NULL; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1109 | return (run); |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * arena_chunk_alloc() failed, but another thread may have made |
| 1114 | * sufficient memory available while this one dropped arena->lock in |
| 1115 | * arena_chunk_alloc(), so search one more time. |
| 1116 | */ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1117 | return (arena_run_alloc_large_helper(arena, size, zero)); |
| 1118 | } |
| 1119 | |
| 1120 | static arena_run_t * |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1121 | arena_run_alloc_small_helper(arena_t *arena, size_t size, index_t binind) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1122 | { |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1123 | arena_run_t *run = arena_run_first_best_fit(arena, size); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1124 | if (run != NULL) { |
| 1125 | if (arena_run_split_small(arena, run, size, binind)) |
| 1126 | run = NULL; |
| 1127 | } |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1128 | return (run); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static arena_run_t * |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1132 | arena_run_alloc_small(arena_t *arena, size_t size, index_t binind) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1133 | { |
| 1134 | arena_chunk_t *chunk; |
| 1135 | arena_run_t *run; |
| 1136 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 1137 | assert(size <= arena_maxrun); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 1138 | assert(size == PAGE_CEILING(size)); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1139 | assert(binind != BININD_INVALID); |
| 1140 | |
| 1141 | /* Search the arena's chunks for the lowest best fit. */ |
| 1142 | run = arena_run_alloc_small_helper(arena, size, binind); |
| 1143 | if (run != NULL) |
| 1144 | return (run); |
| 1145 | |
| 1146 | /* |
| 1147 | * No usable runs. Create a new chunk from which to allocate the run. |
| 1148 | */ |
| 1149 | chunk = arena_chunk_alloc(arena); |
| 1150 | if (chunk != NULL) { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1151 | run = &arena_miscelm_get(chunk, map_bias)->run; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1152 | if (arena_run_split_small(arena, run, size, binind)) |
| 1153 | run = NULL; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1154 | return (run); |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * arena_chunk_alloc() failed, but another thread may have made |
| 1159 | * sufficient memory available while this one dropped arena->lock in |
| 1160 | * arena_chunk_alloc(), so search one more time. |
| 1161 | */ |
| 1162 | return (arena_run_alloc_small_helper(arena, size, binind)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1165 | static bool |
| 1166 | arena_lg_dirty_mult_valid(ssize_t lg_dirty_mult) |
| 1167 | { |
| 1168 | |
Jason Evans | bd16ea4 | 2015-03-24 15:59:28 -0700 | [diff] [blame] | 1169 | return (lg_dirty_mult >= -1 && lg_dirty_mult < (ssize_t)(sizeof(size_t) |
| 1170 | << 3)); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | ssize_t |
| 1174 | arena_lg_dirty_mult_get(arena_t *arena) |
| 1175 | { |
| 1176 | ssize_t lg_dirty_mult; |
| 1177 | |
| 1178 | malloc_mutex_lock(&arena->lock); |
| 1179 | lg_dirty_mult = arena->lg_dirty_mult; |
| 1180 | malloc_mutex_unlock(&arena->lock); |
| 1181 | |
| 1182 | return (lg_dirty_mult); |
| 1183 | } |
| 1184 | |
| 1185 | bool |
| 1186 | arena_lg_dirty_mult_set(arena_t *arena, ssize_t lg_dirty_mult) |
| 1187 | { |
| 1188 | |
| 1189 | if (!arena_lg_dirty_mult_valid(lg_dirty_mult)) |
| 1190 | return (true); |
| 1191 | |
| 1192 | malloc_mutex_lock(&arena->lock); |
| 1193 | arena->lg_dirty_mult = lg_dirty_mult; |
| 1194 | arena_maybe_purge(arena); |
| 1195 | malloc_mutex_unlock(&arena->lock); |
| 1196 | |
| 1197 | return (false); |
| 1198 | } |
| 1199 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1200 | void |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 1201 | arena_maybe_purge(arena_t *arena) |
| 1202 | { |
| 1203 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1204 | /* Don't purge if the option is disabled. */ |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1205 | if (arena->lg_dirty_mult < 0) |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1206 | return; |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1207 | /* Don't recursively purge. */ |
| 1208 | if (arena->purging) |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1209 | return; |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1210 | /* |
| 1211 | * Iterate, since preventing recursive purging could otherwise leave too |
| 1212 | * many dirty pages. |
| 1213 | */ |
| 1214 | while (true) { |
| 1215 | size_t threshold = (arena->nactive >> arena->lg_dirty_mult); |
| 1216 | if (threshold < chunk_npages) |
| 1217 | threshold = chunk_npages; |
| 1218 | /* |
| 1219 | * Don't purge unless the number of purgeable pages exceeds the |
| 1220 | * threshold. |
| 1221 | */ |
| 1222 | if (arena->ndirty <= threshold) |
| 1223 | return; |
| 1224 | arena_purge(arena, false); |
| 1225 | } |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1228 | static size_t |
| 1229 | arena_dirty_count(arena_t *arena) |
| 1230 | { |
| 1231 | size_t ndirty = 0; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1232 | arena_runs_dirty_link_t *rdelm; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1233 | extent_node_t *chunkselm; |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1234 | |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1235 | for (rdelm = qr_next(&arena->runs_dirty, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1236 | chunkselm = qr_next(&arena->chunks_cache, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1237 | rdelm != &arena->runs_dirty; rdelm = qr_next(rdelm, rd_link)) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1238 | size_t npages; |
| 1239 | |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1240 | if (rdelm == &chunkselm->rd) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1241 | npages = extent_node_size_get(chunkselm) >> LG_PAGE; |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1242 | chunkselm = qr_next(chunkselm, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1243 | } else { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1244 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE( |
| 1245 | rdelm); |
| 1246 | arena_chunk_map_misc_t *miscelm = |
| 1247 | arena_rd_to_miscelm(rdelm); |
| 1248 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1249 | assert(arena_mapbits_allocated_get(chunk, pageind) == |
| 1250 | 0); |
| 1251 | assert(arena_mapbits_large_get(chunk, pageind) == 0); |
| 1252 | assert(arena_mapbits_dirty_get(chunk, pageind) != 0); |
| 1253 | npages = arena_mapbits_unallocated_size_get(chunk, |
| 1254 | pageind) >> LG_PAGE; |
| 1255 | } |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1256 | ndirty += npages; |
| 1257 | } |
| 1258 | |
Jason Evans | 2b2f6dc | 2014-11-01 02:29:10 -0700 | [diff] [blame] | 1259 | return (ndirty); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static size_t |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1263 | arena_compute_npurge(arena_t *arena, bool all) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1264 | { |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1265 | size_t npurge; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1266 | |
| 1267 | /* |
| 1268 | * Compute the minimum number of pages that this thread should try to |
| 1269 | * purge. |
| 1270 | */ |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 1271 | if (!all) { |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1272 | size_t threshold = (arena->nactive >> arena->lg_dirty_mult); |
Mike Hommey | 6505733 | 2015-02-04 07:16:55 +0900 | [diff] [blame] | 1273 | threshold = threshold < chunk_npages ? chunk_npages : threshold; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1274 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1275 | npurge = arena->ndirty - threshold; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1276 | } else |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1277 | npurge = arena->ndirty; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1278 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1279 | return (npurge); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1282 | static size_t |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1283 | arena_stash_dirty(arena_t *arena, chunk_hooks_t *chunk_hooks, bool all, |
| 1284 | size_t npurge, arena_runs_dirty_link_t *purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1285 | extent_node_t *purge_chunks_sentinel) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1286 | { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1287 | arena_runs_dirty_link_t *rdelm, *rdelm_next; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1288 | extent_node_t *chunkselm; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1289 | size_t nstashed = 0; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1290 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1291 | /* Stash at least npurge pages. */ |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1292 | for (rdelm = qr_next(&arena->runs_dirty, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1293 | chunkselm = qr_next(&arena->chunks_cache, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1294 | rdelm != &arena->runs_dirty; rdelm = rdelm_next) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1295 | size_t npages; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1296 | rdelm_next = qr_next(rdelm, rd_link); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1297 | |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1298 | if (rdelm == &chunkselm->rd) { |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1299 | extent_node_t *chunkselm_next; |
| 1300 | bool zero; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1301 | UNUSED void *chunk; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1302 | |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1303 | chunkselm_next = qr_next(chunkselm, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1304 | /* |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1305 | * Allocate. chunkselm remains valid due to the |
| 1306 | * dalloc_node=false argument to chunk_alloc_cache(). |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1307 | */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1308 | zero = false; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1309 | chunk = chunk_alloc_cache(arena, chunk_hooks, |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1310 | extent_node_addr_get(chunkselm), |
| 1311 | extent_node_size_get(chunkselm), chunksize, &zero, |
| 1312 | false); |
| 1313 | assert(chunk == extent_node_addr_get(chunkselm)); |
| 1314 | assert(zero == extent_node_zeroed_get(chunkselm)); |
| 1315 | extent_node_dirty_insert(chunkselm, purge_runs_sentinel, |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1316 | purge_chunks_sentinel); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1317 | npages = extent_node_size_get(chunkselm) >> LG_PAGE; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1318 | chunkselm = chunkselm_next; |
| 1319 | } else { |
| 1320 | arena_chunk_t *chunk = |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1321 | (arena_chunk_t *)CHUNK_ADDR2BASE(rdelm); |
| 1322 | arena_chunk_map_misc_t *miscelm = |
| 1323 | arena_rd_to_miscelm(rdelm); |
| 1324 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
| 1325 | arena_run_t *run = &miscelm->run; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1326 | size_t run_size = |
| 1327 | arena_mapbits_unallocated_size_get(chunk, pageind); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1328 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1329 | npages = run_size >> LG_PAGE; |
| 1330 | |
| 1331 | assert(pageind + npages <= chunk_npages); |
| 1332 | assert(arena_mapbits_dirty_get(chunk, pageind) == |
| 1333 | arena_mapbits_dirty_get(chunk, pageind+npages-1)); |
| 1334 | |
| 1335 | /* |
| 1336 | * If purging the spare chunk's run, make it available |
| 1337 | * prior to allocation. |
| 1338 | */ |
| 1339 | if (chunk == arena->spare) |
| 1340 | arena_chunk_alloc(arena); |
| 1341 | |
| 1342 | /* Temporarily allocate the free dirty run. */ |
| 1343 | arena_run_split_large(arena, run, run_size, false); |
Jason Evans | 339c2b2 | 2015-02-17 22:25:56 -0800 | [diff] [blame] | 1344 | /* Stash. */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1345 | if (false) |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1346 | qr_new(rdelm, rd_link); /* Redundant. */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1347 | else { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1348 | assert(qr_next(rdelm, rd_link) == rdelm); |
| 1349 | assert(qr_prev(rdelm, rd_link) == rdelm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1350 | } |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1351 | qr_meld(purge_runs_sentinel, rdelm, rd_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1352 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1353 | |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1354 | nstashed += npages; |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 1355 | if (!all && nstashed >= npurge) |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1356 | break; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1357 | } |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1358 | |
| 1359 | return (nstashed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static size_t |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1363 | arena_purge_stashed(arena_t *arena, chunk_hooks_t *chunk_hooks, |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1364 | arena_runs_dirty_link_t *purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1365 | extent_node_t *purge_chunks_sentinel) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1366 | { |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1367 | size_t npurged, nmadvise; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1368 | arena_runs_dirty_link_t *rdelm; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1369 | extent_node_t *chunkselm; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1370 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1371 | if (config_stats) |
| 1372 | nmadvise = 0; |
| 1373 | npurged = 0; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1374 | |
| 1375 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1376 | for (rdelm = qr_next(purge_runs_sentinel, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1377 | chunkselm = qr_next(purge_chunks_sentinel, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1378 | rdelm != purge_runs_sentinel; rdelm = qr_next(rdelm, rd_link)) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1379 | size_t npages; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1380 | |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1381 | if (rdelm == &chunkselm->rd) { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1382 | /* |
| 1383 | * Don't actually purge the chunk here because 1) |
| 1384 | * chunkselm is embedded in the chunk and must remain |
| 1385 | * valid, and 2) we deallocate the chunk in |
| 1386 | * arena_unstash_purged(), where it is destroyed, |
| 1387 | * decommitted, or purged, depending on chunk |
| 1388 | * deallocation policy. |
| 1389 | */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1390 | size_t size = extent_node_size_get(chunkselm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1391 | npages = size >> LG_PAGE; |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1392 | chunkselm = qr_next(chunkselm, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1393 | } else { |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 1394 | size_t pageind, run_size, flag_unzeroed, flags, i; |
| 1395 | bool decommitted; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1396 | arena_chunk_t *chunk = |
| 1397 | (arena_chunk_t *)CHUNK_ADDR2BASE(rdelm); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1398 | arena_chunk_map_misc_t *miscelm = |
| 1399 | arena_rd_to_miscelm(rdelm); |
| 1400 | pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1401 | run_size = arena_mapbits_large_size_get(chunk, pageind); |
| 1402 | npages = run_size >> LG_PAGE; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1403 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1404 | assert(pageind + npages <= chunk_npages); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 1405 | assert(!arena_mapbits_decommitted_get(chunk, pageind)); |
| 1406 | assert(!arena_mapbits_decommitted_get(chunk, |
| 1407 | pageind+npages-1)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1408 | decommitted = !chunk_hooks->decommit(chunk, chunksize, |
| 1409 | pageind << LG_PAGE, npages << LG_PAGE, arena->ind); |
| 1410 | if (decommitted) { |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 1411 | flag_unzeroed = 0; |
| 1412 | flags = CHUNK_MAP_DECOMMITTED; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1413 | } else { |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 1414 | flag_unzeroed = chunk_purge_wrapper(arena, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1415 | chunk_hooks, chunk, chunksize, pageind << |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 1416 | LG_PAGE, run_size) ? CHUNK_MAP_UNZEROED : 0; |
| 1417 | flags = flag_unzeroed; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1418 | } |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 1419 | arena_mapbits_large_set(chunk, pageind+npages-1, 0, |
| 1420 | flags); |
| 1421 | arena_mapbits_large_set(chunk, pageind, run_size, |
| 1422 | flags); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1423 | |
| 1424 | /* |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 1425 | * Set the unzeroed flag for internal pages, now that |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1426 | * chunk_purge_wrapper() has returned whether the pages |
| 1427 | * were zeroed as a side effect of purging. This chunk |
| 1428 | * map modification is safe even though the arena mutex |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1429 | * isn't currently owned by this thread, because the run |
| 1430 | * is marked as allocated, thus protecting it from being |
| 1431 | * modified by any other thread. As long as these |
| 1432 | * writes don't perturb the first and last elements' |
| 1433 | * CHUNK_MAP_ALLOCATED bits, behavior is well defined. |
| 1434 | */ |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame^] | 1435 | for (i = 1; i < npages-1; i++) { |
| 1436 | arena_mapbits_internal_set(chunk, pageind+i, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1437 | flag_unzeroed); |
| 1438 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1439 | } |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1440 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1441 | npurged += npages; |
| 1442 | if (config_stats) |
| 1443 | nmadvise++; |
| 1444 | } |
| 1445 | malloc_mutex_lock(&arena->lock); |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1446 | |
| 1447 | if (config_stats) { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1448 | arena->stats.nmadvise += nmadvise; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1449 | arena->stats.purged += npurged; |
| 1450 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1451 | |
| 1452 | return (npurged); |
| 1453 | } |
| 1454 | |
| 1455 | static void |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1456 | arena_unstash_purged(arena_t *arena, chunk_hooks_t *chunk_hooks, |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1457 | arena_runs_dirty_link_t *purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1458 | extent_node_t *purge_chunks_sentinel) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1459 | { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1460 | arena_runs_dirty_link_t *rdelm, *rdelm_next; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1461 | extent_node_t *chunkselm; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1462 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1463 | /* Deallocate chunks/runs. */ |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1464 | for (rdelm = qr_next(purge_runs_sentinel, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1465 | chunkselm = qr_next(purge_chunks_sentinel, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1466 | rdelm != purge_runs_sentinel; rdelm = rdelm_next) { |
| 1467 | rdelm_next = qr_next(rdelm, rd_link); |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1468 | if (rdelm == &chunkselm->rd) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1469 | extent_node_t *chunkselm_next = qr_next(chunkselm, |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1470 | cc_link); |
Jason Evans | 339c2b2 | 2015-02-17 22:25:56 -0800 | [diff] [blame] | 1471 | void *addr = extent_node_addr_get(chunkselm); |
| 1472 | size_t size = extent_node_size_get(chunkselm); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1473 | bool zeroed = extent_node_zeroed_get(chunkselm); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 1474 | bool committed = extent_node_committed_get(chunkselm); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1475 | extent_node_dirty_remove(chunkselm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1476 | arena_node_dalloc(arena, chunkselm); |
| 1477 | chunkselm = chunkselm_next; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1478 | chunk_dalloc_arena(arena, chunk_hooks, addr, size, |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 1479 | zeroed, committed); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1480 | } else { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1481 | arena_chunk_t *chunk = |
| 1482 | (arena_chunk_t *)CHUNK_ADDR2BASE(rdelm); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1483 | arena_chunk_map_misc_t *miscelm = |
| 1484 | arena_rd_to_miscelm(rdelm); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1485 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
| 1486 | bool decommitted = (arena_mapbits_decommitted_get(chunk, |
| 1487 | pageind) != 0); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1488 | arena_run_t *run = &miscelm->run; |
| 1489 | qr_remove(rdelm, rd_link); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1490 | arena_run_dalloc(arena, run, false, true, decommitted); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1491 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1495 | static void |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 1496 | arena_purge(arena_t *arena, bool all) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1497 | { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1498 | chunk_hooks_t chunk_hooks = chunk_hooks_get(arena); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1499 | size_t npurge, npurgeable, npurged; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1500 | arena_runs_dirty_link_t purge_runs_sentinel; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1501 | extent_node_t purge_chunks_sentinel; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1502 | |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1503 | arena->purging = true; |
| 1504 | |
Jason Evans | 2b2f6dc | 2014-11-01 02:29:10 -0700 | [diff] [blame] | 1505 | /* |
| 1506 | * Calls to arena_dirty_count() are disabled even for debug builds |
| 1507 | * because overhead grows nonlinearly as memory usage increases. |
| 1508 | */ |
| 1509 | if (false && config_debug) { |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 1510 | size_t ndirty = arena_dirty_count(arena); |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1511 | assert(ndirty == arena->ndirty); |
Jason Evans | 2caa471 | 2010-03-04 21:35:07 -0800 | [diff] [blame] | 1512 | } |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1513 | assert((arena->nactive >> arena->lg_dirty_mult) < arena->ndirty || all); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1514 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1515 | if (config_stats) |
| 1516 | arena->stats.npurge++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1517 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1518 | npurge = arena_compute_npurge(arena, all); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1519 | qr_new(&purge_runs_sentinel, rd_link); |
Jason Evans | 47701b2 | 2015-02-17 22:23:10 -0800 | [diff] [blame] | 1520 | extent_node_dirty_linkage_init(&purge_chunks_sentinel); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1521 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1522 | npurgeable = arena_stash_dirty(arena, &chunk_hooks, all, npurge, |
| 1523 | &purge_runs_sentinel, &purge_chunks_sentinel); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1524 | assert(npurgeable >= npurge); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1525 | npurged = arena_purge_stashed(arena, &chunk_hooks, &purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1526 | &purge_chunks_sentinel); |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1527 | assert(npurged == npurgeable); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1528 | arena_unstash_purged(arena, &chunk_hooks, &purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1529 | &purge_chunks_sentinel); |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1530 | |
| 1531 | arena->purging = false; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1532 | } |
| 1533 | |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 1534 | void |
| 1535 | arena_purge_all(arena_t *arena) |
| 1536 | { |
| 1537 | |
| 1538 | malloc_mutex_lock(&arena->lock); |
| 1539 | arena_purge(arena, true); |
| 1540 | malloc_mutex_unlock(&arena->lock); |
| 1541 | } |
| 1542 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1543 | static void |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1544 | arena_run_coalesce(arena_t *arena, arena_chunk_t *chunk, size_t *p_size, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1545 | size_t *p_run_ind, size_t *p_run_pages, size_t flag_dirty, |
| 1546 | size_t flag_decommitted) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1547 | { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1548 | size_t size = *p_size; |
| 1549 | size_t run_ind = *p_run_ind; |
| 1550 | size_t run_pages = *p_run_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1551 | |
| 1552 | /* Try to coalesce forward. */ |
| 1553 | if (run_ind + run_pages < chunk_npages && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1554 | arena_mapbits_allocated_get(chunk, run_ind+run_pages) == 0 && |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1555 | arena_mapbits_dirty_get(chunk, run_ind+run_pages) == flag_dirty && |
| 1556 | arena_mapbits_decommitted_get(chunk, run_ind+run_pages) == |
| 1557 | flag_decommitted) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1558 | size_t nrun_size = arena_mapbits_unallocated_size_get(chunk, |
| 1559 | run_ind+run_pages); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1560 | size_t nrun_pages = nrun_size >> LG_PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1561 | |
| 1562 | /* |
| 1563 | * Remove successor from runs_avail; the coalesced run is |
| 1564 | * inserted later. |
| 1565 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1566 | assert(arena_mapbits_unallocated_size_get(chunk, |
| 1567 | run_ind+run_pages+nrun_pages-1) == nrun_size); |
| 1568 | assert(arena_mapbits_dirty_get(chunk, |
| 1569 | run_ind+run_pages+nrun_pages-1) == flag_dirty); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1570 | assert(arena_mapbits_decommitted_get(chunk, |
| 1571 | run_ind+run_pages+nrun_pages-1) == flag_decommitted); |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 1572 | arena_avail_remove(arena, chunk, run_ind+run_pages, nrun_pages); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1573 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1574 | /* |
| 1575 | * If the successor is dirty, remove it from the set of dirty |
| 1576 | * pages. |
| 1577 | */ |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 1578 | if (flag_dirty != 0) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1579 | arena_run_dirty_remove(arena, chunk, run_ind+run_pages, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1580 | nrun_pages); |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1583 | size += nrun_size; |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 1584 | run_pages += nrun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1585 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1586 | arena_mapbits_unallocated_size_set(chunk, run_ind, size); |
| 1587 | arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1, |
| 1588 | size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* Try to coalesce backward. */ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1592 | if (run_ind > map_bias && arena_mapbits_allocated_get(chunk, |
| 1593 | run_ind-1) == 0 && arena_mapbits_dirty_get(chunk, run_ind-1) == |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1594 | flag_dirty && arena_mapbits_decommitted_get(chunk, run_ind-1) == |
| 1595 | flag_decommitted) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1596 | size_t prun_size = arena_mapbits_unallocated_size_get(chunk, |
| 1597 | run_ind-1); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1598 | size_t prun_pages = prun_size >> LG_PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1599 | |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 1600 | run_ind -= prun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1601 | |
| 1602 | /* |
| 1603 | * Remove predecessor from runs_avail; the coalesced run is |
| 1604 | * inserted later. |
| 1605 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1606 | assert(arena_mapbits_unallocated_size_get(chunk, run_ind) == |
| 1607 | prun_size); |
| 1608 | assert(arena_mapbits_dirty_get(chunk, run_ind) == flag_dirty); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1609 | assert(arena_mapbits_decommitted_get(chunk, run_ind) == |
| 1610 | flag_decommitted); |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 1611 | arena_avail_remove(arena, chunk, run_ind, prun_pages); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1612 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1613 | /* |
| 1614 | * If the predecessor is dirty, remove it from the set of dirty |
| 1615 | * pages. |
| 1616 | */ |
| 1617 | if (flag_dirty != 0) { |
| 1618 | arena_run_dirty_remove(arena, chunk, run_ind, |
| 1619 | prun_pages); |
| 1620 | } |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 1621 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1622 | size += prun_size; |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 1623 | run_pages += prun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1624 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1625 | arena_mapbits_unallocated_size_set(chunk, run_ind, size); |
| 1626 | arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1, |
| 1627 | size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1630 | *p_size = size; |
| 1631 | *p_run_ind = run_ind; |
| 1632 | *p_run_pages = run_pages; |
| 1633 | } |
| 1634 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1635 | static size_t |
| 1636 | arena_run_size_get(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 1637 | size_t run_ind) |
| 1638 | { |
| 1639 | size_t size; |
| 1640 | |
| 1641 | assert(run_ind >= map_bias); |
| 1642 | assert(run_ind < chunk_npages); |
| 1643 | |
| 1644 | if (arena_mapbits_large_get(chunk, run_ind) != 0) { |
| 1645 | size = arena_mapbits_large_size_get(chunk, run_ind); |
| 1646 | assert(size == PAGE || arena_mapbits_large_size_get(chunk, |
| 1647 | run_ind+(size>>LG_PAGE)-1) == 0); |
| 1648 | } else { |
| 1649 | arena_bin_info_t *bin_info = &arena_bin_info[run->binind]; |
| 1650 | size = bin_info->run_size; |
| 1651 | } |
| 1652 | |
| 1653 | return (size); |
| 1654 | } |
| 1655 | |
| 1656 | static bool |
| 1657 | arena_run_decommit(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run) |
| 1658 | { |
| 1659 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
| 1660 | size_t run_ind = arena_miscelm_to_pageind(miscelm); |
| 1661 | size_t offset = run_ind << LG_PAGE; |
| 1662 | size_t length = arena_run_size_get(arena, chunk, run, run_ind); |
| 1663 | |
| 1664 | return (arena->chunk_hooks.decommit(chunk, chunksize, offset, length, |
| 1665 | arena->ind)); |
| 1666 | } |
| 1667 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1668 | static void |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1669 | arena_run_dalloc(arena_t *arena, arena_run_t *run, bool dirty, bool cleaned, |
| 1670 | bool decommitted) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1671 | { |
| 1672 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1673 | arena_chunk_map_misc_t *miscelm; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1674 | size_t size, run_ind, run_pages, flag_dirty, flag_decommitted; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1675 | |
| 1676 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1677 | miscelm = arena_run_to_miscelm(run); |
| 1678 | run_ind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1679 | assert(run_ind >= map_bias); |
| 1680 | assert(run_ind < chunk_npages); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1681 | size = arena_run_size_get(arena, chunk, run, run_ind); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1682 | run_pages = (size >> LG_PAGE); |
| 1683 | arena_cactive_update(arena, 0, run_pages); |
| 1684 | arena->nactive -= run_pages; |
| 1685 | |
| 1686 | /* |
| 1687 | * The run is dirty if the caller claims to have dirtied it, as well as |
| 1688 | * if it was already dirty before being allocated and the caller |
| 1689 | * doesn't claim to have cleaned it. |
| 1690 | */ |
| 1691 | assert(arena_mapbits_dirty_get(chunk, run_ind) == |
| 1692 | arena_mapbits_dirty_get(chunk, run_ind+run_pages-1)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1693 | if (!cleaned && !decommitted && arena_mapbits_dirty_get(chunk, run_ind) |
| 1694 | != 0) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1695 | dirty = true; |
| 1696 | flag_dirty = dirty ? CHUNK_MAP_DIRTY : 0; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1697 | flag_decommitted = decommitted ? CHUNK_MAP_DECOMMITTED : 0; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1698 | |
| 1699 | /* Mark pages as unallocated in the chunk map. */ |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1700 | if (dirty || decommitted) { |
| 1701 | size_t flags = flag_dirty | flag_decommitted; |
| 1702 | arena_mapbits_unallocated_set(chunk, run_ind, size, flags); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1703 | arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1704 | flags); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1705 | } else { |
| 1706 | arena_mapbits_unallocated_set(chunk, run_ind, size, |
| 1707 | arena_mapbits_unzeroed_get(chunk, run_ind)); |
| 1708 | arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size, |
| 1709 | arena_mapbits_unzeroed_get(chunk, run_ind+run_pages-1)); |
| 1710 | } |
| 1711 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1712 | arena_run_coalesce(arena, chunk, &size, &run_ind, &run_pages, |
| 1713 | flag_dirty, flag_decommitted); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1714 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1715 | /* Insert into runs_avail, now that coalescing is complete. */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1716 | assert(arena_mapbits_unallocated_size_get(chunk, run_ind) == |
| 1717 | arena_mapbits_unallocated_size_get(chunk, run_ind+run_pages-1)); |
| 1718 | assert(arena_mapbits_dirty_get(chunk, run_ind) == |
| 1719 | arena_mapbits_dirty_get(chunk, run_ind+run_pages-1)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1720 | assert(arena_mapbits_decommitted_get(chunk, run_ind) == |
| 1721 | arena_mapbits_decommitted_get(chunk, run_ind+run_pages-1)); |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 1722 | arena_avail_insert(arena, chunk, run_ind, run_pages); |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 1723 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1724 | if (dirty) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1725 | arena_run_dirty_insert(arena, chunk, run_ind, run_pages); |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 1726 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1727 | /* Deallocate chunk if it is now completely unused. */ |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1728 | if (size == arena_maxrun) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1729 | assert(run_ind == map_bias); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1730 | assert(run_pages == (arena_maxrun >> LG_PAGE)); |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 1731 | arena_chunk_dalloc(arena, chunk); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1732 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1733 | |
Jason Evans | 4fb7f51 | 2010-01-27 18:27:09 -0800 | [diff] [blame] | 1734 | /* |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 1735 | * It is okay to do dirty page processing here even if the chunk was |
Jason Evans | 4fb7f51 | 2010-01-27 18:27:09 -0800 | [diff] [blame] | 1736 | * deallocated above, since in that case it is the spare. Waiting |
| 1737 | * until after possible chunk deallocation to do dirty processing |
| 1738 | * allows for an old spare to be fully deallocated, thus decreasing the |
| 1739 | * chances of spuriously crossing the dirty page purging threshold. |
| 1740 | */ |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 1741 | if (dirty) |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 1742 | arena_maybe_purge(arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | static void |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 1746 | arena_run_dalloc_decommit(arena_t *arena, arena_chunk_t *chunk, |
| 1747 | arena_run_t *run) |
| 1748 | { |
| 1749 | bool committed = arena_run_decommit(arena, chunk, run); |
| 1750 | |
| 1751 | arena_run_dalloc(arena, run, committed, false, !committed); |
| 1752 | } |
| 1753 | |
| 1754 | static void |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1755 | arena_run_trim_head(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 1756 | size_t oldsize, size_t newsize) |
| 1757 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1758 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
| 1759 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1760 | size_t head_npages = (oldsize - newsize) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1761 | size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1762 | bool decommitted = (arena_mapbits_decommitted_get(chunk, pageind) != 0); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1763 | |
| 1764 | assert(oldsize > newsize); |
| 1765 | |
| 1766 | /* |
| 1767 | * Update the chunk map so that arena_run_dalloc() can treat the |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1768 | * leading run as separately allocated. Set the last element of each |
| 1769 | * run first, in case of single-page runs. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1770 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1771 | assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1772 | arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty); |
| 1773 | arena_mapbits_large_set(chunk, pageind, oldsize-newsize, flag_dirty); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1774 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1775 | if (config_debug) { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1776 | UNUSED size_t tail_npages = newsize >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1777 | assert(arena_mapbits_large_size_get(chunk, |
| 1778 | pageind+head_npages+tail_npages-1) == 0); |
| 1779 | assert(arena_mapbits_dirty_get(chunk, |
| 1780 | pageind+head_npages+tail_npages-1) == flag_dirty); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1781 | } |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1782 | arena_mapbits_large_set(chunk, pageind+head_npages, newsize, |
| 1783 | flag_dirty); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1784 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1785 | arena_run_dalloc(arena, run, false, false, decommitted); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | static void |
| 1789 | arena_run_trim_tail(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 1790 | size_t oldsize, size_t newsize, bool dirty) |
| 1791 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1792 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
| 1793 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1794 | size_t head_npages = newsize >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1795 | size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 1796 | bool decommitted = (arena_mapbits_decommitted_get(chunk, pageind) != 0); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1797 | arena_chunk_map_misc_t *tail_miscelm; |
| 1798 | arena_run_t *tail_run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1799 | |
| 1800 | assert(oldsize > newsize); |
| 1801 | |
| 1802 | /* |
| 1803 | * Update the chunk map so that arena_run_dalloc() can treat the |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1804 | * trailing run as separately allocated. Set the last element of each |
| 1805 | * run first, in case of single-page runs. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1806 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1807 | assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1808 | arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty); |
| 1809 | arena_mapbits_large_set(chunk, pageind, newsize, flag_dirty); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1810 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1811 | if (config_debug) { |
| 1812 | UNUSED size_t tail_npages = (oldsize - newsize) >> LG_PAGE; |
| 1813 | assert(arena_mapbits_large_size_get(chunk, |
| 1814 | pageind+head_npages+tail_npages-1) == 0); |
| 1815 | assert(arena_mapbits_dirty_get(chunk, |
| 1816 | pageind+head_npages+tail_npages-1) == flag_dirty); |
| 1817 | } |
| 1818 | arena_mapbits_large_set(chunk, pageind+head_npages, oldsize-newsize, |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1819 | flag_dirty); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1820 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1821 | tail_miscelm = arena_miscelm_get(chunk, pageind + head_npages); |
| 1822 | tail_run = &tail_miscelm->run; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1823 | arena_run_dalloc(arena, tail_run, dirty, false, decommitted); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | static arena_run_t * |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1827 | arena_bin_runs_first(arena_bin_t *bin) |
| 1828 | { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 1829 | arena_chunk_map_misc_t *miscelm = arena_run_tree_first(&bin->runs); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1830 | if (miscelm != NULL) |
| 1831 | return (&miscelm->run); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1832 | |
| 1833 | return (NULL); |
| 1834 | } |
| 1835 | |
| 1836 | static void |
| 1837 | arena_bin_runs_insert(arena_bin_t *bin, arena_run_t *run) |
| 1838 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1839 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1840 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 1841 | assert(arena_run_tree_search(&bin->runs, miscelm) == NULL); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1842 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 1843 | arena_run_tree_insert(&bin->runs, miscelm); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | static void |
| 1847 | arena_bin_runs_remove(arena_bin_t *bin, arena_run_t *run) |
| 1848 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1849 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1850 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 1851 | assert(arena_run_tree_search(&bin->runs, miscelm) != NULL); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1852 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 1853 | arena_run_tree_remove(&bin->runs, miscelm); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | static arena_run_t * |
| 1857 | arena_bin_nonfull_run_tryget(arena_bin_t *bin) |
| 1858 | { |
| 1859 | arena_run_t *run = arena_bin_runs_first(bin); |
| 1860 | if (run != NULL) { |
| 1861 | arena_bin_runs_remove(bin, run); |
| 1862 | if (config_stats) |
| 1863 | bin->stats.reruns++; |
| 1864 | } |
| 1865 | return (run); |
| 1866 | } |
| 1867 | |
| 1868 | static arena_run_t * |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1869 | arena_bin_nonfull_run_get(arena_t *arena, arena_bin_t *bin) |
| 1870 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1871 | arena_run_t *run; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1872 | index_t binind; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1873 | arena_bin_info_t *bin_info; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1874 | |
| 1875 | /* Look for a usable run. */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1876 | run = arena_bin_nonfull_run_tryget(bin); |
| 1877 | if (run != NULL) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1878 | return (run); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1879 | /* No existing runs have any space available. */ |
| 1880 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1881 | binind = arena_bin_index(arena, bin); |
| 1882 | bin_info = &arena_bin_info[binind]; |
| 1883 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1884 | /* Allocate a new run. */ |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1885 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 1886 | /******************************/ |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1887 | malloc_mutex_lock(&arena->lock); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1888 | run = arena_run_alloc_small(arena, bin_info->run_size, binind); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1889 | if (run != NULL) { |
| 1890 | /* Initialize run internals. */ |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 1891 | run->binind = binind; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1892 | run->nfree = bin_info->nregs; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 1893 | bitmap_init(run->bitmap, &bin_info->bitmap_info); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 1894 | } |
| 1895 | malloc_mutex_unlock(&arena->lock); |
| 1896 | /********************************/ |
| 1897 | malloc_mutex_lock(&bin->lock); |
| 1898 | if (run != NULL) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1899 | if (config_stats) { |
| 1900 | bin->stats.nruns++; |
| 1901 | bin->stats.curruns++; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1902 | } |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1903 | return (run); |
| 1904 | } |
| 1905 | |
| 1906 | /* |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1907 | * arena_run_alloc_small() failed, but another thread may have made |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1908 | * sufficient memory available while this one dropped bin->lock above, |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1909 | * so search one more time. |
| 1910 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1911 | run = arena_bin_nonfull_run_tryget(bin); |
| 1912 | if (run != NULL) |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1913 | return (run); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1914 | |
| 1915 | return (NULL); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1916 | } |
| 1917 | |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 1918 | /* Re-fill bin->runcur, then call arena_run_reg_alloc(). */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1919 | static void * |
| 1920 | arena_bin_malloc_hard(arena_t *arena, arena_bin_t *bin) |
| 1921 | { |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1922 | void *ret; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1923 | index_t binind; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1924 | arena_bin_info_t *bin_info; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1925 | arena_run_t *run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1926 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1927 | binind = arena_bin_index(arena, bin); |
| 1928 | bin_info = &arena_bin_info[binind]; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1929 | bin->runcur = NULL; |
| 1930 | run = arena_bin_nonfull_run_get(arena, bin); |
| 1931 | if (bin->runcur != NULL && bin->runcur->nfree > 0) { |
| 1932 | /* |
| 1933 | * Another thread updated runcur while this one ran without the |
| 1934 | * bin lock in arena_bin_nonfull_run_get(). |
| 1935 | */ |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1936 | assert(bin->runcur->nfree > 0); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1937 | ret = arena_run_reg_alloc(bin->runcur, bin_info); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1938 | if (run != NULL) { |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1939 | arena_chunk_t *chunk; |
| 1940 | |
| 1941 | /* |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1942 | * arena_run_alloc_small() may have allocated run, or |
| 1943 | * it may have pulled run from the bin's run tree. |
| 1944 | * Therefore it is unsafe to make any assumptions about |
| 1945 | * how run has previously been used, and |
| 1946 | * arena_bin_lower_run() must be called, as if a region |
| 1947 | * were just deallocated from the run. |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1948 | */ |
| 1949 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1950 | if (run->nfree == bin_info->nregs) |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1951 | arena_dalloc_bin_run(arena, chunk, run, bin); |
| 1952 | else |
| 1953 | arena_bin_lower_run(arena, chunk, run, bin); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1954 | } |
| 1955 | return (ret); |
| 1956 | } |
| 1957 | |
| 1958 | if (run == NULL) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1959 | return (NULL); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1960 | |
| 1961 | bin->runcur = run; |
| 1962 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1963 | assert(bin->runcur->nfree > 0); |
| 1964 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1965 | return (arena_run_reg_alloc(bin->runcur, bin_info)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1968 | void |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1969 | arena_tcache_fill_small(arena_t *arena, tcache_bin_t *tbin, index_t binind, |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1970 | uint64_t prof_accumbytes) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1971 | { |
| 1972 | unsigned i, nfill; |
| 1973 | arena_bin_t *bin; |
| 1974 | arena_run_t *run; |
| 1975 | void *ptr; |
| 1976 | |
| 1977 | assert(tbin->ncached == 0); |
| 1978 | |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 1979 | if (config_prof && arena_prof_accum(arena, prof_accumbytes)) |
| 1980 | prof_idump(); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 1981 | bin = &arena->bins[binind]; |
| 1982 | malloc_mutex_lock(&bin->lock); |
Jason Evans | 1dcb4f8 | 2011-03-21 00:18:17 -0700 | [diff] [blame] | 1983 | for (i = 0, nfill = (tcache_bin_info[binind].ncached_max >> |
| 1984 | tbin->lg_fill_div); i < nfill; i++) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1985 | if ((run = bin->runcur) != NULL && run->nfree > 0) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1986 | ptr = arena_run_reg_alloc(run, &arena_bin_info[binind]); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1987 | else |
| 1988 | ptr = arena_bin_malloc_hard(arena, bin); |
Jason Evans | f11a677 | 2014-10-05 13:05:10 -0700 | [diff] [blame] | 1989 | if (ptr == NULL) { |
| 1990 | /* |
| 1991 | * OOM. tbin->avail isn't yet filled down to its first |
| 1992 | * element, so the successful allocations (if any) must |
| 1993 | * be moved to the base of tbin->avail before bailing |
| 1994 | * out. |
| 1995 | */ |
| 1996 | if (i > 0) { |
| 1997 | memmove(tbin->avail, &tbin->avail[nfill - i], |
| 1998 | i * sizeof(void *)); |
| 1999 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2000 | break; |
Jason Evans | f11a677 | 2014-10-05 13:05:10 -0700 | [diff] [blame] | 2001 | } |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2002 | if (config_fill && unlikely(opt_junk_alloc)) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2003 | arena_alloc_junk_small(ptr, &arena_bin_info[binind], |
| 2004 | true); |
| 2005 | } |
Jason Evans | 9c43c13 | 2011-03-18 10:53:15 -0700 | [diff] [blame] | 2006 | /* Insert such that low regions get used first. */ |
| 2007 | tbin->avail[nfill - 1 - i] = ptr; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2008 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2009 | if (config_stats) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2010 | bin->stats.nmalloc += i; |
| 2011 | bin->stats.nrequests += tbin->tstats.nrequests; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2012 | bin->stats.curregs += i; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2013 | bin->stats.nfills++; |
| 2014 | tbin->tstats.nrequests = 0; |
| 2015 | } |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2016 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2017 | tbin->ncached = i; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2018 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2019 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2020 | void |
| 2021 | arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info, bool zero) |
| 2022 | { |
| 2023 | |
| 2024 | if (zero) { |
| 2025 | size_t redzone_size = bin_info->redzone_size; |
| 2026 | memset((void *)((uintptr_t)ptr - redzone_size), 0xa5, |
| 2027 | redzone_size); |
| 2028 | memset((void *)((uintptr_t)ptr + bin_info->reg_size), 0xa5, |
| 2029 | redzone_size); |
| 2030 | } else { |
| 2031 | memset((void *)((uintptr_t)ptr - bin_info->redzone_size), 0xa5, |
| 2032 | bin_info->reg_interval); |
| 2033 | } |
| 2034 | } |
| 2035 | |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2036 | #ifdef JEMALLOC_JET |
| 2037 | #undef arena_redzone_corruption |
| 2038 | #define arena_redzone_corruption JEMALLOC_N(arena_redzone_corruption_impl) |
| 2039 | #endif |
| 2040 | static void |
| 2041 | arena_redzone_corruption(void *ptr, size_t usize, bool after, |
| 2042 | size_t offset, uint8_t byte) |
| 2043 | { |
| 2044 | |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 2045 | malloc_printf("<jemalloc>: Corrupt redzone %zu byte%s %s %p " |
| 2046 | "(size %zu), byte=%#x\n", offset, (offset == 1) ? "" : "s", |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2047 | after ? "after" : "before", ptr, usize, byte); |
| 2048 | } |
| 2049 | #ifdef JEMALLOC_JET |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2050 | #undef arena_redzone_corruption |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2051 | #define arena_redzone_corruption JEMALLOC_N(arena_redzone_corruption) |
| 2052 | arena_redzone_corruption_t *arena_redzone_corruption = |
| 2053 | JEMALLOC_N(arena_redzone_corruption_impl); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2054 | #endif |
| 2055 | |
| 2056 | static void |
| 2057 | arena_redzones_validate(void *ptr, arena_bin_info_t *bin_info, bool reset) |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2058 | { |
| 2059 | size_t size = bin_info->reg_size; |
| 2060 | size_t redzone_size = bin_info->redzone_size; |
| 2061 | size_t i; |
| 2062 | bool error = false; |
| 2063 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2064 | if (opt_junk_alloc) { |
| 2065 | for (i = 1; i <= redzone_size; i++) { |
| 2066 | uint8_t *byte = (uint8_t *)((uintptr_t)ptr - i); |
| 2067 | if (*byte != 0xa5) { |
| 2068 | error = true; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2069 | arena_redzone_corruption(ptr, size, false, i, |
| 2070 | *byte); |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2071 | if (reset) |
| 2072 | *byte = 0xa5; |
| 2073 | } |
| 2074 | } |
| 2075 | for (i = 0; i < redzone_size; i++) { |
| 2076 | uint8_t *byte = (uint8_t *)((uintptr_t)ptr + size + i); |
| 2077 | if (*byte != 0xa5) { |
| 2078 | error = true; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2079 | arena_redzone_corruption(ptr, size, true, i, |
| 2080 | *byte); |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2081 | if (reset) |
| 2082 | *byte = 0xa5; |
| 2083 | } |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2084 | } |
| 2085 | } |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2086 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2087 | if (opt_abort && error) |
| 2088 | abort(); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2089 | } |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2090 | |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2091 | #ifdef JEMALLOC_JET |
| 2092 | #undef arena_dalloc_junk_small |
| 2093 | #define arena_dalloc_junk_small JEMALLOC_N(arena_dalloc_junk_small_impl) |
| 2094 | #endif |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2095 | void |
| 2096 | arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info) |
| 2097 | { |
| 2098 | size_t redzone_size = bin_info->redzone_size; |
| 2099 | |
| 2100 | arena_redzones_validate(ptr, bin_info, false); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2101 | memset((void *)((uintptr_t)ptr - redzone_size), 0x5a, |
| 2102 | bin_info->reg_interval); |
| 2103 | } |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2104 | #ifdef JEMALLOC_JET |
| 2105 | #undef arena_dalloc_junk_small |
| 2106 | #define arena_dalloc_junk_small JEMALLOC_N(arena_dalloc_junk_small) |
| 2107 | arena_dalloc_junk_small_t *arena_dalloc_junk_small = |
| 2108 | JEMALLOC_N(arena_dalloc_junk_small_impl); |
| 2109 | #endif |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2110 | |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2111 | void |
| 2112 | arena_quarantine_junk_small(void *ptr, size_t usize) |
| 2113 | { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2114 | index_t binind; |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2115 | arena_bin_info_t *bin_info; |
| 2116 | cassert(config_fill); |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2117 | assert(opt_junk_free); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2118 | assert(opt_quarantine); |
| 2119 | assert(usize <= SMALL_MAXCLASS); |
| 2120 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2121 | binind = size2index(usize); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2122 | bin_info = &arena_bin_info[binind]; |
| 2123 | arena_redzones_validate(ptr, bin_info, true); |
| 2124 | } |
| 2125 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2126 | void * |
| 2127 | arena_malloc_small(arena_t *arena, size_t size, bool zero) |
| 2128 | { |
| 2129 | void *ret; |
| 2130 | arena_bin_t *bin; |
| 2131 | arena_run_t *run; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2132 | index_t binind; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2133 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2134 | binind = size2index(size); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2135 | assert(binind < NBINS); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2136 | bin = &arena->bins[binind]; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2137 | size = index2size(binind); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2138 | |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2139 | malloc_mutex_lock(&bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2140 | if ((run = bin->runcur) != NULL && run->nfree > 0) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2141 | ret = arena_run_reg_alloc(run, &arena_bin_info[binind]); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2142 | else |
| 2143 | ret = arena_bin_malloc_hard(arena, bin); |
| 2144 | |
| 2145 | if (ret == NULL) { |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2146 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2147 | return (NULL); |
| 2148 | } |
| 2149 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2150 | if (config_stats) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2151 | bin->stats.nmalloc++; |
| 2152 | bin->stats.nrequests++; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2153 | bin->stats.curregs++; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2154 | } |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2155 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2156 | if (config_prof && !isthreaded && arena_prof_accum(arena, size)) |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 2157 | prof_idump(); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2158 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2159 | if (!zero) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2160 | if (config_fill) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2161 | if (unlikely(opt_junk_alloc)) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2162 | arena_alloc_junk_small(ret, |
| 2163 | &arena_bin_info[binind], false); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 2164 | } else if (unlikely(opt_zero)) |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2165 | memset(ret, 0, size); |
| 2166 | } |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 2167 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, size); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2168 | } else { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2169 | if (config_fill && unlikely(opt_junk_alloc)) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2170 | arena_alloc_junk_small(ret, &arena_bin_info[binind], |
| 2171 | true); |
| 2172 | } |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 2173 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2174 | memset(ret, 0, size); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2175 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2176 | |
| 2177 | return (ret); |
| 2178 | } |
| 2179 | |
| 2180 | void * |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2181 | arena_malloc_large(arena_t *arena, size_t size, bool zero) |
| 2182 | { |
| 2183 | void *ret; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2184 | size_t usize; |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2185 | uintptr_t random_offset; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2186 | arena_run_t *run; |
| 2187 | arena_chunk_map_misc_t *miscelm; |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 2188 | UNUSED bool idump; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2189 | |
| 2190 | /* Large allocation. */ |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2191 | usize = s2u(size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2192 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2193 | if (config_cache_oblivious) { |
Jason Evans | bce61d6 | 2015-07-07 09:32:05 -0700 | [diff] [blame] | 2194 | uint64_t r; |
| 2195 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2196 | /* |
| 2197 | * Compute a uniformly distributed offset within the first page |
| 2198 | * that is a multiple of the cacheline size, e.g. [0 .. 63) * 64 |
| 2199 | * for 4 KiB pages and 64-byte cachelines. |
| 2200 | */ |
| 2201 | prng64(r, LG_PAGE - LG_CACHELINE, arena->offset_state, |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 2202 | UINT64_C(6364136223846793009), |
| 2203 | UINT64_C(1442695040888963409)); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2204 | random_offset = ((uintptr_t)r) << LG_CACHELINE; |
| 2205 | } else |
| 2206 | random_offset = 0; |
| 2207 | run = arena_run_alloc_large(arena, usize + large_pad, zero); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2208 | if (run == NULL) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2209 | malloc_mutex_unlock(&arena->lock); |
| 2210 | return (NULL); |
| 2211 | } |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2212 | miscelm = arena_run_to_miscelm(run); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2213 | ret = (void *)((uintptr_t)arena_miscelm_to_rpages(miscelm) + |
| 2214 | random_offset); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2215 | if (config_stats) { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2216 | index_t index = size2index(usize) - NBINS; |
| 2217 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2218 | arena->stats.nmalloc_large++; |
| 2219 | arena->stats.nrequests_large++; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2220 | arena->stats.allocated_large += usize; |
| 2221 | arena->stats.lstats[index].nmalloc++; |
| 2222 | arena->stats.lstats[index].nrequests++; |
| 2223 | arena->stats.lstats[index].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2224 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2225 | if (config_prof) |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2226 | idump = arena_prof_accum_locked(arena, usize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2227 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 2228 | if (config_prof && idump) |
| 2229 | prof_idump(); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2230 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2231 | if (!zero) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2232 | if (config_fill) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2233 | if (unlikely(opt_junk_alloc)) |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2234 | memset(ret, 0xa5, usize); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 2235 | else if (unlikely(opt_zero)) |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2236 | memset(ret, 0, usize); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2237 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | return (ret); |
| 2241 | } |
| 2242 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2243 | /* Only handles large allocations that require more than page alignment. */ |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2244 | static void * |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2245 | arena_palloc_large(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment, |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2246 | bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2247 | { |
| 2248 | void *ret; |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2249 | size_t alloc_size, leadsize, trailsize; |
| 2250 | arena_run_t *run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2251 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2252 | arena_chunk_map_misc_t *miscelm; |
| 2253 | void *rpages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2254 | |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2255 | assert(usize == PAGE_CEILING(usize)); |
Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 2256 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2257 | arena = arena_choose(tsd, arena); |
| 2258 | if (unlikely(arena == NULL)) |
| 2259 | return (NULL); |
| 2260 | |
Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 2261 | alignment = PAGE_CEILING(alignment); |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2262 | alloc_size = usize + large_pad + alignment - PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2263 | |
| 2264 | malloc_mutex_lock(&arena->lock); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2265 | run = arena_run_alloc_large(arena, alloc_size, false); |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2266 | if (run == NULL) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2267 | malloc_mutex_unlock(&arena->lock); |
| 2268 | return (NULL); |
| 2269 | } |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2270 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2271 | miscelm = arena_run_to_miscelm(run); |
| 2272 | rpages = arena_miscelm_to_rpages(miscelm); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2273 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2274 | leadsize = ALIGNMENT_CEILING((uintptr_t)rpages, alignment) - |
| 2275 | (uintptr_t)rpages; |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2276 | assert(alloc_size >= leadsize + usize); |
| 2277 | trailsize = alloc_size - leadsize - usize - large_pad; |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2278 | if (leadsize != 0) { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2279 | arena_chunk_map_misc_t *head_miscelm = miscelm; |
| 2280 | arena_run_t *head_run = run; |
| 2281 | |
| 2282 | miscelm = arena_miscelm_get(chunk, |
| 2283 | arena_miscelm_to_pageind(head_miscelm) + (leadsize >> |
| 2284 | LG_PAGE)); |
| 2285 | run = &miscelm->run; |
| 2286 | |
| 2287 | arena_run_trim_head(arena, chunk, head_run, alloc_size, |
| 2288 | alloc_size - leadsize); |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2289 | } |
| 2290 | if (trailsize != 0) { |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2291 | arena_run_trim_tail(arena, chunk, run, usize + large_pad + |
| 2292 | trailsize, usize + large_pad, false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2293 | } |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2294 | if (arena_run_init_large(arena, run, usize + large_pad, zero)) { |
| 2295 | size_t run_ind = |
| 2296 | arena_miscelm_to_pageind(arena_run_to_miscelm(run)); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 2297 | bool dirty = (arena_mapbits_dirty_get(chunk, run_ind) != 0); |
| 2298 | bool decommitted = (arena_mapbits_decommitted_get(chunk, |
| 2299 | run_ind) != 0); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2300 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 2301 | assert(decommitted); /* Cause of OOM. */ |
| 2302 | arena_run_dalloc(arena, run, dirty, false, decommitted); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2303 | malloc_mutex_unlock(&arena->lock); |
| 2304 | return (NULL); |
| 2305 | } |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2306 | ret = arena_miscelm_to_rpages(miscelm); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2307 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2308 | if (config_stats) { |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2309 | index_t index = size2index(usize) - NBINS; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2310 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2311 | arena->stats.nmalloc_large++; |
| 2312 | arena->stats.nrequests_large++; |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2313 | arena->stats.allocated_large += usize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2314 | arena->stats.lstats[index].nmalloc++; |
| 2315 | arena->stats.lstats[index].nrequests++; |
| 2316 | arena->stats.lstats[index].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2317 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2318 | malloc_mutex_unlock(&arena->lock); |
| 2319 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2320 | if (config_fill && !zero) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2321 | if (unlikely(opt_junk_alloc)) |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2322 | memset(ret, 0xa5, usize); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 2323 | else if (unlikely(opt_zero)) |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2324 | memset(ret, 0, usize); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2325 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2326 | return (ret); |
| 2327 | } |
| 2328 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2329 | void * |
| 2330 | arena_palloc(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment, |
| 2331 | bool zero, tcache_t *tcache) |
| 2332 | { |
| 2333 | void *ret; |
| 2334 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2335 | if (usize <= SMALL_MAXCLASS && (alignment < PAGE || (alignment == PAGE |
Jason Evans | 5154175 | 2015-05-19 17:42:31 -0700 | [diff] [blame] | 2336 | && (usize & PAGE_MASK) == 0))) { |
| 2337 | /* Small; alignment doesn't require special run placement. */ |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2338 | ret = arena_malloc(tsd, arena, usize, zero, tcache); |
Jason Evans | 5154175 | 2015-05-19 17:42:31 -0700 | [diff] [blame] | 2339 | } else if (usize <= arena_maxclass && alignment <= PAGE) { |
| 2340 | /* |
| 2341 | * Large; alignment doesn't require special run placement. |
| 2342 | * However, the cached pointer may be at a random offset from |
| 2343 | * the base of the run, so do some bit manipulation to retrieve |
| 2344 | * the base. |
| 2345 | */ |
| 2346 | ret = arena_malloc(tsd, arena, usize, zero, tcache); |
| 2347 | if (config_cache_oblivious) |
| 2348 | ret = (void *)((uintptr_t)ret & ~PAGE_MASK); |
| 2349 | } else { |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2350 | if (likely(usize <= arena_maxclass)) { |
| 2351 | ret = arena_palloc_large(tsd, arena, usize, alignment, |
| 2352 | zero); |
| 2353 | } else if (likely(alignment <= chunksize)) |
| 2354 | ret = huge_malloc(tsd, arena, usize, zero, tcache); |
| 2355 | else { |
| 2356 | ret = huge_palloc(tsd, arena, usize, alignment, zero, |
| 2357 | tcache); |
| 2358 | } |
| 2359 | } |
| 2360 | return (ret); |
| 2361 | } |
| 2362 | |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2363 | void |
| 2364 | arena_prof_promoted(const void *ptr, size_t size) |
| 2365 | { |
| 2366 | arena_chunk_t *chunk; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2367 | size_t pageind; |
| 2368 | index_t binind; |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2369 | |
Jason Evans | 78f7352 | 2012-04-18 13:38:40 -0700 | [diff] [blame] | 2370 | cassert(config_prof); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2371 | assert(ptr != NULL); |
| 2372 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2373 | assert(isalloc(ptr, false) == LARGE_MINCLASS); |
| 2374 | assert(isalloc(ptr, true) == LARGE_MINCLASS); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2375 | assert(size <= SMALL_MAXCLASS); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2376 | |
| 2377 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2378 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2379 | binind = size2index(size); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2380 | assert(binind < NBINS); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2381 | arena_mapbits_large_binind_set(chunk, pageind, binind); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2382 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2383 | assert(isalloc(ptr, false) == LARGE_MINCLASS); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2384 | assert(isalloc(ptr, true) == size); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2385 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2386 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2387 | static void |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2388 | arena_dissociate_bin_run(arena_chunk_t *chunk, arena_run_t *run, |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2389 | arena_bin_t *bin) |
| 2390 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2391 | |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 2392 | /* Dissociate run from bin. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2393 | if (run == bin->runcur) |
| 2394 | bin->runcur = NULL; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2395 | else { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 2396 | index_t binind = arena_bin_index(extent_node_arena_get( |
| 2397 | &chunk->node), bin); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2398 | arena_bin_info_t *bin_info = &arena_bin_info[binind]; |
| 2399 | |
| 2400 | if (bin_info->nregs != 1) { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2401 | /* |
| 2402 | * This block's conditional is necessary because if the |
| 2403 | * run only contains one region, then it never gets |
| 2404 | * inserted into the non-full runs tree. |
| 2405 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2406 | arena_bin_runs_remove(bin, run); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2407 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2408 | } |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2409 | } |
| 2410 | |
| 2411 | static void |
| 2412 | arena_dalloc_bin_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 2413 | arena_bin_t *bin) |
| 2414 | { |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2415 | |
| 2416 | assert(run != bin->runcur); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2417 | assert(arena_run_tree_search(&bin->runs, arena_run_to_miscelm(run)) == |
| 2418 | NULL); |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2419 | |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2420 | malloc_mutex_unlock(&bin->lock); |
| 2421 | /******************************/ |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2422 | malloc_mutex_lock(&arena->lock); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 2423 | arena_run_dalloc_decommit(arena, chunk, run); |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2424 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2425 | /****************************/ |
| 2426 | malloc_mutex_lock(&bin->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2427 | if (config_stats) |
| 2428 | bin->stats.curruns--; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2431 | static void |
| 2432 | arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 2433 | arena_bin_t *bin) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2434 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2435 | |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2436 | /* |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2437 | * Make sure that if bin->runcur is non-NULL, it refers to the lowest |
| 2438 | * non-full run. It is okay to NULL runcur out rather than proactively |
| 2439 | * keeping it pointing at the lowest non-full run. |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2440 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2441 | if ((uintptr_t)run < (uintptr_t)bin->runcur) { |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2442 | /* Switch runcur. */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2443 | if (bin->runcur->nfree > 0) |
| 2444 | arena_bin_runs_insert(bin, bin->runcur); |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2445 | bin->runcur = run; |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2446 | if (config_stats) |
| 2447 | bin->stats.reruns++; |
| 2448 | } else |
| 2449 | arena_bin_runs_insert(bin, run); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2452 | static void |
| 2453 | arena_dalloc_bin_locked_impl(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
| 2454 | arena_chunk_map_bits_t *bitselm, bool junked) |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2455 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2456 | size_t pageind, rpages_ind; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2457 | arena_run_t *run; |
| 2458 | arena_bin_t *bin; |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 2459 | arena_bin_info_t *bin_info; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2460 | index_t binind; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2461 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2462 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2463 | rpages_ind = pageind - arena_mapbits_small_runind_get(chunk, pageind); |
| 2464 | run = &arena_miscelm_get(chunk, rpages_ind)->run; |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 2465 | binind = run->binind; |
| 2466 | bin = &arena->bins[binind]; |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 2467 | bin_info = &arena_bin_info[binind]; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2468 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2469 | if (!junked && config_fill && unlikely(opt_junk_free)) |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2470 | arena_dalloc_junk_small(ptr, bin_info); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2471 | |
| 2472 | arena_run_reg_dalloc(run, ptr); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2473 | if (run->nfree == bin_info->nregs) { |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2474 | arena_dissociate_bin_run(chunk, run, bin); |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2475 | arena_dalloc_bin_run(arena, chunk, run, bin); |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2476 | } else if (run->nfree == 1 && run != bin->runcur) |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2477 | arena_bin_lower_run(arena, chunk, run, bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2478 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2479 | if (config_stats) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2480 | bin->stats.ndalloc++; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2481 | bin->stats.curregs--; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2482 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2483 | } |
| 2484 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2485 | void |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2486 | arena_dalloc_bin_junked_locked(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
| 2487 | arena_chunk_map_bits_t *bitselm) |
| 2488 | { |
| 2489 | |
| 2490 | arena_dalloc_bin_locked_impl(arena, chunk, ptr, bitselm, true); |
| 2491 | } |
| 2492 | |
| 2493 | void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2494 | arena_dalloc_bin(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 2495 | size_t pageind, arena_chunk_map_bits_t *bitselm) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2496 | { |
| 2497 | arena_run_t *run; |
| 2498 | arena_bin_t *bin; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2499 | size_t rpages_ind; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2500 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2501 | rpages_ind = pageind - arena_mapbits_small_runind_get(chunk, pageind); |
| 2502 | run = &arena_miscelm_get(chunk, rpages_ind)->run; |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 2503 | bin = &arena->bins[run->binind]; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2504 | malloc_mutex_lock(&bin->lock); |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2505 | arena_dalloc_bin_locked_impl(arena, chunk, ptr, bitselm, false); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2506 | malloc_mutex_unlock(&bin->lock); |
| 2507 | } |
| 2508 | |
| 2509 | void |
| 2510 | arena_dalloc_small(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
| 2511 | size_t pageind) |
| 2512 | { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 2513 | arena_chunk_map_bits_t *bitselm; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2514 | |
| 2515 | if (config_debug) { |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 2516 | /* arena_ptr_small_binind_get() does extra sanity checking. */ |
| 2517 | assert(arena_ptr_small_binind_get(ptr, arena_mapbits_get(chunk, |
| 2518 | pageind)) != BININD_INVALID); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2519 | } |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 2520 | bitselm = arena_bitselm_get(chunk, pageind); |
| 2521 | arena_dalloc_bin(arena, chunk, ptr, pageind, bitselm); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2522 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2523 | |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2524 | #ifdef JEMALLOC_JET |
| 2525 | #undef arena_dalloc_junk_large |
| 2526 | #define arena_dalloc_junk_large JEMALLOC_N(arena_dalloc_junk_large_impl) |
| 2527 | #endif |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2528 | void |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2529 | arena_dalloc_junk_large(void *ptr, size_t usize) |
| 2530 | { |
| 2531 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2532 | if (config_fill && unlikely(opt_junk_free)) |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2533 | memset(ptr, 0x5a, usize); |
| 2534 | } |
| 2535 | #ifdef JEMALLOC_JET |
| 2536 | #undef arena_dalloc_junk_large |
| 2537 | #define arena_dalloc_junk_large JEMALLOC_N(arena_dalloc_junk_large) |
| 2538 | arena_dalloc_junk_large_t *arena_dalloc_junk_large = |
| 2539 | JEMALLOC_N(arena_dalloc_junk_large_impl); |
| 2540 | #endif |
| 2541 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2542 | void |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2543 | arena_dalloc_large_locked_impl(arena_t *arena, arena_chunk_t *chunk, |
| 2544 | void *ptr, bool junked) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2545 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2546 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
| 2547 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get(chunk, pageind); |
| 2548 | arena_run_t *run = &miscelm->run; |
Jason Evans | 1366826 | 2010-01-31 03:57:29 -0800 | [diff] [blame] | 2549 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2550 | if (config_fill || config_stats) { |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2551 | size_t usize = arena_mapbits_large_size_get(chunk, pageind) - |
| 2552 | large_pad; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2553 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2554 | if (!junked) |
| 2555 | arena_dalloc_junk_large(ptr, usize); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2556 | if (config_stats) { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2557 | index_t index = size2index(usize) - NBINS; |
| 2558 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2559 | arena->stats.ndalloc_large++; |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2560 | arena->stats.allocated_large -= usize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2561 | arena->stats.lstats[index].ndalloc++; |
| 2562 | arena->stats.lstats[index].curruns--; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2563 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2564 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2565 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 2566 | arena_run_dalloc_decommit(arena, chunk, run); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2567 | } |
| 2568 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2569 | void |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2570 | arena_dalloc_large_junked_locked(arena_t *arena, arena_chunk_t *chunk, |
| 2571 | void *ptr) |
| 2572 | { |
| 2573 | |
| 2574 | arena_dalloc_large_locked_impl(arena, chunk, ptr, true); |
| 2575 | } |
| 2576 | |
| 2577 | void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2578 | arena_dalloc_large(arena_t *arena, arena_chunk_t *chunk, void *ptr) |
| 2579 | { |
| 2580 | |
| 2581 | malloc_mutex_lock(&arena->lock); |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2582 | arena_dalloc_large_locked_impl(arena, chunk, ptr, false); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2583 | malloc_mutex_unlock(&arena->lock); |
| 2584 | } |
| 2585 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2586 | static void |
| 2587 | arena_ralloc_large_shrink(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2588 | size_t oldsize, size_t size) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2589 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2590 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
| 2591 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get(chunk, pageind); |
| 2592 | arena_run_t *run = &miscelm->run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2593 | |
| 2594 | assert(size < oldsize); |
| 2595 | |
| 2596 | /* |
| 2597 | * Shrink the run, and make trailing pages available for other |
| 2598 | * allocations. |
| 2599 | */ |
| 2600 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2601 | arena_run_trim_tail(arena, chunk, run, oldsize + large_pad, size + |
| 2602 | large_pad, true); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2603 | if (config_stats) { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2604 | index_t oldindex = size2index(oldsize) - NBINS; |
| 2605 | index_t index = size2index(size) - NBINS; |
| 2606 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2607 | arena->stats.ndalloc_large++; |
| 2608 | arena->stats.allocated_large -= oldsize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2609 | arena->stats.lstats[oldindex].ndalloc++; |
| 2610 | arena->stats.lstats[oldindex].curruns--; |
Jason Evans | 990d10c | 2010-01-31 03:49:35 -0800 | [diff] [blame] | 2611 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2612 | arena->stats.nmalloc_large++; |
| 2613 | arena->stats.nrequests_large++; |
| 2614 | arena->stats.allocated_large += size; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2615 | arena->stats.lstats[index].nmalloc++; |
| 2616 | arena->stats.lstats[index].nrequests++; |
| 2617 | arena->stats.lstats[index].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2618 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2619 | malloc_mutex_unlock(&arena->lock); |
| 2620 | } |
| 2621 | |
| 2622 | static bool |
| 2623 | arena_ralloc_large_grow(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2624 | size_t oldsize, size_t size, size_t extra, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2625 | { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2626 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 2627 | size_t npages = (oldsize + large_pad) >> LG_PAGE; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2628 | size_t followsize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2629 | size_t usize_min = s2u(size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2630 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2631 | assert(oldsize == arena_mapbits_large_size_get(chunk, pageind) - |
| 2632 | large_pad); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2633 | |
| 2634 | /* Try to extend the run. */ |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2635 | assert(usize_min > oldsize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2636 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 2637 | if (pageind+npages < chunk_npages && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2638 | arena_mapbits_allocated_get(chunk, pageind+npages) == 0 && |
| 2639 | (followsize = arena_mapbits_unallocated_size_get(chunk, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2640 | pageind+npages)) >= usize_min - oldsize) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2641 | /* |
| 2642 | * The next run is available and sufficiently large. Split the |
| 2643 | * following run, then merge the first part with the existing |
| 2644 | * allocation. |
| 2645 | */ |
Guilherme Goncalves | 9c6a8d3 | 2014-12-17 14:46:35 -0200 | [diff] [blame] | 2646 | arena_run_t *run; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2647 | size_t flag_dirty, splitsize, usize; |
| 2648 | |
| 2649 | usize = s2u(size + extra); |
| 2650 | while (oldsize + followsize < usize) |
| 2651 | usize = index2size(size2index(usize)-1); |
| 2652 | assert(usize >= usize_min); |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 2653 | splitsize = usize - oldsize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2654 | |
Guilherme Goncalves | 9c6a8d3 | 2014-12-17 14:46:35 -0200 | [diff] [blame] | 2655 | run = &arena_miscelm_get(chunk, pageind+npages)->run; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2656 | if (arena_run_split_large(arena, run, splitsize, zero)) { |
| 2657 | malloc_mutex_unlock(&arena->lock); |
| 2658 | return (true); |
| 2659 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2660 | |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2661 | size = oldsize + splitsize; |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 2662 | npages = (size + large_pad) >> LG_PAGE; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2663 | |
| 2664 | /* |
| 2665 | * Mark the extended run as dirty if either portion of the run |
| 2666 | * was dirty before allocation. This is rather pedantic, |
| 2667 | * because there's not actually any sequence of events that |
| 2668 | * could cause the resulting run to be passed to |
| 2669 | * arena_run_dalloc() with the dirty argument set to false |
| 2670 | * (which is when dirty flag consistency would really matter). |
| 2671 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2672 | flag_dirty = arena_mapbits_dirty_get(chunk, pageind) | |
| 2673 | arena_mapbits_dirty_get(chunk, pageind+npages-1); |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 2674 | arena_mapbits_large_set(chunk, pageind, size + large_pad, |
| 2675 | flag_dirty); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2676 | arena_mapbits_large_set(chunk, pageind+npages-1, 0, flag_dirty); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2677 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2678 | if (config_stats) { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2679 | index_t oldindex = size2index(oldsize) - NBINS; |
| 2680 | index_t index = size2index(size) - NBINS; |
| 2681 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2682 | arena->stats.ndalloc_large++; |
| 2683 | arena->stats.allocated_large -= oldsize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2684 | arena->stats.lstats[oldindex].ndalloc++; |
| 2685 | arena->stats.lstats[oldindex].curruns--; |
Jason Evans | 990d10c | 2010-01-31 03:49:35 -0800 | [diff] [blame] | 2686 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2687 | arena->stats.nmalloc_large++; |
| 2688 | arena->stats.nrequests_large++; |
| 2689 | arena->stats.allocated_large += size; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2690 | arena->stats.lstats[index].nmalloc++; |
| 2691 | arena->stats.lstats[index].nrequests++; |
| 2692 | arena->stats.lstats[index].curruns++; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2693 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2694 | malloc_mutex_unlock(&arena->lock); |
| 2695 | return (false); |
| 2696 | } |
| 2697 | malloc_mutex_unlock(&arena->lock); |
| 2698 | |
| 2699 | return (true); |
| 2700 | } |
| 2701 | |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2702 | #ifdef JEMALLOC_JET |
| 2703 | #undef arena_ralloc_junk_large |
| 2704 | #define arena_ralloc_junk_large JEMALLOC_N(arena_ralloc_junk_large_impl) |
| 2705 | #endif |
| 2706 | static void |
| 2707 | arena_ralloc_junk_large(void *ptr, size_t old_usize, size_t usize) |
| 2708 | { |
| 2709 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2710 | if (config_fill && unlikely(opt_junk_free)) { |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2711 | memset((void *)((uintptr_t)ptr + usize), 0x5a, |
| 2712 | old_usize - usize); |
| 2713 | } |
| 2714 | } |
| 2715 | #ifdef JEMALLOC_JET |
| 2716 | #undef arena_ralloc_junk_large |
| 2717 | #define arena_ralloc_junk_large JEMALLOC_N(arena_ralloc_junk_large) |
| 2718 | arena_ralloc_junk_large_t *arena_ralloc_junk_large = |
| 2719 | JEMALLOC_N(arena_ralloc_junk_large_impl); |
| 2720 | #endif |
| 2721 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2722 | /* |
| 2723 | * Try to resize a large allocation, in order to avoid copying. This will |
| 2724 | * always fail if growing an object, and the following run is already in use. |
| 2725 | */ |
| 2726 | static bool |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2727 | arena_ralloc_large(void *ptr, size_t oldsize, size_t size, size_t extra, |
| 2728 | bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2729 | { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2730 | size_t usize; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2731 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2732 | /* Make sure extra can't cause size_t overflow. */ |
Daniel Micay | 809b0ac | 2014-10-23 10:30:52 -0400 | [diff] [blame] | 2733 | if (unlikely(extra >= arena_maxclass)) |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2734 | return (true); |
| 2735 | |
| 2736 | usize = s2u(size + extra); |
| 2737 | if (usize == oldsize) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2738 | /* Same size class. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2739 | return (false); |
| 2740 | } else { |
| 2741 | arena_chunk_t *chunk; |
| 2742 | arena_t *arena; |
| 2743 | |
| 2744 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 2745 | arena = extent_node_arena_get(&chunk->node); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2746 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2747 | if (usize < oldsize) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2748 | /* Fill before shrinking in order avoid a race. */ |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2749 | arena_ralloc_junk_large(ptr, oldsize, usize); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2750 | arena_ralloc_large_shrink(arena, chunk, ptr, oldsize, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2751 | usize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2752 | return (false); |
| 2753 | } else { |
| 2754 | bool ret = arena_ralloc_large_grow(arena, chunk, ptr, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2755 | oldsize, size, extra, zero); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2756 | if (config_fill && !ret && !zero) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2757 | if (unlikely(opt_junk_alloc)) { |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2758 | memset((void *)((uintptr_t)ptr + |
| 2759 | oldsize), 0xa5, isalloc(ptr, |
| 2760 | config_prof) - oldsize); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 2761 | } else if (unlikely(opt_zero)) { |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2762 | memset((void *)((uintptr_t)ptr + |
| 2763 | oldsize), 0, isalloc(ptr, |
| 2764 | config_prof) - oldsize); |
| 2765 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2766 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2767 | return (ret); |
| 2768 | } |
| 2769 | } |
| 2770 | } |
| 2771 | |
Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 2772 | bool |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2773 | arena_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra, |
| 2774 | bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2775 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2776 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2777 | if (likely(size <= arena_maxclass)) { |
| 2778 | /* |
| 2779 | * Avoid moving the allocation if the size class can be left the |
| 2780 | * same. |
| 2781 | */ |
| 2782 | if (likely(oldsize <= arena_maxclass)) { |
| 2783 | if (oldsize <= SMALL_MAXCLASS) { |
| 2784 | assert( |
| 2785 | arena_bin_info[size2index(oldsize)].reg_size |
| 2786 | == oldsize); |
| 2787 | if ((size + extra <= SMALL_MAXCLASS && |
| 2788 | size2index(size + extra) == |
| 2789 | size2index(oldsize)) || (size <= oldsize && |
| 2790 | size + extra >= oldsize)) |
Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 2791 | return (false); |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2792 | } else { |
| 2793 | assert(size <= arena_maxclass); |
| 2794 | if (size + extra > SMALL_MAXCLASS) { |
| 2795 | if (!arena_ralloc_large(ptr, oldsize, |
| 2796 | size, extra, zero)) |
| 2797 | return (false); |
| 2798 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2799 | } |
| 2800 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2801 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2802 | /* Reallocation would require a move. */ |
| 2803 | return (true); |
| 2804 | } else |
| 2805 | return (huge_ralloc_no_move(ptr, oldsize, size, extra, zero)); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2806 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2807 | |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2808 | void * |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 2809 | arena_ralloc(tsd_t *tsd, arena_t *arena, void *ptr, size_t oldsize, size_t size, |
Jason Evans | 1cb181e | 2015-01-29 15:30:47 -0800 | [diff] [blame] | 2810 | size_t extra, size_t alignment, bool zero, tcache_t *tcache) |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2811 | { |
| 2812 | void *ret; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2813 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2814 | if (likely(size <= arena_maxclass)) { |
| 2815 | size_t copysize; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2816 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2817 | /* Try to avoid moving the allocation. */ |
| 2818 | if (!arena_ralloc_no_move(ptr, oldsize, size, extra, zero)) |
| 2819 | return (ptr); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2820 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2821 | /* |
| 2822 | * size and oldsize are different enough that we need to move |
| 2823 | * the object. In that case, fall back to allocating new space |
| 2824 | * and copying. |
| 2825 | */ |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 2826 | if (alignment != 0) { |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2827 | size_t usize = sa2u(size + extra, alignment); |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 2828 | if (usize == 0) |
| 2829 | return (NULL); |
Jason Evans | 1cb181e | 2015-01-29 15:30:47 -0800 | [diff] [blame] | 2830 | ret = ipalloct(tsd, usize, alignment, zero, tcache, |
| 2831 | arena); |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2832 | } else { |
| 2833 | ret = arena_malloc(tsd, arena, size + extra, zero, |
| 2834 | tcache); |
| 2835 | } |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2836 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2837 | if (ret == NULL) { |
| 2838 | if (extra == 0) |
| 2839 | return (NULL); |
| 2840 | /* Try again, this time without extra. */ |
| 2841 | if (alignment != 0) { |
| 2842 | size_t usize = sa2u(size, alignment); |
| 2843 | if (usize == 0) |
| 2844 | return (NULL); |
| 2845 | ret = ipalloct(tsd, usize, alignment, zero, |
| 2846 | tcache, arena); |
| 2847 | } else { |
| 2848 | ret = arena_malloc(tsd, arena, size, zero, |
| 2849 | tcache); |
| 2850 | } |
| 2851 | |
| 2852 | if (ret == NULL) |
| 2853 | return (NULL); |
| 2854 | } |
| 2855 | |
| 2856 | /* |
| 2857 | * Junk/zero-filling were already done by |
| 2858 | * ipalloc()/arena_malloc(). |
| 2859 | */ |
| 2860 | |
| 2861 | /* |
| 2862 | * Copy at most size bytes (not size+extra), since the caller |
| 2863 | * has no expectation that the extra bytes will be reliably |
| 2864 | * preserved. |
| 2865 | */ |
| 2866 | copysize = (size < oldsize) ? size : oldsize; |
| 2867 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, copysize); |
| 2868 | memcpy(ret, ptr, copysize); |
| 2869 | isqalloc(tsd, ptr, oldsize, tcache); |
| 2870 | } else { |
| 2871 | ret = huge_ralloc(tsd, arena, ptr, oldsize, size, extra, |
| 2872 | alignment, zero, tcache); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2873 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2874 | return (ret); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2875 | } |
| 2876 | |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2877 | dss_prec_t |
| 2878 | arena_dss_prec_get(arena_t *arena) |
| 2879 | { |
| 2880 | dss_prec_t ret; |
| 2881 | |
| 2882 | malloc_mutex_lock(&arena->lock); |
| 2883 | ret = arena->dss_prec; |
| 2884 | malloc_mutex_unlock(&arena->lock); |
| 2885 | return (ret); |
| 2886 | } |
| 2887 | |
Jason Evans | 4d434ad | 2014-04-15 12:09:48 -0700 | [diff] [blame] | 2888 | bool |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2889 | arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec) |
| 2890 | { |
| 2891 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2892 | if (!have_dss) |
Jason Evans | 4d434ad | 2014-04-15 12:09:48 -0700 | [diff] [blame] | 2893 | return (dss_prec != dss_prec_disabled); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2894 | malloc_mutex_lock(&arena->lock); |
| 2895 | arena->dss_prec = dss_prec; |
| 2896 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 4d434ad | 2014-04-15 12:09:48 -0700 | [diff] [blame] | 2897 | return (false); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2898 | } |
| 2899 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 2900 | ssize_t |
| 2901 | arena_lg_dirty_mult_default_get(void) |
| 2902 | { |
| 2903 | |
| 2904 | return ((ssize_t)atomic_read_z((size_t *)&lg_dirty_mult_default)); |
| 2905 | } |
| 2906 | |
| 2907 | bool |
| 2908 | arena_lg_dirty_mult_default_set(ssize_t lg_dirty_mult) |
| 2909 | { |
| 2910 | |
| 2911 | if (!arena_lg_dirty_mult_valid(lg_dirty_mult)) |
| 2912 | return (true); |
| 2913 | atomic_write_z((size_t *)&lg_dirty_mult_default, (size_t)lg_dirty_mult); |
| 2914 | return (false); |
| 2915 | } |
| 2916 | |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2917 | void |
Jason Evans | 562d266 | 2015-03-24 16:36:12 -0700 | [diff] [blame] | 2918 | arena_stats_merge(arena_t *arena, const char **dss, ssize_t *lg_dirty_mult, |
| 2919 | size_t *nactive, size_t *ndirty, arena_stats_t *astats, |
| 2920 | malloc_bin_stats_t *bstats, malloc_large_stats_t *lstats, |
| 2921 | malloc_huge_stats_t *hstats) |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2922 | { |
| 2923 | unsigned i; |
| 2924 | |
| 2925 | malloc_mutex_lock(&arena->lock); |
| 2926 | *dss = dss_prec_names[arena->dss_prec]; |
Jason Evans | 562d266 | 2015-03-24 16:36:12 -0700 | [diff] [blame] | 2927 | *lg_dirty_mult = arena->lg_dirty_mult; |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2928 | *nactive += arena->nactive; |
| 2929 | *ndirty += arena->ndirty; |
| 2930 | |
| 2931 | astats->mapped += arena->stats.mapped; |
| 2932 | astats->npurge += arena->stats.npurge; |
| 2933 | astats->nmadvise += arena->stats.nmadvise; |
| 2934 | astats->purged += arena->stats.purged; |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 2935 | astats->metadata_mapped += arena->stats.metadata_mapped; |
| 2936 | astats->metadata_allocated += arena_metadata_allocated_get(arena); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2937 | astats->allocated_large += arena->stats.allocated_large; |
| 2938 | astats->nmalloc_large += arena->stats.nmalloc_large; |
| 2939 | astats->ndalloc_large += arena->stats.ndalloc_large; |
| 2940 | astats->nrequests_large += arena->stats.nrequests_large; |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 2941 | astats->allocated_huge += arena->stats.allocated_huge; |
| 2942 | astats->nmalloc_huge += arena->stats.nmalloc_huge; |
| 2943 | astats->ndalloc_huge += arena->stats.ndalloc_huge; |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2944 | |
| 2945 | for (i = 0; i < nlclasses; i++) { |
| 2946 | lstats[i].nmalloc += arena->stats.lstats[i].nmalloc; |
| 2947 | lstats[i].ndalloc += arena->stats.lstats[i].ndalloc; |
| 2948 | lstats[i].nrequests += arena->stats.lstats[i].nrequests; |
| 2949 | lstats[i].curruns += arena->stats.lstats[i].curruns; |
| 2950 | } |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2951 | |
| 2952 | for (i = 0; i < nhclasses; i++) { |
| 2953 | hstats[i].nmalloc += arena->stats.hstats[i].nmalloc; |
| 2954 | hstats[i].ndalloc += arena->stats.hstats[i].ndalloc; |
| 2955 | hstats[i].curhchunks += arena->stats.hstats[i].curhchunks; |
| 2956 | } |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2957 | malloc_mutex_unlock(&arena->lock); |
| 2958 | |
| 2959 | for (i = 0; i < NBINS; i++) { |
| 2960 | arena_bin_t *bin = &arena->bins[i]; |
| 2961 | |
| 2962 | malloc_mutex_lock(&bin->lock); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2963 | bstats[i].nmalloc += bin->stats.nmalloc; |
| 2964 | bstats[i].ndalloc += bin->stats.ndalloc; |
| 2965 | bstats[i].nrequests += bin->stats.nrequests; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2966 | bstats[i].curregs += bin->stats.curregs; |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2967 | if (config_tcache) { |
| 2968 | bstats[i].nfills += bin->stats.nfills; |
| 2969 | bstats[i].nflushes += bin->stats.nflushes; |
| 2970 | } |
| 2971 | bstats[i].nruns += bin->stats.nruns; |
| 2972 | bstats[i].reruns += bin->stats.reruns; |
| 2973 | bstats[i].curruns += bin->stats.curruns; |
| 2974 | malloc_mutex_unlock(&bin->lock); |
| 2975 | } |
| 2976 | } |
| 2977 | |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 2978 | arena_t * |
| 2979 | arena_new(unsigned ind) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2980 | { |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 2981 | arena_t *arena; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2982 | unsigned i; |
| 2983 | arena_bin_t *bin; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2984 | |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 2985 | /* |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2986 | * Allocate arena, arena->lstats, and arena->hstats contiguously, mainly |
| 2987 | * because there is no way to clean up if base_alloc() OOMs. |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 2988 | */ |
| 2989 | if (config_stats) { |
| 2990 | arena = (arena_t *)base_alloc(CACHELINE_CEILING(sizeof(arena_t)) |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2991 | + QUANTUM_CEILING(nlclasses * sizeof(malloc_large_stats_t) + |
| 2992 | nhclasses) * sizeof(malloc_huge_stats_t)); |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 2993 | } else |
| 2994 | arena = (arena_t *)base_alloc(sizeof(arena_t)); |
| 2995 | if (arena == NULL) |
| 2996 | return (NULL); |
| 2997 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2998 | arena->ind = ind; |
Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 2999 | arena->nthreads = 0; |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 3000 | if (malloc_mutex_init(&arena->lock)) |
| 3001 | return (NULL); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3002 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3003 | if (config_stats) { |
| 3004 | memset(&arena->stats, 0, sizeof(arena_stats_t)); |
Jason Evans | d49cb68 | 2014-11-17 10:31:59 -0800 | [diff] [blame] | 3005 | arena->stats.lstats = (malloc_large_stats_t *)((uintptr_t)arena |
| 3006 | + CACHELINE_CEILING(sizeof(arena_t))); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3007 | memset(arena->stats.lstats, 0, nlclasses * |
| 3008 | sizeof(malloc_large_stats_t)); |
Jason Evans | d49cb68 | 2014-11-17 10:31:59 -0800 | [diff] [blame] | 3009 | arena->stats.hstats = (malloc_huge_stats_t *)((uintptr_t)arena |
| 3010 | + CACHELINE_CEILING(sizeof(arena_t)) + |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3011 | QUANTUM_CEILING(nlclasses * sizeof(malloc_large_stats_t))); |
| 3012 | memset(arena->stats.hstats, 0, nhclasses * |
| 3013 | sizeof(malloc_huge_stats_t)); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3014 | if (config_tcache) |
| 3015 | ql_new(&arena->tcache_ql); |
| 3016 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3017 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3018 | if (config_prof) |
| 3019 | arena->prof_accumbytes = 0; |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 3020 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3021 | if (config_cache_oblivious) { |
| 3022 | /* |
| 3023 | * A nondeterministic seed based on the address of arena reduces |
| 3024 | * the likelihood of lockstep non-uniform cache index |
| 3025 | * utilization among identical concurrent processes, but at the |
| 3026 | * cost of test repeatability. For debug builds, instead use a |
| 3027 | * deterministic seed. |
| 3028 | */ |
| 3029 | arena->offset_state = config_debug ? ind : |
| 3030 | (uint64_t)(uintptr_t)arena; |
| 3031 | } |
| 3032 | |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3033 | arena->dss_prec = chunk_dss_prec_get(); |
| 3034 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3035 | arena->spare = NULL; |
| 3036 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 3037 | arena->lg_dirty_mult = arena_lg_dirty_mult_default_get(); |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 3038 | arena->purging = false; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3039 | arena->nactive = 0; |
| 3040 | arena->ndirty = 0; |
| 3041 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 3042 | arena_avail_tree_new(&arena->runs_avail); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3043 | qr_new(&arena->runs_dirty, rd_link); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 3044 | qr_new(&arena->chunks_cache, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3045 | |
| 3046 | ql_new(&arena->huge); |
| 3047 | if (malloc_mutex_init(&arena->huge_mtx)) |
| 3048 | return (NULL); |
| 3049 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 3050 | extent_tree_szad_new(&arena->chunks_szad_cached); |
| 3051 | extent_tree_ad_new(&arena->chunks_ad_cached); |
| 3052 | extent_tree_szad_new(&arena->chunks_szad_retained); |
| 3053 | extent_tree_ad_new(&arena->chunks_ad_retained); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3054 | if (malloc_mutex_init(&arena->chunks_mtx)) |
| 3055 | return (NULL); |
| 3056 | ql_new(&arena->node_cache); |
| 3057 | if (malloc_mutex_init(&arena->node_cache_mtx)) |
| 3058 | return (NULL); |
| 3059 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 3060 | arena->chunk_hooks = chunk_hooks_default; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3061 | |
| 3062 | /* Initialize bins. */ |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3063 | for (i = 0; i < NBINS; i++) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3064 | bin = &arena->bins[i]; |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 3065 | if (malloc_mutex_init(&bin->lock)) |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3066 | return (NULL); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3067 | bin->runcur = NULL; |
| 3068 | arena_run_tree_new(&bin->runs); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3069 | if (config_stats) |
| 3070 | memset(&bin->stats, 0, sizeof(malloc_bin_stats_t)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3071 | } |
| 3072 | |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3073 | return (arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3074 | } |
| 3075 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3076 | /* |
| 3077 | * Calculate bin_info->run_size such that it meets the following constraints: |
| 3078 | * |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3079 | * *) bin_info->run_size <= arena_maxrun |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 3080 | * *) bin_info->nregs <= RUN_MAXREGS |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3081 | * |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3082 | * bin_info->nregs and bin_info->reg0_offset are also calculated here, since |
| 3083 | * these settings are all interdependent. |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3084 | */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3085 | static void |
| 3086 | bin_info_run_size_calc(arena_bin_info_t *bin_info) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3087 | { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3088 | size_t pad_size; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3089 | size_t try_run_size, perfect_run_size, actual_run_size; |
| 3090 | uint32_t try_nregs, perfect_nregs, actual_nregs; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3091 | |
| 3092 | /* |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3093 | * Determine redzone size based on minimum alignment and minimum |
| 3094 | * redzone size. Add padding to the end of the run if it is needed to |
| 3095 | * align the regions. The padding allows each redzone to be half the |
| 3096 | * minimum alignment; without the padding, each redzone would have to |
| 3097 | * be twice as large in order to maintain alignment. |
| 3098 | */ |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 3099 | if (config_fill && unlikely(opt_redzone)) { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 3100 | size_t align_min = ZU(1) << (jemalloc_ffs(bin_info->reg_size) - |
| 3101 | 1); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3102 | if (align_min <= REDZONE_MINSIZE) { |
| 3103 | bin_info->redzone_size = REDZONE_MINSIZE; |
| 3104 | pad_size = 0; |
| 3105 | } else { |
| 3106 | bin_info->redzone_size = align_min >> 1; |
| 3107 | pad_size = bin_info->redzone_size; |
| 3108 | } |
| 3109 | } else { |
| 3110 | bin_info->redzone_size = 0; |
| 3111 | pad_size = 0; |
| 3112 | } |
| 3113 | bin_info->reg_interval = bin_info->reg_size + |
| 3114 | (bin_info->redzone_size << 1); |
| 3115 | |
| 3116 | /* |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3117 | * Compute run size under ideal conditions (no redzones, no limit on run |
| 3118 | * size). |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3119 | */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3120 | try_run_size = PAGE; |
| 3121 | try_nregs = try_run_size / bin_info->reg_size; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3122 | do { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3123 | perfect_run_size = try_run_size; |
| 3124 | perfect_nregs = try_nregs; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3125 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 3126 | try_run_size += PAGE; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3127 | try_nregs = try_run_size / bin_info->reg_size; |
| 3128 | } while (perfect_run_size != perfect_nregs * bin_info->reg_size); |
| 3129 | assert(perfect_nregs <= RUN_MAXREGS); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3130 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3131 | actual_run_size = perfect_run_size; |
| 3132 | actual_nregs = (actual_run_size - pad_size) / bin_info->reg_interval; |
| 3133 | |
| 3134 | /* |
| 3135 | * Redzones can require enough padding that not even a single region can |
| 3136 | * fit within the number of pages that would normally be dedicated to a |
| 3137 | * run for this size class. Increase the run size until at least one |
| 3138 | * region fits. |
| 3139 | */ |
| 3140 | while (actual_nregs == 0) { |
| 3141 | assert(config_fill && unlikely(opt_redzone)); |
| 3142 | |
| 3143 | actual_run_size += PAGE; |
| 3144 | actual_nregs = (actual_run_size - pad_size) / |
| 3145 | bin_info->reg_interval; |
| 3146 | } |
| 3147 | |
| 3148 | /* |
| 3149 | * Make sure that the run will fit within an arena chunk. |
| 3150 | */ |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3151 | while (actual_run_size > arena_maxrun) { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3152 | actual_run_size -= PAGE; |
| 3153 | actual_nregs = (actual_run_size - pad_size) / |
| 3154 | bin_info->reg_interval; |
| 3155 | } |
| 3156 | assert(actual_nregs > 0); |
Jason Evans | 5707d6f | 2015-03-06 17:14:05 -0800 | [diff] [blame] | 3157 | assert(actual_run_size == s2u(actual_run_size)); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3158 | |
| 3159 | /* Copy final settings. */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3160 | bin_info->run_size = actual_run_size; |
| 3161 | bin_info->nregs = actual_nregs; |
| 3162 | bin_info->reg0_offset = actual_run_size - (actual_nregs * |
| 3163 | bin_info->reg_interval) - pad_size + bin_info->redzone_size; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3164 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3165 | if (actual_run_size > small_maxrun) |
| 3166 | small_maxrun = actual_run_size; |
| 3167 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3168 | assert(bin_info->reg0_offset - bin_info->redzone_size + (bin_info->nregs |
| 3169 | * bin_info->reg_interval) + pad_size == bin_info->run_size); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3172 | static void |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3173 | bin_info_init(void) |
| 3174 | { |
| 3175 | arena_bin_info_t *bin_info; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3176 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3177 | #define BIN_INFO_INIT_bin_yes(index, size) \ |
Jason Evans | d04047c | 2014-05-28 16:11:55 -0700 | [diff] [blame] | 3178 | bin_info = &arena_bin_info[index]; \ |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3179 | bin_info->reg_size = size; \ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3180 | bin_info_run_size_calc(bin_info); \ |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3181 | bitmap_info_init(&bin_info->bitmap_info, bin_info->nregs); |
Jason Evans | d04047c | 2014-05-28 16:11:55 -0700 | [diff] [blame] | 3182 | #define BIN_INFO_INIT_bin_no(index, size) |
| 3183 | #define SC(index, lg_grp, lg_delta, ndelta, bin, lg_delta_lookup) \ |
| 3184 | BIN_INFO_INIT_bin_##bin(index, (ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta)) |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3185 | SIZE_CLASSES |
Jason Evans | d04047c | 2014-05-28 16:11:55 -0700 | [diff] [blame] | 3186 | #undef BIN_INFO_INIT_bin_yes |
| 3187 | #undef BIN_INFO_INIT_bin_no |
| 3188 | #undef SC |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3191 | static bool |
| 3192 | small_run_size_init(void) |
| 3193 | { |
| 3194 | |
| 3195 | assert(small_maxrun != 0); |
| 3196 | |
| 3197 | small_run_tab = (bool *)base_alloc(sizeof(bool) * (small_maxrun >> |
| 3198 | LG_PAGE)); |
| 3199 | if (small_run_tab == NULL) |
| 3200 | return (true); |
| 3201 | |
| 3202 | #define TAB_INIT_bin_yes(index, size) { \ |
| 3203 | arena_bin_info_t *bin_info = &arena_bin_info[index]; \ |
| 3204 | small_run_tab[bin_info->run_size >> LG_PAGE] = true; \ |
| 3205 | } |
| 3206 | #define TAB_INIT_bin_no(index, size) |
| 3207 | #define SC(index, lg_grp, lg_delta, ndelta, bin, lg_delta_lookup) \ |
| 3208 | TAB_INIT_bin_##bin(index, (ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta)) |
| 3209 | SIZE_CLASSES |
| 3210 | #undef TAB_INIT_bin_yes |
| 3211 | #undef TAB_INIT_bin_no |
| 3212 | #undef SC |
| 3213 | |
| 3214 | return (false); |
| 3215 | } |
| 3216 | |
| 3217 | bool |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 3218 | arena_boot(void) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3219 | { |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 3220 | size_t header_size; |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3221 | unsigned i; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3222 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 3223 | arena_lg_dirty_mult_default_set(opt_lg_dirty_mult); |
| 3224 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3225 | /* |
| 3226 | * Compute the header size such that it is large enough to contain the |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3227 | * page map. The page map is biased to omit entries for the header |
| 3228 | * itself, so some iteration is necessary to compute the map bias. |
| 3229 | * |
| 3230 | * 1) Compute safe header_size and map_bias values that include enough |
| 3231 | * space for an unbiased page map. |
| 3232 | * 2) Refine map_bias based on (1) to omit the header pages in the page |
| 3233 | * map. The resulting map_bias may be one too small. |
| 3234 | * 3) Refine map_bias based on (2). The result will be >= the result |
| 3235 | * from (2), and will always be correct. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3236 | */ |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3237 | map_bias = 0; |
| 3238 | for (i = 0; i < 3; i++) { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 3239 | header_size = offsetof(arena_chunk_t, map_bits) + |
| 3240 | ((sizeof(arena_chunk_map_bits_t) + |
| 3241 | sizeof(arena_chunk_map_misc_t)) * (chunk_npages-map_bias)); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3242 | map_bias = (header_size + PAGE_MASK) >> LG_PAGE; |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3243 | } |
| 3244 | assert(map_bias > 0); |
| 3245 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 3246 | map_misc_offset = offsetof(arena_chunk_t, map_bits) + |
| 3247 | sizeof(arena_chunk_map_bits_t) * (chunk_npages-map_bias); |
| 3248 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3249 | arena_maxrun = chunksize - (map_bias << LG_PAGE); |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 3250 | assert(arena_maxrun > 0); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3251 | arena_maxclass = index2size(size2index(chunksize)-1); |
| 3252 | if (arena_maxclass > arena_maxrun) { |
| 3253 | /* |
| 3254 | * For small chunk sizes it's possible for there to be fewer |
| 3255 | * non-header pages available than are necessary to serve the |
| 3256 | * size classes just below chunksize. |
| 3257 | */ |
| 3258 | arena_maxclass = arena_maxrun; |
| 3259 | } |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 3260 | assert(arena_maxclass > 0); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3261 | nlclasses = size2index(arena_maxclass) - size2index(SMALL_MAXCLASS); |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3262 | nhclasses = NSIZES - nlclasses - NBINS; |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 3263 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3264 | bin_info_init(); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3265 | return (small_run_size_init()); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3266 | } |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3267 | |
| 3268 | void |
| 3269 | arena_prefork(arena_t *arena) |
| 3270 | { |
| 3271 | unsigned i; |
| 3272 | |
| 3273 | malloc_mutex_prefork(&arena->lock); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 3274 | malloc_mutex_prefork(&arena->huge_mtx); |
| 3275 | malloc_mutex_prefork(&arena->chunks_mtx); |
| 3276 | malloc_mutex_prefork(&arena->node_cache_mtx); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3277 | for (i = 0; i < NBINS; i++) |
| 3278 | malloc_mutex_prefork(&arena->bins[i].lock); |
| 3279 | } |
| 3280 | |
| 3281 | void |
| 3282 | arena_postfork_parent(arena_t *arena) |
| 3283 | { |
| 3284 | unsigned i; |
| 3285 | |
| 3286 | for (i = 0; i < NBINS; i++) |
| 3287 | malloc_mutex_postfork_parent(&arena->bins[i].lock); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 3288 | malloc_mutex_postfork_parent(&arena->node_cache_mtx); |
| 3289 | malloc_mutex_postfork_parent(&arena->chunks_mtx); |
| 3290 | malloc_mutex_postfork_parent(&arena->huge_mtx); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3291 | malloc_mutex_postfork_parent(&arena->lock); |
| 3292 | } |
| 3293 | |
| 3294 | void |
| 3295 | arena_postfork_child(arena_t *arena) |
| 3296 | { |
| 3297 | unsigned i; |
| 3298 | |
| 3299 | for (i = 0; i < NBINS; i++) |
| 3300 | malloc_mutex_postfork_child(&arena->bins[i].lock); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 3301 | malloc_mutex_postfork_child(&arena->node_cache_mtx); |
| 3302 | malloc_mutex_postfork_child(&arena->chunks_mtx); |
| 3303 | malloc_mutex_postfork_child(&arena->huge_mtx); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3304 | malloc_mutex_postfork_child(&arena->lock); |
| 3305 | } |