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