1 /******************************************************************************/
2 #ifdef JEMALLOC_H_TYPES
3
4 #define LARGE_MINCLASS (ZU(1) << LG_LARGE_MINCLASS)
5
6 /* Maximum number of regions in one run. */
7 #define LG_RUN_MAXREGS (LG_PAGE - LG_TINY_MIN)
8 #define RUN_MAXREGS (1U << LG_RUN_MAXREGS)
9
10 /*
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 /*
17 * The minimum ratio of active:dirty pages per arena is computed as:
18 *
19 * (nactive >> lg_dirty_mult) >= ndirty
20 *
21 * So, supposing that lg_dirty_mult is 3, there can be no less than 8 times as
22 * many active pages as dirty pages.
23 */
24 #define LG_DIRTY_MULT_DEFAULT 3
25
26 typedef struct arena_runs_dirty_link_s arena_runs_dirty_link_t;
27 typedef struct arena_run_s arena_run_t;
28 typedef struct arena_chunk_map_bits_s arena_chunk_map_bits_t;
29 typedef struct arena_chunk_map_misc_s arena_chunk_map_misc_t;
30 typedef struct arena_chunk_s arena_chunk_t;
31 typedef struct arena_bin_info_s arena_bin_info_t;
32 typedef struct arena_bin_s arena_bin_t;
33 typedef struct arena_s arena_t;
34
35 #endif /* JEMALLOC_H_TYPES */
36 /******************************************************************************/
37 #ifdef JEMALLOC_H_STRUCTS
38
39 #ifdef JEMALLOC_ARENA_STRUCTS_A
40 struct arena_run_s {
41 /* Index of bin this run is associated with. */
42 szind_t binind;
43
44 /* Number of free regions in run. */
45 unsigned nfree;
46
47 /* Per region allocated/deallocated bitmap. */
48 bitmap_t bitmap[BITMAP_GROUPS_MAX];
49 };
50
51 /* Each element of the chunk map corresponds to one page within the chunk. */
52 struct arena_chunk_map_bits_s {
53 /*
54 * Run address (or size) and various flags are stored together. The bit
55 * layout looks like (assuming 32-bit system):
56 *
57 * ???????? ???????? ???nnnnn nnndumla
58 *
59 * ? : Unallocated: Run address for first/last pages, unset for internal
60 * pages.
61 * Small: Run page offset.
62 * Large: Run page count for first page, unset for trailing pages.
63 * n : binind for small size class, BININD_INVALID for large size class.
64 * d : dirty?
65 * u : unzeroed?
66 * m : decommitted?
67 * l : large?
68 * a : allocated?
69 *
70 * Following are example bit patterns for the three types of runs.
71 *
72 * p : run page offset
73 * s : run size
74 * n : binind for size class; large objects set these to BININD_INVALID
75 * x : don't care
76 * - : 0
77 * + : 1
78 * [DUMLA] : bit set
79 * [dumla] : bit unset
80 *
81 * Unallocated (clean):
82 * ssssssss ssssssss sss+++++ +++dum-a
83 * xxxxxxxx xxxxxxxx xxxxxxxx xxx-Uxxx
84 * ssssssss ssssssss sss+++++ +++dUm-a
85 *
86 * Unallocated (dirty):
87 * ssssssss ssssssss sss+++++ +++D-m-a
88 * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
89 * ssssssss ssssssss sss+++++ +++D-m-a
90 *
91 * Small:
92 * pppppppp pppppppp pppnnnnn nnnd---A
93 * pppppppp pppppppp pppnnnnn nnn----A
94 * pppppppp pppppppp pppnnnnn nnnd---A
95 *
96 * Large:
97 * ssssssss ssssssss sss+++++ +++D--LA
98 * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
99 * -------- -------- ---+++++ +++D--LA
100 *
101 * Large (sampled, size <= LARGE_MINCLASS):
102 * ssssssss ssssssss sssnnnnn nnnD--LA
103 * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
104 * -------- -------- ---+++++ +++D--LA
105 *
106 * Large (not sampled, size == LARGE_MINCLASS):
107 * ssssssss ssssssss sss+++++ +++D--LA
108 * xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
109 * -------- -------- ---+++++ +++D--LA
110 */
111 size_t bits;
112 #define CHUNK_MAP_ALLOCATED ((size_t)0x01U)
113 #define CHUNK_MAP_LARGE ((size_t)0x02U)
114 #define CHUNK_MAP_STATE_MASK ((size_t)0x3U)
115
116 #define CHUNK_MAP_DECOMMITTED ((size_t)0x04U)
117 #define CHUNK_MAP_UNZEROED ((size_t)0x08U)
118 #define CHUNK_MAP_DIRTY ((size_t)0x10U)
119 #define CHUNK_MAP_FLAGS_MASK ((size_t)0x1cU)
120
121 #define CHUNK_MAP_BININD_SHIFT 5
122 #define BININD_INVALID ((size_t)0xffU)
123 #define CHUNK_MAP_BININD_MASK (BININD_INVALID << CHUNK_MAP_BININD_SHIFT)
124 #define CHUNK_MAP_BININD_INVALID CHUNK_MAP_BININD_MASK
125
126 #define CHUNK_MAP_RUNIND_SHIFT (CHUNK_MAP_BININD_SHIFT + 8)
127 #define CHUNK_MAP_SIZE_SHIFT (CHUNK_MAP_RUNIND_SHIFT - LG_PAGE)
128 #define CHUNK_MAP_SIZE_MASK \
129 (~(CHUNK_MAP_BININD_MASK | CHUNK_MAP_FLAGS_MASK | CHUNK_MAP_STATE_MASK))
130 };
131
132 struct arena_runs_dirty_link_s {
133 qr(arena_runs_dirty_link_t) rd_link;
134 };
135
136 /*
137 * Each arena_chunk_map_misc_t corresponds to one page within the chunk, just
138 * like arena_chunk_map_bits_t. Two separate arrays are stored within each
139 * chunk header in order to improve cache locality.
140 */
141 struct arena_chunk_map_misc_s {
142 /*
143 * Linkage for run trees. There are two disjoint uses:
144 *
145 * 1) arena_t's runs_avail tree.
146 * 2) arena_run_t conceptually uses this linkage for in-use non-full
147 * runs, rather than directly embedding linkage.
148 */
149 rb_node(arena_chunk_map_misc_t) rb_link;
150
151 union {
152 /* Linkage for list of dirty runs. */
153 arena_runs_dirty_link_t rd;
154
155 /* Profile counters, used for large object runs. */
156 union {
157 void *prof_tctx_pun;
158 prof_tctx_t *prof_tctx;
159 };
160
161 /* Small region run metadata. */
162 arena_run_t run;
163 };
164 };
165 typedef rb_tree(arena_chunk_map_misc_t) arena_avail_tree_t;
166 typedef rb_tree(arena_chunk_map_misc_t) arena_run_tree_t;
167 #endif /* JEMALLOC_ARENA_STRUCTS_A */
168
169 #ifdef JEMALLOC_ARENA_STRUCTS_B
170 /* Arena chunk header. */
171 struct arena_chunk_s {
172 /*
173 * A pointer to the arena that owns the chunk is stored within the node.
174 * This field as a whole is used by chunks_rtree to support both
175 * ivsalloc() and core-based debugging.
176 */
177 extent_node_t node;
178
179 /*
180 * Map of pages within chunk that keeps track of free/large/small. The
181 * first map_bias entries are omitted, since the chunk header does not
182 * need to be tracked in the map. This omission saves a header page
183 * for common chunk sizes (e.g. 4 MiB).
184 */
185 arena_chunk_map_bits_t map_bits[1]; /* Dynamically sized. */
186 };
187
188 /*
189 * Read-only information associated with each element of arena_t's bins array
190 * is stored separately, partly to reduce memory usage (only one copy, rather
191 * than one per arena), but mainly to avoid false cacheline sharing.
192 *
193 * Each run has the following layout:
194 *
195 * /--------------------\
196 * | pad? |
197 * |--------------------|
198 * | redzone |
199 * reg0_offset | region 0 |
200 * | redzone |
201 * |--------------------| \
202 * | redzone | |
203 * | region 1 | > reg_interval
204 * | redzone | /
205 * |--------------------|
206 * | ... |
207 * | ... |
208 * | ... |
209 * |--------------------|
210 * | redzone |
211 * | region nregs-1 |
212 * | redzone |
213 * |--------------------|
214 * | alignment pad? |
215 * \--------------------/
216 *
217 * reg_interval has at least the same minimum alignment as reg_size; this
218 * preserves the alignment constraint that sa2u() depends on. Alignment pad is
219 * either 0 or redzone_size; it is present only if needed to align reg0_offset.
220 */
221 struct arena_bin_info_s {
222 /* Size of regions in a run for this bin's size class. */
223 size_t reg_size;
224
225 /* Redzone size. */
226 size_t redzone_size;
227
228 /* Interval between regions (reg_size + (redzone_size << 1)). */
229 size_t reg_interval;
230
231 /* Total size of a run for this bin's size class. */
232 size_t run_size;
233
234 /* Total number of regions in a run for this bin's size class. */
235 uint32_t nregs;
236
237 /*
238 * Metadata used to manipulate bitmaps for runs associated with this
239 * bin.
240 */
241 bitmap_info_t bitmap_info;
242
243 /* Offset of first region in a run for this bin's size class. */
244 uint32_t reg0_offset;
245 };
246
247 struct arena_bin_s {
248 /*
249 * All operations on runcur, runs, and stats require that lock be
250 * locked. Run allocation/deallocation are protected by the arena lock,
251 * which may be acquired while holding one or more bin locks, but not
252 * vise versa.
253 */
254 malloc_mutex_t lock;
255
256 /*
257 * Current run being used to service allocations of this bin's size
258 * class.
259 */
260 arena_run_t *runcur;
261
262 /*
263 * Tree of non-full runs. This tree is used when looking for an
264 * existing run when runcur is no longer usable. We choose the
265 * non-full run that is lowest in memory; this policy tends to keep
266 * objects packed well, and it can also help reduce the number of
267 * almost-empty chunks.
268 */
269 arena_run_tree_t runs;
270
271 /* Bin statistics. */
272 malloc_bin_stats_t stats;
273 };
274
275 struct arena_s {
276 /* This arena's index within the arenas array. */
277 unsigned ind;
278
279 /*
280 * Number of threads currently assigned to this arena. This field is
281 * protected by arenas_lock.
282 */
283 unsigned nthreads;
284
285 /*
286 * There are three classes of arena operations from a locking
287 * perspective:
288 * 1) Thread assignment (modifies nthreads) is protected by arenas_lock.
289 * 2) Bin-related operations are protected by bin locks.
290 * 3) Chunk- and run-related operations are protected by this mutex.
291 */
292 malloc_mutex_t lock;
293
294 arena_stats_t stats;
295 /*
296 * List of tcaches for extant threads associated with this arena.
297 * Stats from these are merged incrementally, and at exit if
298 * opt_stats_print is enabled.
299 */
300 ql_head(tcache_t) tcache_ql;
301
302 uint64_t prof_accumbytes;
303
304 /*
305 * PRNG state for cache index randomization of large allocation base
306 * pointers.
307 */
308 uint64_t offset_state;
309
310 dss_prec_t dss_prec;
311
312 /*
313 * In order to avoid rapid chunk allocation/deallocation when an arena
314 * oscillates right on the cusp of needing a new chunk, cache the most
315 * recently freed chunk. The spare is left in the arena's chunk trees
316 * until it is deleted.
317 *
318 * There is one spare chunk per arena, rather than one spare total, in
319 * order to avoid interactions between multiple threads that could make
320 * a single spare inadequate.
321 */
322 arena_chunk_t *spare;
323
324 /* Minimum ratio (log base 2) of nactive:ndirty. */
325 ssize_t lg_dirty_mult;
326
327 /* True if a thread is currently executing arena_purge(). */
328 bool purging;
329
330 /* Number of pages in active runs and huge regions. */
331 size_t nactive;
332
333 /*
334 * Current count of pages within unused runs that are potentially
335 * dirty, and for which madvise(... MADV_DONTNEED) has not been called.
336 * By tracking this, we can institute a limit on how much dirty unused
337 * memory is mapped for each arena.
338 */
339 size_t ndirty;
340
341 /*
342 * Size/address-ordered tree of this arena's available runs. The tree
343 * is used for first-best-fit run allocation.
344 */
345 arena_avail_tree_t runs_avail;
346
347 /*
348 * Unused dirty memory this arena manages. Dirty memory is conceptually
349 * tracked as an arbitrarily interleaved LRU of dirty runs and cached
350 * chunks, but the list linkage is actually semi-duplicated in order to
351 * avoid extra arena_chunk_map_misc_t space overhead.
352 *
353 * LRU-----------------------------------------------------------MRU
354 *
355 * /-- arena ---\
356 * | |
357 * | |
358 * |------------| /- chunk -\
359 * ...->|chunks_cache|<--------------------------->| /----\ |<--...
360 * |------------| | |node| |
361 * | | | | | |
362 * | | /- run -\ /- run -\ | | | |
363 * | | | | | | | | | |
364 * | | | | | | | | | |
365 * |------------| |-------| |-------| | |----| |
366 * ...->|runs_dirty |<-->|rd |<-->|rd |<---->|rd |<----...
367 * |------------| |-------| |-------| | |----| |
368 * | | | | | | | | | |
369 * | | | | | | | \----/ |
370 * | | \-------/ \-------/ | |
371 * | | | |
372 * | | | |
373 * \------------/ \---------/
374 */
375 arena_runs_dirty_link_t runs_dirty;
376 extent_node_t chunks_cache;
377
378 /* Extant huge allocations. */
379 ql_head(extent_node_t) huge;
380 /* Synchronizes all huge allocation/update/deallocation. */
381 malloc_mutex_t huge_mtx;
382
383 /*
384 * Trees of chunks that were previously allocated (trees differ only in
385 * node ordering). These are used when allocating chunks, in an attempt
386 * to re-use address space. Depending on function, different tree
387 * orderings are needed, which is why there are two trees with the same
388 * contents.
389 */
390 extent_tree_t chunks_szad_cached;
391 extent_tree_t chunks_ad_cached;
392 extent_tree_t chunks_szad_retained;
393 extent_tree_t chunks_ad_retained;
394
395 malloc_mutex_t chunks_mtx;
396 /* Cache of nodes that were allocated via base_alloc(). */
397 ql_head(extent_node_t) node_cache;
398 malloc_mutex_t node_cache_mtx;
399
400 /* User-configurable chunk hook functions. */
401 chunk_hooks_t chunk_hooks;
402
403 /* bins is used to store trees of free regions. */
404 arena_bin_t bins[NBINS];
405 };
406 #endif /* JEMALLOC_ARENA_STRUCTS_B */
407
408 #endif /* JEMALLOC_H_STRUCTS */
409 /******************************************************************************/
410 #ifdef JEMALLOC_H_EXTERNS
411
412 static const size_t large_pad =
413 #ifdef JEMALLOC_CACHE_OBLIVIOUS
414 PAGE
415 #else
416 0
417 #endif
418 ;
419
420 extern ssize_t opt_lg_dirty_mult;
421
422 extern arena_bin_info_t arena_bin_info[NBINS];
423
424 extern size_t map_bias; /* Number of arena chunk header pages. */
425 extern size_t map_misc_offset;
426 extern size_t arena_maxrun; /* Max run size for arenas. */
427 extern size_t large_maxclass; /* Max large size class. */
428 extern unsigned nlclasses; /* Number of large size classes. */
429 extern unsigned nhclasses; /* Number of huge size classes. */
430
431 void arena_chunk_cache_maybe_insert(arena_t *arena, extent_node_t *node,
432 bool cache);
433 void arena_chunk_cache_maybe_remove(arena_t *arena, extent_node_t *node,
434 bool cache);
435 extent_node_t *arena_node_alloc(arena_t *arena);
436 void arena_node_dalloc(arena_t *arena, extent_node_t *node);
437 void *arena_chunk_alloc_huge(arena_t *arena, size_t usize, size_t alignment,
438 bool *zero);
439 void arena_chunk_dalloc_huge(arena_t *arena, void *chunk, size_t usize);
440 void arena_chunk_ralloc_huge_similar(arena_t *arena, void *chunk,
441 size_t oldsize, size_t usize);
442 void arena_chunk_ralloc_huge_shrink(arena_t *arena, void *chunk,
443 size_t oldsize, size_t usize);
444 bool arena_chunk_ralloc_huge_expand(arena_t *arena, void *chunk,
445 size_t oldsize, size_t usize, bool *zero);
446 ssize_t arena_lg_dirty_mult_get(arena_t *arena);
447 bool arena_lg_dirty_mult_set(arena_t *arena, ssize_t lg_dirty_mult);
448 void arena_maybe_purge(arena_t *arena);
449 void arena_purge_all(arena_t *arena);
450 void arena_tcache_fill_small(arena_t *arena, tcache_bin_t *tbin,
451 szind_t binind, uint64_t prof_accumbytes);
452 void arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info,
453 bool zero);
454 #ifdef JEMALLOC_JET
455 typedef void (arena_redzone_corruption_t)(void *, size_t, bool, size_t,
456 uint8_t);
457 extern arena_redzone_corruption_t *arena_redzone_corruption;
458 typedef void (arena_dalloc_junk_small_t)(void *, arena_bin_info_t *);
459 extern arena_dalloc_junk_small_t *arena_dalloc_junk_small;
460 #else
461 void arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info);
462 #endif
463 void arena_quarantine_junk_small(void *ptr, size_t usize);
464 void *arena_malloc_small(arena_t *arena, size_t size, bool zero);
465 void *arena_malloc_large(arena_t *arena, size_t size, bool zero);
466 void *arena_palloc(tsd_t *tsd, arena_t *arena, size_t usize,
467 size_t alignment, bool zero, tcache_t *tcache);
468 void arena_prof_promoted(const void *ptr, size_t size);
469 void arena_dalloc_bin_junked_locked(arena_t *arena, arena_chunk_t *chunk,
470 void *ptr, arena_chunk_map_bits_t *bitselm);
471 void arena_dalloc_bin(arena_t *arena, arena_chunk_t *chunk, void *ptr,
472 size_t pageind, arena_chunk_map_bits_t *bitselm);
473 void arena_dalloc_small(arena_t *arena, arena_chunk_t *chunk, void *ptr,
474 size_t pageind);
475 #ifdef JEMALLOC_JET
476 typedef void (arena_dalloc_junk_large_t)(void *, size_t);
477 extern arena_dalloc_junk_large_t *arena_dalloc_junk_large;
478 #else
479 void arena_dalloc_junk_large(void *ptr, size_t usize);
480 #endif
481 void arena_dalloc_large_junked_locked(arena_t *arena, arena_chunk_t *chunk,
482 void *ptr);
483 void arena_dalloc_large(arena_t *arena, arena_chunk_t *chunk, void *ptr);
484 #ifdef JEMALLOC_JET
485 typedef void (arena_ralloc_junk_large_t)(void *, size_t, size_t);
486 extern arena_ralloc_junk_large_t *arena_ralloc_junk_large;
487 #endif
488 bool arena_ralloc_no_move(void *ptr, size_t oldsize, size_t size,
489 size_t extra, bool zero);
490 void *arena_ralloc(tsd_t *tsd, arena_t *arena, void *ptr, size_t oldsize,
491 size_t size, size_t alignment, bool zero, tcache_t *tcache);
492 dss_prec_t arena_dss_prec_get(arena_t *arena);
493 bool arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec);
494 ssize_t arena_lg_dirty_mult_default_get(void);
495 bool arena_lg_dirty_mult_default_set(ssize_t lg_dirty_mult);
496 void arena_stats_merge(arena_t *arena, const char **dss,
497 ssize_t *lg_dirty_mult, size_t *nactive, size_t *ndirty,
498 arena_stats_t *astats, malloc_bin_stats_t *bstats,
499 malloc_large_stats_t *lstats, malloc_huge_stats_t *hstats);
500 arena_t *arena_new(unsigned ind);
501 bool arena_boot(void);
502 void arena_prefork(arena_t *arena);
503 void arena_postfork_parent(arena_t *arena);
504 void arena_postfork_child(arena_t *arena);
505
506 #endif /* JEMALLOC_H_EXTERNS */
507 /******************************************************************************/
508 #ifdef JEMALLOC_H_INLINES
509
510 #ifndef JEMALLOC_ENABLE_INLINE
511 arena_chunk_map_bits_t *arena_bitselm_get(arena_chunk_t *chunk,
512 size_t pageind);
513 arena_chunk_map_misc_t *arena_miscelm_get(arena_chunk_t *chunk,
514 size_t pageind);
515 size_t arena_miscelm_to_pageind(arena_chunk_map_misc_t *miscelm);
516 void *arena_miscelm_to_rpages(arena_chunk_map_misc_t *miscelm);
517 arena_chunk_map_misc_t *arena_rd_to_miscelm(arena_runs_dirty_link_t *rd);
518 arena_chunk_map_misc_t *arena_run_to_miscelm(arena_run_t *run);
519 size_t *arena_mapbitsp_get(arena_chunk_t *chunk, size_t pageind);
520 size_t arena_mapbitsp_read(size_t *mapbitsp);
521 size_t arena_mapbits_get(arena_chunk_t *chunk, size_t pageind);
522 size_t arena_mapbits_size_decode(size_t mapbits);
523 size_t arena_mapbits_unallocated_size_get(arena_chunk_t *chunk,
524 size_t pageind);
525 size_t arena_mapbits_large_size_get(arena_chunk_t *chunk, size_t pageind);
526 size_t arena_mapbits_small_runind_get(arena_chunk_t *chunk, size_t pageind);
527 szind_t arena_mapbits_binind_get(arena_chunk_t *chunk, size_t pageind);
528 size_t arena_mapbits_dirty_get(arena_chunk_t *chunk, size_t pageind);
529 size_t arena_mapbits_unzeroed_get(arena_chunk_t *chunk, size_t pageind);
530 size_t arena_mapbits_decommitted_get(arena_chunk_t *chunk, size_t pageind);
531 size_t arena_mapbits_large_get(arena_chunk_t *chunk, size_t pageind);
532 size_t arena_mapbits_allocated_get(arena_chunk_t *chunk, size_t pageind);
533 void arena_mapbitsp_write(size_t *mapbitsp, size_t mapbits);
534 size_t arena_mapbits_size_encode(size_t size);
535 void arena_mapbits_unallocated_set(arena_chunk_t *chunk, size_t pageind,
536 size_t size, size_t flags);
537 void arena_mapbits_unallocated_size_set(arena_chunk_t *chunk, size_t pageind,
538 size_t size);
539 void arena_mapbits_internal_set(arena_chunk_t *chunk, size_t pageind,
540 size_t flags);
541 void arena_mapbits_large_set(arena_chunk_t *chunk, size_t pageind,
542 size_t size, size_t flags);
543 void arena_mapbits_large_binind_set(arena_chunk_t *chunk, size_t pageind,
544 szind_t binind);
545 void arena_mapbits_small_set(arena_chunk_t *chunk, size_t pageind,
546 size_t runind, szind_t binind, size_t flags);
547 void arena_metadata_allocated_add(arena_t *arena, size_t size);
548 void arena_metadata_allocated_sub(arena_t *arena, size_t size);
549 size_t arena_metadata_allocated_get(arena_t *arena);
550 bool arena_prof_accum_impl(arena_t *arena, uint64_t accumbytes);
551 bool arena_prof_accum_locked(arena_t *arena, uint64_t accumbytes);
552 bool arena_prof_accum(arena_t *arena, uint64_t accumbytes);
553 szind_t arena_ptr_small_binind_get(const void *ptr, size_t mapbits);
554 szind_t arena_bin_index(arena_t *arena, arena_bin_t *bin);
555 unsigned arena_run_regind(arena_run_t *run, arena_bin_info_t *bin_info,
556 const void *ptr);
557 prof_tctx_t *arena_prof_tctx_get(const void *ptr);
558 void arena_prof_tctx_set(const void *ptr, size_t usize, prof_tctx_t *tctx);
559 void arena_prof_tctx_reset(const void *ptr, size_t usize,
560 const void *old_ptr, prof_tctx_t *old_tctx);
561 void *arena_malloc(tsd_t *tsd, arena_t *arena, size_t size, bool zero,
562 tcache_t *tcache);
563 arena_t *arena_aalloc(const void *ptr);
564 size_t arena_salloc(const void *ptr, bool demote);
565 void arena_dalloc(tsd_t *tsd, void *ptr, tcache_t *tcache);
566 void arena_sdalloc(tsd_t *tsd, void *ptr, size_t size, tcache_t *tcache);
567 #endif
568
569 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_ARENA_C_))
570 # ifdef JEMALLOC_ARENA_INLINE_A
571 JEMALLOC_ALWAYS_INLINE arena_chunk_map_bits_t *
arena_bitselm_get(arena_chunk_t * chunk,size_t pageind)572 arena_bitselm_get(arena_chunk_t *chunk, size_t pageind)
573 {
574
575 assert(pageind >= map_bias);
576 assert(pageind < chunk_npages);
577
578 return (&chunk->map_bits[pageind-map_bias]);
579 }
580
581 JEMALLOC_ALWAYS_INLINE arena_chunk_map_misc_t *
arena_miscelm_get(arena_chunk_t * chunk,size_t pageind)582 arena_miscelm_get(arena_chunk_t *chunk, size_t pageind)
583 {
584
585 assert(pageind >= map_bias);
586 assert(pageind < chunk_npages);
587
588 return ((arena_chunk_map_misc_t *)((uintptr_t)chunk +
589 (uintptr_t)map_misc_offset) + pageind-map_bias);
590 }
591
592 JEMALLOC_ALWAYS_INLINE size_t
arena_miscelm_to_pageind(arena_chunk_map_misc_t * miscelm)593 arena_miscelm_to_pageind(arena_chunk_map_misc_t *miscelm)
594 {
595 arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(miscelm);
596 size_t pageind = ((uintptr_t)miscelm - ((uintptr_t)chunk +
597 map_misc_offset)) / sizeof(arena_chunk_map_misc_t) + map_bias;
598
599 assert(pageind >= map_bias);
600 assert(pageind < chunk_npages);
601
602 return (pageind);
603 }
604
605 JEMALLOC_ALWAYS_INLINE void *
arena_miscelm_to_rpages(arena_chunk_map_misc_t * miscelm)606 arena_miscelm_to_rpages(arena_chunk_map_misc_t *miscelm)
607 {
608 arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(miscelm);
609 size_t pageind = arena_miscelm_to_pageind(miscelm);
610
611 return ((void *)((uintptr_t)chunk + (pageind << LG_PAGE)));
612 }
613
614 JEMALLOC_ALWAYS_INLINE arena_chunk_map_misc_t *
arena_rd_to_miscelm(arena_runs_dirty_link_t * rd)615 arena_rd_to_miscelm(arena_runs_dirty_link_t *rd)
616 {
617 arena_chunk_map_misc_t *miscelm = (arena_chunk_map_misc_t
618 *)((uintptr_t)rd - offsetof(arena_chunk_map_misc_t, rd));
619
620 assert(arena_miscelm_to_pageind(miscelm) >= map_bias);
621 assert(arena_miscelm_to_pageind(miscelm) < chunk_npages);
622
623 return (miscelm);
624 }
625
626 JEMALLOC_ALWAYS_INLINE arena_chunk_map_misc_t *
arena_run_to_miscelm(arena_run_t * run)627 arena_run_to_miscelm(arena_run_t *run)
628 {
629 arena_chunk_map_misc_t *miscelm = (arena_chunk_map_misc_t
630 *)((uintptr_t)run - offsetof(arena_chunk_map_misc_t, run));
631
632 assert(arena_miscelm_to_pageind(miscelm) >= map_bias);
633 assert(arena_miscelm_to_pageind(miscelm) < chunk_npages);
634
635 return (miscelm);
636 }
637
638 JEMALLOC_ALWAYS_INLINE size_t *
arena_mapbitsp_get(arena_chunk_t * chunk,size_t pageind)639 arena_mapbitsp_get(arena_chunk_t *chunk, size_t pageind)
640 {
641
642 return (&arena_bitselm_get(chunk, pageind)->bits);
643 }
644
645 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbitsp_read(size_t * mapbitsp)646 arena_mapbitsp_read(size_t *mapbitsp)
647 {
648
649 return (*mapbitsp);
650 }
651
652 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_get(arena_chunk_t * chunk,size_t pageind)653 arena_mapbits_get(arena_chunk_t *chunk, size_t pageind)
654 {
655
656 return (arena_mapbitsp_read(arena_mapbitsp_get(chunk, pageind)));
657 }
658
659 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_size_decode(size_t mapbits)660 arena_mapbits_size_decode(size_t mapbits)
661 {
662 size_t size;
663
664 #if CHUNK_MAP_SIZE_SHIFT > 0
665 size = (mapbits & CHUNK_MAP_SIZE_MASK) >> CHUNK_MAP_SIZE_SHIFT;
666 #elif CHUNK_MAP_SIZE_SHIFT == 0
667 size = mapbits & CHUNK_MAP_SIZE_MASK;
668 #else
669 size = (mapbits & CHUNK_MAP_SIZE_MASK) << -CHUNK_MAP_SIZE_SHIFT;
670 #endif
671
672 return (size);
673 }
674
675 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_unallocated_size_get(arena_chunk_t * chunk,size_t pageind)676 arena_mapbits_unallocated_size_get(arena_chunk_t *chunk, size_t pageind)
677 {
678 size_t mapbits;
679
680 mapbits = arena_mapbits_get(chunk, pageind);
681 assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == 0);
682 return (arena_mapbits_size_decode(mapbits));
683 }
684
685 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_large_size_get(arena_chunk_t * chunk,size_t pageind)686 arena_mapbits_large_size_get(arena_chunk_t *chunk, size_t pageind)
687 {
688 size_t mapbits;
689
690 mapbits = arena_mapbits_get(chunk, pageind);
691 assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) ==
692 (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED));
693 return (arena_mapbits_size_decode(mapbits));
694 }
695
696 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_small_runind_get(arena_chunk_t * chunk,size_t pageind)697 arena_mapbits_small_runind_get(arena_chunk_t *chunk, size_t pageind)
698 {
699 size_t mapbits;
700
701 mapbits = arena_mapbits_get(chunk, pageind);
702 assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) ==
703 CHUNK_MAP_ALLOCATED);
704 return (mapbits >> CHUNK_MAP_RUNIND_SHIFT);
705 }
706
707 JEMALLOC_ALWAYS_INLINE szind_t
arena_mapbits_binind_get(arena_chunk_t * chunk,size_t pageind)708 arena_mapbits_binind_get(arena_chunk_t *chunk, size_t pageind)
709 {
710 size_t mapbits;
711 szind_t binind;
712
713 mapbits = arena_mapbits_get(chunk, pageind);
714 binind = (mapbits & CHUNK_MAP_BININD_MASK) >> CHUNK_MAP_BININD_SHIFT;
715 assert(binind < NBINS || binind == BININD_INVALID);
716 return (binind);
717 }
718
719 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_dirty_get(arena_chunk_t * chunk,size_t pageind)720 arena_mapbits_dirty_get(arena_chunk_t *chunk, size_t pageind)
721 {
722 size_t mapbits;
723
724 mapbits = arena_mapbits_get(chunk, pageind);
725 assert((mapbits & CHUNK_MAP_DECOMMITTED) == 0 || (mapbits &
726 (CHUNK_MAP_DIRTY|CHUNK_MAP_UNZEROED)) == 0);
727 return (mapbits & CHUNK_MAP_DIRTY);
728 }
729
730 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_unzeroed_get(arena_chunk_t * chunk,size_t pageind)731 arena_mapbits_unzeroed_get(arena_chunk_t *chunk, size_t pageind)
732 {
733 size_t mapbits;
734
735 mapbits = arena_mapbits_get(chunk, pageind);
736 assert((mapbits & CHUNK_MAP_DECOMMITTED) == 0 || (mapbits &
737 (CHUNK_MAP_DIRTY|CHUNK_MAP_UNZEROED)) == 0);
738 return (mapbits & CHUNK_MAP_UNZEROED);
739 }
740
741 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_decommitted_get(arena_chunk_t * chunk,size_t pageind)742 arena_mapbits_decommitted_get(arena_chunk_t *chunk, size_t pageind)
743 {
744 size_t mapbits;
745
746 mapbits = arena_mapbits_get(chunk, pageind);
747 assert((mapbits & CHUNK_MAP_DECOMMITTED) == 0 || (mapbits &
748 (CHUNK_MAP_DIRTY|CHUNK_MAP_UNZEROED)) == 0);
749 return (mapbits & CHUNK_MAP_DECOMMITTED);
750 }
751
752 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_large_get(arena_chunk_t * chunk,size_t pageind)753 arena_mapbits_large_get(arena_chunk_t *chunk, size_t pageind)
754 {
755 size_t mapbits;
756
757 mapbits = arena_mapbits_get(chunk, pageind);
758 return (mapbits & CHUNK_MAP_LARGE);
759 }
760
761 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_allocated_get(arena_chunk_t * chunk,size_t pageind)762 arena_mapbits_allocated_get(arena_chunk_t *chunk, size_t pageind)
763 {
764 size_t mapbits;
765
766 mapbits = arena_mapbits_get(chunk, pageind);
767 return (mapbits & CHUNK_MAP_ALLOCATED);
768 }
769
770 JEMALLOC_ALWAYS_INLINE void
arena_mapbitsp_write(size_t * mapbitsp,size_t mapbits)771 arena_mapbitsp_write(size_t *mapbitsp, size_t mapbits)
772 {
773
774 *mapbitsp = mapbits;
775 }
776
777 JEMALLOC_ALWAYS_INLINE size_t
arena_mapbits_size_encode(size_t size)778 arena_mapbits_size_encode(size_t size)
779 {
780 size_t mapbits;
781
782 #if CHUNK_MAP_SIZE_SHIFT > 0
783 mapbits = size << CHUNK_MAP_SIZE_SHIFT;
784 #elif CHUNK_MAP_SIZE_SHIFT == 0
785 mapbits = size;
786 #else
787 mapbits = size >> -CHUNK_MAP_SIZE_SHIFT;
788 #endif
789
790 assert((mapbits & ~CHUNK_MAP_SIZE_MASK) == 0);
791 return (mapbits);
792 }
793
794 JEMALLOC_ALWAYS_INLINE void
arena_mapbits_unallocated_set(arena_chunk_t * chunk,size_t pageind,size_t size,size_t flags)795 arena_mapbits_unallocated_set(arena_chunk_t *chunk, size_t pageind, size_t size,
796 size_t flags)
797 {
798 size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
799
800 assert((size & PAGE_MASK) == 0);
801 assert((flags & CHUNK_MAP_FLAGS_MASK) == flags);
802 assert((flags & CHUNK_MAP_DECOMMITTED) == 0 || (flags &
803 (CHUNK_MAP_DIRTY|CHUNK_MAP_UNZEROED)) == 0);
804 arena_mapbitsp_write(mapbitsp, arena_mapbits_size_encode(size) |
805 CHUNK_MAP_BININD_INVALID | flags);
806 }
807
808 JEMALLOC_ALWAYS_INLINE void
arena_mapbits_unallocated_size_set(arena_chunk_t * chunk,size_t pageind,size_t size)809 arena_mapbits_unallocated_size_set(arena_chunk_t *chunk, size_t pageind,
810 size_t size)
811 {
812 size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
813 size_t mapbits = arena_mapbitsp_read(mapbitsp);
814
815 assert((size & PAGE_MASK) == 0);
816 assert((mapbits & (CHUNK_MAP_LARGE|CHUNK_MAP_ALLOCATED)) == 0);
817 arena_mapbitsp_write(mapbitsp, arena_mapbits_size_encode(size) |
818 (mapbits & ~CHUNK_MAP_SIZE_MASK));
819 }
820
821 JEMALLOC_ALWAYS_INLINE void
arena_mapbits_internal_set(arena_chunk_t * chunk,size_t pageind,size_t flags)822 arena_mapbits_internal_set(arena_chunk_t *chunk, size_t pageind, size_t flags)
823 {
824 size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
825
826 assert((flags & CHUNK_MAP_UNZEROED) == flags);
827 arena_mapbitsp_write(mapbitsp, flags);
828 }
829
830 JEMALLOC_ALWAYS_INLINE void
arena_mapbits_large_set(arena_chunk_t * chunk,size_t pageind,size_t size,size_t flags)831 arena_mapbits_large_set(arena_chunk_t *chunk, size_t pageind, size_t size,
832 size_t flags)
833 {
834 size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
835
836 assert((size & PAGE_MASK) == 0);
837 assert((flags & CHUNK_MAP_FLAGS_MASK) == flags);
838 assert((flags & CHUNK_MAP_DECOMMITTED) == 0 || (flags &
839 (CHUNK_MAP_DIRTY|CHUNK_MAP_UNZEROED)) == 0);
840 arena_mapbitsp_write(mapbitsp, arena_mapbits_size_encode(size) |
841 CHUNK_MAP_BININD_INVALID | flags | CHUNK_MAP_LARGE |
842 CHUNK_MAP_ALLOCATED);
843 }
844
845 JEMALLOC_ALWAYS_INLINE void
arena_mapbits_large_binind_set(arena_chunk_t * chunk,size_t pageind,szind_t binind)846 arena_mapbits_large_binind_set(arena_chunk_t *chunk, size_t pageind,
847 szind_t binind)
848 {
849 size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
850 size_t mapbits = arena_mapbitsp_read(mapbitsp);
851
852 assert(binind <= BININD_INVALID);
853 assert(arena_mapbits_large_size_get(chunk, pageind) == LARGE_MINCLASS +
854 large_pad);
855 arena_mapbitsp_write(mapbitsp, (mapbits & ~CHUNK_MAP_BININD_MASK) |
856 (binind << CHUNK_MAP_BININD_SHIFT));
857 }
858
859 JEMALLOC_ALWAYS_INLINE void
arena_mapbits_small_set(arena_chunk_t * chunk,size_t pageind,size_t runind,szind_t binind,size_t flags)860 arena_mapbits_small_set(arena_chunk_t *chunk, size_t pageind, size_t runind,
861 szind_t binind, size_t flags)
862 {
863 size_t *mapbitsp = arena_mapbitsp_get(chunk, pageind);
864
865 assert(binind < BININD_INVALID);
866 assert(pageind - runind >= map_bias);
867 assert((flags & CHUNK_MAP_UNZEROED) == flags);
868 arena_mapbitsp_write(mapbitsp, (runind << CHUNK_MAP_RUNIND_SHIFT) |
869 (binind << CHUNK_MAP_BININD_SHIFT) | flags | CHUNK_MAP_ALLOCATED);
870 }
871
872 JEMALLOC_INLINE void
arena_metadata_allocated_add(arena_t * arena,size_t size)873 arena_metadata_allocated_add(arena_t *arena, size_t size)
874 {
875
876 atomic_add_z(&arena->stats.metadata_allocated, size);
877 }
878
879 JEMALLOC_INLINE void
arena_metadata_allocated_sub(arena_t * arena,size_t size)880 arena_metadata_allocated_sub(arena_t *arena, size_t size)
881 {
882
883 atomic_sub_z(&arena->stats.metadata_allocated, size);
884 }
885
886 JEMALLOC_INLINE size_t
arena_metadata_allocated_get(arena_t * arena)887 arena_metadata_allocated_get(arena_t *arena)
888 {
889
890 return (atomic_read_z(&arena->stats.metadata_allocated));
891 }
892
893 JEMALLOC_INLINE bool
arena_prof_accum_impl(arena_t * arena,uint64_t accumbytes)894 arena_prof_accum_impl(arena_t *arena, uint64_t accumbytes)
895 {
896
897 cassert(config_prof);
898 assert(prof_interval != 0);
899
900 arena->prof_accumbytes += accumbytes;
901 if (arena->prof_accumbytes >= prof_interval) {
902 arena->prof_accumbytes -= prof_interval;
903 return (true);
904 }
905 return (false);
906 }
907
908 JEMALLOC_INLINE bool
arena_prof_accum_locked(arena_t * arena,uint64_t accumbytes)909 arena_prof_accum_locked(arena_t *arena, uint64_t accumbytes)
910 {
911
912 cassert(config_prof);
913
914 if (likely(prof_interval == 0))
915 return (false);
916 return (arena_prof_accum_impl(arena, accumbytes));
917 }
918
919 JEMALLOC_INLINE bool
arena_prof_accum(arena_t * arena,uint64_t accumbytes)920 arena_prof_accum(arena_t *arena, uint64_t accumbytes)
921 {
922
923 cassert(config_prof);
924
925 if (likely(prof_interval == 0))
926 return (false);
927
928 {
929 bool ret;
930
931 malloc_mutex_lock(&arena->lock);
932 ret = arena_prof_accum_impl(arena, accumbytes);
933 malloc_mutex_unlock(&arena->lock);
934 return (ret);
935 }
936 }
937
938 JEMALLOC_ALWAYS_INLINE szind_t
arena_ptr_small_binind_get(const void * ptr,size_t mapbits)939 arena_ptr_small_binind_get(const void *ptr, size_t mapbits)
940 {
941 szind_t binind;
942
943 binind = (mapbits & CHUNK_MAP_BININD_MASK) >> CHUNK_MAP_BININD_SHIFT;
944
945 if (config_debug) {
946 arena_chunk_t *chunk;
947 arena_t *arena;
948 size_t pageind;
949 size_t actual_mapbits;
950 size_t rpages_ind;
951 arena_run_t *run;
952 arena_bin_t *bin;
953 szind_t run_binind, actual_binind;
954 arena_bin_info_t *bin_info;
955 arena_chunk_map_misc_t *miscelm;
956 void *rpages;
957
958 assert(binind != BININD_INVALID);
959 assert(binind < NBINS);
960 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
961 arena = extent_node_arena_get(&chunk->node);
962 pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
963 actual_mapbits = arena_mapbits_get(chunk, pageind);
964 assert(mapbits == actual_mapbits);
965 assert(arena_mapbits_large_get(chunk, pageind) == 0);
966 assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
967 rpages_ind = pageind - arena_mapbits_small_runind_get(chunk,
968 pageind);
969 miscelm = arena_miscelm_get(chunk, rpages_ind);
970 run = &miscelm->run;
971 run_binind = run->binind;
972 bin = &arena->bins[run_binind];
973 actual_binind = bin - arena->bins;
974 assert(run_binind == actual_binind);
975 bin_info = &arena_bin_info[actual_binind];
976 rpages = arena_miscelm_to_rpages(miscelm);
977 assert(((uintptr_t)ptr - ((uintptr_t)rpages +
978 (uintptr_t)bin_info->reg0_offset)) % bin_info->reg_interval
979 == 0);
980 }
981
982 return (binind);
983 }
984 # endif /* JEMALLOC_ARENA_INLINE_A */
985
986 # ifdef JEMALLOC_ARENA_INLINE_B
987 JEMALLOC_INLINE szind_t
arena_bin_index(arena_t * arena,arena_bin_t * bin)988 arena_bin_index(arena_t *arena, arena_bin_t *bin)
989 {
990 szind_t binind = bin - arena->bins;
991 assert(binind < NBINS);
992 return (binind);
993 }
994
995 JEMALLOC_INLINE unsigned
arena_run_regind(arena_run_t * run,arena_bin_info_t * bin_info,const void * ptr)996 arena_run_regind(arena_run_t *run, arena_bin_info_t *bin_info, const void *ptr)
997 {
998 unsigned shift, diff, regind;
999 size_t interval;
1000 arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run);
1001 void *rpages = arena_miscelm_to_rpages(miscelm);
1002
1003 /*
1004 * Freeing a pointer lower than region zero can cause assertion
1005 * failure.
1006 */
1007 assert((uintptr_t)ptr >= (uintptr_t)rpages +
1008 (uintptr_t)bin_info->reg0_offset);
1009
1010 /*
1011 * Avoid doing division with a variable divisor if possible. Using
1012 * actual division here can reduce allocator throughput by over 20%!
1013 */
1014 diff = (unsigned)((uintptr_t)ptr - (uintptr_t)rpages -
1015 bin_info->reg0_offset);
1016
1017 /* Rescale (factor powers of 2 out of the numerator and denominator). */
1018 interval = bin_info->reg_interval;
1019 shift = jemalloc_ffs(interval) - 1;
1020 diff >>= shift;
1021 interval >>= shift;
1022
1023 if (interval == 1) {
1024 /* The divisor was a power of 2. */
1025 regind = diff;
1026 } else {
1027 /*
1028 * To divide by a number D that is not a power of two we
1029 * multiply by (2^21 / D) and then right shift by 21 positions.
1030 *
1031 * X / D
1032 *
1033 * becomes
1034 *
1035 * (X * interval_invs[D - 3]) >> SIZE_INV_SHIFT
1036 *
1037 * We can omit the first three elements, because we never
1038 * divide by 0, and 1 and 2 are both powers of two, which are
1039 * handled above.
1040 */
1041 #define SIZE_INV_SHIFT ((sizeof(unsigned) << 3) - LG_RUN_MAXREGS)
1042 #define SIZE_INV(s) (((1U << SIZE_INV_SHIFT) / (s)) + 1)
1043 static const unsigned interval_invs[] = {
1044 SIZE_INV(3),
1045 SIZE_INV(4), SIZE_INV(5), SIZE_INV(6), SIZE_INV(7),
1046 SIZE_INV(8), SIZE_INV(9), SIZE_INV(10), SIZE_INV(11),
1047 SIZE_INV(12), SIZE_INV(13), SIZE_INV(14), SIZE_INV(15),
1048 SIZE_INV(16), SIZE_INV(17), SIZE_INV(18), SIZE_INV(19),
1049 SIZE_INV(20), SIZE_INV(21), SIZE_INV(22), SIZE_INV(23),
1050 SIZE_INV(24), SIZE_INV(25), SIZE_INV(26), SIZE_INV(27),
1051 SIZE_INV(28), SIZE_INV(29), SIZE_INV(30), SIZE_INV(31)
1052 };
1053
1054 if (likely(interval <= ((sizeof(interval_invs) /
1055 sizeof(unsigned)) + 2))) {
1056 regind = (diff * interval_invs[interval - 3]) >>
1057 SIZE_INV_SHIFT;
1058 } else
1059 regind = diff / interval;
1060 #undef SIZE_INV
1061 #undef SIZE_INV_SHIFT
1062 }
1063 assert(diff == regind * interval);
1064 assert(regind < bin_info->nregs);
1065
1066 return (regind);
1067 }
1068
1069 JEMALLOC_INLINE prof_tctx_t *
arena_prof_tctx_get(const void * ptr)1070 arena_prof_tctx_get(const void *ptr)
1071 {
1072 prof_tctx_t *ret;
1073 arena_chunk_t *chunk;
1074
1075 cassert(config_prof);
1076 assert(ptr != NULL);
1077
1078 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1079 if (likely(chunk != ptr)) {
1080 size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
1081 size_t mapbits = arena_mapbits_get(chunk, pageind);
1082 assert((mapbits & CHUNK_MAP_ALLOCATED) != 0);
1083 if (likely((mapbits & CHUNK_MAP_LARGE) == 0))
1084 ret = (prof_tctx_t *)(uintptr_t)1U;
1085 else {
1086 arena_chunk_map_misc_t *elm = arena_miscelm_get(chunk,
1087 pageind);
1088 ret = atomic_read_p(&elm->prof_tctx_pun);
1089 }
1090 } else
1091 ret = huge_prof_tctx_get(ptr);
1092
1093 return (ret);
1094 }
1095
1096 JEMALLOC_INLINE void
arena_prof_tctx_set(const void * ptr,size_t usize,prof_tctx_t * tctx)1097 arena_prof_tctx_set(const void *ptr, size_t usize, prof_tctx_t *tctx)
1098 {
1099 arena_chunk_t *chunk;
1100
1101 cassert(config_prof);
1102 assert(ptr != NULL);
1103
1104 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1105 if (likely(chunk != ptr)) {
1106 size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
1107
1108 assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
1109
1110 if (unlikely(usize > SMALL_MAXCLASS || (uintptr_t)tctx >
1111 (uintptr_t)1U)) {
1112 arena_chunk_map_misc_t *elm;
1113
1114 assert(arena_mapbits_large_get(chunk, pageind) != 0);
1115
1116 elm = arena_miscelm_get(chunk, pageind);
1117 atomic_write_p(&elm->prof_tctx_pun, tctx);
1118 } else {
1119 /*
1120 * tctx must always be initialized for large runs.
1121 * Assert that the surrounding conditional logic is
1122 * equivalent to checking whether ptr refers to a large
1123 * run.
1124 */
1125 assert(arena_mapbits_large_get(chunk, pageind) == 0);
1126 }
1127 } else
1128 huge_prof_tctx_set(ptr, tctx);
1129 }
1130
1131 JEMALLOC_INLINE void
arena_prof_tctx_reset(const void * ptr,size_t usize,const void * old_ptr,prof_tctx_t * old_tctx)1132 arena_prof_tctx_reset(const void *ptr, size_t usize, const void *old_ptr,
1133 prof_tctx_t *old_tctx)
1134 {
1135
1136 cassert(config_prof);
1137 assert(ptr != NULL);
1138
1139 if (unlikely(usize > SMALL_MAXCLASS || (ptr == old_ptr &&
1140 (uintptr_t)old_tctx > (uintptr_t)1U))) {
1141 arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1142 if (likely(chunk != ptr)) {
1143 size_t pageind;
1144 arena_chunk_map_misc_t *elm;
1145
1146 pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >>
1147 LG_PAGE;
1148 assert(arena_mapbits_allocated_get(chunk, pageind) !=
1149 0);
1150 assert(arena_mapbits_large_get(chunk, pageind) != 0);
1151
1152 elm = arena_miscelm_get(chunk, pageind);
1153 atomic_write_p(&elm->prof_tctx_pun,
1154 (prof_tctx_t *)(uintptr_t)1U);
1155 } else
1156 huge_prof_tctx_reset(ptr);
1157 }
1158 }
1159
1160 JEMALLOC_ALWAYS_INLINE void *
arena_malloc(tsd_t * tsd,arena_t * arena,size_t size,bool zero,tcache_t * tcache)1161 arena_malloc(tsd_t *tsd, arena_t *arena, size_t size, bool zero,
1162 tcache_t *tcache)
1163 {
1164
1165 assert(size != 0);
1166
1167 arena = arena_choose(tsd, arena);
1168 if (unlikely(arena == NULL))
1169 return (NULL);
1170
1171 if (likely(size <= SMALL_MAXCLASS)) {
1172 if (likely(tcache != NULL)) {
1173 return (tcache_alloc_small(tsd, arena, tcache, size,
1174 zero));
1175 } else
1176 return (arena_malloc_small(arena, size, zero));
1177 } else if (likely(size <= large_maxclass)) {
1178 /*
1179 * Initialize tcache after checking size in order to avoid
1180 * infinite recursion during tcache initialization.
1181 */
1182 if (likely(tcache != NULL) && size <= tcache_maxclass) {
1183 return (tcache_alloc_large(tsd, arena, tcache, size,
1184 zero));
1185 } else
1186 return (arena_malloc_large(arena, size, zero));
1187 } else
1188 return (huge_malloc(tsd, arena, size, zero, tcache));
1189 }
1190
1191 JEMALLOC_ALWAYS_INLINE arena_t *
arena_aalloc(const void * ptr)1192 arena_aalloc(const void *ptr)
1193 {
1194 arena_chunk_t *chunk;
1195
1196 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1197 if (likely(chunk != ptr))
1198 return (extent_node_arena_get(&chunk->node));
1199 else
1200 return (huge_aalloc(ptr));
1201 }
1202
1203 /* Return the size of the allocation pointed to by ptr. */
1204 JEMALLOC_ALWAYS_INLINE size_t
arena_salloc(const void * ptr,bool demote)1205 arena_salloc(const void *ptr, bool demote)
1206 {
1207 size_t ret;
1208 arena_chunk_t *chunk;
1209 size_t pageind;
1210 szind_t binind;
1211
1212 assert(ptr != NULL);
1213
1214 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1215 if (likely(chunk != ptr)) {
1216 pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
1217 assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
1218 binind = arena_mapbits_binind_get(chunk, pageind);
1219 if (unlikely(binind == BININD_INVALID || (config_prof && !demote
1220 && arena_mapbits_large_get(chunk, pageind) != 0))) {
1221 /*
1222 * Large allocation. In the common case (demote), and
1223 * as this is an inline function, most callers will only
1224 * end up looking at binind to determine that ptr is a
1225 * small allocation.
1226 */
1227 assert(config_cache_oblivious || ((uintptr_t)ptr &
1228 PAGE_MASK) == 0);
1229 ret = arena_mapbits_large_size_get(chunk, pageind) -
1230 large_pad;
1231 assert(ret != 0);
1232 assert(pageind + ((ret+large_pad)>>LG_PAGE) <=
1233 chunk_npages);
1234 assert(arena_mapbits_dirty_get(chunk, pageind) ==
1235 arena_mapbits_dirty_get(chunk,
1236 pageind+((ret+large_pad)>>LG_PAGE)-1));
1237 } else {
1238 /*
1239 * Small allocation (possibly promoted to a large
1240 * object).
1241 */
1242 assert(arena_mapbits_large_get(chunk, pageind) != 0 ||
1243 arena_ptr_small_binind_get(ptr,
1244 arena_mapbits_get(chunk, pageind)) == binind);
1245 ret = index2size(binind);
1246 }
1247 } else
1248 ret = huge_salloc(ptr);
1249
1250 return (ret);
1251 }
1252
1253 JEMALLOC_ALWAYS_INLINE void
arena_dalloc(tsd_t * tsd,void * ptr,tcache_t * tcache)1254 arena_dalloc(tsd_t *tsd, void *ptr, tcache_t *tcache)
1255 {
1256 arena_chunk_t *chunk;
1257 size_t pageind, mapbits;
1258
1259 assert(ptr != NULL);
1260
1261 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1262 if (likely(chunk != ptr)) {
1263 pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE;
1264 mapbits = arena_mapbits_get(chunk, pageind);
1265 assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
1266 if (likely((mapbits & CHUNK_MAP_LARGE) == 0)) {
1267 /* Small allocation. */
1268 if (likely(tcache != NULL)) {
1269 szind_t binind = arena_ptr_small_binind_get(ptr,
1270 mapbits);
1271 tcache_dalloc_small(tsd, tcache, ptr, binind);
1272 } else {
1273 arena_dalloc_small(extent_node_arena_get(
1274 &chunk->node), chunk, ptr, pageind);
1275 }
1276 } else {
1277 size_t size = arena_mapbits_large_size_get(chunk,
1278 pageind);
1279
1280 assert(config_cache_oblivious || ((uintptr_t)ptr &
1281 PAGE_MASK) == 0);
1282
1283 if (likely(tcache != NULL) && size - large_pad <=
1284 tcache_maxclass) {
1285 tcache_dalloc_large(tsd, tcache, ptr, size -
1286 large_pad);
1287 } else {
1288 arena_dalloc_large(extent_node_arena_get(
1289 &chunk->node), chunk, ptr);
1290 }
1291 }
1292 } else
1293 huge_dalloc(tsd, ptr, tcache);
1294 }
1295
1296 JEMALLOC_ALWAYS_INLINE void
arena_sdalloc(tsd_t * tsd,void * ptr,size_t size,tcache_t * tcache)1297 arena_sdalloc(tsd_t *tsd, void *ptr, size_t size, tcache_t *tcache)
1298 {
1299 arena_chunk_t *chunk;
1300
1301 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
1302 if (likely(chunk != ptr)) {
1303 if (config_prof && opt_prof) {
1304 size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >>
1305 LG_PAGE;
1306 assert(arena_mapbits_allocated_get(chunk, pageind) != 0);
1307 if (arena_mapbits_large_get(chunk, pageind) != 0) {
1308 /*
1309 * Make sure to use promoted size, not request
1310 * size.
1311 */
1312 size = arena_mapbits_large_size_get(chunk,
1313 pageind) - large_pad;
1314 }
1315 }
1316 assert(s2u(size) == s2u(arena_salloc(ptr, false)));
1317
1318 if (likely(size <= SMALL_MAXCLASS)) {
1319 /* Small allocation. */
1320 if (likely(tcache != NULL)) {
1321 szind_t binind = size2index(size);
1322 tcache_dalloc_small(tsd, tcache, ptr, binind);
1323 } else {
1324 size_t pageind = ((uintptr_t)ptr -
1325 (uintptr_t)chunk) >> LG_PAGE;
1326 arena_dalloc_small(extent_node_arena_get(
1327 &chunk->node), chunk, ptr, pageind);
1328 }
1329 } else {
1330 assert(config_cache_oblivious || ((uintptr_t)ptr &
1331 PAGE_MASK) == 0);
1332
1333 if (likely(tcache != NULL) && size <= tcache_maxclass)
1334 tcache_dalloc_large(tsd, tcache, ptr, size);
1335 else {
1336 arena_dalloc_large(extent_node_arena_get(
1337 &chunk->node), chunk, ptr);
1338 }
1339 }
1340 } else
1341 huge_dalloc(tsd, ptr, tcache);
1342 }
1343 # endif /* JEMALLOC_ARENA_INLINE_B */
1344 #endif
1345
1346 #endif /* JEMALLOC_H_INLINES */
1347 /******************************************************************************/
1348