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
  
2287
  
2288
  
2289
  
2290
  
2291
  
2292
  
2293
  
2294
  
2295
  
2296
  
2297
  
2298
  
2299
  
2300
  
2301
  
2302
  
2303
  
2304
  
2305
  
2306
  
2307
  
2308
  
2309
  
2310
  
2311
  
2312
  
2313
  
2314
  
2315
  
2316
  
2317
  
2318
  
2319
  
2320
  
2321
  
2322
  
2323
  
2324
  
2325
  
2326
  
2327
  
2328
  
2329
  
2330
  
2331
  
2332
  
2333
  
2334
  
2335
  
2336
  
2337
  
2338
  
2339
  
2340
  
2341
  
2342
  
2343
  
2344
  
2345
  
2346
  
2347
  
2348
  
2349
  
2350
  
2351
  
2352
  
2353
  
2354
  
2355
  
2356
  
2357
  
2358
  
2359
  
2360
  
2361
  
2362
  
2363
  
2364
  
2365
  
2366
  
2367
  
2368
  
2369
  
2370
  
2371
  
2372
  
2373
  
2374
  
2375
  
2376
  
2377
  
2378
  
2379
  
2380
  
2381
  
2382
  
2383
  
2384
  
2385
  
2386
  
2387
  
2388
  
2389
  
2390
  
2391
  
2392
  
2393
  
2394
  
2395
  
2396
  
2397
  
2398
  
2399
  
2400
  
2401
  
2402
  
2403
  
2404
  
2405
  
2406
  
2407
  
2408
  
2409
  
2410
  
2411
  
2412
  
2413
  
2414
  
2415
  
2416
  
2417
  
2418
  
2419
  
2420
  
2421
  
2422
  
2423
  
2424
  
2425
  
2426
  
2427
  
2428
  
2429
  
2430
  
2431
  
2432
  
2433
  
2434
  
2435
  
2436
  
2437
  
2438
  
2439
  
2440
  
2441
  
2442
  
2443
  
2444
  
2445
  
2446
  
2447
  
2448
  
2449
  
2450
  
2451
  
2452
  
2453
  
2454
  
2455
  
2456
  
2457
  
2458
  
2459
  
2460
  
2461
  
2462
  
2463
  
2464
  
2465
  
2466
  
2467
  
2468
  
2469
  
2470
  
2471
  
2472
  
2473
  
2474
  
2475
  
2476
  
2477
  
2478
  
2479
  
2480
  
2481
  
2482
  
2483
  
2484
  
2485
  
2486
  
2487
  
2488
  
2489
  
2490
  
2491
  
2492
  
2493
  
2494
  
2495
  
2496
  
2497
  
2498
  
2499
  
2500
  
2501
  
2502
  
2503
  
2504
  
2505
  
2506
  
2507
  
2508
  
2509
  
2510
  
2511
  
2512
  
2513
  
2514
  
2515
  
2516
  
2517
  
2518
  
2519
  
2520
  
2521
  
2522
  
2523
  
2524
  
2525
  
2526
  
2527
  
2528
  
2529
  
2530
  
2531
  
2532
  
2533
  
2534
  
2535
  
2536
  
2537
  
2538
  
2539
  
2540
  
2541
  
2542
  
2543
  
2544
  
2545
  
2546
  
2547
  
2548
  
2549
  
2550
  
2551
  
2552
  
2553
  
2554
  
2555
  
2556
  
2557
  
2558
  
2559
  
2560
  
2561
  
2562
  
2563
  
2564
  
2565
  
2566
  
2567
  
2568
  
2569
  
2570
  
2571
  
2572
  
2573
  
2574
  
2575
  
2576
  
2577
  
2578
  
2579
  
2580
  
2581
  
2582
  
2583
  
2584
  
2585
  
2586
  
2587
  
2588
  
2589
  
2590
  
2591
  
2592
  
2593
  
2594
  
2595
  
2596
  
2597
  
2598
  
2599
  
2600
  
2601
  
2602
  
2603
  
2604
  
2605
  
2606
  
2607
  
2608
  
2609
  
2610
  
2611
  
2612
  
2613
  
2614
  
2615
  
2616
  
2617
  
2618
  
2619
  
2620
  
2621
  
2622
  
2623
  
2624
  
2625
  
2626
  
2627
  
2628
  
2629
  
2630
  
2631
  
2632
  
2633
  
2634
  
2635
  
2636
  
2637
  
2638
  
2639
  
2640
  
2641
  
2642
  
2643
  
2644
  
2645
  
2646
  
2647
  
2648
  
2649
  
2650
  
2651
  
2652
  
2653
  
2654
  
2655
  
2656
  
2657
  
2658
  
2659
  
2660
  
2661
  
2662
  
2663
  
2664
  
2665
  
2666
  
2667
  
2668
  
2669
  
2670
  
2671
  
2672
  
2673
  
2674
  
2675
  
2676
  
2677
  
2678
  
2679
  
2680
  
2681
  
2682
  
2683
  
2684
  
2685
  
2686
  
2687
  
2688
  
2689
  
2690
  
2691
  
2692
  
2693
  
2694
  
2695
  
2696
  
2697
  
2698
  
2699
  
2700
  
2701
  
2702
  
2703
  
2704
  
2705
  
2706
  
2707
  
2708
  
2709
  
2710
  
2711
  
2712
  
2713
  
2714
  
2715
  
2716
  
2717
  
2718
  
2719
  
2720
  
2721
  
2722
  
2723
  
2724
  
2725
  
2726
  
2727
  
2728
  
2729
  
2730
  
2731
  
2732
  
2733
  
2734
  
2735
  
2736
  
2737
  
2738
  
2739
  
2740
  
2741
  
2742
  
2743
  
2744
  
2745
  
2746
  
2747
  
2748
  
2749
  
2750
  
2751
  
2752
  
2753
  
2754
  
2755
  
2756
  
2757
  
2758
  
2759
  
2760
  
2761
  
2762
  
2763
  
2764
  
2765
  
2766
  
2767
  
2768
  
2769
  
2770
  
2771
  
2772
  
2773
  
2774
  
2775
  
2776
  
2777
  
2778
  
2779
  
2780
  
2781
  
2782
  
2783
  
2784
  
2785
  
2786
  
2787
  
2788
  
2789
  
2790
  
2791
  
2792
  
2793
  
2794
  
2795
  
2796
  
2797
  
2798
  
2799
  
2800
  
2801
  
2802
  
2803
  
2804
  
2805
  
2806
  
2807
  
2808
  
2809
  
2810
  
2811
  
2812
  
2813
  
2814
  
2815
  
2816
  
2817
  
2818
  
2819
  
2820
  
2821
  
2822
  
2823
  
2824
  
2825
  
2826
  
2827
  
2828
  
2829
  
2830
  
2831
  
2832
  
2833
  
2834
  
2835
  
2836
  
2837
  
2838
  
2839
  
2840
  
2841
  
2842
  
2843
  
2844
  
2845
  
2846
  
2847
  
2848
  
2849
  
2850
  
2851
  
2852
  
2853
  
2854
  
2855
  
2856
  
2857
  
2858
  
2859
  
2860
  
2861
  
2862
  
2863
  
2864
  
2865
  
2866
  
2867
  
2868
  
2869
  
2870
  
2871
  
2872
  
2873
  
2874
  
2875
  
2876
  
2877
  
2878
  
2879
  
2880
  
2881
  
2882
  
2883
  
2884
  
2885
  
2886
  
2887
  
2888
  
2889
  
2890
  
2891
  
2892
  
2893
  
2894
  
2895
  
2896
  
2897
  
2898
  
2899
  
2900
  
2901
  
2902
  
2903
  
2904
  
2905
  
2906
  
2907
  
2908
  
2909
  
2910
  
2911
  
2912
  
2913
  
2914
  
2915
  
2916
  
2917
  
2918
  
2919
  
2920
  
2921
  
2922
  
2923
  
2924
  
2925
  
2926
  
2927
  
2928
  
2929
  
2930
  
2931
  
2932
  
2933
  
2934
  
2935
  
2936
  
2937
  
2938
  
2939
  
2940
  
2941
  
2942
  
2943
  
2944
  
2945
  
2946
  
2947
  
2948
  
2949
  
2950
  
2951
  
2952
  
2953
  
2954
  
2955
  
2956
  
2957
  
2958
  
2959
  
2960
  
2961
  
2962
  
2963
  
2964
  
2965
  
2966
  
2967
  
2968
  
2969
  
2970
  
2971
  
2972
  
2973
  
2974
  
2975
  
2976
  
2977
  
2978
  
2979
  
2980
  
2981
  
2982
  
2983
  
2984
  
2985
  
2986
  
2987
  
2988
  
2989
  
2990
  
2991
  
2992
  
2993
  
2994
  
2995
  
2996
  
2997
  
2998
  
2999
  
3000
  
3001
  
3002
  
3003
  
3004
  
3005
  
3006
  
3007
  
3008
  
3009
  
3010
  
3011
  
3012
  
3013
  
3014
  
3015
  
3016
  
3017
  
3018
  
3019
  
3020
  
3021
  
3022
  
3023
  
3024
  
3025
  
3026
  
3027
  
3028
  
3029
  
3030
  
3031
  
3032
  
3033
  
3034
  
3035
  
3036
  
3037
  
3038
  
3039
  
3040
  
3041
  
3042
  
3043
  
3044
  
3045
  
3046
  
3047
  
3048
  
3049
  
3050
  
3051
  
3052
  
3053
  
3054
  
3055
  
3056
  
3057
  
3058
  
3059
  
3060
  
3061
  
3062
  
3063
  
3064
  
3065
  
3066
  
3067
  
3068
  
3069
  
3070
  
3071
  
3072
  
3073
  
3074
  
3075
  
3076
  
3077
  
3078
  
3079
  
3080
  
3081
  
3082
  
3083
  
3084
  
3085
  
3086
  
3087
  
3088
  
3089
  
3090
  
3091
  
3092
  
3093
  
3094
  
3095
  
3096
  
3097
  
3098
  
3099
  
3100
  
3101
  
3102
  
3103
  
3104
  
3105
  
3106
  
3107
  
3108
  
3109
  
3110
  
3111
  
3112
  
3113
  
3114
  
3115
  
3116
  
3117
  
3118
  
3119
  
3120
  
3121
  
3122
  
3123
  
3124
  
3125
  
3126
  
3127
  
3128
  
3129
  
3130
  
3131
  
3132
  
3133
  
3134
  
3135
  
3136
  
3137
  
3138
  
3139
  
3140
  
3141
  
3142
  
3143
  
3144
  
3145
  
3146
  
3147
  
3148
  
3149
  
3150
  
3151
  
3152
  
3153
  
3154
  
3155
  
3156
  
3157
  
3158
  
3159
  
3160
  
3161
  
3162
  
3163
  
3164
  
3165
  
3166
  
3167
  
3168
  
3169
  
3170
  
3171
  
3172
  
3173
  
3174
  
3175
  
3176
  
3177
  
3178
  
3179
  
3180
  
3181
  
3182
  
3183
  
3184
  
3185
  
3186
  
3187
  
3188
  
3189
  
3190
  
3191
  
3192
  
3193
  
3194
  
3195
  
3196
  
3197
  
3198
  
3199
  
3200
  
3201
  
3202
  
3203
  
3204
  
3205
  
3206
  
3207
  
3208
  
3209
  
3210
  
3211
  
3212
  
3213
  
3214
  
3215
  
3216
  
3217
  
3218
  
3219
  
3220
  
3221
  
3222
  
3223
  
3224
  
3225
  
3226
  
3227
  
3228
  
3229
  
3230
  
3231
  
3232
  
3233
  
3234
  
3235
  
3236
  
3237
  
3238
  
3239
  
3240
  
3241
  
3242
  
3243
  
3244
  
3245
  
3246
  
3247
  
3248
  
3249
  
3250
  
3251
  
3252
  
3253
  
3254
  
3255
  
3256
  
3257
  
3258
  
3259
  
3260
  
3261
  
3262
  
3263
  
3264
  
3265
  
3266
  
3267
  
3268
  
3269
  
3270
  
3271
  
3272
  
3273
  
3274
  
3275
  
3276
  
3277
  
3278
  
3279
  
3280
  
3281
  
3282
  
3283
  
3284
  
3285
  
3286
  
3287
  
3288
  
3289
  
3290
  
3291
  
3292
  
3293
  
3294
  
3295
  
3296
  
3297
  
3298
  
3299
  
3300
  
3301
  
3302
  
3303
  
3304
  
3305
  
3306
  
3307
  
3308
  
3309
  
3310
  
3311
  
3312
  
3313
  
3314
  
3315
  
3316
  
3317
  
3318
  
3319
  
3320
  
3321
  
3322
  
3323
  
3324
  
3325
  
3326
  
3327
  
3328
  
3329
  
3330
  
3331
  
3332
  
3333
  
3334
  
3335
  
3336
  
3337
  
3338
  
3339
  
3340
  
3341
  
3342
  
3343
  
3344
  
3345
  
3346
  
3347
  
3348
  
3349
  
3350
  
3351
  
3352
  
3353
  
3354
  
3355
  
3356
  
3357
  
3358
  
3359
  
3360
  
3361
  
3362
  
3363
  
3364
  
3365
  
3366
  
3367
  
3368
  
3369
  
3370
  
3371
  
3372
  
3373
  
3374
  
3375
  
3376
  
3377
  
3378
  
3379
  
3380
  
3381
  
3382
  
3383
  
3384
  
3385
  
3386
  
3387
  
3388
  
3389
  
3390
  
3391
  
3392
  
3393
  
3394
  
3395
  
3396
  
3397
  
3398
  
3399
  
3400
  
3401
  
3402
  
3403
  
3404
  
3405
  
3406
  
3407
  
3408
  
3409
  
3410
  
3411
  
3412
  
3413
  
3414
  
3415
  
3416
  
3417
  
3418
  
3419
  
3420
  
3421
  
3422
  
3423
  
3424
  
3425
  
3426
  
3427
  
3428
  
3429
  
3430
  
3431
  
3432
  
3433
  
3434
  
3435
  
3436
  
3437
  
3438
  
3439
  
3440
  
3441
  
3442
  
3443
  
3444
  
3445
  
3446
  
3447
  
3448
  
3449
  
3450
  
3451
  
3452
  
3453
  
3454
  
3455
  
3456
  
3457
  
3458
  
3459
  
3460
  
3461
  
3462
  
3463
  
3464
  
3465
  
3466
  
3467
  
3468
  
3469
  
3470
  
3471
  
3472
  
3473
  
3474
  
3475
  
3476
  
3477
  
3478
  
3479
  
3480
  
3481
  
3482
  
3483
  
3484
  
3485
  
3486
  
3487
  
3488
  
3489
  
3490
  
3491
  
3492
  
3493
  
3494
  
3495
  
3496
  
3497
  
3498
  
3499
  
3500
  
3501
  
3502
  
3503
  
3504
  
3505
  
3506
  
3507
  
3508
  
3509
  
3510
  
3511
  
3512
  
3513
  
3514
  
3515
  
3516
  
3517
  
3518
  
3519
  
3520
  
3521
  
3522
  
3523
  
3524
  
3525
  
3526
  
3527
  
3528
  
3529
  
3530
  
3531
  
3532
  
3533
  
3534
  
3535
  
3536
  
3537
  
3538
  
3539
  
3540
  
3541
  
3542
  
3543
  
3544
  
3545
  
3546
  
3547
  
3548
  
3549
  
3550
  
3551
  
3552
  
3553
  
3554
  
3555
  
3556
  
3557
  
3558
  
3559
  
3560
  
3561
  
3562
  
3563
  
3564
  
3565
  
3566
  
3567
  
3568
  
3569
  
3570
  
3571
  
3572
  
3573
  
3574
  
3575
  
3576
  
3577
  
3578
  
3579
  
3580
  
3581
  
3582
  
3583
  
3584
  
3585
  
3586
  
3587
  
3588
  
3589
  
3590
  
3591
  
3592
  
3593
  
3594
  
3595
  
3596
  
3597
  
3598
  
3599
  
3600
  
3601
  
3602
  
3603
  
3604
  
3605
  
3606
  
3607
  
3608
  
3609
  
3610
  
3611
  
3612
  
3613
  
3614
  
3615
  
3616
  
3617
  
3618
  
3619
  
3620
  
3621
  
3622
  
3623
  
3624
  
3625
  
3626
  
3627
  
3628
  
3629
  
3630
  
3631
  
3632
  
3633
  
3634
  
3635
  
3636
  
3637
  
3638
  
3639
  
3640
  
3641
  
3642
  
3643
  
3644
  
3645
  
3646
  
3647
  
3648
  
3649
  
3650
  
3651
  
3652
  
3653
  
3654
  
3655
  
3656
  
3657
  
3658
  
3659
  
3660
  
3661
  
3662
  
3663
  
3664
  
3665
  
3666
  
3667
  
3668
  
3669
  
3670
  
3671
  
3672
  
3673
  
3674
  
3675
  
3676
  
3677
  
3678
  
3679
  
3680
  
3681
  
3682
  
3683
  
3684
  
3685
  
3686
  
3687
  
3688
  
3689
  
3690
  
3691
  
3692
  
3693
  
3694
  
3695
  
3696
  
3697
  
3698
  
3699
  
3700
  
3701
  
3702
  
3703
  
3704
  
3705
  
3706
  
3707
  
3708
  
3709
  
3710
  
3711
  
3712
  
3713
  
3714
  
3715
  
3716
  
3717
  
3718
  
3719
  
3720
  
3721
  
3722
  
3723
  
3724
  
3725
  
3726
  
3727
  
3728
  
3729
  
3730
  
3731
  
3732
  
3733
  
3734
  
3735
  
3736
  
3737
  
3738
  
3739
  
3740
  
3741
  
3742
  
3743
  
3744
  
3745
  
3746
  
3747
  
3748
  
3749
  
3750
  
3751
  
3752
  
3753
  
3754
  
3755
  
3756
  
3757
  
3758
  
3759
  
3760
  
3761
  
3762
  
3763
  
3764
  
3765
  
3766
  
3767
  
3768
  
3769
  
3770
  
3771
  
3772
  
3773
  
3774
  
3775
  
3776
  
3777
  
3778
  
3779
  
3780
  
3781
  
3782
  
3783
  
3784
  
3785
  
3786
  
3787
  
3788
  
3789
  
3790
  
3791
  
3792
  
3793
  
3794
  
3795
  
3796
  
3797
  
3798
  
3799
  
3800
  
3801
  
3802
  
3803
  
3804
  
3805
  
3806
  
3807
  
3808
  
3809
  
3810
  
3811
  
3812
  
3813
  
3814
  
3815
  
3816
  
3817
  
3818
  
3819
  
3820
  
3821
  
3822
  
3823
  
3824
  
3825
  
3826
  
3827
  
3828
  
3829
  
3830
  
3831
  
3832
  
3833
  
3834
  
3835
  
3836
  
3837
  
3838
  
3839
  
3840
  
3841
  
3842
  
3843
  
3844
  
3845
  
3846
  
3847
  
3848
  
3849
  
3850
  
3851
  
3852
  
3853
  
3854
  
3855
  
3856
  
3857
  
3858
  
3859
  
3860
  
3861
  
3862
  
3863
  
3864
  
3865
  
3866
  
3867
  
3868
  
3869
  
3870
  
3871
  
3872
  
3873
  
3874
  
3875
  
3876
  
3877
  
3878
  
3879
  
3880
  
3881
  
3882
  
3883
  
3884
  
3885
  
3886
  
3887
  
3888
  
3889
  
3890
  
3891
  
3892
  
3893
  
3894
  
3895
  
3896
  
3897
  
3898
  
3899
  
3900
  
3901
  
3902
  
3903
  
3904
  
3905
  
3906
  
3907
  
3908
  
3909
  
3910
  
3911
  
3912
  
3913
  
3914
  
3915
  
3916
  
3917
  
3918
  
3919
  
3920
  
3921
  
3922
  
3923
  
3924
  
3925
  
3926
  
3927
  
3928
  
3929
  
3930
  
3931
  
3932
  
3933
  
3934
  
3935
  
3936
  
3937
  
3938
  
3939
  
3940
  
3941
  
3942
  
3943
  
3944
  
3945
  
3946
  
3947
  
3948
  
3949
  
3950
  
3951
  
3952
  
3953
  
3954
  
3955
  
3956
  
3957
  
3958
  
3959
  
3960
  
3961
  
3962
  
3963
  
3964
  
3965
  
3966
  
3967
  
3968
  
3969
  
3970
  
3971
  
3972
  
3973
  
3974
  
3975
  
3976
  
3977
  
3978
  
3979
  
3980
  
3981
  
3982
  
3983
  
3984
  
3985
  
3986
  
3987
  
3988
  
3989
  
3990
  
3991
  
3992
  
3993
  
3994
  
3995
  
3996
  
3997
  
3998
  
3999
  
4000
  
4001
  
4002
  
4003
  
4004
  
4005
  
4006
  
4007
  
4008
  
4009
  
4010
  
4011
  
4012
  
4013
  
4014
  
4015
  
4016
  
4017
  
4018
  
4019
  
4020
  
4021
  
4022
  
4023
  
4024
  
4025
  
4026
  
4027
  
4028
  
4029
  
4030
  
4031
  
4032
  
4033
  
4034
  
4035
  
4036
  
4037
  
4038
  
4039
  
4040
  
4041
  
4042
  
4043
  
4044
  
4045
  
4046
  
4047
  
4048
  
4049
  
4050
  
4051
  
4052
  
4053
  
4054
  
4055
  
4056
  
4057
  
4058
  
4059
  
4060
  
4061
  
4062
  
4063
  
4064
  
4065
  
4066
  
4067
  
4068
  
4069
  
4070
  
4071
  
4072
  
4073
  
4074
  
4075
  
4076
  
4077
  
4078
  
4079
  
4080
  
4081
  
4082
  
4083
  
4084
  
4085
  
4086
  
4087
  
4088
  
4089
  
4090
  
4091
  
4092
  
4093
  
4094
  
4095
  
4096
  
4097
  
4098
  
4099
  
4100
  
4101
  
4102
  
4103
  
4104
  
4105
  
4106
  
4107
  
4108
  
4109
  
4110
  
4111
  
4112
  
4113
  
4114
  
4115
  
4116
  
4117
  
4118
  
4119
  
4120
  
4121
  
4122
  
4123
  
4124
  
4125
  
4126
  
4127
  
4128
  
4129
  
4130
  
4131
  
4132
  
4133
  
4134
  
4135
  
4136
  
4137
  
4138
  
4139
  
4140
  
4141
  
4142
  
4143
  
4144
  
4145
  
4146
  
4147
  
4148
  
4149
  
4150
  
4151
  
4152
  
4153
  
4154
  
4155
  
4156
  
4157
  
4158
  
4159
  
4160
  
4161
  
4162
  
4163
  
4164
  
4165
  
4166
  
4167
  
4168
  
4169
  
4170
  
4171
  
4172
  
4173
  
4174
  
4175
  
4176
  
4177
  
4178
  
4179
  
4180
  
4181
  
4182
  
4183
  
4184
  
4185
  
4186
  
4187
  
4188
  
4189
  
4190
  
4191
  
4192
  
4193
  
4194
  
4195
  
4196
  
4197
  
4198
  
4199
  
4200
  
4201
  
4202
  
4203
  
4204
  
4205
  
4206
  
4207
  
4208
  
4209
  
4210
  
4211
  
4212
  
4213
  
4214
  
4215
  
4216
  
4217
  
4218
  
4219
  
4220
  
4221
  
4222
  
4223
  
4224
  
4225
  
4226
  
4227
  
4228
  
4229
  
4230
  
4231
  
4232
  
4233
  
4234
  
4235
  
4236
  
4237
  
4238
  
4239
  
4240
  
4241
  
4242
  
4243
  
4244
  
4245
  
4246
  
4247
  
4248
  
4249
  
4250
  
4251
  
4252
  
4253
  
4254
  
4255
  
4256
  
4257
  
4258
  
4259
  
4260
  
4261
  
4262
  
4263
  
4264
  
4265
  
4266
  
4267
  
4268
  
4269
  
4270
  
4271
  
4272
  
4273
  
4274
  
4275
  
4276
  
4277
  
4278
  
4279
  
4280
  
4281
  
4282
  
4283
  
4284
  
4285
  
4286
  
4287
  
4288
  
4289
  
4290
  
4291
  
4292
  
4293
  
4294
  
4295
  
4296
  
4297
  
4298
  
4299
  
4300
  
4301
  
4302
  
4303
  
4304
  
4305
  
4306
  
4307
  
4308
  
4309
  
4310
  
4311
  
4312
  
4313
  
4314
  
4315
  
4316
  
4317
  
4318
  
4319
  
4320
  
4321
  
4322
  
4323
  
4324
  
4325
  
4326
  
4327
  
4328
  
4329
  
4330
  
4331
  
4332
  
4333
  
4334
  
4335
  
4336
  
4337
  
4338
  
4339
  
4340
  
4341
  
4342
  
4343
  
4344
  
4345
  
4346
  
4347
  
4348
  
4349
  
4350
  
4351
  
4352
  
4353
  
4354
  
4355
  
4356
  
4357
  
4358
  
4359
  
4360
  
4361
  
4362
  
4363
  
4364
  
4365
  
4366
  
4367
  
4368
  
4369
  
4370
  
4371
  
4372
  
4373
  
4374
  
4375
  
4376
  
4377
  
4378
  
4379
  
4380
  
4381
  
4382
  
4383
  
4384
  
4385
  
4386
  
4387
  
4388
  
4389
  
4390
  
4391
  
4392
  
4393
  
4394
  
4395
  
4396
  
4397
  
4398
  
4399
  
4400
  
4401
  
4402
  
4403
  
4404
  
4405
  
4406
  
4407
  
4408
  
4409
  
4410
  
4411
  
4412
  
4413
  
4414
  
4415
  
4416
  
4417
  
4418
  
4419
  
4420
  
4421
  
4422
  
4423
  
4424
  
4425
  
4426
  
4427
  
4428
  
4429
  
4430
  
4431
  
4432
  
4433
  
4434
  
4435
  
4436
  
4437
  
4438
  
4439
  
4440
  
4441
  
4442
  
4443
  
4444
  
4445
  
4446
  
4447
  
4448
  
4449
  
4450
  
4451
  
4452
  
4453
  
4454
  
4455
  
4456
  
4457
  
4458
  
4459
  
4460
  
4461
  
4462
  
4463
  
4464
  
4465
  
4466
  
4467
  
4468
  
4469
  
4470
  
4471
  
4472
  
4473
  
4474
  
4475
  
4476
  
4477
  
4478
  
4479
  
4480
  
4481
  
4482
  
4483
  
4484
  
4485
  
4486
  
4487
  
4488
  
4489
  
4490
  
4491
  
4492
  
4493
  
4494
  
4495
  
4496
  
4497
  
4498
  
4499
  
4500
  
4501
  
4502
  
4503
  
4504
  
4505
  
4506
  
4507
  
4508
  
4509
  
4510
  
4511
  
4512
  
4513
  
4514
  
4515
  
4516
  
4517
  
4518
  
4519
  
4520
  
4521
  
4522
  
4523
  
4524
  
4525
  
4526
  
4527
  
4528
  
4529
  
4530
  
4531
  
4532
  
4533
  
4534
  
4535
  
4536
  
4537
  
4538
  
4539
  
4540
  
4541
  
4542
  
4543
  
4544
  
4545
  
4546
  
4547
  
4548
  
4549
  
4550
  
4551
  
4552
  
4553
  
4554
  
4555
  
4556
  
4557
  
4558
  
4559
  
4560
  
4561
  
4562
  
4563
  
4564
  
4565
  
4566
  
4567
  
4568
  
4569
  
4570
  
4571
  
4572
  
4573
  
4574
  
4575
  
4576
  
4577
  
4578
  
4579
  
4580
  
4581
  
4582
  
4583
  
4584
  
4585
  
4586
  
4587
  
4588
  
4589
  
4590
  
4591
  
4592
  
4593
  
4594
  
4595
  
4596
  
4597
  
4598
  
4599
  
4600
  
4601
  
4602
  
4603
  
4604
  
4605
  
4606
  
4607
  
4608
  
4609
  
4610
  
4611
  
4612
  
4613
  
4614
  
4615
  
4616
  
4617
  
4618
  
4619
  
4620
  
4621
  
4622
  
4623
  
4624
  
4625
  
4626
  
4627
  
4628
  
4629
  
4630
  
4631
  
4632
  
4633
  
4634
  
4635
  
4636
  
4637
  
4638
  
4639
  
4640
  
4641
  
4642
  
4643
  
4644
  
4645
  
4646
  
4647
  
4648
  
4649
  
4650
  
4651
  
4652
  
4653
  
4654
  
4655
  
4656
  
4657
  
4658
  
4659
  
4660
  
4661
  
4662
  
4663
  
4664
  
4665
  
4666
  
4667
  
4668
  
4669
  
4670
  
4671
  
4672
  
4673
  
4674
  
4675
  
4676
  
4677
  
4678
  
4679
  
4680
  
4681
  
4682
  
4683
  
4684
  
4685
  
4686
  
4687
  
4688
  
4689
  
4690
  
4691
  
4692
  
4693
  
4694
  
4695
  
4696
  
4697
  
4698
  
4699
  
4700
  
4701
  
4702
  
4703
  
4704
  
4705
  
4706
  
4707
  
4708
  
4709
  
4710
  
4711
  
4712
  
4713
  
4714
  
4715
  
4716
  
4717
  
4718
  
4719
  
4720
  
4721
  
4722
  
4723
  
4724
  
4725
  
4726
  
4727
  
4728
  
4729
  
4730
  
4731
  
4732
  
4733
  
4734
  
4735
  
4736
  
4737
  
4738
  
4739
  
4740
  
4741
  
4742
  
4743
  
4744
  
4745
  
4746
  
4747
  
4748
  
4749
  
4750
  
4751
  
4752
  
4753
  
4754
  
4755
  
4756
  
4757
  
4758
  
4759
  
4760
  
4761
  
4762
  
4763
  
4764
  
4765
  
4766
  
4767
  
4768
  
4769
  
4770
  
4771
  
4772
  
4773
  
4774
  
4775
  
4776
  
4777
  
4778
  
4779
  
4780
  
4781
  
4782
  
4783
  
4784
  
4785
  
4786
  
4787
  
4788
  
4789
  
4790
  
4791
  
4792
  
4793
  
4794
  
4795
  
4796
  
4797
  
4798
  
4799
  
4800
  
4801
  
4802
  
4803
  
4804
  
4805
  
4806
  
4807
  
4808
  
4809
  
4810
  
4811
  
4812
  
4813
  
4814
  
4815
  
4816
  
4817
  
4818
  
4819
  
4820
  
4821
  
4822
  
4823
  
4824
  
4825
  
4826
  
4827
  
4828
  
4829
  
4830
  
4831
  
4832
  
4833
  
4834
  
4835
  
4836
  
4837
  
4838
  
4839
  
4840
  
4841
  
4842
  
4843
  
4844
  
4845
  
4846
  
4847
  
4848
  
4849
  
4850
  
4851
  
4852
  
4853
  
4854
  
4855
  
4856
  
4857
  
4858
  
4859
  
4860
  
4861
  
4862
  
4863
  
4864
  
4865
  
4866
  
4867
  
4868
  
4869
  
4870
  
4871
  
4872
  
4873
  
4874
  
4875
  
4876
  
4877
  
4878
  
4879
  
4880
  
4881
  
4882
  
4883
  
4884
  
4885
  
4886
  
4887
  
4888
  
4889
  
4890
  
4891
  
4892
  
4893
  
4894
  
4895
  
4896
  
4897
  
4898
  
4899
  
4900
  
4901
  
4902
  
4903
  
4904
  
4905
  
4906
  
4907
  
4908
  
4909
  
4910
  
4911
  
4912
  
4913
  
4914
  
4915
  
4916
  
4917
  
4918
  
4919
  
4920
  
4921
  
4922
  
4923
  
4924
  
4925
  
4926
  
4927
  
4928
  
4929
  
4930
  
4931
  
4932
  
4933
  
4934
  
4935
  
4936
  
4937
  
4938
  
4939
  
4940
  
4941
  
4942
  
4943
  
4944
  
4945
  
4946
  
4947
  
4948
  
4949
  
4950
  
4951
  
4952
  
4953
  
4954
  
4955
  
4956
  
4957
  
4958
  
4959
  
4960
  
4961
  
4962
  
4963
  
4964
  
4965
  
4966
  
4967
  
4968
  
4969
  
4970
  
4971
  
4972
  
4973
  
4974
  
4975
  
4976
  
4977
  
4978
  
4979
  
4980
  
4981
  
4982
  
4983
  
4984
  
4985
  
4986
  
4987
  
4988
  
4989
  
4990
  
4991
  
4992
  
4993
  
4994
  
4995
  
4996
  
4997
  
4998
  
4999
  
5000
  
5001
  
5002
  
5003
  
5004
  
5005
  
5006
  
5007
  
5008
  
5009
  
5010
  
5011
  
5012
  
5013
  
5014
  
5015
  
5016
  
5017
  
5018
  
5019
  
5020
  
5021
  
5022
  
5023
  
5024
  
5025
  
5026
  
5027
  
5028
  
5029
  
5030
  
5031
  
5032
  
5033
  
5034
  
5035
  
5036
  
5037
  
5038
  
5039
  
5040
  
5041
  
5042
  
5043
  
5044
  
5045
  
5046
  
5047
  
5048
  
5049
  
5050
  
5051
  
5052
  
5053
  
5054
  
5055
  
5056
  
5057
  
5058
  
5059
  
5060
  
5061
  
5062
  
5063
  
5064
  
5065
  
5066
  
5067
  
5068
  
5069
  
5070
  
5071
  
5072
  
5073
  
5074
  
5075
  
5076
  
5077
  
5078
  
5079
  
5080
  
5081
  
5082
  
5083
  
5084
  
5085
  
5086
  
5087
  
5088
  
5089
  
5090
  
5091
  
5092
  
5093
  
5094
  
5095
  
5096
  
5097
  
5098
  
5099
  
5100
  
5101
  
5102
  
5103
  
5104
  
5105
  
5106
  
5107
  
5108
  
5109
  
5110
  
5111
  
5112
  
5113
  
5114
  
5115
  
5116
  
5117
  
5118
  
5119
  
5120
  
5121
  
5122
  
5123
  
5124
  
5125
  
5126
  
5127
  
5128
  
5129
  
5130
  
5131
  
5132
  
5133
  
5134
  
5135
  
5136
  
5137
  
5138
  
5139
  
5140
  
5141
  
5142
  
5143
  
5144
  
5145
  
5146
  
5147
  
5148
  
5149
  
5150
  
5151
  
5152
  
5153
  
5154
  
5155
  
5156
  
5157
  
5158
  
5159
  
5160
  
5161
  
5162
  
5163
  
5164
  
5165
  
5166
  
5167
  
5168
  
5169
  
5170
  
5171
  
5172
  
5173
  
5174
  
5175
  
5176
  
5177
  
5178
  
5179
  
5180
  
5181
  
5182
  
5183
  
5184
  
5185
  
5186
  
5187
  
5188
  
5189
  
5190
  
5191
  
5192
  
5193
  
5194
  
5195
  
5196
  
5197
  
5198
  
5199
  
5200
  
5201
  
5202
  
5203
  
5204
  
5205
  
5206
  
5207
  
5208
  
5209
  
5210
  
5211
  
5212
  
5213
  
5214
  
5215
  
5216
  
5217
  
5218
  
5219
  
5220
  
5221
  
5222
  
5223
  
5224
  
5225
  
5226
  
5227
  
5228
  
5229
  
5230
  
5231
  
5232
  
5233
  
5234
  
5235
  
5236
  
5237
  
5238
  
5239
  
5240
  
5241
  
5242
  
5243
  
5244
  
5245
  
5246
  
5247
  
5248
  
5249
  
5250
  
5251
  
5252
  
5253
  
5254
  
5255
  
5256
  
5257
  
5258
  
5259
  
5260
  
5261
  
5262
  
5263
  
5264
  
5265
  
5266
  
5267
  
5268
  
5269
  
5270
  
5271
  
5272
  
5273
  
5274
  
5275
  
5276
  
5277
  
5278
  
5279
  
5280
  
5281
  
5282
  
5283
  
5284
  
5285
  
5286
  
5287
  
5288
  
5289
  
5290
  
5291
  
5292
  
5293
  
5294
  
5295
  
5296
  
5297
  
5298
  
5299
  
5300
  
5301
  
5302
  
5303
  
5304
  
5305
  
5306
  
5307
  
5308
  
5309
  
5310
  
5311
  
5312
  
5313
  
5314
  
5315
  
5316
  
5317
  
5318
  
5319
  
5320
  
5321
  
5322
  
5323
  
5324
  
5325
  
5326
  
5327
  
5328
  
5329
  
5330
  
5331
  
5332
  
5333
  
5334
  
5335
  
5336
  
5337
  
5338
  
5339
  
5340
  
5341
  
5342
  
5343
  
5344
  
5345
  
5346
  
5347
  
5348
  
5349
  
5350
  
5351
  
5352
  
5353
  
5354
  
5355
  
5356
  
5357
  
5358
  
5359
  
5360
  
5361
  
5362
  
5363
  
5364
  
5365
  
5366
  
5367
  
5368
  
5369
  
5370
  
5371
  
5372
  
5373
  
5374
  
5375
  
5376
  
5377
  
5378
  
5379
  
5380
  
5381
  
5382
  
5383
  
5384
  
5385
  
5386
  
5387
  
5388
  
5389
  
5390
  
5391
  
5392
  
5393
  
5394
  
5395
  
5396
  
5397
  
5398
  
5399
  
5400
  
5401
  
5402
  
5403
  
5404
  
5405
  
5406
  
5407
  
5408
  
5409
  
5410
  
5411
  
5412
  
5413
  
5414
  
5415
  
5416
  
5417
  
5418
  
5419
  
5420
  
5421
  
5422
  
5423
  
5424
  
5425
  
5426
  
5427
  
5428
  
5429
  
5430
  
5431
  
5432
  
5433
  
5434
  
5435
  
5436
  
5437
  
5438
  
5439
  
5440
  
5441
  
5442
  
5443
  
5444
  
5445
  
5446
  
5447
  
5448
  
5449
  
5450
  
5451
  
5452
  
5453
  
5454
  
5455
  
5456
  
5457
  
5458
  
5459
  
5460
  
5461
  
5462
  
5463
  
5464
  
5465
  
5466
  
5467
  
5468
  
5469
  
5470
  
5471
  
5472
  
5473
  
5474
  
5475
  
5476
  
5477
  
5478
  
5479
  
5480
  
5481
  
5482
  
5483
  
5484
  
5485
  
5486
  
5487
  
5488
  
5489
  
5490
  
5491
  
5492
  
5493
  
5494
  
5495
  
5496
  
5497
  
5498
  
5499
  
5500
  
5501
  
5502
  
5503
  
5504
  
5505
  
5506
  
5507
  
5508
  
5509
  
5510
  
5511
  
5512
  
5513
  
5514
  
5515
  
5516
  
5517
  
5518
  
5519
  
5520
  
5521
  
5522
  
5523
  
5524
  
5525
  
5526
  
5527
  
5528
  
5529
  
5530
  
5531
  
5532
  
5533
  
5534
  
5535
  
5536
  
5537
  
5538
  
5539
  
5540
  
5541
  
5542
  
5543
  
5544
  
5545
  
5546
  
5547
  
5548
  
5549
  
5550
  
5551
  
5552
  
5553
  
5554
  
5555
  
5556
  
5557
  
5558
  
5559
  
5560
  
5561
  
5562
  
5563
  
5564
  
5565
  
5566
  
5567
  
5568
  
5569
  
5570
  
5571
  
5572
  
5573
  
5574
  
5575
  
5576
  
5577
  
5578
  
5579
  
5580
  
5581
  
5582
  
5583
  
5584
  
5585
  
5586
  
5587
  
5588
  
5589
  
5590
  
5591
  
5592
  
5593
  
5594
  
5595
  
5596
  
5597
  
5598
  
5599
  
5600
  
5601
  
5602
  
5603
  
5604
  
5605
  
5606
  
5607
  
5608
  
5609
  
5610
  
5611
  
5612
  
5613
  
5614
  
5615
  
5616
  
5617
  
5618
  
5619
  
5620
  
5621
  
5622
  
5623
  
5624
  
5625
  
5626
  
5627
  
5628
  
5629
  
5630
  
5631
  
5632
  
5633
  
5634
  
5635
  
5636
  
5637
  
5638
  
5639
  
5640
  
5641
  
5642
  
5643
  
5644
  
5645
  
5646
  
5647
  
5648
  
5649
  
5650
  
5651
  
5652
  
5653
  
5654
  
5655
  
5656
  
5657
  
5658
  
5659
  
5660
  
5661
  
5662
  
5663
  
5664
  
5665
  
5666
  
5667
  
5668
  
5669
  
5670
  
5671
  
5672
  
5673
  
5674
  
5675
  
5676
  
5677
  
5678
  
5679
  
5680
  
5681
  
5682
  
5683
  
5684
  
5685
  
5686
  
5687
  
5688
  
5689
  
5690
  
5691
  
5692
  
5693
  
5694
  
5695
  
5696
  
5697
  
5698
  
5699
  
5700
  
5701
  
5702
  
5703
  
5704
  
5705
  
5706
  
5707
  
5708
  
5709
  
5710
  
5711
  
5712
  
5713
  
5714
  
5715
  
5716
  
5717
  
5718
  
5719
  
5720
  
5721
  
5722
  
5723
  
5724
  
5725
  
5726
  
5727
  
5728
  
5729
  
5730
  
5731
  
5732
  
5733
  
5734
  
5735
  
5736
  
5737
  
5738
  
5739
  
5740
  
5741
  
5742
  
5743
  
5744
  
5745
  
5746
  
5747
  
5748
  
5749
  
5750
  
5751
  
5752
  
5753
  
5754
  
5755
  
5756
  
5757
  
5758
  
5759
  
5760
  
5761
  
5762
  
5763
  
5764
  
5765
  
5766
  
5767
  
5768
  
5769
  
5770
  
5771
  
5772
  
5773
  
5774
  
5775
  
5776
  
5777
  
5778
  
5779
  
5780
  
5781
  
5782
  
5783
  
5784
  
5785
  
5786
  
5787
  
5788
  
5789
  
5790
  
5791
  
5792
  
5793
  
5794
  
5795
  
5796
  
5797
  
5798
  
5799
  
5800
  
5801
  
5802
  
5803
  
5804
  
5805
  
5806
  
5807
  
5808
  
5809
  
5810
  
5811
  
5812
  
5813
  
5814
  
5815
  
5816
  
5817
  
5818
  
5819
  
5820
  
5821
  
5822
  
5823
  
5824
  
5825
  
5826
  
5827
  
5828
  
5829
  
5830
  
5831
  
5832
  
5833
  
5834
  
5835
  
5836
  
5837
  
5838
  
5839
  
5840
  
5841
  
5842
  
5843
  
5844
  
5845
  
5846
  
5847
  
5848
  
5849
  
5850
  
5851
  
5852
  
5853
  
5854
  
5855
  
5856
  
5857
  
5858
  
5859
  
5860
  
5861
  
5862
  
5863
  
5864
  
5865
  
5866
  
5867
  
5868
  
5869
  
5870
  
5871
  
5872
  
5873
  
5874
  
5875
  
5876
  
5877
  
5878
  
5879
  
5880
  
5881
  
5882
  
5883
  
5884
  
5885
  
5886
  
5887
  
5888
  
5889
  
5890
  
5891
  
5892
  
5893
  
5894
  
5895
  
5896
  
5897
  
5898
  
5899
  
5900
  
5901
  
5902
  
5903
  
5904
  
5905
  
5906
  
5907
  
5908
  
5909
  
5910
  
5911
  
5912
  
5913
  
5914
  
5915
  
5916
  
5917
  
5918
  
5919
  
5920
  
5921
  
5922
  
5923
  
5924
  
5925
  
5926
  
5927
  
5928
  
5929
  
5930
  
5931
  
5932
  
5933
  
5934
  
5935
  
5936
  
5937
  
5938
  
5939
  
5940
  
5941
  
5942
  
5943
  
5944
  
5945
  
5946
  
5947
  
5948
  
5949
  
5950
  
5951
  
5952
  
5953
  
5954
  
5955
  
5956
  
5957
  
5958
  
5959
  
5960
  
5961
  
5962
  
5963
  
5964
  
5965
  
5966
  
5967
  
5968
  
5969
  
5970
  
5971
  
5972
  
5973
  
5974
  
5975
  
5976
  
5977
  
5978
  
5979
  
5980
  
5981
  
5982
  
5983
  
5984
  
5985
  
5986
  
5987
  
5988
  
5989
  
5990
  
5991
  
5992
  
5993
  
5994
  
5995
  
5996
  
5997
  
5998
  
5999
  
6000
  
6001
  
6002
  
6003
  
6004
  
6005
  
6006
  
6007
  
6008
  
6009
  
6010
  
6011
  
6012
  
6013
  
6014
  
6015
  
6016
  
6017
  
6018
  
6019
  
6020
  
6021
  
6022
  
6023
  
6024
  
6025
  
6026
  
6027
  
6028
  
6029
  
6030
  
6031
  
6032
  
6033
  
6034
  
6035
  
6036
  
6037
  
6038
  
6039
  
6040
  
6041
  
6042
  
6043
  
6044
  
6045
  
6046
  
6047
  
6048
  
6049
  
6050
  
6051
  
6052
  
6053
  
6054
  
6055
  
6056
  
6057
  
6058
  
6059
  
6060
  
6061
  
6062
  
6063
  
6064
  
6065
  
6066
  
6067
  
6068
  
6069
  
6070
  
6071
  
6072
  
6073
  
6074
  
6075
  
6076
  
6077
  
6078
  
6079
  
6080
  
6081
  
6082
  
6083
  
6084
  
6085
  
6086
  
6087
  
6088
  
6089
  
6090
  
6091
  
6092
  
6093
  
6094
  
6095
  
6096
  
6097
  
6098
  
6099
  
6100
  
6101
  
6102
  
6103
  
6104
  
6105
  
6106
  
6107
  
6108
  
6109
  
6110
  
6111
  
6112
  
6113
  
6114
  
6115
  
6116
  
6117
  
6118
  
6119
  
6120
  
6121
  
6122
  
6123
  
6124
  
6125
  
6126
  
6127
  
6128
  
6129
  
6130
  
6131
  
6132
  
6133
  
6134
  
6135
  
6136
  
6137
  
6138
  
6139
  
6140
  
6141
  
6142
  
6143
  
6144
  
6145
  
6146
  
6147
  
6148
  
6149
  
6150
  
6151
  
6152
  
6153
  
6154
  
6155
  
6156
  
6157
  
6158
  
6159
  
6160
  
6161
  
6162
  
6163
  
6164
  
6165
  
6166
  
6167
  
6168
  
6169
  
6170
  
6171
  
6172
  
6173
  
6174
  
6175
  
6176
  
6177
  
6178
  
6179
  
6180
  
6181
  
6182
  
6183
  
6184
  
6185
  
6186
  
6187
  
6188
  
6189
  
6190
  
6191
  
6192
  
6193
  
6194
  
6195
  
6196
  
6197
  
6198
  
6199
  
6200
  
6201
  
6202
  
6203
  
6204
  
6205
  
6206
  
6207
  
6208
  
6209
  
6210
  
6211
  
6212
  
6213
  
6214
  
6215
  
6216
  
6217
  
6218
  
6219
  
6220
  
6221
  
6222
  
6223
  
6224
  
6225
  
6226
  
6227
  
6228
  
6229
  
6230
  
6231
  
6232
  
6233
  
6234
  
6235
  
6236
  
6237
  
6238
  
6239
  
6240
  
6241
  
6242
  
6243
  
6244
  
6245
  
6246
  
6247
  
6248
  
6249
  
6250
  
6251
  
6252
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
 
struct callback *gc_evaluator_callback=0; 
 
#include "array.h" 
#include "multiset.h" 
#include "mapping.h" 
#include "object.h" 
#include "program.h" 
#include "stralloc.h" 
#include "stuff.h" 
#include "pike_error.h" 
#include "pike_memory.h" 
#include "pike_macros.h" 
#include "pike_rusage.h" 
#include "pike_types.h" 
#include "time_stuff.h" 
#include "constants.h" 
#include "interpret.h" 
#include "bignum.h" 
#include "pike_threadlib.h" 
#include "gc.h" 
#include "main.h" 
#include "builtin_functions.h" 
#include "block_allocator.h" 
 
#include <math.h> 
 
#include "block_alloc.h" 
 
int gc_enabled = 1; 
 
/* These defaults are only guesses and hardly tested at all. Please improve. */ 
double gc_garbage_ratio_low = 0.2; 
double gc_time_ratio = 0.05; 
double gc_garbage_ratio_high = 0.5; 
double gc_min_time_ratio = 1.0/10000.0; /* Martys constant. */ 
 
/* This slowness factor approximately corresponds to the average over 
 * the last ten gc rounds. (0.9 == 1 - 1/10) */ 
double gc_average_slowness = 0.9; 
 
/* High-level callbacks. 
 * NB: These are initialized from builtin.cmod. 
 */ 
/* Callback called when gc() starts. */ 
struct svalue gc_pre_cb; 
 
/* Callback called when the mark and sweep phase of the gc() is done. */ 
struct svalue gc_post_cb; 
 
/* Callback called for each object that is to be destructed explicitly 
 * by the gc(). 
 */ 
struct svalue gc_destruct_cb; 
 
/* Callback called when the gc() is about to exit. */ 
struct svalue gc_done_cb; 
 
/* The gc will free all things with no external nonweak references 
 * that isn't referenced by live objects. An object is considered 
 * "live" if it contains code that must be executed when it is 
 * destructed; see gc_object_is_live for details. Live objects without 
 * external references are then destructed and garbage collected with 
 * normal refcount garbing (which might leave dead garbage around for 
 * the next gc). These live objects are destructed in an order that 
 * tries to be as well defined as possible using several rules: 
 * 
 * o  If an object A references B single way, then A is destructed 
 *    before B. 
 * o  If A and B are in a cycle, and there is a reference somewhere 
 *    from B to A that is weaker than any reference from A to B, then 
 *    the cycle is resolved by disregarding the weaker reference, and 
 *    A is therefore destructed before B. 
 * o  If a cycle is resolved through disregarding a weaker reference 
 *    according to the preceding rule, and there is another cycle 
 *    without weak references which also gets resolved through 
 *    disregarding the same reference, then the other cycle won't be 
 *    resolved by disregarding some other reference. 
 * o  Weak references are considered weaker than normal ones, and both 
 *    are considered weaker than strong references. 
 * o  Strong references are used in special cases like parent object 
 *    references. There can never be a cycle consisting only of strong 
 *    references. (This means the gc will never destruct a parent 
 *    object before all children have been destructed.) 
 * 
 * The gc tries to detect and warn about cases where there are live 
 * objects with no well defined order between them. There are cases 
 * that are missed by this detection, though. 
 * 
 * Things that aren't live objects but are referenced from them are 
 * still intact during this destruct pass, so it's entirely possible 
 * to save them by adding external references to them. However, it's 
 * not possible for live objects to save themselves or other live 
 * objects; all live objects that didn't have external references at 
 * the start of the gc pass will be destructed regardless of added 
 * references. 
 * 
 * Things that have only weak external references at the start of the 
 * gc pass will be freed. That's done before the live object destruct 
 * pass. Internal weak references are however still intact. 
 * 
 * Note: Keep the doc for lfun::destroy up-to-date with the above. 
 */ 
 
/* #define GC_DEBUG */ 
/* #define GC_VERBOSE */ 
/* #define GC_CYCLE_DEBUG */ 
/* #define GC_STACK_DEBUG */ 
/* #define GC_INTERVAL_DEBUG */ 
 
#if defined(GC_VERBOSE) && !defined(PIKE_DEBUG) 
#undef GC_VERBOSE 
#endif 
#ifdef GC_VERBOSE 
#define GC_VERBOSE_DO(X) X 
#else 
#define GC_VERBOSE_DO(X) 
#endif 
 
int num_objects = 2;                /* Account for *_empty_array. */ 
int got_unlinked_things; 
ALLOC_COUNT_TYPE num_allocs =0; 
ALLOC_COUNT_TYPE alloc_threshold = GC_MIN_ALLOC_THRESHOLD; 
PMOD_EXPORT int Pike_in_gc = 0; 
int gc_generation = 0; 
time_t last_gc; 
int gc_trace = 0, gc_debug = 0; 
#ifdef DO_PIKE_CLEANUP 
int gc_destruct_everything = 0; 
#endif 
size_t gc_ext_weak_refs; 
 
ALLOC_COUNT_TYPE saved_alloc_threshold; 
/* Used to backup alloc_threshold if the gc is disabled, so that it 
 * can be restored when it's enabled again. This is to not affect the 
 * gc interval if it's disabled only for a short duration. 
 * alloc_threshold is set to GC_MAX_ALLOC_THRESHOLD while it's 
 * disabled, to avoid complicating the test in GC_ALLOC(). */ 
 
static double objects_alloced = 0.0; 
static double objects_freed = 0.0; 
static double gc_time = 0.0, non_gc_time = 0.0; 
static cpu_time_t last_gc_end_real_time = -1; 
cpu_time_t auto_gc_time = 0; 
cpu_time_t auto_gc_real_time = 0; 
 
struct link_frame           /* See cycle checking blurb below. */ 
{ 
  void *data; 
  struct link_frame *prev;  /* Previous frame in the link stack. */ 
  gc_cycle_check_cb *checkfn;   /* Function to call to recurse the thing. */ 
  int weak;                 /* Weak flag to checkfn. */ 
}; 
 
struct gc_rec_frame         /* See cycle checking blurb below. */ 
{ 
  void *data; 
  int rf_flags; 
  struct gc_rec_frame *prev;        /* The previous frame in the recursion stack. 
                                 * NULL for frames not in the stack (i.e. on a 
                                 * cycle piece list or in the kill list). */ 
  struct gc_rec_frame *next;        /* The next frame in the recursion stack or the 
                                 * kill list. Undefined for frames on cycle 
                                 * piece lists. */ 
  struct gc_rec_frame *cycle_id;/* For a frame in the recursion stack: The 
                                 * cycle identifier frame. 
                                 * For a frame on a cycle piece list: The frame 
                                 * in the recursion stack whose cycle piece 
                                 * list this frame is in. */ 
  struct gc_rec_frame *cycle_piece;/* The start of the cycle piece list for 
                                 * frames on the recursion stack, or the next 
                                 * frame in the list for frames in cycle piece 
                                 * lists. */ 
  union { 
    struct link_frame *link_top;/* The top of the link stack for frames on the 
                                 * recursion stack. */ 
    struct gc_rec_frame *last_cycle_piece;/* In the first frame on a cycle 
                                 * piece list, this is used to point to the 
                                 * last frame in the list. */ 
  } u; 
}; 
 
/* rf_flags bits. */ 
#define GC_PREV_WEAK          0x0001 
#define GC_PREV_STRONG              0x0002 
#define GC_PREV_BROKEN              0x0004 
#define GC_MARK_LIVE                0x0008 
#define GC_ON_KILL_LIST             0x0010 
#ifdef PIKE_DEBUG 
#define GC_ON_CYCLE_PIECE_LIST      0x0020 
#define GC_FRAME_FREED              0x0040 
#define GC_FOLLOWED_NONSTRONG       0x0080 
#define GC_IS_VALID_CP_CYCLE_ID     0x0100 
#endif 
 
static struct gc_rec_frame sentinel_frame = { 
  (void *) (ptrdiff_t) -1, 
  0, 
  (struct gc_rec_frame *) (ptrdiff_t) -1, 
  (struct gc_rec_frame *) (ptrdiff_t) -1, 
  &sentinel_frame,              /* Recognize as cycle id frame. */ 
  (struct gc_rec_frame *) (ptrdiff_t) -1, 
  {(struct link_frame *) (ptrdiff_t) -1} 
}; 
static struct gc_rec_frame *stack_top = &sentinel_frame; 
static struct gc_rec_frame *kill_list = &sentinel_frame; 
 
/* Cycle checking 
 * 
 * When a thing is recursed into, a gc_rec_frame is pushed onto the 
 * recursion stack whose top pointer is stack_top. After that the 
 * links emanating from that thing are collected through the 
 * gc_cycle_check_* functions and pushed as link_frames onto a link 
 * stack that is specific to the rec frame. gc_rec_frame.u.link_top is 
 * the top pointer of that stack. The link frames are then popped off 
 * again one by one. If the thing that the link points to hasn't been 
 * visited already then it's recursed, which means that the link frame 
 * is popped off the link stack and a new rec frame is pushed onto the 
 * main stack instead. 
 * 
 * When a reference is followed to a thing which has a rec frame 
 * (either on the stack or on a cycle piece list - see below), we have 
 * a cycle. However, if that reference is weak (or becomes weak after 
 * rotation - see below), it's still not regarded as a cycle since 
 * weak refs always are eligible to be broken to resolve cycles. 
 * 
 * A sequence of frames on the recursion stack forms a cycle iff they 
 * have the same value in gc_rec_frame.cycle_id. A cycle is always 
 * continuous on the stack. 
 * 
 * Furthermore, the cycle_ids always point to the first (deepest) 
 * frame on the stack that is part of the cycle. That frame is called 
 * the "cycle identifier frame" since all frames in the cycle will end 
 * up there if the cycle pointers are followed transitively. The 
 * cycle_id pointer in the cycle identifier frame points to itself. 
 * Every frame is initially treated as a cycle containing only itself. 
 * 
 * When the recursion leaves a thing, the rec frame is popped off the 
 * stack. If the frame is part of a cycle that isn't finished at that 
 * point, it's not freed but instead linked onto the cycle piece list 
 * in gc_rec_frame.cycle_piece of the parent rec frame (which 
 * necessarily is part of the same cycle). That is done to detect 
 * cyclic refs that end up at the popped frame later on. 
 * 
 * The cycle_id pointers for frames on cycle piece lists point back 
 * towards the rec frame that still is on the stack, but not past it 
 * to the cycle id frame (which might be further back in the stack). 
 * Whenever cycle_id pointer chains are traversed to find the root of 
 * a cycle piece list, they are compacted to avoid O(n) complexity. 
 * 
 * The current tentative destruct order is described by the order on 
 * the stack and the attached cycle piece lists: The thing that's 
 * deepest in the stack is destructed first and the recursion stack 
 * has precedence over the cycle piece list (the reason for that is 
 * explained later). To illustrate: 
 *                                   ,- stack_top 
 *                                  v 
 *           t1 <=> t2 <=> ... <=> t3 
 *            |      |              `-> t4 -> ... -> t5 
 *            |      `-> t6 -> ... -> t7 
 *            `-> t8 -> ... -> t9 
 * 
 * Here <=> represents links on the recursion stack and -> links in 
 * the cycle piece lists. The tentative destruct order for these 
 * things is the same as the numbering above. 
 * 
 * Since we strive to keep the refs intact during destruction, the 
 * above means that the refs which have priority to be kept intact 
 * should point towards the top of the stack and towards the end of 
 * the cycle piece lists. 
 * 
 * To allow rotations, the recursion stack is a double linked list 
 * using gc_rec_frame.prev and gc_rec_frame.next. Rotations are the 
 * operation used to manipulate the order to avoid getting a 
 * prioritized link pointing in the wrong direction: 
 *                                                        ,- stack_top 
 *                                   weak                v 
 *  t1 <=> ... <=> t2 <=> ... <=> t3 <-> t4 <=> ... <=> t5 
 * 
 * If a nonweak backward pointer from t5 to t2 is encountered here, we 
 * should prefer to break(*) the weak ref between t3 and t4. The stack 
 * is therefore rotated to become: 
 *                                                        ,- stack_top 
 *            broken                                     v 
 *  t1 <=> ... <#> t4 <=> ... <=> t5 <=> t2 <=> ... <=> t3 
 * 
 * The section to rotate always ends at the top of the stack. 
 * 
 * The strength of the refs along the stack links are represented as 
 * follows: 
 * 
 * o  Things with a strong ref between them are kept next to each 
 *    other, and the second (the one being referenced by the strong 
 *    ref) has the GC_PREV_STRONG bit set. A rotation never breaks the 
 *    list inside a sequence of strong refs. 
 * 
 * o  The GC_PREV_WEAK bit is set in the next frame for every link on 
 *    the stack where no preceding frame reference any following frame 
 *    with anything but weak refs. 
 * 
 * o  GC_PREV_BROKEN is set in frames that are rotated back, i.e. t4 
 *    in the example above. This is used to break later cycles in the 
 *    same position when they can't be broken at a weak link. 
 * 
 * If a nonweak backward pointer is found and there are no weak refs 
 * on the stack to break at, the section from the top of the stack 
 * down to the thing referenced by the backward pointer is marked up 
 * as a cycle (possibly extending the cycle which that thing already 
 * belongs to). Therefore weak refs never occur inside cycles. 
 * 
 * Several separate cycles may be present on the stack simultaneously. 
 * That happens when a subcycle which is referenced one way from an 
 * earlier cycle is encountered. E.g. 
 * 
 *                          L--.        L--. 
 *                        t1    t2 -> t3    t4 
 *                          `--7        `--7 
 * 
 * where the visit order is t1, t2, t3 and then t4. Because of the 
 * stack which causes a subcycle to always be added to the top, it can 
 * be handled independently of the earlier cycles, and those earlier 
 * cycles can also be extended later on when the subcycle has been 
 * popped off. If a ref from the subcycle to an earlier cycle is 
 * found, that means that both are really the same cycle, and the 
 * frames in the former subcycle will instead become a cycle piece 
 * list on a frame in the former preceding cycle. 
 * 
 * Cycles are always kept continuous on the recursion stack. Since 
 * breaking a weak ref doesn't mark up a cycle, it's necessary to 
 * rotate between whole cycles when a weak ref is broken. I.e: 
 * 
 *                                             weak 
 *  ... <=> t1a <=> t1b <=> t1c <=> ... <=> t2 <-> t3 <=> ... <=> t4 
 * 
 * Here all the t1 things are members of a cycle, with t1a being the 
 * first and t1c the last. Let's say a nonweak pointer is found from 
 * t4 to t1b, and the weak link between t2 and t3 is chosen to be 
 * broken. In this case the whole t1 cycle is rotated up: 
 * 
 *     broken 
 *  ... <#> t3 <=> ... <=> t4 <=> t1a <=> t1b <=> t1c <=> ... <=> t2 
 * 
 * This way the t1 cycle can continue to be processed independently 
 * and possibly be popped off separately from the segment between t3 
 * and t4. 
 * 
 * Since the link frames are kept in substacks attached to the rec 
 * frames, they get rotated with the rec frames. This has the effect 
 * that the links from the top rec frame on the stack always are 
 * tested first. That is necessary to avoid clobbering weak ref 
 * partitions. Example: 
 * 
 *                             weak          weak 
 *                   t1 <=> t2 <-> t3 <=> t4 <-> t5 
 * 
 * A nonweak ref is found from t5 to t2. We get this after rotation 
 * (assuming t1 and t2 aren't part of the same cycle): 
 * 
 *                     broken         weak 
 *                   t1 <#> t5 <=> t2 <-> t3 <=> t4 
 * 
 * Now, if we would continue to follow the links from t5 and encounter 
 * a new thing t7, we'd have to add it to the top. If that ref isn't 
 * weak we'd have to blank out the weak flag which could be used in 
 * other rotations above t5 (e.g. if a normal ref from t4 to t2 is 
 * encountered). To avoid this we do the t4 links instead and continue 
 * with t5 when t4, t3 and t2 are done. 
 * 
 * As said earlier, rec frames are moved to cycle piece lists when 
 * they are popped off while being part of unfinished cycles. Since 
 * there are no more outgoing refs at that point, there can be no more 
 * rotations that affect the order between the rec frame and its 
 * predecessor. Therefore the order on a cycle piece list is optimal 
 * (in as far as the gc destruct order policy goes). Any further 
 * rotations can move the predecessor around, but it can always be 
 * treated as one unit together with its cycle piece list. 
 * 
 * If the preceding frame already has a cycle piece list when a rec 
 * frame should be added to it, the rec frame (and its attached cycle 
 * piece list) is linked in before that list. That since the rec frame 
 * might have refs to the earlier cycle piece list, but the opposite 
 * can't happen. 
 * 
 * When a cycle identifier frame is popped off the stack, the frame 
 * together with its cycle piece list represent the complete cycle, 
 * and the list holds an optimal order for destructing it. The frames 
 * are freed at that point, except for the ones which correspond to 
 * live objects, which instead are linked in order into the beginning 
 * of the kill list. That list, whose beginning is pointed to by 
 * kill_list, holds the final destruct order for all live objects. 
 * 
 * Note that the complete cycle has to be added to the kill list at 
 * once since all live objects that are referenced single way from the 
 * cycle should be destructed later and must therefore be put on the 
 * kill list before the cycle. 
 * 
 * The cycle check functions might recurse another round through the 
 * frames that have been recursed already, to propagate the GC_LIVE 
 * flag to things that have been found to be referenced from live 
 * objects. In this mode a single dummy rec frame with the 
 * GC_MARK_LIVE bit is pushed on the recursion stack, and all link 
 * frames are stacked in it, regardless of the things they originate 
 * from. Nothing else happens while this is done, i.e. no rotations 
 * and so forth, so the dummy frame always stays at the top until it's 
 * removed again. 
 * 
 * *)  Here "breaking" a ref doesn't mean that it actually gets 
 *     zeroed out. It's only disregarded to resolve the cycle to 
 *     produce an optimal destruct order. I.e. it will still be intact 
 *     when the first object in the cycle is destructed, and it will 
 *     only be zeroed when the thing it points to has been destructed. 
 */ 
 
/* The free extra list. See note in gc_delayed_free. */ 
struct free_extra_frame 
{ 
  void *data; 
  struct free_extra_frame *next; /* Next pointer. */ 
  int type;                 /* The type of the thing. */ 
}; 
static struct free_extra_frame *free_extra_list = NULL; 
 
#ifdef PIKE_DEBUG 
static unsigned delayed_freed, weak_freed, checked, marked, cycle_checked, live_ref; 
static unsigned mark_live, frame_rot, link_search; 
static unsigned gc_extra_refs = 0; 
static unsigned tot_cycle_checked = 0, tot_mark_live = 0, tot_frame_rot = 0; 
static unsigned gc_rec_frame_seq_max; 
#endif 
 
static unsigned rec_frames, link_frames, free_extra_frames; 
static unsigned max_rec_frames, max_link_frames; 
static unsigned tot_max_rec_frames = 0, tot_max_link_frames = 0, tot_max_free_extra_frames = 0; 
 
struct block_allocator gc_rec_frame_allocator = 
    BA_INIT_PAGES(sizeof(struct gc_rec_frame), 2); 
 
static void really_free_gc_rec_frame(struct gc_rec_frame * f) { 
#ifdef PIKE_DEBUG 
  if (f->rf_flags & GC_FRAME_FREED) 
    gc_fatal (f->data, 0, "Freeing gc_rec_frame twice.\n"); 
  f->rf_flags |= GC_FRAME_FREED; 
  f->u.link_top = (struct link_frame *) (ptrdiff_t) -1; 
  f->prev = f->next = f->cycle_id = f->cycle_piece = 
    (struct gc_rec_frame *) (ptrdiff_t) -1; 
#endif 
  rec_frames--; 
  ba_free(&gc_rec_frame_allocator, f); 
} 
 
void count_memory_in_gc_rec_frames(size_t *num, size_t * size) { 
  ba_count_all(&gc_rec_frame_allocator, num, size); 
} 
 
/* Link and free_extra frames are approximately the same size, so let 
 * them share block_alloc area. */ 
struct ba_mixed_frame 
{ 
  union { 
    struct link_frame link; 
    struct free_extra_frame free_extra; 
    struct ba_mixed_frame *next; /* For block_alloc internals. */ 
  } u; 
}; 
 
static struct block_allocator ba_mixed_frame_allocator 
    = BA_INIT_PAGES(sizeof(struct ba_mixed_frame), 2); 
 
void count_memory_in_ba_mixed_frames(size_t *num, size_t * size) { 
  ba_count_all(&ba_mixed_frame_allocator, num, size); 
} 
 
static INLINE struct link_frame *alloc_link_frame() 
{ 
  struct ba_mixed_frame *f = ba_alloc(&ba_mixed_frame_allocator); 
  if (++link_frames > max_link_frames) 
    max_link_frames = link_frames; 
  return (struct link_frame *) f; 
} 
 
static INLINE struct free_extra_frame *alloc_free_extra_frame() 
{ 
  struct ba_mixed_frame *f = ba_alloc(&ba_mixed_frame_allocator); 
  free_extra_frames++; 
  return (struct free_extra_frame *) f; 
} 
 
static INLINE void really_free_link_frame (struct link_frame *f) 
{ 
  link_frames--; 
  ba_free(&ba_mixed_frame_allocator, f); 
} 
 
static INLINE void really_free_free_extra_frame (struct free_extra_frame *f) 
{ 
  free_extra_frames--; 
  ba_free(&ba_mixed_frame_allocator, f); 
} 
 
/* These are only collected for the sake of gc_status. */ 
static double last_garbage_ratio = 0.0; 
static enum { 
  GARBAGE_RATIO_LOW, GARBAGE_RATIO_HIGH, GARBAGE_MAX_INTERVAL 
} last_garbage_strategy = GARBAGE_RATIO_LOW; 
 
struct callback_list gc_callbacks; 
 
/* These callbacks are run early in the check pass of the gc and when 
 * locate_references is called. They are typically used to mark 
 * external references (using gc_mark_external) for debug purposes. */ 
struct callback *debug_add_gc_callback(callback_func call, 
                                       void *arg, 
                                       callback_func free_func) 
{ 
  return add_to_callback(&gc_callbacks, call, arg, free_func); 
} 
 
static void init_gc(void); 
static void gc_cycle_pop(); 
 
#undef BLOCK_ALLOC_NEXT 
#define BLOCK_ALLOC_NEXT next 
 
#undef INIT_BLOCK 
#ifdef PIKE_DEBUG 
#define INIT_BLOCK(X)                                       \ 
  (X)->flags=(X)->refs=(X)->weak_refs=(X)->xrefs=0;     \ 
  (X)->saved_refs=-1;                                   \ 
  (X)->frame = 0; 
#else 
#define INIT_BLOCK(X)                                       \ 
  (X)->flags=(X)->refs=(X)->weak_refs=0;                \ 
  (X)->frame = 0; 
#endif 
#undef EXIT_BLOCK 
#define EXIT_BLOCK(f) 
 
#undef get_marker 
#define get_marker debug_get_marker 
#undef find_marker 
#define find_marker debug_find_marker 
 
PTR_HASH_ALLOC_FIXED_FILL_PAGES(marker,2) 
 
#undef get_marker 
#define get_marker(X) ((struct marker *) debug_malloc_pass(debug_get_marker(X))) 
#undef find_marker 
#define find_marker(X) ((struct marker *) debug_malloc_pass(debug_find_marker(X))) 
 
PMOD_EXPORT struct marker *pmod_get_marker (void *p) 
{ 
  return debug_get_marker (p); 
} 
 
PMOD_EXPORT struct marker *pmod_find_marker (void *p) 
{ 
  return debug_find_marker (p); 
} 
 
#if defined (PIKE_DEBUG) || defined (GC_MARK_DEBUG) 
PMOD_EXPORT void *gc_found_in = NULL; 
PMOD_EXPORT int gc_found_in_type = PIKE_T_UNKNOWN; 
PMOD_EXPORT const char *gc_found_place = NULL; 
#endif 
 
#ifdef DO_PIKE_CLEANUP 
/* To keep the markers after the gc. Only used for the leak report at exit. */ 
int gc_keep_markers = 0; 
PMOD_EXPORT int gc_external_refs_zapped = 0; 
#endif 
 
#if defined (PIKE_DEBUG) || defined (GC_CYCLE_DEBUG) 
 
static void describe_rec_frame (struct gc_rec_frame *f) 
{ 
  fprintf (stderr, "data=%p rf_flags=0x%02x prev=%p next=%p " 
           "cycle_id=%p cycle_piece=%p link_top/last_cycle_piece=%p", 
           f->data, f->rf_flags, f->prev, f->next, 
           f->cycle_id, f->cycle_piece, f->u.link_top); 
} 
 
/* If p* isn't NULL then p*_name will be written out next to the 
 * matching frame in the stack, if any is found. */ 
static void describe_rec_stack (struct gc_rec_frame *p1, const char *p1_name, 
                                struct gc_rec_frame *p2, const char *p2_name, 
                                struct gc_rec_frame *p3, const char *p3_name) 
{ 
  struct gc_rec_frame *l, *cp; 
  size_t longest = 0; 
 
  if (p1) longest = strlen (p1_name); 
  if (p2) {size_t l = strlen (p2_name); if (l > longest) longest = l;} 
  if (p3) {size_t l = strlen (p3_name); if (l > longest) longest = l;} 
  longest++; 
 
  /* Note: Stack is listed from top to bottom, but cycle piece lists 
   * are lists from first to last, i.e. reverse order. */ 
 
  for (l = stack_top; l != &sentinel_frame; l = l->prev) { 
    size_t c = 0; 
 
    if (!l) {fputs ("  <broken prev link in rec stack>\n", stderr); break;} 
    fprintf (stderr, "  %p", l); 
 
    if (l == p1) {fprintf (stderr, " %s", p1_name); c += strlen (p1_name) + 1;} 
    if (l == p2) {fprintf (stderr, " %s", p2_name); c += strlen (p2_name) + 1;} 
    if (l == p3) {fprintf (stderr, " %s", p3_name); c += strlen (p3_name) + 1;} 
    fprintf (stderr, ": %*s", c < longest ? (int) (longest - c) : 0, ""); 
 
    describe_rec_frame (l); 
    fputc ('\n', stderr); 
 
    for (cp = l->cycle_piece; cp; cp = cp->cycle_piece) { 
      fprintf (stderr, "    %p", cp); 
 
      c = 0; 
      if (cp == p1) {fprintf (stderr, " %s", p1_name); c += strlen (p1_name)+1;} 
      if (cp == p2) {fprintf (stderr, " %s", p2_name); c += strlen (p2_name)+1;} 
      if (cp == p3) {fprintf (stderr, " %s", p3_name); c += strlen (p3_name)+1;} 
      fprintf (stderr, ": %*s", c < longest ? (int) (longest - c) : 0, ""); 
 
      describe_rec_frame (cp); 
      fputc ('\n', stderr); 
    } 
  } 
} 
 
#endif 
 
#ifdef PIKE_DEBUG 
 
int gc_in_cycle_check = 0; 
 
static int gc_is_watching = 0; 
 
int attempt_to_identify(void *something, void **inblock) 
{ 
  size_t i; 
  struct array *a; 
  struct object *o; 
  struct program *p; 
  struct mapping *m; 
  struct multiset *mu; 
  struct pike_type *t; 
  struct callable *c; 
 
  if (inblock) *inblock = 0; 
 
  for (a = first_array; a; a = a->next) { 
    if(a==(struct array *)something) return T_ARRAY; 
  } 
 
  for(o=first_object;o;o=o->next) { 
    if(o==(struct object *)something) 
      return T_OBJECT; 
    if (o->storage && o->prog && 
        (char *) something >= o->storage && 
        (char *) something < o->storage + o->prog->storage_needed) { 
      if (inblock) *inblock = (void *) o; 
      return T_STORAGE; 
    } 
  } 
 
  for(p=first_program;p;p=p->next) 
    if(p==(struct program *)something) 
      return T_PROGRAM; 
 
  for(m=first_mapping;m;m=m->next) 
    if(m==(struct mapping *)something) 
      return T_MAPPING; 
    else if (m->data == (struct mapping_data *) something) 
      return T_MAPPING_DATA; 
 
  for(mu=first_multiset;mu;mu=mu->next) 
    if(mu==(struct multiset *)something) 
      return T_MULTISET; 
    else if (mu->msd == (struct multiset_data *) something) 
      return T_MULTISET_DATA; 
 
  if(safe_debug_findstring((struct pike_string *)something)) 
    return T_STRING; 
 
  if (pike_type_hash) 
    for (i = 0; i < pike_type_hash_size; i++) 
      for (t = pike_type_hash[i]; t; t = t->next) 
        if (t == (struct pike_type *) something) 
          return T_TYPE; 
 
  for (c = first_callable; c; c = c->next) 
    if (c == (struct callable *) something) 
      return T_STRUCT_CALLABLE; 
 
  return PIKE_T_UNKNOWN; 
} 
 
void *check_for =0; 
void *gc_svalue_location=0; 
static size_t found_ref_count; 
 
char *fatal_after_gc=0; 
 
#define DESCRIBE_MEM 1 
#define DESCRIBE_SHORT 4 
#define DESCRIBE_NO_DMALLOC 8 
 
/* type == -1 means that memblock is a char* and should be 
 * really be printed.. 
 */ 
void describe_location(void *real_memblock, 
                       int type, 
                       void *location, 
                       int indent, 
                       int depth, 
                       int flags) 
{ 
  struct program *p; 
  void *memblock=0, *descblock, *inblock; 
  if(!location) return; 
/*  fprintf(stderr,"**Location of (short) svalue: %p\n",location); */ 
 
  if(type!=-1 && real_memblock != (void *) -1) memblock=real_memblock; 
 
#ifdef DEBUG_MALLOC 
  if(memblock == 0 || type == -1) 
  { 
    extern void *dmalloc_find_memblock_base(void *); 
    memblock=dmalloc_find_memblock_base(location); 
  } 
#endif 
 
  if(type==PIKE_T_UNKNOWN) 
    type=attempt_to_identify(memblock, &inblock); 
 
  if(memblock) 
    fprintf(stderr,"%*s-> from %s %p offset %"PRINTPTRDIFFT"d\n", 
            indent,"", 
            get_name_of_type(type), 
            memblock, 
            (char *)location - (char *)memblock); 
  else 
    fprintf(stderr,"%*s-> at location %p%s\n", 
            indent,"", 
            location, 
            real_memblock == (void *) -1 ? "" :  " in unknown memblock (mmaped?)"); 
 
 again: 
  descblock = memblock; 
  switch(type) 
  { 
    case PIKE_T_UNKNOWN: 
      for(p=first_program;p;p=p->next) 
      { 
        if(memblock == (void *)p->program) 
        { 
          fprintf(stderr,"%*s  **In memory block for program at %p\n", 
                  indent,"", 
                  p); 
          memblock=p; 
          type=T_PROGRAM; 
          goto again; 
        } 
      } 
      break; 
       
    case T_PROGRAM: 
    { 
      ptrdiff_t e; 
      char *ptr=(char *)location; 
      p=(struct program *)memblock; 
 
      if(location == (void *)&p->prev) 
        fprintf(stderr,"%*s  **In p->prev\n",indent,""); 
 
      if(location == (void *)&p->next) 
        fprintf(stderr,"%*s  **In p->next\n",indent,""); 
 
      if(location == (void *)&p->parent) 
        fprintf(stderr,"%*s  **In p->parent\n",indent,""); 
 
      if(p->inherits && 
         ptr >= (char *)p->inherits  && 
         ptr < (char*)(p->inherits+p->num_inherits))  
      { 
        e=((char *)ptr - (char *)(p->inherits)) / sizeof(struct inherit); 
        fprintf(stderr,"%*s  **In p->inherits[%"PRINTPTRDIFFT"d] (%s)\n",indent,"", 
                e, p->inherits[e].name ? p->inherits[e].name->str : "no name"); 
        break; 
      } 
 
      if(p->constants && 
         ptr >= (char *)p->constants  && 
         ptr < (char*)(p->constants+p->num_constants)) 
      { 
        e = ((char *)ptr - (char *)(p->constants)) / 
          sizeof(struct program_constant); 
#if 0 
      fprintf(stderr,"%*s  **In p->constants[%"PRINTPTRDIFFT"d] (%s)\n",indent,"", 
                e, p->constants[e].name ? p->constants[e].name->str : "no name"); 
#else /* !0 */ 
      fprintf(stderr,"%*s  **In p->constants[%"PRINTPTRDIFFT"d] " 
                "(%"PRINTPTRDIFFT"d)\n",indent,"", 
                e, p->constants[e].offset); 
#endif /* 0 */ 
      break; 
      } 
 
 
      if(p->identifiers &&  
         ptr >= (char *)p->identifiers  && 
         ptr < (char*)(p->identifiers+p->num_identifiers)) 
      { 
        e = ((char *)ptr - (char *)(p->identifiers)) / 
          sizeof(struct identifier); 
 
        fprintf(stderr,"%*s  **In p->identifiers[%"PRINTPTRDIFFT"d] (%s)\n",indent,"", 
                e, p->identifiers[e].name ? 
                (strlen(p->identifiers[e].name->str)<100 ? p->identifiers[e].name->str : "Name too long or already freed.."  ) 
                : "no name"); 
        break; 
      } 
 
#define FOO(NUMTYPE,TYPE,ARGTYPE,NAME) \ 
    if(location == (void *)&p->NAME) fprintf(stderr,"%*s  **In p->" #NAME "\n",indent,""); \ 
    if(ptr >= (char *)p->NAME  && ptr<(char*)(p->NAME+p->PIKE_CONCAT(num_,NAME))) \ 
      fprintf(stderr,"%*s  **In p->" #NAME "[%"PRINTPTRDIFFT"d]\n",indent,"", \ 
              ((char *)ptr - (char *)(p->NAME)) / sizeof(TYPE)); 
#include "program_areas.h" 
       
      break; 
    } 
 
    case T_OBJECT: 
    { 
      struct object *o=(struct object *)memblock; 
      struct program *p; 
 
      if(o->prog && o->prog->flags & PROGRAM_USES_PARENT) 
      { 
        if(location == (void *)&PARENT_INFO(o)->parent) 
          fprintf(stderr,"%*s  **In o->parent\n",indent,""); 
      } 
      if(location == (void *)&o->prog)  fprintf(stderr,"%*s  **In o->prog\n",indent,""); 
      if(location == (void *)&o->next)  fprintf(stderr,"%*s  **In o->next\n",indent,""); 
      if(location == (void *)&o->prev)  fprintf(stderr,"%*s  **In o->prev\n",indent,""); 
 
      p=o->prog; 
 
      if(!o->prog) 
      { 
        p=id_to_program(o->program_id); 
        if(p) 
          fprintf(stderr,"%*s  **(We are lucky, found program for destructed object)\n",indent,""); 
      } 
 
      if(p) 
      { 
        INT32 e,d; 
        for(e=0;e<(INT32)p->num_inherits;e++) 
        { 
          struct inherit tmp=p->inherits[e]; 
          char *base=o->storage + tmp.storage_offset; 
           
          for(d=0;d<(INT32)tmp.prog->num_identifiers;d++) 
          { 
            struct identifier *id=tmp.prog->identifiers+d; 
            if(!IDENTIFIER_IS_VARIABLE(id->identifier_flags)) continue; 
             
            if(location == (void *)(base + id->func.offset)) 
            { 
              fprintf(stderr,"%*s  **In variable %s\n",indent,"",id->name->str); 
            } 
          } 
 
          if((char *)location >= base && (char *)location <= base + 
             ( tmp.prog->storage_needed - tmp.prog->inherits[0].storage_offset )) 
          { 
            fprintf(stderr,"%*s  **In storage for inherit %d",indent,"",e); 
            if(tmp.name && !tmp.name->size_shift) 
              fprintf(stderr," (%s)",tmp.name->str); 
            fprintf(stderr,"\n"); 
          } 
              
        } 
      } 
      break; 
    } 
 
    case T_STORAGE: 
      fprintf(stderr, "%*s  **In storage of object\n", indent, ""); 
      break; 
 
    case T_MULTISET: 
      descblock = ((struct multiset *) memblock)->msd; 
      /* FALL THROUGH */ 
 
    case T_MULTISET_DATA: { 
      struct multiset_data *msd = (struct multiset_data *) descblock; 
      union msnode *node = low_multiset_first (msd); 
      struct svalue ind; 
      int indval = msd->flags & MULTISET_INDVAL; 
      for (; node; node = low_multiset_next (node)) { 
        if (&node->i.ind == (struct svalue *) location) { 
          fprintf (stderr, "%*s  **In index ", indent, ""); 
          safe_print_svalue (stderr, low_use_multiset_index (node, ind)); 
          fputc ('\n', stderr); 
          break; 
        } 
        else if (indval && &node->iv.val == (struct svalue *) location) { 
          fprintf(stderr, "%*s  **In value with index ", indent, ""); 
          safe_print_svalue (stderr, low_use_multiset_index (node, ind)); 
          fputc('\n', stderr); 
          break; 
        } 
      } 
      break; 
    } 
 
    case T_ARRAY: 
    { 
      struct array *a=(struct array *)descblock; 
      struct svalue *s=(struct svalue *)location; 
 
      if(location == (void *)&a->next) 
        fprintf(stderr,"%*s  **In a->next\n",indent,""); 
 
      if(location == (void *)&a->prev) 
        fprintf(stderr,"%*s  **In a->prev\n",indent,""); 
 
      if( s-ITEM(a) > 0) 
        fprintf(stderr,"%*s  **In index number %"PRINTPTRDIFFT"d\n",indent,"", 
                s-ITEM(a)); 
      break; 
    } 
 
    case T_MAPPING: 
      descblock = ((struct mapping *) memblock)->data; 
      /* FALL THROUGH */ 
    case T_MAPPING_DATA: { 
      INT32 e; 
      struct keypair *k; 
      NEW_MAPPING_LOOP((struct mapping_data *) descblock) 
        if (&k->ind == (struct svalue *) location) { 
          fprintf(stderr, "%*s  **In index ", indent, ""); 
          safe_print_svalue (stderr, &k->ind); 
          fputc('\n', stderr); 
          break; 
        } 
        else if (&k->val == (struct svalue *) location) { 
          fprintf(stderr, "%*s  **In value with index ", indent, ""); 
          safe_print_svalue (stderr, &k->ind); 
          fputc('\n', stderr); 
          break; 
        } 
      break; 
    } 
 
    case T_PIKE_FRAME: { 
      struct pike_frame *f = (struct pike_frame *) descblock; 
      if (f->locals) {              /* Paranoia. */ 
        ptrdiff_t pos = (struct svalue *) location - f->locals; 
        if (pos >= 0) { 
          if (pos < f->num_args) 
            fprintf (stderr, "%*s  **In argument %"PRINTPTRDIFFT"d\n", 
                     indent, "", pos); 
          else 
            fprintf (stderr, "%*s  **At position %"PRINTPTRDIFFT"d among locals\n", 
                     indent, "", pos - f->num_args); 
          /* Don't describe current_object for the frame. */ 
          flags |= DESCRIBE_SHORT; 
        } 
      } 
      break; 
    } 
  } 
 
  if(memblock && depth>0) 
    describe_something(memblock,type,indent+2,depth-1,flags,inblock); 
 
#ifdef DEBUG_MALLOC 
  /* FIXME: Is the following call correct? 
   * Shouldn't the second argument be an offset? 
   */ 
  /* dmalloc_describe_location(descblock, location, indent); */ 
  /* My attempt to fix it, although I'm not really sure: /mast */ 
  if (memblock) 
    dmalloc_describe_location(memblock, (char *) location - (char *) memblock, indent); 
#endif 
} 
 
#ifdef GC_STACK_DEBUG 
static void describe_link_frame (struct link_frame *f) 
{ 
  fprintf (stderr, "data=%p prev=%p checkfn=%p weak=%d", 
           f->data, f->prev, f->checkfn, f->weak); 
} 
#endif 
 
static void describe_marker(struct marker *m) 
{ 
  if (m) { 
    fprintf(stderr, "marker at %p: flags=0x%05lx refs=%d weak=%d " 
            "xrefs=%d saved=%d frame=%p", 
            m, (long) m->flags, m->refs, m->weak_refs, 
            m->xrefs, m->saved_refs, m->frame); 
    if (m->frame) { 
      fputs(" [", stderr); 
      describe_rec_frame (m->frame); 
      putc(']', stderr); 
    } 
    putc('\n', stderr); 
  } 
  else 
    fprintf(stderr, "no marker\n"); 
} 
 
#endif /* PIKE_DEBUG */ 
 
static void debug_gc_fatal_va (void *DEBUGUSED(a), int DEBUGUSED(type), int DEBUGUSED(flags), 
                               const char *fmt, va_list args) 
{ 
  int orig_gc_pass = Pike_in_gc; 
 
  (void) vfprintf(stderr, fmt, args); 
 
#ifdef PIKE_DEBUG 
  if (a) { 
    void *inblock; 
    /* Temporarily jumping out of gc to avoid being caught in debug 
     * checks in describe(). */ 
    Pike_in_gc = 0; 
    if (type == PIKE_T_UNKNOWN) 
      type = attempt_to_identify (a, &inblock); 
    describe_something (a, type, 0, 0, 0, inblock); 
    if (flags & 1) locate_references(a); 
    Pike_in_gc = orig_gc_pass; 
  } 
 
  if (flags & 2) 
    fatal_after_gc = "Fatal in garbage collector.\n"; 
  else 
#endif 
  { 
    d_flag = 0; /* The instruction backlog is never of any use here. */ 
    debug_fatal (NULL); 
  } 
} 
 
void debug_gc_fatal (void *a, int flags, const char *fmt, ...) 
{ 
  va_list args; 
  va_start (args, fmt); 
  debug_gc_fatal_va (a, PIKE_T_UNKNOWN, flags, fmt, args); 
  va_end (args); 
} 
 
void debug_gc_fatal_2 (void *a, int type, int flags, const char *fmt, ...) 
{ 
  va_list args; 
  va_start (args, fmt); 
  debug_gc_fatal_va (a, type, flags, fmt, args); 
  va_end (args); 
} 
 
#ifdef PIKE_DEBUG 
 
static void dloc_gc_fatal (const char *file, INT_TYPE line, 
                           void *a, int flags, const char *fmt, ...) 
{ 
  va_list args; 
  fprintf (stderr, "%s:%ld: GC fatal:\n", file, (long)line); 
  va_start (args, fmt); 
  debug_gc_fatal_va (a, PIKE_T_UNKNOWN, flags, fmt, args); 
  va_end (args); 
} 
 
static void rec_stack_fatal (struct gc_rec_frame *DEBUGUSED(err), 
                             const char *DEBUGUSED(err_name), 
                             struct gc_rec_frame *DEBUGUSED(p1),  
                             const char *DEBUGUSED(p1n), 
                             struct gc_rec_frame *DEBUGUSED(p2),  
                             const char *DEBUGUSED(p2n), 
                             const char *file, INT_TYPE line, 
                             const char *fmt, ...) 
{ 
  va_list args; 
  va_start (args, fmt); 
  fprintf (stderr, msg_fatal_error, file, line); 
  (void) vfprintf (stderr, fmt, args); 
#if defined (PIKE_DEBUG) || defined (GC_CYCLE_DEBUG) 
  fputs ("Recursion stack:\n", stderr); 
  describe_rec_stack (err, err_name, p1, p1n, p2, p2n); 
  if (err) { 
    fprintf (stderr, "Describing frame %p: ", err); 
    describe_rec_frame (err); 
    fputc ('\n', stderr); 
  } 
#endif 
  d_flag = 0; /* The instruction backlog is never of any use here. */ 
  debug_fatal (NULL); 
  va_end (args); 
} 
 
static void gdb_gc_stop_here(void *UNUSED(a), int weak) 
{ 
  found_ref_count++; 
  fprintf(stderr,"***One %sref found%s. ", 
          weak ? "weak " : "", 
          gc_found_place ? gc_found_place : ""); 
  if (gc_found_in) { 
    if (gc_svalue_location) 
      describe_location(gc_found_in , gc_found_in_type, gc_svalue_location,0,1, 
                        DESCRIBE_SHORT); 
    else { 
      fputc('\n', stderr); 
      describe_something(gc_found_in, gc_found_in_type, 0, 0, DESCRIBE_SHORT, 0); 
    } 
  } 
  else 
    fputc('\n', stderr); 
  fprintf(stderr,"----------end------------\n"); 
} 
 
void low_describe_something(void *a, 
                            int t, 
                            int indent, 
                            int depth, 
                            int flags, 
                            void *inblock) 
{ 
  struct program *p=(struct program *)a; 
  struct marker *m; 
 
  if(depth<0) return; 
 
  if (marker_hash_table && (m = find_marker(a))) { 
    fprintf(stderr,"%*s**Got gc ",indent,""); 
    describe_marker(m); 
  } 
 
again: 
  switch(t) 
  { 
    case T_STORAGE: 
      if (!inblock) attempt_to_identify (a, &a); 
      t = T_OBJECT; 
      goto again; 
 
    case T_FUNCTION: 
      if(attempt_to_identify(a, 0) != T_OBJECT) 
      { 
        fprintf(stderr,"%*s**Builtin function!\n",indent,""); 
        break; 
      } 
      /* FALL THROUGH */ 
 
    case T_OBJECT: 
      p=((struct object *)a)->prog; 
      if(p && (p->flags & PROGRAM_USES_PARENT)) 
      { 
        fprintf(stderr,"%*s**Parent identifier: %d\n",indent,"",PARENT_INFO( ((struct object *)a) )->parent_identifier); 
      } 
      fprintf(stderr,"%*s**Program id: %d\n",indent,"",((struct object *)a)->program_id); 
 
      if (((struct object *)a)->next == ((struct object *)a)) 
        fprintf(stderr, "%*s**The object is fake.\n",indent,""); 
 
      { 
        struct object *o; 
        for (o = first_object; o && o != (struct object *) a; o = o->next) {} 
        if (!o) 
          fprintf(stderr,"%*s**The object is not on the object link list.\n",indent,""); 
        for (o = objects_to_destruct; o && o != (struct object *) a; o = o->next) {} 
        if (o) 
          fprintf(stderr,"%*s**The object is on objects_to_destruct.\n",indent,""); 
      } 
 
      { 
        struct program_state *ps; 
        for (ps = Pike_compiler; ps; ps = ps->previous) 
          if (ps->fake_object == (struct object *) a) { 
            fprintf (stderr, "%*s**The object is a fake for new program %p " 
                     "in compiler program state %p.\n", 
                     indent, "", ps->new_program, ps); 
            break; 
          } 
      } 
 
      if(!p) 
      { 
        p=id_to_program(((struct object *)a)->program_id); 
        if(p) 
          fprintf(stderr,"%*s**The object is destructed but program found from id.\n", 
                  indent,""); 
        else 
          fprintf(stderr,"%*s**The object is destructed and program not found from id.\n", 
                  indent,""); 
      } 
 
      if (p == pike_trampoline_program && ((struct object *) a)->refs > 0) { 
        /* Special hack to get something useful out of trampolines. 
         * Ought to have an event hook for this sort of thing. */ 
        struct pike_trampoline *t = 
          (struct pike_trampoline *) ((struct object *) a)->storage; 
        struct object *o = t->frame->current_object; 
        struct program *p = o->prog; 
        struct identifier *id; 
        INT_TYPE line; 
        struct pike_string *file; 
 
        fprintf (stderr, "%*s**The object is a trampoline.\n", indent, ""); 
 
        if (!p) { 
          fprintf (stderr, "%*s**The trampoline function's object " 
                   "is destructed.\n", indent, ""); 
          p = id_to_program (o->program_id); 
        } 
 
        if (p) { 
          id = ID_FROM_INT (p, t->func); 
          if (IDENTIFIER_IS_PIKE_FUNCTION(id->identifier_flags) && 
              id->func.offset >= 0 && 
              (file = get_line(p->program + id->func.offset, p, &line))) { 
            fprintf(stderr, "%*s**Function %s at %s:%ld\n", 
                    indent, "", id->name->str, file->str, (long) line); 
            free_string(file); 
          } 
          else 
            fprintf(stderr, "%*s**Function %s at unknown location.\n", 
                    indent, "", id->name->str); 
 
          if (depth && o->prog) { 
            fprintf (stderr, "%*s**Describing function's object:\n", 
                     indent, ""); 
            describe_something (o, T_OBJECT, indent + 2, depth - 1, 
                                (flags & DESCRIBE_SHORT) & ~DESCRIBE_MEM, 
                                0); 
          } 
        } 
      } 
 
      else if (((struct object *) a)->refs > 0 && p) { 
        size_t inh_idx, var_idx, var_count = 0; 
 
        if (p) { 
          fprintf (stderr, "%*s**Object variables:\n", indent, ""); 
 
          for (inh_idx = 0; inh_idx < p->num_inherits; inh_idx++) { 
            struct inherit *inh = p->inherits + inh_idx; 
            struct program *p2 = inh->prog; 
 
            if (inh->inherit_level) { 
              if (inh->name) { 
                fprintf (stderr, "%*s**%*s=== In inherit ", 
                         indent, "", inh->inherit_level + 1, ""); 
                safe_print_short_svalue (stderr, (union anything *) &inh->name, 
                                         T_STRING); 
                fprintf (stderr, ", program %d:\n", inh->prog->id); 
              } 
              else 
                fprintf (stderr, 
                         "%*s**%*s=== In nameless inherit, program %d:\n", 
                         indent, "", inh->inherit_level + 1, "", inh->prog->id); 
            } 
 
            for (var_idx = 0; var_idx < p2->num_variable_index; var_idx++) { 
              struct identifier *id = 
                p2->identifiers + p2->variable_index[var_idx]; 
              if (id->run_time_type != PIKE_T_FREE && 
                  id->run_time_type != PIKE_T_GET_SET) { 
                void *ptr; 
 
                fprintf (stderr, "%*s**%*srtt: %-8s  name: ", 
                         indent, "", inh->inherit_level + 1, "", 
                         get_name_of_type (id->run_time_type)); 
 
                if (id->name->size_shift) 
                  safe_print_short_svalue (stderr, (union anything *) &id->name, 
                                           T_STRING); 
                else 
                  fprintf (stderr, "%-20s", id->name->str); 
 
                fprintf (stderr, "  off: %4"PRINTPTRDIFFT"d  value: ", 
                         inh->storage_offset + id->func.offset); 
 
                ptr = PIKE_OBJ_STORAGE ((struct object *) a) + 
                  inh->storage_offset + id->func.offset; 
                if (id->run_time_type == T_MIXED) 
                  safe_print_svalue_compact (stderr, (struct svalue *) ptr); 
                else 
                  safe_print_short_svalue_compact (stderr, 
                                                   (union anything *) ptr, 
                                                   id->run_time_type); 
 
                fputc ('\n', stderr); 
                var_count++; 
              } 
            } 
          } 
 
          if (!var_count) 
            fprintf (stderr, "%*s** (none)\n", indent, ""); 
        } 
 
        fprintf(stderr,"%*s**Describing program %p of object:\n",indent,"", p); 
#ifdef DEBUG_MALLOC 
      if ((INT32)(ptrdiff_t) p == 0x55555555) 
          fprintf(stderr, "%*s**Zapped program pointer.\n", indent, ""); 
        else 
#endif 
        low_describe_something(p, T_PROGRAM, indent, depth, 
                                 depth ? flags : flags | DESCRIBE_SHORT, 0); 
 
        if((p->flags & PROGRAM_USES_PARENT) && 
           LOW_PARENT_INFO(((struct object *)a),p)->parent) 
        { 
          if (depth) { 
            fprintf(stderr,"%*s**Describing parent of object:\n",indent,""); 
            describe_something( PARENT_INFO((struct object *)a)->parent, T_OBJECT, 
                                indent+2, depth-1, 
                                (flags | DESCRIBE_SHORT) & ~DESCRIBE_MEM, 
                                0); 
          } 
          else 
            fprintf (stderr, "%*s**Object got a parent.\n", indent, ""); 
        }else{ 
          fprintf(stderr,"%*s**There is no parent (any longer?)\n",indent,""); 
        } 
      } 
      break; 
 
    case T_PROGRAM: 
    { 
      char *tmp; 
      INT_TYPE line; 
      ptrdiff_t id_idx, id_count = 0; 
      struct inherit *inh = p->inherits, *next_inh = p->inherits + 1; 
      ptrdiff_t inh_id_end = p->num_identifier_references; 
 
      fprintf(stderr,"%*s**Program id: %ld, flags: %x, parent id: %d\n", 
              indent,"", (long)(p->id), p->flags, 
              p->parent ? p->parent->id : -1); 
 
      if(p->flags & PROGRAM_HAS_C_METHODS) 
      { 
        fprintf(stderr,"%*s**The program was written in C.\n",indent,""); 
      } 
 
      tmp = low_get_program_line_plain(p, &line, 1); 
      if (tmp) { 
        fprintf(stderr,"%*s**Location: %s:%ld\n", 
                indent, "", tmp, (long)line); 
        free (tmp); 
      } 
 
      if (!(flags & DESCRIBE_SHORT) && p->refs > 0) { 
        fprintf (stderr, "%*s**Identifiers:\n", indent, ""); 
 
        for (id_idx = 0; id_idx < p->num_identifier_references; id_idx++) { 
          struct reference *id_ref = p->identifier_references + id_idx; 
          struct inherit *id_inh; 
          struct identifier *id; 
          const char *type; 
          char prot[100], descr[120]; 
 
          while (next_inh < p->inherits + p->num_inherits && 
                 id_idx == next_inh->identifier_level) { 
            inh = next_inh++; 
            inh_id_end = inh->identifier_level + inh->prog->num_identifier_references; 
            if (inh->name) { 
              fprintf (stderr, "%*s**%*s=== In inherit ", 
                       indent, "", inh->inherit_level + 1, ""); 
              safe_print_short_svalue (stderr, (union anything *) &inh->name, 
                                       T_STRING); 
              fprintf (stderr, ", program %d:\n", inh->prog->id); 
            } 
            else 
              fprintf (stderr, "%*s**%*s=== In nameless inherit, program %d:\n", 
                       indent, "", inh->inherit_level + 1, "", inh->prog->id); 
          } 
 
          while (id_idx == inh_id_end) { 
            int cur_lvl = inh->inherit_level; 
            if (inh->name) { 
              fprintf (stderr, "%*s**%*s=== End of inherit ", 
                       indent, "", inh->inherit_level + 1, ""); 
              safe_print_short_svalue (stderr, (union anything *) &inh->name, 
                                       T_STRING); 
              fputc ('\n', stderr); 
            } 
            else 
              fprintf (stderr, "%*s**%*s=== End of nameless inherit\n", 
                       indent, "", inh->inherit_level + 1, ""); 
            while (inh > p->inherits) { /* Paranoia. */ 
              if ((--inh)->inherit_level < cur_lvl) break; 
            } 
            inh_id_end = inh->identifier_level + inh->prog->num_identifier_references; 
          } 
 
#if 0 
        /* Can be illuminating to see these too.. */ 
          if (id_ref->id_flags & ID_HIDDEN || 
              (id_ref->id_flags & (ID_INHERITED|ID_PRIVATE)) == 
              (ID_INHERITED|ID_PRIVATE)) continue; 
#endif 
 
          id_inh = INHERIT_FROM_PTR (p, id_ref); 
          id = id_inh->prog->identifiers + id_ref->identifier_offset; 
 
          if (IDENTIFIER_IS_ALIAS (id->identifier_flags)) type = "alias"; 
          else if (IDENTIFIER_IS_PIKE_FUNCTION (id->identifier_flags)) type = "fun"; 
          else if (IDENTIFIER_IS_FUNCTION (id->identifier_flags)) type = "cfun"; 
          else if (IDENTIFIER_IS_CONSTANT (id->identifier_flags)) type = "const"; 
          else if (IDENTIFIER_IS_VARIABLE (id->identifier_flags)) type = "var"; 
          else type = "???"; 
 
          prot[0] = prot[1] = 0; 
          if (id_ref->id_flags & ID_PRIVATE) { 
            strcat (prot, ",pri"); 
            if (!(id_ref->id_flags & ID_PROTECTED)) strcat (prot, ",!pro"); 
          } 
          else 
            if (id_ref->id_flags & ID_PROTECTED) strcat (prot, ",pro"); 
          if (id_ref->id_flags & ID_FINAL)     strcat (prot, ",fin"); 
          if (id_ref->id_flags & ID_PUBLIC)    strcat (prot, ",pub"); 
          if (id_ref->id_flags & ID_INLINE)    strcat (prot, ",inl"); 
          if (id_ref->id_flags & ID_OPTIONAL)  strcat (prot, ",opt"); 
          if (id_ref->id_flags & ID_HIDDEN)    strcat (prot, ",hid"); 
          if (id_ref->id_flags & ID_INHERITED) strcat (prot, ",inh"); 
          if (id_ref->id_flags & ID_EXTERN)    strcat (prot, ",ext"); 
          if (id_ref->id_flags & ID_VARIANT)   strcat (prot, ",var"); 
          if (id_ref->id_flags & ID_USED)      strcat (prot, ",use"); 
 
          sprintf (descr, "%s: %s", type, prot + 1); 
          fprintf (stderr, "%*s**%*s%-3"PRINTPTRDIFFT"d %-18s name: ", 
                   indent, "", id_inh->inherit_level + 1, "", id_idx, descr); 
 
          if (id->name->size_shift) 
            safe_print_short_svalue (stderr, (union anything *) &id->name, 
                                     T_STRING); 
          else 
            fprintf (stderr, "%-20s", id->name->str); 
 
          if (IDENTIFIER_IS_ALIAS(id->identifier_flags)) { 
            fprintf(stderr, "  depth: %d  id: %d", 
                    id->func.ext_ref.depth, id->func.ext_ref.id); 
          } else if (id->identifier_flags & IDENTIFIER_C_FUNCTION) 
            fprintf (stderr, "  addr: %p", id->func.c_fun); 
          else if (IDENTIFIER_IS_VARIABLE (id->identifier_flags)) { 
            if (id->run_time_type == PIKE_T_GET_SET) { 
              fprintf (stderr, "  "); 
              if (id->func.gs_info.getter >= 0) { 
                fprintf (stderr, "getter: %d(%d)", 
                         id->func.gs_info.getter, 
                         id->func.gs_info.getter + id_inh->identifier_level); 
                if (id->func.gs_info.setter >= 0) 
                  fprintf (stderr, ", "); 
              } 
              if (id->func.gs_info.setter >= 0) 
                fprintf (stderr, "setter: %d(%d)", 
                         id->func.gs_info.setter, 
                         id->func.gs_info.setter + id_inh->identifier_level); 
            } 
            else if (id->run_time_type == PIKE_T_FREE) 
              fprintf (stderr, "  extern"); 
            else 
              fprintf (stderr, "  rtt: %s  off: %"PRINTPTRDIFFT"d", 
                       get_name_of_type (id->run_time_type), id->func.offset); 
          } 
          else if (IDENTIFIER_IS_PIKE_FUNCTION (id->identifier_flags)) 
            fprintf (stderr, "  pc: %"PRINTPTRDIFFT"d", id->func.offset); 
          else if (IDENTIFIER_IS_CONSTANT (id->identifier_flags)) { 
            if (id->func.const_info.offset != -1) { 
              fputs ("  value: ", stderr); 
              safe_print_svalue_compact ( 
                stderr, &id_inh->prog->constants[id->func.const_info.offset].sval); 
            } else { 
              fputs ("  placeholder constant", stderr); 
            } 
          } 
 
          fputc ('\n', stderr); 
          id_count++; 
        } 
 
        if (!id_count) 
          fprintf (stderr, "%*s** (none)\n", indent, ""); 
      } 
 
      if(flags & DESCRIBE_MEM) 
      { 
#define FOO(NUMTYPE,TYPE,ARGTYPE,NAME) \ 
      fprintf(stderr, "%*s* " #NAME " %p[%"PRINTSIZET"u]\n", \ 
              indent, "", p->NAME, (size_t)p->PIKE_CONCAT(num_,NAME)); 
#include "program_areas.h" 
      } 
 
      break; 
    } 
 
    case T_MULTISET_DATA: { 
      int found = 0; 
      struct multiset *l; 
      for (l = first_multiset; l; l = l->next) { 
        if (l->msd == (struct multiset_data *) a) { 
          fprintf(stderr, "%*s**Describing multiset %p for this data block:\n", 
                  indent, "", l); 
          debug_dump_multiset(l); 
          found = 1; 
        } 
      } 
      if (!found) 
        fprintf (stderr, "%*s**Didn't find multiset for this data block!\n", indent, ""); 
      break; 
    } 
 
    case T_MULTISET: 
      if (((struct multiset *) a)->refs > 0) 
        debug_dump_multiset((struct multiset *) a); 
      break; 
 
    case T_ARRAY: 
      if (((struct array *) a)->refs > 0) 
        debug_dump_array((struct array *)a); 
      break; 
 
    case T_MAPPING_DATA: 
    { 
      int found = 0; 
      struct mapping *m; 
      for(m=first_mapping;m;m=m->next) 
      { 
        if(m->data == (struct mapping_data *)a) 
        { 
          fprintf(stderr,"%*s**Describing mapping for this data block:\n",indent,""); 
          debug_dump_mapping((struct mapping *)m); 
          found = 1; 
        } 
      } 
      if (!found) 
        fprintf (stderr, "%*s**Didn't find mapping for this data block!\n", indent, ""); 
      break; 
    } 
     
    case T_MAPPING: 
      if (((struct mapping *) a)->refs > 0) 
        debug_dump_mapping((struct mapping *)a); 
      break; 
 
    case T_STRING: 
    { 
      struct pike_string *s=(struct pike_string *)a; 
      fprintf(stderr,"%*s**size_shift: %d, " 
              "len: %"PRINTPTRDIFFT"d, " 
              "hash: %"PRINTSIZET"x\n", 
              indent,"", s->size_shift, s->len, s->hval); 
      if (!s->size_shift && s->refs > 0) { 
        if(s->len>77) 
        { 
          fprintf(stderr,"%*s** \"%60s\"...\n",indent,"",s->str); 
        }else{ 
          fprintf(stderr,"%*s** \"%s\"\n",indent,"",s->str); 
        } 
      } 
      break; 
    } 
 
    case PIKE_T_TYPE: 
    { 
      fprintf(stderr, "%*s**type: ", indent, ""); 
      simple_describe_type((struct pike_type *)a); 
      fprintf(stderr, "\n"); 
      break; 
    } 
 
    case T_PIKE_FRAME: { 
      struct pike_frame *f = (struct pike_frame *) a; 
      do { 
        if (f->refs <= 0) break; 
        if (f->current_object) { 
          struct program *p = f->current_object->prog; 
          if (p) { 
            struct identifier *id = ID_FROM_INT(p, f->fun); 
            INT_TYPE line; 
            struct pike_string *file; 
            if (IDENTIFIER_IS_PIKE_FUNCTION(id->identifier_flags) && 
                id->func.offset >= 0 && 
                (file = get_line(p->program + id->func.offset, p, &line))) { 
              fprintf(stderr, "%*s**Function %s at %s:%ld\n", 
                      indent, "", id->name->str, file->str, (long) line); 
              free_string(file); 
            } 
            else 
              fprintf(stderr, "%*s**Function %s at unknown location.\n", 
                      indent, "", id->name->str); 
          } 
          if (!(flags & DESCRIBE_SHORT)) { 
            fprintf(stderr, "%*s**Describing the current object:\n", indent, ""); 
            describe_something(f->current_object, T_OBJECT, indent+2, depth, flags, 0); 
          } 
        } 
        else 
          fprintf(stderr, "%*s**No current object.\n", indent, ""); 
        if ((f = f->scope)) 
          fprintf(stderr, "%*s**Moving on to outer scope frame %p:\n", indent, "", f); 
      } while (f); 
      break; 
    } 
 
    default: 
      fprintf(stderr, "%*s**Cannot describe block of type %s (%d)\n", 
              indent, "", get_name_of_type (t), t); 
  } 
} 
 
void describe_something(void *a, int t, int indent, int depth, int flags, 
                        void *inblock) 
{ 
  int tmp; 
  struct program *p=(struct program *)a; 
  if(!a) { 
    fprintf (stderr, "%*s**NULL pointer\n", indent, ""); 
    return; 
  } 
 
  if(t==-1) 
  { 
    fprintf(stderr,"%*s**Location description: %s\n",indent,"",(char *)a); 
    return; 
  } 
 
  /* Disable debug, this may help reduce recursion bugs */ 
  tmp=d_flag; 
  d_flag=0; 
 
#ifdef DEBUG_MALLOC 
  if (((INT32)(ptrdiff_t)a) == 0x55555555) { 
    fprintf(stderr,"%*s**Block: %p  Type: %s  Zapped pointer\n",indent,"",a, 
            get_name_of_type(t)); 
  } else 
#endif /* DEBUG_MALLOC */ 
    if (((ptrdiff_t)a) & 3) { 
      fprintf(stderr,"%*s**Block: %p  Type: %s  Misaligned address\n",indent,"",a, 
              get_name_of_type(t)); 
    } else { 
      fprintf(stderr,"%*s**Block: %p  Type: %s  Refs: %d\n",indent,"",a, 
              get_name_of_type(t), 
              *(INT32 *)a); 
 
      low_describe_something(a,t,indent,depth,flags,inblock); 
 
#ifdef DEBUG_MALLOC 
      if(!(flags & DESCRIBE_NO_DMALLOC)) 
        debug_malloc_dump_references(a,indent+2,depth-1,flags); 
#endif 
    } 
 
  fprintf(stderr,"%*s*******************\n",indent,""); 
  d_flag=tmp; 
} 
 
PMOD_EXPORT void describe(void *x) 
{ 
  void *inblock; 
  int type = attempt_to_identify(x, &inblock); 
  describe_something(x, type, 0, 0, 0, inblock); 
} 
 
void debug_describe_svalue(struct svalue *s) 
{ 
  fprintf(stderr,"Svalue at %p is:\n",s); 
  switch(TYPEOF(*s)) 
  { 
    case T_INT: 
      fprintf(stderr,"    %"PRINTPIKEINT"d (subtype %d)\n",s->u.integer, 
              SUBTYPEOF(*s)); 
      return; 
 
    case T_FLOAT: 
      fprintf(stderr,"    %"PRINTPIKEFLOAT"f\n",s->u.float_number); 
      return; 
 
    case T_FUNCTION: 
      if(SUBTYPEOF(*s) == FUNCTION_BUILTIN) 
      { 
        fprintf(stderr,"    Builtin function: %s\n",s->u.efun->name->str); 
      }else{ 
        if(!s->u.object->prog) 
        { 
          struct program *p=id_to_program(s->u.object->program_id); 
          if(p) 
          { 
            fprintf(stderr,"    Function in destructed object: %s\n", 
                    ID_FROM_INT(p, SUBTYPEOF(*s))->name->str); 
          }else{ 
            fprintf(stderr,"    Function in destructed object.\n"); 
          } 
        }else{ 
          fprintf(stderr,"    Function name: %s\n", 
                  ID_FROM_INT(s->u.object->prog, SUBTYPEOF(*s))->name->str); 
        } 
      } 
  } 
  describe_something(s->u.refs, TYPEOF(*s), 0, 1, 0, 0); 
} 
 
void gc_watch(void *a) 
{ 
  struct marker *m; 
  init_gc(); 
  m = get_marker(a); 
  if (!(m->flags & GC_WATCHED)) { 
    m->flags |= GC_WATCHED; 
    fprintf(stderr, "## Watching thing %p.\n", a); 
    gc_is_watching++; 
  } 
  else 
    fprintf(stderr, "## Already watching thing %p.\n", a); 
} 
 
static void gc_watched_found (struct marker *m, const char *found_in) 
{ 
  fprintf(stderr, "## Watched thing %p with %d refs found in " 
          "%s in pass %d.\n", m->data, *(INT32 *) m->data, found_in, Pike_in_gc); 
  describe_marker (m); 
} 
 
#endif /* PIKE_DEBUG */ 
 
#ifndef GC_MARK_DEBUG 
struct pike_queue gc_mark_queue; 
#define CHECK_MARK_QUEUE_EMPTY() assert (!gc_mark_queue.first) 
#else  /* GC_MARK_DEBUG */ 
 
/* Cut'n'paste from queue.c. */ 
 
struct gc_queue_entry 
{ 
  queue_call call; 
  void *data; 
  int in_type; 
  void *in; 
  const char *place; 
}; 
 
#define GC_QUEUE_ENTRIES 8191 
 
struct gc_queue_block 
{ 
  struct gc_queue_block *next; 
  int used; 
  struct gc_queue_entry entries[GC_QUEUE_ENTRIES]; 
}; 
 
struct gc_queue_block *gc_mark_first = NULL, *gc_mark_last = NULL; 
 
#define CHECK_MARK_QUEUE_EMPTY() assert (!gc_mark_first) 
 
void gc_mark_run_queue(void) 
{ 
  struct gc_queue_block *b; 
 
  while((b=gc_mark_first)) 
  { 
    int e; 
    for(e=0;e<b->used;e++) 
    { 
      debug_malloc_touch(b->entries[e].data); 
      b->entries[e].call(b->entries[e].data); 
    } 
 
    gc_mark_first=b->next; 
    free((char *)b); 
  } 
  gc_mark_last=0; 
} 
 
void gc_mark_discard_queue(void) 
{ 
  struct gc_queue_block *b = gc_mark_first; 
  while (b) 
  { 
    struct gc_queue_block *next = b->next; 
    free((char *) b); 
    b = next; 
  } 
  gc_mark_first = gc_mark_last = 0; 
} 
 
void gc_mark_enqueue (queue_call call, void *data) 
{ 
  struct gc_queue_block *b; 
 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc != GC_PASS_MARK && Pike_in_gc != GC_PASS_ZAP_WEAK) 
    gc_fatal (data, 0, "gc_mark_enqueue() called in invalid gc pass.\n"); 
  if (gc_found_in_type == PIKE_T_UNKNOWN || !gc_found_in) 
    gc_fatal (data, 0, "gc_mark_enqueue() called outside GC_ENTER.\n"); 
  { 
    struct marker *m; 
    if (gc_is_watching && (m = find_marker(data)) && m->flags & GC_WATCHED) { 
      /* This is useful to set breakpoints on. */ 
      gc_watched_found (m, "gc_mark_enqueue()"); 
    } 
  } 
#endif 
 
  b=gc_mark_last; 
  if(!b || b->used >= GC_QUEUE_ENTRIES) 
  { 
    b = (struct gc_queue_block *) malloc (sizeof (struct gc_queue_block)); 
    if (!b) fatal ("Out of memory in gc.\n"); 
    b->used=0; 
    b->next=0; 
    if(gc_mark_first) 
      gc_mark_last->next=b; 
    else 
      gc_mark_first=b; 
    gc_mark_last=b; 
  } 
 
  b->entries[b->used].call=call; 
  b->entries[b->used].data=debug_malloc_pass(data); 
  b->entries[b->used].in_type = gc_found_in_type; 
  b->entries[b->used].in = debug_malloc_pass (gc_found_in); 
  b->entries[b->used].place = gc_found_place; 
  b->used++; 
} 
 
#endif        /* GC_MARK_DEBUG */ 
 
void debug_gc_touch(void *a) 
{ 
  struct marker *m; 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_touch()"); 
  } 
#endif 
 
  if (!a) Pike_fatal("Got null pointer.\n"); 
 
  switch (Pike_in_gc) { 
    case GC_PASS_PRETOUCH: 
      m = find_marker(a); 
      if ( 
#ifdef DO_PIKE_CLEANUP 
        !gc_keep_markers && 
#endif 
        m && !(m->flags & (GC_PRETOUCHED 
#ifdef PIKE_DEBUG 
                           |GC_WATCHED 
#endif 
                          ))) 
        gc_fatal(a, 1, "Thing got an existing but untouched marker.\n"); 
      m = get_marker(a); 
      m->flags |= GC_PRETOUCHED; 
#ifdef PIKE_DEBUG 
      m->saved_refs = *(INT32 *) a; 
#endif 
      break; 
 
    case GC_PASS_POSTTOUCH: { 
#ifdef PIKE_DEBUG 
      int extra_ref; 
#endif 
      m = find_marker(a); 
      if (!m) 
        gc_fatal(a, 1, "Found a thing without marker.\n"); 
      else if (!(m->flags & GC_PRETOUCHED)) 
        gc_fatal(a, 1, "Thing got an existing but untouched marker.\n"); 
      if (gc_destruct_everything && (m->flags & GC_MARKED)) 
        gc_fatal (a, 1, "Thing got marked in gc_destruct_everything mode.\n"); 
#ifdef PIKE_DEBUG 
      extra_ref = (m->flags & GC_GOT_EXTRA_REF) == GC_GOT_EXTRA_REF; 
      if (m->saved_refs + extra_ref < *(INT32 *) a) 
        if (m->flags & GC_WEAK_FREED) 
          gc_fatal(a, 1, "Something failed to remove weak reference(s) to thing, " 
                   "or it has gotten more references since gc start.\n"); 
        else 
          gc_fatal(a, 1, "Thing has gotten more references since gc start.\n"); 
      else 
        if (m->weak_refs > m->saved_refs) 
          gc_fatal(a, 0, "A thing got more weak references than references.\n"); 
#endif 
      m->flags |= GC_POSTTOUCHED; 
      break; 
    } 
 
    default: 
      Pike_fatal("debug_gc_touch() used in invalid gc pass.\n"); 
  } 
} 
 
#ifdef PIKE_DEBUG 
 
static INLINE struct marker *gc_check_debug(void *a, int weak) 
{ 
  struct marker *m; 
 
  if (!a) Pike_fatal("Got null pointer.\n"); 
  if(Pike_in_gc == GC_PASS_LOCATE) 
  { 
    if(check_for == a) 
    { 
      gdb_gc_stop_here(a, weak); 
    } 
    return 0; 
  } 
 
#if 0 
  fprintf (stderr, "Ref: %s %p -> %p%s\n", 
           get_name_of_type (gc_found_in_type), gc_found_in, a, 
           gc_found_place ? gc_found_place : ""); 
#endif 
 
  if (Pike_in_gc != GC_PASS_CHECK) 
    Pike_fatal("gc check attempted in invalid pass.\n"); 
 
  m = get_marker(a); 
 
  if (!*(INT32 *)a) 
    gc_fatal(a, 1, "GC check on thing without refs.\n"); 
  if (m->saved_refs == -1) m->saved_refs = *(INT32 *)a; 
  else if (m->saved_refs != *(INT32 *)a) 
    gc_fatal(a, 1, "Refs changed in gc check pass.\n"); 
  if (m->refs + m->xrefs >= *(INT32 *) a) 
    /* m->refs will be incremented by the caller. */ 
    gc_fatal (a, 1, "Thing is getting more internal refs (%d + %d) " 
              "than refs (%d).\n" 
              "(Could be an extra free somewhere, or " 
              "a pointer might have been checked more than once.)\n", 
              m->refs, m->xrefs, *(INT32 *) a); 
  checked++; 
 
  return m; 
} 
 
#endif /* PIKE_DEBUG */ 
 
PMOD_EXPORT INT32 real_gc_check(void *a) 
{ 
  struct marker *m; 
  INT32 ret; 
 
#ifdef PIKE_DEBUG 
  if (gc_found_in_type == PIKE_T_UNKNOWN || !gc_found_in) 
    gc_fatal (a, 0, "gc_check() called outside GC_ENTER.\n"); 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_check()"); 
  } 
  if (!(m = gc_check_debug(a, 0))) return 0; 
#else 
  m = get_marker(a); 
#endif 
 
  ret=m->refs; 
  add_ref(m); 
  if (m->refs == *(INT32 *) a) 
    m->flags |= GC_NOT_REFERENCED; 
  return ret; 
} 
 
PMOD_EXPORT INT32 real_gc_check_weak(void *a) 
{ 
  struct marker *m; 
  INT32 ret; 
 
#ifdef PIKE_DEBUG 
  if (gc_found_in_type == PIKE_T_UNKNOWN || !gc_found_in) 
    gc_fatal (a, 0, "gc_check_weak() called outside GC_ENTER.\n"); 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_check_weak()"); 
  } 
  if (!(m = gc_check_debug(a, 1))) return 0; 
  if (m->weak_refs < 0) 
    gc_fatal(a, 1, "Thing has already reached threshold for weak free.\n"); 
  if (m->weak_refs >= *(INT32 *) a) 
    gc_fatal(a, 1, "Thing has gotten more weak refs than refs.\n"); 
  if (m->weak_refs > m->refs + 1) 
    gc_fatal(a, 1, "Thing has gotten more weak refs than internal refs.\n"); 
#else 
  m = get_marker(a); 
#endif 
 
  m->weak_refs++; 
  gc_ext_weak_refs++; 
  if (m->weak_refs == *(INT32 *) a) 
    m->weak_refs = -1; 
 
  ret=m->refs; 
  add_ref(m); 
  if (m->refs == *(INT32 *) a) 
    m->flags |= GC_NOT_REFERENCED; 
  return ret; 
} 
 
static void cleanup_markers (void) 
{ 
#ifdef DO_PIKE_CLEANUP 
  size_t e=0; 
 
  if (gc_keep_markers) { 
    /* Carry over any GC_CLEANUP_LEAKED flags but reinitialize them 
     * otherwise. */ 
    for(e=0;e<marker_hash_table_size;e++) { 
      struct marker *m; 
      for (m = marker_hash_table[e]; m; m = m->next) { 
#ifdef PIKE_DEBUG 
      m->flags &= GC_CLEANUP_LEAKED; 
        m->xrefs = 0; 
        m->saved_refs = -1; 
#else 
      m->flags = 0; 
#endif 
      m->refs = m->weak_refs = 0; 
        m->frame = 0; 
      } 
    } 
    return; 
  } 
 
  for(e=0;e<marker_hash_table_size;e++) 
    while(marker_hash_table[e]) 
      remove_marker(marker_hash_table[e]->data); 
#endif 
  exit_marker_hash(); 
} 
 
static void init_gc(void) 
{ 
#ifdef PIKE_DEBUG 
  if (!gc_is_watching) { 
#endif 
#if defined (PIKE_DEBUG) || defined (DO_PIKE_CLEANUP) 
    /* The marker hash table is left around after a previous gc if 
     * gc_keep_markers is set. */ 
    if (marker_hash_table) cleanup_markers(); 
    if (!marker_hash_table) 
#endif 
      low_init_marker_hash(num_objects); 
#ifdef PIKE_DEBUG 
  } 
#endif 
} 
 
void exit_gc(void) 
{ 
  if (gc_evaluator_callback) { 
    remove_callback(gc_evaluator_callback); 
    gc_evaluator_callback = NULL; 
  } 
  if (!gc_keep_markers) 
    cleanup_markers(); 
 
  ba_free_all(&gc_rec_frame_allocator); 
  ba_free_all(&ba_mixed_frame_allocator); 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching) { 
    fprintf(stderr, "## Exiting gc and resetting watches for %d things.\n", 
            gc_is_watching); 
    gc_is_watching = 0; 
  } 
#endif 
} 
 
#ifdef PIKE_DEBUG 
 
PMOD_EXPORT void gc_check_zapped (void *a, TYPE_T type, const char *file, INT_TYPE line) 
{ 
  struct marker *m = find_marker (a); 
  if (m && (m->flags & GC_CLEANUP_LEAKED)) 
    fprintf (stderr, "Free of leaked %s %p from %s:%ld, %d refs remaining\n", 
             get_name_of_type (type), a, file, (long)line, *(INT32 *)a - 1); 
} 
 
/* This function marks some known externals. The rest are handled by 
 * callbacks added with add_gc_callback. */ 
static void mark_externals (void) 
{ 
  struct mapping *constants; 
  if (master_object) 
    gc_mark_external (master_object, " as master_object"); 
  if ((constants = get_builtin_constants())) 
    gc_mark_external (constants, " as global constants mapping"); 
} 
 
void locate_references(void *a) 
{ 
  int tmp, orig_in_gc = Pike_in_gc; 
  const char *orig_gc_found_place = gc_found_place; 
  int i=0; 
  if(!marker_hash_table) 
  { 
    i=1; 
    init_gc(); 
  } 
  Pike_in_gc = GC_PASS_LOCATE; 
  gc_found_place = NULL; 
 
  /* Disable debug, this may help reduce recursion bugs */ 
  tmp=d_flag; 
  d_flag=0; 
 
  fprintf(stderr,"**Looking for references to %p:\n", a); 
   
  check_for=a; 
  found_ref_count = 0; 
 
  GC_ENTER (NULL, PIKE_T_UNKNOWN) { 
    mark_externals(); 
    call_callback(& gc_callbacks, NULL); 
 
    gc_check_all_arrays(); 
    gc_check_all_multisets(); 
    gc_check_all_mappings(); 
    gc_check_all_programs(); 
    gc_check_all_objects(); 
    gc_check_all_types(); 
  } GC_LEAVE; 
 
#ifdef DEBUG_MALLOC 
  { 
    extern void dmalloc_find_references_to(void *); 
#if 0 
    fprintf(stderr,"**DMALLOC Looking for references:\n"); 
    dmalloc_find_references_to(a); 
#endif 
  } 
#endif 
 
  fprintf(stderr,"**Done looking for references to %p, " 
          "found %"PRINTSIZET"u refs.\n", a, found_ref_count); 
 
  Pike_in_gc = orig_in_gc; 
  gc_found_place = orig_gc_found_place; 
  if(i) exit_gc(); 
  d_flag=tmp; 
} 
 
void debug_gc_add_extra_ref(void *a) 
{ 
  struct marker *m; 
 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_add_extra_ref()"); 
  } 
 
  if (gc_debug) { 
    m = find_marker(a); 
    if ((!m || !(m->flags & GC_PRETOUCHED)) && 
        !safe_debug_findstring((struct pike_string *) a)) 
      gc_fatal(a, 0, "Doing gc_add_extra_ref() on invalid object.\n"); 
    if (!m) m = get_marker(a); 
  } 
  else m = get_marker(a); 
 
  if (m->flags & GC_GOT_EXTRA_REF) 
    gc_fatal(a, 0, "Thing already got an extra gc ref.\n"); 
  m->flags |= GC_GOT_EXTRA_REF; 
  gc_extra_refs++; 
  add_ref( (struct ref_dummy *)a); 
} 
 
void debug_gc_free_extra_ref(void *a) 
{ 
  struct marker *m; 
 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_free_extra_ref()"); 
  } 
 
  if (gc_debug) { 
    m = find_marker(a); 
    if ((!m || !(m->flags & GC_PRETOUCHED)) && 
        !safe_debug_findstring((struct pike_string *) a)) 
      gc_fatal(a, 0, "Doing gc_add_extra_ref() on invalid object.\n"); 
    if (!m) m = get_marker(a); 
  } 
  else m = get_marker(a); 
 
  if (!(m->flags & GC_GOT_EXTRA_REF)) 
    gc_fatal(a, 0, "Thing haven't got an extra gc ref.\n"); 
  m->flags &= ~GC_GOT_EXTRA_REF; 
  gc_extra_refs--; 
} 
 
 
int debug_gc_is_referenced(void *a) 
{ 
  struct marker *m; 
 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_is_referenced()"); 
  } 
 
  if (!a) Pike_fatal("Got null pointer.\n"); 
  if (Pike_in_gc != GC_PASS_MARK) 
    Pike_fatal("gc_is_referenced() called in invalid gc pass.\n"); 
 
  if (gc_debug) { 
    m = find_marker(a); 
    if ((!m || !(m->flags & GC_PRETOUCHED)) && 
        !safe_debug_findstring((struct pike_string *) a)) 
      gc_fatal(a, 0, "Doing gc_is_referenced() on invalid object.\n"); 
    if (!m) m = get_marker(a); 
  } 
  else m = get_marker(a); 
 
  if (m->flags & GC_IS_REFERENCED) 
    gc_fatal(a, 0, "gc_is_referenced() called twice for thing.\n"); 
  m->flags |= GC_IS_REFERENCED; 
 
  return !(m->flags & GC_NOT_REFERENCED); 
} 
 
int gc_mark_external (void *a, const char *place) 
{ 
  struct marker *m; 
 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_mark_external()"); 
  } 
 
  if (!a) Pike_fatal("Got null pointer.\n"); 
 
  if(Pike_in_gc == GC_PASS_LOCATE) 
  { 
    if(a==check_for) { 
      const char *orig_gc_found_place = gc_found_place; 
      gc_found_place = place; 
      gdb_gc_stop_here(a, 0); 
      gc_found_place = orig_gc_found_place; 
    } 
    return 0; 
  } 
 
  if (Pike_in_gc != GC_PASS_CHECK) 
    Pike_fatal("gc_mark_external() called in invalid gc pass.\n"); 
 
#ifdef DEBUG_MALLOC 
  if (gc_external_refs_zapped) { 
    fprintf (stderr, "One external ref to %p found%s.\n", 
             a, place ? place : ""); 
    if (gc_found_in) describe (gc_found_in); 
    return 0; 
  } 
#endif 
 
  m=get_marker(a); 
  m->xrefs++; 
  m->flags|=GC_XREFERENCED; 
  if(Pike_in_gc == GC_PASS_CHECK && 
     (m->refs + m->xrefs > *(INT32 *)a || 
      (m->saved_refs != -1 && m->saved_refs != *(INT32 *)a))) 
    gc_fatal(a, 1, "Ref counts are wrong.\n"); 
  return 0; 
} 
 
#define LOW_CHECK_REC_FRAME(f, file, line) do {                               \ 
    if (f->rf_flags & GC_FRAME_FREED)                                   \ 
      dloc_gc_fatal (file, line, f->data, 0,                            \ 
                     "Accessing freed gc_stack_frame %p.\n", f);        \ 
    if (f->cycle_id->rf_flags & GC_FRAME_FREED) {                       \ 
      fprintf (stderr, "Cycle id frame %p is freed. It is: ", f->cycle_id); \ 
      describe_rec_frame (f->cycle_id);                                 \ 
      fputc ('\n', stderr);                                             \ 
      dloc_gc_fatal (file, line, f->data, 0, "Cycle id frame is freed.\n"); \ 
    }                                                                   \ 
  } while (0) 
 
static void check_rec_stack_frame (struct gc_rec_frame *f, 
                                   struct gc_rec_frame *p1, const char *p1n, 
                                   struct gc_rec_frame *p2, const char *p2n, 
                                   const char *file, INT_TYPE line) 
{ 
  /* To allow this function to be used after a stack rotation but 
   * before cycle_id markup, there are no checks here for cycle_id 
   * consistency wrt other frames on the rec stack. */ 
  LOW_CHECK_REC_FRAME (f, file, line); 
  if (f->rf_flags & (GC_ON_CYCLE_PIECE_LIST|GC_ON_KILL_LIST)) 
    rec_stack_fatal (f, "err", p1, p1n, p2, p2n, file, line, 
                     "Frame %p is not on the rec stack (according to flags).\n", 
                     f); 
  if (!f->prev) 
    rec_stack_fatal (f, "err", p1, p1n, p2, p2n, file, line, 
                     "Prev pointer not set for rec stack frame %p.\n", f); 
  if (f->prev->next != f) 
    rec_stack_fatal (f, "err", p1, p1n, p2, p2n, file, line, 
                     "Rec stack pointers are inconsistent before %p.\n", f); 
  if (f->cycle_id && 
      f->cycle_id->rf_flags & (GC_ON_CYCLE_PIECE_LIST|GC_ON_KILL_LIST)) 
    /* p2 and p2n gets lost here. No bother. */ 
    rec_stack_fatal (f->cycle_id, "cycle id", f, "err", p1, p1n, file, line, 
                     "Cycle id frame %p for %p not on the rec stack " 
                     "(according to flags).\n", f->cycle_id, f); 
  if ((f->rf_flags & GC_MARK_LIVE) && f != stack_top) 
    rec_stack_fatal (f, "err", p1, p1n, p2, p2n, file, line, 
                     "GC_MARK_LIVE frame %p found that " 
                     "isn't on the stack top.\n", f); 
  if ((f->rf_flags & GC_PREV_STRONG) && 
      (f->rf_flags & (GC_PREV_WEAK|GC_PREV_BROKEN))) 
    rec_stack_fatal (f, "err", p1, p1n, p2, p2n, file, line, 
                     "GC_PREV_STRONG set together with " 
                     "GC_PREV_WEAK or GC_PREV_BROKEN in %p.\n", f); 
  if (f->cycle_piece && 
      (!f->cycle_piece->u.last_cycle_piece || 
       f->cycle_piece->u.last_cycle_piece->cycle_piece)) 
    rec_stack_fatal (f, "err", p1, p1n, p2, p2n, file, line, 
                     "Bogus last_cycle_piece %p is %p " 
                     "in cycle piece top %p in %p.\n", 
                     f->cycle_piece->u.last_cycle_piece, 
                     f->cycle_piece->u.last_cycle_piece ? 
                     f->cycle_piece->u.last_cycle_piece->cycle_piece : NULL, 
                     f->cycle_piece, f); 
} 
#define CHECK_REC_STACK_FRAME(f)                                      \ 
  do check_rec_stack_frame ((f), NULL, NULL, NULL, NULL, __FILE__, __LINE__); \ 
  while (0) 
 
static void check_cycle_piece_frame (struct gc_rec_frame *f, 
                                     const char *file, INT_TYPE line) 
{ 
  LOW_CHECK_REC_FRAME (f, file, line); 
  if ((f->rf_flags & (GC_ON_CYCLE_PIECE_LIST|GC_ON_KILL_LIST)) != 
      GC_ON_CYCLE_PIECE_LIST) 
    dloc_gc_fatal (file, line, f->data, 0, 
                   "Frame is not on a cycle piece list " 
                   "(according to flags).\n"); 
  if (f->prev) 
    dloc_gc_fatal (file, line, f->data, 0, 
                   "Prev pointer set for frame on cycle piece list.\n"); 
} 
#define CHECK_CYCLE_PIECE_FRAME(f)                                    \ 
  do check_cycle_piece_frame ((f), __FILE__, __LINE__); while (0) 
 
static void check_kill_list_frame (struct gc_rec_frame *f, 
                                   const char *file, INT_TYPE line) 
{ 
  LOW_CHECK_REC_FRAME (f, file, line); 
  if ((f->rf_flags & (GC_ON_CYCLE_PIECE_LIST|GC_ON_KILL_LIST)) != 
      GC_ON_KILL_LIST) 
    dloc_gc_fatal (file, line, f->data, 0, 
                   "Frame is not on kill list (according to flags).\n"); 
  if (f->prev) 
    dloc_gc_fatal (file, line, f->data, 0, 
                   "Prev pointer set for frame on kill list.\n"); 
} 
#define CHECK_KILL_LIST_FRAME(f)                                      \ 
  do check_kill_list_frame ((f), __FILE__, __LINE__); while (0) 
 
static void check_rec_stack (struct gc_rec_frame *p1, const char *p1n, 
                             struct gc_rec_frame *p2, const char *p2n, 
                             const char *file, int line) 
{ 
  /* This debug check is disabled during the final cleanup since this 
   * is O(n^2) on the stack size, and the stack gets a lot larger then. */ 
  if (gc_debug && !gc_destruct_everything) { 
    struct gc_rec_frame *l, *last_cycle_id = NULL; 
    for (l = &sentinel_frame; l != stack_top;) { 
      l = l->next; 
      check_rec_stack_frame (l, p1, p1n, p2, p2n, file, line); 
      if (l->cycle_id == l) 
        last_cycle_id = l; 
      else if (l->cycle_id != last_cycle_id) 
        rec_stack_fatal (l, "err", p1, p1n, p2, p2n, file, line, 
                         "Unexpected cycle id for frame %p.\n", l); 
      else if (l->rf_flags & GC_PREV_WEAK) 
        rec_stack_fatal (l, "err", p1, p1n, p2, p2n, file, line, 
                         "Unexpected weak ref before %p inside a cycle.\n", l); 
 
      if (l->rf_flags & GC_IS_VALID_CP_CYCLE_ID) 
        rec_stack_fatal (l, "err", p1, p1n, p2, p2n, file, line, 
                         "Frame %p got stray " 
                         "GC_IS_VALID_CP_CYCLE_ID flag.\n", l); 
      if (l->cycle_piece) { 
        struct gc_rec_frame *cp = l->cycle_piece; 
        l->rf_flags |= GC_IS_VALID_CP_CYCLE_ID; 
 
        while (1) { 
          if (!cp->cycle_id || 
              !(cp->cycle_id->rf_flags & GC_IS_VALID_CP_CYCLE_ID)) 
            rec_stack_fatal (cp, "err", p1, p1n, p2, p2n, file, line, 
                             "Unexpected cycle id for frame %p " 
                             "on cycle piece list.\n", cp); 
          if (cp->rf_flags & GC_IS_VALID_CP_CYCLE_ID) 
            rec_stack_fatal (cp, "err", p1, p1n, p2, p2n, file, line, 
                             "Frame %p got stray " 
                             "GC_IS_VALID_CP_CYCLE_ID flag.\n", cp); 
          cp->rf_flags |= GC_IS_VALID_CP_CYCLE_ID; 
          check_cycle_piece_frame (cp, file, line); 
          if (!cp->cycle_piece) break; 
          cp = cp->cycle_piece; 
        } 
 
        if (l->cycle_piece->u.last_cycle_piece != cp) 
          rec_stack_fatal (l->cycle_piece, "err", p1, p1n, p2, p2n, file, line, 
                           "last_cycle_piece is wrong for frame %p, " 
                           "expected %p.\n", l->cycle_piece, cp); 
 
        l->rf_flags &= ~GC_IS_VALID_CP_CYCLE_ID; 
        cp = l->cycle_piece; 
        do { 
          cp->rf_flags &= ~GC_IS_VALID_CP_CYCLE_ID; 
          cp = cp->cycle_piece; 
        } while (cp); 
      } 
    } 
  } 
} 
#define CHECK_REC_STACK(p1, p1n, p2, p2n)                             \ 
  do check_rec_stack ((p1), (p1n), (p2), (p2n), __FILE__, __LINE__); while (0) 
 
#else  /* !PIKE_DEBUG */ 
#define CHECK_REC_STACK_FRAME(f) do {} while (0) 
#define CHECK_CYCLE_PIECE_FRAME(f) do {} while (0) 
#define CHECK_KILL_LIST_FRAME(f) do {} while (0) 
#define CHECK_REC_STACK(p1, p1n, p2, p2n) do {} while (0) 
#endif      /* !PIKE_DEBUG */ 
 
int gc_do_weak_free(void *a) 
{ 
  struct marker *m; 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_do_weak_free()"); 
  } 
  if (!a) Pike_fatal("Got null pointer.\n"); 
  if (Pike_in_gc != GC_PASS_MARK && Pike_in_gc != GC_PASS_ZAP_WEAK) 
    Pike_fatal("gc_do_weak_free() called in invalid gc pass.\n"); 
  if (gc_debug) { 
    if (!(m = find_marker(a))) 
      gc_fatal(a, 0, "gc_do_weak_free() got unknown object.\n"); 
  } 
  else m = get_marker(a); 
  debug_malloc_touch(a); 
 
  if (m->weak_refs > m->refs) 
    gc_fatal(a, 0, "More weak references than internal references.\n"); 
#else 
  m = get_marker(a); 
#endif 
 
  if (Pike_in_gc != GC_PASS_ZAP_WEAK) { 
    if (m->weak_refs < 0) 
      goto should_free; 
  } 
  else 
    if (!(m->flags & GC_MARKED)) { 
#ifdef PIKE_DEBUG 
      if (m->weak_refs <= 0) 
        gc_fatal(a, 0, "Too many weak refs cleared to thing with external " 
                 "weak refs.\n"); 
#endif 
      m->weak_refs--; 
      goto should_free; 
    } 
  return 0; 
 
should_free: 
  gc_ext_weak_refs--; 
#ifdef PIKE_DEBUG 
  m->saved_refs--; 
  m->flags |= GC_WEAK_FREED; 
#endif 
 
  if (*(INT32 *) a == 1) { 
    /* Make sure the thing doesn't run out of refs, since we can't 
     * handle cascading frees now. We'll do it in the free pass 
     * instead. */ 
    gc_add_extra_ref(a); 
    m->flags |= GC_GOT_DEAD_REF; 
#ifdef PIKE_DEBUG 
    delayed_freed++; 
#endif 
  } 
 
  return 1; 
} 
 
void gc_delayed_free(void *a, int type) 
{ 
  struct marker *m; 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_delayed_free()"); 
  } 
  if (Pike_in_gc != GC_PASS_MARK && Pike_in_gc != GC_PASS_CYCLE && 
      Pike_in_gc != GC_PASS_ZAP_WEAK) 
    Pike_fatal("gc_delayed_free() called in invalid gc pass.\n"); 
  if (gc_debug) { 
    if (!(m = find_marker(a))) 
      gc_fatal(a, 0, "gc_delayed_free() got unknown object (missed by pretouch pass).\n"); 
  } 
  else m = get_marker(a); 
  if (*(INT32 *) a != 1) 
    Pike_fatal("gc_delayed_free() called for thing that haven't got a single ref.\n"); 
  debug_malloc_touch(a); 
  delayed_freed++; 
#else 
  m = get_marker(a); 
#endif 
 
  if (m->flags & GC_MARKED) { 
    /* Note that we can get marked things here, e.g. if the index in a 
     * mapping with weak indices is removed in the zap weak pass, the 
     * value will be zapped too, but it will still have a mark from 
     * the mark pass. This means that the stuff referenced by the 
     * value will only be refcount garbed, which can leave cyclic 
     * garbage for the next gc round. 
     * 
     * Since the value has been marked we won't find it in the free 
     * pass, so we have to keep special track of it. :P */ 
    struct free_extra_frame *l = alloc_free_extra_frame(); 
    l->data = a; 
    l->type = type; 
    l->next = free_extra_list; 
    free_extra_list = l; 
  } 
 
  gc_add_extra_ref(a); 
  m->flags |= GC_GOT_DEAD_REF; 
} 
 
int real_gc_mark(void *a DO_IF_DEBUG (COMMA int type)) 
{ 
  struct marker *m; 
 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc == GC_PASS_ZAP_WEAK && !find_marker (a)) 
    gc_fatal_2 (a, type, 0, "gc_mark() called for for thing without marker " 
                "in zap weak pass.\n"); 
#endif 
 
  m = get_marker (a); 
 
  /* Note: m->refs and m->xrefs are useless already here due to how 
   * gc_free_(short_)svalue works. */ 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && m && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_mark()"); 
  } 
  if (!a) Pike_fatal("Got null pointer.\n"); 
  if (Pike_in_gc != GC_PASS_MARK && Pike_in_gc != GC_PASS_ZAP_WEAK) 
    Pike_fatal("GC mark attempted in invalid pass.\n"); 
  if (!*(INT32 *) a) 
    gc_fatal_2 (a, type, 1, "Marked a thing without refs.\n"); 
  if (m->weak_refs < 0) 
    gc_fatal_2 (a, type, 1, "Marked a thing scheduled for weak free.\n"); 
#endif 
 
  if (Pike_in_gc == GC_PASS_ZAP_WEAK) { 
    /* Things are visited in the zap weak pass through the mark 
     * functions to free refs to internal things that only got weak 
     * external references. That happens only when a thing also have 
     * internal cyclic nonweak refs. */ 
#ifdef PIKE_DEBUG 
    if (!(m->flags & GC_MARKED)) 
      gc_fatal_2 (a, type, 0, "gc_mark() called for thing in zap weak pass " 
                  "that wasn't marked before.\n"); 
#endif 
    if (m->flags & GC_FREE_VISITED) { 
      debug_malloc_touch (a); 
      return 0; 
    } 
    else { 
      debug_malloc_touch (a); 
      m->flags |= GC_FREE_VISITED; 
      return 1; 
    } 
  } 
 
  else if (m->flags & GC_MARKED) { 
    debug_malloc_touch (a); 
#ifdef PIKE_DEBUG 
    if (m->weak_refs != 0) 
      gc_fatal_2 (a, type, 0, "weak_refs changed in marker " 
                  "already visited by gc_mark().\n"); 
#endif 
    return 0; 
  } 
 
  else { 
    debug_malloc_touch (a); 
    if (m->weak_refs) { 
      gc_ext_weak_refs -= m->weak_refs; 
      m->weak_refs = 0; 
    } 
    m->flags = (m->flags & ~GC_NOT_REFERENCED) | GC_MARKED; 
    DO_IF_DEBUG(marked++); 
    return 1; 
  } 
} 
 
void gc_move_marker (void *old, void *new) 
{ 
  struct marker *m = find_marker (old); 
 
#ifdef PIKE_DEBUG 
  if (!Pike_in_gc || Pike_in_gc >= GC_PASS_FREE) 
    Pike_fatal ("gc move mark attempted in invalid pass.\n"); 
  if (!old) Pike_fatal ("Got null pointer in old.\n"); 
  if (!new) Pike_fatal ("Got null pointer in new.\n"); 
  if (!m) Pike_fatal ("Have no marker for old block %p.\n", old); 
  if (find_marker (new)) 
    Pike_fatal ("New block %p already got a marker.\n", new); 
#endif 
 
  move_marker (m, debug_malloc_pass (new)); 
} 
 
PMOD_EXPORT void gc_cycle_enqueue(gc_cycle_check_cb *checkfn, void *data, int weak) 
{ 
  struct link_frame *l = alloc_link_frame(); 
#ifdef PIKE_DEBUG 
  { 
    struct marker *m; 
    if (gc_is_watching && (m = find_marker(data)) && m->flags & GC_WATCHED) { 
      /* This is useful to set breakpoints on. */ 
      gc_watched_found (m, "gc_cycle_enqueue()"); 
    } 
  } 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    gc_fatal(data, 0, "Use of the gc frame stack outside the cycle check pass.\n"); 
  if (stack_top == &sentinel_frame) 
    gc_fatal (data, 0, "No thing on rec stack to follow links from.\n"); 
#endif 
  l->data = data; 
  l->checkfn = checkfn; 
  l->weak = weak; 
  l->prev = stack_top->u.link_top; 
#ifdef GC_STACK_DEBUG 
  fprintf (stderr, "push link %p [%p in %p]: ", l, stack_top->u.link_top, stack_top); 
  describe_link_frame (l); 
  fputc('\n', stderr); 
#endif 
  stack_top->u.link_top = l; 
} 
 
static struct gc_rec_frame *gc_cycle_enqueue_rec (void *data) 
{ 
  struct gc_rec_frame *r = 
    (struct gc_rec_frame*)ba_alloc(&gc_rec_frame_allocator); 
  if (++rec_frames > max_rec_frames) max_rec_frames = rec_frames; 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    gc_fatal(data, 0, "Use of the gc frame stack outside the cycle check pass.\n"); 
  r->next = (struct gc_rec_frame *) (ptrdiff_t) -1; 
#endif 
  r->data = data; 
  r->u.link_top = NULL; 
  r->prev = stack_top; 
  r->cycle_id = r; 
  r->cycle_piece = NULL; 
#ifdef GC_STACK_DEBUG 
  fprintf (stderr, "push rec  %p [%p]: ", r, stack_top); 
  describe_rec_frame (r); 
  fputc('\n', stderr); 
#endif 
  stack_top->next = r; 
  stack_top = r; 
  return r; 
} 
 
void gc_cycle_run_queue() 
{ 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    Pike_fatal("Use of the gc frame stack outside the cycle check pass.\n"); 
#endif 
 
  while (stack_top != &sentinel_frame) { 
    while (stack_top->u.link_top) { 
      struct link_frame l = *stack_top->u.link_top; 
#ifdef GC_STACK_DEBUG 
      fprintf (stderr, "pop  link %p [%p in %p]: ", 
               stack_top->u.link_top, l.prev, stack_top); 
      describe_link_frame (stack_top->u.link_top); 
      fputc ('\n', stderr); 
#endif 
      really_free_link_frame (stack_top->u.link_top); 
      stack_top->u.link_top = l.prev; 
      l.checkfn (l.data, l.weak); /* Might change stack_top. */ 
    } 
 
#ifdef GC_STACK_DEBUG 
    fprintf (stderr, "pop  rec  %p [%p]: ", stack_top, stack_top->prev); 
    describe_rec_frame (stack_top); 
    fputc ('\n', stderr); 
#endif 
    CHECK_REC_STACK_FRAME (stack_top); 
 
#ifdef PIKE_DEBUG 
    { 
      struct gc_rec_frame *old_stack_top = stack_top; 
      gc_cycle_pop(); 
      if (stack_top == old_stack_top) 
        fatal ("gc_cycle_pop didn't pop the stack.\n"); 
    } 
#else 
    gc_cycle_pop(); 
#endif 
  } 
} 
 
#ifdef GC_CYCLE_DEBUG 
static int gc_cycle_indent = 0; 
#define CYCLE_DEBUG_MSG(REC, TXT) do {                                        \ 
    struct gc_rec_frame *r_ = (REC);                                    \ 
    fprintf (stderr, "%*s%-35s %p [%p] ", gc_cycle_indent, "",          \ 
             (TXT), r_ ? r_->data : NULL, stack_top->data);             \ 
    if (r_) describe_rec_frame (r_);                                    \ 
    putc ('\n', stderr);                                                \ 
  } while (0) 
#else 
#define CYCLE_DEBUG_MSG(REC, TXT) do {} while (0) 
#endif 
 
static struct gc_rec_frame *rotate_rec_stack (struct gc_rec_frame *beg, 
                                              struct gc_rec_frame *pos) 
/* Performs a rotation of the recursion stack so the part from pos 
 * down to the end gets before the part from beg down to pos. The beg 
 * position might be moved further back the list to avoid breaking 
 * strong link sequences. Returns the actual beg position. Example: 
 * 
 *                         strong 
 * a1 <=> ... <=> a2 <=> b1 <*> b2 <=> ... <=> b3 <=> c1 <=> ... <=> c2 
 *                              ^ beg                 ^ pos 
 * 
 * becomes 
 * 
 *                  broken                       strong 
 * a1 <=> ... <=> a2 <#> c1 <=> ... <=> c2 <=> b1 <*> b2 <=> ... <=> b3 
 *                       ^ pos                 ^      ^ beg 
 *                                             returned 
 * 
 * Note: The part from pos down to the end is assumed to not contain 
 * any weak refs. (If it does they must be cleared, unless the link 
 * before beg is weak.) 
 */ 
{ 
  CYCLE_DEBUG_MSG (beg, "> rotate_rec_stack, requested beg"); 
 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    Pike_fatal("Use of the gc frame stack outside the cycle check pass.\n"); 
  CHECK_REC_STACK_FRAME (beg); 
  CHECK_REC_STACK_FRAME (pos); 
  if (beg == pos) 
    gc_fatal (beg->data, 0, "Cycle already broken at requested position.\n"); 
#endif 
 
#ifdef GC_STACK_DEBUG 
  fprintf(stderr,"Stack before:\n"); 
  describe_rec_stack (beg, "beg", pos, "pos", NULL, NULL); 
#endif 
 
  /* Always keep chains of strong refs continuous, or else we risk 
   * breaking the order in a later rotation. */ 
  for (; beg->rf_flags & GC_PREV_STRONG; beg = beg->prev) 
    CYCLE_DEBUG_MSG (beg, "> rotate_rec_stack, skipping strong"); 
#ifdef PIKE_DEBUG 
  if (beg == &sentinel_frame) fatal ("Strong ref chain ended up off stack.\n"); 
#endif 
  CYCLE_DEBUG_MSG (beg, "> rotate_rec_stack, actual beg"); 
 
  { 
    struct gc_rec_frame *new_stack_top = pos->prev; 
 
    beg->prev->next = pos; 
    pos->prev = beg->prev; 
 
    stack_top->next = beg; 
    beg->prev = stack_top; 
 
    stack_top = new_stack_top; 
#ifdef PIKE_DEBUG 
    stack_top->next = (struct gc_rec_frame *) (ptrdiff_t) -1; 
#endif 
  } 
 
#ifdef PIKE_DEBUG 
  frame_rot++; 
#endif 
 
  pos->rf_flags |= GC_PREV_BROKEN; 
 
#ifdef GC_STACK_DEBUG 
  fprintf(stderr,"Stack after:\n"); 
  describe_rec_stack (beg, "ret", pos, "pos", NULL, NULL); 
#endif 
 
  return beg; 
} 
 
int gc_cycle_push(void *data, struct marker *m, int weak) 
{ 
  struct marker *pm; 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && m && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_cycle_push()"); 
  } 
 
  debug_malloc_touch (data); 
 
  if (!data) Pike_fatal ("Got null pointer.\n"); 
  if (m->data != data) Pike_fatal ("Got wrong marker.\n"); 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    Pike_fatal("GC cycle push attempted in invalid pass.\n"); 
  if (gc_debug && !(m->flags & GC_PRETOUCHED)) 
    gc_fatal (data, 0, "gc_cycle_push() called for untouched thing.\n"); 
  if (!gc_destruct_everything) { 
    if ((!(m->flags & GC_NOT_REFERENCED) || m->flags & GC_MARKED) && 
        *(INT32 *) data) 
      gc_fatal (data, 1, "Got a referenced marker to gc_cycle_push.\n"); 
    if (m->flags & GC_XREFERENCED) 
      gc_fatal (data, 1, "Doing cycle check in externally referenced thing " 
                "missed in mark pass.\n"); 
  } 
  if (weak && stack_top == &sentinel_frame) 
    gc_fatal (data, 1, "weak is %d when stack is empty.\n", weak); 
  if (gc_debug > 1) { 
    struct array *a; 
    struct object *o; 
    struct program *p; 
    struct mapping *m; 
    struct multiset *l; 
    for(a = gc_internal_array; a; a = a->next) 
      if(a == (struct array *) data) goto on_gc_internal_lists; 
    for(o = gc_internal_object; o; o = o->next) 
      if(o == (struct object *) data) goto on_gc_internal_lists; 
    for(p = gc_internal_program; p; p = p->next) 
      if(p == (struct program *) data) goto on_gc_internal_lists; 
    for(m = gc_internal_mapping; m; m = m->next) 
      if(m == (struct mapping *) data) goto on_gc_internal_lists; 
    for(l = gc_internal_multiset; l; l = l->next) 
      if(l == (struct multiset *) data) goto on_gc_internal_lists; 
    gc_fatal (data, 0, "gc_cycle_check() called for thing not on gc_internal lists.\n"); 
  on_gc_internal_lists: 
    ; /* We must have a least one expression after a label! - Hubbe */ 
  } 
#endif 
 
  if (stack_top->rf_flags & GC_MARK_LIVE) { 
    /* Only recurse through things already handled; we'll get to the 
     * other later in the normal recursion. */ 
    if (m->flags & GC_CYCLE_CHECKED && !(m->flags & GC_LIVE)) { 
      CYCLE_DEBUG_MSG (m->frame, "gc_cycle_push, mark live"); 
      goto mark_live; 
    } 
    CYCLE_DEBUG_MSG (m->frame, "gc_cycle_push, no mark live"); 
    return 0; 
  } 
 
  if (stack_top == &sentinel_frame) 
    pm = NULL; 
  else { 
    pm = find_marker (stack_top->data); 
#ifdef PIKE_DEBUG 
    if (!pm) 
      gc_fatal (stack_top->data, 0, "No marker for thing on top of the stack.\n"); 
#endif 
  } 
 
#ifdef PIKE_DEBUG 
  if (weak < 0 && stack_top->rf_flags & GC_FOLLOWED_NONSTRONG) 
    gc_fatal (data, 0, "Followed strong link too late.\n"); 
  if (weak >= 0) stack_top->rf_flags |= GC_FOLLOWED_NONSTRONG; 
#endif 
 
  if (m->frame) { 
    /* A cyclic ref or a ref to something on the kill list is found. */ 
    struct gc_rec_frame *cycle_frame = m->frame; 
 
    if (cycle_frame->rf_flags & GC_ON_KILL_LIST) 
      CYCLE_DEBUG_MSG (cycle_frame, "gc_cycle_push, ref to kill list"); 
    else if (cycle_frame == stack_top) 
      CYCLE_DEBUG_MSG (cycle_frame, "gc_cycle_push, self-ref"); 
    else if (weak > 0) 
      /* Ignore weak refs since they always are eligible to be broken anyway. */ 
      CYCLE_DEBUG_MSG (cycle_frame, "gc_cycle_push, weak cyclic ref"); 
 
    else { 
      struct gc_rec_frame *weakly_refd = NULL; 
      struct gc_rec_frame *brokenly_refd = NULL; 
      struct gc_rec_frame *nonstrongly_refd = NULL; 
#ifdef PIKE_DEBUG 
      if (stack_top == &sentinel_frame) 
        gc_fatal (data, 0, "Cyclic ref involves dummy sentinel frame.\n"); 
      CHECK_REC_STACK_FRAME (stack_top); 
#endif 
 
      CYCLE_DEBUG_MSG (cycle_frame, "gc_cycle_push, cyclic ref"); 
 
      /* Find the corresponding frame still on the stack and compress 
       * indirect cycle_id links. */ 
      { 
        struct gc_rec_frame *r; 
        for (r = cycle_frame; !r->prev; r = r->cycle_id) 
          CHECK_CYCLE_PIECE_FRAME (r); 
        while (cycle_frame != r) { 
          struct gc_rec_frame *next = cycle_frame->cycle_id; 
          cycle_frame->cycle_id = r; 
          cycle_frame = next; 
        } 
        CHECK_REC_STACK_FRAME (cycle_frame); 
      } 
 
      if (!weak) { 
        struct gc_rec_frame *r; 
        CYCLE_DEBUG_MSG (cycle_frame, "gc_cycle_push, search normal"); 
        /* Find the last weakly linked thing and the last thing whose 
         * normal ref already has been broken. */ 
        for (r = stack_top; r != cycle_frame; r = r->prev) { 
          CHECK_REC_STACK_FRAME (r); 
          DO_IF_DEBUG (link_search++); 
          if (r->rf_flags & GC_PREV_WEAK) { 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, found weak"); 
            weakly_refd = r; 
            break; 
          } 
          if (!brokenly_refd && (r->rf_flags & GC_PREV_BROKEN)) { 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, found broken"); 
            brokenly_refd = r; 
          } 
          else 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, search"); 
        } 
      } 
 
      else if (weak < 0) { 
        struct gc_rec_frame *r; 
        CYCLE_DEBUG_MSG (cycle_frame, "gc_cycle_push, search strong"); 
        /* Find the last weakly linked thing and the last one which 
         * isn't strongly linked. */ 
        for (r = stack_top; r != cycle_frame; r = r->prev) { 
          CHECK_REC_STACK_FRAME (r); 
          DO_IF_DEBUG (link_search++); 
          if (r->rf_flags & GC_PREV_WEAK) { 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, found weak"); 
            weakly_refd = r; 
            break; 
          } 
          if (!nonstrongly_refd && !(r->rf_flags & GC_PREV_STRONG)) { 
            nonstrongly_refd = r; 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, found nonstrong"); 
          } 
          if (!brokenly_refd && (r->rf_flags & GC_PREV_BROKEN)) { 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, found broken"); 
            brokenly_refd = r; 
          } 
#ifdef GC_CYCLE_DEBUG 
        else if (r != nonstrongly_refd) 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, search"); 
#endif 
      } 
#ifdef PIKE_DEBUG 
      if (weak && r == cycle_frame && !nonstrongly_refd) { 
          fprintf(stderr, "Only strong links in cycle:\n"); 
          for (r = cycle_frame;; r = r->next) { 
            describe (r->data); 
            locate_references (r->data); 
            if (r == stack_top) break; 
            fprintf(stderr, "========= next =========\n"); 
          } 
          gc_fatal(0, 0, "Only strong links in cycle.\n"); 
        } 
#endif 
      } 
 
      if (weakly_refd) { 
        /* The backward link is normal or strong and there are one or 
         * more weak links in the cycle. Let's break it at the last 
         * one (to avoid having to clobber the others after the 
         * rotation). */ 
        CYCLE_DEBUG_MSG (weakly_refd, "gc_cycle_push, weak break"); 
        /* If the backward link points into a cycle, we rotate the 
         * whole cycle up the stack. See the "cycle checking" blurb 
         * above for rationale. */ 
        rotate_rec_stack (cycle_frame->cycle_id, weakly_refd); 
        CHECK_REC_STACK (cycle_frame, "cycle_frame", 
                         weakly_refd, "weakly_refd"); 
      } 
 
      else { 
        struct gc_rec_frame *cycle_id = cycle_frame->cycle_id; 
        struct gc_rec_frame *break_pos; 
 
        if (brokenly_refd) { 
          /* Found a link that already has been broken once, so we 
           * prefer to break at it again. */ 
          CYCLE_DEBUG_MSG (brokenly_refd, "gc_cycle_push, break at broken"); 
          break_pos = brokenly_refd; 
        } 
        else if (!weak) { 
          CYCLE_DEBUG_MSG (cycle_frame, "gc_cycle_push, no break spot found"); 
          break_pos = NULL; 
        } 
        else {                      /* weak < 0 */ 
          /* The backward link is strong. Must break the cycle at the 
           * last nonstrong link. */ 
          CYCLE_DEBUG_MSG (nonstrongly_refd, "gc_cycle_push, nonstrong break"); 
          break_pos = nonstrongly_refd; 
        } 
 
        if (break_pos) { 
          struct gc_rec_frame *rot_beg; 
          rot_beg = rotate_rec_stack (cycle_frame, break_pos); 
          rot_beg->rf_flags &= ~(GC_PREV_WEAK|GC_PREV_BROKEN); 
          if (weak >= 0) rot_beg->rf_flags |= GC_PREV_STRONG; 
 
          if (rot_beg->cycle_id != break_pos->prev->cycle_id) 
            /* Ensure that the cycle id frame is kept deepest in the 
             * stack: Since there's no already marked cycle that 
             * continues past the beginning of the rotated portion 
             * (rot_beg and break_pos->prev were previously next to 
             * each other), break_pos is now the deepest frame in the 
             * cycle. */ 
            cycle_id = break_pos; 
        } 
 
        /* Mark the cycle. NB: This causes O(n^2) complexity for some 
         * kinds of data structures. */ 
        CHECK_REC_STACK_FRAME (cycle_id); 
        { 
          struct gc_rec_frame *r, *bottom = break_pos ? break_pos : cycle_frame; 
          CYCLE_DEBUG_MSG (cycle_id, "gc_cycle_push, cycle"); 
          for (r = stack_top;; r = r->prev) { 
            r->cycle_id = cycle_id; 
            CHECK_REC_STACK_FRAME (r); 
            CYCLE_DEBUG_MSG (r, "> gc_cycle_push, mark cycle 1"); 
            if (r == bottom) break; 
          } 
        } 
        CHECK_REC_STACK (cycle_frame, "cycle_frame", break_pos, "break_pos"); 
      } 
    } 
  } 
 
  else 
    if (!(m->flags & GC_CYCLE_CHECKED)) { 
      struct gc_rec_frame *r; 
#ifdef PIKE_DEBUG 
      cycle_checked++; 
      if (m->frame) 
        gc_fatal (data, 0, "Marker already got a frame.\n"); 
#endif 
 
      m->flags |= GC_CYCLE_CHECKED | (pm ? pm->flags & GC_LIVE : 0); 
      m->frame = r = gc_cycle_enqueue_rec (data); 
      debug_malloc_touch (data); 
      if (weak) { 
        if (weak > 0) r->rf_flags = GC_PREV_WEAK; 
        else r->rf_flags = GC_PREV_STRONG; 
      } 
      else 
        r->rf_flags = 0; 
 
#ifdef GC_CYCLE_DEBUG 
      if (weak > 0) CYCLE_DEBUG_MSG (r, "gc_cycle_push, recurse weak"); 
      else if (weak < 0) CYCLE_DEBUG_MSG (r, "gc_cycle_push, recurse strong"); 
      else CYCLE_DEBUG_MSG (r, "gc_cycle_push, recurse"); 
      gc_cycle_indent += 2; 
#endif 
      CHECK_REC_STACK (NULL, NULL, NULL, NULL); 
      return 1; 
    } 
 
  /* Should normally not recurse now, but got to do that anyway if we 
   * must propagate GC_LIVE flags. */ 
  if (!pm || !(pm->flags & GC_LIVE) || m->flags & GC_LIVE) { 
    CYCLE_DEBUG_MSG (m->frame ? m->frame : NULL, "gc_cycle_push, no recurse"); 
    return 0; 
  } 
 
  /* Initialize mark live recursion. */ 
  gc_cycle_enqueue_rec (NULL)->rf_flags = GC_MARK_LIVE; 
#ifdef GC_CYCLE_DEBUG 
  CYCLE_DEBUG_MSG (m->frame ? m->frame : NULL, "gc_cycle_push, mark live begins"); 
  gc_cycle_indent += 2; 
#endif 
 
mark_live: 
#ifdef PIKE_DEBUG 
  if (m->flags & GC_LIVE) 
    Pike_fatal("Shouldn't mark live recurse when there's nothing to do.\n"); 
#endif 
  m->flags |= GC_LIVE; 
  debug_malloc_touch (data); 
 
  if (m->flags & GC_GOT_DEAD_REF) { 
    /* A thing previously popped as dead is now being marked live. 
     * Have to remove the extra ref added by gc_cycle_pop(). */ 
    gc_free_extra_ref (data); 
    if (!sub_ref ((struct ref_dummy *) data)) { 
#ifdef PIKE_DEBUG 
      gc_fatal (data, 0, "Thing got zero refs after removing the dead gc ref.\n"); 
#endif 
    } 
  } 
 
  /* Visit links without pushing a rec frame. */ 
#ifdef PIKE_DEBUG 
  mark_live++; 
#endif 
  return 1; 
} 
 
static void gc_cycle_pop() 
{ 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    Pike_fatal("GC cycle pop attempted in invalid pass.\n"); 
  if (stack_top->u.link_top) 
    gc_fatal (stack_top->data, 0, "Link list not empty for popped rec frame.\n"); 
#endif 
#ifdef GC_CYCLE_DEBUG 
  gc_cycle_indent -= 2; 
#endif 
 
  if (stack_top->rf_flags & GC_MARK_LIVE) { 
    struct gc_rec_frame *r = stack_top->prev; 
    CYCLE_DEBUG_MSG (stack_top, "gc_cycle_pop, mark live ends"); 
    really_free_gc_rec_frame (stack_top); 
    stack_top = r; 
  } 
 
  else { 
    struct gc_rec_frame *popped = stack_top; 
 
#ifdef PIKE_DEBUG 
    { 
      void *data = popped->data; 
      struct marker *m = find_marker (data); 
      if (gc_is_watching && m && m->flags & GC_WATCHED) { 
        /* This is useful to set breakpoints on. */ 
        gc_watched_found (m, "gc_cycle_pop()"); 
      } 
      if (!(m->flags & GC_CYCLE_CHECKED)) 
        gc_fatal (data, 0, "Marker being popped doesn't have GC_CYCLE_CHECKED.\n"); 
      if (!gc_destruct_everything) { 
        if ((!(m->flags & GC_NOT_REFERENCED) || m->flags & GC_MARKED) && 
            *(INT32 *) data) 
          gc_fatal (data, 1, "Got a referenced marker to gc_cycle_pop.\n"); 
        if (m->flags & GC_XREFERENCED) 
          gc_fatal (data, 1, "Doing cycle check in externally referenced thing " 
                    "missed in mark pass.\n"); 
      } 
      if (popped->next != (struct gc_rec_frame *) (ptrdiff_t) -1) 
        gc_fatal (data, 0, "Popped rec frame got stuff in the next pointer.\n"); 
    } 
#endif 
 
    stack_top = popped->prev; 
#ifdef PIKE_DEBUG 
    if (stack_top != &sentinel_frame) CHECK_REC_STACK_FRAME (stack_top); 
    CHECK_REC_STACK_FRAME (popped); 
#endif 
    popped->prev = NULL; 
 
    if (popped->cycle_id != popped) { 
      /* Part of a cycle that extends further back - move to the cycle 
       * piece list of the previous frame. */ 
      struct gc_rec_frame *this_list_last = 
        popped->cycle_piece ? popped->cycle_piece->u.last_cycle_piece : popped; 
#ifdef PIKE_DEBUG 
      if (this_list_last->cycle_piece) 
        gc_fatal (this_list_last->data, 0, 
                  "This frame should be last on the cycle piece list.\n"); 
      popped->rf_flags |= GC_ON_CYCLE_PIECE_LIST; 
      CHECK_CYCLE_PIECE_FRAME (this_list_last); 
#endif 
      CYCLE_DEBUG_MSG (popped, "gc_cycle_pop, keep cycle piece"); 
 
      if (!stack_top->cycle_piece) 
        popped->u.last_cycle_piece = this_list_last; 
      else { 
        /* Link in the popped frame and its cycle piece list before 
         * the one that the previous frame has. */ 
        struct gc_rec_frame *up_list_first = stack_top->cycle_piece; 
        struct gc_rec_frame *up_list_last = up_list_first->u.last_cycle_piece; 
#ifdef PIKE_DEBUG 
      CHECK_CYCLE_PIECE_FRAME (up_list_last); 
        if (up_list_last->cycle_piece) 
          gc_fatal (up_list_last->data, 0, 
                    "This frame should be last on the cycle piece list.\n"); 
#endif 
      CYCLE_DEBUG_MSG (up_list_first, "> gc_cycle_pop, inserted before"); 
        this_list_last->cycle_piece = up_list_first; 
        popped->u.last_cycle_piece = up_list_last; 
      } 
      stack_top->cycle_piece = popped; 
      popped->cycle_id = stack_top; 
#ifdef PIKE_DEBUG 
      popped->next = (void *) (ptrdiff_t) -1; 
#endif 
 
      CHECK_CYCLE_PIECE_FRAME (popped); 
      CHECK_REC_STACK_FRAME (stack_top); 
    } 
 
    else { 
      /* Free or move to the kill list the popped frame and its cycle 
       * piece list. */ 
      struct gc_rec_frame **kill_list_ptr = &kill_list; 
      struct gc_rec_frame *cycle_id = NULL; 
 
#ifdef PIKE_DEBUG 
      { 
        struct gc_rec_frame *r; 
        for (r = popped->cycle_piece; r; r = r->cycle_piece) 
          /* Can't do this while the list is being freed below. */ 
          CHECK_CYCLE_PIECE_FRAME (r); 
      } 
#endif 
 
      CYCLE_DEBUG_MSG (popped, "gc_cycle_pop, popping cycle"); 
 
      do { 
        struct marker *m = find_marker (popped->data); 
        struct gc_rec_frame *next = popped->cycle_piece; 
 
        if (m->flags & GC_LIVE_OBJ) { 
          /* Move to the kill list. */ 
#ifdef PIKE_DEBUG 
        popped->rf_flags &= ~GC_ON_CYCLE_PIECE_LIST; 
          popped->cycle_piece = popped->u.last_cycle_piece = 
            (struct gc_rec_frame *) (ptrdiff_t) -1; 
#endif 
        popped->next = *kill_list_ptr; 
          *kill_list_ptr = popped; 
          kill_list_ptr = &popped->next; 
          popped->rf_flags |= GC_ON_KILL_LIST; 
 
          /* Ensure that the frames on the kill list have a valid 
           * cycle id frame and that every frame is linked directly to 
           * it. This is only for the sake of warn_bad_cycles. */ 
          if (!cycle_id) cycle_id = popped; 
          popped->cycle_id = cycle_id; 
 
          /* This extra ref is taken away in the kill pass. Don't add one 
           * if it got an extra ref already due to weak free. */ 
          if (!(m->flags & GC_GOT_DEAD_REF)) 
            gc_add_extra_ref (popped->data); 
 
          CHECK_KILL_LIST_FRAME (popped); 
          CYCLE_DEBUG_MSG (popped, "> gc_cycle_pop, move to kill list"); 
        } 
 
        else { 
          if (!(m->flags & GC_LIVE)) { 
            /* Add an extra ref which is taken away in the free pass. This 
             * is done to not refcount garb the cycles themselves 
             * recursively, which in bad cases can consume a lot of C 
             * stack. */ 
            if (!(m->flags & GC_GOT_DEAD_REF)) { 
              gc_add_extra_ref (popped->data); 
              m->flags |= GC_GOT_DEAD_REF; 
            } 
          } 
#ifdef PIKE_DEBUG 
        else 
            if (m->flags & GC_GOT_DEAD_REF) 
              gc_fatal (popped->data, 0, "Didn't expect a dead extra ref.\n"); 
#endif 
 
          CYCLE_DEBUG_MSG (popped, "> gc_cycle_pop, free"); 
          m->frame = NULL; 
          really_free_gc_rec_frame (popped); 
        } 
 
        popped = next; 
      } while (popped); 
    } 
  } 
 
#ifdef PIKE_DEBUG 
  stack_top->next = (struct gc_rec_frame *) (ptrdiff_t) -1; 
#endif 
} 
 
void do_gc_recurse_svalues(struct svalue *s, int num) 
{ 
  gc_recurse_svalues(s, num); 
} 
 
void do_gc_recurse_short_svalue(union anything *u, int type) 
{ 
  gc_recurse_short_svalue(u, type); 
} 
 
int gc_do_free(void *a) 
{ 
  struct marker *m; 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && (m = find_marker(a)) && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    gc_watched_found (m, "gc_do_free()"); 
  } 
  if (!a) Pike_fatal("Got null pointer.\n"); 
  if (Pike_in_gc != GC_PASS_FREE) 
    Pike_fatal("gc free attempted in invalid pass.\n"); 
#endif 
 
  m=find_marker(debug_malloc_pass(a)); 
  if (!m) return 0;             /* Object created after cycle pass. */ 
 
  if (gc_destruct_everything) { 
    /* We don't actually free much in this mode, just destruct 
     * objects. So when we normally would return nonzero we just 
     * remove the extra ref again. */ 
    if (!(m->flags & GC_LIVE)) { 
      if (*(INT32 *) a == 1) 
        return 1; 
      else { 
        gc_free_extra_ref (a); 
        sub_ref ((struct ref_dummy *) a); 
      } 
    } 
    return 0; 
  } 
 
#ifdef PIKE_DEBUG 
  if (*(INT32 *) a > !!(m->flags & GC_GOT_EXTRA_REF)) { 
    if (!gc_destruct_everything && 
        (!(m->flags & GC_NOT_REFERENCED) || m->flags & GC_MARKED)) 
      gc_fatal(a, 0, "gc_do_free() called for referenced thing.\n"); 
    if (gc_debug && 
        (m->flags & (GC_PRETOUCHED|GC_MARKED|GC_IS_REFERENCED)) == GC_PRETOUCHED) 
      gc_fatal(a, 0, "gc_do_free() called without prior call to " 
               "gc_mark() or gc_is_referenced().\n"); 
  } 
  if(!gc_destruct_everything && 
     (m->flags & (GC_MARKED|GC_XREFERENCED)) == GC_XREFERENCED) 
    gc_fatal(a, 1, "Thing with external reference missed in gc mark pass.\n"); 
  if ((m->flags & (GC_DO_FREE|GC_LIVE)) == GC_LIVE) live_ref++; 
  m->flags |= GC_DO_FREE; 
#endif 
 
  return !(m->flags & GC_LIVE); 
} 
 
#if 0 
static void free_obj_arr(void *oa) 
{ 
  struct array *obj_arr = *((struct array **)oa); 
 
  if (obj_arr) free_array(obj_arr); 
  free(oa); 
} 
#endif 
 
/*! @class MasterObject 
 */ 
 
/*! @decl void runtime_warning(string subsystem, string msg, mixed|void data) 
 *! 
 *!   Called by the Pike runtime to warn about data inconsistencies. 
 *! 
 *! @param subsystem 
 *!   Runtime subsystem where the warning was generated. 
 *!   Currently the following subsystems may call this function: 
 *!   @string 
 *!     @value "gc" 
 *!       The garbage collector. 
 *!   @endstring 
 *! 
 *! @param msg 
 *!   Warning message. 
 *!   Currently the following messages may be generated: 
 *!   @string 
 *!     @value "bad_cycle" 
 *!       A cycle where the destruction order isn't deterministic 
 *!       was detected by the garbage collector. 
 *! 
 *!       @[data] will in this case contain an array of the elements 
 *!       in the cycle. 
 *!   @endstring 
 *! 
 *! @param data 
 *!   Optional data that further describes the warning specified by @[msg]. 
 */ 
 
/*! @endclass 
 */ 
 
static void warn_bad_cycles(void) 
{ 
  /* The reason for the extra level of indirection, is that it might 
   * be clobbered by the longjump() in SET_ONERROR otherwise. 
   * (On some architectures longjump() might restore obj_arr's original 
   * value (eg if obj_arr is in a register)). 
   */ 
#if 0 
  struct array **obj_arr_ = (struct array **)xalloc(sizeof(struct array *)); 
  ONERROR tmp; 
 
  *obj_arr_ = NULL; 
 
  SET_ONERROR(tmp, free_obj_arr, obj_arr_); 
 
  { 
    struct gc_pop_frame *p; 
    unsigned cycle = 0; 
    *obj_arr_ = allocate_array(0); 
 
    for (p = kill_list; p;) { 
      if ((cycle = p->cycle)) { 
        push_object((struct object *) p->data); 
        dmalloc_touch_svalue(Pike_sp-1); 
        *obj_arr_ = append_array(*obj_arr_, --Pike_sp); 
      } 
      p = p->next; 
      if (p ? ((unsigned)(p->cycle != cycle)) : cycle) { 
        if ((*obj_arr_)->size >= 2) { 
          push_text("gc"); 
          push_text("bad_cycle"); 
          push_array(*obj_arr_); 
          *obj_arr_ = 0; 
          SAFE_APPLY_MASTER("runtime_warning", 3); 
          pop_stack(); 
          *obj_arr_ = allocate_array(0); 
        } 
        else *obj_arr_ = resize_array(*obj_arr_, 0); 
      } 
      if (!p) break; 
    } 
  } 
 
  CALL_AND_UNSET_ONERROR(tmp); 
#endif 
} 
 
size_t do_gc(void *UNUSED(ignored), int explicit_call) 
{ 
  ALLOC_COUNT_TYPE start_allocs; 
  size_t start_num_objs, unreferenced; 
  cpu_time_t gc_start_time, gc_start_real_time; 
  ptrdiff_t objs, pre_kill_objs; 
#if defined (PIKE_DEBUG) || defined (DO_PIKE_CLEANUP) 
  unsigned destroy_count; 
#endif 
#ifdef PIKE_DEBUG 
  unsigned obj_count; 
  ONERROR uwp; 
#endif 
 
  if(Pike_in_gc) return 0; 
 
  if (gc_enabled <= 0 && (gc_enabled < 0 || !explicit_call)) { 
    /* If this happens then the gc has been disabled for a very long 
     * time and num_allocs > GC_MAX_ALLOC_THRESHOLD. Have to reset 
     * num_allocs, but then we also reset saved_alloc_threshold to 
     * GC_MIN_ALLOC_THRESHOLD so that a gc is run quickly if it ever 
     * is enabled again. */ 
#ifdef GC_INTERVAL_DEBUG 
    fprintf (stderr, "GC disabled: num_allocs %"PRINT_ALLOC_COUNT_TYPE", " 
             ", alloc_threshold %"PRINT_ALLOC_COUNT_TYPE"\n", 
             num_allocs, alloc_threshold); 
#endif 
    num_allocs = 0; 
    saved_alloc_threshold = GC_MIN_ALLOC_THRESHOLD; 
    if (gc_evaluator_callback) { 
      remove_callback (gc_evaluator_callback); 
      gc_evaluator_callback = NULL; 
    } 
    return 0; 
  } 
 
#ifdef DEBUG_MALLOC 
  if(debug_options & GC_RESET_DMALLOC) 
    reset_debug_malloc(); 
#endif 
  init_gc(); 
  gc_generation++; 
  Pike_in_gc=GC_PASS_PREPARE; 
 
  if (!SAFE_IS_ZERO(&gc_pre_cb)) { 
    safe_apply_svalue(&gc_pre_cb, 0, 1); 
    pop_stack(); 
  } 
 
  gc_start_time = get_cpu_time(); 
  gc_start_real_time = get_real_time(); 
#ifdef GC_DEBUG 
  gc_debug = (GC_DEBUG + 0) || 1; 
#else 
  gc_debug = d_flag; 
#endif 
#ifdef PIKE_DEBUG 
  SET_ONERROR(uwp, fatal_on_error, "Shouldn't get an exception inside the gc.\n"); 
  if (gc_is_watching) 
    fprintf(stderr, "## Doing gc while watching for %d things.\n", gc_is_watching); 
#endif 
 
  destruct_objects_to_destruct(); 
 
  if(gc_evaluator_callback) 
  { 
    remove_callback(gc_evaluator_callback); 
    gc_evaluator_callback=0; 
  } 
 
  objs=num_objects; 
 
  if(GC_VERBOSE_DO(1 ||) gc_trace) { 
    if (gc_destruct_everything) 
      fprintf (stderr, "Destructing all objects... "); 
    else 
      fprintf(stderr,"Garbage collecting... "); 
    GC_VERBOSE_DO(fprintf(stderr, "\n")); 
  } 
#ifdef PIKE_DEBUG 
  if(num_objects < 0) 
    Pike_fatal("Panic, less than zero objects!\n"); 
#endif 
 
  last_gc=time(0); 
  start_num_objs = num_objects; 
  start_allocs = num_allocs; 
  num_allocs = 0; 
 
  /* Object alloc/free and any reference changes are disallowed now. */ 
 
#ifdef PIKE_DEBUG 
  delayed_freed = weak_freed = checked = marked = cycle_checked = live_ref = 0; 
  mark_live = frame_rot = link_search = 0; 
#endif 
  rec_frames = link_frames = free_extra_frames = 0; 
  max_rec_frames = max_link_frames = 0; 
  if (gc_debug) { 
    unsigned n; 
    Pike_in_gc = GC_PASS_PRETOUCH; 
    n = gc_touch_all_arrays(); 
    n += gc_touch_all_multisets(); 
    n += gc_touch_all_mappings(); 
    n += gc_touch_all_programs(); 
    n += gc_touch_all_objects(); 
#ifdef PIKE_DEBUG 
    gc_touch_all_strings(); 
#endif 
    if (n != (unsigned) num_objects && !got_unlinked_things) 
      Pike_fatal("Object count wrong before gc; expected %d, got %d.\n", num_objects, n); 
    GC_VERBOSE_DO(fprintf(stderr, "| pretouch: %u things\n", n)); 
  } 
 
  /* First we count internal references */ 
  Pike_in_gc=GC_PASS_CHECK; 
  gc_ext_weak_refs = 0; 
 
#ifdef PIKE_DEBUG 
  mark_externals(); 
#endif 
  call_callback(& gc_callbacks, NULL); 
 
  ACCEPT_UNFINISHED_TYPE_FIELDS { 
    gc_check_all_arrays(); 
    gc_check_all_multisets(); 
    gc_check_all_mappings(); 
    gc_check_all_programs(); 
    gc_check_all_objects(); 
#if defined (PIKE_DEBUG) || defined (DO_PIKE_CLEANUP) 
    gc_check_all_types(); 
#endif 
  } END_ACCEPT_UNFINISHED_TYPE_FIELDS; 
 
  GC_VERBOSE_DO(fprintf(stderr, "| check: %u references in %d things, " 
                        "counted %"PRINTSIZET"u weak refs\n", 
                        checked, num_objects, gc_ext_weak_refs)); 
 
  /* Object alloc/free are still disallowed, but refs might be lowered 
   * by gc_free_(short_)svalue. */ 
 
  Pike_in_gc=GC_PASS_MARK; 
 
  /* Anything after and including gc_internal_* in the linked lists 
   * are considered to lack external references. The mark pass move 
   * externally referenced things in front of these pointers. */ 
  gc_internal_array = first_array; 
  gc_internal_multiset = first_multiset; 
  gc_internal_mapping = first_mapping; 
  gc_internal_program = first_program; 
  gc_internal_object = first_object; 
 
  if (gc_destruct_everything) { 
    GC_VERBOSE_DO(fprintf(stderr, 
                          "| mark pass skipped - will destruct all objects\n")); 
  } 
  else { 
    /* Next we mark anything with external references. Note that we can 
     * follow the same reference several times, e.g. with shared mapping 
     * data blocks. */ 
    ACCEPT_UNFINISHED_TYPE_FIELDS { 
      CHECK_MARK_QUEUE_EMPTY(); 
      gc_mark_all_arrays(); 
      gc_mark_run_queue(); 
      gc_mark_all_multisets(); 
      gc_mark_run_queue(); 
      gc_mark_all_mappings(); 
      gc_mark_run_queue(); 
      gc_mark_all_programs(); 
      gc_mark_run_queue(); 
      gc_mark_all_objects(); 
      gc_mark_run_queue(); 
#ifdef PIKE_DEBUG 
      if(gc_debug) gc_mark_all_strings(); 
#endif /* PIKE_DEBUG */ 
      CHECK_MARK_QUEUE_EMPTY(); 
    } END_ACCEPT_UNFINISHED_TYPE_FIELDS; 
 
    GC_VERBOSE_DO(fprintf(stderr, 
                          "| mark: %u markers referenced, %u weak references freed,\n" 
                          "|       %d things to free, " 
                          "got %"PRINTSIZET"u tricky weak refs\n", 
                          marked, weak_freed, delayed_freed, gc_ext_weak_refs)); 
  } 
 
  { 
#ifdef PIKE_DEBUG 
    size_t orig_ext_weak_refs = gc_ext_weak_refs; 
    obj_count = delayed_freed; 
#endif 
    Pike_in_gc=GC_PASS_CYCLE; 
 
    /* Now find all cycles in the internal structures. Note that we can 
     * follow the same reference several times, just like in the mark 
     * pass. */ 
    /* Note: The order between types here is normally not significant, 
     * but the permuting destruct order tests in the testsuite won't be 
     * really effective unless objects are handled first. :P */ 
    gc_cycle_check_all_objects(); 
    gc_cycle_check_all_arrays(); 
    gc_cycle_check_all_multisets(); 
    gc_cycle_check_all_mappings(); 
    gc_cycle_check_all_programs(); 
 
#ifdef PIKE_DEBUG 
    if (stack_top != &sentinel_frame) 
      Pike_fatal("Frame stack not empty at end of cycle check pass.\n"); 
    if (gc_ext_weak_refs != orig_ext_weak_refs) 
      Pike_fatal("gc_ext_weak_refs changed from %"PRINTSIZET"u " 
            "to %"PRINTSIZET"u in cycle check pass.\n", 
            orig_ext_weak_refs, gc_ext_weak_refs); 
#endif 
 
    GC_VERBOSE_DO(fprintf(stderr, 
                          "| cycle: %u internal things visited,\n" 
                          "|        %u weak references freed, %d more things to free,\n" 
                          "|        %u mark live visits, %u frame rotations,\n" 
                          "|        %u links searched, used max %u link frames,\n" 
                          "|        %u rec frames and %u free extra frames\n", 
                          cycle_checked, weak_freed, delayed_freed - obj_count, 
                          mark_live, frame_rot, link_search, max_link_frames, 
                          max_rec_frames, free_extra_frames)); 
 
#ifdef PIKE_DEBUG 
    if (link_frames) fatal ("Leaked %u link frames.\n", link_frames); 
#endif 
  } 
 
  if (gc_ext_weak_refs) { 
    size_t to_free = gc_ext_weak_refs; 
#ifdef PIKE_DEBUG 
    obj_count = delayed_freed; 
#endif 
    Pike_in_gc = GC_PASS_ZAP_WEAK; 
    CHECK_MARK_QUEUE_EMPTY(); 
    /* Zap weak references from external to internal things. That 
     * occurs when something has both external weak refs and nonweak 
     * cyclic refs from internal things. */ 
    gc_zap_ext_weak_refs_in_mappings(); 
    gc_zap_ext_weak_refs_in_arrays(); 
    gc_zap_ext_weak_refs_in_multisets(); 
    gc_zap_ext_weak_refs_in_objects(); 
    gc_zap_ext_weak_refs_in_programs(); 
    CHECK_MARK_QUEUE_EMPTY(); 
    GC_VERBOSE_DO( 
      fprintf(stderr, 
              "| zap weak: freed %"PRINTPTRDIFFT"d external weak refs, " 
              "%"PRINTSIZET"u internal still around,\n" 
              "|           %d more things to free\n", 
              to_free - gc_ext_weak_refs, gc_ext_weak_refs, 
              delayed_freed - obj_count)); 
  } 
 
  if (gc_debug) { 
    unsigned n; 
#ifdef DEBUG_MALLOC 
    size_t i; 
    struct marker *m; 
#endif 
    Pike_in_gc=GC_PASS_POSTTOUCH; 
    n = gc_touch_all_arrays(); 
    n += gc_touch_all_multisets(); 
    n += gc_touch_all_mappings(); 
    n += gc_touch_all_programs(); 
    n += gc_touch_all_objects(); 
#ifdef PIKE_DEBUG 
    gc_touch_all_strings(); 
#endif 
    if (n != (unsigned) num_objects && !got_unlinked_things) 
      Pike_fatal("Object count wrong in gc; expected %d, got %d.\n", num_objects, n); 
#if 0 /* Temporarily disabled - Hubbe */ 
#ifdef PIKE_DEBUG 
#ifdef DEBUG_MALLOC 
    PTR_HASH_LOOP(marker, i, m) 
      if (!(m->flags & (GC_POSTTOUCHED|GC_WEAK_FREED)) && 
          dmalloc_is_invalid_memory_block(m->data)) { 
        fprintf(stderr, "Found a stray marker after posttouch pass: "); 
        describe_marker(m); 
        fprintf(stderr, "Describing marker location(s):\n"); 
        debug_malloc_dump_references(m, 2, 1, 0); 
        fprintf(stderr, "Describing thing for marker:\n"); 
        Pike_in_gc = 0; 
        describe(m->data); 
        Pike_in_gc = GC_PASS_POSTTOUCH; 
        Pike_fatal("Fatal in garbage collector.\n"); 
      } 
#endif 
#endif 
#endif 
    GC_VERBOSE_DO(fprintf(stderr, "| posttouch\n")); 
  } 
 
  /* Object alloc/free and reference changes are allowed again now. */ 
 
  Pike_in_gc=GC_PASS_FREE; 
#ifdef PIKE_DEBUG 
  weak_freed = 0; 
  obj_count = num_objects; 
#endif 
 
  /* Now we free the unused stuff. The extra refs to gc_internal_* 
   * added above are removed just before the calls so we'll get the 
   * correct relative positions in them. */ 
  unreferenced = 0; 
  if (gc_internal_array) 
    unreferenced += gc_free_all_unreferenced_arrays(); 
  if (gc_internal_multiset) 
    unreferenced += gc_free_all_unreferenced_multisets(); 
  if (gc_internal_mapping) 
    unreferenced += gc_free_all_unreferenced_mappings(); 
  if (gc_internal_object) 
    unreferenced += gc_free_all_unreferenced_objects(); 
  /* Note: gc_free_all_unreferenced_objects needs to have the programs 
   * around to handle the free (even when they aren't live). So it's 
   * necessary to free the objects before the programs. */ 
  if (gc_internal_program) 
    unreferenced += gc_free_all_unreferenced_programs(); 
 
  if (free_extra_frames > tot_max_free_extra_frames) 
    tot_max_free_extra_frames = free_extra_frames; 
 
  /* We might occasionally get things to gc_delayed_free that the free 
   * calls above won't find. They're tracked in this list. */ 
  while (free_extra_list) { 
    struct free_extra_frame *next = free_extra_list->next; 
    union anything u; 
    u.refs = (INT32 *) free_extra_list->data; 
    gc_free_extra_ref (u.refs); 
    free_short_svalue (&u, free_extra_list->type); 
    really_free_free_extra_frame (free_extra_list); 
    free_extra_list = next; 
  } 
 
#ifdef PIKE_DEBUG 
  if (free_extra_frames) fatal ("Leaked %u free extra frames.\n", free_extra_frames); 
#endif 
 
  GC_VERBOSE_DO(fprintf(stderr, "| free: %"PRINTSIZET"u unreferenced, " 
                        "%d really freed, %u left with live references\n", 
                        unreferenced, obj_count - num_objects, live_ref)); 
 
#ifdef PIKE_DEBUG 
  gc_internal_array = (struct array *) (ptrdiff_t) -1; 
  gc_internal_multiset = (struct multiset *) (ptrdiff_t) -1; 
  gc_internal_mapping = (struct mapping *) (ptrdiff_t) -1; 
  gc_internal_program = (struct program *) (ptrdiff_t) -1; 
  gc_internal_object = (struct object *) (ptrdiff_t) -1; 
 
  if(fatal_after_gc) Pike_fatal("%s", fatal_after_gc); 
#endif 
 
  Pike_in_gc=GC_PASS_KILL; 
 
  /* Destruct the live objects in cycles, but first warn about any bad 
   * cycles. */ 
  pre_kill_objs = num_objects; 
  if (Pike_interpreter.evaluator_stack && !gc_destruct_everything) { 
    objs -= num_objects; 
    warn_bad_cycles(); 
    objs += num_objects; 
  } 
#if defined (PIKE_DEBUG) || defined (DO_PIKE_CLEANUP) 
  destroy_count = 0; 
#endif 
 
  if (!SAFE_IS_ZERO(&gc_post_cb)) { 
    safe_apply_svalue(&gc_post_cb, 0, 1); 
    pop_stack(); 
  } 
 
  { 
    enum object_destruct_reason reason = 
#ifdef DO_PIKE_CLEANUP 
      gc_destruct_everything ? DESTRUCT_CLEANUP : 
#endif 
      DESTRUCT_GC; 
 
#ifdef PIKE_DEBUG 
      { 
        struct gc_rec_frame *r; 
        for (r = kill_list; r != &sentinel_frame; r = r->next) 
          /* Can't do this while the list is being freed below. */ 
          CHECK_KILL_LIST_FRAME (r); 
      } 
 
      /* A helper to locate garbage through trampolines. 
       * FIXME: This ought to be accessible even when pike is compiled 
       * without rtldebug. */ 
      if (gc_trace >= 3 && !gc_destruct_everything) { 
        struct gc_rec_frame *r; 
        for (r = kill_list; r != &sentinel_frame; r = r->next) { 
          struct object *o = (struct object *) r->data; 
          if (o->prog == pike_trampoline_program && o->refs > 1) { 
            fprintf (stderr, "Got trampoline garbage:\n"); 
            describe_something (o, T_OBJECT, 0, 0, 0, NULL); 
            locate_references (o); 
          } 
        } 
      } 
#endif 
 
    while (kill_list != &sentinel_frame) { 
      struct gc_rec_frame *next = kill_list->next; 
      struct object *o = (struct object *) kill_list->data; 
 
#ifdef PIKE_DEBUG 
      if ((get_marker(kill_list->data)->flags & (GC_LIVE|GC_LIVE_OBJ)) != 
          (GC_LIVE|GC_LIVE_OBJ)) 
        gc_fatal(o, 0, "Invalid object on kill list.\n"); 
      if (o->prog && (o->prog->flags & PROGRAM_USES_PARENT) && 
          PARENT_INFO(o)->parent && 
          !PARENT_INFO(o)->parent->prog && 
          get_marker(PARENT_INFO(o)->parent)->flags & GC_LIVE_OBJ) 
        gc_fatal(o, 0, "GC destructed parent prematurely.\n"); 
#endif 
 
      GC_VERBOSE_DO( 
        fprintf(stderr, "|   Killing %p with %d refs", o, o->refs); 
        if (o->prog) { 
          INT_TYPE line; 
          struct pike_string *file = get_program_line (o->prog, &line); 
          fprintf(stderr, ", prog %s:%d\n", file->str, line); 
          free_string(file); 
        } 
        else fputs(", is destructed\n", stderr); 
      ); 
      if (!SAFE_IS_ZERO(&gc_destruct_cb)) { 
        ref_push_object(o); 
        push_int(reason); 
        push_int(o->refs - 1); 
        safe_apply_svalue(&gc_destruct_cb, 3, 1); 
        pop_stack(); 
      } 
 
      destruct_object (o, reason); 
      free_object(o); 
      gc_free_extra_ref(o); 
#if defined (PIKE_DEBUG) || defined (DO_PIKE_CLEANUP) 
      destroy_count++; 
#endif 
      really_free_gc_rec_frame (kill_list); 
      kill_list = next; 
    } 
  } 
 
#ifdef PIKE_DEBUG 
  if (rec_frames) fatal ("Leaked %u rec frames.\n", rec_frames); 
#endif 
 
  GC_VERBOSE_DO(fprintf(stderr, "| kill: %u objects killed, " 
                        "%"PRINTSIZET"u things really freed\n", 
                        destroy_count, pre_kill_objs - num_objects)); 
 
  Pike_in_gc=GC_PASS_DESTRUCT; 
  /* Destruct objects on the destruct queue. */ 
  GC_VERBOSE_DO(obj_count = num_objects); 
  destruct_objects_to_destruct(); 
  GC_VERBOSE_DO(fprintf(stderr, "| destruct: %d things really freed\n", 
                        obj_count - num_objects)); 
 
#ifdef PIKE_DEBUG 
  if (gc_extra_refs) { 
    size_t e; 
    fprintf (stderr, "Lost track of %d extra refs to things in gc.\n" 
             "Searching for marker(s) with extra refs:\n", gc_extra_refs); 
    for (e = 0; e < marker_hash_table_size; e++) { 
      struct marker *s = marker_hash_table[e], *m; 
      for (m = s; m;) { 
        if (m->flags & GC_GOT_EXTRA_REF) { 
          fprintf (stderr, "========================================\n" 
                   "Found marker with extra ref: "); 
          describe_marker (m); 
          fprintf (stderr, "Describing the thing pointed to:\n"); 
          describe (m->data); 
        } 
        m = m->next; 
        /* The marker might be moved to the head of the chain via 
         * describe() above, so do this to avoid infinite recursion. 
         * Some entries in the chain might be missed, but I don't want 
         * to bother. */ 
        if (m == s) break; 
      } 
    } 
    fprintf (stderr, "========================================\n" 
             "Done searching for marker(s) with extra refs.\n"); 
    Pike_fatal("Lost track of %d extra refs to things in gc.\n", gc_extra_refs); 
  } 
  if(fatal_after_gc) Pike_fatal("%s", fatal_after_gc); 
#endif 
 
  /* Calculate the next alloc_threshold. */ 
  { 
    double multiplier, new_threshold; 
    cpu_time_t last_non_gc_time, last_gc_time; 
#ifdef GC_INTERVAL_DEBUG 
    double tmp_dbl1, tmp_dbl2; 
#endif 
 
    /* If we're at an automatic and timely gc then start_allocs == 
     * alloc_threshold and we're using gc_average_slowness in the 
     * decaying average calculation. Otherwise this is either an 
     * explicit call (start_allocs < alloc_threshold) or the gc has 
     * been delayed past its due time (start_allocs > 
     * alloc_threshold), and in those cases we adjust the multiplier 
     * to give the appropriate weight to this last instance. */ 
    multiplier=pow(gc_average_slowness, 
                   (double) start_allocs / (double) alloc_threshold); 
 
#ifdef GC_INTERVAL_DEBUG 
    if (GC_VERBOSE_DO(1 ||) gc_trace) fputc ('\n', stderr); 
    fprintf (stderr, "IN:  GC start @ %"PRINT_CPU_TIME" "CPU_TIME_UNIT"\n" 
             "     avg slow %g, start_allocs %"PRINT_ALLOC_COUNT_TYPE", " 
             "alloc_threshold %"PRINT_ALLOC_COUNT_TYPE" -> mult %g\n", 
             gc_start_real_time, 
             gc_average_slowness, start_allocs, alloc_threshold, multiplier); 
    tmp_dbl1 = non_gc_time; 
    tmp_dbl2 = gc_time; 
#endif 
 
    /* Comparisons to avoid that overflows mess up the statistics. */ 
    if (last_gc_end_real_time != -1 && 
        gc_start_real_time > last_gc_end_real_time) { 
      last_non_gc_time = gc_start_real_time - last_gc_end_real_time; 
      non_gc_time = non_gc_time * multiplier + 
        last_non_gc_time * (1.0 - multiplier); 
    } 
    else last_non_gc_time = (cpu_time_t) -1; 
    last_gc_end_real_time = get_real_time(); 
    if (last_gc_end_real_time > gc_start_real_time) { 
      gc_time = gc_time * multiplier + 
        (last_gc_end_real_time - gc_start_real_time) * (1.0 - multiplier); 
    } 
 
#ifdef GC_INTERVAL_DEBUG 
    fprintf (stderr, 
             "     non_gc_time: %13"PRINT_CPU_TIME" "CPU_TIME_UNIT", " 
             "%.12g -> %.12g\n" 
             "     gc_time:     %13"PRINT_CPU_TIME" "CPU_TIME_UNIT", " 
             "%.12g -> %.12g\n", 
             last_non_gc_time, tmp_dbl1, non_gc_time, 
             last_gc_end_real_time > gc_start_real_time ? 
             last_gc_end_real_time - gc_start_real_time : (cpu_time_t) -1, 
             tmp_dbl2, gc_time); 
    tmp_dbl1 = objects_alloced; 
    tmp_dbl2 = objects_freed; 
#endif 
 
    { 
      cpu_time_t gc_end_time = get_cpu_time(); 
      if (gc_end_time > gc_start_time) 
        last_gc_time = gc_end_time - gc_start_time; 
      else 
        last_gc_time = (cpu_time_t) -1; 
    } 
 
    /* At this point, unreferenced contains the number of things that 
     * were without external references during the check and mark 
     * passes. In the process of freeing them, destroy functions might 
     * have been called which means anything might have happened. 
     * Therefore we use that figure instead of the difference between 
     * the number of allocated things to measure the amount of 
     * garbage. */ 
    last_garbage_ratio = (double) unreferenced / start_num_objs; 
 
    objects_alloced = objects_alloced * multiplier + 
      start_allocs * (1.0 - multiplier); 
    objects_freed = objects_freed * multiplier + 
      unreferenced * (1.0 - multiplier); 
 
#ifdef GC_INTERVAL_DEBUG 
    fprintf (stderr, 
             "     objects_alloced: %9"PRINT_ALLOC_COUNT_TYPE" allocs, " 
             "%.12g -> %.12g\n" 
             "     objects_freed:   %9"PRINT_ALLOC_COUNT_TYPE" unrefd, " 
             "%.12g -> %.12g\n", 
             start_allocs, tmp_dbl1, objects_alloced, 
             unreferenced, tmp_dbl2, objects_freed); 
#endif 
 
    if (last_non_gc_time == (cpu_time_t) -1 || 
        gc_time / non_gc_time <= gc_time_ratio) { 
      /* Calculate the new threshold by adjusting the average 
       * threshold (objects_alloced) with the ratio between the wanted 
       * garbage at the next gc (gc_garbage_ratio_low * 
       * start_num_objs) and the actual average garbage 
       * (objects_freed). (Where the +1.0's come from I don't know. 
       * Perhaps they're to avoid division by zero. /mast) */ 
      new_threshold = (objects_alloced+1.0) * 
        (gc_garbage_ratio_low * start_num_objs) / (objects_freed+1.0); 
      last_garbage_strategy = GARBAGE_RATIO_LOW; 
#ifdef GC_INTERVAL_DEBUG 
      fprintf (stderr, "     strategy: low ratio %g, objs %"PRINTSIZET"u, " 
               "new threshold -> %.12g\n", 
               gc_garbage_ratio_low, start_num_objs, new_threshold); 
#endif 
    } 
    else { 
      new_threshold = (objects_alloced+1.0) * 
        (gc_garbage_ratio_high * start_num_objs) / (objects_freed+1.0); 
      last_garbage_strategy = GARBAGE_RATIO_HIGH; 
#ifdef GC_INTERVAL_DEBUG 
      fprintf (stderr, "     strategy: high ratio %g, objs %"PRINTSIZET"u, " 
               "new threshold -> %.12g\n", 
               gc_garbage_ratio_high, start_num_objs, new_threshold); 
#endif 
    } 
 
    if (non_gc_time > 0.0 && gc_min_time_ratio > 0.0) { 
      /* Upper limit on the new threshold based on gc_min_time_ratio. */ 
      double max_threshold = (objects_alloced+1.0) * 
        gc_time / (gc_min_time_ratio * non_gc_time); 
#ifdef GC_INTERVAL_DEBUG 
      fprintf (stderr, "     max interval? min time ratio %g, " 
               "max threshold %.12g -> %s\n", 
               gc_min_time_ratio, max_threshold, 
               max_threshold < new_threshold ? "yes" : "no"); 
#endif 
      if (max_threshold < new_threshold) { 
        new_threshold = max_threshold; 
        last_garbage_strategy = GARBAGE_MAX_INTERVAL; 
      } 
    } 
 
#if 0 
    /* Afaics this is to limit the growth of the threshold to avoid 
     * that a single sudden allocation spike causes a very long gc 
     * interval the next time. Now when the bug in the decaying 
     * average calculation is fixed there should be no risk for that, 
     * at least not in any case when this would help. /mast */ 
    if(alloc_threshold + start_allocs < new_threshold) 
      new_threshold = (double)(alloc_threshold + start_allocs); 
#endif 
 
    if(new_threshold < GC_MIN_ALLOC_THRESHOLD) 
      alloc_threshold = GC_MIN_ALLOC_THRESHOLD; 
    else if(new_threshold > GC_MAX_ALLOC_THRESHOLD) 
      alloc_threshold = GC_MAX_ALLOC_THRESHOLD; 
    else 
      alloc_threshold = (ALLOC_COUNT_TYPE) new_threshold; 
 
#ifdef GC_INTERVAL_DEBUG 
    fprintf (stderr, "OUT: GC end   @ %"PRINT_CPU_TIME" "CPU_TIME_UNIT", " 
             "new capped threshold %"PRINT_ALLOC_COUNT_TYPE"\n", 
             last_gc_end_real_time, alloc_threshold); 
#endif 
 
    if (!explicit_call) { 
      auto_gc_real_time += get_real_time() - gc_start_real_time; 
 
      if (last_gc_time != (cpu_time_t) -1) { 
#ifdef CPU_TIME_MIGHT_BE_THREAD_LOCAL 
      if (cpu_time_is_thread_local 
#ifdef PIKE_DEBUG 
          /* At high debug levels, the gc may get called before 
             * the threads are initialized. 
             */ 
            && Pike_interpreter.thread_state 
#endif 
         ) 
          Pike_interpreter.thread_state->auto_gc_time += last_gc_time; 
#endif 
      auto_gc_time += last_gc_time; 
      } 
    } 
 
    if(GC_VERBOSE_DO(1 ||) gc_trace) 
    { 
      char timestr[40]; 
      if (last_gc_time != (cpu_time_t) -1) 
        sprintf (timestr, ", %ld ms", 
                 (long) (last_gc_time / (CPU_TIME_TICKS / 1000))); 
      else 
        timestr[0] = 0; 
#ifdef DO_PIKE_CLEANUP 
      if (gc_destruct_everything) 
        fprintf(stderr, "done (%u %s destructed)%s\n", 
                destroy_count, destroy_count == 1 ? "was" : "were", timestr); 
      else 
#endif 
      fprintf(stderr, "done (%"PRINTSIZET"u of %"PRINTSIZET"u " 
                "%s unreferenced)%s\n", 
                unreferenced, start_num_objs, 
                unreferenced == 1 ? "was" : "were", 
                timestr); 
    } 
  } 
 
#ifdef PIKE_DEBUG 
  UNSET_ONERROR (uwp); 
  tot_cycle_checked += cycle_checked; 
  tot_mark_live += mark_live, tot_frame_rot += frame_rot; 
#endif 
  if (max_rec_frames > tot_max_rec_frames) 
    tot_max_rec_frames = max_rec_frames; 
  if (max_link_frames > tot_max_link_frames) 
    tot_max_link_frames = max_link_frames; 
 
  Pike_in_gc=0; 
  exit_gc(); 
 
#ifdef ALWAYS_GC 
  ADD_GC_CALLBACK(); 
#else 
  if(d_flag > 3) ADD_GC_CALLBACK(); 
#endif 
 
#ifdef DO_PIKE_CLEANUP 
  if (gc_destruct_everything) 
    return destroy_count; 
#endif 
 
  if (!SAFE_IS_ZERO(&gc_done_cb)) { 
    push_int(unreferenced); 
    safe_apply_svalue(&gc_done_cb, 1, 1); 
    pop_stack(); 
  } 
 
  return unreferenced; 
} 
 
/*! @decl mapping(string:int|float) gc_status() 
 *! @belongs Debug 
 *! 
 *! Get statistics from the garbage collector. 
 *! 
 *! @returns 
 *!   A mapping with the following content will be returned: 
 *!   @mapping 
 *!     @member int "num_objects" 
 *!       Number of arrays, mappings, multisets, objects and programs. 
 *!     @member int "num_allocs" 
 *!       Number of memory allocations since the last gc run. 
 *!     @member int "alloc_threshold" 
 *!       Threshold for "num_allocs" when another automatic gc run is 
 *!       scheduled. 
 *!     @member float "projected_garbage" 
 *!       Estimation of the current amount of garbage. 
 *!     @member int "objects_alloced" 
 *!       Decaying average over the number of allocated objects 
 *!       between gc runs. 
 *!     @member int "objects_freed" 
 *!       Decaying average over the number of freed objects in each gc 
 *!       run. 
 *!     @member float "last_garbage_ratio" 
 *!       Garbage ratio in the last gc run. 
 *!     @member int "non_gc_time" 
 *!       Decaying average over the interval between gc runs, measured 
 *!       in real time nanoseconds. 
 *!     @member int "gc_time" 
 *!       Decaying average over the length of the gc runs, measured in 
 *!       real time nanoseconds. 
 *!     @member string "last_garbage_strategy" 
 *!       The garbage accumulation goal that the gc aimed for when 
 *!       setting "alloc_threshold" in the last run. The value is 
 *!       either "garbage_ratio_low", "garbage_ratio_high" or 
 *!       "garbage_max_interval". The first two correspond to the gc 
 *!       parameters with the same names in @[Pike.gc_parameters], and 
 *!       the last is the minimum gc time limit specified through the 
 *!       "min_gc_time_ratio" parameter to @[Pike.gc_parameters]. 
 *!     @member int "last_gc" 
 *!       Time when the garbage-collector last ran. 
 *!     @member int "total_gc_cpu_time" 
 *!       The total amount of CPU time that has been consumed in 
 *!       implicit GC runs, in nanoseconds. 0 on systems where Pike 
 *!       lacks support for CPU time measurement. 
 *!     @member int "total_gc_real_time" 
 *!       The total amount of real time that has been spent in 
 *!       implicit GC runs, in nanoseconds. 
 *!   @endmapping 
 *! 
 *! @seealso 
 *!   @[gc()], @[Pike.gc_parameters()], @[Pike.implicit_gc_real_time] 
 */ 
void f__gc_status(INT32 args) 
{ 
  int size = 0; 
 
  pop_n_elems(args); 
 
  push_text("num_objects"); 
  push_int(num_objects); 
  size++; 
 
  push_text("num_allocs"); 
  push_int64(num_allocs); 
  size++; 
 
  push_text("alloc_threshold"); 
  push_int64(alloc_threshold); 
  size++; 
 
  push_text("projected_garbage"); 
  push_float(DO_NOT_WARN((FLOAT_TYPE)(objects_freed * (double) num_allocs / 
                                      (double) alloc_threshold))); 
  size++; 
 
  push_text("objects_alloced"); 
  push_int64(DO_NOT_WARN((INT64)objects_alloced)); 
  size++; 
 
  push_text("objects_freed"); 
  push_int64(DO_NOT_WARN((INT64)objects_freed)); 
  size++; 
 
  push_text("last_garbage_ratio"); 
  push_float(DO_NOT_WARN((FLOAT_TYPE) last_garbage_ratio)); 
  size++; 
 
  push_text("non_gc_time"); 
  push_int64(DO_NOT_WARN((INT64) non_gc_time)); 
  size++; 
 
  push_text("gc_time"); 
  push_int64(DO_NOT_WARN((INT64) gc_time)); 
  size++; 
 
  push_text ("last_garbage_strategy"); 
  switch (last_garbage_strategy) { 
    case GARBAGE_RATIO_LOW: 
      push_text ("garbage_ratio_low"); break; 
    case GARBAGE_RATIO_HIGH: 
      push_text ("garbage_ratio_high"); break; 
    case GARBAGE_MAX_INTERVAL: 
      push_text ("garbage_max_interval"); break; 
#ifdef PIKE_DEBUG 
    default: 
      Pike_fatal ("Unknown last_garbage_strategy %d\n", last_garbage_strategy); 
#endif 
  } 
  size++; 
 
  push_text("last_gc"); 
  push_int64(last_gc); 
  size++; 
 
  push_text ("total_gc_cpu_time"); 
  push_int64 (auto_gc_time); 
#ifndef LONG_CPU_TIME 
  push_int (1000000000 / CPU_TIME_TICKS); 
  o_multiply(); 
#endif 
  size++; 
 
  push_text ("total_gc_real_time"); 
  push_int64 (auto_gc_real_time); 
#ifndef LONG_CPU_TIME 
  push_int (1000000000 / CPU_TIME_TICKS); 
  o_multiply(); 
#endif 
  size++; 
 
#ifdef PIKE_DEBUG 
  push_text ("max_rec_frames"); 
  push_int64 (DO_NOT_WARN ((INT64) tot_max_rec_frames)); 
  size++; 
 
  push_text ("max_link_frames"); 
  push_int64 (DO_NOT_WARN ((INT64) tot_max_link_frames)); 
  size++; 
 
  push_text ("max_free_extra_frames"); 
  push_int64 (DO_NOT_WARN ((INT64) tot_max_free_extra_frames)); 
  size++; 
#endif 
 
  f_aggregate_mapping(size * 2); 
} 
 
/*! @decl int implicit_gc_real_time (void|int nsec) 
 *! @belongs Pike 
 *! 
 *! Returns the total amount of real time that has been spent in 
 *! implicit GC runs. The time is normally returned in microseconds, 
 *! but if the optional argument @[nsec] is nonzero it's returned in 
 *! nanoseconds. 
 *! 
 *! @seealso 
 *!   @[Debug.gc_status] 
 */ 
void f_implicit_gc_real_time (INT32 args) 
{ 
  int nsec = args && !UNSAFE_IS_ZERO (Pike_sp - args); 
  pop_n_elems (args); 
  if (nsec) { 
    push_int64 (auto_gc_real_time); 
#ifndef LONG_CPU_TIME 
    push_int (1000000000 / CPU_TIME_TICKS); 
    o_multiply(); 
#endif 
  } 
  else { 
#if CPU_TIME_TICKS_LOW > 1000000 
    push_int64 (auto_gc_real_time / (CPU_TIME_TICKS / 1000000)); 
#else 
    push_int64 (auto_gc_real_time); 
    push_int (1000000 / CPU_TIME_TICKS); 
    o_multiply(); 
#endif 
  } 
} 
 
void dump_gc_info(void) 
{ 
  fprintf(stderr,"Current number of things   : %d\n",num_objects); 
  fprintf(stderr,"Allocations since last gc  : %"PRINT_ALLOC_COUNT_TYPE"\n", 
          num_allocs); 
  fprintf(stderr,"Threshold for next gc      : %"PRINT_ALLOC_COUNT_TYPE"\n", 
          alloc_threshold); 
  fprintf(stderr,"Projected current garbage  : %f\n", 
          objects_freed * (double) num_allocs / (double) alloc_threshold); 
 
  fprintf(stderr,"Avg allocs between gc      : %f\n",objects_alloced); 
  fprintf(stderr,"Avg frees per gc           : %f\n",objects_freed); 
  fprintf(stderr,"Garbage ratio in last gc   : %f\n", last_garbage_ratio); 
                                              
  fprintf(stderr,"Avg "CPU_TIME_UNIT" between gc          : %f\n", non_gc_time); 
  fprintf(stderr,"Avg "CPU_TIME_UNIT" in gc               : %f\n", gc_time); 
  fprintf(stderr,"Avg time ratio in gc       : %f\n", gc_time / non_gc_time); 
 
  fprintf(stderr,"Garbage strategy in last gc: %s\n", 
          last_garbage_strategy == GARBAGE_RATIO_LOW ? "garbage_ratio_low" : 
          last_garbage_strategy == GARBAGE_RATIO_HIGH ? "garbage_ratio_high" : 
          last_garbage_strategy == GARBAGE_MAX_INTERVAL ? 
          "garbage_max_interval" : "???"); 
 
#ifdef PIKE_DEBUG 
  fprintf(stderr,"Max used recursion frames  : %u\n", tot_max_rec_frames); 
  fprintf(stderr,"Max used link frames       : %u\n", tot_max_link_frames); 
  fprintf(stderr,"Max used free extra frames : %u\n", tot_max_free_extra_frames); 
  fprintf(stderr,"Marked live ratio          : %g\n", 
          (double) tot_mark_live / tot_cycle_checked); 
  fprintf(stderr,"Frame rotation ratio       : %g\n", 
          (double) tot_frame_rot / tot_cycle_checked); 
#endif 
 
  fprintf(stderr,"in_gc                      : %d\n", Pike_in_gc); 
} 
 
void cleanup_gc(void) 
{ 
#ifdef PIKE_DEBUG 
  if (gc_evaluator_callback) { 
    remove_callback(gc_evaluator_callback); 
    gc_evaluator_callback = NULL; 
  } 
#endif /* PIKE_DEBUG */ 
} 
 
/* Visit things API */ 
 
PMOD_EXPORT visit_ref_cb *visit_ref = NULL; 
PMOD_EXPORT visit_enter_cb *visit_enter = NULL; 
PMOD_EXPORT visit_leave_cb *visit_leave = NULL; 
 
/* Be careful if extending this with internal types like 
 * T_MAPPING_DATA and T_MULTISET_DATA; there's code that assumes 
 * type_from_visit_fn only returns types that fit in a TYPE_FIELD. */ 
PMOD_EXPORT visit_thing_fn *const visit_fn_from_type[MAX_TYPE + 1] = { 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *)&visit_array, 
  (visit_thing_fn *)&visit_mapping, 
  (visit_thing_fn *)&visit_multiset, 
  (visit_thing_fn *)&visit_object, 
  /* visit_function must be called with a whole svalue, so it's not 
   * included here. */ 
  (visit_thing_fn *) (ptrdiff_t) -1, 
  (visit_thing_fn *)&visit_program, 
  (visit_thing_fn *)&visit_string, 
  (visit_thing_fn *)&visit_type, 
}; 
 
PMOD_EXPORT TYPE_T type_from_visit_fn (visit_thing_fn *fn) 
{ 
  /* Since the array to search is so small, linear search is probably 
   * fastest. */ 
  unsigned t; 
  for (t = 0; t < NELEM (visit_fn_from_type); t++) 
    if (visit_fn_from_type[t] == fn) 
      return (TYPE_T) t; 
  return PIKE_T_UNKNOWN; 
} 
 
PMOD_EXPORT TYPE_FIELD real_visit_svalues (struct svalue *s, size_t num, 
                                           int ref_type, void *extra) 
{ 
  for (; num; num--, s++) 
    visit_svalue (s, ref_type, extra); 
  return 0; 
} 
 
/* Memory counting 
 * 
 * This mode is used by f_count_memory, and it's recognized by a 
 * nonzero value in mc_pass. 
 * 
 * The basic idea is to follow and count all refs from the starting 
 * point things given to f_count_memory. Whenever the counted refs add 
 * up to the refcount for a thing, that thing is known to have only 
 * internal refs, and so it's memory counted and then all its refs are 
 * followed too. 
 * 
 * To cope with internal cyclic refs, there's a "lookahead" algorithm 
 * which recurses through more things in the hope of finding cycles 
 * that otherwise would make us miss internal refs. This lookahead is 
 * limited by mc_lookahead, mc_block_lookahead, and the constant or 
 * variable "pike_cycle_depth" in objects. 
 * 
 * All things are categorized as follows: 
 * 
 * o  Internal things: These are known to have only internal 
 *    references and are memory counted. The things given to 
 *    f_count_memory as starting points are initially asserted to be 
 *    internal regardless of how many refs they got. 
 * 
 * o  Lookahead things: A lookahead thing is one that has been found 
 *    by following refs from an internal thing. 
 * 
 *    Lookahead things are further divided into three categories: 
 * 
 *    o  Incomplete: Things whose refcounts (still) are higher than 
 *       all found refs to them from both internal and lookahead 
 *       things. 
 * 
 *    o  Complete: Things whose refs from internal and lookahead 
 *       things equal their refcounts. I.e. we've found all refs going 
 *       to these. 
 * 
 *       Complete things can also be "candidates", which means they 
 *       have a direct ref from an internal thing or another 
 *       candidate. 
 * 
 *    o  Indirectly incomplete: In MC_PASS_MARK_EXTERNAL, these are 
 *       all the complete things found to be referenced by incomplete 
 *       things. 
 * 
 *    These sets are tracked through three double linked lists, 
 *    mc_incomplete, mc_complete, and mc_indirect, respectively. 
 * 
 *    The lookahead is controlled by a lookahead count for each thing. 
 *    The count is the number of links to follow emanating from that 
 *    thing. The count for internal things and candidates default to 
 *    mc_lookahead, but if a thing is an object with a 
 *    "pike_cycle_depth" variable, that number overrides it. 
 * 
 *    As links are followed to other things, their lookahead count 
 *    gets lowered, and the lookahead stops when it reaches zero (or 
 *    when reaching a thing of a type in mc_block_lookahead). If a 
 *    lookahead thing is found later on through another path with 
 *    fewer links, its lookahead count is raised so that it eventually 
 *    reflects the shortest path. 
 * 
 *    The reason for the "candidate" things which are kept at max 
 *    lookahead count is that the lookahead thereby continue as long 
 *    as it resolves complete things which eventually might turn out 
 *    to be internal. That means the lookahead distance only needs to 
 *    be large enough to cover the largest "loop" inside a structure 
 *    with many cycles, rather than the longest cyclic path. 
 * 
 *    E.g. to cover a double linked list which can be arbitrary long, 
 *    it's enough that mc_lookahead is 3; that makes the lookahead 
 *    account for the two refs to the next node (B) from the previous 
 *    complete node (A), by traversing through the next-to-next node 
 *    (C): 
 *                                      3 
 *                    L---.   L---.   L---.   L---. 
 *                 ...     (A)     (B)     (C)     ... 
 *                    `---7   `---7   `---7   `---7 
 *                              1       2 
 * 
 * o  Unvisited things: Everything else that hasn't been visited yet. 
 * 
 * For every visited thing we record the number of refs from internal 
 * things (int_refs) and from lookahead things (la_refs). 
 * 
 * The basic algorithm for finding all internal things works like 
 * this: 
 * 
 * First the starting point things are labelled internal and put into 
 * the work queue (mc_work_queue). 
 * 
 * mc_pass is set to MC_PASS_LOOKAHEAD: 
 * 
 * We do a breadth-first recursion through the things in the work 
 * queue until the lookahead count reaches zero, always starting with 
 * the things with the highest count. 
 * 
 * Every time we visit something we calculate its lookahead count as 
 * either max (if it's found to be referenced from an internal or 
 * candidate thing), the same as the source thing (if the followed ref 
 * is REF_TYPE_INTERNAL), or the next lower count (otherwise). If the 
 * count is above zero and the thing is either new or its old count 
 * was lower, it's added to the work list. 
 * 
 * mc_work_queue is a priority queue which always has the thing with 
 * the highest lookahead count first, thereby ensuring breadth-first 
 * recursion also when things have their count raised. 
 * 
 * int_refs and la_refs are updated when things are visited. They 
 * become internal if int_refs add up to the refcount. Otherwise they 
 * are put in the incomplete or complete sets as appropriate. 
 * 
 * mc_pass is set to MC_PASS_MARK_EXTERNAL: 
 * 
 * At this point the set of lookahead things is complete (as far as we 
 * are concerned), and it's divided into complete and incomplete 
 * lookahead things. All references in the incomplete list are 
 * followed to build up the set of indirectly incomplete things. The 
 * incomplete and indirectly incomplete things are referenced 
 * externally and should not be memory counted. 
 * 
 * If there's anything left in the complete list then it's internal 
 * cyclic stuff. In that case we put those things into the work list, 
 * move the indirectly incomplete list back to complete and repeat 
 * MC_PASS_LOOKAHEAD. Otherwise we're done. 
 */ 
 
/* #define MEMORY_COUNT_DEBUG */ 
 
#define MC_WQ_START_SIZE 1024 
 
PMOD_EXPORT int mc_pass; 
PMOD_EXPORT size_t mc_counted_bytes; 
 
static int mc_lookahead, mc_block_pike_cycle_depth; 
static TYPE_FIELD mc_block_lookahead; 
static TYPE_FIELD mc_block_lookahead_default = BIT_PROGRAM|BIT_STRING|BIT_TYPE; 
/* Strings are blocked because they don't contain refs. Types are 
 * blocked because they are acyclic and don't contain refs to anything 
 * but strings and other types. */ 
 
static INLINE int mc_lookahead_blocked(unsigned INT16 type) { 
    if (type < sizeof(TYPE_FIELD)*8) { 
        return !!(mc_block_lookahead & ((TYPE_FIELD)1 << type)); 
    } 
 
    return 0; 
} 
 
static int mc_block_strings; 
 
static int mc_enqueued_noninternal; 
/* Set whenever something is enqueued in MC_PASS_LOOKAHEAD that isn't 
 * internal already. This is used to detect whether another 
 * MC_PASS_MARK_EXTERNAL is necessary. */ 
 
static unsigned mc_ext_toggle_bias = 0; 
 
#define MC_PASS_LOOKAHEAD 1 
#define MC_PASS_MARK_EXTERNAL 2 
 
/* Set when a thing has become internal. */ 
#define MC_FLAG_INTERNAL      0x01 
 
/* Set when an internal thing has been visited, i.e. after its refs 
 * has been gone through for the first time. This implies that the 
 * thing has been memory counted, and taken off mc_incomplete or 
 * mc_complete if it was there. */ 
#define MC_FLAG_INT_VISITED   0x02 
 
/* Set when a non-internal thing has been visited. If 
 * MC_FLAG_INT_VISITED isn't then the thing is on one of mc_incomplete, 
 * mc_complete, or (in MC_PASS_MARK_EXTERNAL) mc_indirect. */ 
#define MC_FLAG_LA_VISITED    0x04 
 
/* Set when a thing has become a candidate (i.e. complete and 
 * referenced directly from an internal or candidate thing). This 
 * flag is meaningless when MC_FLAG_INTERNAL is set. */ 
#define MC_FLAG_CANDIDATE     0x08 
 
/* Set when a thing is visited directly from an internal or candidate 
 * thing. */ 
#define MC_FLAG_CANDIDATE_REF 0x10 
 
/* The lookahead count should not change. Use when it has been lowered 
 * from pike_cycle_depth. */ 
#define MC_FLAG_LA_COUNT_FIXED        0x20 
 
/* A toggle flag to mark external (i.e. incomplete and indirectly 
 * incomplete) things in MC_PASS_MARK_EXTERNAL so that we don't 
 * recurse them repeatedly. If mc_ext_toggle_bias is zero then it's 
 * external if this is set. If mc_ext_toggle_bias is one then it's 
 * external if this is cleared. mc_ext_toggle_bias toggles every time 
 * we leave MC_PASS_MARK_EXTERNAL, thus we avoid the work to go 
 * through the externals clear the flag for the next round. */ 
#define MC_FLAG_EXT_TOGGLE    0x40 
 
/* The value of IS_EXTERNAL is meaningless when MC_FLAG_INTERNAL is set. */ 
#define IS_EXTERNAL(M)                                                        \ 
  (((M)->flags ^ mc_ext_toggle_bias) & MC_FLAG_EXT_TOGGLE) 
 
#define INIT_CLEARED_EXTERNAL(M) do {                                 \ 
    struct mc_marker *_m = (M);                                         \ 
    if (mc_ext_toggle_bias) _m->flags |= MC_FLAG_EXT_TOGGLE;            \ 
  } while (0) 
#define FLAG_EXTERNAL(M) do {                                               \ 
    struct mc_marker *_m = (M);                                         \ 
    assert (!IS_EXTERNAL (_m));                                         \ 
    _m->flags ^= MC_FLAG_EXT_TOGGLE;                                    \ 
  } while (0) 
#define TOGGLE_EXT_FLAGS() do {                                             \ 
    mc_ext_toggle_bias ^= MC_FLAG_EXT_TOGGLE;                           \ 
  } while (0) 
 
struct mc_marker 
{ 
  struct mc_marker *hash_next;      /* Used by PTR_HASH_ALLOC. */ 
  struct mc_marker *dl_prev;        /* For the mc_incomplete, mc_complete and */ 
  struct mc_marker *dl_next;        /*   mc_indirect lists. Used iff not internal.*/ 
  void *thing;                      /* Referenced thing. */ 
  visit_thing_fn *visit_fn;     /* Visit function for it */ 
  void *extra;                      /*   and its extra data. */ 
  INT32 int_refs;           /* These refcounts are bogus */ 
  INT32 la_refs;            /*   for internal things. */ 
  unsigned INT32 queuepos;      /* Position in mc_work_queue, or 
                                 * MAX_UINT32 if not queued. */ 
  unsigned INT16 la_count;  /* Lookahead count. */ 
  unsigned INT16 flags; 
}; 
 
#undef BLOCK_ALLOC_NEXT 
#undef BLOCK_ALLOC_NEXT 
#define BLOCK_ALLOC_NEXT hash_next 
#undef PTR_HASH_ALLOC_DATA 
#define PTR_HASH_ALLOC_DATA thing 
#undef INIT_BLOCK 
#define INIT_BLOCK(f) 
#undef EXIT_BLOCK 
#define EXIT_BLOCK(f) 
 
PTR_HASH_ALLOC_FILL_PAGES (mc_marker, 2) 
 
static struct mc_marker *my_make_mc_marker (void *thing, 
                                            visit_thing_fn *visit_fn, 
                                            void *extra) 
{ 
  struct mc_marker *m = make_mc_marker (thing); 
  assert (thing); 
  assert (visit_fn); 
  m->thing = thing; 
  m->visit_fn = visit_fn; 
  m->extra = extra; 
  m->int_refs = m->la_refs = m->flags = 0; 
  INIT_CLEARED_EXTERNAL (m); 
  m->queuepos = MAX_UINT32; 
#ifdef PIKE_DEBUG 
  m->dl_prev = m->dl_next = (void *) (ptrdiff_t) -1; 
  m->la_count = ((unsigned INT16) -1) >> 1; 
#endif 
  return m; 
} 
 
#ifdef MEMORY_COUNT_DEBUG 
static void describe_mc_marker (struct mc_marker *m) 
{ 
  fprintf (stderr, "%s %p: refs %d, int %d, la %d, cnt %d", 
           get_name_of_type (type_from_visit_fn (m->visit_fn)), 
           m->thing, *(INT32 *) m->thing, m->int_refs, m->la_refs, m->la_count); 
  if (m->queuepos != MAX_UINT32) fprintf (stderr, ", wq %u", m->queuepos); 
  if (m->flags & MC_FLAG_INTERNAL) fputs (", I", stderr); 
  if (m->flags & MC_FLAG_INT_VISITED) fputs (", IV", stderr); 
  if (m->flags & MC_FLAG_LA_VISITED) fputs (", LAV", stderr); 
  if (m->flags & MC_FLAG_CANDIDATE) fputs (", C", stderr); 
  if (m->flags & MC_FLAG_CANDIDATE_REF) fputs (", CR", stderr); 
  if (m->flags & MC_FLAG_LA_COUNT_FIXED) fputs (", CF", stderr); 
  if (IS_EXTERNAL (m)) 
    fputs (m->flags & MC_FLAG_INTERNAL ? ", (E)" : ", E", stderr); 
} 
#endif 
 
/* Sentinel for the incomplete lookaheads list. */ 
static struct mc_marker mc_incomplete = { 
  (void *) (ptrdiff_t) -1, 
  &mc_incomplete, &mc_incomplete, 
  (void *) (ptrdiff_t) -1, (visit_thing_fn *) (ptrdiff_t) -1, 
  (void *) (ptrdiff_t) -1, 
  -1, -1, MAX_UINT32, 0, (unsigned INT16) -1 
}; 
 
/* Sentinel for the complete lookaheads list. The reason all complete 
 * things are tracked and not only the candidates is that elements 
 * then can be easily moved to mc_indirect (and back) without special 
 * cases when noncandidate complete things become indirectly 
 * incomplete. */ 
static struct mc_marker mc_complete = { 
  (void *) (ptrdiff_t) -1, 
  &mc_complete, &mc_complete, 
  (void *) (ptrdiff_t) -1, (visit_thing_fn *) (ptrdiff_t) -1, 
  (void *) (ptrdiff_t) -1, 
  -1, -1, MAX_UINT32, 0, (unsigned INT16) -1 
}; 
 
/* Sentinel for the indirectly incomplete lookaheads list. */ 
static struct mc_marker mc_indirect = { 
  (void *) (ptrdiff_t) -1, 
  &mc_indirect, &mc_indirect, 
  (void *) (ptrdiff_t) -1, (visit_thing_fn *) (ptrdiff_t) -1, 
  (void *) (ptrdiff_t) -1, 
  -1, -1, MAX_UINT32, 0, (unsigned INT16) -1 
}; 
 
#define DL_IS_EMPTY(LIST) (LIST.dl_next == &LIST) 
 
#define DL_ADD_LAST(LIST, M) do {                                     \ 
    struct mc_marker *_m = (M);                                         \ 
    struct mc_marker *_list_prev = LIST.dl_prev;                        \ 
    DO_IF_DEBUG (                                                       \ 
      assert (_m->dl_prev == (void *) (ptrdiff_t) -1);                  \ 
      assert (_m->dl_next == (void *) (ptrdiff_t) -1);                  \ 
    );                                                                  \ 
    _m->dl_prev = _list_prev;                                           \ 
    _m->dl_next = &LIST;                                                \ 
    LIST.dl_prev = _list_prev->dl_next = _m;                            \ 
  } while (0) 
 
#define DL_REMOVE(M) do {                                             \ 
    struct mc_marker *_m = (M);                                         \ 
    struct mc_marker *_list_prev = _m->dl_prev;                         \ 
    struct mc_marker *_list_next = _m->dl_next;                         \ 
    assert (_m->dl_prev != (void *) (ptrdiff_t) -1);                    \ 
    assert (_m->dl_next != (void *) (ptrdiff_t) -1);                    \ 
    _list_prev->dl_next = _list_next;                                   \ 
    _list_next->dl_prev = _list_prev;                                   \ 
    DO_IF_DEBUG (_m->dl_prev = _m->dl_next = (void *) (ptrdiff_t) -1);  \ 
  } while (0) 
 
#define DL_MOVE(FROM_LIST, TO_LIST) do {                              \ 
    if (FROM_LIST.dl_next != &FROM_LIST) {                              \ 
      struct mc_marker *to_list_last = TO_LIST.dl_prev;                 \ 
      TO_LIST.dl_prev = FROM_LIST.dl_prev;                              \ 
      to_list_last->dl_next = FROM_LIST.dl_next;                        \ 
      FROM_LIST.dl_prev->dl_next = &TO_LIST;                            \ 
      FROM_LIST.dl_next->dl_prev = to_list_last;                        \ 
      FROM_LIST.dl_prev = FROM_LIST.dl_next = &FROM_LIST;               \ 
    }                                                                   \ 
  } while (0) 
 
#define DL_MAKE_EMPTY(LIST) do {                                      \ 
    LIST.dl_prev = LIST.dl_next = &LIST;                                \ 
  } while (0) 
 
static struct mc_marker *mc_ref_from = (void *) (ptrdiff_t) -1; 
 
#ifdef MEMORY_COUNT_DEBUG 
static void MC_DEBUG_MSG (struct mc_marker *m, const char *msg) 
{ 
  switch (mc_pass) { 
    case MC_PASS_LOOKAHEAD: fputs ("LA ", stderr); break; 
    case MC_PASS_MARK_EXTERNAL: fputs ("ME ", stderr); break; 
  } 
  if (m) { 
    if (mc_ref_from != (void *) (ptrdiff_t) -1) fputs ("  [", stderr); 
    else fputs ("[", stderr); 
    describe_mc_marker (m); 
    fprintf (stderr, "] %s\n", msg); 
  } 
  else if (mc_ref_from != (void *) (ptrdiff_t) -1) { 
    fputs ("{", stderr); 
    describe_mc_marker (mc_ref_from); 
    fprintf (stderr, "} %s\n", msg); 
  } 
} 
#else 
#define MC_DEBUG_MSG(m, msg) do {} while (0) 
#endif 
 
/* The following is a standard binary heap priority queue implemented 
 * using an array. C.f. http://www.sbhatnagar.com/SourceCode/pqueue.html. */ 
 
/* Note: 1-based indexing is used in mc_work_queue to avoid 
 * off-by-ones in the binary arithmetic. */ 
static struct mc_marker **mc_work_queue = NULL; 
static unsigned INT32 mc_wq_size, mc_wq_used; 
 
#ifdef PIKE_DEBUG 
#define CHECK_WQ() if (d_flag) {                                    \ 
    unsigned i;                                                         \