memapi-jemalloc-sys 0.1.2+5.3.0-298-g70d4102f48dce2d5755e9139a15eeec606f97bff

FFI bindings to Jemalloc made for memapi.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
#include "jemalloc/internal/jemalloc_preamble.h"
#include "jemalloc/internal/jemalloc_internal_includes.h"

#include "jemalloc/internal/assert.h"
#include "jemalloc/internal/base.h"
#include "jemalloc/internal/mutex.h"
#include "jemalloc/internal/safety_check.h"
#include "jemalloc/internal/san.h"
#include "jemalloc/internal/sc.h"

/******************************************************************************/
/* Data. */

bool opt_tcache = true;

/* global_do_not_change_tcache_maxclass is set to 32KB by default. */
size_t opt_tcache_max = ((size_t)1) << 15;

/* Reasonable defaults for min and max values. */
unsigned opt_tcache_nslots_small_min = 20;
unsigned opt_tcache_nslots_small_max = 200;
unsigned opt_tcache_nslots_large = 20;

/*
 * We attempt to make the number of slots in a tcache bin for a given size class
 * equal to the number of objects in a slab times some multiplier.  By default,
 * the multiplier is 2 (i.e. we set the maximum number of objects in the tcache
 * to twice the number of objects in a slab).
 * This is bounded by some other constraints as well, like the fact that it
 * must be even, must be less than opt_tcache_nslots_small_max, etc..
 */
ssize_t	opt_lg_tcache_nslots_mul = 1;

/*
 * Number of allocation bytes between tcache incremental GCs.  Again, this
 * default just seems to work well; more tuning is possible.
 */
size_t opt_tcache_gc_incr_bytes = 65536;

/*
 * With default settings, we may end up flushing small bins frequently with
 * small flush amounts.  To limit this tendency, we can set a number of bytes to
 * "delay" by.  If we try to flush N M-byte items, we decrease that size-class's
 * delay by N * M.  So, if delay is 1024 and we're looking at the 64-byte size
 * class, we won't do any flushing until we've been asked to flush 1024/64 == 16
 * items.  This can happen in any configuration (i.e. being asked to flush 16
 * items once, or 4 items 4 times).
 *
 * Practically, this is stored as a count of items in a uint8_t, so the
 * effective maximum value for a size class is 255 * sz.
 */
size_t opt_tcache_gc_delay_bytes = 0;

/*
 * When a cache bin is flushed because it's full, how much of it do we flush?
 * By default, we flush half the maximum number of items.
 */
unsigned opt_lg_tcache_flush_small_div = 1;
unsigned opt_lg_tcache_flush_large_div = 1;

/*
 * Number of cache bins enabled, including both large and small.  This value
 * is only used to initialize tcache_nbins in the per-thread tcache.
 * Directly modifying it will not affect threads already launched.
 */
unsigned		global_do_not_change_tcache_nbins;
/*
 * Max size class to be cached (can be small or large). This value is only used
 * to initialize tcache_max in the per-thread tcache.   Directly modifying it
 * will not affect threads already launched.
 */
size_t			global_do_not_change_tcache_maxclass;

/*
 * Default bin info for each bin.  Will be initialized in malloc_conf_init
 * and tcache_boot and should not be modified after that.
 */
static cache_bin_info_t opt_tcache_ncached_max[TCACHE_NBINS_MAX] = {{0}};
/*
 * Marks whether a bin's info is set already.  This is used in
 * tcache_bin_info_compute to avoid overwriting ncached_max specified by
 * malloc_conf.  It should be set only when parsing malloc_conf.
 */
static bool opt_tcache_ncached_max_set[TCACHE_NBINS_MAX] = {0};

tcaches_t		*tcaches;

/* Index of first element within tcaches that has never been used. */
static unsigned		tcaches_past;

/* Head of singly linked list tracking available tcaches elements. */
static tcaches_t	*tcaches_avail;

/* Protects tcaches{,_past,_avail}. */
static malloc_mutex_t	tcaches_mtx;

/******************************************************************************/

size_t
tcache_salloc(tsdn_t *tsdn, const void *ptr) {
	return arena_salloc(tsdn, ptr);
}

uint64_t
tcache_gc_new_event_wait(tsd_t *tsd) {
	return opt_tcache_gc_incr_bytes;
}

uint64_t
tcache_gc_postponed_event_wait(tsd_t *tsd) {
	return TE_MIN_START_WAIT;
}

uint64_t
tcache_gc_dalloc_new_event_wait(tsd_t *tsd) {
	return opt_tcache_gc_incr_bytes;
}

uint64_t
tcache_gc_dalloc_postponed_event_wait(tsd_t *tsd) {
	return TE_MIN_START_WAIT;
}

static inline void
tcache_bin_fill_ctl_init(tcache_slow_t *tcache_slow, szind_t szind) {
	assert(szind < SC_NBINS);
	cache_bin_fill_ctl_t *ctl =
	    &tcache_slow->bin_fill_ctl_do_not_access_directly[szind];
	ctl->base = 1;
	ctl->offset = 0;
}

static inline cache_bin_fill_ctl_t *
tcache_bin_fill_ctl_get(tcache_slow_t *tcache_slow, szind_t szind) {
	assert(szind < SC_NBINS);
	cache_bin_fill_ctl_t *ctl =
	    &tcache_slow->bin_fill_ctl_do_not_access_directly[szind];
	assert(ctl->base > ctl->offset);
	return ctl;
}

/*
 * The number of items to be filled at a time for a given small bin is
 * calculated by (ncached_max >> lg_fill_div).
 * The actual ctl struct consists of two fields, i.e. base and offset,
 * and the difference between the two(base - offset) is the final lg_fill_div.
 * The base is adjusted during GC based on the traffic within a period of time,
 * while the offset is updated in real time to handle the immediate traffic.
 */
static inline uint8_t
tcache_nfill_small_lg_div_get(tcache_slow_t *tcache_slow, szind_t szind) {
	cache_bin_fill_ctl_t *ctl = tcache_bin_fill_ctl_get(tcache_slow, szind);
	return (ctl->base - (opt_experimental_tcache_gc ? ctl->offset : 0));
}

/*
 * When we want to fill more items to respond to burst load,
 * offset is increased so that (base - offset) is decreased,
 * which in return increases the number of items to be filled.
 */
static inline void
tcache_nfill_small_burst_prepare(tcache_slow_t *tcache_slow, szind_t szind) {
	cache_bin_fill_ctl_t *ctl = tcache_bin_fill_ctl_get(tcache_slow, szind);
	if (ctl->offset + 1 < ctl->base) {
		ctl->offset++;
	}
}

static inline void
tcache_nfill_small_burst_reset(tcache_slow_t *tcache_slow, szind_t szind) {
	cache_bin_fill_ctl_t *ctl = tcache_bin_fill_ctl_get(tcache_slow, szind);
	ctl->offset = 0;
}

/*
 * limit == 0: indicating that the fill count should be increased,
 * i.e. lg_div(base) should be decreased.
 *
 * limit != 0: limit is set to ncached_max, indicating that the fill
 * count should be decreased, i.e. lg_div(base) should be increased.
 */
static inline void
tcache_nfill_small_gc_update(tcache_slow_t *tcache_slow, szind_t szind,
    cache_bin_sz_t limit) {
	cache_bin_fill_ctl_t *ctl = tcache_bin_fill_ctl_get(tcache_slow, szind);
	if (!limit && ctl->base > 1) {
		/*
		 * Increase fill count by 2X for small bins.  Make sure
		 * lg_fill_div stays greater than 1.
		 */
		ctl->base--;
	} else if (limit && (limit >> ctl->base) > 1) {
		/*
		 * Reduce fill count by 2X.  Limit lg_fill_div such that
		 * the fill count is always at least 1.
		 */
		ctl->base++;
	}
	/* Reset the offset for the next GC period. */
	ctl->offset = 0;
}

static uint8_t
tcache_gc_item_delay_compute(szind_t szind) {
	assert(szind < SC_NBINS);
	size_t sz = sz_index2size(szind);
	size_t item_delay = opt_tcache_gc_delay_bytes / sz;
	size_t delay_max = ZU(1)
	    << (sizeof(((tcache_slow_t *)NULL)->bin_flush_delay_items[0]) * 8);
	if (item_delay >= delay_max) {
		item_delay = delay_max - 1;
	}
	return (uint8_t)item_delay;
}

static inline void *
tcache_gc_small_heuristic_addr_get(tsd_t *tsd, tcache_slow_t *tcache_slow,
    szind_t szind) {
	assert(szind < SC_NBINS);
	tsdn_t *tsdn = tsd_tsdn(tsd);
	bin_t *bin = arena_bin_choose(tsdn, tcache_slow->arena, szind, NULL);
	assert(bin != NULL);

	malloc_mutex_lock(tsdn, &bin->lock);
	edata_t *slab = (bin->slabcur == NULL) ?
	    edata_heap_first(&bin->slabs_nonfull) : bin->slabcur;
	assert(slab != NULL || edata_heap_empty(&bin->slabs_nonfull));
	void *ret = (slab != NULL) ? edata_addr_get(slab) : NULL;
	assert(ret != NULL || slab == NULL);
	malloc_mutex_unlock(tsdn, &bin->lock);

	return ret;
}

static inline bool
tcache_gc_is_addr_remote(void *addr, uintptr_t min, uintptr_t max) {
	assert(addr != NULL);
	return ((uintptr_t)addr < min || (uintptr_t)addr >= max);
}

static inline cache_bin_sz_t
tcache_gc_small_nremote_get(cache_bin_t *cache_bin, void *addr,
    uintptr_t *addr_min, uintptr_t *addr_max, szind_t szind, size_t nflush) {
	assert(addr != NULL && addr_min != NULL && addr_max != NULL);
	/* The slab address range that the provided addr belongs to. */
	uintptr_t slab_min = (uintptr_t)addr;
	uintptr_t slab_max = slab_min + bin_infos[szind].slab_size;
	/*
	 * When growing retained virtual memory, it's increased exponentially,
	 * starting from 2M, so that the total number of disjoint virtual
	 * memory ranges retained by each shard is limited.
	 */
	uintptr_t neighbor_min = ((uintptr_t)addr > TCACHE_GC_NEIGHBOR_LIMIT) ?
	    ((uintptr_t)addr - TCACHE_GC_NEIGHBOR_LIMIT) : 0;
	uintptr_t neighbor_max = ((uintptr_t)addr < (UINTPTR_MAX -
	    TCACHE_GC_NEIGHBOR_LIMIT)) ? ((uintptr_t)addr +
	    TCACHE_GC_NEIGHBOR_LIMIT) : UINTPTR_MAX;

	/* Scan the entire bin to count the number of remote pointers. */
	void **head = cache_bin->stack_head;
	cache_bin_sz_t n_remote_slab = 0, n_remote_neighbor = 0;
	cache_bin_sz_t ncached = cache_bin_ncached_get_local(cache_bin);
	for (void **cur = head; cur < head + ncached; cur++) {
		n_remote_slab += (cache_bin_sz_t)tcache_gc_is_addr_remote(*cur,
		    slab_min, slab_max);
		n_remote_neighbor += (cache_bin_sz_t)tcache_gc_is_addr_remote(*cur,
		    neighbor_min, neighbor_max);
	}
	/*
	 * Note: since slab size is dynamic and can be larger than 2M, i.e.
	 * TCACHE_GC_NEIGHBOR_LIMIT, there is no guarantee as to which of
	 * n_remote_slab and n_remote_neighbor is greater.
	 */
	assert(n_remote_slab <= ncached && n_remote_neighbor <= ncached);
	/*
	 * We first consider keeping ptrs from the neighboring addr range,
	 * since in most cases the range is greater than the slab range.
	 * So if the number of non-neighbor ptrs is more than the intended
	 * flush amount, we use it as the anchor for flushing.
	 */
	if (n_remote_neighbor >= nflush) {
		*addr_min = neighbor_min;
		*addr_max = neighbor_max;
		return n_remote_neighbor;
	}
	/*
	 * We then consider only keeping ptrs from the local slab, and in most
	 * cases this is stricter, assuming that slab < 2M is the common case.
	 */
	*addr_min = slab_min;
	*addr_max = slab_max;
	return n_remote_slab;
}

/* Shuffle the ptrs in the bin to put the remote pointers at the bottom. */
static inline void
tcache_gc_small_bin_shuffle(cache_bin_t *cache_bin, cache_bin_sz_t nremote,
   uintptr_t addr_min, uintptr_t addr_max) {
	void **swap = NULL;
	cache_bin_sz_t ncached = cache_bin_ncached_get_local(cache_bin);
	cache_bin_sz_t ntop = ncached - nremote, cnt = 0;
	assert(ntop > 0 && ntop < ncached);
	/*
	 * Scan the [head, head + ntop) part of the cache bin, during which
	 * bubbling the non-remote ptrs to the top of the bin.
	 * After this, the [head, head + cnt) part of the bin contains only
	 * non-remote ptrs, and they're in the same relative order as before.
	 * While the [head + cnt, head + ntop) part contains only remote ptrs.
	 */
	void **head = cache_bin->stack_head;
	for (void **cur = head; cur < head + ntop; cur++) {
		if (!tcache_gc_is_addr_remote(*cur, addr_min, addr_max)) {
			/* Tracks the number of non-remote ptrs seen so far. */
			cnt++;
			/*
			 * There is remote ptr before the current non-remote ptr,
			 * swap the current non-remote ptr with the remote ptr,
			 * and increment the swap pointer so that it's still
			 * pointing to the top remote ptr in the bin.
			 */
			if (swap != NULL) {
				assert(swap < cur);
				assert(tcache_gc_is_addr_remote(*swap, addr_min, addr_max));
				void *tmp = *cur;
				*cur = *swap;
				*swap = tmp;
				swap++;
				assert(swap <= cur);
				assert(tcache_gc_is_addr_remote(*swap, addr_min, addr_max));
			}
			continue;
		} else if (swap == NULL) {
			/* Swap always points to the top remote ptr in the bin. */
			swap = cur;
		}
	}
	/*
	 * Scan the [head + ntop, head + ncached) part of the cache bin,
	 * after which it should only contain remote ptrs.
	 */
	for (void **cur = head + ntop; cur < head + ncached; cur++) {
		/* Early break if all non-remote ptrs have been moved. */
		if (cnt == ntop) {
			break;
		}
		if (!tcache_gc_is_addr_remote(*cur, addr_min, addr_max)) {
			assert(tcache_gc_is_addr_remote(*(head + cnt), addr_min,
			    addr_max));
			void *tmp = *cur;
			*cur = *(head + cnt);
			*(head + cnt) = tmp;
			cnt++;
		}
	}
	assert(cnt == ntop);
	/* Sanity check to make sure the shuffle is done correctly. */
	for (void **cur = head; cur < head + ncached; cur++) {
		assert(*cur != NULL);
		assert(((cur < head + ntop) && !tcache_gc_is_addr_remote(
		    *cur, addr_min, addr_max)) || ((cur >= head + ntop) &&
		    tcache_gc_is_addr_remote(*cur, addr_min, addr_max)));
	}
}

static bool
tcache_gc_small(tsd_t *tsd, tcache_slow_t *tcache_slow, tcache_t *tcache,
    szind_t szind) {
	/*
	 * Aim to flush 3/4 of items below low-water, with remote pointers being
	 * prioritized for flushing.
	 */
	assert(szind < SC_NBINS);

	cache_bin_t *cache_bin = &tcache->bins[szind];
	assert(!tcache_bin_disabled(szind, cache_bin, tcache->tcache_slow));
	cache_bin_sz_t ncached = cache_bin_ncached_get_local(cache_bin);
	cache_bin_sz_t low_water = cache_bin_low_water_get(cache_bin);
	if (low_water > 0) {
		/*
		 * There is unused items within the GC period => reduce fill count.
		 * limit field != 0 is borrowed to indicate that the fill count
		 * should be reduced.
		 */
		tcache_nfill_small_gc_update(tcache_slow, szind,
		    /* limit */ cache_bin_ncached_max_get(cache_bin));
	} else if (tcache_slow->bin_refilled[szind]) {
		/*
		 * There has been refills within the GC period => increase fill count.
		 * limit field set to 0 is borrowed to indicate that the fill count
		 * should be increased.
		 */
		tcache_nfill_small_gc_update(tcache_slow, szind, /* limit */ 0);
		tcache_slow->bin_refilled[szind] = false;
	}
	assert(!tcache_slow->bin_refilled[szind]);

	cache_bin_sz_t nflush = low_water - (low_water >> 2);
	/*
	 * When the new tcache gc is not enabled, keep the flush delay logic,
	 * and directly flush the bottom nflush items if needed.
	 */
	if (!opt_experimental_tcache_gc) {
		if (nflush < tcache_slow->bin_flush_delay_items[szind]) {
			/* Workaround for a conversion warning. */
			uint8_t nflush_uint8 = (uint8_t)nflush;
			assert(sizeof(tcache_slow->bin_flush_delay_items[0]) ==
			    sizeof(nflush_uint8));
			tcache_slow->bin_flush_delay_items[szind] -= nflush_uint8;
			return false;
		}

		tcache_slow->bin_flush_delay_items[szind]
		    = tcache_gc_item_delay_compute(szind);
		goto label_flush;
	}

	/* Directly goto the flush path when the entire bin needs to be flushed. */
	if ( nflush == ncached) {
		goto label_flush;
	}

	/* Query arena binshard to get heuristic locality info. */
	void *addr = tcache_gc_small_heuristic_addr_get(tsd, tcache_slow, szind);
	if (addr == NULL) {
		goto label_flush;
	}

	/*
	 * Use the queried addr above to get the number of remote ptrs in the
	 * bin, and the min/max of the local addr range.
	 */
	uintptr_t addr_min, addr_max;
	cache_bin_sz_t nremote = tcache_gc_small_nremote_get(cache_bin, addr,
	    &addr_min, &addr_max, szind, nflush);

	/*
	 * Update the nflush to the larger value between the intended flush count
	 * and the number of remote ptrs.
	 */
	if (nremote > nflush) {
		nflush = nremote;
	}
	/*
	 * When entering the locality check, nflush should be less than ncached,
	 * otherwise the entire bin should be flushed regardless. The only case
	 * when nflush gets updated to ncached after locality check is, when all
	 * the items in the bin are remote, in which case the entire bin should
	 * also be flushed.
	 */
	assert(nflush < ncached || nremote == ncached);
	if (nremote == 0 || nremote == ncached)	{
		goto label_flush;
	}

	/*
	 * Move the remote points to the bottom of the bin for flushing.
	 * As long as moved to the bottom, the order of these nremote ptrs
	 * does not matter, since they are going to be flushed anyway.
	 * The rest of the ptrs are moved to the top of the bin, and their
	 * relative order is maintained.
	 */
	tcache_gc_small_bin_shuffle(cache_bin, nremote, addr_min, addr_max);

label_flush:
	if (nflush == 0) {
		assert(low_water == 0);
		return false;
	}
	assert(nflush <= ncached);
	tcache_bin_flush_small(tsd, tcache, cache_bin, szind,
	    (unsigned)(ncached - nflush));
	return true;
}

static bool
tcache_gc_large(tsd_t *tsd, tcache_slow_t *tcache_slow, tcache_t *tcache,
    szind_t szind) {
	/*
	 * Like the small GC, flush 3/4 of untouched items. However, simply flush
	 * the bottom nflush items, without any locality check.
	 */
	assert(szind >= SC_NBINS);
	cache_bin_t *cache_bin = &tcache->bins[szind];
	assert(!tcache_bin_disabled(szind, cache_bin, tcache->tcache_slow));
	cache_bin_sz_t low_water = cache_bin_low_water_get(cache_bin);
	if (low_water == 0) {
		return false;
	}
	unsigned nrem = (unsigned)(cache_bin_ncached_get_local(cache_bin) -
	    low_water + (low_water >> 2));
	tcache_bin_flush_large(tsd, tcache, cache_bin, szind, nrem);
	return true;
}

/* Try to gc one bin by szind, return true if there is item flushed. */
static bool
tcache_try_gc_bin(tsd_t *tsd, tcache_slow_t *tcache_slow, tcache_t *tcache,
    szind_t szind) {
	assert(tcache != NULL);
	cache_bin_t *cache_bin = &tcache->bins[szind];
	if (tcache_bin_disabled(szind, cache_bin, tcache_slow)) {
		return false;
	}

	bool is_small = (szind < SC_NBINS);
	tcache_bin_flush_stashed(tsd, tcache, cache_bin, szind, is_small);
	bool ret = is_small ? tcache_gc_small(tsd, tcache_slow, tcache, szind) :
	    tcache_gc_large(tsd, tcache_slow, tcache, szind);
	cache_bin_low_water_set(cache_bin);
	return ret;
}

static void
tcache_event(tsd_t *tsd) {
	tcache_t *tcache = tcache_get(tsd);
	if (tcache == NULL) {
		return;
	}

	tcache_slow_t *tcache_slow = tsd_tcache_slowp_get(tsd);
	assert(tcache_slow != NULL);

	/* When the new tcache gc is not enabled, GC one bin at a time. */
	if (!opt_experimental_tcache_gc) {
		szind_t szind = tcache_slow->next_gc_bin;
		tcache_try_gc_bin(tsd, tcache_slow, tcache, szind);
		tcache_slow->next_gc_bin++;
		if (tcache_slow->next_gc_bin == tcache_nbins_get(tcache_slow)) {
			tcache_slow->next_gc_bin = 0;
		}
		return;
	}

	nstime_t now;
	nstime_copy(&now, &tcache_slow->last_gc_time);
	nstime_update(&now);
	assert(nstime_compare(&now, &tcache_slow->last_gc_time) >= 0);

	if (nstime_ns(&now) - nstime_ns(&tcache_slow->last_gc_time) <
	    TCACHE_GC_INTERVAL_NS) {
		// time interval is too short, skip this event.
		return;
	}
	/* Update last_gc_time to now. */
	nstime_copy(&tcache_slow->last_gc_time, &now);

	unsigned gc_small_nbins = 0, gc_large_nbins = 0;
	unsigned tcache_nbins = tcache_nbins_get(tcache_slow);
	unsigned small_nbins = tcache_nbins > SC_NBINS ? SC_NBINS : tcache_nbins;
	szind_t szind_small = tcache_slow->next_gc_bin_small;
	szind_t szind_large = tcache_slow->next_gc_bin_large;

	/* Flush at most TCACHE_GC_SMALL_NBINS_MAX small bins at a time. */
	for (unsigned i = 0; i < small_nbins && gc_small_nbins <
	    TCACHE_GC_SMALL_NBINS_MAX; i++) {
		assert(szind_small < SC_NBINS);
		if (tcache_try_gc_bin(tsd, tcache_slow, tcache, szind_small)) {
			gc_small_nbins++;
		}
		if (++szind_small == small_nbins) {
			szind_small = 0;
		}
	}
	tcache_slow->next_gc_bin_small = szind_small;

	if (tcache_nbins <= SC_NBINS) {
		return;
	}

	/* Flush at most TCACHE_GC_LARGE_NBINS_MAX large bins at a time. */
	for (unsigned i = SC_NBINS; i < tcache_nbins && gc_large_nbins <
	    TCACHE_GC_LARGE_NBINS_MAX; i++) {
		assert(szind_large >= SC_NBINS && szind_large < tcache_nbins);
		if (tcache_try_gc_bin(tsd, tcache_slow, tcache, szind_large)) {
			gc_large_nbins++;
		}
		if (++szind_large == tcache_nbins) {
			szind_large = SC_NBINS;
		}
	}
	tcache_slow->next_gc_bin_large = szind_large;
}

void
tcache_gc_event_handler(tsd_t *tsd, uint64_t elapsed) {
	assert(elapsed == TE_INVALID_ELAPSED);
	tcache_event(tsd);
}

void
tcache_gc_dalloc_event_handler(tsd_t *tsd, uint64_t elapsed) {
	assert(elapsed == TE_INVALID_ELAPSED);
	tcache_event(tsd);
}

void *
tcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena,
    tcache_t *tcache, cache_bin_t *cache_bin, szind_t binind,
    bool *tcache_success) {
	tcache_slow_t *tcache_slow = tcache->tcache_slow;
	void *ret;

	assert(tcache_slow->arena != NULL);
	assert(!tcache_bin_disabled(binind, cache_bin, tcache_slow));
	cache_bin_sz_t nfill = cache_bin_ncached_max_get(cache_bin)
	    >> tcache_nfill_small_lg_div_get(tcache_slow, binind);
	if (nfill == 0) {
		nfill = 1;
	}
	arena_cache_bin_fill_small(tsdn, arena, cache_bin, binind,
	    /* nfill_min */ opt_experimental_tcache_gc ?
	    ((nfill >> 1) + 1) : nfill, /* nfill_max */ nfill);
	tcache_slow->bin_refilled[binind] = true;
	tcache_nfill_small_burst_prepare(tcache_slow, binind);
	ret = cache_bin_alloc(cache_bin, tcache_success);

	return ret;
}

static const void *
tcache_bin_flush_ptr_getter(void *arr_ctx, size_t ind) {
	cache_bin_ptr_array_t *arr = (cache_bin_ptr_array_t *)arr_ctx;
	return arr->ptr[ind];
}

static void
tcache_bin_flush_metadata_visitor(void *szind_sum_ctx,
    emap_full_alloc_ctx_t *alloc_ctx) {
	size_t *szind_sum = (size_t *)szind_sum_ctx;
	*szind_sum -= alloc_ctx->szind;
	util_prefetch_write_range(alloc_ctx->edata, sizeof(edata_t));
}

JEMALLOC_NOINLINE static void
tcache_bin_flush_size_check_fail(cache_bin_ptr_array_t *arr, szind_t szind,
    size_t nptrs, emap_batch_lookup_result_t *edatas) {
	bool found_mismatch = false;
	for (size_t i = 0; i < nptrs; i++) {
		szind_t true_szind = edata_szind_get(edatas[i].edata);
		if (true_szind != szind) {
			found_mismatch = true;
			safety_check_fail_sized_dealloc(
			    /* current_dealloc */ false,
			    /* ptr */ tcache_bin_flush_ptr_getter(arr, i),
			    /* true_size */ sz_index2size(true_szind),
			    /* input_size */ sz_index2size(szind));
		}
	}
	assert(found_mismatch);
}

static void
tcache_bin_flush_edatas_lookup(tsd_t *tsd, cache_bin_ptr_array_t *arr,
    szind_t binind, size_t nflush, emap_batch_lookup_result_t *edatas) {

	/*
	 * This gets compiled away when config_opt_safety_checks is false.
	 * Checks for sized deallocation bugs, failing early rather than
	 * corrupting metadata.
	 */
	size_t szind_sum = binind * nflush;
	emap_edata_lookup_batch(tsd, &arena_emap_global, nflush,
	    &tcache_bin_flush_ptr_getter, (void *)arr,
	    &tcache_bin_flush_metadata_visitor, (void *)&szind_sum,
	    edatas);
	if (config_opt_safety_checks && unlikely(szind_sum != 0)) {
		tcache_bin_flush_size_check_fail(arr, binind, nflush, edatas);
	}
}

JEMALLOC_ALWAYS_INLINE void
tcache_bin_flush_impl_small(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin,
    szind_t binind, cache_bin_ptr_array_t *ptrs, unsigned nflush) {
	tcache_slow_t *tcache_slow = tcache->tcache_slow;
	/*
	 * A couple lookup calls take tsdn; declare it once for convenience
	 * instead of calling tsd_tsdn(tsd) all the time.
	 */
	tsdn_t *tsdn = tsd_tsdn(tsd);

	assert(binind < SC_NBINS);
	arena_t *tcache_arena = tcache_slow->arena;
	assert(tcache_arena != NULL);
	unsigned tcache_binshard = tsd_binshardsp_get(tsdn_tsd(tsdn))->binshard[binind];

	/*
	 * Variable length array must have > 0 length; the last element is never
	 * touched (it's just included to satisfy the no-zero-length rule).
	 */
	VARIABLE_ARRAY(emap_batch_lookup_result_t, item_edata, nflush + 1);
	tcache_bin_flush_edatas_lookup(tsd, ptrs, binind, nflush, item_edata);

	/*
	 * The slabs where we freed the last remaining object in the slab (and
	 * so need to free the slab itself).
	 * Used only if small == true.
	 */
	unsigned dalloc_count = 0;
	VARIABLE_ARRAY(edata_t *, dalloc_slabs, nflush + 1);

	/*
	 * There's an edge case where we need to deallocate more slabs than we
	 * have elements of dalloc_slabs.  This can if we end up deallocating
	 * items batched by another thread in addition to ones flushed from the
	 * cache.  Since this is not very likely (most small object
	 * deallocations don't free up a whole slab), we don't want to burn the
	 * stack space to keep those excess slabs in an array.  Instead we'll
	 * maintain an overflow list.
	 */
	edata_list_active_t dalloc_slabs_extra;
	edata_list_active_init(&dalloc_slabs_extra);

	/*
	 * We're about to grab a bunch of locks.  If one of them happens to be
	 * the one guarding the arena-level stats counters we flush our
	 * thread-local ones to, we do so under one critical section.
	 */
	bool merged_stats = false;

	/*
	 * We maintain the invariant that all edatas yet to be flushed are
	 * contained in the half-open range [flush_start, flush_end).  We'll
	 * repeatedly partition the array so that the unflushed items are at the
	 * end.
	 */
	unsigned flush_start = 0;

	while (flush_start < nflush) {
		/*
		 * After our partitioning step, all objects to flush will be in
		 * the half-open range [prev_flush_start, flush_start), and
		 * flush_start will be updated to correspond to the next loop
		 * iteration.
		 */
		unsigned prev_flush_start = flush_start;

		edata_t *cur_edata = item_edata[flush_start].edata;
		unsigned cur_arena_ind = edata_arena_ind_get(cur_edata);
		arena_t *cur_arena = arena_get(tsdn, cur_arena_ind, false);

		unsigned cur_binshard = edata_binshard_get(cur_edata);
		bin_t *cur_bin = arena_get_bin(cur_arena, binind,
		    cur_binshard);
		assert(cur_binshard < bin_infos[binind].n_shards);

		/*
		 * Start off the partition; item_edata[i] always matches itself
		 * of course.
		 */
		flush_start++;
		for (unsigned i = flush_start; i < nflush; i++) {
			void *ptr = ptrs->ptr[i];
			edata_t *edata = item_edata[i].edata;
			assert(ptr != NULL && edata != NULL);
			assert((uintptr_t)ptr >= (uintptr_t)edata_addr_get(edata));
			assert((uintptr_t)ptr < (uintptr_t)edata_past_get(edata));
			if (edata_arena_ind_get(edata) == cur_arena_ind
			    && edata_binshard_get(edata) == cur_binshard) {
				/* Swap the edatas. */
				emap_batch_lookup_result_t temp_edata
				    = item_edata[flush_start];
				item_edata[flush_start] = item_edata[i];
				item_edata[i] = temp_edata;
				/* Swap the pointers */
				void *temp_ptr = ptrs->ptr[flush_start];
				ptrs->ptr[flush_start] = ptrs->ptr[i];
				ptrs->ptr[i] = temp_ptr;
				flush_start++;
			}
		}
		/* Make sure we implemented partitioning correctly. */
		if (config_debug) {
			for (unsigned i = prev_flush_start; i < flush_start;
			    i++) {
				edata_t *edata = item_edata[i].edata;
				unsigned arena_ind = edata_arena_ind_get(edata);
				assert(arena_ind == cur_arena_ind);
				unsigned binshard = edata_binshard_get(edata);
				assert(binshard == cur_binshard);
			}
			for (unsigned i = flush_start; i < nflush; i++) {
				edata_t *edata = item_edata[i].edata;
				assert(edata_arena_ind_get(edata)
				    != cur_arena_ind
				    || edata_binshard_get(edata)
				    != cur_binshard);
			}
		}

		/*
		 * We never batch when flushing to our home-base bin shard,
		 * since it's likely that we'll have to acquire that lock anyway
		 * when flushing stats.
		 *
		 * A plausible check we could add to can_batch is
		 * '&& arena_is_auto(cur_arena)'.  The motivation would be that
		 * we have a higher tolerance for dubious user assumptions
		 * around non-auto arenas (e.g. "if I deallocate every object I
		 * allocated, and then call tcache.flush, then the arena stats
		 * must reflect zero live allocations").
		 *
		 * This is dubious for a couple reasons:
		 * - We already don't provide perfect fidelity for stats
		 *   counting (e.g. for profiled allocations, whose size can
		 *   inflate in stats).
		 * - Hanging load-bearing guarantees around stats impedes
		 *   scalability in general.
		 *
		 * There are some "complete" strategies we could do instead:
		 * - Add a arena.<i>.quiesce call to pop all bins for users who
		 *   do want those stats accounted for.
		 * - Make batchability a user-controllable per-arena option.
		 * - Do a batch pop after every mutex acquisition for which we
		 *   want to provide accurate stats.  This gives perfectly
		 *   accurate stats, but can cause weird performance effects
		 *   (because doing stats collection can now result in slabs
		 *   becoming empty, and therefore purging, large mutex
		 *   acquisition, etc.).
		 * - Propagate the "why" behind a flush down to the level of the
		 *   batcher, and include a batch pop attempt down full tcache
		 *   flushing pathways.  This is just a lot of plumbing and
		 *   internal complexity.
		 *
		 * We don't do any of these right now, but the decision calculus
		 * and tradeoffs are subtle enough that the reasoning was worth
		 * leaving in this comment.
		 */
		bool bin_is_batched = arena_bin_has_batch(binind);
		bool home_binshard = (cur_arena == tcache_arena
		    && cur_binshard == tcache_binshard);
		bool can_batch = (flush_start - prev_flush_start
		    <= opt_bin_info_remote_free_max_batch)
		    && !home_binshard && bin_is_batched;

		/*
		 * We try to avoid the batching pathway if we can, so we always
		 * at least *try* to lock.
		 */
		bool locked = false;
		bool batched = false;
		bool batch_failed = false;
		if (can_batch) {
			locked = !malloc_mutex_trylock(tsdn, &cur_bin->lock);
		}
		if (can_batch && !locked) {
			bin_with_batch_t *batched_bin =
			    (bin_with_batch_t *)cur_bin;
			size_t push_idx = batcher_push_begin(tsdn,
			    &batched_bin->remote_frees,
			    flush_start - prev_flush_start);
			bin_batching_test_after_push(push_idx);

			if (push_idx != BATCHER_NO_IDX) {
				batched = true;
				unsigned nbatched
				    = flush_start - prev_flush_start;
				for (unsigned i = 0; i < nbatched; i++) {
					unsigned src_ind = prev_flush_start + i;
					batched_bin->remote_free_data[
					    push_idx + i].ptr
						= ptrs->ptr[src_ind];
					batched_bin->remote_free_data[
					    push_idx + i].slab
						= item_edata[src_ind].edata;
				}
				batcher_push_end(tsdn,
				    &batched_bin->remote_frees);
			} else {
				batch_failed = true;
			}
		}
		if (!batched) {
			if (!locked) {
				malloc_mutex_lock(tsdn, &cur_bin->lock);
			}
			/*
			 * Unlike other stats (which only ever get flushed into
			 * a tcache's associated arena), batch_failed counts get
			 * accumulated into the bin where the push attempt
			 * failed.
			 */
			if (config_stats && batch_failed) {
				cur_bin->stats.batch_failed_pushes++;
			}

			/*
			 * Flush stats first, if that was the right lock.  Note
			 * that we don't actually have to flush stats into the
			 * current thread's binshard. Flushing into any binshard
			 * in the same arena is enough; we don't expose stats on
			 * per-binshard basis (just per-bin).
			 */
			if (config_stats && tcache_arena == cur_arena
			    && !merged_stats) {
				merged_stats = true;
				cur_bin->stats.nflushes++;
				cur_bin->stats.nrequests +=
				    cache_bin->tstats.nrequests;
				cache_bin->tstats.nrequests = 0;
			}
			unsigned preallocated_slabs = nflush;
			unsigned ndalloc_slabs = arena_bin_batch_get_ndalloc_slabs(
			    preallocated_slabs);

			/* Next flush objects our own objects. */
			/* Init only to avoid used-uninitialized warning. */
			arena_dalloc_bin_locked_info_t dalloc_bin_info = {0};
			arena_dalloc_bin_locked_begin(&dalloc_bin_info, binind);
			for (unsigned i = prev_flush_start; i < flush_start;
			    i++) {
				void *ptr = ptrs->ptr[i];
				edata_t *edata = item_edata[i].edata;
				arena_dalloc_bin_locked_step(tsdn, cur_arena,
				    cur_bin, &dalloc_bin_info, binind, edata,
				    ptr, dalloc_slabs, ndalloc_slabs,
				    &dalloc_count, &dalloc_slabs_extra);
			}
			/*
			 * Lastly, flush any batched objects (from other
			 * threads).
			 */
			if (bin_is_batched) {
				arena_bin_flush_batch_impl(tsdn, cur_arena,
				    cur_bin, &dalloc_bin_info, binind,
				    dalloc_slabs, ndalloc_slabs,
				    &dalloc_count, &dalloc_slabs_extra);
			}

			arena_dalloc_bin_locked_finish(tsdn, cur_arena, cur_bin,
			    &dalloc_bin_info);
			malloc_mutex_unlock(tsdn, &cur_bin->lock);
		}
		arena_decay_ticks(tsdn, cur_arena,
		    flush_start - prev_flush_start);
	}

	/* Handle all deferred slab dalloc. */
	for (unsigned i = 0; i < dalloc_count; i++) {
		edata_t *slab = dalloc_slabs[i];
		arena_slab_dalloc(tsdn, arena_get_from_edata(slab), slab);
	}
	while (!edata_list_active_empty(&dalloc_slabs_extra)) {
		edata_t *slab = edata_list_active_first(&dalloc_slabs_extra);
		edata_list_active_remove(&dalloc_slabs_extra, slab);
		arena_slab_dalloc(tsdn, arena_get_from_edata(slab), slab);
	}

	if (config_stats && !merged_stats) {
			/*
			 * The flush loop didn't happen to flush to this
			 * thread's arena, so the stats didn't get merged.
			 * Manually do so now.
			 */
			bin_t *bin = arena_bin_choose(tsdn, tcache_arena,
			    binind, NULL);
			malloc_mutex_lock(tsdn, &bin->lock);
			bin->stats.nflushes++;
			bin->stats.nrequests += cache_bin->tstats.nrequests;
			cache_bin->tstats.nrequests = 0;
			malloc_mutex_unlock(tsdn, &bin->lock);
	}
}

JEMALLOC_ALWAYS_INLINE void
tcache_bin_flush_impl_large(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin,
    szind_t binind, cache_bin_ptr_array_t *ptrs, unsigned nflush) {
	tcache_slow_t *tcache_slow = tcache->tcache_slow;
	/*
	 * A couple lookup calls take tsdn; declare it once for convenience
	 * instead of calling tsd_tsdn(tsd) all the time.
	 */
	tsdn_t *tsdn = tsd_tsdn(tsd);

	assert(binind < tcache_nbins_get(tcache_slow));
	arena_t *tcache_arena = tcache_slow->arena;
	assert(tcache_arena != NULL);

	/*
	 * Variable length array must have > 0 length; the last element is never
	 * touched (it's just included to satisfy the no-zero-length rule).
	 */
	VARIABLE_ARRAY(emap_batch_lookup_result_t, item_edata, nflush + 1);
	tcache_bin_flush_edatas_lookup(tsd, ptrs, binind, nflush, item_edata);

	/*
	 * We're about to grab a bunch of locks.  If one of them happens to be
	 * the one guarding the arena-level stats counters we flush our
	 * thread-local ones to, we do so under one critical section.
	 */
	bool merged_stats = false;
	while (nflush > 0) {
		/* Lock the arena, or bin, associated with the first object. */
		edata_t *edata = item_edata[0].edata;
		unsigned cur_arena_ind = edata_arena_ind_get(edata);
		arena_t *cur_arena = arena_get(tsdn, cur_arena_ind, false);

		if (!arena_is_auto(cur_arena)) {
			malloc_mutex_lock(tsdn, &cur_arena->large_mtx);
		}

		/*
		 * If we acquired the right lock and have some stats to flush,
		 * flush them.
		 */
		if (config_stats && tcache_arena == cur_arena
		    && !merged_stats) {
			merged_stats = true;
			arena_stats_large_flush_nrequests_add(tsdn,
			    &tcache_arena->stats, binind,
			    cache_bin->tstats.nrequests);
			cache_bin->tstats.nrequests = 0;
		}

		/*
		 * Large allocations need special prep done.  Afterwards, we can
		 * drop the large lock.
		 */
		for (unsigned i = 0; i < nflush; i++) {
			void *ptr = ptrs->ptr[i];
			edata = item_edata[i].edata;
			assert(ptr != NULL && edata != NULL);

			if (edata_arena_ind_get(edata) == cur_arena_ind) {
				large_dalloc_prep_locked(tsdn,
				    edata);
			}
		}
		if (!arena_is_auto(cur_arena)) {
			malloc_mutex_unlock(tsdn, &cur_arena->large_mtx);
		}

		/* Deallocate whatever we can. */
		unsigned ndeferred = 0;
		for (unsigned i = 0; i < nflush; i++) {
			void *ptr = ptrs->ptr[i];
			edata = item_edata[i].edata;
			assert(ptr != NULL && edata != NULL);
			if (edata_arena_ind_get(edata) != cur_arena_ind) {
				/*
				 * The object was allocated either via a
				 * different arena, or a different bin in this
				 * arena.  Either way, stash the object so that
				 * it can be handled in a future pass.
				 */
				ptrs->ptr[ndeferred] = ptr;
				item_edata[ndeferred].edata = edata;
				ndeferred++;
				continue;
			}
			if (large_dalloc_safety_checks(edata, ptr,
			    sz_index2size(binind))) {
				/* See the comment in isfree. */
				continue;
			}
			large_dalloc_finish(tsdn, edata);
		}
		arena_decay_ticks(tsdn, cur_arena, nflush - ndeferred);
		nflush = ndeferred;
	}

	if (config_stats && !merged_stats) {
		arena_stats_large_flush_nrequests_add(tsdn,
		    &tcache_arena->stats, binind,
		    cache_bin->tstats.nrequests);
		cache_bin->tstats.nrequests = 0;
	}
}

JEMALLOC_ALWAYS_INLINE void
tcache_bin_flush_impl(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin,
    szind_t binind, cache_bin_ptr_array_t *ptrs, unsigned nflush, bool small) {
	assert(ptrs != NULL && ptrs->ptr != NULL);
	unsigned nflush_batch, nflushed = 0;
	cache_bin_ptr_array_t ptrs_batch;
	do {
		nflush_batch = nflush - nflushed;
		if (nflush_batch > CACHE_BIN_NFLUSH_BATCH_MAX) {
			nflush_batch = CACHE_BIN_NFLUSH_BATCH_MAX;
		}
		assert(nflush_batch <= CACHE_BIN_NFLUSH_BATCH_MAX);
		(&ptrs_batch)->n = (cache_bin_sz_t)nflush_batch;
		(&ptrs_batch)->ptr = ptrs->ptr + nflushed;
		/*
		 * The small/large flush logic is very similar; you might conclude that
		 * it's a good opportunity to share code.  We've tried this, and by and
		 * large found this to obscure more than it helps; there are so many
		 * fiddly bits around things like stats handling, precisely when and
		 * which mutexes are acquired, etc., that almost all code ends up being
		 * gated behind 'if (small) { ... } else { ... }'.  Even though the
		 * '...' is morally equivalent, the code itself needs slight tweaks.
		 */
		if (small) {
			tcache_bin_flush_impl_small(tsd, tcache, cache_bin, binind,
			    &ptrs_batch, nflush_batch);
		} else {
			tcache_bin_flush_impl_large(tsd, tcache, cache_bin, binind,
			    &ptrs_batch, nflush_batch);
		}
		nflushed += nflush_batch;
	} while (nflushed < nflush);
	assert(nflush == nflushed);
	assert((ptrs->ptr + nflush) == ((&ptrs_batch)->ptr + nflush_batch));
}

JEMALLOC_ALWAYS_INLINE void
tcache_bin_flush_bottom(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin,
    szind_t binind, unsigned rem, bool small) {
	assert(rem <= cache_bin_ncached_max_get(cache_bin));
	assert(!tcache_bin_disabled(binind, cache_bin, tcache->tcache_slow));
	cache_bin_sz_t orig_nstashed = cache_bin_nstashed_get_local(cache_bin);
	tcache_bin_flush_stashed(tsd, tcache, cache_bin, binind, small);

	cache_bin_sz_t ncached = cache_bin_ncached_get_local(cache_bin);
	assert((cache_bin_sz_t)rem <= ncached + orig_nstashed);
	if ((cache_bin_sz_t)rem > ncached) {
		/*
		 * The flush_stashed above could have done enough flushing, if
		 * there were many items stashed.  Validate that: 1) non zero
		 * stashed, and 2) bin stack has available space now.
		 */
		assert(orig_nstashed > 0);
		assert(ncached + cache_bin_nstashed_get_local(cache_bin)
		    < cache_bin_ncached_max_get(cache_bin));
		/* Still go through the flush logic for stats purpose only. */
		rem = ncached;
	}
	cache_bin_sz_t nflush = ncached - (cache_bin_sz_t)rem;

	CACHE_BIN_PTR_ARRAY_DECLARE(ptrs, nflush);
	cache_bin_init_ptr_array_for_flush(cache_bin, &ptrs, nflush);

	tcache_bin_flush_impl(tsd, tcache, cache_bin, binind, &ptrs, nflush,
	    small);

	cache_bin_finish_flush(cache_bin, &ptrs, nflush);
}

void
tcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin,
    szind_t binind, unsigned rem) {
	tcache_nfill_small_burst_reset(tcache->tcache_slow, binind);
	tcache_bin_flush_bottom(tsd, tcache, cache_bin, binind, rem,
	    /* small */ true);
}

void
tcache_bin_flush_large(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin,
    szind_t binind, unsigned rem) {
	tcache_bin_flush_bottom(tsd, tcache, cache_bin, binind, rem,
	    /* small */ false);
}

/*
 * Flushing stashed happens when 1) tcache fill, 2) tcache flush, or 3) tcache
 * GC event.  This makes sure that the stashed items do not hold memory for too
 * long, and new buffers can only be allocated when nothing is stashed.
 *
 * The downside is, the time between stash and flush may be relatively short,
 * especially when the request rate is high.  It lowers the chance of detecting
 * write-after-free -- however that is a delayed detection anyway, and is less
 * of a focus than the memory overhead.
 */
void
tcache_bin_flush_stashed(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin,
    szind_t binind, bool is_small) {
	assert(!tcache_bin_disabled(binind, cache_bin, tcache->tcache_slow));
	/*
	 * The two below are for assertion only.  The content of original cached
	 * items remain unchanged -- the stashed items reside on the other end
	 * of the stack.  Checking the stack head and ncached to verify.
	 */
	void *head_content = *cache_bin->stack_head;
	cache_bin_sz_t orig_cached = cache_bin_ncached_get_local(cache_bin);

	cache_bin_sz_t nstashed = cache_bin_nstashed_get_local(cache_bin);
	assert(orig_cached + nstashed <= cache_bin_ncached_max_get(cache_bin));
	if (nstashed == 0) {
		return;
	}

	CACHE_BIN_PTR_ARRAY_DECLARE(ptrs, nstashed);
	cache_bin_init_ptr_array_for_stashed(cache_bin, binind, &ptrs,
	    nstashed);
	san_check_stashed_ptrs(ptrs.ptr, nstashed, sz_index2size(binind));
	tcache_bin_flush_impl(tsd, tcache, cache_bin, binind, &ptrs, nstashed,
	    is_small);
	cache_bin_finish_flush_stashed(cache_bin);

	assert(cache_bin_nstashed_get_local(cache_bin) == 0);
	assert(cache_bin_ncached_get_local(cache_bin) == orig_cached);
	assert(head_content == *cache_bin->stack_head);
}

JET_EXTERN bool
tcache_get_default_ncached_max_set(szind_t ind) {
	return opt_tcache_ncached_max_set[ind];
}

JET_EXTERN const cache_bin_info_t *
tcache_get_default_ncached_max(void) {
	return opt_tcache_ncached_max;
}

bool
tcache_bin_ncached_max_read(tsd_t *tsd, size_t bin_size,
    cache_bin_sz_t *ncached_max) {
	if (bin_size > TCACHE_MAXCLASS_LIMIT) {
		return true;
	}

	if (!tcache_available(tsd)) {
		*ncached_max = 0;
		return false;
	}

	tcache_t *tcache = tsd_tcachep_get(tsd);
	assert(tcache != NULL);
	szind_t bin_ind = sz_size2index(bin_size);

	cache_bin_t *bin = &tcache->bins[bin_ind];
	*ncached_max = tcache_bin_disabled(bin_ind, bin, tcache->tcache_slow) ?
	    0: cache_bin_ncached_max_get(bin);
	return false;
}

void
tcache_arena_associate(tsdn_t *tsdn, tcache_slow_t *tcache_slow,
    tcache_t *tcache, arena_t *arena) {
	assert(tcache_slow->arena == NULL);
	tcache_slow->arena = arena;

	if (config_stats) {
		/* Link into list of extant tcaches. */
		malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);

		ql_elm_new(tcache_slow, link);
		ql_tail_insert(&arena->tcache_ql, tcache_slow, link);
		cache_bin_array_descriptor_init(
		    &tcache_slow->cache_bin_array_descriptor, tcache->bins);
		ql_tail_insert(&arena->cache_bin_array_descriptor_ql,
		    &tcache_slow->cache_bin_array_descriptor, link);

		malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);
	}
}

static void
tcache_arena_dissociate(tsdn_t *tsdn, tcache_slow_t *tcache_slow,
    tcache_t *tcache) {
	arena_t *arena = tcache_slow->arena;
	assert(arena != NULL);
	if (config_stats) {
		/* Unlink from list of extant tcaches. */
		malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);
		if (config_debug) {
			bool in_ql = false;
			tcache_slow_t *iter;
			ql_foreach(iter, &arena->tcache_ql, link) {
				if (iter == tcache_slow) {
					in_ql = true;
					break;
				}
			}
			assert(in_ql);
		}
		ql_remove(&arena->tcache_ql, tcache_slow, link);
		ql_remove(&arena->cache_bin_array_descriptor_ql,
		    &tcache_slow->cache_bin_array_descriptor, link);
		tcache_stats_merge(tsdn, tcache_slow->tcache, arena);
		malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);
	}
	tcache_slow->arena = NULL;
}

void
tcache_arena_reassociate(tsdn_t *tsdn, tcache_slow_t *tcache_slow,
    tcache_t *tcache, arena_t *arena) {
	tcache_arena_dissociate(tsdn, tcache_slow, tcache);
	tcache_arena_associate(tsdn, tcache_slow, tcache, arena);
}

static void
tcache_default_settings_init(tcache_slow_t *tcache_slow) {
	assert(tcache_slow != NULL);
	assert(global_do_not_change_tcache_maxclass != 0);
	assert(global_do_not_change_tcache_nbins != 0);
	tcache_slow->tcache_nbins = global_do_not_change_tcache_nbins;
}

static void
tcache_init(tsd_t *tsd, tcache_slow_t *tcache_slow, tcache_t *tcache,
    void *mem, const cache_bin_info_t *tcache_bin_info) {
	tcache->tcache_slow = tcache_slow;
	tcache_slow->tcache = tcache;

	memset(&tcache_slow->link, 0, sizeof(ql_elm(tcache_t)));
	nstime_init_zero(&tcache_slow->last_gc_time);
	tcache_slow->next_gc_bin = 0;
	tcache_slow->next_gc_bin_small = 0;
	tcache_slow->next_gc_bin_large = SC_NBINS;
	tcache_slow->arena = NULL;
	tcache_slow->dyn_alloc = mem;

	/*
	 * We reserve cache bins for all small size classes, even if some may
	 * not get used (i.e. bins higher than tcache_nbins).  This allows
	 * the fast and common paths to access cache bin metadata safely w/o
	 * worrying about which ones are disabled.
	 */
	unsigned tcache_nbins = tcache_nbins_get(tcache_slow);
	size_t cur_offset = 0;
	cache_bin_preincrement(tcache_bin_info, tcache_nbins, mem,
	    &cur_offset);
	for (unsigned i = 0; i < tcache_nbins; i++) {
		if (i < SC_NBINS) {
			tcache_bin_fill_ctl_init(tcache_slow, i);
			tcache_slow->bin_refilled[i] = false;
			tcache_slow->bin_flush_delay_items[i]
			    = tcache_gc_item_delay_compute(i);
		}
		cache_bin_t *cache_bin = &tcache->bins[i];
		if (tcache_bin_info[i].ncached_max > 0) {
			cache_bin_init(cache_bin, &tcache_bin_info[i], mem,
			    &cur_offset);
		} else {
			cache_bin_init_disabled(cache_bin,
			    tcache_bin_info[i].ncached_max);
		}
	}
	/*
	 * Initialize all disabled bins to a state that can safely and
	 * efficiently fail all fastpath alloc / free, so that no additional
	 * check around tcache_nbins is needed on fastpath.  Yet we still
	 * store the ncached_max in the bin_info for future usage.
	 */
	for (unsigned i = tcache_nbins; i < TCACHE_NBINS_MAX; i++) {
		cache_bin_t *cache_bin = &tcache->bins[i];
		cache_bin_init_disabled(cache_bin,
		    tcache_bin_info[i].ncached_max);
		assert(tcache_bin_disabled(i, cache_bin, tcache->tcache_slow));
	}

	cache_bin_postincrement(mem, &cur_offset);
	if (config_debug) {
		/* Sanity check that the whole stack is used. */
		size_t size, alignment;
		cache_bin_info_compute_alloc(tcache_bin_info, tcache_nbins,
		    &size, &alignment);
		assert(cur_offset == size);
	}
}

static inline unsigned
tcache_ncached_max_compute(szind_t szind) {
	if (szind >= SC_NBINS) {
		return opt_tcache_nslots_large;
	}
	unsigned slab_nregs = bin_infos[szind].nregs;

	/* We may modify these values; start with the opt versions. */
	unsigned nslots_small_min = opt_tcache_nslots_small_min;
	unsigned nslots_small_max = opt_tcache_nslots_small_max;

	/*
	 * Clamp values to meet our constraints -- even, nonzero, min < max, and
	 * suitable for a cache bin size.
	 */
	if (opt_tcache_nslots_small_max > CACHE_BIN_NCACHED_MAX) {
		nslots_small_max = CACHE_BIN_NCACHED_MAX;
	}
	if (nslots_small_min % 2 != 0) {
		nslots_small_min++;
	}
	if (nslots_small_max % 2 != 0) {
		nslots_small_max--;
	}
	if (nslots_small_min < 2) {
		nslots_small_min = 2;
	}
	if (nslots_small_max < 2) {
		nslots_small_max = 2;
	}
	if (nslots_small_min > nslots_small_max) {
		nslots_small_min = nslots_small_max;
	}

	unsigned candidate;
	if (opt_lg_tcache_nslots_mul < 0) {
		candidate = slab_nregs >> (-opt_lg_tcache_nslots_mul);
	} else {
		candidate = slab_nregs << opt_lg_tcache_nslots_mul;
	}
	if (candidate % 2 != 0) {
		/*
		 * We need the candidate size to be even -- we assume that we
		 * can divide by two and get a positive number (e.g. when
		 * flushing).
		 */
		++candidate;
	}
	if (candidate <= nslots_small_min) {
		return nslots_small_min;
	} else if (candidate <= nslots_small_max) {
		return candidate;
	} else {
		return nslots_small_max;
	}
}

JET_EXTERN void
tcache_bin_info_compute(cache_bin_info_t tcache_bin_info[TCACHE_NBINS_MAX]) {
	/*
	 * Compute the values for each bin, but for bins with indices larger
	 * than tcache_nbins, no items will be cached.
	 */
	for (szind_t i = 0; i < TCACHE_NBINS_MAX; i++) {
		unsigned ncached_max = tcache_get_default_ncached_max_set(i) ?
		    (unsigned)tcache_get_default_ncached_max()[i].ncached_max:
		    tcache_ncached_max_compute(i);
		assert(ncached_max <= CACHE_BIN_NCACHED_MAX);
		cache_bin_info_init(&tcache_bin_info[i],
		    (cache_bin_sz_t)ncached_max);
	}
}

static bool
tsd_tcache_data_init_impl(tsd_t *tsd, arena_t *arena,
    const cache_bin_info_t *tcache_bin_info) {
	tcache_slow_t *tcache_slow = tsd_tcache_slowp_get_unsafe(tsd);
	tcache_t *tcache = tsd_tcachep_get_unsafe(tsd);

	assert(cache_bin_still_zero_initialized(&tcache->bins[0]));
	unsigned tcache_nbins = tcache_nbins_get(tcache_slow);
	size_t size, alignment;
	cache_bin_info_compute_alloc(tcache_bin_info, tcache_nbins,
	    &size, &alignment);

	void *mem;
	if (cache_bin_stack_use_thp()) {
		/* Alignment is ignored since it comes from THP. */
		assert(alignment == QUANTUM);
		mem = b0_alloc_tcache_stack(tsd_tsdn(tsd), size);
	} else {
		size = sz_sa2u(size, alignment);
		mem = ipallocztm(tsd_tsdn(tsd), size, alignment, true, NULL,
		    true, arena_get(TSDN_NULL, 0, true));
	}
	if (mem == NULL) {
		return true;
	}

	tcache_init(tsd, tcache_slow, tcache, mem, tcache_bin_info);
	/*
	 * Initialization is a bit tricky here.  After malloc init is done, all
	 * threads can rely on arena_choose and associate tcache accordingly.
	 * However, the thread that does actual malloc bootstrapping relies on
	 * functional tsd, and it can only rely on a0.  In that case, we
	 * associate its tcache to a0 temporarily, and later on
	 * arena_choose_hard() will re-associate properly.
	 */
	tcache_slow->arena = NULL;
	if (!malloc_initialized()) {
		/* If in initialization, assign to a0. */
		arena = arena_get(tsd_tsdn(tsd), 0, false);
		tcache_arena_associate(tsd_tsdn(tsd), tcache_slow, tcache,
		    arena);
	} else {
		if (arena == NULL) {
			arena = arena_choose(tsd, NULL);
		}
		/* This may happen if thread.tcache.enabled is used. */
		if (tcache_slow->arena == NULL) {
			tcache_arena_associate(tsd_tsdn(tsd), tcache_slow,
			    tcache, arena);
		}
	}
	assert(arena == tcache_slow->arena);

	return false;
}

/* Initialize auto tcache (embedded in TSD). */
static bool
tsd_tcache_data_init(tsd_t *tsd, arena_t *arena,
    const cache_bin_info_t tcache_bin_info[TCACHE_NBINS_MAX]) {
	assert(tcache_bin_info != NULL);
	return tsd_tcache_data_init_impl(tsd, arena, tcache_bin_info);
}

/* Created manual tcache for tcache.create mallctl. */
tcache_t *
tcache_create_explicit(tsd_t *tsd) {
	/*
	 * We place the cache bin stacks, then the tcache_t, then a pointer to
	 * the beginning of the whole allocation (for freeing).  The makes sure
	 * the cache bins have the requested alignment.
	 */
	unsigned tcache_nbins = global_do_not_change_tcache_nbins;
	size_t tcache_size, alignment;
	cache_bin_info_compute_alloc(tcache_get_default_ncached_max(),
	    tcache_nbins, &tcache_size, &alignment);

	size_t size = tcache_size + sizeof(tcache_t)
	    + sizeof(tcache_slow_t);
	/* Naturally align the pointer stacks. */
	size = PTR_CEILING(size);
	size = sz_sa2u(size, alignment);

	void *mem = ipallocztm(tsd_tsdn(tsd), size, alignment,
	    true, NULL, true, arena_get(TSDN_NULL, 0, true));
	if (mem == NULL) {
		return NULL;
	}
	tcache_t *tcache = (void *)((byte_t *)mem + tcache_size);
	tcache_slow_t *tcache_slow =
	    (void *)((byte_t *)mem + tcache_size + sizeof(tcache_t));
	tcache_default_settings_init(tcache_slow);
	tcache_init(tsd, tcache_slow, tcache, mem,
	    tcache_get_default_ncached_max());

	tcache_arena_associate(tsd_tsdn(tsd), tcache_slow, tcache,
	    arena_ichoose(tsd, NULL));

	return tcache;
}

bool
tsd_tcache_enabled_data_init(tsd_t *tsd) {
	/* Called upon tsd initialization. */
	tsd_tcache_enabled_set(tsd, opt_tcache);
	/*
	 * tcache is not available yet, but we need to set up its tcache_nbins
	 * in advance.
	 */
	tcache_default_settings_init(tsd_tcache_slowp_get(tsd));
	tsd_slow_update(tsd);

	if (opt_tcache) {
		/* Trigger tcache init. */
		tsd_tcache_data_init(tsd, NULL,
		    tcache_get_default_ncached_max());
	}

	return false;
}

void
tcache_enabled_set(tsd_t *tsd, bool enabled) {
	bool was_enabled = tsd_tcache_enabled_get(tsd);

	if (!was_enabled && enabled) {
		tsd_tcache_data_init(tsd, NULL,
		    tcache_get_default_ncached_max());
	} else if (was_enabled && !enabled) {
		tcache_cleanup(tsd);
	}
	/* Commit the state last.  Above calls check current state. */
	tsd_tcache_enabled_set(tsd, enabled);
	tsd_slow_update(tsd);
}

void
thread_tcache_max_set(tsd_t *tsd, size_t tcache_max) {
	assert(tcache_max <= TCACHE_MAXCLASS_LIMIT);
	assert(tcache_max == sz_s2u(tcache_max));
	tcache_t *tcache = tsd_tcachep_get(tsd);
	tcache_slow_t *tcache_slow = tcache->tcache_slow;
	cache_bin_info_t tcache_bin_info[TCACHE_NBINS_MAX] = {{0}};
	assert(tcache != NULL && tcache_slow != NULL);

	bool enabled = tcache_available(tsd);
	arena_t *assigned_arena JEMALLOC_CLANG_ANALYZER_SILENCE_INIT(NULL);
	if (enabled) {
		assigned_arena = tcache_slow->arena;
		/* Carry over the bin settings during the reboot. */
		tcache_bin_settings_backup(tcache, tcache_bin_info);
		/* Shutdown and reboot the tcache for a clean slate. */
		tcache_cleanup(tsd);
	}

	/*
	* Still set tcache_nbins of the tcache even if the tcache is not
	* available yet because the values are stored in tsd_t and are
	* always available for changing.
	*/
	tcache_max_set(tcache_slow, tcache_max);

	if (enabled) {
		tsd_tcache_data_init(tsd, assigned_arena, tcache_bin_info);
	}

	assert(tcache_nbins_get(tcache_slow) == sz_size2index(tcache_max) + 1);
}

static bool
tcache_bin_info_settings_parse(const char *bin_settings_segment_cur,
    size_t len_left, cache_bin_info_t tcache_bin_info[TCACHE_NBINS_MAX],
    bool bin_info_is_set[TCACHE_NBINS_MAX]) {
	do {
		size_t size_start, size_end;
		size_t ncached_max;
		bool err = multi_setting_parse_next(&bin_settings_segment_cur,
		    &len_left, &size_start, &size_end, &ncached_max);
		if (err) {
			return true;
		}
		if (size_end > TCACHE_MAXCLASS_LIMIT) {
			size_end = TCACHE_MAXCLASS_LIMIT;
		}
		if (size_start > TCACHE_MAXCLASS_LIMIT ||
		    size_start > size_end) {
			continue;
		}
		/* May get called before sz_init (during malloc_conf_init). */
		szind_t bin_start = sz_size2index_compute(size_start);
		szind_t bin_end = sz_size2index_compute(size_end);
		if (ncached_max > CACHE_BIN_NCACHED_MAX) {
			ncached_max = (size_t)CACHE_BIN_NCACHED_MAX;
		}
		for (szind_t i = bin_start; i <= bin_end; i++) {
			cache_bin_info_init(&tcache_bin_info[i],
			    (cache_bin_sz_t)ncached_max);
			if (bin_info_is_set != NULL) {
				bin_info_is_set[i] = true;
			}
		}
	} while (len_left > 0);

	return false;
}

bool
tcache_bin_info_default_init(const char *bin_settings_segment_cur,
    size_t len_left) {
	return tcache_bin_info_settings_parse(bin_settings_segment_cur,
	    len_left, opt_tcache_ncached_max, opt_tcache_ncached_max_set);
}


bool
tcache_bins_ncached_max_write(tsd_t *tsd, char *settings, size_t len) {
	assert(tcache_available(tsd));
	assert(len != 0);
	tcache_t *tcache = tsd_tcachep_get(tsd);
	assert(tcache != NULL);
	cache_bin_info_t tcache_bin_info[TCACHE_NBINS_MAX];
	tcache_bin_settings_backup(tcache, tcache_bin_info);

	if(tcache_bin_info_settings_parse(settings, len, tcache_bin_info,
	    NULL)) {
		return true;
	}

	arena_t *assigned_arena = tcache->tcache_slow->arena;
	tcache_cleanup(tsd);
	tsd_tcache_data_init(tsd, assigned_arena,
	    tcache_bin_info);

	return false;
}

static void
tcache_flush_cache(tsd_t *tsd, tcache_t *tcache) {
	tcache_slow_t *tcache_slow = tcache->tcache_slow;
	assert(tcache_slow->arena != NULL);

	for (unsigned i = 0; i < tcache_nbins_get(tcache_slow); i++) {
		cache_bin_t *cache_bin = &tcache->bins[i];
		if (tcache_bin_disabled(i, cache_bin, tcache_slow)) {
			continue;
		}
		if (i < SC_NBINS) {
			tcache_bin_flush_small(tsd, tcache, cache_bin, i, 0);
		} else {
			tcache_bin_flush_large(tsd, tcache, cache_bin, i, 0);
		}
		if (config_stats) {
			assert(cache_bin->tstats.nrequests == 0);
		}
	}
}

void
tcache_flush(tsd_t *tsd) {
	assert(tcache_available(tsd));
	tcache_flush_cache(tsd, tsd_tcachep_get(tsd));
}

static void
tcache_destroy(tsd_t *tsd, tcache_t *tcache, bool tsd_tcache) {
	tcache_slow_t *tcache_slow = tcache->tcache_slow;
	tcache_flush_cache(tsd, tcache);
	arena_t *arena = tcache_slow->arena;
	tcache_arena_dissociate(tsd_tsdn(tsd), tcache_slow, tcache);

	if (tsd_tcache) {
		cache_bin_t *cache_bin = &tcache->bins[0];
		cache_bin_assert_empty(cache_bin);
	}
	if (tsd_tcache && cache_bin_stack_use_thp()) {
		b0_dalloc_tcache_stack(tsd_tsdn(tsd), tcache_slow->dyn_alloc);
	} else {
		idalloctm(tsd_tsdn(tsd), tcache_slow->dyn_alloc, NULL, NULL,
		    true, true);
	}

	/*
	 * The deallocation and tcache flush above may not trigger decay since
	 * we are on the tcache shutdown path (potentially with non-nominal
	 * tsd).  Manually trigger decay to avoid pathological cases.  Also
	 * include arena 0 because the tcache array is allocated from it.
	 */
	arena_decay(tsd_tsdn(tsd), arena_get(tsd_tsdn(tsd), 0, false),
	    false, false);

	if (arena_nthreads_get(arena, false) == 0 &&
	    !background_thread_enabled()) {
		/* Force purging when no threads assigned to the arena anymore. */
		arena_decay(tsd_tsdn(tsd), arena,
		    /* is_background_thread */ false, /* all */ true);
	} else {
		arena_decay(tsd_tsdn(tsd), arena,
		    /* is_background_thread */ false, /* all */ false);
	}
}

/* For auto tcache (embedded in TSD) only. */
void
tcache_cleanup(tsd_t *tsd) {
	tcache_t *tcache = tsd_tcachep_get(tsd);
	if (!tcache_available(tsd)) {
		assert(tsd_tcache_enabled_get(tsd) == false);
		assert(cache_bin_still_zero_initialized(&tcache->bins[0]));
		return;
	}
	assert(tsd_tcache_enabled_get(tsd));
	assert(!cache_bin_still_zero_initialized(&tcache->bins[0]));

	tcache_destroy(tsd, tcache, true);
	/* Make sure all bins used are reinitialized to the clean state. */
	memset(tcache->bins, 0, sizeof(cache_bin_t) * TCACHE_NBINS_MAX);
}

void
tcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {
	cassert(config_stats);

	/* Merge and reset tcache stats. */
	for (unsigned i = 0; i < tcache_nbins_get(tcache->tcache_slow); i++) {
		cache_bin_t *cache_bin = &tcache->bins[i];
		if (tcache_bin_disabled(i, cache_bin, tcache->tcache_slow)) {
			continue;
		}
		if (i < SC_NBINS) {
			bin_t *bin = arena_bin_choose(tsdn, arena, i, NULL);
			malloc_mutex_lock(tsdn, &bin->lock);
			bin->stats.nrequests += cache_bin->tstats.nrequests;
			malloc_mutex_unlock(tsdn, &bin->lock);
		} else {
			arena_stats_large_flush_nrequests_add(tsdn,
			    &arena->stats, i, cache_bin->tstats.nrequests);
		}
		cache_bin->tstats.nrequests = 0;
	}
}

static bool
tcaches_create_prep(tsd_t *tsd, base_t *base) {
	bool err;

	malloc_mutex_assert_owner(tsd_tsdn(tsd), &tcaches_mtx);

	if (tcaches == NULL) {
		tcaches = base_alloc(tsd_tsdn(tsd), base,
		    sizeof(tcache_t *) * (MALLOCX_TCACHE_MAX+1), CACHELINE);
		if (tcaches == NULL) {
			err = true;
			goto label_return;
		}
	}

	if (tcaches_avail == NULL && tcaches_past > MALLOCX_TCACHE_MAX) {
		err = true;
		goto label_return;
	}

	err = false;
label_return:
	return err;
}

bool
tcaches_create(tsd_t *tsd, base_t *base, unsigned *r_ind) {
	witness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);

	bool err;

	malloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);

	if (tcaches_create_prep(tsd, base)) {
		err = true;
		goto label_return;
	}

	tcache_t *tcache = tcache_create_explicit(tsd);
	if (tcache == NULL) {
		err = true;
		goto label_return;
	}

	tcaches_t *elm;
	if (tcaches_avail != NULL) {
		elm = tcaches_avail;
		tcaches_avail = tcaches_avail->next;
		elm->tcache = tcache;
		*r_ind = (unsigned)(elm - tcaches);
	} else {
		elm = &tcaches[tcaches_past];
		elm->tcache = tcache;
		*r_ind = tcaches_past;
		tcaches_past++;
	}

	err = false;
label_return:
	malloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);
	witness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);
	return err;
}

static tcache_t *
tcaches_elm_remove(tsd_t *tsd, tcaches_t *elm, bool allow_reinit) {
	malloc_mutex_assert_owner(tsd_tsdn(tsd), &tcaches_mtx);

	if (elm->tcache == NULL) {
		return NULL;
	}
	tcache_t *tcache = elm->tcache;
	if (allow_reinit) {
		elm->tcache = TCACHES_ELM_NEED_REINIT;
	} else {
		elm->tcache = NULL;
	}

	if (tcache == TCACHES_ELM_NEED_REINIT) {
		return NULL;
	}
	return tcache;
}

void
tcaches_flush(tsd_t *tsd, unsigned ind) {
	malloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);
	tcache_t *tcache = tcaches_elm_remove(tsd, &tcaches[ind], true);
	malloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);
	if (tcache != NULL) {
		/* Destroy the tcache; recreate in tcaches_get() if needed. */
		tcache_destroy(tsd, tcache, false);
	}
}

void
tcaches_destroy(tsd_t *tsd, unsigned ind) {
	malloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);
	tcaches_t *elm = &tcaches[ind];
	tcache_t *tcache = tcaches_elm_remove(tsd, elm, false);
	elm->next = tcaches_avail;
	tcaches_avail = elm;
	malloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);
	if (tcache != NULL) {
		tcache_destroy(tsd, tcache, false);
	}
}

bool
tcache_boot(tsdn_t *tsdn, base_t *base) {
	global_do_not_change_tcache_maxclass = sz_s2u(opt_tcache_max);
	assert(global_do_not_change_tcache_maxclass <= TCACHE_MAXCLASS_LIMIT);
	global_do_not_change_tcache_nbins =
	    sz_size2index(global_do_not_change_tcache_maxclass) + 1;
	/*
	 * Pre-compute default bin info and store the results in
	 * opt_tcache_ncached_max. After the changes here,
	 * opt_tcache_ncached_max should not be modified and should always be
	 * accessed using tcache_get_default_ncached_max.
	 */
	tcache_bin_info_compute(opt_tcache_ncached_max);

	if (malloc_mutex_init(&tcaches_mtx, "tcaches", WITNESS_RANK_TCACHES,
	    malloc_mutex_rank_exclusive)) {
		return true;
	}

	return false;
}

void
tcache_prefork(tsdn_t *tsdn) {
	malloc_mutex_prefork(tsdn, &tcaches_mtx);
}

void
tcache_postfork_parent(tsdn_t *tsdn) {
	malloc_mutex_postfork_parent(tsdn, &tcaches_mtx);
}

void
tcache_postfork_child(tsdn_t *tsdn) {
	malloc_mutex_postfork_child(tsdn, &tcaches_mtx);
}

void tcache_assert_initialized(tcache_t *tcache) {
	assert(!cache_bin_still_zero_initialized(&tcache->bins[0]));
}