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