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