File: zend_gc.c

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (2286 lines) | stat: -rw-r--r-- 59,292 bytes parent folder | download
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
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.00 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.zend.com/license/2_00.txt.                                |
   | If you did not receive a copy of the Zend license and are unable to  |
   | obtain it through the world-wide-web, please send a note to          |
   | license@zend.com so we can mail you a copy immediately.              |
   +----------------------------------------------------------------------+
   | Authors: David Wang <planetbeing@gmail.com>                          |
   |          Dmitry Stogov <dmitry@php.net>                              |
   +----------------------------------------------------------------------+
*/

/**
 * zend_gc_collect_cycles
 * ======================
 *
 * Colors and its meaning
 * ----------------------
 *
 * BLACK  (GC_BLACK)   - In use or free.
 * GREY   (GC_GREY)    - Possible member of cycle.
 * WHITE  (GC_WHITE)   - Member of garbage cycle.
 * PURPLE (GC_PURPLE)  - Possible root of cycle.
 *
 * Colors described in the paper but not used
 * ------------------------------------------
 *
 * GREEN - Acyclic
 * RED   - Candidate cycle undergoing
 * ORANGE - Candidate cycle awaiting epoch boundary.
 *
 *
 * Flow
 * =====
 *
 * The garbage collect cycle starts from 'gc_mark_roots', which traverses the
 * possible roots, and calls mark_grey for roots are marked purple with
 * depth-first traverse.
 *
 * After all possible roots are traversed and marked,
 * gc_scan_roots will be called, and each root will be called with
 * gc_scan(root->ref)
 *
 * gc_scan checks the colors of possible members.
 *
 * If the node is marked as grey and the refcount > 0
 *    gc_scan_black will be called on that node to scan it's subgraph.
 * otherwise (refcount == 0), it marks the node white.
 *
 * A node MAY be added to possible roots when ZEND_UNSET_VAR happens or
 * zend_assign_to_variable is called only when possible garbage node is
 * produced.
 * gc_possible_root() will be called to add the nodes to possible roots.
 *
 *
 * For objects, we call their get_gc handler (by default 'zend_std_get_gc') to
 * get the object properties to scan.
 *
 *
 * @see http://researcher.watson.ibm.com/researcher/files/us-bacon/Bacon01Concurrent.pdf
 */
#include "zend.h"
#include "zend_API.h"
#include "zend_compile.h"
#include "zend_errors.h"
#include "zend_fibers.h"
#include "zend_hrtime.h"
#include "zend_portability.h"
#include "zend_types.h"
#include "zend_weakrefs.h"
#include "zend_string.h"

#ifndef GC_BENCH
# define GC_BENCH 0
#endif

#ifndef ZEND_GC_DEBUG
# define ZEND_GC_DEBUG 0
#endif

/* GC_INFO layout */
#define GC_ADDRESS  0x0fffffu
#define GC_COLOR    0x300000u

#define GC_BLACK    0x000000u /* must be zero */
#define GC_WHITE    0x100000u
#define GC_GREY     0x200000u
#define GC_PURPLE   0x300000u

/* Debug tracing */
#if ZEND_GC_DEBUG > 1
# define GC_TRACE(format, ...) fprintf(stderr, format "\n", ##__VA_ARGS__);
# define GC_TRACE_REF(ref, format, ...) \
	do { \
		gc_trace_ref((zend_refcounted *) ref); \
		fprintf(stderr, format "\n", ##__VA_ARGS__); \
	} while (0)
# define GC_TRACE_SET_COLOR(ref, color) \
	GC_TRACE_REF(ref, "->%s", gc_color_name(color))
#else
# define GC_TRACE_REF(ref, format, ...)
# define GC_TRACE_SET_COLOR(ref, new_color)
# define GC_TRACE(str)
#endif

/* GC_INFO access */
#define GC_REF_ADDRESS(ref) \
	(((GC_TYPE_INFO(ref)) & (GC_ADDRESS << GC_INFO_SHIFT)) >> GC_INFO_SHIFT)

#define GC_REF_COLOR(ref) \
	(((GC_TYPE_INFO(ref)) & (GC_COLOR << GC_INFO_SHIFT)) >> GC_INFO_SHIFT)

#define GC_REF_CHECK_COLOR(ref, color) \
	((GC_TYPE_INFO(ref) & (GC_COLOR << GC_INFO_SHIFT)) == ((color) << GC_INFO_SHIFT))

#define GC_REF_SET_INFO(ref, info) do { \
		GC_TYPE_INFO(ref) = \
			(GC_TYPE_INFO(ref) & (GC_TYPE_MASK | GC_FLAGS_MASK)) | \
			((info) << GC_INFO_SHIFT); \
	} while (0)

#define GC_REF_SET_COLOR(ref, c) do { \
		GC_TRACE_SET_COLOR(ref, c); \
		GC_TYPE_INFO(ref) = \
			(GC_TYPE_INFO(ref) & ~(GC_COLOR << GC_INFO_SHIFT)) | \
			((c) << GC_INFO_SHIFT); \
	} while (0)

#define GC_REF_SET_BLACK(ref) do { \
		GC_TRACE_SET_COLOR(ref, GC_BLACK); \
		GC_TYPE_INFO(ref) &= ~(GC_COLOR << GC_INFO_SHIFT); \
	} while (0)

#define GC_REF_SET_PURPLE(ref) do { \
		GC_TRACE_SET_COLOR(ref, GC_PURPLE); \
		GC_TYPE_INFO(ref) |= (GC_COLOR << GC_INFO_SHIFT); \
	} while (0)

/* bit stealing tags for gc_root_buffer.ref */
#define GC_BITS    0x3

#define GC_ROOT    0x0 /* possible root of circular garbage     */
#define GC_UNUSED  0x1 /* part of linked list of unused buffers */
#define GC_GARBAGE 0x2 /* garbage to delete                     */
#define GC_DTOR_GARBAGE 0x3 /* garbage on which only the dtor should be invoked */

#define GC_GET_PTR(ptr) \
	((void*)(((uintptr_t)(ptr)) & ~GC_BITS))

#define GC_IS_ROOT(ptr) \
	((((uintptr_t)(ptr)) & GC_BITS) == GC_ROOT)
#define GC_IS_UNUSED(ptr) \
	((((uintptr_t)(ptr)) & GC_BITS) == GC_UNUSED)
#define GC_IS_GARBAGE(ptr) \
	((((uintptr_t)(ptr)) & GC_BITS) == GC_GARBAGE)
#define GC_IS_DTOR_GARBAGE(ptr) \
	((((uintptr_t)(ptr)) & GC_BITS) == GC_DTOR_GARBAGE)

#define GC_MAKE_GARBAGE(ptr) \
	((void*)(((uintptr_t)(ptr)) | GC_GARBAGE))
#define GC_MAKE_DTOR_GARBAGE(ptr) \
	((void*)(((uintptr_t)(ptr)) | GC_DTOR_GARBAGE))

/* GC address conversion */
#define GC_IDX2PTR(idx)      (GC_G(buf) + (idx))
#define GC_PTR2IDX(ptr)      ((ptr) - GC_G(buf))

#define GC_IDX2LIST(idx)     ((void*)(uintptr_t)(((idx) * sizeof(void*)) | GC_UNUSED))
#define GC_LIST2IDX(list)    (((uint32_t)(uintptr_t)(list)) / sizeof(void*))

/* GC buffers */
#define GC_INVALID           0
#define GC_FIRST_ROOT        1

#define GC_DEFAULT_BUF_SIZE  (16 * 1024)
#define GC_BUF_GROW_STEP     (128 * 1024)

#define GC_MAX_UNCOMPRESSED  (512 * 1024)
#define GC_MAX_BUF_SIZE      0x40000000

#define GC_THRESHOLD_DEFAULT (10000 + GC_FIRST_ROOT)
#define GC_THRESHOLD_STEP    10000
#define GC_THRESHOLD_MAX     1000000000
#define GC_THRESHOLD_TRIGGER 100

/* GC flags */
#define GC_HAS_DESTRUCTORS  (1<<0)

/* Weak maps */
#define Z_FROM_WEAKMAP_KEY		(1<<0)
#define Z_FROM_WEAKMAP			(1<<1)

/* The WeakMap entry zv is reachable from roots by following the virtual
 * reference from the a WeakMap key to the entry */
#define GC_FROM_WEAKMAP_KEY(zv) \
	(Z_TYPE_INFO_P((zv)) & (Z_FROM_WEAKMAP_KEY << Z_TYPE_INFO_EXTRA_SHIFT))

#define GC_SET_FROM_WEAKMAP_KEY(zv) do {									   \
	zval *_z = (zv);														   \
	Z_TYPE_INFO_P(_z) = Z_TYPE_INFO_P(_z) | (Z_FROM_WEAKMAP_KEY << Z_TYPE_INFO_EXTRA_SHIFT); \
} while (0)

#define GC_UNSET_FROM_WEAKMAP_KEY(zv) do {									   \
	zval *_z = (zv);														   \
	Z_TYPE_INFO_P(_z) = Z_TYPE_INFO_P(_z) & ~(Z_FROM_WEAKMAP_KEY << Z_TYPE_INFO_EXTRA_SHIFT); \
} while (0)

/* The WeakMap entry zv is reachable from roots by following the reference from
 * the WeakMap */
#define GC_FROM_WEAKMAP(zv) \
	(Z_TYPE_INFO_P((zv)) & (Z_FROM_WEAKMAP << Z_TYPE_INFO_EXTRA_SHIFT))

#define GC_SET_FROM_WEAKMAP(zv) do {									       \
	zval *_z = (zv);														   \
	Z_TYPE_INFO_P(_z) = Z_TYPE_INFO_P(_z) | (Z_FROM_WEAKMAP << Z_TYPE_INFO_EXTRA_SHIFT); \
} while (0)

#define GC_UNSET_FROM_WEAKMAP(zv) do {										   \
	zval *_z = (zv);														   \
	Z_TYPE_INFO_P(_z) = Z_TYPE_INFO_P(_z) & ~(Z_FROM_WEAKMAP << Z_TYPE_INFO_EXTRA_SHIFT); \
} while (0)

/* unused buffers */
#define GC_HAS_UNUSED() \
	(GC_G(unused) != GC_INVALID)
#define GC_FETCH_UNUSED() \
	gc_fetch_unused()
#define GC_LINK_UNUSED(root) \
	gc_link_unused(root)

#define GC_HAS_NEXT_UNUSED_UNDER_THRESHOLD() \
	(GC_G(first_unused) < GC_G(gc_threshold))
#define GC_HAS_NEXT_UNUSED() \
	(GC_G(first_unused) != GC_G(buf_size))
#define GC_FETCH_NEXT_UNUSED() \
	gc_fetch_next_unused()

ZEND_API int (*gc_collect_cycles)(void);

typedef struct _gc_root_buffer {
	zend_refcounted  *ref;
} gc_root_buffer;

typedef struct _zend_gc_globals {
	gc_root_buffer   *buf;				/* preallocated arrays of buffers   */

	bool         gc_enabled;
	bool         gc_active;        /* GC currently running, forbid nested GC */
	bool         gc_protected;     /* GC protected, forbid root additions */
	bool         gc_full;

	uint32_t          unused;			/* linked list of unused buffers    */
	uint32_t          first_unused;		/* first unused buffer              */
	uint32_t          gc_threshold;     /* GC collection threshold          */
	uint32_t          buf_size;			/* size of the GC buffer            */
	uint32_t          num_roots;		/* number of roots in GC buffer     */

	uint32_t gc_runs;
	uint32_t collected;

	zend_hrtime_t activated_at;
	zend_hrtime_t collector_time;
	zend_hrtime_t dtor_time;
	zend_hrtime_t free_time;

	uint32_t dtor_idx;			/* root buffer index */
	uint32_t dtor_end;
	zend_fiber *dtor_fiber;
	bool dtor_fiber_running;

#if GC_BENCH
	uint32_t root_buf_length;
	uint32_t root_buf_peak;
	uint32_t zval_possible_root;
	uint32_t zval_buffered;
	uint32_t zval_remove_from_buffer;
	uint32_t zval_marked_grey;
#endif
} zend_gc_globals;

#ifdef ZTS
static int gc_globals_id;
static size_t gc_globals_offset;
#define GC_G(v) ZEND_TSRMG_FAST(gc_globals_offset, zend_gc_globals *, v)
#else
#define GC_G(v) (gc_globals.v)
static zend_gc_globals gc_globals;
#endif

#if GC_BENCH
# define GC_BENCH_INC(counter) GC_G(counter)++
# define GC_BENCH_DEC(counter) GC_G(counter)--
# define GC_BENCH_PEAK(peak, counter) do {		\
		if (GC_G(counter) > GC_G(peak)) {		\
			GC_G(peak) = GC_G(counter);			\
		}										\
	} while (0)
#else
# define GC_BENCH_INC(counter)
# define GC_BENCH_DEC(counter)
# define GC_BENCH_PEAK(peak, counter)
#endif


#define GC_STACK_SEGMENT_SIZE (((4096 - ZEND_MM_OVERHEAD) / sizeof(void*)) - 2)

typedef struct _gc_stack gc_stack;

struct _gc_stack {
	gc_stack        *prev;
	gc_stack        *next;
	zend_refcounted *data[GC_STACK_SEGMENT_SIZE];
};

#define GC_STACK_DCL(init) \
	gc_stack *_stack = init; \
	size_t    _top = 0;

#define GC_STACK_PUSH(ref) \
	gc_stack_push(&_stack, &_top, ref);

#define GC_STACK_POP() \
	gc_stack_pop(&_stack, &_top)

static zend_never_inline gc_stack* gc_stack_next(gc_stack *stack)
{
	if (UNEXPECTED(!stack->next)) {
		gc_stack *segment = emalloc(sizeof(gc_stack));
		segment->prev = stack;
		segment->next = NULL;
		stack->next = segment;
	}
	return stack->next;
}

static zend_always_inline void gc_stack_push(gc_stack **stack, size_t *top, zend_refcounted *ref)
{
	if (UNEXPECTED(*top == GC_STACK_SEGMENT_SIZE)) {
		(*stack) = gc_stack_next(*stack);
		(*top) = 0;
	}
	(*stack)->data[(*top)++] = ref;
}

static zend_always_inline zend_refcounted* gc_stack_pop(gc_stack **stack, size_t *top)
{
	if (UNEXPECTED((*top) == 0)) {
		if (!(*stack)->prev) {
			return NULL;
		} else {
			(*stack) = (*stack)->prev;
			(*top) = GC_STACK_SEGMENT_SIZE - 1;
			return (*stack)->data[GC_STACK_SEGMENT_SIZE - 1];
		}
	} else {
		return (*stack)->data[--(*top)];
	}
}

static void gc_stack_free(gc_stack *stack)
{
	gc_stack *p = stack->next;

	while (p) {
		stack = p->next;
		efree(p);
		p = stack;
	}
}

static zend_always_inline uint32_t gc_compress(uint32_t idx)
{
	if (EXPECTED(idx < GC_MAX_UNCOMPRESSED)) {
		return idx;
	}
	return (idx % GC_MAX_UNCOMPRESSED) | GC_MAX_UNCOMPRESSED;
}

static zend_always_inline gc_root_buffer* gc_decompress(zend_refcounted *ref, uint32_t idx)
{
	gc_root_buffer *root = GC_IDX2PTR(idx);

	if (EXPECTED(GC_GET_PTR(root->ref) == ref)) {
		return root;
	}

	while (1) {
		idx += GC_MAX_UNCOMPRESSED;
		ZEND_ASSERT(idx < GC_G(first_unused));
		root = GC_IDX2PTR(idx);
		if (GC_GET_PTR(root->ref) == ref) {
			return root;
		}
	}
}

static zend_always_inline uint32_t gc_fetch_unused(void)
{
	uint32_t idx;
	gc_root_buffer *root;

	ZEND_ASSERT(GC_HAS_UNUSED());
	idx = GC_G(unused);
	root = GC_IDX2PTR(idx);
	ZEND_ASSERT(GC_IS_UNUSED(root->ref));
	GC_G(unused) = GC_LIST2IDX(root->ref);
	return idx;
}

static zend_always_inline void gc_link_unused(gc_root_buffer *root)
{
	root->ref = GC_IDX2LIST(GC_G(unused));
	GC_G(unused) = GC_PTR2IDX(root);
}

static zend_always_inline uint32_t gc_fetch_next_unused(void)
{
	uint32_t idx;

	ZEND_ASSERT(GC_HAS_NEXT_UNUSED());
	idx = GC_G(first_unused);
	GC_G(first_unused) = GC_G(first_unused) + 1;
	return idx;
}

#if ZEND_GC_DEBUG > 1
static const char *gc_color_name(uint32_t color) {
	switch (color) {
		case GC_BLACK: return "black";
		case GC_WHITE: return "white";
		case GC_GREY: return "grey";
		case GC_PURPLE: return "purple";
		default: return "unknown";
	}
}
static void gc_trace_ref(zend_refcounted *ref) {
	if (GC_TYPE(ref) == IS_OBJECT) {
		zend_object *obj = (zend_object *) ref;
		fprintf(stderr, "[%p] rc=%d addr=%d %s object(%s)#%d ",
			ref, GC_REFCOUNT(ref), GC_REF_ADDRESS(ref),
			gc_color_name(GC_REF_COLOR(ref)),
			obj->ce->name->val, obj->handle);
	} else if (GC_TYPE(ref) == IS_ARRAY) {
		zend_array *arr = (zend_array *) ref;
		fprintf(stderr, "[%p] rc=%d addr=%d %s array(%d) ",
			ref, GC_REFCOUNT(ref), GC_REF_ADDRESS(ref),
			gc_color_name(GC_REF_COLOR(ref)),
			zend_hash_num_elements(arr));
	} else {
		fprintf(stderr, "[%p] rc=%d addr=%d %s %s ",
			ref, GC_REFCOUNT(ref), GC_REF_ADDRESS(ref),
			gc_color_name(GC_REF_COLOR(ref)),
			GC_TYPE(ref) == IS_REFERENCE
				? "reference" : zend_get_type_by_const(GC_TYPE(ref)));
	}
}
#endif

static zend_always_inline void gc_remove_from_roots(gc_root_buffer *root)
{
	GC_LINK_UNUSED(root);
	GC_G(num_roots)--;
	GC_BENCH_DEC(root_buf_length);
}

static void root_buffer_dtor(zend_gc_globals *gc_globals)
{
	if (gc_globals->buf) {
		free(gc_globals->buf);
		gc_globals->buf = NULL;
	}
}

static void gc_globals_ctor_ex(zend_gc_globals *gc_globals)
{
	gc_globals->gc_enabled = 0;
	gc_globals->gc_active = 0;
	gc_globals->gc_protected = 1;
	gc_globals->gc_full = 0;

	gc_globals->buf = NULL;
	gc_globals->unused = GC_INVALID;
	gc_globals->first_unused = GC_INVALID;
	gc_globals->gc_threshold = GC_INVALID;
	gc_globals->buf_size = GC_INVALID;
	gc_globals->num_roots = 0;

	gc_globals->gc_runs = 0;
	gc_globals->collected = 0;
	gc_globals->collector_time = 0;
	gc_globals->dtor_time = 0;
	gc_globals->free_time = 0;
	gc_globals->activated_at = 0;

	gc_globals->dtor_idx = GC_FIRST_ROOT;
	gc_globals->dtor_end = 0;
	gc_globals->dtor_fiber = NULL;
	gc_globals->dtor_fiber_running = false;

#if GC_BENCH
	gc_globals->root_buf_length = 0;
	gc_globals->root_buf_peak = 0;
	gc_globals->zval_possible_root = 0;
	gc_globals->zval_buffered = 0;
	gc_globals->zval_remove_from_buffer = 0;
	gc_globals->zval_marked_grey = 0;
#endif
}

void gc_globals_ctor(void)
{
#ifdef ZTS
	ts_allocate_fast_id(&gc_globals_id, &gc_globals_offset, sizeof(zend_gc_globals), (ts_allocate_ctor) gc_globals_ctor_ex, (ts_allocate_dtor) root_buffer_dtor);
#else
	gc_globals_ctor_ex(&gc_globals);
#endif
}

void gc_globals_dtor(void)
{
#ifndef ZTS
	root_buffer_dtor(&gc_globals);
#endif
}

void gc_reset(void)
{
	if (GC_G(buf)) {
		GC_G(gc_active) = 0;
		GC_G(gc_protected) = 0;
		GC_G(gc_full) = 0;
		GC_G(unused) = GC_INVALID;
		GC_G(first_unused) = GC_FIRST_ROOT;
		GC_G(num_roots) = 0;

		GC_G(gc_runs) = 0;
		GC_G(collected) = 0;

		GC_G(collector_time) = 0;
		GC_G(dtor_time) = 0;
		GC_G(free_time) = 0;

		GC_G(dtor_idx) = GC_FIRST_ROOT;
		GC_G(dtor_end) = 0;
		GC_G(dtor_fiber) = NULL;
		GC_G(dtor_fiber_running) = false;

#if GC_BENCH
		GC_G(root_buf_length) = 0;
		GC_G(root_buf_peak) = 0;
		GC_G(zval_possible_root) = 0;
		GC_G(zval_buffered) = 0;
		GC_G(zval_remove_from_buffer) = 0;
		GC_G(zval_marked_grey) = 0;
#endif
	}

	GC_G(activated_at) = zend_hrtime();
}

ZEND_API bool gc_enable(bool enable)
{
	bool old_enabled = GC_G(gc_enabled);
	GC_G(gc_enabled) = enable;
	if (enable && !old_enabled && GC_G(buf) == NULL) {
		GC_G(buf) = (gc_root_buffer*) pemalloc(sizeof(gc_root_buffer) * GC_DEFAULT_BUF_SIZE, 1);
		GC_G(buf)[0].ref = NULL;
		GC_G(buf_size) = GC_DEFAULT_BUF_SIZE;
		GC_G(gc_threshold) = GC_THRESHOLD_DEFAULT;
		gc_reset();
	}
	return old_enabled;
}

ZEND_API bool gc_enabled(void)
{
	return GC_G(gc_enabled);
}

ZEND_API bool gc_protect(bool protect)
{
	bool old_protected = GC_G(gc_protected);
	GC_G(gc_protected) = protect;
	return old_protected;
}

ZEND_API bool gc_protected(void)
{
	return GC_G(gc_protected);
}

static void gc_grow_root_buffer(void)
{
	size_t new_size;

	if (GC_G(buf_size) >= GC_MAX_BUF_SIZE) {
		if (!GC_G(gc_full)) {
			zend_error(E_WARNING, "GC buffer overflow (GC disabled)\n");
			GC_G(gc_active) = 1;
			GC_G(gc_protected) = 1;
			GC_G(gc_full) = 1;
			return;
		}
	}
	if (GC_G(buf_size) < GC_BUF_GROW_STEP) {
		new_size = GC_G(buf_size) * 2;
	} else {
		new_size = GC_G(buf_size) + GC_BUF_GROW_STEP;
	}
	if (new_size > GC_MAX_BUF_SIZE) {
		new_size = GC_MAX_BUF_SIZE;
	}
	GC_G(buf) = perealloc(GC_G(buf), sizeof(gc_root_buffer) * new_size, 1);
	GC_G(buf_size) = new_size;
}

static void gc_adjust_threshold(int count)
{
	uint32_t new_threshold;

	/* TODO Very simple heuristic for dynamic GC buffer resizing:
	 * If there are "too few" collections, increase the collection threshold
	 * by a fixed step */
	if (count < GC_THRESHOLD_TRIGGER || GC_G(num_roots) >= GC_G(gc_threshold)) {
		/* increase */
		if (GC_G(gc_threshold) < GC_THRESHOLD_MAX) {
			new_threshold = GC_G(gc_threshold) + GC_THRESHOLD_STEP;
			if (new_threshold > GC_THRESHOLD_MAX) {
				new_threshold = GC_THRESHOLD_MAX;
			}
			if (new_threshold > GC_G(buf_size)) {
				gc_grow_root_buffer();
			}
			if (new_threshold <= GC_G(buf_size)) {
				GC_G(gc_threshold) = new_threshold;
			}
		}
	} else if (GC_G(gc_threshold) > GC_THRESHOLD_DEFAULT) {
		new_threshold = GC_G(gc_threshold) - GC_THRESHOLD_STEP;
		if (new_threshold < GC_THRESHOLD_DEFAULT) {
			new_threshold = GC_THRESHOLD_DEFAULT;
		}
		GC_G(gc_threshold) = new_threshold;
	}
}

static zend_never_inline void ZEND_FASTCALL gc_possible_root_when_full(zend_refcounted *ref)
{
	uint32_t idx;
	gc_root_buffer *newRoot;

	ZEND_ASSERT(GC_TYPE(ref) == IS_ARRAY || GC_TYPE(ref) == IS_OBJECT);
	ZEND_ASSERT(GC_INFO(ref) == 0);

	if (GC_G(gc_enabled) && !GC_G(gc_active)) {
		GC_ADDREF(ref);
		gc_adjust_threshold(gc_collect_cycles());
		if (UNEXPECTED(GC_DELREF(ref) == 0)) {
			rc_dtor_func(ref);
			return;
		} else if (UNEXPECTED(GC_INFO(ref))) {
			return;
		}
	}

	if (GC_HAS_UNUSED()) {
		idx = GC_FETCH_UNUSED();
	} else if (EXPECTED(GC_HAS_NEXT_UNUSED())) {
		idx = GC_FETCH_NEXT_UNUSED();
	} else {
		gc_grow_root_buffer();
		if (UNEXPECTED(!GC_HAS_NEXT_UNUSED())) {
			return;
		}
		idx = GC_FETCH_NEXT_UNUSED();
	}

	newRoot = GC_IDX2PTR(idx);
	newRoot->ref = ref; /* GC_ROOT tag is 0 */
	GC_TRACE_SET_COLOR(ref, GC_PURPLE);

	idx = gc_compress(idx);
	GC_REF_SET_INFO(ref, idx | GC_PURPLE);
	GC_G(num_roots)++;

	GC_BENCH_INC(zval_buffered);
	GC_BENCH_INC(root_buf_length);
	GC_BENCH_PEAK(root_buf_peak, root_buf_length);
}

ZEND_API void ZEND_FASTCALL gc_possible_root(zend_refcounted *ref)
{
	uint32_t idx;
	gc_root_buffer *newRoot;

	if (UNEXPECTED(GC_G(gc_protected))) {
		return;
	}

	GC_BENCH_INC(zval_possible_root);

	if (EXPECTED(GC_HAS_UNUSED())) {
		idx = GC_FETCH_UNUSED();
	} else if (EXPECTED(GC_HAS_NEXT_UNUSED_UNDER_THRESHOLD())) {
		idx = GC_FETCH_NEXT_UNUSED();
	} else {
		gc_possible_root_when_full(ref);
		return;
	}

	ZEND_ASSERT(GC_TYPE(ref) == IS_ARRAY || GC_TYPE(ref) == IS_OBJECT);
	ZEND_ASSERT(GC_INFO(ref) == 0);

	newRoot = GC_IDX2PTR(idx);
	newRoot->ref = ref; /* GC_ROOT tag is 0 */
	GC_TRACE_SET_COLOR(ref, GC_PURPLE);

	idx = gc_compress(idx);
	GC_REF_SET_INFO(ref, idx | GC_PURPLE);
	GC_G(num_roots)++;

	GC_BENCH_INC(zval_buffered);
	GC_BENCH_INC(root_buf_length);
	GC_BENCH_PEAK(root_buf_peak, root_buf_length);
}

static void ZEND_FASTCALL gc_extra_root(zend_refcounted *ref)
{
	uint32_t idx;
	gc_root_buffer *newRoot;

	if (EXPECTED(GC_HAS_UNUSED())) {
		idx = GC_FETCH_UNUSED();
	} else if (EXPECTED(GC_HAS_NEXT_UNUSED())) {
		idx = GC_FETCH_NEXT_UNUSED();
	} else {
		gc_grow_root_buffer();
		if (UNEXPECTED(!GC_HAS_NEXT_UNUSED())) {
			/* TODO: can this really happen? */
			return;
		}
		idx = GC_FETCH_NEXT_UNUSED();
	}

	ZEND_ASSERT(GC_TYPE(ref) == IS_ARRAY || GC_TYPE(ref) == IS_OBJECT);
	ZEND_ASSERT(GC_REF_ADDRESS(ref) == 0);

	newRoot = GC_IDX2PTR(idx);
	newRoot->ref = ref; /* GC_ROOT tag is 0 */

	idx = gc_compress(idx);
	GC_REF_SET_INFO(ref, idx | GC_REF_COLOR(ref));
	GC_G(num_roots)++;

	GC_BENCH_INC(zval_buffered);
	GC_BENCH_INC(root_buf_length);
	GC_BENCH_PEAK(root_buf_peak, root_buf_length);
}

static zend_never_inline void ZEND_FASTCALL gc_remove_compressed(zend_refcounted *ref, uint32_t idx)
{
	gc_root_buffer *root = gc_decompress(ref, idx);
	gc_remove_from_roots(root);
}

ZEND_API void ZEND_FASTCALL gc_remove_from_buffer(zend_refcounted *ref)
{
	gc_root_buffer *root;
	uint32_t idx = GC_REF_ADDRESS(ref);

	GC_BENCH_INC(zval_remove_from_buffer);

	if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
		GC_TRACE_SET_COLOR(ref, GC_BLACK);
	}
	GC_REF_SET_INFO(ref, 0);

	/* Perform decompression only in case of large buffers */
	if (UNEXPECTED(GC_G(first_unused) >= GC_MAX_UNCOMPRESSED)) {
		gc_remove_compressed(ref, idx);
		return;
	}

	ZEND_ASSERT(idx);
	root = GC_IDX2PTR(idx);
	gc_remove_from_roots(root);
}

static void gc_scan_black(zend_refcounted *ref, gc_stack *stack)
{
	HashTable *ht;
	Bucket *p;
	zval *zv;
	uint32_t n;
	GC_STACK_DCL(stack);

tail_call:
	if (GC_TYPE(ref) == IS_OBJECT) {
		zend_object *obj = (zend_object*)ref;

		if (EXPECTED(!(OBJ_FLAGS(ref) & IS_OBJ_FREE_CALLED))) {
			zval *table;
			int len;

			if (UNEXPECTED(GC_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)) {
				zend_weakmap_get_object_key_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n-=2) {
					ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
					zval *entry = (zval*) Z_PTR_P(zv);
					zval *weakmap = zv+1;
					ZEND_ASSERT(Z_REFCOUNTED_P(weakmap));
					if (Z_OPT_REFCOUNTED_P(entry)) {
						GC_UNSET_FROM_WEAKMAP_KEY(entry);
						if (GC_REF_CHECK_COLOR(Z_COUNTED_P(weakmap), GC_GREY)) {
							/* Weakmap was scanned in gc_mark_roots, we must
							 * ensure that it's eventually scanned in
							 * gc_scan_roots as well. */
							if (!GC_REF_ADDRESS(Z_COUNTED_P(weakmap))) {
								gc_extra_root(Z_COUNTED_P(weakmap));
							}
						} else if (/* GC_REF_CHECK_COLOR(Z_COUNTED_P(weakmap), GC_BLACK) && */ !GC_FROM_WEAKMAP(entry)) {
							/* Both the entry weakmap and key are BLACK, so we
							 * can mark the entry BLACK as well.
							 * !GC_FROM_WEAKMAP(entry) means that the weakmap
							 * was already scanned black (or will not be
							 * scanned), so it's our responsibility to mark the
							 * entry */
							ZEND_ASSERT(GC_REF_CHECK_COLOR(Z_COUNTED_P(weakmap), GC_BLACK));
							ref = Z_COUNTED_P(entry);
							GC_ADDREF(ref);
							if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
								GC_REF_SET_BLACK(ref);
								GC_STACK_PUSH(ref);
							}
						}
					}
					zv+=2;
				}
			}

			if (UNEXPECTED(obj->handlers->get_gc == zend_weakmap_get_gc)) {
				zend_weakmap_get_key_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n-=2) {
					ZEND_ASSERT(Z_TYPE_P(zv+1) == IS_PTR);
					zval *key = zv;
					zval *entry = (zval*) Z_PTR_P(zv+1);
					if (Z_OPT_REFCOUNTED_P(entry)) {
						GC_UNSET_FROM_WEAKMAP(entry);
						if (GC_REF_CHECK_COLOR(Z_COUNTED_P(key), GC_GREY)) {
							/* Key was scanned in gc_mark_roots, we must
							 * ensure that it's eventually scanned in
							 * gc_scan_roots as well. */
							if (!GC_REF_ADDRESS(Z_COUNTED_P(key))) {
								gc_extra_root(Z_COUNTED_P(key));
							}
						} else if (/* GC_REF_CHECK_COLOR(Z_COUNTED_P(key), GC_BLACK) && */ !GC_FROM_WEAKMAP_KEY(entry)) {
							/* Both the entry weakmap and key are BLACK, so we
							 * can mark the entry BLACK as well.
							 * !GC_FROM_WEAKMAP_KEY(entry) means that the key
							 * was already scanned black (or will not be
							 * scanned), so it's our responsibility to mark the
							 * entry */
							ZEND_ASSERT(GC_REF_CHECK_COLOR(Z_COUNTED_P(key), GC_BLACK));
							ref = Z_COUNTED_P(entry);
							GC_ADDREF(ref);
							if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
								GC_REF_SET_BLACK(ref);
								GC_STACK_PUSH(ref);
							}
						}
					}
					zv += 2;
				}
				goto next;
			}

			ht = obj->handlers->get_gc(obj, &table, &len);
			n = len;
			zv = table;
			if (UNEXPECTED(ht)) {
				GC_ADDREF(ht);
				if (!GC_REF_CHECK_COLOR(ht, GC_BLACK)) {
					GC_REF_SET_BLACK(ht);
					for (; n != 0; n--) {
						if (Z_REFCOUNTED_P(zv)) {
							ref = Z_COUNTED_P(zv);
							GC_ADDREF(ref);
							if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
								GC_REF_SET_BLACK(ref);
								GC_STACK_PUSH(ref);
							}
						}
						zv++;
					}
					goto handle_ht;
				}
			}

handle_zvals:
			for (; n != 0; n--) {
				if (Z_REFCOUNTED_P(zv)) {
					ref = Z_COUNTED_P(zv);
					GC_ADDREF(ref);
					if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
						GC_REF_SET_BLACK(ref);
						zv++;
						while (--n) {
							if (Z_REFCOUNTED_P(zv)) {
								zend_refcounted *ref = Z_COUNTED_P(zv);
								GC_ADDREF(ref);
								if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
									GC_REF_SET_BLACK(ref);
									GC_STACK_PUSH(ref);
								}
							}
							zv++;
						}
						goto tail_call;
					}
				}
				zv++;
			}
		}
	} else if (GC_TYPE(ref) == IS_ARRAY) {
		ZEND_ASSERT((zend_array*)ref != &EG(symbol_table));
		ht = (zend_array*)ref;
handle_ht:
		n = ht->nNumUsed;
		zv = ht->arPacked;
		if (HT_IS_PACKED(ht)) {
			goto handle_zvals;
		}

		p = (Bucket*)zv;
		for (; n != 0; n--) {
			zv = &p->val;
			if (Z_TYPE_P(zv) == IS_INDIRECT) {
				zv = Z_INDIRECT_P(zv);
			}
			if (Z_REFCOUNTED_P(zv)) {
				ref = Z_COUNTED_P(zv);
				GC_ADDREF(ref);
				if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
					GC_REF_SET_BLACK(ref);
					p++;
					while (--n) {
						zv = &p->val;
						if (Z_TYPE_P(zv) == IS_INDIRECT) {
							zv = Z_INDIRECT_P(zv);
						}
						if (Z_REFCOUNTED_P(zv)) {
							zend_refcounted *ref = Z_COUNTED_P(zv);
							GC_ADDREF(ref);
							if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
								GC_REF_SET_BLACK(ref);
								GC_STACK_PUSH(ref);
							}
						}
						p++;
					}
					goto tail_call;
				}
			}
			p++;
		}
	} else if (GC_TYPE(ref) == IS_REFERENCE) {
		if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
			ref = Z_COUNTED(((zend_reference*)ref)->val);
			GC_ADDREF(ref);
			if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
				GC_REF_SET_BLACK(ref);
				goto tail_call;
			}
		}
	}

next:
	ref = GC_STACK_POP();
	if (ref) {
		goto tail_call;
	}
}

static void gc_mark_grey(zend_refcounted *ref, gc_stack *stack)
{
	HashTable *ht;
	Bucket *p;
	zval *zv;
	uint32_t n;
	GC_STACK_DCL(stack);

tail_call:
	GC_BENCH_INC(zval_marked_grey);

	if (GC_TYPE(ref) == IS_OBJECT) {
		zend_object *obj = (zend_object*)ref;

		if (EXPECTED(!(OBJ_FLAGS(ref) & IS_OBJ_FREE_CALLED))) {
			zval *table;
			int len;

			if (UNEXPECTED(GC_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)) {
				zend_weakmap_get_object_key_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n-=2) {
					ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
					zval *entry = (zval*) Z_PTR_P(zv);
					zval *weakmap = zv+1;
					ZEND_ASSERT(Z_REFCOUNTED_P(weakmap));
					if (Z_REFCOUNTED_P(entry)) {
						GC_SET_FROM_WEAKMAP_KEY(entry);
						ref = Z_COUNTED_P(entry);
						/* Only DELREF if the contribution from the weakmap has
						 * not been cancelled yet */
						if (!GC_FROM_WEAKMAP(entry)) {
							GC_DELREF(ref);
						}
						if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
							GC_REF_SET_COLOR(ref, GC_GREY);
							GC_STACK_PUSH(ref);
						}
					}
					zv+=2;
				}
			}

			if (UNEXPECTED(obj->handlers->get_gc == zend_weakmap_get_gc)) {
				zend_weakmap_get_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n--) {
					ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
					zval *entry = (zval*) Z_PTR_P(zv);
					if (Z_REFCOUNTED_P(entry)) {
						GC_SET_FROM_WEAKMAP(entry);
						ref = Z_COUNTED_P(entry);
						/* Only DELREF if the contribution from the weakmap key
						 * has not been cancelled yet */
						if (!GC_FROM_WEAKMAP_KEY(entry)) {
							GC_DELREF(ref);
						}
						if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
							GC_REF_SET_COLOR(ref, GC_GREY);
							GC_STACK_PUSH(ref);
						}
					}
					zv++;
				}
				goto next;
			}

			ht = obj->handlers->get_gc(obj, &table, &len);
			n = len;
			zv = table;
			if (UNEXPECTED(ht)) {
				GC_DELREF(ht);
				if (!GC_REF_CHECK_COLOR(ht, GC_GREY)) {
					GC_REF_SET_COLOR(ht, GC_GREY);
					for (; n != 0; n--) {
						if (Z_REFCOUNTED_P(zv)) {
							ref = Z_COUNTED_P(zv);
							GC_DELREF(ref);
							if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
								GC_REF_SET_COLOR(ref, GC_GREY);
								GC_STACK_PUSH(ref);
							}
						}
						zv++;
					}
					goto handle_ht;
				}
			}
handle_zvals:
			for (; n != 0; n--) {
				if (Z_REFCOUNTED_P(zv)) {
					ref = Z_COUNTED_P(zv);
					GC_DELREF(ref);
					if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
						GC_REF_SET_COLOR(ref, GC_GREY);
						zv++;
						while (--n) {
							if (Z_REFCOUNTED_P(zv)) {
								zend_refcounted *ref = Z_COUNTED_P(zv);
								GC_DELREF(ref);
								if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
									GC_REF_SET_COLOR(ref, GC_GREY);
									GC_STACK_PUSH(ref);
								}
							}
							zv++;
						}
						goto tail_call;
					}
				}
				zv++;
			}
		}
	} else if (GC_TYPE(ref) == IS_ARRAY) {
		ZEND_ASSERT(((zend_array*)ref) != &EG(symbol_table));
		ht = (zend_array*)ref;
handle_ht:
		n = ht->nNumUsed;
		if (HT_IS_PACKED(ht)) {
            zv = ht->arPacked;
            goto handle_zvals;
		}

		p = ht->arData;
		for (; n != 0; n--) {
			zv = &p->val;
			if (Z_TYPE_P(zv) == IS_INDIRECT) {
				zv = Z_INDIRECT_P(zv);
			}
			if (Z_REFCOUNTED_P(zv)) {
				ref = Z_COUNTED_P(zv);
				GC_DELREF(ref);
				if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
					GC_REF_SET_COLOR(ref, GC_GREY);
					p++;
					while (--n) {
						zv = &p->val;
						if (Z_TYPE_P(zv) == IS_INDIRECT) {
							zv = Z_INDIRECT_P(zv);
						}
						if (Z_REFCOUNTED_P(zv)) {
							zend_refcounted *ref = Z_COUNTED_P(zv);
							GC_DELREF(ref);
							if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
								GC_REF_SET_COLOR(ref, GC_GREY);
								GC_STACK_PUSH(ref);
							}
						}
						p++;
					}
					goto tail_call;
				}
			}
			p++;
		}
	} else if (GC_TYPE(ref) == IS_REFERENCE) {
		if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
			ref = Z_COUNTED(((zend_reference*)ref)->val);
			GC_DELREF(ref);
			if (!GC_REF_CHECK_COLOR(ref, GC_GREY)) {
				GC_REF_SET_COLOR(ref, GC_GREY);
				goto tail_call;
			}
		}
	}

next:
	ref = GC_STACK_POP();
	if (ref) {
		goto tail_call;
	}
}

/* Two-Finger compaction algorithm */
static void gc_compact(void)
{
	if (GC_G(num_roots) + GC_FIRST_ROOT != GC_G(first_unused)) {
		if (GC_G(num_roots)) {
			gc_root_buffer *free = GC_IDX2PTR(GC_FIRST_ROOT);
			gc_root_buffer *scan = GC_IDX2PTR(GC_G(first_unused) - 1);
			gc_root_buffer *end  = GC_IDX2PTR(GC_G(num_roots));
			uint32_t idx;
			zend_refcounted *p;

			while (free < scan) {
				while (!GC_IS_UNUSED(free->ref)) {
					free++;
				}
				while (GC_IS_UNUSED(scan->ref)) {
					scan--;
				}
				if (scan > free) {
					p = scan->ref;
					free->ref = p;
					p = GC_GET_PTR(p);
					idx = gc_compress(GC_PTR2IDX(free));
					GC_REF_SET_INFO(p, idx | GC_REF_COLOR(p));
					free++;
					scan--;
					if (scan <= end) {
						break;
					}
				}
			}
		}
		GC_G(unused) = GC_INVALID;
		GC_G(first_unused) = GC_G(num_roots) + GC_FIRST_ROOT;
	}
}

static void gc_mark_roots(gc_stack *stack)
{
	gc_root_buffer *current, *last;

	gc_compact();

	current = GC_IDX2PTR(GC_FIRST_ROOT);
	last = GC_IDX2PTR(GC_G(first_unused));
	while (current != last) {
		if (GC_IS_ROOT(current->ref)) {
			if (GC_REF_CHECK_COLOR(current->ref, GC_PURPLE)) {
				GC_REF_SET_COLOR(current->ref, GC_GREY);
				gc_mark_grey(current->ref, stack);
			}
		}
		current++;
	}
}

static void gc_scan(zend_refcounted *ref, gc_stack *stack)
{
	HashTable *ht;
	Bucket *p;
	zval *zv;
	uint32_t n;
	GC_STACK_DCL(stack);

tail_call:
	if (!GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
		goto next;
	}

	if (GC_REFCOUNT(ref) > 0) {
		if (!GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
			GC_REF_SET_BLACK(ref);
			if (UNEXPECTED(!_stack->next)) {
				gc_stack_next(_stack);
			}
			/* Split stack and reuse the tail */
			_stack->next->prev = NULL;
			gc_scan_black(ref, _stack->next);
			_stack->next->prev = _stack;
		}
		goto next;
	}

	if (GC_TYPE(ref) == IS_OBJECT) {
		zend_object *obj = (zend_object*)ref;
		if (EXPECTED(!(OBJ_FLAGS(ref) & IS_OBJ_FREE_CALLED))) {
			zval *table;
			int len;

			if (UNEXPECTED(GC_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)) {
				zend_weakmap_get_object_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n--) {
					ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
					zval *entry = (zval*) Z_PTR_P(zv);
					if (Z_OPT_REFCOUNTED_P(entry)) {
						ref = Z_COUNTED_P(entry);
						if (GC_REF_CHECK_COLOR(ref, GC_GREY)) {
							GC_REF_SET_COLOR(ref, GC_WHITE);
							GC_STACK_PUSH(ref);
						}
					}
					zv++;
				}
			}

			ht = obj->handlers->get_gc(obj, &table, &len);
			n = len;
			zv = table;
			if (UNEXPECTED(ht)) {
				if (GC_REF_CHECK_COLOR(ht, GC_GREY)) {
					GC_REF_SET_COLOR(ht, GC_WHITE);
					GC_STACK_PUSH((zend_refcounted *) ht);
					for (; n != 0; n--) {
						if (Z_REFCOUNTED_P(zv)) {
							ref = Z_COUNTED_P(zv);
							if (GC_REF_CHECK_COLOR(ref, GC_GREY)) {
								GC_REF_SET_COLOR(ref, GC_WHITE);
								GC_STACK_PUSH(ref);
							}
						}
						zv++;
					}
					goto handle_ht;
				}
			}

handle_zvals:
			for (; n != 0; n--) {
				if (Z_REFCOUNTED_P(zv)) {
					ref = Z_COUNTED_P(zv);
					if (GC_REF_CHECK_COLOR(ref, GC_GREY)) {
						GC_REF_SET_COLOR(ref, GC_WHITE);
						zv++;
						while (--n) {
							if (Z_REFCOUNTED_P(zv)) {
								zend_refcounted *ref = Z_COUNTED_P(zv);
								if (GC_REF_CHECK_COLOR(ref, GC_GREY)) {
									GC_REF_SET_COLOR(ref, GC_WHITE);
									GC_STACK_PUSH(ref);
								}
							}
							zv++;
						}
						goto tail_call;
					}
				}
				zv++;
			}
		}
	} else if (GC_TYPE(ref) == IS_ARRAY) {
		ht = (HashTable *)ref;
		ZEND_ASSERT(ht != &EG(symbol_table));

handle_ht:
		n = ht->nNumUsed;
		if (HT_IS_PACKED(ht)) {
            zv = ht->arPacked;
            goto handle_zvals;
		}

		p = ht->arData;
		for (; n != 0; n--) {
			zv = &p->val;
			if (Z_TYPE_P(zv) == IS_INDIRECT) {
				zv = Z_INDIRECT_P(zv);
			}
			if (Z_REFCOUNTED_P(zv)) {
				ref = Z_COUNTED_P(zv);
				if (GC_REF_CHECK_COLOR(ref, GC_GREY)) {
					GC_REF_SET_COLOR(ref, GC_WHITE);
					p++;
					while (--n) {
						zv = &p->val;
						if (Z_TYPE_P(zv) == IS_INDIRECT) {
							zv = Z_INDIRECT_P(zv);
						}
						if (Z_REFCOUNTED_P(zv)) {
							zend_refcounted *ref = Z_COUNTED_P(zv);
							if (GC_REF_CHECK_COLOR(ref, GC_GREY)) {
								GC_REF_SET_COLOR(ref, GC_WHITE);
								GC_STACK_PUSH(ref);
							}
						}
						p++;
					}
					goto tail_call;
				}
			}
			p++;
		}
	} else if (GC_TYPE(ref) == IS_REFERENCE) {
		if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
			ref = Z_COUNTED(((zend_reference*)ref)->val);
			if (GC_REF_CHECK_COLOR(ref, GC_GREY)) {
				GC_REF_SET_COLOR(ref, GC_WHITE);
				goto tail_call;
			}
		}
	}

next:
	ref = GC_STACK_POP();
	if (ref) {
		goto tail_call;
	}
}

static void gc_scan_roots(gc_stack *stack)
{
	uint32_t idx, end;
	gc_root_buffer *current;

	/* Root buffer might be reallocated during gc_scan,
	 * make sure to reload pointers. */
	idx = GC_FIRST_ROOT;
	end = GC_G(first_unused);
	while (idx != end) {
		current = GC_IDX2PTR(idx);
		if (GC_IS_ROOT(current->ref)) {
			if (GC_REF_CHECK_COLOR(current->ref, GC_GREY)) {
				GC_REF_SET_COLOR(current->ref, GC_WHITE);
				gc_scan(current->ref, stack);
			}
		}
		idx++;
	}

	/* Scan extra roots added during gc_scan */
	while (idx != GC_G(first_unused)) {
		current = GC_IDX2PTR(idx);
		if (GC_IS_ROOT(current->ref)) {
			if (GC_REF_CHECK_COLOR(current->ref, GC_GREY)) {
				GC_REF_SET_COLOR(current->ref, GC_WHITE);
				gc_scan(current->ref, stack);
			}
		}
		idx++;
	}
}

static void gc_add_garbage(zend_refcounted *ref)
{
	uint32_t idx;
	gc_root_buffer *buf;

	if (GC_HAS_UNUSED()) {
		idx = GC_FETCH_UNUSED();
	} else if (GC_HAS_NEXT_UNUSED()) {
		idx = GC_FETCH_NEXT_UNUSED();
	} else {
		gc_grow_root_buffer();
		if (UNEXPECTED(!GC_HAS_NEXT_UNUSED())) {
			return;
		}
		idx = GC_FETCH_NEXT_UNUSED();
	}

	buf = GC_IDX2PTR(idx);
	buf->ref = GC_MAKE_GARBAGE(ref);

	idx = gc_compress(idx);
	GC_REF_SET_INFO(ref, idx | GC_BLACK);
	GC_G(num_roots)++;
}

static int gc_collect_white(zend_refcounted *ref, uint32_t *flags, gc_stack *stack)
{
	int count = 0;
	HashTable *ht;
	Bucket *p;
	zval *zv;
	uint32_t n;
	GC_STACK_DCL(stack);

tail_call:
	/* don't count references for compatibility ??? */
	if (GC_TYPE(ref) != IS_REFERENCE) {
		count++;
	}

	if (GC_TYPE(ref) == IS_OBJECT) {
		zend_object *obj = (zend_object*)ref;

		if (EXPECTED(!(OBJ_FLAGS(ref) & IS_OBJ_FREE_CALLED))) {
			int len;
			zval *table;

			/* optimization: color is GC_BLACK (0) */
			if (!GC_INFO(ref)) {
				gc_add_garbage(ref);
			}
			if (!(OBJ_FLAGS(obj) & IS_OBJ_DESTRUCTOR_CALLED)
			 && (obj->handlers->dtor_obj != zend_objects_destroy_object
			  || obj->ce->destructor != NULL)) {
				*flags |= GC_HAS_DESTRUCTORS;
			}

			if (UNEXPECTED(GC_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)) {
				zend_weakmap_get_object_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n--) {
					ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
					zval *entry = (zval*) Z_PTR_P(zv);
					if (Z_REFCOUNTED_P(entry) && GC_FROM_WEAKMAP_KEY(entry)) {
						GC_UNSET_FROM_WEAKMAP_KEY(entry);
						GC_UNSET_FROM_WEAKMAP(entry);
						ref = Z_COUNTED_P(entry);
						GC_ADDREF(ref);
						if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
							GC_REF_SET_BLACK(ref);
							GC_STACK_PUSH(ref);
						}
					}
					zv++;
				}
			}

			if (UNEXPECTED(obj->handlers->get_gc == zend_weakmap_get_gc)) {
				zend_weakmap_get_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n--) {
					ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
					zval *entry = (zval*) Z_PTR_P(zv);
					if (Z_REFCOUNTED_P(entry) && GC_FROM_WEAKMAP(entry)) {
						GC_UNSET_FROM_WEAKMAP_KEY(entry);
						GC_UNSET_FROM_WEAKMAP(entry);
						ref = Z_COUNTED_P(entry);
						GC_ADDREF(ref);
						if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
							GC_REF_SET_BLACK(ref);
							GC_STACK_PUSH(ref);
						}
					}
					zv++;
				}
				goto next;
			}

			ht = obj->handlers->get_gc(obj, &table, &len);
			n = len;
			zv = table;
			if (UNEXPECTED(ht)) {
				GC_ADDREF(ht);
				if (GC_REF_CHECK_COLOR(ht, GC_WHITE)) {
					GC_REF_SET_BLACK(ht);
					for (; n != 0; n--) {
						if (Z_REFCOUNTED_P(zv)) {
							ref = Z_COUNTED_P(zv);
							GC_ADDREF(ref);
							if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
								GC_REF_SET_BLACK(ref);
								GC_STACK_PUSH(ref);
							}
						}
						zv++;
					}
					goto handle_ht;
				}
			}

handle_zvals:
			for (; n != 0; n--) {
				if (Z_REFCOUNTED_P(zv)) {
					ref = Z_COUNTED_P(zv);
					GC_ADDREF(ref);
					if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
						GC_REF_SET_BLACK(ref);
						zv++;
						while (--n) {
							if (Z_REFCOUNTED_P(zv)) {
								zend_refcounted *ref = Z_COUNTED_P(zv);
								GC_ADDREF(ref);
								if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
									GC_REF_SET_BLACK(ref);
									GC_STACK_PUSH(ref);
								}
							}
							zv++;
						}
						goto tail_call;
					}
				}
				zv++;
			}
		}
	} else if (GC_TYPE(ref) == IS_ARRAY) {
		/* optimization: color is GC_BLACK (0) */
		if (!GC_INFO(ref)) {
			gc_add_garbage(ref);
		}
		ht = (zend_array*)ref;

handle_ht:
		n = ht->nNumUsed;
		if (HT_IS_PACKED(ht)) {
			zv = ht->arPacked;
			goto handle_zvals;
		}

		p = ht->arData;
		for (; n != 0; n--) {
			zv = &p->val;
			if (Z_TYPE_P(zv) == IS_INDIRECT) {
				zv = Z_INDIRECT_P(zv);
			}
			if (Z_REFCOUNTED_P(zv)) {
				ref = Z_COUNTED_P(zv);
				GC_ADDREF(ref);
				if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
					GC_REF_SET_BLACK(ref);
					p++;
					while (--n) {
						zv = &p->val;
						if (Z_TYPE_P(zv) == IS_INDIRECT) {
							zv = Z_INDIRECT_P(zv);
						}
						if (Z_REFCOUNTED_P(zv)) {
							zend_refcounted *ref = Z_COUNTED_P(zv);
							GC_ADDREF(ref);
							if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
								GC_REF_SET_BLACK(ref);
								GC_STACK_PUSH(ref);
							}
						}
						p++;
					}
					goto tail_call;
				}
			}
			p++;
		}
	} else if (GC_TYPE(ref) == IS_REFERENCE) {
		if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
			ref = Z_COUNTED(((zend_reference*)ref)->val);
			GC_ADDREF(ref);
			if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
				GC_REF_SET_BLACK(ref);
				goto tail_call;
			}
		}
	}

next:
	ref = GC_STACK_POP();
	if (ref) {
		goto tail_call;
	}

	return count;
}

static int gc_collect_roots(uint32_t *flags, gc_stack *stack)
{
	uint32_t idx, end;
	zend_refcounted *ref;
	int count = 0;
	gc_root_buffer *current = GC_IDX2PTR(GC_FIRST_ROOT);
	gc_root_buffer *last = GC_IDX2PTR(GC_G(first_unused));

	/* remove non-garbage from the list */
	while (current != last) {
		if (GC_IS_ROOT(current->ref)) {
			if (GC_REF_CHECK_COLOR(current->ref, GC_BLACK)) {
				GC_REF_SET_INFO(current->ref, 0); /* reset GC_ADDRESS() and keep GC_BLACK */
				gc_remove_from_roots(current);
			}
		}
		current++;
	}

	gc_compact();

	/* Root buffer might be reallocated during gc_collect_white,
	 * make sure to reload pointers. */
	idx = GC_FIRST_ROOT;
	end = GC_G(first_unused);
	while (idx != end) {
		current = GC_IDX2PTR(idx);
		ref = current->ref;
		ZEND_ASSERT(GC_IS_ROOT(ref));
		current->ref = GC_MAKE_GARBAGE(ref);
		if (GC_REF_CHECK_COLOR(ref, GC_WHITE)) {
			GC_REF_SET_BLACK(ref);
			count += gc_collect_white(ref, flags, stack);
		}
		idx++;
	}

	return count;
}

static int gc_remove_nested_data_from_buffer(zend_refcounted *ref, gc_root_buffer *root, gc_stack *stack)
{
	HashTable *ht;
	Bucket *p;
	zval *zv;
	uint32_t n;
	int count = 0;
	GC_STACK_DCL(stack);

tail_call:
	if (root) {
		root = NULL;
		count++;
	} else if (GC_REF_ADDRESS(ref) != 0
	 && GC_REF_CHECK_COLOR(ref, GC_BLACK)) {
		GC_TRACE_REF(ref, "removing from buffer");
		GC_REMOVE_FROM_BUFFER(ref);
		count++;
	} else if (GC_TYPE(ref) == IS_REFERENCE) {
		if (Z_REFCOUNTED(((zend_reference*)ref)->val)) {
			ref = Z_COUNTED(((zend_reference*)ref)->val);
			goto tail_call;
		}
		goto next;
	} else {
		goto next;
	}

	if (GC_TYPE(ref) == IS_OBJECT) {
		zend_object *obj = (zend_object*)ref;

		if (EXPECTED(!(OBJ_FLAGS(ref) & IS_OBJ_FREE_CALLED))) {
			int len;
			zval *table;

			if (UNEXPECTED(GC_FLAGS(obj) & IS_OBJ_WEAKLY_REFERENCED)) {
				zend_weakmap_get_object_entry_gc(obj, &table, &len);
				n = len;
				zv = table;
				for (; n != 0; n--) {
					ZEND_ASSERT(Z_TYPE_P(zv) == IS_PTR);
					zval *entry = (zval*) Z_PTR_P(zv);
					if (Z_OPT_REFCOUNTED_P(entry)) {
						ref = Z_COUNTED_P(entry);
						GC_STACK_PUSH(ref);
					}
					zv++;
				}
			}

			ht = obj->handlers->get_gc(obj, &table, &len);
			n = len;
			zv = table;
			if (UNEXPECTED(ht)) {
				for (; n != 0; n--) {
					if (Z_REFCOUNTED_P(zv)) {
						ref = Z_COUNTED_P(zv);
						GC_STACK_PUSH(ref);
					}
					zv++;
				}
				if (GC_REF_ADDRESS(ht) != 0 && GC_REF_CHECK_COLOR(ht, GC_BLACK)) {
					GC_TRACE_REF(ht, "removing from buffer");
					GC_REMOVE_FROM_BUFFER(ht);
				}
				goto handle_ht;
			}

handle_zvals:
			for (; n != 0; n--) {
				if (Z_REFCOUNTED_P(zv)) {
					ref = Z_COUNTED_P(zv);
					zv++;
					while (--n) {
						if (Z_REFCOUNTED_P(zv)) {
							zend_refcounted *ref = Z_COUNTED_P(zv);
							GC_STACK_PUSH(ref);
						}
						zv++;
					}
					goto tail_call;
				}
				zv++;
			}
		}
	} else if (GC_TYPE(ref) == IS_ARRAY) {
		ht = (zend_array*)ref;

handle_ht:
		n = ht->nNumUsed;
		if (HT_IS_PACKED(ht)) {
			zv = ht->arPacked;
			goto handle_zvals;
		}

		p = ht->arData;
		for (; n != 0; n--) {
			zv = &p->val;
			if (Z_TYPE_P(zv) == IS_INDIRECT) {
				zv = Z_INDIRECT_P(zv);
			}
			if (Z_REFCOUNTED_P(zv)) {
				ref = Z_COUNTED_P(zv);
				p++;
				while (--n) {
					zv = &p->val;
					if (Z_TYPE_P(zv) == IS_INDIRECT) {
						zv = Z_INDIRECT_P(zv);
					}
					if (Z_REFCOUNTED_P(zv)) {
						zend_refcounted *ref = Z_COUNTED_P(zv);
						GC_STACK_PUSH(ref);
					}
					p++;
				}
				goto tail_call;
			}
			p++;
		}
	}

next:
	ref = GC_STACK_POP();
	if (ref) {
		goto tail_call;
	}

	return count;
}

static void zend_get_gc_buffer_release(void);
static void zend_gc_check_root_tmpvars(void);
static void zend_gc_remove_root_tmpvars(void);

static zend_internal_function gc_destructor_fiber;

static ZEND_COLD ZEND_NORETURN void gc_create_destructor_fiber_error(void)
{
	zend_error_noreturn(E_ERROR, "Unable to create destructor fiber");
}

static ZEND_COLD ZEND_NORETURN void gc_start_destructor_fiber_error(void)
{
	zend_error_noreturn(E_ERROR, "Unable to start destructor fiber");
}

static zend_always_inline zend_result gc_call_destructors(uint32_t idx, uint32_t end, zend_fiber *fiber)
{
	gc_root_buffer *current;
	zend_refcounted *p;

	/* The root buffer might be reallocated during destructors calls,
	 * make sure to reload pointers as necessary. */
	while (idx != end) {
		current = GC_IDX2PTR(idx);
		if (GC_IS_DTOR_GARBAGE(current->ref)) {
			p = GC_GET_PTR(current->ref);
			/* Mark this is as a normal root for the next GC run */
			current->ref = p;
			/* Double check that the destructor hasn't been called yet. It
			 * could have already been invoked indirectly by some other
			 * destructor. */
			if (!(OBJ_FLAGS(p) & IS_OBJ_DESTRUCTOR_CALLED)) {
				if (fiber != NULL) {
					GC_G(dtor_idx) = idx;
				}
				zend_object *obj = (zend_object*)p;
				GC_TRACE_REF(obj, "calling destructor");
				GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
				GC_ADDREF(obj);
				obj->handlers->dtor_obj(obj);
				GC_TRACE_REF(obj, "returned from destructor");
				GC_DELREF(obj);
				if (UNEXPECTED(fiber != NULL && GC_G(dtor_fiber) != fiber)) {
					/* We resumed after suspension */
					gc_check_possible_root((zend_refcounted*)&obj->gc);
					return FAILURE;
				}
			}
		}
		idx++;
	}

	return SUCCESS;
}

static zend_fiber *gc_create_destructor_fiber(void)
{
	zval zobj;
	zend_fiber *fiber;

	GC_TRACE("starting destructor fiber");

	if (UNEXPECTED(object_init_ex(&zobj, zend_ce_fiber) == FAILURE)) {
		gc_create_destructor_fiber_error();
	}

	fiber = (zend_fiber *)Z_OBJ(zobj);
	fiber->fci.size = sizeof(fiber->fci);
	fiber->fci_cache.function_handler = (zend_function*) &gc_destructor_fiber;

	GC_G(dtor_fiber) = fiber;

	if (UNEXPECTED(zend_fiber_start(fiber, NULL) == FAILURE)) {
		gc_start_destructor_fiber_error();
	}

	return fiber;
}

static zend_never_inline void gc_call_destructors_in_fiber(uint32_t end)
{
	ZEND_ASSERT(!GC_G(dtor_fiber_running));

	zend_fiber *fiber = GC_G(dtor_fiber);

	GC_G(dtor_idx) = GC_FIRST_ROOT;
	GC_G(dtor_end) = GC_G(first_unused);

	if (UNEXPECTED(!fiber)) {
		fiber = gc_create_destructor_fiber();
	} else {
		zend_fiber_resume(fiber, NULL, NULL);
	}

	for (;;) {
		/* At this point, fiber has executed until suspension */
		GC_TRACE("resumed from destructor fiber");

		if (UNEXPECTED(GC_G(dtor_fiber_running))) {
			/* Fiber was suspended by a destructor. Start a new one for the
			 * remaining destructors. */
			GC_TRACE("destructor fiber suspended by destructor");
			GC_G(dtor_fiber) = NULL;
			GC_G(dtor_idx)++;
			/* We do not own the fiber anymore. It may be collected if the
			 * application does not reference it. */
			zend_object_release(&fiber->std);
			fiber = gc_create_destructor_fiber();
			continue;
		} else {
			/* Fiber suspended itself after calling all destructors */
			GC_TRACE("destructor fiber suspended itself");
			break;
		}
	}
}

ZEND_API int zend_gc_collect_cycles(void)
{
	int total_count = 0;
	bool should_rerun_gc = 0;
	bool did_rerun_gc = 0;

	zend_hrtime_t start_time = zend_hrtime();
	if (GC_G(num_roots) && !GC_G(gc_active)) {
		zend_gc_remove_root_tmpvars();
	}

rerun_gc:
	if (GC_G(num_roots)) {
		int count;
		gc_root_buffer *current, *last;
		zend_refcounted *p;
		uint32_t gc_flags = 0;
		uint32_t idx, end;
		gc_stack stack;

		stack.prev = NULL;
		stack.next = NULL;

		if (GC_G(gc_active)) {
			GC_G(collector_time) += zend_hrtime() - start_time;
			return 0;
		}

		GC_TRACE("Collecting cycles");
		GC_G(gc_runs)++;
		GC_G(gc_active) = 1;

		GC_TRACE("Marking roots");
		gc_mark_roots(&stack);
		GC_TRACE("Scanning roots");
		gc_scan_roots(&stack);

		GC_TRACE("Collecting roots");
		count = gc_collect_roots(&gc_flags, &stack);

		if (!GC_G(num_roots)) {
			/* nothing to free */
			GC_TRACE("Nothing to free");
			gc_stack_free(&stack);
			GC_G(gc_active) = 0;
			goto finish;
		}

		end = GC_G(first_unused);

		if (gc_flags & GC_HAS_DESTRUCTORS) {
			GC_TRACE("Calling destructors");

			/* During a destructor call, new externally visible references to nested data may
			 * be introduced. These references can be introduced in a way that does not
			 * modify any refcounts, so we have no real way to detect this situation
			 * short of rerunning full GC tracing. What we do instead is to only run
			 * destructors at this point and automatically re-run GC afterwards. */
			should_rerun_gc = 1;

			/* Mark all roots for which a dtor will be invoked as DTOR_GARBAGE. Additionally
			 * color them purple. This serves a double purpose: First, they should be
			 * considered new potential roots for the next GC run. Second, it will prevent
			 * their removal from the root buffer by nested data removal. */
			idx = GC_FIRST_ROOT;
			current = GC_IDX2PTR(GC_FIRST_ROOT);
			while (idx != end) {
				if (GC_IS_GARBAGE(current->ref)) {
					p = GC_GET_PTR(current->ref);
					if (GC_TYPE(p) == IS_OBJECT && !(OBJ_FLAGS(p) & IS_OBJ_DESTRUCTOR_CALLED)) {
						zend_object *obj = (zend_object *) p;
						if (obj->handlers->dtor_obj != zend_objects_destroy_object
							|| obj->ce->destructor) {
							current->ref = GC_MAKE_DTOR_GARBAGE(obj);
							GC_REF_SET_COLOR(obj, GC_PURPLE);
						} else {
							GC_ADD_FLAGS(obj, IS_OBJ_DESTRUCTOR_CALLED);
						}
					}
				}
				current++;
				idx++;
			}

			/* Remove nested data for objects on which a destructor will be called.
			 * This will not remove the objects themselves, as they have been colored
			 * purple. */
			idx = GC_FIRST_ROOT;
			current = GC_IDX2PTR(GC_FIRST_ROOT);
			while (idx != end) {
				if (GC_IS_DTOR_GARBAGE(current->ref)) {
					p = GC_GET_PTR(current->ref);
					count -= gc_remove_nested_data_from_buffer(p, current, &stack);
				}
				current++;
				idx++;
			}

			/* Actually call destructors. */
			zend_hrtime_t dtor_start_time = zend_hrtime();
			if (EXPECTED(!EG(active_fiber))) {
				gc_call_destructors(GC_FIRST_ROOT, end, NULL);
			} else {
				gc_call_destructors_in_fiber(end);
			}
			GC_G(dtor_time) += zend_hrtime() - dtor_start_time;

			if (GC_G(gc_protected)) {
				/* something went wrong */
				zend_get_gc_buffer_release();
				GC_G(collector_time) += zend_hrtime() - start_time;
				return 0;
			}
		}

		gc_stack_free(&stack);

		/* Destroy zvals. The root buffer may be reallocated. */
		GC_TRACE("Destroying zvals");
		zend_hrtime_t free_start_time = zend_hrtime();
		idx = GC_FIRST_ROOT;
		while (idx != end) {
			current = GC_IDX2PTR(idx);
			if (GC_IS_GARBAGE(current->ref)) {
				p = GC_GET_PTR(current->ref);
				GC_TRACE_REF(p, "destroying");
				if (GC_TYPE(p) == IS_OBJECT) {
					zend_object *obj = (zend_object*)p;

					EG(objects_store).object_buckets[obj->handle] = SET_OBJ_INVALID(obj);
					GC_TYPE_INFO(obj) = GC_NULL |
						(GC_TYPE_INFO(obj) & ~GC_TYPE_MASK);
					/* Modify current before calling free_obj (bug #78811: free_obj() can cause the root buffer (with current) to be reallocated.) */
					current->ref = GC_MAKE_GARBAGE(((char*)obj) - obj->handlers->offset);
					if (!(OBJ_FLAGS(obj) & IS_OBJ_FREE_CALLED)) {
						GC_ADD_FLAGS(obj, IS_OBJ_FREE_CALLED);
						GC_ADDREF(obj);
						obj->handlers->free_obj(obj);
						GC_DELREF(obj);
					}

					ZEND_OBJECTS_STORE_ADD_TO_FREE_LIST(obj->handle);
				} else if (GC_TYPE(p) == IS_ARRAY) {
					zend_array *arr = (zend_array*)p;

					GC_TYPE_INFO(arr) = GC_NULL |
						(GC_TYPE_INFO(arr) & ~GC_TYPE_MASK);

					/* GC may destroy arrays with rc>1. This is valid and safe. */
					HT_ALLOW_COW_VIOLATION(arr);

					zend_hash_destroy(arr);
				}
			}
			idx++;
		}

		/* Free objects */
		current = GC_IDX2PTR(GC_FIRST_ROOT);
		last = GC_IDX2PTR(end);
		while (current != last) {
			if (GC_IS_GARBAGE(current->ref)) {
				p = GC_GET_PTR(current->ref);
				GC_LINK_UNUSED(current);
				GC_G(num_roots)--;
				efree(p);
			}
			current++;
		}

		GC_G(free_time) += zend_hrtime() - free_start_time;

		GC_TRACE("Collection finished");
		GC_G(collected) += count;
		total_count += count;
		GC_G(gc_active) = 0;
	}

	gc_compact();

	/* Objects with destructors were removed from this GC run. Rerun GC right away to clean them
	 * up. We do this only once: If we encounter more destructors on the second run, we'll not
	 * run GC another time. */
	if (should_rerun_gc && !did_rerun_gc) {
		did_rerun_gc = 1;
		goto rerun_gc;
	}

finish:
	zend_get_gc_buffer_release();

	/* Prevent GC from running during zend_gc_check_root_tmpvars, before
	 * gc_threshold is adjusted, as this may result in unbounded recursion */
	GC_G(gc_active) = 1;
	zend_gc_check_root_tmpvars();
	GC_G(gc_active) = 0;

	GC_G(collector_time) += zend_hrtime() - start_time;
	return total_count;
}

ZEND_API void zend_gc_get_status(zend_gc_status *status)
{
	status->active = GC_G(gc_active);
	status->gc_protected = GC_G(gc_protected);
	status->full = GC_G(gc_full);
	status->runs = GC_G(gc_runs);
	status->collected = GC_G(collected);
	status->threshold = GC_G(gc_threshold);
	status->buf_size = GC_G(buf_size);
	status->num_roots = GC_G(num_roots);
	status->application_time = zend_hrtime() - GC_G(activated_at);
	status->collector_time = GC_G(collector_time);
	status->dtor_time = GC_G(dtor_time);
	status->free_time = GC_G(free_time);
}

ZEND_API zend_get_gc_buffer *zend_get_gc_buffer_create(void) {
	/* There can only be one get_gc() call active at a time,
	 * so there only needs to be one buffer. */
	zend_get_gc_buffer *gc_buffer = &EG(get_gc_buffer);
	gc_buffer->cur = gc_buffer->start;
	return gc_buffer;
}

ZEND_API void zend_get_gc_buffer_grow(zend_get_gc_buffer *gc_buffer) {
	size_t old_capacity = gc_buffer->end - gc_buffer->start;
	size_t new_capacity = old_capacity == 0 ? 64 : old_capacity * 2;
	gc_buffer->start = erealloc(gc_buffer->start, new_capacity * sizeof(zval));
	gc_buffer->end = gc_buffer->start + new_capacity;
	gc_buffer->cur = gc_buffer->start + old_capacity;
}

static void zend_get_gc_buffer_release(void) {
	zend_get_gc_buffer *gc_buffer = &EG(get_gc_buffer);
	efree(gc_buffer->start);
	gc_buffer->start = gc_buffer->end = gc_buffer->cur = NULL;
}

/* TMPVAR operands are destroyed using zval_ptr_dtor_nogc(), because they usually cannot contain
 * cycles. However, there are some rare exceptions where this is possible, in which case we rely
 * on the producing code to root the value. If a GC run occurs between the rooting and consumption
 * of the value, we would end up leaking it. To avoid this, root all live TMPVAR values here. */
static void zend_gc_check_root_tmpvars(void) {
	zend_execute_data *ex = EG(current_execute_data);
	for (; ex; ex = ex->prev_execute_data) {
		zend_function *func = ex->func;
		if (!func || !ZEND_USER_CODE(func->type)) {
			continue;
		}

		uint32_t op_num = ex->opline - ex->func->op_array.opcodes;
		for (uint32_t i = 0; i < func->op_array.last_live_range; i++) {
			const zend_live_range *range = &func->op_array.live_range[i];
			if (range->start > op_num) {
				break;
			}
			if (range->end <= op_num) {
				continue;
			}

			uint32_t kind = range->var & ZEND_LIVE_MASK;
			if (kind == ZEND_LIVE_TMPVAR || kind == ZEND_LIVE_LOOP) {
				uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
				zval *var = ZEND_CALL_VAR(ex, var_num);
				if (Z_REFCOUNTED_P(var)) {
					gc_check_possible_root(Z_COUNTED_P(var));
				}
			}
		}
	}
}

static void zend_gc_remove_root_tmpvars(void) {
	zend_execute_data *ex = EG(current_execute_data);
	for (; ex; ex = ex->prev_execute_data) {
		zend_function *func = ex->func;
		if (!func || !ZEND_USER_CODE(func->type)) {
			continue;
		}

		uint32_t op_num = ex->opline - ex->func->op_array.opcodes;
		for (uint32_t i = 0; i < func->op_array.last_live_range; i++) {
			const zend_live_range *range = &func->op_array.live_range[i];
			if (range->start > op_num) {
				break;
			}
			if (range->end <= op_num) {
				continue;
			}

			uint32_t kind = range->var & ZEND_LIVE_MASK;
			if (kind == ZEND_LIVE_TMPVAR || kind == ZEND_LIVE_LOOP) {
				uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
				zval *var = ZEND_CALL_VAR(ex, var_num);
				if (Z_REFCOUNTED_P(var)) {
					GC_REMOVE_FROM_BUFFER(Z_COUNTED_P(var));
				}
			}
		}
	}
}

#if GC_BENCH
void gc_bench_print(void)
{
	fprintf(stderr, "GC Statistics\n");
	fprintf(stderr, "-------------\n");
	fprintf(stderr, "Runs:               %d\n", GC_G(gc_runs));
	fprintf(stderr, "Collected:          %d\n", GC_G(collected));
	fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
	fprintf(stderr, "Root buffer peak:   %d\n\n", GC_G(root_buf_peak));
	fprintf(stderr, "      Possible            Remove from  Marked\n");
	fprintf(stderr, "        Root    Buffered     buffer     grey\n");
	fprintf(stderr, "      --------  --------  -----------  ------\n");
	fprintf(stderr, "ZVAL  %8d  %8d  %9d  %8d\n", GC_G(zval_possible_root), GC_G(zval_buffered), GC_G(zval_remove_from_buffer), GC_G(zval_marked_grey));
}
#endif

#ifdef ZTS
size_t zend_gc_globals_size(void)
{
	return sizeof(zend_gc_globals);
}
#endif

static ZEND_FUNCTION(gc_destructor_fiber)
{
	uint32_t idx, end;

	zend_fiber *fiber = GC_G(dtor_fiber);
	ZEND_ASSERT(fiber != NULL);
	ZEND_ASSERT(fiber == EG(active_fiber));

	for (;;) {
		GC_G(dtor_fiber_running) = true;

		idx = GC_G(dtor_idx);
		end = GC_G(dtor_end);
		if (UNEXPECTED(gc_call_destructors(idx, end, fiber) == FAILURE)) {
			/* We resumed after being suspended by a destructor */
			return;
		}

		/* We have called all destructors. Suspend fiber until the next GC run
		 */
		GC_G(dtor_fiber_running) = false;
		zend_fiber_suspend(fiber, NULL, NULL);

		if (UNEXPECTED(fiber->flags & ZEND_FIBER_FLAG_DESTROYED)) {
			/* Fiber is being destroyed by shutdown sequence */
			if (GC_G(dtor_fiber) == fiber) {
				GC_G(dtor_fiber) = NULL;
			}
			GC_DELREF(&fiber->std);
			gc_check_possible_root((zend_refcounted*)&fiber->std.gc);
			return;
		}
	}
}

static zend_internal_function gc_destructor_fiber = {
	.type = ZEND_INTERNAL_FUNCTION,
	.fn_flags = ZEND_ACC_PUBLIC,
	.handler = ZEND_FN(gc_destructor_fiber),
};

void gc_init(void)
{
	gc_destructor_fiber.function_name = zend_string_init_interned(
			"gc_destructor_fiber",
			strlen("gc_destructor_fiber"),
			true);
}