Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1 | /******************************************************************************/ |
| 2 | #ifdef JEMALLOC_H_TYPES |
| 3 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 4 | #define LARGE_MINCLASS (ZU(1) << LG_LARGE_MINCLASS) |
| 5 | |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 6 | /* Maximum number of regions in one run. */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 7 | #define LG_RUN_MAXREGS (LG_PAGE - LG_TINY_MIN) |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 8 | #define RUN_MAXREGS (1U << LG_RUN_MAXREGS) |
| 9 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 10 | /* |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 11 | * Minimum redzone size. Redzones may be larger than this if necessary to |
| 12 | * preserve region alignment. |
| 13 | */ |
| 14 | #define REDZONE_MINSIZE 16 |
| 15 | |
| 16 | /* |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 17 | * The minimum ratio of active:dirty pages per arena is computed as: |
| 18 | * |
| 19 | * (nactive >> opt_lg_dirty_mult) >= ndirty |
| 20 | * |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 21 | * So, supposing that opt_lg_dirty_mult is 3, there can be no less than 8 times |
| 22 | * as many active pages as dirty pages. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 23 | */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 24 | #define LG_DIRTY_MULT_DEFAULT 3 |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 25 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 26 | typedef struct arena_run_s arena_run_t; |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 27 | typedef struct arena_chunk_map_bits_s arena_chunk_map_bits_t; |
| 28 | typedef struct arena_chunk_map_misc_s arena_chunk_map_misc_t; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 29 | typedef struct arena_chunk_s arena_chunk_t; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 30 | typedef struct arena_bin_info_s arena_bin_info_t; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 31 | typedef struct arena_bin_s arena_bin_t; |
| 32 | typedef struct arena_s arena_t; |
| 33 | |
| 34 | #endif /* JEMALLOC_H_TYPES */ |
| 35 | /******************************************************************************/ |
| 36 | #ifdef JEMALLOC_H_STRUCTS |
| 37 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 38 | struct arena_run_s { |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 39 | /* Index of bin this run is associated with. */ |
| 40 | index_t binind; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 41 | |
| 42 | /* Number of free regions in run. */ |
| 43 | unsigned nfree; |
| 44 | |
| 45 | /* Per region allocated/deallocated bitmap. */ |
| 46 | bitmap_t bitmap[BITMAP_GROUPS_MAX]; |
| 47 | }; |
| 48 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 49 | /* Each element of the chunk map corresponds to one page within the chunk. */ |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 50 | struct arena_chunk_map_bits_s { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 51 | /* |
| 52 | * Run address (or size) and various flags are stored together. The bit |
| 53 | * layout looks like (assuming 32-bit system): |
| 54 | * |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 55 | * ???????? ???????? ????nnnn nnnndula |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 56 | * |
| 57 | * ? : Unallocated: Run address for first/last pages, unset for internal |
| 58 | * pages. |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 59 | * Small: Run page offset. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 60 | * Large: Run size for first page, unset for trailing pages. |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 61 | * n : binind for small size class, BININD_INVALID for large size class. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 62 | * d : dirty? |
Jason Evans | 8ad0eac | 2010-12-17 18:07:53 -0800 | [diff] [blame] | 63 | * u : unzeroed? |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 64 | * l : large? |
| 65 | * a : allocated? |
| 66 | * |
| 67 | * Following are example bit patterns for the three types of runs. |
| 68 | * |
| 69 | * p : run page offset |
| 70 | * s : run size |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 71 | * n : binind for size class; large objects set these to BININD_INVALID |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 72 | * x : don't care |
| 73 | * - : 0 |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 74 | * + : 1 |
Jason Evans | 3377ffa | 2010-10-01 17:53:37 -0700 | [diff] [blame] | 75 | * [DULA] : bit set |
| 76 | * [dula] : bit unset |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 77 | * |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 78 | * Unallocated (clean): |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 79 | * ssssssss ssssssss ssss++++ ++++du-a |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 80 | * xxxxxxxx xxxxxxxx xxxxxxxx xxxx-Uxx |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 81 | * ssssssss ssssssss ssss++++ ++++dU-a |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 82 | * |
| 83 | * Unallocated (dirty): |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 84 | * ssssssss ssssssss ssss++++ ++++D--a |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 85 | * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 86 | * ssssssss ssssssss ssss++++ ++++D--a |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 87 | * |
Jason Evans | dafde14 | 2010-03-17 16:27:39 -0700 | [diff] [blame] | 88 | * Small: |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 89 | * pppppppp pppppppp ppppnnnn nnnnd--A |
| 90 | * pppppppp pppppppp ppppnnnn nnnn---A |
| 91 | * pppppppp pppppppp ppppnnnn nnnnd--A |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 92 | * |
| 93 | * Large: |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 94 | * ssssssss ssssssss ssss++++ ++++D-LA |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 95 | * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 96 | * -------- -------- ----++++ ++++D-LA |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 97 | * |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 98 | * Large (sampled, size <= LARGE_MINCLASS): |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 99 | * ssssssss ssssssss ssssnnnn nnnnD-LA |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 100 | * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx |
| 101 | * -------- -------- ----++++ ++++D-LA |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 102 | * |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 103 | * Large (not sampled, size == LARGE_MINCLASS): |
Jason Evans | 53bd42c | 2012-05-10 00:18:46 -0700 | [diff] [blame] | 104 | * ssssssss ssssssss ssss++++ ++++D-LA |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 105 | * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx |
| 106 | * -------- -------- ----++++ ++++D-LA |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 107 | */ |
| 108 | size_t bits; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 109 | #define CHUNK_MAP_BININD_SHIFT 4 |
| 110 | #define BININD_INVALID ((size_t)0xffU) |
| 111 | /* CHUNK_MAP_BININD_MASK == (BININD_INVALID << CHUNK_MAP_BININD_SHIFT) */ |
| 112 | #define CHUNK_MAP_BININD_MASK ((size_t)0xff0U) |
| 113 | #define CHUNK_MAP_BININD_INVALID CHUNK_MAP_BININD_MASK |
| 114 | #define CHUNK_MAP_FLAGS_MASK ((size_t)0xcU) |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 115 | #define CHUNK_MAP_DIRTY ((size_t)0x8U) |
Jason Evans | 3377ffa | 2010-10-01 17:53:37 -0700 | [diff] [blame] | 116 | #define CHUNK_MAP_UNZEROED ((size_t)0x4U) |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 117 | #define CHUNK_MAP_LARGE ((size_t)0x2U) |
| 118 | #define CHUNK_MAP_ALLOCATED ((size_t)0x1U) |
| 119 | #define CHUNK_MAP_KEY CHUNK_MAP_ALLOCATED |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 120 | }; |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * Each arena_chunk_map_misc_t corresponds to one page within the chunk, just |
| 124 | * like arena_chunk_map_bits_t. Two separate arrays are stored within each |
| 125 | * chunk header in order to improve cache locality. |
| 126 | */ |
| 127 | struct arena_chunk_map_misc_s { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 128 | /* |
| 129 | * Linkage for run trees. There are two disjoint uses: |
| 130 | * |
| 131 | * 1) arena_t's runs_avail tree. |
| 132 | * 2) arena_run_t conceptually uses this linkage for in-use non-full |
| 133 | * runs, rather than directly embedding linkage. |
| 134 | */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 135 | rb_node(arena_chunk_map_misc_t) rb_link; |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 136 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 137 | union { |
| 138 | /* Linkage for list of dirty runs. */ |
| 139 | ql_elm(arena_chunk_map_misc_t) dr_link; |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 140 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 141 | /* Profile counters, used for large object runs. */ |
| 142 | prof_tctx_t *prof_tctx; |
| 143 | |
| 144 | /* Small region run metadata. */ |
| 145 | arena_run_t run; |
| 146 | }; |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 147 | }; |
| 148 | typedef rb_tree(arena_chunk_map_misc_t) arena_avail_tree_t; |
| 149 | typedef rb_tree(arena_chunk_map_misc_t) arena_run_tree_t; |
| 150 | typedef ql_head(arena_chunk_map_misc_t) arena_chunk_miscelms_t; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 151 | |
| 152 | /* Arena chunk header. */ |
| 153 | struct arena_chunk_s { |
| 154 | /* Arena that owns the chunk. */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 155 | arena_t *arena; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 156 | |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 157 | /* |
| 158 | * Map of pages within chunk that keeps track of free/large/small. The |
| 159 | * first map_bias entries are omitted, since the chunk header does not |
| 160 | * need to be tracked in the map. This omission saves a header page |
| 161 | * for common chunk sizes (e.g. 4 MiB). |
| 162 | */ |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 163 | arena_chunk_map_bits_t map_bits[1]; /* Dynamically sized. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 164 | }; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 165 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 166 | /* |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 167 | * Read-only information associated with each element of arena_t's bins array |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 168 | * is stored separately, partly to reduce memory usage (only one copy, rather |
| 169 | * than one per arena), but mainly to avoid false cacheline sharing. |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 170 | * |
| 171 | * Each run has the following layout: |
| 172 | * |
| 173 | * /--------------------\ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 174 | * | pad? | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 175 | * |--------------------| |
| 176 | * | redzone | |
| 177 | * reg0_offset | region 0 | |
| 178 | * | redzone | |
| 179 | * |--------------------| \ |
| 180 | * | redzone | | |
| 181 | * | region 1 | > reg_interval |
| 182 | * | redzone | / |
| 183 | * |--------------------| |
| 184 | * | ... | |
| 185 | * | ... | |
| 186 | * | ... | |
| 187 | * |--------------------| |
| 188 | * | redzone | |
| 189 | * | region nregs-1 | |
| 190 | * | redzone | |
| 191 | * |--------------------| |
| 192 | * | alignment pad? | |
| 193 | * \--------------------/ |
| 194 | * |
| 195 | * reg_interval has at least the same minimum alignment as reg_size; this |
| 196 | * preserves the alignment constraint that sa2u() depends on. Alignment pad is |
| 197 | * either 0 or redzone_size; it is present only if needed to align reg0_offset. |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 198 | */ |
| 199 | struct arena_bin_info_s { |
| 200 | /* Size of regions in a run for this bin's size class. */ |
| 201 | size_t reg_size; |
| 202 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 203 | /* Redzone size. */ |
| 204 | size_t redzone_size; |
| 205 | |
| 206 | /* Interval between regions (reg_size + (redzone_size << 1)). */ |
| 207 | size_t reg_interval; |
| 208 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 209 | /* Total size of a run for this bin's size class. */ |
| 210 | size_t run_size; |
| 211 | |
| 212 | /* Total number of regions in a run for this bin's size class. */ |
| 213 | uint32_t nregs; |
| 214 | |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 215 | /* |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 216 | * Metadata used to manipulate bitmaps for runs associated with this |
| 217 | * bin. |
| 218 | */ |
| 219 | bitmap_info_t bitmap_info; |
| 220 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 221 | /* Offset of first region in a run for this bin's size class. */ |
| 222 | uint32_t reg0_offset; |
| 223 | }; |
| 224 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 225 | struct arena_bin_s { |
| 226 | /* |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 227 | * All operations on runcur, runs, and stats require that lock be |
| 228 | * locked. Run allocation/deallocation are protected by the arena lock, |
| 229 | * which may be acquired while holding one or more bin locks, but not |
| 230 | * vise versa. |
| 231 | */ |
| 232 | malloc_mutex_t lock; |
| 233 | |
| 234 | /* |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 235 | * Current run being used to service allocations of this bin's size |
| 236 | * class. |
| 237 | */ |
| 238 | arena_run_t *runcur; |
| 239 | |
| 240 | /* |
| 241 | * Tree of non-full runs. This tree is used when looking for an |
| 242 | * existing run when runcur is no longer usable. We choose the |
| 243 | * non-full run that is lowest in memory; this policy tends to keep |
| 244 | * objects packed well, and it can also help reduce the number of |
| 245 | * almost-empty chunks. |
| 246 | */ |
| 247 | arena_run_tree_t runs; |
| 248 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 249 | /* Bin statistics. */ |
| 250 | malloc_bin_stats_t stats; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | struct arena_s { |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 254 | /* This arena's index within the arenas array. */ |
| 255 | unsigned ind; |
| 256 | |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 257 | /* |
Jason Evans | 597632b | 2011-03-18 13:41:33 -0700 | [diff] [blame] | 258 | * Number of threads currently assigned to this arena. This field is |
| 259 | * protected by arenas_lock. |
| 260 | */ |
| 261 | unsigned nthreads; |
| 262 | |
| 263 | /* |
| 264 | * There are three classes of arena operations from a locking |
| 265 | * perspective: |
| 266 | * 1) Thread asssignment (modifies nthreads) is protected by |
| 267 | * arenas_lock. |
| 268 | * 2) Bin-related operations are protected by bin locks. |
| 269 | * 3) Chunk- and run-related operations are protected by this mutex. |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 270 | */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 271 | malloc_mutex_t lock; |
| 272 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 273 | arena_stats_t stats; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 274 | /* |
| 275 | * List of tcaches for extant threads associated with this arena. |
| 276 | * Stats from these are merged incrementally, and at exit. |
| 277 | */ |
| 278 | ql_head(tcache_t) tcache_ql; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 279 | |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 280 | uint64_t prof_accumbytes; |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 281 | |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 282 | dss_prec_t dss_prec; |
| 283 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 284 | /* |
| 285 | * In order to avoid rapid chunk allocation/deallocation when an arena |
| 286 | * oscillates right on the cusp of needing a new chunk, cache the most |
| 287 | * recently freed chunk. The spare is left in the arena's chunk trees |
| 288 | * until it is deleted. |
| 289 | * |
| 290 | * There is one spare chunk per arena, rather than one spare total, in |
| 291 | * order to avoid interactions between multiple threads that could make |
| 292 | * a single spare inadequate. |
| 293 | */ |
| 294 | arena_chunk_t *spare; |
| 295 | |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 296 | /* Number of pages in active runs and huge regions. */ |
Jason Evans | bc25a47 | 2010-01-24 16:41:01 -0800 | [diff] [blame] | 297 | size_t nactive; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 298 | |
| 299 | /* |
| 300 | * Current count of pages within unused runs that are potentially |
| 301 | * dirty, and for which madvise(... MADV_DONTNEED) has not been called. |
| 302 | * By tracking this, we can institute a limit on how much dirty unused |
| 303 | * memory is mapped for each arena. |
| 304 | */ |
| 305 | size_t ndirty; |
| 306 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 307 | /* |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 308 | * Size/address-ordered trees of this arena's available runs. The trees |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 309 | * are used for first-best-fit run allocation. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 310 | */ |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 311 | arena_avail_tree_t runs_avail; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 312 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 313 | /* List of dirty runs this arena manages. */ |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 314 | arena_chunk_miscelms_t runs_dirty; |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 315 | |
aravind | fb7fe50 | 2014-05-05 15:16:56 -0700 | [diff] [blame] | 316 | /* |
| 317 | * user-configureable chunk allocation and deallocation functions. |
| 318 | */ |
| 319 | chunk_alloc_t *chunk_alloc; |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 320 | chunk_dalloc_t *chunk_dalloc; |
aravind | fb7fe50 | 2014-05-05 15:16:56 -0700 | [diff] [blame] | 321 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 322 | /* bins is used to store trees of free regions. */ |
| 323 | arena_bin_t bins[NBINS]; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | #endif /* JEMALLOC_H_STRUCTS */ |
| 327 | /******************************************************************************/ |
| 328 | #ifdef JEMALLOC_H_EXTERNS |
| 329 | |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 330 | extern ssize_t opt_lg_dirty_mult; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 331 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 332 | extern arena_bin_info_t arena_bin_info[NBINS]; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 333 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 334 | extern size_t map_bias; /* Number of arena chunk header pages. */ |
| 335 | extern size_t map_misc_offset; |
| 336 | extern size_t arena_maxrun; /* Max run size for arenas. */ |
| 337 | extern size_t arena_maxclass; /* Max size class for arenas. */ |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 338 | extern unsigned nlclasses; /* Number of large size classes. */ |
| 339 | extern unsigned nhclasses; /* Number of huge size classes. */ |
Jason Evans | 3c23435 | 2010-01-27 13:10:55 -0800 | [diff] [blame] | 340 | |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 341 | void *arena_chunk_alloc_huge(arena_t *arena, size_t usize, size_t alignment, |
| 342 | bool *zero); |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 343 | void arena_chunk_dalloc_huge(arena_t *arena, void *chunk, size_t usize); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 344 | void arena_chunk_ralloc_huge_similar(arena_t *arena, void *chunk, |
| 345 | size_t oldsize, size_t usize); |
| 346 | void arena_chunk_ralloc_huge_shrink(arena_t *arena, void *chunk, |
| 347 | size_t oldsize, size_t usize); |
| 348 | bool arena_chunk_ralloc_huge_expand(arena_t *arena, void *chunk, |
| 349 | size_t oldsize, size_t usize, bool *zero); |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 350 | void arena_purge_all(arena_t *arena); |
Jason Evans | dafde14 | 2010-03-17 16:27:39 -0700 | [diff] [blame] | 351 | void arena_tcache_fill_small(arena_t *arena, tcache_bin_t *tbin, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 352 | index_t binind, uint64_t prof_accumbytes); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 353 | void arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info, |
| 354 | bool zero); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 355 | #ifdef JEMALLOC_JET |
| 356 | typedef void (arena_redzone_corruption_t)(void *, size_t, bool, size_t, |
| 357 | uint8_t); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 358 | extern arena_redzone_corruption_t *arena_redzone_corruption; |
| 359 | typedef void (arena_dalloc_junk_small_t)(void *, arena_bin_info_t *); |
| 360 | extern arena_dalloc_junk_small_t *arena_dalloc_junk_small; |
| 361 | #else |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 362 | void arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 363 | #endif |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 364 | void arena_quarantine_junk_small(void *ptr, size_t usize); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 365 | void *arena_malloc_small(arena_t *arena, size_t size, bool zero); |
Jason Evans | dafde14 | 2010-03-17 16:27:39 -0700 | [diff] [blame] | 366 | void *arena_malloc_large(arena_t *arena, size_t size, bool zero); |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 367 | void *arena_palloc(arena_t *arena, size_t size, size_t alignment, bool zero); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 368 | void arena_prof_promoted(const void *ptr, size_t size); |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 369 | void arena_dalloc_bin_junked_locked(arena_t *arena, arena_chunk_t *chunk, |
| 370 | void *ptr, arena_chunk_map_bits_t *bitselm); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 371 | void arena_dalloc_bin(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 372 | size_t pageind, arena_chunk_map_bits_t *bitselm); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 373 | void arena_dalloc_small(arena_t *arena, arena_chunk_t *chunk, void *ptr, |
| 374 | size_t pageind); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 375 | #ifdef JEMALLOC_JET |
| 376 | typedef void (arena_dalloc_junk_large_t)(void *, size_t); |
| 377 | extern arena_dalloc_junk_large_t *arena_dalloc_junk_large; |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 378 | #else |
| 379 | void arena_dalloc_junk_large(void *ptr, size_t usize); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 380 | #endif |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 381 | void arena_dalloc_large_junked_locked(arena_t *arena, arena_chunk_t *chunk, |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 382 | void *ptr); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 383 | void arena_dalloc_large(arena_t *arena, arena_chunk_t *chunk, void *ptr); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 384 | #ifdef JEMALLOC_JET |
| 385 | typedef void (arena_ralloc_junk_large_t)(void *, size_t, size_t); |
| 386 | extern arena_ralloc_junk_large_t *arena_ralloc_junk_large; |
| 387 | #endif |
Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 388 | bool arena_ralloc_no_move(void *ptr, size_t oldsize, size_t size, |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 389 | size_t extra, bool zero); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 390 | void *arena_ralloc(tsd_t *tsd, arena_t *arena, void *ptr, size_t oldsize, |
| 391 | size_t size, size_t extra, size_t alignment, bool zero, |
| 392 | bool try_tcache_alloc, bool try_tcache_dalloc); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 393 | dss_prec_t arena_dss_prec_get(arena_t *arena); |
Jason Evans | 4d434ad | 2014-04-15 12:09:48 -0700 | [diff] [blame] | 394 | bool arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 395 | void arena_stats_merge(arena_t *arena, const char **dss, size_t *nactive, |
| 396 | size_t *ndirty, arena_stats_t *astats, malloc_bin_stats_t *bstats, |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 397 | malloc_large_stats_t *lstats, malloc_huge_stats_t *hstats); |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 398 | arena_t *arena_new(unsigned ind); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 399 | void arena_boot(void); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 400 | void arena_prefork(arena_t *arena); |
| 401 | void arena_postfork_parent(arena_t *arena); |
| 402 | void arena_postfork_child(arena_t *arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 403 | |
| 404 | #endif /* JEMALLOC_H_EXTERNS */ |
| 405 | /******************************************************************************/ |
| 406 | #ifdef JEMALLOC_H_INLINES |
| 407 | |
| 408 | #ifndef JEMALLOC_ENABLE_INLINE |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 409 | arena_chunk_map_bits_t *arena_bitselm_get(arena_chunk_t *chunk, |
| 410 | size_t pageind); |
| 411 | arena_chunk_map_misc_t *arena_miscelm_get(arena_chunk_t *chunk, |
| 412 | size_t pageind); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 413 | size_t arena_miscelm_to_pageind(arena_chunk_map_misc_t *miscelm); |
| 414 | void *arena_miscelm_to_rpages(arena_chunk_map_misc_t *miscelm); |
| 415 | arena_chunk_map_misc_t *arena_run_to_miscelm(arena_run_t *run); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 416 | size_t *arena_mapbitsp_get(arena_chunk_t *chunk, size_t pageind); |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 417 | size_t arena_mapbitsp_read(size_t *mapbitsp); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 418 | size_t arena_mapbits_get(arena_chunk_t *chunk, size_t pageind); |
| 419 | size_t arena_mapbits_unallocated_size_get(arena_chunk_t *chunk, |
| 420 | size_t pageind); |
| 421 | size_t arena_mapbits_large_size_get(arena_chunk_t *chunk, size_t pageind); |
| 422 | size_t arena_mapbits_small_runind_get(arena_chunk_t *chunk, size_t pageind); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 423 | index_t arena_mapbits_binind_get(arena_chunk_t *chunk, size_t pageind); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 424 | size_t arena_mapbits_dirty_get(arena_chunk_t *chunk, size_t pageind); |
| 425 | size_t arena_mapbits_unzeroed_get(arena_chunk_t *chunk, size_t pageind); |
| 426 | size_t arena_mapbits_large_get(arena_chunk_t *chunk, size_t pageind); |
| 427 | size_t arena_mapbits_allocated_get(arena_chunk_t *chunk, size_t pageind); |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 428 | void arena_mapbitsp_write(size_t *mapbitsp, size_t mapbits); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 429 | void arena_mapbits_unallocated_set(arena_chunk_t *chunk, size_t pageind, |
| 430 | size_t size, size_t flags); |
| 431 | void arena_mapbits_unallocated_size_set(arena_chunk_t *chunk, size_t pageind, |
| 432 | size_t size); |
| 433 | void arena_mapbits_large_set(arena_chunk_t *chunk, size_t pageind, |
| 434 | size_t size, size_t flags); |
| 435 | void arena_mapbits_large_binind_set(arena_chunk_t *chunk, size_t pageind, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 436 | index_t binind); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 437 | void arena_mapbits_small_set(arena_chunk_t *chunk, size_t pageind, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 438 | size_t runind, index_t binind, size_t flags); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 439 | void arena_mapbits_unzeroed_set(arena_chunk_t *chunk, size_t pageind, |
| 440 | size_t unzeroed); |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 441 | bool arena_prof_accum_impl(arena_t *arena, uint64_t accumbytes); |
| 442 | bool arena_prof_accum_locked(arena_t *arena, uint64_t accumbytes); |
| 443 | bool arena_prof_accum(arena_t *arena, uint64_t accumbytes); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 444 | index_t arena_ptr_small_binind_get(const void *ptr, size_t mapbits); |
| 445 | index_t arena_bin_index(arena_t *arena, arena_bin_t *bin); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 446 | unsigned arena_run_regind(arena_run_t *run, arena_bin_info_t *bin_info, |
Jason Evans | b602daa | 2011-03-15 22:19:45 -0700 | [diff] [blame] | 447 | const void *ptr); |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 448 | prof_tctx_t *arena_prof_tctx_get(const void *ptr); |
| 449 | void arena_prof_tctx_set(const void *ptr, prof_tctx_t *tctx); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 450 | void *arena_malloc(tsd_t *tsd, arena_t *arena, size_t size, bool zero, |
| 451 | bool try_tcache); |
Jason Evans | f7088e6 | 2012-04-19 18:28:03 -0700 | [diff] [blame] | 452 | size_t arena_salloc(const void *ptr, bool demote); |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 453 | void arena_dalloc(tsd_t *tsd, arena_chunk_t *chunk, void *ptr, |
| 454 | bool try_tcache); |
| 455 | void arena_sdalloc(tsd_t *tsd, arena_chunk_t *chunk, void *ptr, size_t size, |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 456 | bool try_tcache); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 457 | #endif |
| 458 | |
| 459 | #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_ARENA_C_)) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 460 | # ifdef JEMALLOC_ARENA_INLINE_A |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 461 | JEMALLOC_ALWAYS_INLINE arena_chunk_map_bits_t * |
| 462 | arena_bitselm_get(arena_chunk_t *chunk, size_t pageind) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 463 | { |
| 464 | |
| 465 | assert(pageind >= map_bias); |
| 466 | assert(pageind < chunk_npages); |
| 467 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 468 | return (&chunk->map_bits[pageind-map_bias]); |
| 469 | } |
| 470 | |
| 471 | JEMALLOC_ALWAYS_INLINE arena_chunk_map_misc_t * |
| 472 | arena_miscelm_get(arena_chunk_t *chunk, size_t pageind) |
| 473 | { |
| 474 | |
| 475 | assert(pageind >= map_bias); |
| 476 | assert(pageind < chunk_npages); |
| 477 | |
| 478 | return ((arena_chunk_map_misc_t *)((uintptr_t)chunk + |
| 479 | (uintptr_t)map_misc_offset) + pageind-map_bias); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 482 | JEMALLOC_ALWAYS_INLINE size_t |
| 483 | arena_miscelm_to_pageind(arena_chunk_map_misc_t *miscelm) |
| 484 | { |
| 485 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(miscelm); |
| 486 | size_t pageind = ((uintptr_t)miscelm - ((uintptr_t)chunk + |
| 487 | map_misc_offset)) / sizeof(arena_chunk_map_misc_t) + map_bias; |
| 488 | |
| 489 | assert(pageind >= map_bias); |
| 490 | assert(pageind < chunk_npages); |
| 491 | |
| 492 | return (pageind); |
| 493 | } |
| 494 | |
| 495 | JEMALLOC_ALWAYS_INLINE void * |
| 496 | arena_miscelm_to_rpages(arena_chunk_map_misc_t *miscelm) |
| 497 | { |
| 498 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(miscelm); |
| 499 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
| 500 | |
| 501 | return ((void *)((uintptr_t)chunk + (pageind << LG_PAGE))); |
| 502 | } |
| 503 | |
| 504 | JEMALLOC_ALWAYS_INLINE arena_chunk_map_misc_t * |
| 505 | arena_run_to_miscelm(arena_run_t *run) |
| 506 | { |
| 507 | arena_chunk_map_misc_t *miscelm = (arena_chunk_map_misc_t |
| 508 | *)((uintptr_t)run - offsetof(arena_chunk_map_misc_t, run)); |
| 509 | |
| 510 | assert(arena_miscelm_to_pageind(miscelm) >= map_bias); |
| 511 | assert(arena_miscelm_to_pageind(miscelm) < chunk_npages); |
| 512 | |
| 513 | return (miscelm); |
| 514 | } |
| 515 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 516 | JEMALLOC_ALWAYS_INLINE size_t * |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 517 | arena_mapbitsp_get(arena_chunk_t *chunk, size_t pageind) |
| 518 | { |
| 519 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 520 | return (&arena_bitselm_get(chunk, pageind)->bits); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 523 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 524 | arena_mapbitsp_read(size_t *mapbitsp) |
| 525 | { |
| 526 | |
| 527 | return (*mapbitsp); |
| 528 | } |
| 529 | |
| 530 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 531 | arena_mapbits_get(arena_chunk_t *chunk, size_t pageind) |
| 532 | { |
| 533 | |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 534 | return (arena_mapbitsp_read(arena_mapbitsp_get(chunk, pageind))); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 537 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 538 | arena_mapbits_unallocated_size_get(arena_chunk_t *chunk, size_t pageind) |
| 539 | { |
| 540 | size_t mapbits; |
| 541 | |
| 542 | mapbits = arena_mapbits_get(chunk, pageind); |
| 543 | assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == 0); |
| 544 | return (mapbits & ~PAGE_MASK); |
| 545 | } |
| 546 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 547 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 548 | arena_mapbits_large_size_get(arena_chunk_t *chunk, size_t pageind) |
| 549 | { |
| 550 | size_t mapbits; |
| 551 | |
| 552 | mapbits = arena_mapbits_get(chunk, pageind); |
| 553 | assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == |
| 554 | (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)); |
| 555 | return (mapbits & ~PAGE_MASK); |
| 556 | } |
| 557 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 558 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 559 | arena_mapbits_small_runind_get(arena_chunk_t *chunk, size_t pageind) |
| 560 | { |
| 561 | size_t mapbits; |
| 562 | |
| 563 | mapbits = arena_mapbits_get(chunk, pageind); |
| 564 | assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == |
| 565 | CHUNK_MAP_ALLOCATED); |
| 566 | return (mapbits >> LG_PAGE); |
| 567 | } |
| 568 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 569 | JEMALLOC_ALWAYS_INLINE index_t |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 570 | arena_mapbits_binind_get(arena_chunk_t *chunk, size_t pageind) |
| 571 | { |
| 572 | size_t mapbits; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 573 | index_t binind; |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 574 | |
| 575 | mapbits = arena_mapbits_get(chunk, pageind); |
| 576 | binind = (mapbits & CHUNK_MAP_BININD_MASK) >> CHUNK_MAP_BININD_SHIFT; |
| 577 | assert(binind < NBINS || binind == BININD_INVALID); |
| 578 | return (binind); |
| 579 | } |
| 580 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 581 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 582 | arena_mapbits_dirty_get(arena_chunk_t *chunk, size_t pageind) |
| 583 | { |
| 584 | size_t mapbits; |
| 585 | |
| 586 | mapbits = arena_mapbits_get(chunk, pageind); |
| 587 | return (mapbits & CHUNK_MAP_DIRTY); |
| 588 | } |
| 589 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 590 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 591 | arena_mapbits_unzeroed_get(arena_chunk_t *chunk, size_t pageind) |
| 592 | { |
| 593 | size_t mapbits; |
| 594 | |
| 595 | mapbits = arena_mapbits_get(chunk, pageind); |
| 596 | return (mapbits & CHUNK_MAP_UNZEROED); |
| 597 | } |
| 598 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 599 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 600 | arena_mapbits_large_get(arena_chunk_t *chunk, size_t pageind) |
| 601 | { |
| 602 | size_t mapbits; |
| 603 | |
| 604 | mapbits = arena_mapbits_get(chunk, pageind); |
| 605 | return (mapbits & CHUNK_MAP_LARGE); |
| 606 | } |
| 607 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 608 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 609 | arena_mapbits_allocated_get(arena_chunk_t *chunk, size_t pageind) |
| 610 | { |
| 611 | size_t mapbits; |
| 612 | |
| 613 | mapbits = arena_mapbits_get(chunk, pageind); |
| 614 | return (mapbits & CHUNK_MAP_ALLOCATED); |
| 615 | } |
| 616 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 617 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 618 | arena_mapbitsp_write(size_t *mapbitsp, size_t mapbits) |
| 619 | { |
| 620 | |
| 621 | *mapbitsp = mapbits; |
| 622 | } |
| 623 | |
| 624 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 625 | arena_mapbits_unallocated_set(arena_chunk_t *chunk, size_t pageind, size_t size, |
| 626 | size_t flags) |
| 627 | { |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 628 | size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 629 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 630 | assert((size & PAGE_MASK) == 0); |
| 631 | assert((flags & ~CHUNK_MAP_FLAGS_MASK) == 0); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 632 | assert((flags & (CHUNK_MAP_DIRTY|CHUNK_MAP_UNZEROED)) == flags); |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 633 | arena_mapbitsp_write(mapbitsp, size | CHUNK_MAP_BININD_INVALID | flags); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 636 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 637 | arena_mapbits_unallocated_size_set(arena_chunk_t *chunk, size_t pageind, |
| 638 | size_t size) |
| 639 | { |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 640 | size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind); |
| 641 | size_t mapbits = arena_mapbitsp_read(mapbitsp); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 642 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 643 | assert((size & PAGE_MASK) == 0); |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 644 | assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == 0); |
| 645 | arena_mapbitsp_write(mapbitsp, size | (mapbits & PAGE_MASK)); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 648 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 649 | arena_mapbits_large_set(arena_chunk_t *chunk, size_t pageind, size_t size, |
| 650 | size_t flags) |
| 651 | { |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 652 | size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind); |
| 653 | size_t mapbits = arena_mapbitsp_read(mapbitsp); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 654 | size_t unzeroed; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 655 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 656 | assert((size & PAGE_MASK) == 0); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 657 | assert((flags & CHUNK_MAP_DIRTY) == flags); |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 658 | unzeroed = mapbits & CHUNK_MAP_UNZEROED; /* Preserve unzeroed. */ |
| 659 | arena_mapbitsp_write(mapbitsp, size | CHUNK_MAP_BININD_INVALID | flags |
| 660 | | unzeroed | CHUNK_MAP_LARGE | CHUNK_MAP_ALLOCATED); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 663 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 664 | arena_mapbits_large_binind_set(arena_chunk_t *chunk, size_t pageind, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 665 | index_t binind) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 666 | { |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 667 | size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind); |
| 668 | size_t mapbits = arena_mapbitsp_read(mapbitsp); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 669 | |
| 670 | assert(binind <= BININD_INVALID); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 671 | assert(arena_mapbits_large_size_get(chunk, pageind) == LARGE_MINCLASS); |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 672 | arena_mapbitsp_write(mapbitsp, (mapbits & ~CHUNK_MAP_BININD_MASK) | |
| 673 | (binind << CHUNK_MAP_BININD_SHIFT)); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 676 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 677 | arena_mapbits_small_set(arena_chunk_t *chunk, size_t pageind, size_t runind, |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 678 | index_t binind, size_t flags) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 679 | { |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 680 | size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind); |
| 681 | size_t mapbits = arena_mapbitsp_read(mapbitsp); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 682 | size_t unzeroed; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 683 | |
| 684 | assert(binind < BININD_INVALID); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 685 | assert(pageind - runind >= map_bias); |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 686 | assert((flags & CHUNK_MAP_DIRTY) == flags); |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 687 | unzeroed = mapbits & CHUNK_MAP_UNZEROED; /* Preserve unzeroed. */ |
| 688 | arena_mapbitsp_write(mapbitsp, (runind << LG_PAGE) | (binind << |
| 689 | CHUNK_MAP_BININD_SHIFT) | flags | unzeroed | CHUNK_MAP_ALLOCATED); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 692 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 693 | arena_mapbits_unzeroed_set(arena_chunk_t *chunk, size_t pageind, |
| 694 | size_t unzeroed) |
| 695 | { |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 696 | size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind); |
| 697 | size_t mapbits = arena_mapbitsp_read(mapbitsp); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 698 | |
Jason Evans | 87a02d2 | 2013-10-19 21:40:20 -0700 | [diff] [blame] | 699 | arena_mapbitsp_write(mapbitsp, (mapbits & ~CHUNK_MAP_UNZEROED) | |
| 700 | unzeroed); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 703 | JEMALLOC_INLINE bool |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 704 | arena_prof_accum_impl(arena_t *arena, uint64_t accumbytes) |
| 705 | { |
| 706 | |
| 707 | cassert(config_prof); |
| 708 | assert(prof_interval != 0); |
| 709 | |
| 710 | arena->prof_accumbytes += accumbytes; |
| 711 | if (arena->prof_accumbytes >= prof_interval) { |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 712 | arena->prof_accumbytes -= prof_interval; |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 713 | return (true); |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 714 | } |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 715 | return (false); |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 718 | JEMALLOC_INLINE bool |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 719 | arena_prof_accum_locked(arena_t *arena, uint64_t accumbytes) |
| 720 | { |
| 721 | |
| 722 | cassert(config_prof); |
| 723 | |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 724 | if (likely(prof_interval == 0)) |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 725 | return (false); |
| 726 | return (arena_prof_accum_impl(arena, accumbytes)); |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 729 | JEMALLOC_INLINE bool |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 730 | arena_prof_accum(arena_t *arena, uint64_t accumbytes) |
| 731 | { |
| 732 | |
| 733 | cassert(config_prof); |
| 734 | |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 735 | if (likely(prof_interval == 0)) |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 736 | return (false); |
| 737 | |
| 738 | { |
| 739 | bool ret; |
| 740 | |
| 741 | malloc_mutex_lock(&arena->lock); |
| 742 | ret = arena_prof_accum_impl(arena, accumbytes); |
| 743 | malloc_mutex_unlock(&arena->lock); |
| 744 | return (ret); |
| 745 | } |
Jason Evans | a3b3386 | 2012-11-13 12:56:27 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 748 | JEMALLOC_ALWAYS_INLINE index_t |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 749 | arena_ptr_small_binind_get(const void *ptr, size_t mapbits) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 750 | { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 751 | index_t binind; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 752 | |
| 753 | binind = (mapbits & CHUNK_MAP_BININD_MASK) >> CHUNK_MAP_BININD_SHIFT; |
| 754 | |
| 755 | if (config_debug) { |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 756 | arena_chunk_t *chunk; |
| 757 | arena_t *arena; |
| 758 | size_t pageind; |
| 759 | size_t actual_mapbits; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 760 | size_t rpages_ind; |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 761 | arena_run_t *run; |
| 762 | arena_bin_t *bin; |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 763 | index_t run_binind, actual_binind; |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 764 | arena_bin_info_t *bin_info; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 765 | arena_chunk_map_misc_t *miscelm; |
| 766 | void *rpages; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 767 | |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 768 | assert(binind != BININD_INVALID); |
| 769 | assert(binind < NBINS); |
| 770 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
| 771 | arena = chunk->arena; |
| 772 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
| 773 | actual_mapbits = arena_mapbits_get(chunk, pageind); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 774 | assert(mapbits == actual_mapbits); |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 775 | assert(arena_mapbits_large_get(chunk, pageind) == 0); |
| 776 | assert(arena_mapbits_allocated_get(chunk, pageind) != 0); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 777 | rpages_ind = pageind - arena_mapbits_small_runind_get(chunk, |
| 778 | pageind); |
| 779 | miscelm = arena_miscelm_get(chunk, rpages_ind); |
| 780 | run = &miscelm->run; |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 781 | run_binind = run->binind; |
| 782 | bin = &arena->bins[run_binind]; |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 783 | actual_binind = bin - arena->bins; |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 784 | assert(run_binind == actual_binind); |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 785 | bin_info = &arena_bin_info[actual_binind]; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 786 | rpages = arena_miscelm_to_rpages(miscelm); |
| 787 | assert(((uintptr_t)ptr - ((uintptr_t)rpages + |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 788 | (uintptr_t)bin_info->reg0_offset)) % bin_info->reg_interval |
| 789 | == 0); |
| 790 | } |
| 791 | |
| 792 | return (binind); |
| 793 | } |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 794 | # endif /* JEMALLOC_ARENA_INLINE_A */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 795 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 796 | # ifdef JEMALLOC_ARENA_INLINE_B |
| 797 | JEMALLOC_INLINE index_t |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 798 | arena_bin_index(arena_t *arena, arena_bin_t *bin) |
| 799 | { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 800 | index_t binind = bin - arena->bins; |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 801 | assert(binind < NBINS); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 802 | return (binind); |
| 803 | } |
| 804 | |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 805 | JEMALLOC_INLINE unsigned |
Jason Evans | b602daa | 2011-03-15 22:19:45 -0700 | [diff] [blame] | 806 | arena_run_regind(arena_run_t *run, arena_bin_info_t *bin_info, const void *ptr) |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 807 | { |
| 808 | unsigned shift, diff, regind; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 809 | size_t interval; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 810 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
| 811 | void *rpages = arena_miscelm_to_rpages(miscelm); |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 812 | |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 813 | /* |
| 814 | * Freeing a pointer lower than region zero can cause assertion |
| 815 | * failure. |
| 816 | */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 817 | assert((uintptr_t)ptr >= (uintptr_t)rpages + |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 818 | (uintptr_t)bin_info->reg0_offset); |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 819 | |
| 820 | /* |
| 821 | * Avoid doing division with a variable divisor if possible. Using |
| 822 | * actual division here can reduce allocator throughput by over 20%! |
| 823 | */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 824 | diff = (unsigned)((uintptr_t)ptr - (uintptr_t)rpages - |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 825 | bin_info->reg0_offset); |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 826 | |
| 827 | /* Rescale (factor powers of 2 out of the numerator and denominator). */ |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 828 | interval = bin_info->reg_interval; |
Richard Diamond | 9c3a10f | 2014-05-28 21:37:02 -0500 | [diff] [blame] | 829 | shift = jemalloc_ffs(interval) - 1; |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 830 | diff >>= shift; |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 831 | interval >>= shift; |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 832 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 833 | if (interval == 1) { |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 834 | /* The divisor was a power of 2. */ |
| 835 | regind = diff; |
| 836 | } else { |
| 837 | /* |
| 838 | * To divide by a number D that is not a power of two we |
| 839 | * multiply by (2^21 / D) and then right shift by 21 positions. |
| 840 | * |
| 841 | * X / D |
| 842 | * |
| 843 | * becomes |
| 844 | * |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 845 | * (X * interval_invs[D - 3]) >> SIZE_INV_SHIFT |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 846 | * |
| 847 | * We can omit the first three elements, because we never |
| 848 | * divide by 0, and 1 and 2 are both powers of two, which are |
| 849 | * handled above. |
| 850 | */ |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 851 | #define SIZE_INV_SHIFT ((sizeof(unsigned) << 3) - LG_RUN_MAXREGS) |
| 852 | #define SIZE_INV(s) (((1U << SIZE_INV_SHIFT) / (s)) + 1) |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 853 | static const unsigned interval_invs[] = { |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 854 | SIZE_INV(3), |
| 855 | SIZE_INV(4), SIZE_INV(5), SIZE_INV(6), SIZE_INV(7), |
| 856 | SIZE_INV(8), SIZE_INV(9), SIZE_INV(10), SIZE_INV(11), |
| 857 | SIZE_INV(12), SIZE_INV(13), SIZE_INV(14), SIZE_INV(15), |
| 858 | SIZE_INV(16), SIZE_INV(17), SIZE_INV(18), SIZE_INV(19), |
| 859 | SIZE_INV(20), SIZE_INV(21), SIZE_INV(22), SIZE_INV(23), |
| 860 | SIZE_INV(24), SIZE_INV(25), SIZE_INV(26), SIZE_INV(27), |
| 861 | SIZE_INV(28), SIZE_INV(29), SIZE_INV(30), SIZE_INV(31) |
| 862 | }; |
| 863 | |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 864 | if (likely(interval <= ((sizeof(interval_invs) / |
| 865 | sizeof(unsigned)) + 2))) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 866 | regind = (diff * interval_invs[interval - 3]) >> |
| 867 | SIZE_INV_SHIFT; |
| 868 | } else |
| 869 | regind = diff / interval; |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 870 | #undef SIZE_INV |
| 871 | #undef SIZE_INV_SHIFT |
| 872 | } |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 873 | assert(diff == regind * interval); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 874 | assert(regind < bin_info->nregs); |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 875 | |
| 876 | return (regind); |
| 877 | } |
| 878 | |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 879 | JEMALLOC_INLINE prof_tctx_t * |
| 880 | arena_prof_tctx_get(const void *ptr) |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 881 | { |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 882 | prof_tctx_t *ret; |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 883 | arena_chunk_t *chunk; |
| 884 | size_t pageind, mapbits; |
| 885 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 886 | cassert(config_prof); |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 887 | assert(ptr != NULL); |
| 888 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
| 889 | |
| 890 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 891 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 892 | mapbits = arena_mapbits_get(chunk, pageind); |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 893 | assert((mapbits & CHUNK_MAP_ALLOCATED) != 0); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 894 | if (likely((mapbits & CHUNK_MAP_LARGE) == 0)) |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 895 | ret = (prof_tctx_t *)(uintptr_t)1U; |
Jason Evans | 9b0cbf0 | 2014-04-11 14:24:51 -0700 | [diff] [blame] | 896 | else |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 897 | ret = arena_miscelm_get(chunk, pageind)->prof_tctx; |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 898 | |
| 899 | return (ret); |
| 900 | } |
Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 901 | |
| 902 | JEMALLOC_INLINE void |
Jason Evans | 602c8e0 | 2014-08-18 16:22:13 -0700 | [diff] [blame] | 903 | arena_prof_tctx_set(const void *ptr, prof_tctx_t *tctx) |
Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 904 | { |
| 905 | arena_chunk_t *chunk; |
Jason Evans | 5fbad09 | 2013-12-15 22:08:44 -0800 | [diff] [blame] | 906 | size_t pageind; |
Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 907 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 908 | cassert(config_prof); |
Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 909 | assert(ptr != NULL); |
| 910 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
| 911 | |
| 912 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 913 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 5fbad09 | 2013-12-15 22:08:44 -0800 | [diff] [blame] | 914 | assert(arena_mapbits_allocated_get(chunk, pageind) != 0); |
Jason Evans | 6657693 | 2013-12-15 16:21:30 -0800 | [diff] [blame] | 915 | |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 916 | if (unlikely(arena_mapbits_large_get(chunk, pageind) != 0)) |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 917 | arena_miscelm_get(chunk, pageind)->prof_tctx = tctx; |
Jason Evans | e4f7846 | 2010-10-22 10:45:59 -0700 | [diff] [blame] | 918 | } |
Jason Evans | 81b4e6e | 2010-10-20 20:52:00 -0700 | [diff] [blame] | 919 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 920 | JEMALLOC_ALWAYS_INLINE void * |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 921 | arena_malloc(tsd_t *tsd, arena_t *arena, size_t size, bool zero, |
| 922 | bool try_tcache) |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 923 | { |
Jason Evans | ef8897b | 2012-02-13 14:30:52 -0800 | [diff] [blame] | 924 | tcache_t *tcache; |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 925 | |
| 926 | assert(size != 0); |
Jason Evans | cd9a134 | 2012-03-21 18:33:03 -0700 | [diff] [blame] | 927 | assert(size <= arena_maxclass); |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 928 | |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 929 | if (likely(size <= SMALL_MAXCLASS)) { |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 930 | if (likely(try_tcache) && likely((tcache = tcache_get(tsd, |
| 931 | true)) != NULL)) |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 932 | return (tcache_alloc_small(tcache, size, zero)); |
Jason Evans | 01b3fe5 | 2012-04-03 09:28:00 -0700 | [diff] [blame] | 933 | else { |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 934 | arena = arena_choose(tsd, arena); |
| 935 | if (unlikely(arena == NULL)) |
| 936 | return (NULL); |
| 937 | return (arena_malloc_small(arena, size, zero)); |
Jason Evans | 01b3fe5 | 2012-04-03 09:28:00 -0700 | [diff] [blame] | 938 | } |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 939 | } else { |
Jason Evans | 7468689 | 2012-02-13 15:18:19 -0800 | [diff] [blame] | 940 | /* |
| 941 | * Initialize tcache after checking size in order to avoid |
| 942 | * infinite recursion during tcache initialization. |
| 943 | */ |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 944 | if (try_tcache && size <= tcache_maxclass && likely((tcache = |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 945 | tcache_get(tsd, true)) != NULL)) |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 946 | return (tcache_alloc_large(tcache, size, zero)); |
Jason Evans | 01b3fe5 | 2012-04-03 09:28:00 -0700 | [diff] [blame] | 947 | else { |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 948 | arena = arena_choose(tsd, arena); |
| 949 | if (unlikely(arena == NULL)) |
| 950 | return (NULL); |
| 951 | return (arena_malloc_large(arena, size, zero)); |
Jason Evans | 01b3fe5 | 2012-04-03 09:28:00 -0700 | [diff] [blame] | 952 | } |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
Jason Evans | f7088e6 | 2012-04-19 18:28:03 -0700 | [diff] [blame] | 956 | /* Return the size of the allocation pointed to by ptr. */ |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 957 | JEMALLOC_ALWAYS_INLINE size_t |
Jason Evans | f7088e6 | 2012-04-19 18:28:03 -0700 | [diff] [blame] | 958 | arena_salloc(const void *ptr, bool demote) |
| 959 | { |
| 960 | size_t ret; |
| 961 | arena_chunk_t *chunk; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 962 | size_t pageind; |
| 963 | index_t binind; |
Jason Evans | f7088e6 | 2012-04-19 18:28:03 -0700 | [diff] [blame] | 964 | |
| 965 | assert(ptr != NULL); |
| 966 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
| 967 | |
| 968 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
| 969 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 970 | assert(arena_mapbits_allocated_get(chunk, pageind) != 0); |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 971 | binind = arena_mapbits_binind_get(chunk, pageind); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 972 | if (unlikely(binind == BININD_INVALID || (config_prof && !demote && |
| 973 | arena_mapbits_large_get(chunk, pageind) != 0))) { |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 974 | /* |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 975 | * Large allocation. In the common case (demote), and as this |
| 976 | * is an inline function, most callers will only end up looking |
| 977 | * at binind to determine that ptr is a small allocation. |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 978 | */ |
Jason Evans | f7088e6 | 2012-04-19 18:28:03 -0700 | [diff] [blame] | 979 | assert(((uintptr_t)ptr & PAGE_MASK) == 0); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 980 | ret = arena_mapbits_large_size_get(chunk, pageind); |
Jason Evans | f7088e6 | 2012-04-19 18:28:03 -0700 | [diff] [blame] | 981 | assert(ret != 0); |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 982 | assert(pageind + (ret>>LG_PAGE) <= chunk_npages); |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 983 | assert(arena_mapbits_dirty_get(chunk, pageind) == |
| 984 | arena_mapbits_dirty_get(chunk, pageind+(ret>>LG_PAGE)-1)); |
| 985 | } else { |
Jason Evans | 9b0cbf0 | 2014-04-11 14:24:51 -0700 | [diff] [blame] | 986 | /* Small allocation (possibly promoted to a large object). */ |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 987 | assert(arena_mapbits_large_get(chunk, pageind) != 0 || |
| 988 | arena_ptr_small_binind_get(ptr, arena_mapbits_get(chunk, |
| 989 | pageind)) == binind); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 990 | ret = index2size(binind); |
Jason Evans | f7088e6 | 2012-04-19 18:28:03 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return (ret); |
| 994 | } |
| 995 | |
Jason Evans | 88393cb | 2013-01-22 08:45:43 -0800 | [diff] [blame] | 996 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 997 | arena_dalloc(tsd_t *tsd, arena_chunk_t *chunk, void *ptr, bool try_tcache) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 998 | { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 999 | size_t pageind, mapbits; |
Jason Evans | 01b3fe5 | 2012-04-03 09:28:00 -0700 | [diff] [blame] | 1000 | tcache_t *tcache; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1001 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1002 | assert(ptr != NULL); |
| 1003 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
| 1004 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1005 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1006 | mapbits = arena_mapbits_get(chunk, pageind); |
| 1007 | assert(arena_mapbits_allocated_get(chunk, pageind) != 0); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 1008 | if (likely((mapbits & CHUNK_MAP_LARGE) == 0)) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1009 | /* Small allocation. */ |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1010 | if (likely(try_tcache) && likely((tcache = tcache_get(tsd, |
| 1011 | false)) != NULL)) { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1012 | index_t binind = arena_ptr_small_binind_get(ptr, |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 1013 | mapbits); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1014 | tcache_dalloc_small(tcache, ptr, binind); |
| 1015 | } else |
Ben Maurer | be8e59f | 2014-04-05 15:59:08 -0700 | [diff] [blame] | 1016 | arena_dalloc_small(chunk->arena, chunk, ptr, pageind); |
Jason Evans | f00bb7f | 2010-03-15 16:38:27 -0700 | [diff] [blame] | 1017 | } else { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1018 | size_t size = arena_mapbits_large_size_get(chunk, pageind); |
Jason Evans | dafde14 | 2010-03-17 16:27:39 -0700 | [diff] [blame] | 1019 | |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 1020 | assert(((uintptr_t)ptr & PAGE_MASK) == 0); |
Jason Evans | dafde14 | 2010-03-17 16:27:39 -0700 | [diff] [blame] | 1021 | |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 1022 | if (try_tcache && size <= tcache_maxclass && likely((tcache = |
Jason Evans | 6da2e9d | 2014-10-31 17:08:13 -0700 | [diff] [blame] | 1023 | tcache_get(tsd, false)) != NULL)) |
Jason Evans | 962463d | 2012-02-13 12:29:49 -0800 | [diff] [blame] | 1024 | tcache_dalloc_large(tcache, ptr, size); |
Jason Evans | 6da2e9d | 2014-10-31 17:08:13 -0700 | [diff] [blame] | 1025 | else |
Ben Maurer | be8e59f | 2014-04-05 15:59:08 -0700 | [diff] [blame] | 1026 | arena_dalloc_large(chunk->arena, chunk, ptr); |
Jason Evans | f00bb7f | 2010-03-15 16:38:27 -0700 | [diff] [blame] | 1027 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1028 | } |
Daniel Micay | 4cfe551 | 2014-08-28 15:41:48 -0400 | [diff] [blame] | 1029 | |
| 1030 | JEMALLOC_ALWAYS_INLINE void |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1031 | arena_sdalloc(tsd_t *tsd, arena_chunk_t *chunk, void *ptr, size_t size, |
| 1032 | bool try_tcache) |
Daniel Micay | 4cfe551 | 2014-08-28 15:41:48 -0400 | [diff] [blame] | 1033 | { |
| 1034 | tcache_t *tcache; |
| 1035 | |
| 1036 | assert(ptr != NULL); |
| 1037 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
| 1038 | |
Jason Evans | 6da2e9d | 2014-10-31 17:08:13 -0700 | [diff] [blame] | 1039 | if (config_prof && opt_prof) { |
Jason Evans | 6da2e9d | 2014-10-31 17:08:13 -0700 | [diff] [blame] | 1040 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | d7a9bab | 2014-10-31 22:26:24 -0700 | [diff] [blame] | 1041 | assert(arena_mapbits_allocated_get(chunk, pageind) != 0); |
| 1042 | if (arena_mapbits_large_get(chunk, pageind) != 0) { |
| 1043 | /* Make sure to use promoted size, not request size. */ |
| 1044 | assert(((uintptr_t)ptr & PAGE_MASK) == 0); |
| 1045 | size = arena_mapbits_large_size_get(chunk, pageind); |
| 1046 | } |
| 1047 | } |
| 1048 | assert(s2u(size) == s2u(arena_salloc(ptr, false))); |
Jason Evans | 6da2e9d | 2014-10-31 17:08:13 -0700 | [diff] [blame] | 1049 | |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 1050 | if (likely(size <= SMALL_MAXCLASS)) { |
Daniel Micay | 4cfe551 | 2014-08-28 15:41:48 -0400 | [diff] [blame] | 1051 | /* Small allocation. */ |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 1052 | if (likely(try_tcache) && likely((tcache = tcache_get(tsd, |
Jason Evans | d7a9bab | 2014-10-31 22:26:24 -0700 | [diff] [blame] | 1053 | false)) != NULL)) { |
| 1054 | index_t binind = size2index(size); |
Daniel Micay | 4cfe551 | 2014-08-28 15:41:48 -0400 | [diff] [blame] | 1055 | tcache_dalloc_small(tcache, ptr, binind); |
Jason Evans | d7a9bab | 2014-10-31 22:26:24 -0700 | [diff] [blame] | 1056 | } else { |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 1057 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> |
| 1058 | LG_PAGE; |
Daniel Micay | 4cfe551 | 2014-08-28 15:41:48 -0400 | [diff] [blame] | 1059 | arena_dalloc_small(chunk->arena, chunk, ptr, pageind); |
| 1060 | } |
| 1061 | } else { |
| 1062 | assert(((uintptr_t)ptr & PAGE_MASK) == 0); |
| 1063 | |
| 1064 | if (try_tcache && size <= tcache_maxclass && (tcache = |
Jason Evans | 6da2e9d | 2014-10-31 17:08:13 -0700 | [diff] [blame] | 1065 | tcache_get(tsd, false)) != NULL) |
Daniel Micay | 4cfe551 | 2014-08-28 15:41:48 -0400 | [diff] [blame] | 1066 | tcache_dalloc_large(tcache, ptr, size); |
Jason Evans | 6da2e9d | 2014-10-31 17:08:13 -0700 | [diff] [blame] | 1067 | else |
Daniel Micay | 4cfe551 | 2014-08-28 15:41:48 -0400 | [diff] [blame] | 1068 | arena_dalloc_large(chunk->arena, chunk, ptr); |
| 1069 | } |
| 1070 | } |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 1071 | # endif /* JEMALLOC_ARENA_INLINE_B */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1072 | #endif |
| 1073 | |
| 1074 | #endif /* JEMALLOC_H_INLINES */ |
| 1075 | /******************************************************************************/ |