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