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