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 | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 8 | arena_bin_info_t arena_bin_info[NBINS]; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 9 | |
Mike Hommey | da99e31 | 2012-04-30 12:38:29 +0200 | [diff] [blame] | 10 | JEMALLOC_ALIGNED(CACHELINE) |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 11 | const uint8_t small_size2bin[] = { |
Jason Evans | 41ade96 | 2011-03-06 22:56:36 -0800 | [diff] [blame] | 12 | #define S2B_8(i) i, |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 13 | #define S2B_16(i) S2B_8(i) S2B_8(i) |
| 14 | #define S2B_32(i) S2B_16(i) S2B_16(i) |
| 15 | #define S2B_64(i) S2B_32(i) S2B_32(i) |
| 16 | #define S2B_128(i) S2B_64(i) S2B_64(i) |
| 17 | #define S2B_256(i) S2B_128(i) S2B_128(i) |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 18 | #define S2B_512(i) S2B_256(i) S2B_256(i) |
| 19 | #define S2B_1024(i) S2B_512(i) S2B_512(i) |
| 20 | #define S2B_2048(i) S2B_1024(i) S2B_1024(i) |
| 21 | #define S2B_4096(i) S2B_2048(i) S2B_2048(i) |
| 22 | #define S2B_8192(i) S2B_4096(i) S2B_4096(i) |
| 23 | #define SIZE_CLASS(bin, delta, size) \ |
| 24 | S2B_##delta(bin) |
| 25 | SIZE_CLASSES |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 26 | #undef S2B_8 |
| 27 | #undef S2B_16 |
| 28 | #undef S2B_32 |
| 29 | #undef S2B_64 |
| 30 | #undef S2B_128 |
| 31 | #undef S2B_256 |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 32 | #undef S2B_512 |
| 33 | #undef S2B_1024 |
| 34 | #undef S2B_2048 |
| 35 | #undef S2B_4096 |
| 36 | #undef S2B_8192 |
| 37 | #undef SIZE_CLASS |
| 38 | }; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 39 | |
| 40 | /******************************************************************************/ |
| 41 | /* Function prototypes for non-inline static functions. */ |
| 42 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 43 | static void arena_avail_insert(arena_t *arena, arena_chunk_t *chunk, |
| 44 | size_t pageind, size_t npages, bool maybe_adjac_pred, |
| 45 | bool maybe_adjac_succ); |
| 46 | static void arena_avail_remove(arena_t *arena, arena_chunk_t *chunk, |
| 47 | size_t pageind, size_t npages, bool maybe_adjac_pred, |
| 48 | bool maybe_adjac_succ); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 49 | static void arena_run_split(arena_t *arena, arena_run_t *run, size_t size, |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 50 | bool large, size_t binind, bool zero); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 51 | static arena_chunk_t *arena_chunk_alloc(arena_t *arena); |
| 52 | static void arena_chunk_dealloc(arena_t *arena, arena_chunk_t *chunk); |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 53 | static arena_run_t *arena_run_alloc_helper(arena_t *arena, size_t size, |
| 54 | bool large, size_t binind, bool zero); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 55 | static arena_run_t *arena_run_alloc(arena_t *arena, size_t size, bool large, |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 56 | size_t binind, bool zero); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 57 | static arena_chunk_t *chunks_dirty_iter_cb(arena_chunk_tree_t *tree, |
| 58 | arena_chunk_t *chunk, void *arg); |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 59 | static void arena_purge(arena_t *arena, bool all); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 60 | static void arena_run_dalloc(arena_t *arena, arena_run_t *run, bool dirty, |
| 61 | bool cleaned); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 62 | static void arena_run_trim_head(arena_t *arena, arena_chunk_t *chunk, |
| 63 | arena_run_t *run, size_t oldsize, size_t newsize); |
| 64 | static void arena_run_trim_tail(arena_t *arena, arena_chunk_t *chunk, |
| 65 | arena_run_t *run, size_t oldsize, size_t newsize, bool dirty); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 66 | static arena_run_t *arena_bin_runs_first(arena_bin_t *bin); |
| 67 | static void arena_bin_runs_insert(arena_bin_t *bin, arena_run_t *run); |
| 68 | static void arena_bin_runs_remove(arena_bin_t *bin, arena_run_t *run); |
| 69 | static arena_run_t *arena_bin_nonfull_run_tryget(arena_bin_t *bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 70 | static arena_run_t *arena_bin_nonfull_run_get(arena_t *arena, arena_bin_t *bin); |
| 71 | static void *arena_bin_malloc_hard(arena_t *arena, arena_bin_t *bin); |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 72 | static void arena_dissociate_bin_run(arena_chunk_t *chunk, arena_run_t *run, |
| 73 | arena_bin_t *bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 74 | static void arena_dalloc_bin_run(arena_t *arena, arena_chunk_t *chunk, |
| 75 | arena_run_t *run, arena_bin_t *bin); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 76 | static void arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk, |
| 77 | arena_run_t *run, arena_bin_t *bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 78 | static void arena_ralloc_large_shrink(arena_t *arena, arena_chunk_t *chunk, |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 79 | void *ptr, size_t oldsize, size_t size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 80 | static bool arena_ralloc_large_grow(arena_t *arena, arena_chunk_t *chunk, |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 81 | void *ptr, size_t oldsize, size_t size, size_t extra, bool zero); |
| 82 | static bool arena_ralloc_large(void *ptr, size_t oldsize, size_t size, |
| 83 | size_t extra, bool zero); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 84 | static size_t bin_info_run_size_calc(arena_bin_info_t *bin_info, |
| 85 | size_t min_run_size); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 86 | static void bin_info_init(void); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 87 | |
| 88 | /******************************************************************************/ |
| 89 | |
| 90 | static inline int |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 91 | arena_run_comp(arena_chunk_map_t *a, arena_chunk_map_t *b) |
| 92 | { |
| 93 | uintptr_t a_mapelm = (uintptr_t)a; |
| 94 | uintptr_t b_mapelm = (uintptr_t)b; |
| 95 | |
| 96 | assert(a != NULL); |
| 97 | assert(b != NULL); |
| 98 | |
| 99 | return ((a_mapelm > b_mapelm) - (a_mapelm < b_mapelm)); |
| 100 | } |
| 101 | |
Jason Evans | f3ff752 | 2010-02-28 15:00:18 -0800 | [diff] [blame] | 102 | /* Generate red-black tree functions. */ |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 103 | rb_gen(static UNUSED, arena_run_tree_, arena_run_tree_t, arena_chunk_map_t, |
| 104 | u.rb_link, arena_run_comp) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 105 | |
| 106 | static inline int |
| 107 | arena_avail_comp(arena_chunk_map_t *a, arena_chunk_map_t *b) |
| 108 | { |
| 109 | int ret; |
| 110 | size_t a_size = a->bits & ~PAGE_MASK; |
| 111 | size_t b_size = b->bits & ~PAGE_MASK; |
| 112 | |
| 113 | ret = (a_size > b_size) - (a_size < b_size); |
| 114 | if (ret == 0) { |
| 115 | uintptr_t a_mapelm, b_mapelm; |
| 116 | |
| 117 | if ((a->bits & CHUNK_MAP_KEY) != CHUNK_MAP_KEY) |
| 118 | a_mapelm = (uintptr_t)a; |
| 119 | else { |
| 120 | /* |
| 121 | * Treat keys as though they are lower than anything |
| 122 | * else. |
| 123 | */ |
| 124 | a_mapelm = 0; |
| 125 | } |
| 126 | b_mapelm = (uintptr_t)b; |
| 127 | |
| 128 | ret = (a_mapelm > b_mapelm) - (a_mapelm < b_mapelm); |
| 129 | } |
| 130 | |
| 131 | return (ret); |
| 132 | } |
| 133 | |
Jason Evans | f3ff752 | 2010-02-28 15:00:18 -0800 | [diff] [blame] | 134 | /* Generate red-black tree functions. */ |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 135 | rb_gen(static UNUSED, arena_avail_tree_, arena_avail_tree_t, arena_chunk_map_t, |
| 136 | u.rb_link, arena_avail_comp) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 137 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 138 | static inline int |
| 139 | arena_chunk_dirty_comp(arena_chunk_t *a, arena_chunk_t *b) |
| 140 | { |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 141 | |
| 142 | assert(a != NULL); |
| 143 | assert(b != NULL); |
| 144 | |
| 145 | /* |
Jason Evans | abf6739 | 2012-11-07 10:05:04 -0800 | [diff] [blame^] | 146 | * Short-circuit for self comparison. The following comparison code |
| 147 | * would come to the same result, but at the cost of executing the slow |
| 148 | * path. |
| 149 | */ |
| 150 | if (a == b) |
| 151 | return (0); |
| 152 | |
| 153 | /* |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 154 | * Order such that chunks with higher fragmentation are "less than" |
Jason Evans | abf6739 | 2012-11-07 10:05:04 -0800 | [diff] [blame^] | 155 | * those with lower fragmentation -- purging order is from "least" to |
| 156 | * "greatest". Fragmentation is measured as: |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 157 | * |
| 158 | * mean current avail run size |
| 159 | * -------------------------------- |
| 160 | * mean defragmented avail run size |
| 161 | * |
| 162 | * navail |
| 163 | * ----------- |
| 164 | * nruns_avail nruns_avail-nruns_adjac |
| 165 | * = ========================= = ----------------------- |
| 166 | * navail nruns_avail |
| 167 | * ----------------------- |
| 168 | * nruns_avail-nruns_adjac |
| 169 | * |
| 170 | * The following code multiplies away the denominator prior to |
| 171 | * comparison, in order to avoid division. |
| 172 | * |
| 173 | */ |
Jason Evans | abf6739 | 2012-11-07 10:05:04 -0800 | [diff] [blame^] | 174 | { |
| 175 | size_t a_val = (a->nruns_avail - a->nruns_adjac) * |
| 176 | b->nruns_avail; |
| 177 | size_t b_val = (b->nruns_avail - b->nruns_adjac) * |
| 178 | a->nruns_avail; |
| 179 | |
| 180 | if (a_val < b_val) |
| 181 | return (1); |
| 182 | if (a_val > b_val) |
| 183 | return (-1); |
| 184 | } |
| 185 | /* |
| 186 | * Break ties by chunk address. For fragmented chunks, report lower |
| 187 | * addresses as "lower", so that fragmentation reduction happens first |
| 188 | * at lower addresses. However, use the opposite ordering for |
| 189 | * unfragmented chunks, in order to increase the chances of |
| 190 | * re-allocating dirty runs. |
| 191 | */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 192 | { |
| 193 | uintptr_t a_chunk = (uintptr_t)a; |
| 194 | uintptr_t b_chunk = (uintptr_t)b; |
Jason Evans | abf6739 | 2012-11-07 10:05:04 -0800 | [diff] [blame^] | 195 | int ret = ((a_chunk > b_chunk) - (a_chunk < b_chunk)); |
| 196 | if (a->nruns_adjac == 0) { |
| 197 | assert(b->nruns_adjac == 0); |
| 198 | ret = -ret; |
| 199 | } |
| 200 | return (ret); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | /* Generate red-black tree functions. */ |
| 205 | rb_gen(static UNUSED, arena_chunk_dirty_, arena_chunk_tree_t, arena_chunk_t, |
| 206 | dirty_link, arena_chunk_dirty_comp) |
| 207 | |
| 208 | static inline bool |
| 209 | arena_avail_adjac_pred(arena_chunk_t *chunk, size_t pageind) |
| 210 | { |
| 211 | bool ret; |
| 212 | |
| 213 | if (pageind-1 < map_bias) |
| 214 | ret = false; |
| 215 | else { |
| 216 | ret = (arena_mapbits_allocated_get(chunk, pageind-1) == 0); |
| 217 | assert(ret == false || arena_mapbits_dirty_get(chunk, |
| 218 | pageind-1) != arena_mapbits_dirty_get(chunk, pageind)); |
| 219 | } |
| 220 | return (ret); |
| 221 | } |
| 222 | |
| 223 | static inline bool |
| 224 | arena_avail_adjac_succ(arena_chunk_t *chunk, size_t pageind, size_t npages) |
| 225 | { |
| 226 | bool ret; |
| 227 | |
| 228 | if (pageind+npages == chunk_npages) |
| 229 | ret = false; |
| 230 | else { |
| 231 | assert(pageind+npages < chunk_npages); |
| 232 | ret = (arena_mapbits_allocated_get(chunk, pageind+npages) == 0); |
| 233 | assert(ret == false || arena_mapbits_dirty_get(chunk, pageind) |
| 234 | != arena_mapbits_dirty_get(chunk, pageind+npages)); |
| 235 | } |
| 236 | return (ret); |
| 237 | } |
| 238 | |
| 239 | static inline bool |
| 240 | arena_avail_adjac(arena_chunk_t *chunk, size_t pageind, size_t npages) |
| 241 | { |
| 242 | |
| 243 | return (arena_avail_adjac_pred(chunk, pageind) || |
| 244 | arena_avail_adjac_succ(chunk, pageind, npages)); |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | arena_avail_insert(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
| 249 | size_t npages, bool maybe_adjac_pred, bool maybe_adjac_succ) |
| 250 | { |
| 251 | |
| 252 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 253 | LG_PAGE)); |
| 254 | |
| 255 | /* |
| 256 | * chunks_dirty is keyed by nruns_{avail,adjac}, so the chunk must be |
| 257 | * removed and reinserted even if the run to be inserted is clean. |
| 258 | */ |
| 259 | if (chunk->ndirty != 0) |
| 260 | arena_chunk_dirty_remove(&arena->chunks_dirty, chunk); |
| 261 | |
| 262 | if (maybe_adjac_pred && arena_avail_adjac_pred(chunk, pageind)) |
| 263 | chunk->nruns_adjac++; |
| 264 | if (maybe_adjac_succ && arena_avail_adjac_succ(chunk, pageind, npages)) |
| 265 | chunk->nruns_adjac++; |
| 266 | chunk->nruns_avail++; |
| 267 | assert(chunk->nruns_avail > chunk->nruns_adjac); |
| 268 | |
| 269 | if (arena_mapbits_dirty_get(chunk, pageind) != 0) { |
| 270 | arena->ndirty += npages; |
| 271 | chunk->ndirty += npages; |
| 272 | } |
| 273 | if (chunk->ndirty != 0) |
| 274 | arena_chunk_dirty_insert(&arena->chunks_dirty, chunk); |
| 275 | |
| 276 | arena_avail_tree_insert(&arena->runs_avail, arena_mapp_get(chunk, |
| 277 | pageind)); |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | arena_avail_remove(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
| 282 | size_t npages, bool maybe_adjac_pred, bool maybe_adjac_succ) |
| 283 | { |
| 284 | |
| 285 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 286 | LG_PAGE)); |
| 287 | |
| 288 | /* |
| 289 | * chunks_dirty is keyed by nruns_{avail,adjac}, so the chunk must be |
| 290 | * removed and reinserted even if the run to be removed is clean. |
| 291 | */ |
| 292 | if (chunk->ndirty != 0) |
| 293 | arena_chunk_dirty_remove(&arena->chunks_dirty, chunk); |
| 294 | |
| 295 | if (maybe_adjac_pred && arena_avail_adjac_pred(chunk, pageind)) |
| 296 | chunk->nruns_adjac--; |
| 297 | if (maybe_adjac_succ && arena_avail_adjac_succ(chunk, pageind, npages)) |
| 298 | chunk->nruns_adjac--; |
| 299 | chunk->nruns_avail--; |
| 300 | assert(chunk->nruns_avail > chunk->nruns_adjac || (chunk->nruns_avail |
| 301 | == 0 && chunk->nruns_adjac == 0)); |
| 302 | |
| 303 | if (arena_mapbits_dirty_get(chunk, pageind) != 0) { |
| 304 | arena->ndirty -= npages; |
| 305 | chunk->ndirty -= npages; |
| 306 | } |
| 307 | if (chunk->ndirty != 0) |
| 308 | arena_chunk_dirty_insert(&arena->chunks_dirty, chunk); |
| 309 | |
| 310 | arena_avail_tree_remove(&arena->runs_avail, arena_mapp_get(chunk, |
| 311 | pageind)); |
| 312 | } |
| 313 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 314 | static inline void * |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 315 | 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] | 316 | { |
| 317 | void *ret; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 318 | unsigned regind; |
| 319 | bitmap_t *bitmap = (bitmap_t *)((uintptr_t)run + |
| 320 | (uintptr_t)bin_info->bitmap_offset); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 321 | |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 322 | assert(run->nfree > 0); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 323 | assert(bitmap_full(bitmap, &bin_info->bitmap_info) == false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 324 | |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 325 | regind = bitmap_sfu(bitmap, &bin_info->bitmap_info); |
| 326 | ret = (void *)((uintptr_t)run + (uintptr_t)bin_info->reg0_offset + |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 327 | (uintptr_t)(bin_info->reg_interval * regind)); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 328 | run->nfree--; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 329 | if (regind == run->nextind) |
| 330 | run->nextind++; |
| 331 | assert(regind < run->nextind); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 332 | return (ret); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static inline void |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 336 | arena_run_reg_dalloc(arena_run_t *run, void *ptr) |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 337 | { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 338 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 339 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
| 340 | size_t mapbits = arena_mapbits_get(chunk, pageind); |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 341 | size_t binind = arena_ptr_small_binind_get(ptr, mapbits); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 342 | arena_bin_info_t *bin_info = &arena_bin_info[binind]; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 343 | unsigned regind = arena_run_regind(run, bin_info, ptr); |
| 344 | bitmap_t *bitmap = (bitmap_t *)((uintptr_t)run + |
| 345 | (uintptr_t)bin_info->bitmap_offset); |
| 346 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 347 | assert(run->nfree < bin_info->nregs); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 348 | /* Freeing an interior pointer can cause assertion failure. */ |
| 349 | assert(((uintptr_t)ptr - ((uintptr_t)run + |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 350 | (uintptr_t)bin_info->reg0_offset)) % |
| 351 | (uintptr_t)bin_info->reg_interval == 0); |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 352 | assert((uintptr_t)ptr >= (uintptr_t)run + |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 353 | (uintptr_t)bin_info->reg0_offset); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 354 | /* Freeing an unallocated pointer can cause assertion failure. */ |
| 355 | assert(bitmap_get(bitmap, &bin_info->bitmap_info, regind)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 356 | |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 357 | bitmap_unset(bitmap, &bin_info->bitmap_info, regind); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 358 | run->nfree++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 361 | static inline void |
| 362 | arena_chunk_validate_zeroed(arena_chunk_t *chunk, size_t run_ind) |
| 363 | { |
Jason Evans | d4bab21 | 2010-10-24 20:08:37 -0700 | [diff] [blame] | 364 | size_t i; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 365 | 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] | 366 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 367 | for (i = 0; i < PAGE / sizeof(size_t); i++) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 368 | assert(p[i] == 0); |
| 369 | } |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 370 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 371 | static void |
| 372 | arena_run_split(arena_t *arena, arena_run_t *run, size_t size, bool large, |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 373 | size_t binind, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 374 | { |
| 375 | arena_chunk_t *chunk; |
Jason Evans | bdcadf4 | 2012-02-28 21:11:03 -0800 | [diff] [blame] | 376 | size_t run_ind, total_pages, need_pages, rem_pages, i; |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 377 | size_t flag_dirty; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 378 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 379 | assert((large && binind == BININD_INVALID) || (large == false && binind |
| 380 | != BININD_INVALID)); |
| 381 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 382 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 383 | run_ind = (unsigned)(((uintptr_t)run - (uintptr_t)chunk) >> LG_PAGE); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 384 | flag_dirty = arena_mapbits_dirty_get(chunk, run_ind); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 385 | total_pages = arena_mapbits_unallocated_size_get(chunk, run_ind) >> |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 386 | LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 387 | assert(arena_mapbits_dirty_get(chunk, run_ind+total_pages-1) == |
| 388 | flag_dirty); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 389 | need_pages = (size >> LG_PAGE); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 390 | assert(need_pages > 0); |
| 391 | assert(need_pages <= total_pages); |
| 392 | rem_pages = total_pages - need_pages; |
| 393 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 394 | arena_avail_remove(arena, chunk, run_ind, total_pages, true, true); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 395 | if (config_stats) { |
| 396 | /* |
| 397 | * Update stats_cactive if nactive is crossing a chunk |
| 398 | * multiple. |
| 399 | */ |
| 400 | size_t cactive_diff = CHUNK_CEILING((arena->nactive + |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 401 | need_pages) << LG_PAGE) - CHUNK_CEILING(arena->nactive << |
| 402 | LG_PAGE); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 403 | if (cactive_diff != 0) |
| 404 | stats_cactive_add(cactive_diff); |
| 405 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 406 | arena->nactive += need_pages; |
| 407 | |
| 408 | /* Keep track of trailing unused pages for later use. */ |
| 409 | if (rem_pages > 0) { |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 410 | if (flag_dirty != 0) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 411 | arena_mapbits_unallocated_set(chunk, run_ind+need_pages, |
| 412 | (rem_pages << LG_PAGE), CHUNK_MAP_DIRTY); |
| 413 | arena_mapbits_unallocated_set(chunk, |
| 414 | run_ind+total_pages-1, (rem_pages << LG_PAGE), |
| 415 | CHUNK_MAP_DIRTY); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 416 | } else { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 417 | arena_mapbits_unallocated_set(chunk, run_ind+need_pages, |
| 418 | (rem_pages << LG_PAGE), |
| 419 | arena_mapbits_unzeroed_get(chunk, |
| 420 | run_ind+need_pages)); |
| 421 | arena_mapbits_unallocated_set(chunk, |
| 422 | run_ind+total_pages-1, (rem_pages << LG_PAGE), |
| 423 | arena_mapbits_unzeroed_get(chunk, |
| 424 | run_ind+total_pages-1)); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 425 | } |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 426 | arena_avail_insert(arena, chunk, run_ind+need_pages, rem_pages, |
| 427 | false, true); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Update the page map separately for large vs. small runs, since it is |
| 432 | * possible to avoid iteration for large mallocs. |
| 433 | */ |
| 434 | if (large) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 435 | if (zero) { |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 436 | if (flag_dirty == 0) { |
| 437 | /* |
| 438 | * The run is clean, so some pages may be |
| 439 | * zeroed (i.e. never before touched). |
| 440 | */ |
| 441 | for (i = 0; i < need_pages; i++) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 442 | if (arena_mapbits_unzeroed_get(chunk, |
| 443 | run_ind+i) != 0) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 444 | VALGRIND_MAKE_MEM_UNDEFINED( |
| 445 | (void *)((uintptr_t) |
| 446 | chunk + ((run_ind+i) << |
| 447 | LG_PAGE)), PAGE); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 448 | memset((void *)((uintptr_t) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 449 | chunk + ((run_ind+i) << |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 450 | LG_PAGE)), 0, PAGE); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 451 | } else if (config_debug) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 452 | VALGRIND_MAKE_MEM_DEFINED( |
| 453 | (void *)((uintptr_t) |
| 454 | chunk + ((run_ind+i) << |
| 455 | LG_PAGE)), PAGE); |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 456 | arena_chunk_validate_zeroed( |
| 457 | chunk, run_ind+i); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 458 | } |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 459 | } |
| 460 | } else { |
| 461 | /* |
| 462 | * The run is dirty, so all pages must be |
| 463 | * zeroed. |
| 464 | */ |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 465 | VALGRIND_MAKE_MEM_UNDEFINED((void |
| 466 | *)((uintptr_t)chunk + (run_ind << |
| 467 | LG_PAGE)), (need_pages << LG_PAGE)); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 468 | memset((void *)((uintptr_t)chunk + (run_ind << |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 469 | LG_PAGE)), 0, (need_pages << LG_PAGE)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 473 | /* |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 474 | * Set the last element first, in case the run only contains one |
| 475 | * page (i.e. both statements set the same element). |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 476 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 477 | arena_mapbits_large_set(chunk, run_ind+need_pages-1, 0, |
| 478 | flag_dirty); |
| 479 | arena_mapbits_large_set(chunk, run_ind, size, flag_dirty); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 480 | } else { |
| 481 | assert(zero == false); |
| 482 | /* |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 483 | * Propagate the dirty and unzeroed flags to the allocated |
| 484 | * small run, so that arena_dalloc_bin_run() has the ability to |
| 485 | * conditionally trim clean pages. |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 486 | */ |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 487 | arena_mapbits_small_set(chunk, run_ind, 0, binind, flag_dirty); |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 488 | /* |
| 489 | * The first page will always be dirtied during small run |
| 490 | * initialization, so a validation failure here would not |
| 491 | * actually cause an observable failure. |
| 492 | */ |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 493 | if (config_debug && flag_dirty == 0 && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 494 | arena_mapbits_unzeroed_get(chunk, run_ind) == 0) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 495 | arena_chunk_validate_zeroed(chunk, run_ind); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 496 | for (i = 1; i < need_pages - 1; i++) { |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 497 | arena_mapbits_small_set(chunk, run_ind+i, i, binind, 0); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 498 | if (config_debug && flag_dirty == 0 && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 499 | arena_mapbits_unzeroed_get(chunk, run_ind+i) == 0) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 500 | arena_chunk_validate_zeroed(chunk, run_ind+i); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 501 | } |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 502 | arena_mapbits_small_set(chunk, run_ind+need_pages-1, |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 503 | need_pages-1, binind, flag_dirty); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 504 | if (config_debug && flag_dirty == 0 && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 505 | arena_mapbits_unzeroed_get(chunk, run_ind+need_pages-1) == |
| 506 | 0) { |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 507 | arena_chunk_validate_zeroed(chunk, |
| 508 | run_ind+need_pages-1); |
| 509 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
| 513 | static arena_chunk_t * |
| 514 | arena_chunk_alloc(arena_t *arena) |
| 515 | { |
| 516 | arena_chunk_t *chunk; |
| 517 | size_t i; |
| 518 | |
| 519 | if (arena->spare != NULL) { |
| 520 | chunk = arena->spare; |
| 521 | arena->spare = NULL; |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 522 | |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 523 | assert(arena_mapbits_allocated_get(chunk, map_bias) == 0); |
| 524 | assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 525 | assert(arena_mapbits_unallocated_size_get(chunk, map_bias) == |
| 526 | arena_maxclass); |
| 527 | assert(arena_mapbits_unallocated_size_get(chunk, |
| 528 | chunk_npages-1) == arena_maxclass); |
| 529 | assert(arena_mapbits_dirty_get(chunk, map_bias) == |
| 530 | arena_mapbits_dirty_get(chunk, chunk_npages-1)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 531 | } else { |
Jason Evans | 41631d0 | 2010-01-24 17:13:07 -0800 | [diff] [blame] | 532 | bool zero; |
Jason Evans | 3377ffa | 2010-10-01 17:53:37 -0700 | [diff] [blame] | 533 | size_t unzeroed; |
Jason Evans | 41631d0 | 2010-01-24 17:13:07 -0800 | [diff] [blame] | 534 | |
| 535 | zero = false; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 536 | malloc_mutex_unlock(&arena->lock); |
Mike Hommey | eae2690 | 2012-04-10 19:50:33 +0200 | [diff] [blame] | 537 | chunk = (arena_chunk_t *)chunk_alloc(chunksize, chunksize, |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 538 | false, &zero, arena->dss_prec); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 539 | malloc_mutex_lock(&arena->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 540 | if (chunk == NULL) |
| 541 | return (NULL); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 542 | if (config_stats) |
| 543 | arena->stats.mapped += chunksize; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 544 | |
| 545 | chunk->arena = arena; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | * Claim that no pages are in use, since the header is merely |
| 549 | * overhead. |
| 550 | */ |
| 551 | chunk->ndirty = 0; |
| 552 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 553 | chunk->nruns_avail = 0; |
| 554 | chunk->nruns_adjac = 0; |
| 555 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 556 | /* |
| 557 | * Initialize the map to contain one maximal free untouched run. |
Jason Evans | 41631d0 | 2010-01-24 17:13:07 -0800 | [diff] [blame] | 558 | * Mark the pages as zeroed iff chunk_alloc() returned a zeroed |
| 559 | * chunk. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 560 | */ |
Jason Evans | 3377ffa | 2010-10-01 17:53:37 -0700 | [diff] [blame] | 561 | unzeroed = zero ? 0 : CHUNK_MAP_UNZEROED; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 562 | arena_mapbits_unallocated_set(chunk, map_bias, arena_maxclass, |
| 563 | unzeroed); |
Jason Evans | 3377ffa | 2010-10-01 17:53:37 -0700 | [diff] [blame] | 564 | /* |
| 565 | * There is no need to initialize the internal page map entries |
| 566 | * unless the chunk is not zeroed. |
| 567 | */ |
| 568 | if (zero == false) { |
| 569 | for (i = map_bias+1; i < chunk_npages-1; i++) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 570 | arena_mapbits_unzeroed_set(chunk, i, unzeroed); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 571 | } else if (config_debug) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 572 | for (i = map_bias+1; i < chunk_npages-1; i++) { |
| 573 | assert(arena_mapbits_unzeroed_get(chunk, i) == |
| 574 | unzeroed); |
| 575 | } |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 576 | } |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 577 | arena_mapbits_unallocated_set(chunk, chunk_npages-1, |
| 578 | arena_maxclass, unzeroed); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 579 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 580 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 581 | /* Insert the run into the runs_avail tree. */ |
| 582 | arena_avail_insert(arena, chunk, map_bias, chunk_npages-map_bias, |
| 583 | false, false); |
| 584 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 585 | return (chunk); |
| 586 | } |
| 587 | |
| 588 | static void |
| 589 | arena_chunk_dealloc(arena_t *arena, arena_chunk_t *chunk) |
| 590 | { |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 591 | assert(arena_mapbits_allocated_get(chunk, map_bias) == 0); |
| 592 | assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0); |
| 593 | assert(arena_mapbits_unallocated_size_get(chunk, map_bias) == |
| 594 | arena_maxclass); |
| 595 | assert(arena_mapbits_unallocated_size_get(chunk, chunk_npages-1) == |
| 596 | arena_maxclass); |
| 597 | assert(arena_mapbits_dirty_get(chunk, map_bias) == |
| 598 | arena_mapbits_dirty_get(chunk, chunk_npages-1)); |
| 599 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 600 | /* |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 601 | * Remove run from the runs_avail tree, so that the arena does not use |
| 602 | * it. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 603 | */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 604 | arena_avail_remove(arena, chunk, map_bias, chunk_npages-map_bias, |
| 605 | false, false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 606 | |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 607 | if (arena->spare != NULL) { |
| 608 | arena_chunk_t *spare = arena->spare; |
| 609 | |
| 610 | arena->spare = chunk; |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 611 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 12a4887 | 2011-11-11 14:41:59 -0800 | [diff] [blame] | 612 | chunk_dealloc((void *)spare, chunksize, true); |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 613 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 614 | if (config_stats) |
| 615 | arena->stats.mapped -= chunksize; |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 616 | } else |
| 617 | arena->spare = chunk; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static arena_run_t * |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 621 | arena_run_alloc_helper(arena_t *arena, size_t size, bool large, size_t binind, |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 622 | bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 623 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 624 | arena_run_t *run; |
| 625 | arena_chunk_map_t *mapelm, key; |
| 626 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 627 | key.bits = size | CHUNK_MAP_KEY; |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 628 | mapelm = arena_avail_tree_nsearch(&arena->runs_avail, &key); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 629 | if (mapelm != NULL) { |
| 630 | arena_chunk_t *run_chunk = CHUNK_ADDR2BASE(mapelm); |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 631 | size_t pageind = (((uintptr_t)mapelm - |
| 632 | (uintptr_t)run_chunk->map) / sizeof(arena_chunk_map_t)) |
| 633 | + map_bias; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 634 | |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 635 | run = (arena_run_t *)((uintptr_t)run_chunk + (pageind << |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 636 | LG_PAGE)); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 637 | arena_run_split(arena, run, size, large, binind, zero); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 638 | return (run); |
| 639 | } |
| 640 | |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 641 | return (NULL); |
| 642 | } |
| 643 | |
| 644 | static arena_run_t * |
| 645 | arena_run_alloc(arena_t *arena, size_t size, bool large, size_t binind, |
| 646 | bool zero) |
| 647 | { |
| 648 | arena_chunk_t *chunk; |
| 649 | arena_run_t *run; |
| 650 | |
| 651 | assert(size <= arena_maxclass); |
| 652 | assert((size & PAGE_MASK) == 0); |
| 653 | assert((large && binind == BININD_INVALID) || (large == false && binind |
| 654 | != BININD_INVALID)); |
| 655 | |
| 656 | /* Search the arena's chunks for the lowest best fit. */ |
| 657 | run = arena_run_alloc_helper(arena, size, large, binind, zero); |
| 658 | if (run != NULL) |
| 659 | return (run); |
| 660 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 661 | /* |
| 662 | * No usable runs. Create a new chunk from which to allocate the run. |
| 663 | */ |
| 664 | chunk = arena_chunk_alloc(arena); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 665 | if (chunk != NULL) { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 666 | run = (arena_run_t *)((uintptr_t)chunk + (map_bias << LG_PAGE)); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 667 | arena_run_split(arena, run, size, large, binind, zero); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 668 | return (run); |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * arena_chunk_alloc() failed, but another thread may have made |
| 673 | * sufficient memory available while this one dropped arena->lock in |
| 674 | * arena_chunk_alloc(), so search one more time. |
| 675 | */ |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 676 | return (arena_run_alloc_helper(arena, size, large, binind, zero)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 679 | static inline void |
| 680 | arena_maybe_purge(arena_t *arena) |
| 681 | { |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 682 | size_t npurgeable, threshold; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 683 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 684 | /* Don't purge if the option is disabled. */ |
| 685 | if (opt_lg_dirty_mult < 0) |
| 686 | return; |
| 687 | /* Don't purge if all dirty pages are already being purged. */ |
| 688 | if (arena->ndirty <= arena->npurgatory) |
| 689 | return; |
| 690 | npurgeable = arena->ndirty - arena->npurgatory; |
| 691 | threshold = (arena->nactive >> opt_lg_dirty_mult); |
| 692 | /* |
| 693 | * Don't purge unless the number of purgeable pages exceeds the |
| 694 | * threshold. |
| 695 | */ |
| 696 | if (npurgeable <= threshold) |
| 697 | return; |
| 698 | |
| 699 | arena_purge(arena, false); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 702 | static inline size_t |
| 703 | arena_chunk_purge(arena_t *arena, arena_chunk_t *chunk, bool all) |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 704 | { |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 705 | size_t npurged; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 706 | ql_head(arena_chunk_map_t) mapelms; |
| 707 | arena_chunk_map_t *mapelm; |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 708 | size_t pageind, npages; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 709 | size_t nmadvise; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 710 | |
| 711 | ql_new(&mapelms); |
| 712 | |
| 713 | /* |
| 714 | * If chunk is the spare, temporarily re-allocate it, 1) so that its |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 715 | * run is reinserted into runs_avail, and 2) so that it cannot be |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 716 | * completely discarded by another thread while arena->lock is dropped |
| 717 | * by this thread. Note that the arena_run_dalloc() call will |
| 718 | * implicitly deallocate the chunk, so no explicit action is required |
| 719 | * in this function to deallocate the chunk. |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 720 | * |
| 721 | * Note that once a chunk contains dirty pages, it cannot again contain |
| 722 | * a single run unless 1) it is a dirty run, or 2) this function purges |
| 723 | * dirty pages and causes the transition to a single clean run. Thus |
| 724 | * (chunk == arena->spare) is possible, but it is not possible for |
| 725 | * this function to be called on the spare unless it contains a dirty |
| 726 | * run. |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 727 | */ |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 728 | if (chunk == arena->spare) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 729 | assert(arena_mapbits_dirty_get(chunk, map_bias) != 0); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 730 | assert(arena_mapbits_dirty_get(chunk, chunk_npages-1) != 0); |
| 731 | |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 732 | arena_chunk_alloc(arena); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 733 | } |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 734 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 735 | if (config_stats) |
| 736 | arena->stats.purged += chunk->ndirty; |
| 737 | |
| 738 | /* |
| 739 | * Operate on all dirty runs if there is no clean/dirty run |
| 740 | * fragmentation. |
| 741 | */ |
| 742 | if (chunk->nruns_adjac == 0) |
| 743 | all = true; |
| 744 | |
| 745 | /* |
| 746 | * Temporarily allocate free dirty runs within chunk. If all is false, |
| 747 | * only operate on dirty runs that are fragments; otherwise operate on |
| 748 | * all dirty runs. |
| 749 | */ |
| 750 | for (pageind = map_bias; pageind < chunk_npages; pageind += npages) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 751 | mapelm = arena_mapp_get(chunk, pageind); |
| 752 | if (arena_mapbits_allocated_get(chunk, pageind) == 0) { |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 753 | size_t run_size = |
| 754 | arena_mapbits_unallocated_size_get(chunk, pageind); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 755 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 756 | npages = run_size >> LG_PAGE; |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 757 | assert(pageind + npages <= chunk_npages); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 758 | assert(arena_mapbits_dirty_get(chunk, pageind) == |
| 759 | arena_mapbits_dirty_get(chunk, pageind+npages-1)); |
Jason Evans | c03a63d | 2010-03-22 11:45:01 -0700 | [diff] [blame] | 760 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 761 | if (arena_mapbits_dirty_get(chunk, pageind) != 0 && |
| 762 | (all || arena_avail_adjac(chunk, pageind, |
| 763 | npages))) { |
| 764 | arena_run_t *run = (arena_run_t *)((uintptr_t) |
| 765 | chunk + (uintptr_t)(pageind << LG_PAGE)); |
Jason Evans | c03a63d | 2010-03-22 11:45:01 -0700 | [diff] [blame] | 766 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 767 | arena_run_split(arena, run, run_size, true, |
| 768 | BININD_INVALID, false); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 769 | /* Append to list for later processing. */ |
| 770 | ql_elm_new(mapelm, u.ql_link); |
| 771 | ql_tail_insert(&mapelms, mapelm, u.ql_link); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 772 | } |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 773 | } else { |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 774 | /* Skip run. */ |
| 775 | if (arena_mapbits_large_get(chunk, pageind) != 0) { |
| 776 | npages = arena_mapbits_large_size_get(chunk, |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 777 | pageind) >> LG_PAGE; |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 778 | } else { |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 779 | size_t binind; |
| 780 | arena_bin_info_t *bin_info; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 781 | arena_run_t *run = (arena_run_t *)((uintptr_t) |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 782 | chunk + (uintptr_t)(pageind << LG_PAGE)); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 783 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 784 | assert(arena_mapbits_small_runind_get(chunk, |
| 785 | pageind) == 0); |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 786 | binind = arena_bin_index(arena, run->bin); |
| 787 | bin_info = &arena_bin_info[binind]; |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 788 | npages = bin_info->run_size >> LG_PAGE; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | } |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 792 | assert(pageind == chunk_npages); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 793 | assert(chunk->ndirty == 0 || all == false); |
| 794 | assert(chunk->nruns_adjac == 0); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 795 | |
| 796 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 797 | if (config_stats) |
| 798 | nmadvise = 0; |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 799 | npurged = 0; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 800 | ql_foreach(mapelm, &mapelms, u.ql_link) { |
Jason Evans | 7de9276 | 2012-10-08 17:56:11 -0700 | [diff] [blame] | 801 | bool unzeroed; |
| 802 | size_t flag_unzeroed, i; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 803 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 804 | pageind = (((uintptr_t)mapelm - (uintptr_t)chunk->map) / |
| 805 | sizeof(arena_chunk_map_t)) + map_bias; |
| 806 | npages = arena_mapbits_large_size_get(chunk, pageind) >> |
| 807 | LG_PAGE; |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 808 | assert(pageind + npages <= chunk_npages); |
Jason Evans | 7de9276 | 2012-10-08 17:56:11 -0700 | [diff] [blame] | 809 | unzeroed = pages_purge((void *)((uintptr_t)chunk + (pageind << |
| 810 | LG_PAGE)), (npages << LG_PAGE)); |
| 811 | flag_unzeroed = unzeroed ? CHUNK_MAP_UNZEROED : 0; |
| 812 | /* |
| 813 | * Set the unzeroed flag for all pages, now that pages_purge() |
| 814 | * has returned whether the pages were zeroed as a side effect |
| 815 | * of purging. This chunk map modification is safe even though |
| 816 | * the arena mutex isn't currently owned by this thread, |
| 817 | * because the run is marked as allocated, thus protecting it |
| 818 | * from being modified by any other thread. As long as these |
| 819 | * writes don't perturb the first and last elements' |
| 820 | * CHUNK_MAP_ALLOCATED bits, behavior is well defined. |
| 821 | */ |
| 822 | for (i = 0; i < npages; i++) { |
| 823 | arena_mapbits_unzeroed_set(chunk, pageind+i, |
| 824 | flag_unzeroed); |
| 825 | } |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 826 | npurged += npages; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 827 | if (config_stats) |
| 828 | nmadvise++; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 829 | } |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 830 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 831 | if (config_stats) |
| 832 | arena->stats.nmadvise += nmadvise; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 833 | |
| 834 | /* Deallocate runs. */ |
| 835 | for (mapelm = ql_first(&mapelms); mapelm != NULL; |
| 836 | mapelm = ql_first(&mapelms)) { |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 837 | arena_run_t *run; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 838 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 839 | pageind = (((uintptr_t)mapelm - (uintptr_t)chunk->map) / |
| 840 | sizeof(arena_chunk_map_t)) + map_bias; |
| 841 | run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)(pageind << |
| 842 | LG_PAGE)); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 843 | ql_remove(&mapelms, mapelm, u.ql_link); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 844 | arena_run_dalloc(arena, run, false, true); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 845 | } |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 846 | |
| 847 | return (npurged); |
| 848 | } |
| 849 | |
| 850 | static arena_chunk_t * |
| 851 | chunks_dirty_iter_cb(arena_chunk_tree_t *tree, arena_chunk_t *chunk, void *arg) |
| 852 | { |
| 853 | size_t *ndirty = (size_t *)arg; |
| 854 | |
| 855 | assert(chunk->ndirty != 0); |
| 856 | *ndirty += chunk->ndirty; |
| 857 | return (NULL); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 860 | static void |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 861 | arena_purge(arena_t *arena, bool all) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 862 | { |
| 863 | arena_chunk_t *chunk; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 864 | size_t npurgatory; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 865 | if (config_debug) { |
| 866 | size_t ndirty = 0; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 867 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 868 | arena_chunk_dirty_iter(&arena->chunks_dirty, NULL, |
| 869 | chunks_dirty_iter_cb, (void *)&ndirty); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 870 | assert(ndirty == arena->ndirty); |
Jason Evans | 2caa471 | 2010-03-04 21:35:07 -0800 | [diff] [blame] | 871 | } |
Jason Evans | af8ad3e | 2011-03-23 20:39:02 -0700 | [diff] [blame] | 872 | assert(arena->ndirty > arena->npurgatory || all); |
Jason Evans | af8ad3e | 2011-03-23 20:39:02 -0700 | [diff] [blame] | 873 | assert((arena->nactive >> opt_lg_dirty_mult) < (arena->ndirty - |
Jason Evans | f9a8edb | 2011-06-12 16:46:03 -0700 | [diff] [blame] | 874 | arena->npurgatory) || all); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 875 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 876 | if (config_stats) |
| 877 | arena->stats.npurge++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 878 | |
| 879 | /* |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 880 | * Compute the minimum number of pages that this thread should try to |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 881 | * purge, and add the result to arena->npurgatory. This will keep |
| 882 | * multiple threads from racing to reduce ndirty below the threshold. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 883 | */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 884 | { |
| 885 | size_t npurgeable = arena->ndirty - arena->npurgatory; |
| 886 | |
| 887 | if (all == false) { |
| 888 | size_t threshold = (arena->nactive >> |
| 889 | opt_lg_dirty_mult); |
| 890 | |
| 891 | npurgatory = npurgeable - threshold; |
| 892 | } else |
| 893 | npurgatory = npurgeable; |
Jason Evans | af8ad3e | 2011-03-23 20:39:02 -0700 | [diff] [blame] | 894 | } |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 895 | arena->npurgatory += npurgatory; |
| 896 | |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 897 | while (npurgatory > 0) { |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 898 | size_t npurgeable, npurged, nunpurged; |
| 899 | |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 900 | /* Get next chunk with dirty pages. */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 901 | chunk = arena_chunk_dirty_first(&arena->chunks_dirty); |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 902 | if (chunk == NULL) { |
| 903 | /* |
| 904 | * This thread was unable to purge as many pages as |
| 905 | * originally intended, due to races with other threads |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 906 | * that either did some of the purging work, or re-used |
| 907 | * dirty pages. |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 908 | */ |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 909 | arena->npurgatory -= npurgatory; |
| 910 | return; |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 911 | } |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 912 | npurgeable = chunk->ndirty; |
| 913 | assert(npurgeable != 0); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 914 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 915 | if (npurgeable > npurgatory && chunk->nruns_adjac == 0) { |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 916 | /* |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 917 | * This thread will purge all the dirty pages in chunk, |
| 918 | * so set npurgatory to reflect this thread's intent to |
| 919 | * purge the pages. This tends to reduce the chances |
| 920 | * of the following scenario: |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 921 | * |
| 922 | * 1) This thread sets arena->npurgatory such that |
| 923 | * (arena->ndirty - arena->npurgatory) is at the |
| 924 | * threshold. |
| 925 | * 2) This thread drops arena->lock. |
| 926 | * 3) Another thread causes one or more pages to be |
| 927 | * dirtied, and immediately determines that it must |
| 928 | * purge dirty pages. |
| 929 | * |
| 930 | * If this scenario *does* play out, that's okay, |
| 931 | * because all of the purging work being done really |
| 932 | * needs to happen. |
| 933 | */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 934 | arena->npurgatory += npurgeable - npurgatory; |
| 935 | npurgatory = npurgeable; |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 938 | /* |
| 939 | * Keep track of how many pages are purgeable, versus how many |
| 940 | * actually get purged, and adjust counters accordingly. |
| 941 | */ |
| 942 | arena->npurgatory -= npurgeable; |
| 943 | npurgatory -= npurgeable; |
| 944 | npurged = arena_chunk_purge(arena, chunk, all); |
| 945 | nunpurged = npurgeable - npurged; |
| 946 | arena->npurgatory += nunpurged; |
| 947 | npurgatory += nunpurged; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 948 | } |
| 949 | } |
| 950 | |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 951 | void |
| 952 | arena_purge_all(arena_t *arena) |
| 953 | { |
| 954 | |
| 955 | malloc_mutex_lock(&arena->lock); |
| 956 | arena_purge(arena, true); |
| 957 | malloc_mutex_unlock(&arena->lock); |
| 958 | } |
| 959 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 960 | static void |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 961 | arena_run_dalloc(arena_t *arena, arena_run_t *run, bool dirty, bool cleaned) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 962 | { |
| 963 | arena_chunk_t *chunk; |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 964 | size_t size, run_ind, run_pages, flag_dirty; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 965 | |
| 966 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 967 | run_ind = (size_t)(((uintptr_t)run - (uintptr_t)chunk) >> LG_PAGE); |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 968 | assert(run_ind >= map_bias); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 969 | assert(run_ind < chunk_npages); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 970 | if (arena_mapbits_large_get(chunk, run_ind) != 0) { |
| 971 | size = arena_mapbits_large_size_get(chunk, run_ind); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 972 | assert(size == PAGE || |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 973 | arena_mapbits_large_size_get(chunk, |
| 974 | run_ind+(size>>LG_PAGE)-1) == 0); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 975 | } else { |
| 976 | size_t binind = arena_bin_index(arena, run->bin); |
| 977 | arena_bin_info_t *bin_info = &arena_bin_info[binind]; |
| 978 | size = bin_info->run_size; |
| 979 | } |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 980 | run_pages = (size >> LG_PAGE); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 981 | if (config_stats) { |
| 982 | /* |
| 983 | * Update stats_cactive if nactive is crossing a chunk |
| 984 | * multiple. |
| 985 | */ |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 986 | size_t cactive_diff = CHUNK_CEILING(arena->nactive << LG_PAGE) - |
| 987 | CHUNK_CEILING((arena->nactive - run_pages) << LG_PAGE); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 988 | if (cactive_diff != 0) |
| 989 | stats_cactive_sub(cactive_diff); |
| 990 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 991 | arena->nactive -= run_pages; |
| 992 | |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 993 | /* |
| 994 | * The run is dirty if the caller claims to have dirtied it, as well as |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 995 | * if it was already dirty before being allocated and the caller |
| 996 | * doesn't claim to have cleaned it. |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 997 | */ |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 998 | assert(arena_mapbits_dirty_get(chunk, run_ind) == |
| 999 | arena_mapbits_dirty_get(chunk, run_ind+run_pages-1)); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1000 | if (cleaned == false && arena_mapbits_dirty_get(chunk, run_ind) != 0) |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 1001 | dirty = true; |
| 1002 | flag_dirty = dirty ? CHUNK_MAP_DIRTY : 0; |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 1003 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1004 | /* Mark pages as unallocated in the chunk map. */ |
| 1005 | if (dirty) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1006 | arena_mapbits_unallocated_set(chunk, run_ind, size, |
| 1007 | CHUNK_MAP_DIRTY); |
| 1008 | arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size, |
| 1009 | CHUNK_MAP_DIRTY); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1010 | } else { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1011 | arena_mapbits_unallocated_set(chunk, run_ind, size, |
| 1012 | arena_mapbits_unzeroed_get(chunk, run_ind)); |
| 1013 | arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size, |
| 1014 | arena_mapbits_unzeroed_get(chunk, run_ind+run_pages-1)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1015 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1016 | |
| 1017 | /* Try to coalesce forward. */ |
| 1018 | if (run_ind + run_pages < chunk_npages && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1019 | arena_mapbits_allocated_get(chunk, run_ind+run_pages) == 0 && |
| 1020 | arena_mapbits_dirty_get(chunk, run_ind+run_pages) == flag_dirty) { |
| 1021 | size_t nrun_size = arena_mapbits_unallocated_size_get(chunk, |
| 1022 | run_ind+run_pages); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1023 | size_t nrun_pages = nrun_size >> LG_PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1024 | |
| 1025 | /* |
| 1026 | * Remove successor from runs_avail; the coalesced run is |
| 1027 | * inserted later. |
| 1028 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1029 | assert(arena_mapbits_unallocated_size_get(chunk, |
| 1030 | run_ind+run_pages+nrun_pages-1) == nrun_size); |
| 1031 | assert(arena_mapbits_dirty_get(chunk, |
| 1032 | run_ind+run_pages+nrun_pages-1) == flag_dirty); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1033 | arena_avail_remove(arena, chunk, run_ind+run_pages, nrun_pages, |
| 1034 | false, true); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1035 | |
| 1036 | size += nrun_size; |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 1037 | run_pages += nrun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1038 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1039 | arena_mapbits_unallocated_size_set(chunk, run_ind, size); |
| 1040 | arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1, |
| 1041 | size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /* Try to coalesce backward. */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1045 | if (run_ind > map_bias && arena_mapbits_allocated_get(chunk, run_ind-1) |
| 1046 | == 0 && arena_mapbits_dirty_get(chunk, run_ind-1) == flag_dirty) { |
| 1047 | size_t prun_size = arena_mapbits_unallocated_size_get(chunk, |
| 1048 | run_ind-1); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1049 | size_t prun_pages = prun_size >> LG_PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1050 | |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 1051 | run_ind -= prun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1052 | |
| 1053 | /* |
| 1054 | * Remove predecessor from runs_avail; the coalesced run is |
| 1055 | * inserted later. |
| 1056 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1057 | assert(arena_mapbits_unallocated_size_get(chunk, run_ind) == |
| 1058 | prun_size); |
| 1059 | assert(arena_mapbits_dirty_get(chunk, run_ind) == flag_dirty); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1060 | arena_avail_remove(arena, chunk, run_ind, prun_pages, true, |
| 1061 | false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1062 | |
| 1063 | size += prun_size; |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 1064 | run_pages += prun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1065 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1066 | arena_mapbits_unallocated_size_set(chunk, run_ind, size); |
| 1067 | arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1, |
| 1068 | size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /* Insert into runs_avail, now that coalescing is complete. */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1072 | assert(arena_mapbits_unallocated_size_get(chunk, run_ind) == |
| 1073 | arena_mapbits_unallocated_size_get(chunk, run_ind+run_pages-1)); |
| 1074 | assert(arena_mapbits_dirty_get(chunk, run_ind) == |
| 1075 | arena_mapbits_dirty_get(chunk, run_ind+run_pages-1)); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1076 | arena_avail_insert(arena, chunk, run_ind, run_pages, true, true); |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 1077 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1078 | /* Deallocate chunk if it is now completely unused. */ |
| 1079 | if (size == arena_maxclass) { |
| 1080 | assert(run_ind == map_bias); |
| 1081 | assert(run_pages == (arena_maxclass >> LG_PAGE)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1082 | arena_chunk_dealloc(arena, chunk); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1083 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1084 | |
Jason Evans | 4fb7f51 | 2010-01-27 18:27:09 -0800 | [diff] [blame] | 1085 | /* |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 1086 | * 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] | 1087 | * deallocated above, since in that case it is the spare. Waiting |
| 1088 | * until after possible chunk deallocation to do dirty processing |
| 1089 | * allows for an old spare to be fully deallocated, thus decreasing the |
| 1090 | * chances of spuriously crossing the dirty page purging threshold. |
| 1091 | */ |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 1092 | if (dirty) |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 1093 | arena_maybe_purge(arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | static void |
| 1097 | arena_run_trim_head(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 1098 | size_t oldsize, size_t newsize) |
| 1099 | { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1100 | size_t pageind = ((uintptr_t)run - (uintptr_t)chunk) >> LG_PAGE; |
| 1101 | size_t head_npages = (oldsize - newsize) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1102 | size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1103 | |
| 1104 | assert(oldsize > newsize); |
| 1105 | |
| 1106 | /* |
| 1107 | * Update the chunk map so that arena_run_dalloc() can treat the |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1108 | * leading run as separately allocated. Set the last element of each |
| 1109 | * run first, in case of single-page runs. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1110 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1111 | assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1112 | arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty); |
| 1113 | arena_mapbits_large_set(chunk, pageind, oldsize-newsize, flag_dirty); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1114 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1115 | if (config_debug) { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1116 | UNUSED size_t tail_npages = newsize >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1117 | assert(arena_mapbits_large_size_get(chunk, |
| 1118 | pageind+head_npages+tail_npages-1) == 0); |
| 1119 | assert(arena_mapbits_dirty_get(chunk, |
| 1120 | pageind+head_npages+tail_npages-1) == flag_dirty); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1121 | } |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1122 | arena_mapbits_large_set(chunk, pageind+head_npages, newsize, |
| 1123 | flag_dirty); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1124 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1125 | arena_run_dalloc(arena, run, false, false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | static void |
| 1129 | arena_run_trim_tail(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 1130 | size_t oldsize, size_t newsize, bool dirty) |
| 1131 | { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1132 | size_t pageind = ((uintptr_t)run - (uintptr_t)chunk) >> LG_PAGE; |
| 1133 | size_t head_npages = newsize >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1134 | size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1135 | |
| 1136 | assert(oldsize > newsize); |
| 1137 | |
| 1138 | /* |
| 1139 | * Update the chunk map so that arena_run_dalloc() can treat the |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1140 | * trailing run as separately allocated. Set the last element of each |
| 1141 | * run first, in case of single-page runs. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1142 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1143 | assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1144 | arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty); |
| 1145 | arena_mapbits_large_set(chunk, pageind, newsize, flag_dirty); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1146 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1147 | if (config_debug) { |
| 1148 | UNUSED size_t tail_npages = (oldsize - newsize) >> LG_PAGE; |
| 1149 | assert(arena_mapbits_large_size_get(chunk, |
| 1150 | pageind+head_npages+tail_npages-1) == 0); |
| 1151 | assert(arena_mapbits_dirty_get(chunk, |
| 1152 | pageind+head_npages+tail_npages-1) == flag_dirty); |
| 1153 | } |
| 1154 | arena_mapbits_large_set(chunk, pageind+head_npages, oldsize-newsize, |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1155 | flag_dirty); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1156 | |
| 1157 | arena_run_dalloc(arena, (arena_run_t *)((uintptr_t)run + newsize), |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1158 | dirty, false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | static arena_run_t * |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1162 | arena_bin_runs_first(arena_bin_t *bin) |
| 1163 | { |
| 1164 | arena_chunk_map_t *mapelm = arena_run_tree_first(&bin->runs); |
| 1165 | if (mapelm != NULL) { |
| 1166 | arena_chunk_t *chunk; |
| 1167 | size_t pageind; |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 1168 | arena_run_t *run; |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1169 | |
| 1170 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(mapelm); |
| 1171 | pageind = ((((uintptr_t)mapelm - (uintptr_t)chunk->map) / |
| 1172 | sizeof(arena_chunk_map_t))) + map_bias; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1173 | run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind - |
| 1174 | arena_mapbits_small_runind_get(chunk, pageind)) << |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1175 | LG_PAGE)); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1176 | return (run); |
| 1177 | } |
| 1178 | |
| 1179 | return (NULL); |
| 1180 | } |
| 1181 | |
| 1182 | static void |
| 1183 | arena_bin_runs_insert(arena_bin_t *bin, arena_run_t *run) |
| 1184 | { |
| 1185 | arena_chunk_t *chunk = CHUNK_ADDR2BASE(run); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1186 | size_t pageind = ((uintptr_t)run - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1187 | arena_chunk_map_t *mapelm = arena_mapp_get(chunk, pageind); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1188 | |
| 1189 | assert(arena_run_tree_search(&bin->runs, mapelm) == NULL); |
| 1190 | |
| 1191 | arena_run_tree_insert(&bin->runs, mapelm); |
| 1192 | } |
| 1193 | |
| 1194 | static void |
| 1195 | arena_bin_runs_remove(arena_bin_t *bin, arena_run_t *run) |
| 1196 | { |
| 1197 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1198 | size_t pageind = ((uintptr_t)run - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1199 | arena_chunk_map_t *mapelm = arena_mapp_get(chunk, pageind); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1200 | |
| 1201 | assert(arena_run_tree_search(&bin->runs, mapelm) != NULL); |
| 1202 | |
| 1203 | arena_run_tree_remove(&bin->runs, mapelm); |
| 1204 | } |
| 1205 | |
| 1206 | static arena_run_t * |
| 1207 | arena_bin_nonfull_run_tryget(arena_bin_t *bin) |
| 1208 | { |
| 1209 | arena_run_t *run = arena_bin_runs_first(bin); |
| 1210 | if (run != NULL) { |
| 1211 | arena_bin_runs_remove(bin, run); |
| 1212 | if (config_stats) |
| 1213 | bin->stats.reruns++; |
| 1214 | } |
| 1215 | return (run); |
| 1216 | } |
| 1217 | |
| 1218 | static arena_run_t * |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1219 | arena_bin_nonfull_run_get(arena_t *arena, arena_bin_t *bin) |
| 1220 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1221 | arena_run_t *run; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1222 | size_t binind; |
| 1223 | arena_bin_info_t *bin_info; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1224 | |
| 1225 | /* Look for a usable run. */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1226 | run = arena_bin_nonfull_run_tryget(bin); |
| 1227 | if (run != NULL) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1228 | return (run); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1229 | /* No existing runs have any space available. */ |
| 1230 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1231 | binind = arena_bin_index(arena, bin); |
| 1232 | bin_info = &arena_bin_info[binind]; |
| 1233 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1234 | /* Allocate a new run. */ |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1235 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 1236 | /******************************/ |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1237 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1238 | run = arena_run_alloc(arena, bin_info->run_size, false, binind, false); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1239 | if (run != NULL) { |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 1240 | bitmap_t *bitmap = (bitmap_t *)((uintptr_t)run + |
| 1241 | (uintptr_t)bin_info->bitmap_offset); |
| 1242 | |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1243 | /* Initialize run internals. */ |
Jason Evans | f54166e | 2012-04-23 22:41:36 -0700 | [diff] [blame] | 1244 | VALGRIND_MAKE_MEM_UNDEFINED(run, bin_info->reg0_offset - |
| 1245 | bin_info->redzone_size); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1246 | run->bin = bin; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 1247 | run->nextind = 0; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1248 | run->nfree = bin_info->nregs; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 1249 | bitmap_init(bitmap, &bin_info->bitmap_info); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 1250 | } |
| 1251 | malloc_mutex_unlock(&arena->lock); |
| 1252 | /********************************/ |
| 1253 | malloc_mutex_lock(&bin->lock); |
| 1254 | if (run != NULL) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1255 | if (config_stats) { |
| 1256 | bin->stats.nruns++; |
| 1257 | bin->stats.curruns++; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1258 | } |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1259 | return (run); |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * arena_run_alloc() failed, but another thread may have made |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1264 | * sufficient memory available while this one dropped bin->lock above, |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1265 | * so search one more time. |
| 1266 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1267 | run = arena_bin_nonfull_run_tryget(bin); |
| 1268 | if (run != NULL) |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1269 | return (run); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1270 | |
| 1271 | return (NULL); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 1274 | /* Re-fill bin->runcur, then call arena_run_reg_alloc(). */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1275 | static void * |
| 1276 | arena_bin_malloc_hard(arena_t *arena, arena_bin_t *bin) |
| 1277 | { |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1278 | void *ret; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1279 | size_t binind; |
| 1280 | arena_bin_info_t *bin_info; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1281 | arena_run_t *run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1282 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1283 | binind = arena_bin_index(arena, bin); |
| 1284 | bin_info = &arena_bin_info[binind]; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1285 | bin->runcur = NULL; |
| 1286 | run = arena_bin_nonfull_run_get(arena, bin); |
| 1287 | if (bin->runcur != NULL && bin->runcur->nfree > 0) { |
| 1288 | /* |
| 1289 | * Another thread updated runcur while this one ran without the |
| 1290 | * bin lock in arena_bin_nonfull_run_get(). |
| 1291 | */ |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1292 | assert(bin->runcur->nfree > 0); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1293 | ret = arena_run_reg_alloc(bin->runcur, bin_info); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1294 | if (run != NULL) { |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1295 | arena_chunk_t *chunk; |
| 1296 | |
| 1297 | /* |
| 1298 | * arena_run_alloc() may have allocated run, or it may |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 1299 | * have pulled run from the bin's run tree. Therefore |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1300 | * it is unsafe to make any assumptions about how run |
| 1301 | * has previously been used, and arena_bin_lower_run() |
| 1302 | * must be called, as if a region were just deallocated |
| 1303 | * from the run. |
| 1304 | */ |
| 1305 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1306 | if (run->nfree == bin_info->nregs) |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1307 | arena_dalloc_bin_run(arena, chunk, run, bin); |
| 1308 | else |
| 1309 | arena_bin_lower_run(arena, chunk, run, bin); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1310 | } |
| 1311 | return (ret); |
| 1312 | } |
| 1313 | |
| 1314 | if (run == NULL) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1315 | return (NULL); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1316 | |
| 1317 | bin->runcur = run; |
| 1318 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1319 | assert(bin->runcur->nfree > 0); |
| 1320 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1321 | return (arena_run_reg_alloc(bin->runcur, bin_info)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1324 | void |
| 1325 | arena_prof_accum(arena_t *arena, uint64_t accumbytes) |
| 1326 | { |
| 1327 | |
Jason Evans | 78f7352 | 2012-04-18 13:38:40 -0700 | [diff] [blame] | 1328 | cassert(config_prof); |
| 1329 | |
| 1330 | if (config_prof && prof_interval != 0) { |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1331 | arena->prof_accumbytes += accumbytes; |
| 1332 | if (arena->prof_accumbytes >= prof_interval) { |
| 1333 | prof_idump(); |
| 1334 | arena->prof_accumbytes -= prof_interval; |
| 1335 | } |
| 1336 | } |
| 1337 | } |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1338 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1339 | void |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1340 | arena_tcache_fill_small(arena_t *arena, tcache_bin_t *tbin, size_t binind, |
| 1341 | uint64_t prof_accumbytes) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1342 | { |
| 1343 | unsigned i, nfill; |
| 1344 | arena_bin_t *bin; |
| 1345 | arena_run_t *run; |
| 1346 | void *ptr; |
| 1347 | |
| 1348 | assert(tbin->ncached == 0); |
| 1349 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1350 | if (config_prof) { |
| 1351 | malloc_mutex_lock(&arena->lock); |
| 1352 | arena_prof_accum(arena, prof_accumbytes); |
| 1353 | malloc_mutex_unlock(&arena->lock); |
| 1354 | } |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 1355 | bin = &arena->bins[binind]; |
| 1356 | malloc_mutex_lock(&bin->lock); |
Jason Evans | 1dcb4f8 | 2011-03-21 00:18:17 -0700 | [diff] [blame] | 1357 | for (i = 0, nfill = (tcache_bin_info[binind].ncached_max >> |
| 1358 | tbin->lg_fill_div); i < nfill; i++) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1359 | if ((run = bin->runcur) != NULL && run->nfree > 0) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1360 | ptr = arena_run_reg_alloc(run, &arena_bin_info[binind]); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1361 | else |
| 1362 | ptr = arena_bin_malloc_hard(arena, bin); |
Jason Evans | 3fa9a2f | 2010-03-07 15:34:14 -0800 | [diff] [blame] | 1363 | if (ptr == NULL) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1364 | break; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1365 | if (config_fill && opt_junk) { |
| 1366 | arena_alloc_junk_small(ptr, &arena_bin_info[binind], |
| 1367 | true); |
| 1368 | } |
Jason Evans | 9c43c13 | 2011-03-18 10:53:15 -0700 | [diff] [blame] | 1369 | /* Insert such that low regions get used first. */ |
| 1370 | tbin->avail[nfill - 1 - i] = ptr; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1371 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1372 | if (config_stats) { |
| 1373 | bin->stats.allocated += i * arena_bin_info[binind].reg_size; |
| 1374 | bin->stats.nmalloc += i; |
| 1375 | bin->stats.nrequests += tbin->tstats.nrequests; |
| 1376 | bin->stats.nfills++; |
| 1377 | tbin->tstats.nrequests = 0; |
| 1378 | } |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1379 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1380 | tbin->ncached = i; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1381 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1382 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1383 | void |
| 1384 | arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info, bool zero) |
| 1385 | { |
| 1386 | |
| 1387 | if (zero) { |
| 1388 | size_t redzone_size = bin_info->redzone_size; |
| 1389 | memset((void *)((uintptr_t)ptr - redzone_size), 0xa5, |
| 1390 | redzone_size); |
| 1391 | memset((void *)((uintptr_t)ptr + bin_info->reg_size), 0xa5, |
| 1392 | redzone_size); |
| 1393 | } else { |
| 1394 | memset((void *)((uintptr_t)ptr - bin_info->redzone_size), 0xa5, |
| 1395 | bin_info->reg_interval); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | void |
| 1400 | arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info) |
| 1401 | { |
| 1402 | size_t size = bin_info->reg_size; |
| 1403 | size_t redzone_size = bin_info->redzone_size; |
| 1404 | size_t i; |
| 1405 | bool error = false; |
| 1406 | |
| 1407 | for (i = 1; i <= redzone_size; i++) { |
| 1408 | unsigned byte; |
| 1409 | if ((byte = *(uint8_t *)((uintptr_t)ptr - i)) != 0xa5) { |
| 1410 | error = true; |
| 1411 | malloc_printf("<jemalloc>: Corrupt redzone " |
| 1412 | "%zu byte%s before %p (size %zu), byte=%#x\n", i, |
| 1413 | (i == 1) ? "" : "s", ptr, size, byte); |
| 1414 | } |
| 1415 | } |
| 1416 | for (i = 0; i < redzone_size; i++) { |
| 1417 | unsigned byte; |
| 1418 | if ((byte = *(uint8_t *)((uintptr_t)ptr + size + i)) != 0xa5) { |
| 1419 | error = true; |
| 1420 | malloc_printf("<jemalloc>: Corrupt redzone " |
| 1421 | "%zu byte%s after end of %p (size %zu), byte=%#x\n", |
| 1422 | i, (i == 1) ? "" : "s", ptr, size, byte); |
| 1423 | } |
| 1424 | } |
| 1425 | if (opt_abort && error) |
| 1426 | abort(); |
| 1427 | |
| 1428 | memset((void *)((uintptr_t)ptr - redzone_size), 0x5a, |
| 1429 | bin_info->reg_interval); |
| 1430 | } |
| 1431 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1432 | void * |
| 1433 | arena_malloc_small(arena_t *arena, size_t size, bool zero) |
| 1434 | { |
| 1435 | void *ret; |
| 1436 | arena_bin_t *bin; |
| 1437 | arena_run_t *run; |
| 1438 | size_t binind; |
| 1439 | |
Jason Evans | 41ade96 | 2011-03-06 22:56:36 -0800 | [diff] [blame] | 1440 | binind = SMALL_SIZE2BIN(size); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1441 | assert(binind < NBINS); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1442 | bin = &arena->bins[binind]; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1443 | size = arena_bin_info[binind].reg_size; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1444 | |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1445 | malloc_mutex_lock(&bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1446 | if ((run = bin->runcur) != NULL && run->nfree > 0) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1447 | ret = arena_run_reg_alloc(run, &arena_bin_info[binind]); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1448 | else |
| 1449 | ret = arena_bin_malloc_hard(arena, bin); |
| 1450 | |
| 1451 | if (ret == NULL) { |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1452 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1453 | return (NULL); |
| 1454 | } |
| 1455 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1456 | if (config_stats) { |
| 1457 | bin->stats.allocated += size; |
| 1458 | bin->stats.nmalloc++; |
| 1459 | bin->stats.nrequests++; |
| 1460 | } |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1461 | malloc_mutex_unlock(&bin->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1462 | if (config_prof && isthreaded == false) { |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1463 | malloc_mutex_lock(&arena->lock); |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 1464 | arena_prof_accum(arena, size); |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1465 | malloc_mutex_unlock(&arena->lock); |
| 1466 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1467 | |
| 1468 | if (zero == false) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1469 | if (config_fill) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1470 | if (opt_junk) { |
| 1471 | arena_alloc_junk_small(ret, |
| 1472 | &arena_bin_info[binind], false); |
| 1473 | } else if (opt_zero) |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1474 | memset(ret, 0, size); |
| 1475 | } |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1476 | } else { |
| 1477 | if (config_fill && opt_junk) { |
| 1478 | arena_alloc_junk_small(ret, &arena_bin_info[binind], |
| 1479 | true); |
| 1480 | } |
| 1481 | VALGRIND_MAKE_MEM_UNDEFINED(ret, size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1482 | memset(ret, 0, size); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1483 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1484 | |
| 1485 | return (ret); |
| 1486 | } |
| 1487 | |
| 1488 | void * |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1489 | arena_malloc_large(arena_t *arena, size_t size, bool zero) |
| 1490 | { |
| 1491 | void *ret; |
| 1492 | |
| 1493 | /* Large allocation. */ |
| 1494 | size = PAGE_CEILING(size); |
| 1495 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1496 | ret = (void *)arena_run_alloc(arena, size, true, BININD_INVALID, zero); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1497 | if (ret == NULL) { |
| 1498 | malloc_mutex_unlock(&arena->lock); |
| 1499 | return (NULL); |
| 1500 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1501 | if (config_stats) { |
| 1502 | arena->stats.nmalloc_large++; |
| 1503 | arena->stats.nrequests_large++; |
| 1504 | arena->stats.allocated_large += size; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1505 | arena->stats.lstats[(size >> LG_PAGE) - 1].nmalloc++; |
| 1506 | arena->stats.lstats[(size >> LG_PAGE) - 1].nrequests++; |
| 1507 | arena->stats.lstats[(size >> LG_PAGE) - 1].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1508 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1509 | if (config_prof) |
| 1510 | arena_prof_accum(arena, size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1511 | malloc_mutex_unlock(&arena->lock); |
| 1512 | |
| 1513 | if (zero == false) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1514 | if (config_fill) { |
| 1515 | if (opt_junk) |
| 1516 | memset(ret, 0xa5, size); |
| 1517 | else if (opt_zero) |
| 1518 | memset(ret, 0, size); |
| 1519 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | return (ret); |
| 1523 | } |
| 1524 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1525 | /* Only handles large allocations that require more than page alignment. */ |
| 1526 | void * |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 1527 | arena_palloc(arena_t *arena, size_t size, size_t alignment, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1528 | { |
| 1529 | void *ret; |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 1530 | size_t alloc_size, leadsize, trailsize; |
| 1531 | arena_run_t *run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1532 | arena_chunk_t *chunk; |
| 1533 | |
| 1534 | assert((size & PAGE_MASK) == 0); |
Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 1535 | |
| 1536 | alignment = PAGE_CEILING(alignment); |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 1537 | alloc_size = size + alignment - PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1538 | |
| 1539 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1540 | run = arena_run_alloc(arena, alloc_size, true, BININD_INVALID, zero); |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 1541 | if (run == NULL) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1542 | malloc_mutex_unlock(&arena->lock); |
| 1543 | return (NULL); |
| 1544 | } |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 1545 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1546 | |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 1547 | leadsize = ALIGNMENT_CEILING((uintptr_t)run, alignment) - |
| 1548 | (uintptr_t)run; |
| 1549 | assert(alloc_size >= leadsize + size); |
| 1550 | trailsize = alloc_size - leadsize - size; |
| 1551 | ret = (void *)((uintptr_t)run + leadsize); |
| 1552 | if (leadsize != 0) { |
| 1553 | arena_run_trim_head(arena, chunk, run, alloc_size, alloc_size - |
| 1554 | leadsize); |
| 1555 | } |
| 1556 | if (trailsize != 0) { |
| 1557 | arena_run_trim_tail(arena, chunk, ret, size + trailsize, size, |
| 1558 | false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1561 | if (config_stats) { |
| 1562 | arena->stats.nmalloc_large++; |
| 1563 | arena->stats.nrequests_large++; |
| 1564 | arena->stats.allocated_large += size; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1565 | arena->stats.lstats[(size >> LG_PAGE) - 1].nmalloc++; |
| 1566 | arena->stats.lstats[(size >> LG_PAGE) - 1].nrequests++; |
| 1567 | arena->stats.lstats[(size >> LG_PAGE) - 1].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1568 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1569 | malloc_mutex_unlock(&arena->lock); |
| 1570 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1571 | if (config_fill && zero == false) { |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1572 | if (opt_junk) |
| 1573 | memset(ret, 0xa5, size); |
| 1574 | else if (opt_zero) |
| 1575 | memset(ret, 0, size); |
| 1576 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1577 | return (ret); |
| 1578 | } |
| 1579 | |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1580 | void |
| 1581 | arena_prof_promoted(const void *ptr, size_t size) |
| 1582 | { |
| 1583 | arena_chunk_t *chunk; |
| 1584 | size_t pageind, binind; |
| 1585 | |
Jason Evans | 78f7352 | 2012-04-18 13:38:40 -0700 | [diff] [blame] | 1586 | cassert(config_prof); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1587 | assert(ptr != NULL); |
| 1588 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1589 | assert(isalloc(ptr, false) == PAGE); |
| 1590 | assert(isalloc(ptr, true) == PAGE); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1591 | assert(size <= SMALL_MAXCLASS); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1592 | |
| 1593 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1594 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 41ade96 | 2011-03-06 22:56:36 -0800 | [diff] [blame] | 1595 | binind = SMALL_SIZE2BIN(size); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1596 | assert(binind < NBINS); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1597 | arena_mapbits_large_binind_set(chunk, pageind, binind); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1598 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1599 | assert(isalloc(ptr, false) == PAGE); |
| 1600 | assert(isalloc(ptr, true) == size); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 1601 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 1602 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1603 | static void |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 1604 | arena_dissociate_bin_run(arena_chunk_t *chunk, arena_run_t *run, |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1605 | arena_bin_t *bin) |
| 1606 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1607 | |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 1608 | /* Dissociate run from bin. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1609 | if (run == bin->runcur) |
| 1610 | bin->runcur = NULL; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1611 | else { |
| 1612 | size_t binind = arena_bin_index(chunk->arena, bin); |
| 1613 | arena_bin_info_t *bin_info = &arena_bin_info[binind]; |
| 1614 | |
| 1615 | if (bin_info->nregs != 1) { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1616 | /* |
| 1617 | * This block's conditional is necessary because if the |
| 1618 | * run only contains one region, then it never gets |
| 1619 | * inserted into the non-full runs tree. |
| 1620 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1621 | arena_bin_runs_remove(bin, run); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1622 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1623 | } |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | static void |
| 1627 | arena_dalloc_bin_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 1628 | arena_bin_t *bin) |
| 1629 | { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1630 | size_t binind; |
| 1631 | arena_bin_info_t *bin_info; |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 1632 | size_t npages, run_ind, past; |
| 1633 | |
| 1634 | assert(run != bin->runcur); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1635 | assert(arena_run_tree_search(&bin->runs, |
| 1636 | arena_mapp_get(chunk, ((uintptr_t)run-(uintptr_t)chunk)>>LG_PAGE)) |
| 1637 | == NULL); |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1638 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1639 | binind = arena_bin_index(chunk->arena, run->bin); |
| 1640 | bin_info = &arena_bin_info[binind]; |
| 1641 | |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1642 | malloc_mutex_unlock(&bin->lock); |
| 1643 | /******************************/ |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1644 | npages = bin_info->run_size >> LG_PAGE; |
| 1645 | run_ind = (size_t)(((uintptr_t)run - (uintptr_t)chunk) >> LG_PAGE); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 1646 | past = (size_t)(PAGE_CEILING((uintptr_t)run + |
| 1647 | (uintptr_t)bin_info->reg0_offset + (uintptr_t)(run->nextind * |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1648 | bin_info->reg_interval - bin_info->redzone_size) - |
| 1649 | (uintptr_t)chunk) >> LG_PAGE); |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1650 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 1651 | |
| 1652 | /* |
| 1653 | * If the run was originally clean, and some pages were never touched, |
| 1654 | * trim the clean pages before deallocating the dirty portion of the |
| 1655 | * run. |
| 1656 | */ |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 1657 | assert(arena_mapbits_dirty_get(chunk, run_ind) == |
| 1658 | arena_mapbits_dirty_get(chunk, run_ind+npages-1)); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1659 | if (arena_mapbits_dirty_get(chunk, run_ind) == 0 && past - run_ind < |
| 1660 | npages) { |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 1661 | /* Trim clean pages. Convert to large run beforehand. */ |
| 1662 | assert(npages > 0); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 1663 | arena_mapbits_large_set(chunk, run_ind, bin_info->run_size, 0); |
| 1664 | arena_mapbits_large_set(chunk, run_ind+npages-1, 0, 0); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1665 | arena_run_trim_tail(arena, chunk, run, (npages << LG_PAGE), |
| 1666 | ((past - run_ind) << LG_PAGE), false); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1667 | /* npages = past - run_ind; */ |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 1668 | } |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1669 | arena_run_dalloc(arena, run, true, false); |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 1670 | malloc_mutex_unlock(&arena->lock); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1671 | /****************************/ |
| 1672 | malloc_mutex_lock(&bin->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1673 | if (config_stats) |
| 1674 | bin->stats.curruns--; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1675 | } |
| 1676 | |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1677 | static void |
| 1678 | arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 1679 | arena_bin_t *bin) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1680 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1681 | |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1682 | /* |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1683 | * Make sure that if bin->runcur is non-NULL, it refers to the lowest |
| 1684 | * non-full run. It is okay to NULL runcur out rather than proactively |
| 1685 | * keeping it pointing at the lowest non-full run. |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1686 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1687 | if ((uintptr_t)run < (uintptr_t)bin->runcur) { |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1688 | /* Switch runcur. */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1689 | if (bin->runcur->nfree > 0) |
| 1690 | arena_bin_runs_insert(bin, bin->runcur); |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1691 | bin->runcur = run; |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 1692 | if (config_stats) |
| 1693 | bin->stats.reruns++; |
| 1694 | } else |
| 1695 | arena_bin_runs_insert(bin, run); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1699 | arena_dalloc_bin_locked(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1700 | arena_chunk_map_t *mapelm) |
| 1701 | { |
| 1702 | size_t pageind; |
| 1703 | arena_run_t *run; |
| 1704 | arena_bin_t *bin; |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 1705 | arena_bin_info_t *bin_info; |
| 1706 | size_t size, binind; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1707 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1708 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1709 | run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind - |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1710 | arena_mapbits_small_runind_get(chunk, pageind)) << LG_PAGE)); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1711 | bin = run->bin; |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 1712 | binind = arena_ptr_small_binind_get(ptr, mapelm->bits); |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 1713 | bin_info = &arena_bin_info[binind]; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1714 | if (config_fill || config_stats) |
| 1715 | size = bin_info->reg_size; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1716 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1717 | if (config_fill && opt_junk) |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 1718 | arena_dalloc_junk_small(ptr, bin_info); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1719 | |
| 1720 | arena_run_reg_dalloc(run, ptr); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1721 | if (run->nfree == bin_info->nregs) { |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 1722 | arena_dissociate_bin_run(chunk, run, bin); |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1723 | arena_dalloc_bin_run(arena, chunk, run, bin); |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 1724 | } else if (run->nfree == 1 && run != bin->runcur) |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 1725 | arena_bin_lower_run(arena, chunk, run, bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1726 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1727 | if (config_stats) { |
| 1728 | bin->stats.allocated -= size; |
| 1729 | bin->stats.ndalloc++; |
| 1730 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1733 | void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1734 | arena_dalloc_bin(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
| 1735 | size_t pageind, arena_chunk_map_t *mapelm) |
| 1736 | { |
| 1737 | arena_run_t *run; |
| 1738 | arena_bin_t *bin; |
| 1739 | |
| 1740 | run = (arena_run_t *)((uintptr_t)chunk + (uintptr_t)((pageind - |
| 1741 | arena_mapbits_small_runind_get(chunk, pageind)) << LG_PAGE)); |
| 1742 | bin = run->bin; |
| 1743 | malloc_mutex_lock(&bin->lock); |
| 1744 | arena_dalloc_bin_locked(arena, chunk, ptr, mapelm); |
| 1745 | malloc_mutex_unlock(&bin->lock); |
| 1746 | } |
| 1747 | |
| 1748 | void |
| 1749 | arena_dalloc_small(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
| 1750 | size_t pageind) |
| 1751 | { |
| 1752 | arena_chunk_map_t *mapelm; |
| 1753 | |
| 1754 | if (config_debug) { |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 1755 | /* arena_ptr_small_binind_get() does extra sanity checking. */ |
| 1756 | assert(arena_ptr_small_binind_get(ptr, arena_mapbits_get(chunk, |
| 1757 | pageind)) != BININD_INVALID); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1758 | } |
| 1759 | mapelm = arena_mapp_get(chunk, pageind); |
| 1760 | arena_dalloc_bin(arena, chunk, ptr, pageind, mapelm); |
| 1761 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1762 | |
| 1763 | void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1764 | arena_dalloc_large_locked(arena_t *arena, arena_chunk_t *chunk, void *ptr) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1765 | { |
Jason Evans | 1366826 | 2010-01-31 03:57:29 -0800 | [diff] [blame] | 1766 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1767 | if (config_fill || config_stats) { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1768 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1769 | size_t size = arena_mapbits_large_size_get(chunk, pageind); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1770 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1771 | if (config_fill && config_stats && opt_junk) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1772 | memset(ptr, 0x5a, size); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1773 | if (config_stats) { |
| 1774 | arena->stats.ndalloc_large++; |
| 1775 | arena->stats.allocated_large -= size; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1776 | arena->stats.lstats[(size >> LG_PAGE) - 1].ndalloc++; |
| 1777 | arena->stats.lstats[(size >> LG_PAGE) - 1].curruns--; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1778 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1779 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1780 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1781 | arena_run_dalloc(arena, (arena_run_t *)ptr, true, false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1782 | } |
| 1783 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1784 | void |
| 1785 | arena_dalloc_large(arena_t *arena, arena_chunk_t *chunk, void *ptr) |
| 1786 | { |
| 1787 | |
| 1788 | malloc_mutex_lock(&arena->lock); |
| 1789 | arena_dalloc_large_locked(arena, chunk, ptr); |
| 1790 | malloc_mutex_unlock(&arena->lock); |
| 1791 | } |
| 1792 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1793 | static void |
| 1794 | 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] | 1795 | size_t oldsize, size_t size) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1796 | { |
| 1797 | |
| 1798 | assert(size < oldsize); |
| 1799 | |
| 1800 | /* |
| 1801 | * Shrink the run, and make trailing pages available for other |
| 1802 | * allocations. |
| 1803 | */ |
| 1804 | malloc_mutex_lock(&arena->lock); |
| 1805 | arena_run_trim_tail(arena, chunk, (arena_run_t *)ptr, oldsize, size, |
| 1806 | true); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1807 | if (config_stats) { |
| 1808 | arena->stats.ndalloc_large++; |
| 1809 | arena->stats.allocated_large -= oldsize; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1810 | arena->stats.lstats[(oldsize >> LG_PAGE) - 1].ndalloc++; |
| 1811 | arena->stats.lstats[(oldsize >> LG_PAGE) - 1].curruns--; |
Jason Evans | 990d10c | 2010-01-31 03:49:35 -0800 | [diff] [blame] | 1812 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1813 | arena->stats.nmalloc_large++; |
| 1814 | arena->stats.nrequests_large++; |
| 1815 | arena->stats.allocated_large += size; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1816 | arena->stats.lstats[(size >> LG_PAGE) - 1].nmalloc++; |
| 1817 | arena->stats.lstats[(size >> LG_PAGE) - 1].nrequests++; |
| 1818 | arena->stats.lstats[(size >> LG_PAGE) - 1].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1819 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1820 | malloc_mutex_unlock(&arena->lock); |
| 1821 | } |
| 1822 | |
| 1823 | static bool |
| 1824 | arena_ralloc_large_grow(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1825 | size_t oldsize, size_t size, size_t extra, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1826 | { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1827 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
| 1828 | size_t npages = oldsize >> LG_PAGE; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1829 | size_t followsize; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1830 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1831 | assert(oldsize == arena_mapbits_large_size_get(chunk, pageind)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1832 | |
| 1833 | /* Try to extend the run. */ |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1834 | assert(size + extra > oldsize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1835 | malloc_mutex_lock(&arena->lock); |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 1836 | if (pageind + npages < chunk_npages && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1837 | arena_mapbits_allocated_get(chunk, pageind+npages) == 0 && |
| 1838 | (followsize = arena_mapbits_unallocated_size_get(chunk, |
| 1839 | pageind+npages)) >= size - oldsize) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1840 | /* |
| 1841 | * The next run is available and sufficiently large. Split the |
| 1842 | * following run, then merge the first part with the existing |
| 1843 | * allocation. |
| 1844 | */ |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1845 | size_t flag_dirty; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1846 | size_t splitsize = (oldsize + followsize <= size + extra) |
| 1847 | ? followsize : size + extra - oldsize; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1848 | arena_run_split(arena, (arena_run_t *)((uintptr_t)chunk + |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1849 | ((pageind+npages) << LG_PAGE)), splitsize, true, |
| 1850 | BININD_INVALID, zero); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1851 | |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 1852 | size = oldsize + splitsize; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1853 | npages = size >> LG_PAGE; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1854 | |
| 1855 | /* |
| 1856 | * Mark the extended run as dirty if either portion of the run |
| 1857 | * was dirty before allocation. This is rather pedantic, |
| 1858 | * because there's not actually any sequence of events that |
| 1859 | * could cause the resulting run to be passed to |
| 1860 | * arena_run_dalloc() with the dirty argument set to false |
| 1861 | * (which is when dirty flag consistency would really matter). |
| 1862 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1863 | flag_dirty = arena_mapbits_dirty_get(chunk, pageind) | |
| 1864 | arena_mapbits_dirty_get(chunk, pageind+npages-1); |
| 1865 | arena_mapbits_large_set(chunk, pageind, size, flag_dirty); |
| 1866 | arena_mapbits_large_set(chunk, pageind+npages-1, 0, flag_dirty); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1867 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1868 | if (config_stats) { |
| 1869 | arena->stats.ndalloc_large++; |
| 1870 | arena->stats.allocated_large -= oldsize; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1871 | arena->stats.lstats[(oldsize >> LG_PAGE) - 1].ndalloc++; |
| 1872 | arena->stats.lstats[(oldsize >> LG_PAGE) - 1].curruns--; |
Jason Evans | 990d10c | 2010-01-31 03:49:35 -0800 | [diff] [blame] | 1873 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1874 | arena->stats.nmalloc_large++; |
| 1875 | arena->stats.nrequests_large++; |
| 1876 | arena->stats.allocated_large += size; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1877 | arena->stats.lstats[(size >> LG_PAGE) - 1].nmalloc++; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1878 | arena->stats.lstats[(size >> LG_PAGE) - 1].nrequests++; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1879 | arena->stats.lstats[(size >> LG_PAGE) - 1].curruns++; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 1880 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1881 | malloc_mutex_unlock(&arena->lock); |
| 1882 | return (false); |
| 1883 | } |
| 1884 | malloc_mutex_unlock(&arena->lock); |
| 1885 | |
| 1886 | return (true); |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Try to resize a large allocation, in order to avoid copying. This will |
| 1891 | * always fail if growing an object, and the following run is already in use. |
| 1892 | */ |
| 1893 | static bool |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1894 | arena_ralloc_large(void *ptr, size_t oldsize, size_t size, size_t extra, |
| 1895 | bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1896 | { |
| 1897 | size_t psize; |
| 1898 | |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1899 | psize = PAGE_CEILING(size + extra); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1900 | if (psize == oldsize) { |
| 1901 | /* Same size class. */ |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1902 | if (config_fill && opt_junk && size < oldsize) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1903 | memset((void *)((uintptr_t)ptr + size), 0x5a, oldsize - |
| 1904 | size); |
| 1905 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1906 | return (false); |
| 1907 | } else { |
| 1908 | arena_chunk_t *chunk; |
| 1909 | arena_t *arena; |
| 1910 | |
| 1911 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
| 1912 | arena = chunk->arena; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1913 | |
| 1914 | if (psize < oldsize) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1915 | /* Fill before shrinking in order avoid a race. */ |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1916 | if (config_fill && opt_junk) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1917 | memset((void *)((uintptr_t)ptr + size), 0x5a, |
| 1918 | oldsize - size); |
| 1919 | } |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1920 | arena_ralloc_large_shrink(arena, chunk, ptr, oldsize, |
| 1921 | psize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1922 | return (false); |
| 1923 | } else { |
| 1924 | bool ret = arena_ralloc_large_grow(arena, chunk, ptr, |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1925 | oldsize, PAGE_CEILING(size), |
| 1926 | psize - PAGE_CEILING(size), zero); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1927 | if (config_fill && ret == false && zero == false && |
| 1928 | opt_zero) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1929 | memset((void *)((uintptr_t)ptr + oldsize), 0, |
| 1930 | size - oldsize); |
| 1931 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1932 | return (ret); |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | void * |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1938 | arena_ralloc_no_move(void *ptr, size_t oldsize, size_t size, size_t extra, |
| 1939 | bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1940 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1941 | |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1942 | /* |
| 1943 | * Avoid moving the allocation if the size class can be left the same. |
| 1944 | */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1945 | if (oldsize <= arena_maxclass) { |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1946 | if (oldsize <= SMALL_MAXCLASS) { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 1947 | assert(arena_bin_info[SMALL_SIZE2BIN(oldsize)].reg_size |
| 1948 | == oldsize); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1949 | if ((size + extra <= SMALL_MAXCLASS && |
Jason Evans | 41ade96 | 2011-03-06 22:56:36 -0800 | [diff] [blame] | 1950 | SMALL_SIZE2BIN(size + extra) == |
| 1951 | SMALL_SIZE2BIN(oldsize)) || (size <= oldsize && |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1952 | size + extra >= oldsize)) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1953 | if (config_fill && opt_junk && size < oldsize) { |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1954 | memset((void *)((uintptr_t)ptr + size), |
| 1955 | 0x5a, oldsize - size); |
| 1956 | } |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1957 | return (ptr); |
| 1958 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1959 | } else { |
| 1960 | assert(size <= arena_maxclass); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 1961 | if (size + extra > SMALL_MAXCLASS) { |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1962 | if (arena_ralloc_large(ptr, oldsize, size, |
| 1963 | extra, zero) == false) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1964 | return (ptr); |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1969 | /* Reallocation would require a move. */ |
| 1970 | return (NULL); |
| 1971 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1972 | |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1973 | void * |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 1974 | arena_ralloc(arena_t *arena, void *ptr, size_t oldsize, size_t size, |
| 1975 | size_t extra, size_t alignment, bool zero, bool try_tcache_alloc, |
| 1976 | bool try_tcache_dalloc) |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1977 | { |
| 1978 | void *ret; |
| 1979 | size_t copysize; |
| 1980 | |
| 1981 | /* Try to avoid moving the allocation. */ |
| 1982 | ret = arena_ralloc_no_move(ptr, oldsize, size, extra, zero); |
| 1983 | if (ret != NULL) |
| 1984 | return (ret); |
| 1985 | |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1986 | /* |
| 1987 | * size and oldsize are different enough that we need to move the |
| 1988 | * object. In that case, fall back to allocating new space and |
| 1989 | * copying. |
| 1990 | */ |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1991 | if (alignment != 0) { |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 1992 | size_t usize = sa2u(size + extra, alignment); |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1993 | if (usize == 0) |
| 1994 | return (NULL); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 1995 | ret = ipallocx(usize, alignment, zero, try_tcache_alloc, arena); |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 1996 | } else |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 1997 | ret = arena_malloc(arena, size + extra, zero, try_tcache_alloc); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 1998 | |
| 1999 | if (ret == NULL) { |
| 2000 | if (extra == 0) |
| 2001 | return (NULL); |
| 2002 | /* Try again, this time without extra. */ |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 2003 | if (alignment != 0) { |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2004 | size_t usize = sa2u(size, alignment); |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 2005 | if (usize == 0) |
| 2006 | return (NULL); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2007 | ret = ipallocx(usize, alignment, zero, try_tcache_alloc, |
| 2008 | arena); |
Jason Evans | 38d9210 | 2011-03-23 00:37:29 -0700 | [diff] [blame] | 2009 | } else |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2010 | ret = arena_malloc(arena, size, zero, try_tcache_alloc); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2011 | |
| 2012 | if (ret == NULL) |
| 2013 | return (NULL); |
| 2014 | } |
| 2015 | |
| 2016 | /* Junk/zero-filling were already done by ipalloc()/arena_malloc(). */ |
| 2017 | |
| 2018 | /* |
| 2019 | * Copy at most size bytes (not size+extra), since the caller has no |
| 2020 | * expectation that the extra bytes will be reliably preserved. |
| 2021 | */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2022 | copysize = (size < oldsize) ? size : oldsize; |
Jason Evans | f54166e | 2012-04-23 22:41:36 -0700 | [diff] [blame] | 2023 | VALGRIND_MAKE_MEM_UNDEFINED(ret, copysize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2024 | memcpy(ret, ptr, copysize); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2025 | iqallocx(ptr, try_tcache_dalloc); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2026 | return (ret); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2029 | dss_prec_t |
| 2030 | arena_dss_prec_get(arena_t *arena) |
| 2031 | { |
| 2032 | dss_prec_t ret; |
| 2033 | |
| 2034 | malloc_mutex_lock(&arena->lock); |
| 2035 | ret = arena->dss_prec; |
| 2036 | malloc_mutex_unlock(&arena->lock); |
| 2037 | return (ret); |
| 2038 | } |
| 2039 | |
| 2040 | void |
| 2041 | arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec) |
| 2042 | { |
| 2043 | |
| 2044 | malloc_mutex_lock(&arena->lock); |
| 2045 | arena->dss_prec = dss_prec; |
| 2046 | malloc_mutex_unlock(&arena->lock); |
| 2047 | } |
| 2048 | |
| 2049 | void |
| 2050 | arena_stats_merge(arena_t *arena, const char **dss, size_t *nactive, |
| 2051 | size_t *ndirty, arena_stats_t *astats, malloc_bin_stats_t *bstats, |
| 2052 | malloc_large_stats_t *lstats) |
| 2053 | { |
| 2054 | unsigned i; |
| 2055 | |
| 2056 | malloc_mutex_lock(&arena->lock); |
| 2057 | *dss = dss_prec_names[arena->dss_prec]; |
| 2058 | *nactive += arena->nactive; |
| 2059 | *ndirty += arena->ndirty; |
| 2060 | |
| 2061 | astats->mapped += arena->stats.mapped; |
| 2062 | astats->npurge += arena->stats.npurge; |
| 2063 | astats->nmadvise += arena->stats.nmadvise; |
| 2064 | astats->purged += arena->stats.purged; |
| 2065 | astats->allocated_large += arena->stats.allocated_large; |
| 2066 | astats->nmalloc_large += arena->stats.nmalloc_large; |
| 2067 | astats->ndalloc_large += arena->stats.ndalloc_large; |
| 2068 | astats->nrequests_large += arena->stats.nrequests_large; |
| 2069 | |
| 2070 | for (i = 0; i < nlclasses; i++) { |
| 2071 | lstats[i].nmalloc += arena->stats.lstats[i].nmalloc; |
| 2072 | lstats[i].ndalloc += arena->stats.lstats[i].ndalloc; |
| 2073 | lstats[i].nrequests += arena->stats.lstats[i].nrequests; |
| 2074 | lstats[i].curruns += arena->stats.lstats[i].curruns; |
| 2075 | } |
| 2076 | malloc_mutex_unlock(&arena->lock); |
| 2077 | |
| 2078 | for (i = 0; i < NBINS; i++) { |
| 2079 | arena_bin_t *bin = &arena->bins[i]; |
| 2080 | |
| 2081 | malloc_mutex_lock(&bin->lock); |
| 2082 | bstats[i].allocated += bin->stats.allocated; |
| 2083 | bstats[i].nmalloc += bin->stats.nmalloc; |
| 2084 | bstats[i].ndalloc += bin->stats.ndalloc; |
| 2085 | bstats[i].nrequests += bin->stats.nrequests; |
| 2086 | if (config_tcache) { |
| 2087 | bstats[i].nfills += bin->stats.nfills; |
| 2088 | bstats[i].nflushes += bin->stats.nflushes; |
| 2089 | } |
| 2090 | bstats[i].nruns += bin->stats.nruns; |
| 2091 | bstats[i].reruns += bin->stats.reruns; |
| 2092 | bstats[i].curruns += bin->stats.curruns; |
| 2093 | malloc_mutex_unlock(&bin->lock); |
| 2094 | } |
| 2095 | } |
| 2096 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2097 | bool |
| 2098 | arena_new(arena_t *arena, unsigned ind) |
| 2099 | { |
| 2100 | unsigned i; |
| 2101 | arena_bin_t *bin; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2102 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2103 | arena->ind = ind; |
Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 2104 | arena->nthreads = 0; |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2105 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2106 | if (malloc_mutex_init(&arena->lock)) |
| 2107 | return (true); |
| 2108 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2109 | if (config_stats) { |
| 2110 | memset(&arena->stats, 0, sizeof(arena_stats_t)); |
| 2111 | arena->stats.lstats = |
| 2112 | (malloc_large_stats_t *)base_alloc(nlclasses * |
| 2113 | sizeof(malloc_large_stats_t)); |
| 2114 | if (arena->stats.lstats == NULL) |
| 2115 | return (true); |
| 2116 | memset(arena->stats.lstats, 0, nlclasses * |
| 2117 | sizeof(malloc_large_stats_t)); |
| 2118 | if (config_tcache) |
| 2119 | ql_new(&arena->tcache_ql); |
| 2120 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2121 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2122 | if (config_prof) |
| 2123 | arena->prof_accumbytes = 0; |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 2124 | |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 2125 | arena->dss_prec = chunk_dss_prec_get(); |
| 2126 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2127 | /* Initialize chunks. */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 2128 | arena_chunk_dirty_new(&arena->chunks_dirty); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2129 | arena->spare = NULL; |
| 2130 | |
| 2131 | arena->nactive = 0; |
| 2132 | arena->ndirty = 0; |
Jason Evans | 799ca0b | 2010-04-08 20:31:58 -0700 | [diff] [blame] | 2133 | arena->npurgatory = 0; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2134 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 2135 | arena_avail_tree_new(&arena->runs_avail); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2136 | |
| 2137 | /* Initialize bins. */ |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2138 | for (i = 0; i < NBINS; i++) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2139 | bin = &arena->bins[i]; |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2140 | if (malloc_mutex_init(&bin->lock)) |
| 2141 | return (true); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2142 | bin->runcur = NULL; |
| 2143 | arena_run_tree_new(&bin->runs); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2144 | if (config_stats) |
| 2145 | memset(&bin->stats, 0, sizeof(malloc_bin_stats_t)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2148 | return (false); |
| 2149 | } |
| 2150 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2151 | /* |
| 2152 | * Calculate bin_info->run_size such that it meets the following constraints: |
| 2153 | * |
| 2154 | * *) bin_info->run_size >= min_run_size |
| 2155 | * *) bin_info->run_size <= arena_maxclass |
| 2156 | * *) run header overhead <= RUN_MAX_OVRHD (or header overhead relaxed). |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 2157 | * *) bin_info->nregs <= RUN_MAXREGS |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2158 | * |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 2159 | * bin_info->nregs, bin_info->bitmap_offset, and bin_info->reg0_offset are also |
| 2160 | * calculated here, since these settings are all interdependent. |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2161 | */ |
| 2162 | static size_t |
| 2163 | bin_info_run_size_calc(arena_bin_info_t *bin_info, size_t min_run_size) |
| 2164 | { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2165 | size_t pad_size; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2166 | size_t try_run_size, good_run_size; |
| 2167 | uint32_t try_nregs, good_nregs; |
| 2168 | uint32_t try_hdr_size, good_hdr_size; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 2169 | uint32_t try_bitmap_offset, good_bitmap_offset; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2170 | uint32_t try_ctx0_offset, good_ctx0_offset; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2171 | uint32_t try_redzone0_offset, good_redzone0_offset; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2172 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2173 | assert(min_run_size >= PAGE); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2174 | assert(min_run_size <= arena_maxclass); |
| 2175 | |
| 2176 | /* |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2177 | * Determine redzone size based on minimum alignment and minimum |
| 2178 | * redzone size. Add padding to the end of the run if it is needed to |
| 2179 | * align the regions. The padding allows each redzone to be half the |
| 2180 | * minimum alignment; without the padding, each redzone would have to |
| 2181 | * be twice as large in order to maintain alignment. |
| 2182 | */ |
| 2183 | if (config_fill && opt_redzone) { |
| 2184 | size_t align_min = ZU(1) << (ffs(bin_info->reg_size) - 1); |
| 2185 | if (align_min <= REDZONE_MINSIZE) { |
| 2186 | bin_info->redzone_size = REDZONE_MINSIZE; |
| 2187 | pad_size = 0; |
| 2188 | } else { |
| 2189 | bin_info->redzone_size = align_min >> 1; |
| 2190 | pad_size = bin_info->redzone_size; |
| 2191 | } |
| 2192 | } else { |
| 2193 | bin_info->redzone_size = 0; |
| 2194 | pad_size = 0; |
| 2195 | } |
| 2196 | bin_info->reg_interval = bin_info->reg_size + |
| 2197 | (bin_info->redzone_size << 1); |
| 2198 | |
| 2199 | /* |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2200 | * Calculate known-valid settings before entering the run_size |
| 2201 | * expansion loop, so that the first part of the loop always copies |
| 2202 | * valid settings. |
| 2203 | * |
| 2204 | * The do..while loop iteratively reduces the number of regions until |
| 2205 | * the run header and the regions no longer overlap. A closed formula |
| 2206 | * would be quite messy, since there is an interdependency between the |
| 2207 | * header's mask length and the number of regions. |
| 2208 | */ |
| 2209 | try_run_size = min_run_size; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2210 | try_nregs = ((try_run_size - sizeof(arena_run_t)) / |
| 2211 | bin_info->reg_interval) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2212 | + 1; /* Counter-act try_nregs-- in loop. */ |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 2213 | if (try_nregs > RUN_MAXREGS) { |
| 2214 | try_nregs = RUN_MAXREGS |
| 2215 | + 1; /* Counter-act try_nregs-- in loop. */ |
| 2216 | } |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2217 | do { |
| 2218 | try_nregs--; |
| 2219 | try_hdr_size = sizeof(arena_run_t); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 2220 | /* Pad to a long boundary. */ |
| 2221 | try_hdr_size = LONG_CEILING(try_hdr_size); |
| 2222 | try_bitmap_offset = try_hdr_size; |
| 2223 | /* Add space for bitmap. */ |
| 2224 | try_hdr_size += bitmap_size(try_nregs); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2225 | if (config_prof && opt_prof && prof_promote == false) { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2226 | /* Pad to a quantum boundary. */ |
| 2227 | try_hdr_size = QUANTUM_CEILING(try_hdr_size); |
| 2228 | try_ctx0_offset = try_hdr_size; |
| 2229 | /* Add space for one (prof_ctx_t *) per region. */ |
| 2230 | try_hdr_size += try_nregs * sizeof(prof_ctx_t *); |
| 2231 | } else |
| 2232 | try_ctx0_offset = 0; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2233 | try_redzone0_offset = try_run_size - (try_nregs * |
| 2234 | bin_info->reg_interval) - pad_size; |
| 2235 | } while (try_hdr_size > try_redzone0_offset); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2236 | |
| 2237 | /* run_size expansion loop. */ |
| 2238 | do { |
| 2239 | /* |
| 2240 | * Copy valid settings before trying more aggressive settings. |
| 2241 | */ |
| 2242 | good_run_size = try_run_size; |
| 2243 | good_nregs = try_nregs; |
| 2244 | good_hdr_size = try_hdr_size; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 2245 | good_bitmap_offset = try_bitmap_offset; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2246 | good_ctx0_offset = try_ctx0_offset; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2247 | good_redzone0_offset = try_redzone0_offset; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2248 | |
| 2249 | /* Try more aggressive settings. */ |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2250 | try_run_size += PAGE; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2251 | try_nregs = ((try_run_size - sizeof(arena_run_t) - pad_size) / |
| 2252 | bin_info->reg_interval) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2253 | + 1; /* Counter-act try_nregs-- in loop. */ |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 2254 | if (try_nregs > RUN_MAXREGS) { |
| 2255 | try_nregs = RUN_MAXREGS |
| 2256 | + 1; /* Counter-act try_nregs-- in loop. */ |
| 2257 | } |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2258 | do { |
| 2259 | try_nregs--; |
| 2260 | try_hdr_size = sizeof(arena_run_t); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 2261 | /* Pad to a long boundary. */ |
| 2262 | try_hdr_size = LONG_CEILING(try_hdr_size); |
| 2263 | try_bitmap_offset = try_hdr_size; |
| 2264 | /* Add space for bitmap. */ |
| 2265 | try_hdr_size += bitmap_size(try_nregs); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2266 | if (config_prof && opt_prof && prof_promote == false) { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2267 | /* Pad to a quantum boundary. */ |
| 2268 | try_hdr_size = QUANTUM_CEILING(try_hdr_size); |
| 2269 | try_ctx0_offset = try_hdr_size; |
| 2270 | /* |
| 2271 | * Add space for one (prof_ctx_t *) per region. |
| 2272 | */ |
| 2273 | try_hdr_size += try_nregs * |
| 2274 | sizeof(prof_ctx_t *); |
| 2275 | } |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2276 | try_redzone0_offset = try_run_size - (try_nregs * |
| 2277 | bin_info->reg_interval) - pad_size; |
| 2278 | } while (try_hdr_size > try_redzone0_offset); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2279 | } while (try_run_size <= arena_maxclass |
| 2280 | && try_run_size <= arena_maxclass |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2281 | && RUN_MAX_OVRHD * (bin_info->reg_interval << 3) > |
| 2282 | RUN_MAX_OVRHD_RELAX |
| 2283 | && (try_redzone0_offset << RUN_BFP) > RUN_MAX_OVRHD * try_run_size |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 2284 | && try_nregs < RUN_MAXREGS); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2285 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2286 | assert(good_hdr_size <= good_redzone0_offset); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2287 | |
| 2288 | /* Copy final settings. */ |
| 2289 | bin_info->run_size = good_run_size; |
| 2290 | bin_info->nregs = good_nregs; |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 2291 | bin_info->bitmap_offset = good_bitmap_offset; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2292 | bin_info->ctx0_offset = good_ctx0_offset; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2293 | bin_info->reg0_offset = good_redzone0_offset + bin_info->redzone_size; |
| 2294 | |
| 2295 | assert(bin_info->reg0_offset - bin_info->redzone_size + (bin_info->nregs |
| 2296 | * bin_info->reg_interval) + pad_size == bin_info->run_size); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2297 | |
| 2298 | return (good_run_size); |
| 2299 | } |
| 2300 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2301 | static void |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2302 | bin_info_init(void) |
| 2303 | { |
| 2304 | arena_bin_info_t *bin_info; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2305 | size_t prev_run_size = PAGE; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2306 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2307 | #define SIZE_CLASS(bin, delta, size) \ |
| 2308 | bin_info = &arena_bin_info[bin]; \ |
| 2309 | bin_info->reg_size = size; \ |
| 2310 | prev_run_size = bin_info_run_size_calc(bin_info, prev_run_size);\ |
| 2311 | bitmap_info_init(&bin_info->bitmap_info, bin_info->nregs); |
| 2312 | SIZE_CLASSES |
| 2313 | #undef SIZE_CLASS |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2314 | } |
| 2315 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2316 | void |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 2317 | arena_boot(void) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2318 | { |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 2319 | size_t header_size; |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 2320 | unsigned i; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2321 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2322 | /* |
| 2323 | * 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] | 2324 | * page map. The page map is biased to omit entries for the header |
| 2325 | * itself, so some iteration is necessary to compute the map bias. |
| 2326 | * |
| 2327 | * 1) Compute safe header_size and map_bias values that include enough |
| 2328 | * space for an unbiased page map. |
| 2329 | * 2) Refine map_bias based on (1) to omit the header pages in the page |
| 2330 | * map. The resulting map_bias may be one too small. |
| 2331 | * 3) Refine map_bias based on (2). The result will be >= the result |
| 2332 | * from (2), and will always be correct. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2333 | */ |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 2334 | map_bias = 0; |
| 2335 | for (i = 0; i < 3; i++) { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2336 | header_size = offsetof(arena_chunk_t, map) + |
| 2337 | (sizeof(arena_chunk_map_t) * (chunk_npages-map_bias)); |
| 2338 | map_bias = (header_size >> LG_PAGE) + ((header_size & PAGE_MASK) |
| 2339 | != 0); |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 2340 | } |
| 2341 | assert(map_bias > 0); |
| 2342 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2343 | arena_maxclass = chunksize - (map_bias << LG_PAGE); |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 2344 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2345 | bin_info_init(); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2346 | } |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 2347 | |
| 2348 | void |
| 2349 | arena_prefork(arena_t *arena) |
| 2350 | { |
| 2351 | unsigned i; |
| 2352 | |
| 2353 | malloc_mutex_prefork(&arena->lock); |
| 2354 | for (i = 0; i < NBINS; i++) |
| 2355 | malloc_mutex_prefork(&arena->bins[i].lock); |
| 2356 | } |
| 2357 | |
| 2358 | void |
| 2359 | arena_postfork_parent(arena_t *arena) |
| 2360 | { |
| 2361 | unsigned i; |
| 2362 | |
| 2363 | for (i = 0; i < NBINS; i++) |
| 2364 | malloc_mutex_postfork_parent(&arena->bins[i].lock); |
| 2365 | malloc_mutex_postfork_parent(&arena->lock); |
| 2366 | } |
| 2367 | |
| 2368 | void |
| 2369 | arena_postfork_child(arena_t *arena) |
| 2370 | { |
| 2371 | unsigned i; |
| 2372 | |
| 2373 | for (i = 0; i < NBINS; i++) |
| 2374 | malloc_mutex_postfork_child(&arena->bins[i].lock); |
| 2375 | malloc_mutex_postfork_child(&arena->lock); |
| 2376 | } |