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
  
/* 
|| 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. 
|| $Id: gc.c,v 1.230 2003/09/09 14:30:17 mast Exp $ 
*/ 
 
#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 <math.h> 
 
#include "block_alloc.h" 
 
RCSID("$Id: gc.c,v 1.230 2003/09/09 14:30:17 mast Exp $"); 
 
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; 
 
/* 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; 
 
#define GC_LINK_CHUNK_SIZE 64 
 
/* The gc will free all things with no external references that isn't 
 * referenced by undestructed objects with destroy() lfuns (known as 
 * "live" objects). 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 
 *    A is destructed before B. 
 * 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. 
 */ 
 
/* #define GC_VERBOSE */ 
/* #define GC_CYCLE_DEBUG */ 
 
/* #define GC_STACK_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 = 3;                /* Account for *_empty_array. */ 
int num_allocs =0; 
ptrdiff_t 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; 
 
struct gc_frame 
{ 
  struct gc_frame *back;    /* Previous stack frame. */ 
  void *data; 
  union { 
    struct {                        /* Pop frame. */ 
      struct gc_frame *prev;        /* Previous frame in rec_list. */ 
      struct gc_frame *next;        /* Next pointer in rec_list and kill_list. */ 
      unsigned INT16 cycle; /* Cycle id number. */ 
    } pop; 
    struct {                        /* Link frame. */ 
      gc_cycle_check_cb *checkfn; 
      int weak; 
    } link; 
    int free_extra_type;    /* Used on free_extra_list. The type of the thing. */ 
  } u; 
  unsigned INT16 frameflags; 
}; 
 
#define GC_POP_FRAME          0x01 
#define GC_WEAK_REF         0x02 
#define GC_STRONG_REF               0x04 
#define GC_OFF_STACK                0x08 
#define GC_ON_KILL_LIST             0x10 
#ifdef PIKE_DEBUG 
#define GC_LINK_FREED               0x20 
#define GC_FOLLOWED_NONSTRONG       0x40 
#endif 
 
#undef BLOCK_ALLOC_NEXT 
#define BLOCK_ALLOC_NEXT back 
 
BLOCK_ALLOC(gc_frame,GC_LINK_CHUNK_SIZE) 
 
#define PREV(frame) ((frame)->u.pop.prev) 
#define NEXT(frame) ((frame)->u.pop.next) 
#define CYCLE(frame) ((frame)->u.pop.cycle) 
 
#ifdef PIKE_DEBUG 
#define CHECK_POP_FRAME(frame) do {                                 \ 
  if ((frame)->frameflags & GC_LINK_FREED)                              \ 
    gc_fatal((frame)->data, 0, "Accessing freed gc_frame.\n");          \ 
  if (!((frame)->frameflags & GC_POP_FRAME))                            \ 
    gc_fatal((frame)->data, 0, #frame " is not a pop frame.\n");        \ 
  if (NEXT(PREV(frame)) != (frame))                                     \ 
    gc_fatal((frame)->data, 0,                                          \ 
             "Pop frame pointers are inconsistent.\n");                 \ 
} while (0) 
#else 
#define CHECK_POP_FRAME(frame) do {} while (0) 
#endif 
 
static struct gc_frame rec_list = {0, 0, {{0, 0, 0}}, GC_POP_FRAME}; 
static struct gc_frame *gc_rec_last = &rec_list, *gc_rec_top = 0; 
static struct gc_frame *kill_list = 0; 
static struct gc_frame *free_extra_list = 0; /* See note in gc_delayed_free. */ 
 
static unsigned last_cycle; 
size_t gc_ext_weak_refs; 
 
/* gc_frame objects are used as frames in a recursion stack during the 
 * cycle check pass. gc_rec_top points to the current top of the 
 * stack. When a thing is recursed, a pop frame is first pushed on the 
 * stack and then the gc_cycle_check_* function fills in with link 
 * frames for every reference the thing contains. 
 * 
 * rec_list is a double linked list of the pop frames on the stack, 
 * and that list represents the current prospective destruct order. 
 * gc_rec_last points at the last frame in the list and new frames are 
 * linked in after it. A cycle is always treated as one atomic unit, 
 * i.e. it's either popped whole or not at all. That means that 
 * rec_list might contain frames that are no longer on the stack. 
 * 
 * A range of frames which always ends at the end of the list may be 
 * rotated a number of slots to break a cyclic reference at a chosen 
 * point. The stack of link frames are rotated simultaneously. 
 * 
 * Frames for live objects are linked into the beginning of kill_list 
 * when they're popped from rec_list. 
 * 
 * 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. rec_list is not touched at all in this extra round. 
 */ 
 
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_time = 0; 
#if CPU_TIME_IS_THREAD_LOCAL == PIKE_NO 
cpu_time_t auto_gc_time = 0; 
#endif 
 
/* 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 
} last_garbage_strategy = GARBAGE_RATIO_LOW; 
 
struct callback_list gc_callbacks; 
 
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(void *a); 
 
#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 
 
#ifdef PIKE_DEBUG 
#undef get_marker 
#define get_marker debug_get_marker 
#undef find_marker 
#define find_marker debug_find_marker 
#endif 
 
PTR_HASH_ALLOC_FIXED_FILL_PAGES(marker,2) 
 
#if defined (PIKE_DEBUG) || defined (GC_MARK_DEBUG) 
void *gc_found_in = NULL; 
int gc_found_in_type = PIKE_T_UNKNOWN; 
const char *gc_found_place = NULL; 
#endif 
 
#ifdef PIKE_DEBUG 
 
#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))) 
 
int gc_in_cycle_check = 0; 
static unsigned delayed_freed, weak_freed, checked, marked, cycle_checked, live_ref; 
static unsigned max_gc_frames, num_gc_frames = 0, live_rec, frame_rot; 
static unsigned gc_extra_refs = 0; 
 
static unsigned max_tot_gc_frames = 0; 
static unsigned tot_cycle_checked = 0, tot_live_rec = 0, tot_frame_rot = 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; 
 
  a=&empty_array; 
  do 
  { 
    if(a==(struct array *)something) return T_ARRAY; 
    a=a->next; 
  }while(a!=&empty_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; 
#ifdef PIKE_NEW_MULTISETS 
    else if (mu->msd == (struct multiset_data *) something) 
      return T_MULTISET_DATA; 
#endif 
 
  if(safe_debug_findstring((struct pike_string *)something)) 
    return T_STRING; 
 
  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; 
char *fatal_after_gc=0; 
 
#ifdef DEBUG_MALLOC 
 
/* To keep the markers after the gc. Only used for the dmalloc leak 
 * report at exit. */ 
int gc_keep_markers = 0; 
 
int gc_external_refs_zapped = 0; 
 
#endif 
 
#define DESCRIBE_MEM 1 
#define DESCRIBE_NO_REFS 2 
#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); 
        fprintf(stderr,"%*s  **In p->constants[%"PRINTPTRDIFFT"d] (%s)\n",indent,"", 
                e, p->constants[e].name ? p->constants[e].name->str : "no name"); 
        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) 
              fprintf(stderr," (%s)",tmp.name->str); 
            fprintf(stderr,"\n"); 
          } 
              
        } 
      } 
      break; 
    } 
 
    case T_STORAGE: 
      fprintf(stderr, "%*s  **In storage of object\n", indent, ""); 
      break; 
 
#ifdef PIKE_NEW_MULTISETS 
    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, ""); 
          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, ""); 
          print_svalue(stderr, low_use_multiset_index (node, ind)); 
          fputc('\n', stderr); 
          break; 
        } 
      } 
      break; 
    } 
 
#else  /* PIKE_NEW_MULTISETS */ 
    case T_MULTISET: 
      descblock = ((struct multiset *) memblock)->ind; 
      /* FALL THROUGH */ 
#endif 
 
    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, ""); 
          print_svalue(stderr, &k->ind); 
          fputc('\n', stderr); 
          break; 
        } 
        else if (&k->val == (struct svalue *) location) { 
          fprintf(stderr, "%*s  **In value with index ", indent, ""); 
          print_svalue(stderr, &k->ind); 
          fputc('\n', stderr); 
          break; 
        } 
      break; 
    } 
  } 
 
  if(memblock && depth>0) 
    describe_something(memblock,type,indent+2,depth-1,flags | DESCRIBE_MEM,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 
} 
 
static void describe_gc_frame(struct gc_frame *l) 
{ 
  if (l->frameflags & GC_POP_FRAME) 
    fprintf(stderr, "back=%p, prev=%p, next=%p, data=%p, cycle=%u, flags=0x%02x", 
            l->back, PREV(l), NEXT(l), l->data, CYCLE(l), l->frameflags); 
  else 
    fprintf(stderr, "LINK back=%p, data=%p, weak=%d, flags=0x%02x", 
            l->back, l->data, l->u.link.weak, l->frameflags); 
} 
 
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); 
#ifdef PIKE_DEBUG 
    if (m->frame) { 
      fputs(" [", stderr); 
      describe_gc_frame(m->frame); 
      putc(']', stderr); 
    } 
#endif 
    putc('\n', stderr); 
  } 
  else 
    fprintf(stderr, "no marker\n"); 
} 
 
#endif /* PIKE_DEBUG */ 
 
void debug_gc_fatal(void *a, int flags, const char *fmt, ...) 
{ 
#ifdef PIKE_DEBUG 
  struct marker *m; 
#endif 
  int orig_gc_pass = Pike_in_gc; 
  va_list args; 
 
  va_start(args, fmt); 
 
  fprintf(stderr, "**"); 
  (void) VFPRINTF(stderr, fmt, args); 
 
  va_end(args); 
 
#ifdef PIKE_DEBUG 
  if (a) { 
    /* Temporarily jumping out of gc to avoid being catched in debug 
     * checks in describe(). */ 
    Pike_in_gc = 0; 
    describe(a); 
   
    if (flags & 1) locate_references(a); 
 
    m=find_marker(a); 
    if(m) 
    { 
      fprintf(stderr,"** Describing marker for this thing.\n"); 
      describe(m); 
    }else{ 
      fprintf(stderr,"** No marker found for this thing.\n"); 
    } 
    Pike_in_gc = orig_gc_pass; 
  } 
 
  if (flags & 2) 
    fatal_after_gc = "Fatal in garbage collector.\n"; 
  else 
#endif 
    debug_fatal("Fatal in garbage collector.\n"); 
} 
 
#ifdef PIKE_DEBUG 
 
static void gdb_gc_stop_here(void *a, int weak) 
{ 
  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,0); 
    else { 
      fputc('\n', stderr); 
      describe_something(gc_found_in, gc_found_in_type, 0, 0, DESCRIBE_MEM, 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,""); 
      } 
 
      if(!p) 
      { 
        fprintf(stderr,"%*s**The object is destructed.\n",indent,""); 
        p=id_to_program(((struct object *)a)->program_id); 
      } 
 
      if (p) { 
        size_t inh_idx, var_idx, var_count = 0; 
 
        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, ""); 
              push_string (inh->name); 
              print_svalue (stderr, --Pike_sp); 
              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]; 
            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) { 
              push_string (id->name); 
              print_svalue (stderr, --Pike_sp); 
            } 
            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) 
              push_svalue ((struct svalue *) ptr); 
            else 
              assign_from_short_svalue_no_free ( 
                Pike_sp++, (union anything *) ptr, id->run_time_type); 
            print_svalue (stderr, Pike_sp - 1); 
            pop_stack(); 
 
            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 ((int) p == 0x55555555) 
          fprintf(stderr, "%*s**Zapped program pointer.\n", indent, ""); 
        else 
#endif 
        low_describe_something(p, T_PROGRAM, indent, depth, flags, 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_NO_REFS ) 
                                & ~ (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; 
      INT32 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); 
      } 
 
      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, ""); 
            push_string (inh->name); 
            print_svalue (stderr, --Pike_sp); 
            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, ""); 
            push_string (inh->name); 
            print_svalue (stderr, --Pike_sp); 
            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 (id_ref->id_flags & ID_HIDDEN || 
            (id_ref->id_flags & (ID_INHERITED|ID_PRIVATE)) == 
            (ID_INHERITED|ID_PRIVATE)) continue; 
 
        id_inh = INHERIT_FROM_PTR (p, id_ref); 
        id = id_inh->prog->identifiers + id_ref->identifier_offset; 
 
        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_ALIAS (id->identifier_flags))    type = "alias"; 
        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_STATIC)) strcat (prot, ",!sta"); 
        } 
        else 
          if (id_ref->id_flags & ID_STATIC) strcat (prot, ",sta"); 
        if (id_ref->id_flags & ID_NOMASK)    strcat (prot, ",nom"); 
        if (id_ref->id_flags & ID_PUBLIC)    strcat (prot, ",pub"); 
        if (id_ref->id_flags & ID_PROTECTED) strcat (prot, ",pro"); 
        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_EXTERN)    strcat (prot, ",ext"); 
        if (id_ref->id_flags & ID_VARIANT)   strcat (prot, ",var"); 
        if (id_ref->id_flags & ID_ALIAS)     strcat (prot, ",ali"); 
 
        sprintf (descr, "%s: %s", type, prot + 1); 
        fprintf (stderr, "%*s**%*s%-18s name: ", 
                 indent, "", id_inh->inherit_level + 1, "", descr); 
 
        if (id->name->size_shift) { 
          push_string (id->name); 
          print_svalue (stderr, --Pike_sp); 
        } 
        else 
          fprintf (stderr, "%-20s", id->name->str); 
 
        if (id->identifier_flags & IDENTIFIER_C_FUNCTION) 
          fprintf (stderr, "  addr: %p", id->func.c_fun); 
        else if (IDENTIFIER_IS_VARIABLE (id->identifier_flags)) 
          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)) { 
          fputs ("  value: ", stderr); 
          print_svalue (stderr, &id_inh->prog->constants[id->func.offset].sval); 
        } 
 
        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, p->PIKE_CONCAT(num_,NAME)); 
#include "program_areas.h" 
      } 
 
      break; 
    } 
 
#ifdef PIKE_NEW_MULTISETS 
    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: 
      debug_dump_multiset((struct multiset *) a); 
      break; 
 
#else  /* PIKE_NEW_MULTISETS */ 
    case T_MULTISET: 
      fprintf(stderr,"%*s**Describing array %p of multiset:\n", 
              indent,"", ((struct multiset *)a)->ind); 
      debug_dump_array(((struct multiset *)a)->ind); 
      break; 
#endif 
 
    case T_ARRAY: 
      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: 
      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->len, s->size_shift, s->hval); 
      if (!s->size_shift) { 
        if(s->len>77) 
        { 
          fprintf(stderr,"%*s** \"%60s\"...\n",indent,"",s->str); 
        }else{ 
          fprintf(stderr,"%*s** \"%s\"\n",indent,"",s->str); 
        } 
      } 
      break; 
    } 
 
    case T_PIKE_FRAME: { 
      struct pike_frame *f = (struct pike_frame *) a; 
      do { 
        if (f->current_object) { 
          struct program *p = f->current_object->prog; 
          if (p) { 
            struct identifier *id = ID_FROM_INT(p, f->fun); 
            INT32 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); 
          } 
          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 unknown type %d\n", 
              indent, "", 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) 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 (((int)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(s->type) 
  { 
    case T_INT: 
      fprintf(stderr,"    %"PRINTPIKEINT"d\n",s->u.integer); 
      break; 
 
    case T_FLOAT: 
      fprintf(stderr,"    %f\n",s->u.float_number); 
      break; 
 
    case T_FUNCTION: 
      if(s->subtype == 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 (destructed) name: %s\n",ID_FROM_INT(p,s->subtype)->name->str); 
          }else{ 
            fprintf(stderr,"    Function in destructed object.\n"); 
          } 
        }else{ 
          fprintf(stderr,"    Function name: %s\n",ID_FROM_INT(s->u.object->prog,s->subtype)->name->str); 
        } 
      } 
  } 
  describe_something(s->u.refs,s->type,0,0,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); 
} 
 
#endif /* PIKE_DEBUG */ 
 
#ifndef GC_MARK_DEBUG 
struct pike_queue gc_mark_queue; 
#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; 
 
void gc_mark_run_queue() 
{ 
  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() 
{ 
  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 (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. */ 
      fprintf(stderr, "## Watched thing %p found in " 
              "gc_mark_enqueue() in pass %d.\n", data, Pike_in_gc); 
    } 
  } 
#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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_touch() in pass %d.\n", a, Pike_in_gc); 
  } 
#endif 
 
  if (!a) Pike_fatal("Got null pointer.\n"); 
 
  switch (Pike_in_gc) { 
    case GC_PASS_PRETOUCH: 
      m = find_marker(a); 
      if (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_MIDDLETOUCH: { 
#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"); 
#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_MIDDLETOUCHED; 
      break; 
    } 
 
    case GC_PASS_POSTTOUCH: 
      m = find_marker(a); 
      if (!*(INT32 *) a) 
        gc_fatal(a, 1, "Found a thing without refs.\n"); 
      if (m) { 
        if (!(m->flags & (GC_PRETOUCHED|GC_MIDDLETOUCHED))) 
          gc_fatal(a, 2, "An existing but untouched marker found " 
                   "for object in linked lists.\n"); 
        else if (m->flags & GC_LIVE_RECURSE || 
                 (m->frame && m->frame->frameflags & (GC_WEAK_REF|GC_STRONG_REF))) 
          gc_fatal(a, 2, "Thing still got flag from recurse list.\n"); 
#ifdef PIKE_DEBUG 
      else if (m->flags & GC_MARKED) 
          return; 
        else if (!(m->flags & GC_NOT_REFERENCED) || m->flags & GC_XREFERENCED) 
          gc_fatal(a, 3, "A thing with external references " 
                   "got missed by mark pass.\n"); 
        else if (!(m->flags & GC_CYCLE_CHECKED)) 
          gc_fatal(a, 2, "A thing was missed by " 
                   "both mark and cycle check pass.\n"); 
        else if (!(m->flags & GC_IS_REFERENCED)) 
          gc_fatal(a, 2, "An unreferenced thing " 
                   "got missed by gc_is_referenced().\n"); 
        else if (!(m->flags & GC_DO_FREE)) 
          gc_fatal(a, 2, "An unreferenced thing " 
                   "got missed by gc_do_free().\n"); 
        else if (m->flags & GC_GOT_EXTRA_REF) 
          gc_fatal(a, 2, "A thing still got an extra ref.\n"); 
        else if (m->weak_refs > m->saved_refs) 
          gc_fatal(a, 2, "A thing got more weak references than references.\n"); 
        else if (!(m->flags & GC_LIVE)) { 
          if (m->weak_refs < 0) 
            gc_fatal(a, 3, "A thing which had only weak references is " 
                     "still around after gc.\n"); 
          else 
            gc_fatal(a, 3, "A thing to garb is still around.\n"); 
        } 
#endif 
      } 
      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 than refs.\n"); 
  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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_check() in pass %d.\n", a, Pike_in_gc); 
  } 
  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; 
} 
 
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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_check_weak() in pass %d.\n", a, Pike_in_gc); 
  } 
  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; 
  struct marker *h; 
  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 
#ifdef DEBUG_MALLOC 
    /* The marker hash table is left around after a previous gc if 
     * gc_keep_markers is set. */ 
    if (marker_hash_table) cleanup_markers(); 
#endif 
 
    low_init_marker_hash(num_objects); 
    get_marker(rec_list.data);  /* Used to simplify fencepost conditions. */ 
#ifdef PIKE_DEBUG 
  } 
#endif 
} 
 
static void exit_gc(void) 
{ 
#ifdef DEBUG_MALLOC 
  if (!gc_keep_markers) 
#endif 
    cleanup_markers(); 
 
  free_all_gc_frame_blocks(); 
 
#ifdef GC_VERBOSE 
  num_gc_frames = 0; 
#endif 
 
#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 
void locate_references(void *a) 
{ 
  int tmp, orig_in_gc = Pike_in_gc; 
  int i=0; 
  if(!marker_blocks) 
  { 
    i=1; 
    init_gc(); 
  } 
  Pike_in_gc = GC_PASS_LOCATE; 
 
  /* 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; 
 
  gc_check_all_arrays(); 
  gc_check_all_multisets(); 
  gc_check_all_mappings(); 
  gc_check_all_programs(); 
  gc_check_all_objects(); 
 
#ifdef PIKE_DEBUG 
  if(master_object) 
    gc_mark_external (master_object, " as master_object"); 
  { 
    extern struct mapping *builtin_constants; 
    if(builtin_constants) 
      gc_mark_external (builtin_constants, " as builtin_constants"); 
  } 
#endif 
   
  call_callback(& gc_callbacks, NULL); 
 
#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.\n", a); 
 
  Pike_in_gc = orig_in_gc; 
  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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_add_extra_ref() in pass %d.\n", a, Pike_in_gc); 
  } 
 
  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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_free_extra_ref() in pass %d.\n", a, Pike_in_gc); 
  } 
 
  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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_is_referenced() in pass %d.\n", a, Pike_in_gc); 
  } 
 
  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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_mark_external() in pass %d.\n", a, Pike_in_gc); 
  } 
 
  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; 
} 
 
void debug_really_free_gc_frame(struct gc_frame *l) 
{ 
  if (l->frameflags & GC_LINK_FREED) 
    gc_fatal(l->data, 0, "Freeing freed gc_frame.\n"); 
  l->frameflags |= GC_LINK_FREED; 
  l->back = PREV(l) = NEXT(l) = (struct gc_frame *)(ptrdiff_t) -1; 
  really_free_gc_frame(l); 
#ifdef GC_VERBOSE 
  num_gc_frames--; 
#endif 
} 
 
#else  /* PIKE_DEBUG */ 
 
#define debug_really_free_gc_frame(l) really_free_gc_frame(l) 
 
#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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_do_weak_free() in pass %d.\n", a, Pike_in_gc); 
  } 
  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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_delayed_free() in pass %d.\n", a, Pike_in_gc); 
  } 
  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 gc_frame *l = alloc_gc_frame(); 
    l->data = a; 
    l->u.free_extra_type = type; 
    l->back = free_extra_list; 
    l->frameflags = 0; 
    free_extra_list = l; 
  } 
 
  gc_add_extra_ref(a); 
  m->flags |= GC_GOT_DEAD_REF; 
} 
 
int gc_mark(void *a) 
{ 
  struct marker *m = get_marker(debug_malloc_pass(a)); 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && m && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_mark() in pass %d.\n", a, Pike_in_gc); 
  } 
  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(a, 0, "Marked a thing without refs.\n"); 
  if (m->weak_refs < 0) 
    gc_fatal(a, 0, "Marking 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 non-weak refs. */ 
#ifdef PIKE_DEBUG 
    if (!(m->flags & GC_MARKED)) 
      gc_fatal(a, 0, "gc_mark() called for thing in zap weak pass " 
               "that wasn't marked before.\n"); 
#endif 
    if (m->flags & GC_FREE_VISITED) 
      return 0; 
    else { 
      m->flags |= GC_FREE_VISITED; 
      return 1; 
    } 
  } 
 
  else if (m->flags & GC_MARKED) { 
#ifdef PIKE_DEBUG 
    if (m->weak_refs != 0) 
      gc_fatal(a, 0, "weak_refs changed in marker already visited by gc_mark().\n"); 
#endif 
    return 0; 
  } 
  else { 
    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; 
  } 
} 
 
PMOD_EXPORT void gc_cycle_enqueue(gc_cycle_check_cb *checkfn, void *data, int weak) 
{ 
  struct gc_frame *l = alloc_gc_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. */ 
      fprintf(stderr, "## Watched thing %p found in " 
              "gc_cycle_enqueue() in pass %d.\n", data, Pike_in_gc); 
    } 
  } 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    gc_fatal(data, 0, "Use of the gc frame stack outside the cycle check pass.\n"); 
#endif 
#ifdef GC_VERBOSE 
  if (++num_gc_frames > max_gc_frames) max_gc_frames = num_gc_frames; 
#endif 
  l->data = data; 
  l->u.link.checkfn = checkfn; 
  l->u.link.weak = weak; 
  l->frameflags = 0; 
  l->back = gc_rec_top; 
#ifdef GC_STACK_DEBUG 
  fprintf(stderr, "enqueue %p [%p]: ", l, gc_rec_top); 
  describe_gc_frame(l); 
  fputc('\n', stderr); 
#endif 
  gc_rec_top = l; 
} 
 
static struct gc_frame *gc_cycle_enqueue_pop(void *data) 
{ 
  struct gc_frame *l = alloc_gc_frame(); 
#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"); 
#endif 
#ifdef GC_VERBOSE 
  if (++num_gc_frames > max_gc_frames) max_gc_frames = num_gc_frames; 
#endif 
  l->data = data; 
  PREV(l) = gc_rec_last; 
  NEXT(l) = 0; 
  CYCLE(l) = 0; 
  l->frameflags = GC_POP_FRAME; 
  l->back = gc_rec_top; 
#ifdef GC_STACK_DEBUG 
  fprintf(stderr, "enqueue %p [%p]: ", l, gc_rec_top); 
  describe_gc_frame(l); 
  fputc('\n', stderr); 
#endif 
  gc_rec_top = l; 
  return l; 
} 
 
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 (gc_rec_top) { 
#ifdef GC_STACK_DEBUG 
    fprintf(stderr, "dequeue %p [%p]: ", gc_rec_top, gc_rec_top->back); 
    describe_gc_frame(gc_rec_top); 
    fputc('\n', stderr); 
#endif 
    if (gc_rec_top->frameflags & GC_POP_FRAME) { 
      struct gc_frame *l = gc_rec_top->back; 
      gc_cycle_pop(gc_rec_top->data); 
      gc_rec_top = l; 
    } else { 
      struct gc_frame l = *gc_rec_top; 
#ifdef PIKE_DEBUG 
      if (l.frameflags & GC_LINK_FREED) 
        gc_fatal(l.data, 0, "Accessing freed gc_frame.\n"); 
#endif 
      debug_really_free_gc_frame(gc_rec_top); 
      gc_rec_top = l.back; 
      l.u.link.checkfn(l.data, l.u.link.weak); 
    } 
  } 
} 
 
#ifdef GC_CYCLE_DEBUG 
static int gc_cycle_indent = 0; 
#define CYCLE_DEBUG_MSG(M, TXT) do {                                  \ 
  fprintf(stderr, "%*s%-35s %p [%p] ", gc_cycle_indent, "",             \ 
          (TXT), (M) ? (M)->data : 0, gc_rec_last->data);               \ 
  describe_marker(M);                                                   \ 
} while (0) 
#else 
#define CYCLE_DEBUG_MSG(M, TXT) do {} while (0) 
#endif 
 
static void rotate_rec_list (struct gc_frame *beg, struct gc_frame *pos) 
/* Rotates the marker list and the cycle stack so the bit from pos 
 * down to the end gets before the bit from beg down to pos. The beg 
 * pos might be moved further down the stack to avoid mixing cycles or 
 * breaking strong link sequences. */ 
{ 
#ifdef GC_STACK_DEBUG 
  struct gc_frame *l; 
#endif 
  CYCLE_DEBUG_MSG(find_marker(beg->data), "> rotate_rec_list, asked to begin at"); 
 
#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_POP_FRAME(beg); 
  CHECK_POP_FRAME(pos); 
  if (beg == pos) 
    gc_fatal(beg->data, 0, "Cycle already broken at requested position.\n"); 
  if (NEXT(gc_rec_last)) 
    gc_fatal(gc_rec_last->data, 0, "gc_rec_last not at end.\n"); 
#endif 
 
#ifdef GC_STACK_DEBUG 
  fprintf(stderr,"Stack before:\n"); 
  for (l = gc_rec_top; l; l = l->back) { 
    fprintf(stderr, "  %p ", l); 
    describe_gc_frame(l); 
    fputc('\n', stderr); 
  } 
#endif 
 
#if 0 
  if (CYCLE(beg)) { 
    for (l = beg; CYCLE(PREV(l)) == CYCLE(beg); l = PREV(l)) 
      CHECK_POP_FRAME(l); 
    if (CYCLE(l) == CYCLE(pos)) { 
      /* Breaking something previously marked as a cycle. Clear it 
       * since we're no longer sure it's an unambiguous cycle. */ 
      unsigned cycle = CYCLE(l); 
      for (; l && CYCLE(l) == cycle; l = NEXT(l)) { 
        CHECK_POP_FRAME(l); 
#ifdef GC_CYCLE_DEBUG 
      if (CYCLE(l)) 
          CYCLE_DEBUG_MSG(find_marker(l->data), "> rotate_rec_list, clear cycle"); 
#endif 
      CYCLE(l) = 0; 
      } 
    } 
    else beg = l;           /* Keep the cycle continuous. */ 
  } 
#endif 
 
  /* Always keep chains of strong refs continuous, or else we risk 
   * breaking the order in a later rotation. */ 
  for (; beg->frameflags & GC_STRONG_REF; beg = PREV(beg)) {} 
 
  CYCLE_DEBUG_MSG(find_marker(beg->data), "> rotate_rec_list, begin at"); 
 
  { 
    struct gc_frame *b = beg, *p = pos, *old_rec_top; 
    while (b->frameflags & GC_OFF_STACK) { 
      if ((b = NEXT(b)) == pos) goto done; 
      CHECK_POP_FRAME(b); 
      DO_IF_DEBUG(frame_rot++); 
    } 
    while (p->frameflags & GC_OFF_STACK) { 
      if (!(p = NEXT(p))) goto done; 
      CHECK_POP_FRAME(p); 
      DO_IF_DEBUG(frame_rot++); 
    } 
    old_rec_top = gc_rec_top; 
    gc_rec_top = p->back; 
    p->back = b->back; 
    b->back = old_rec_top; 
  } 
done: 
  DO_IF_DEBUG(frame_rot++); 
 
  { 
    struct gc_frame *new_rec_last = PREV(pos); 
    NEXT(PREV(beg)) = pos; 
    PREV(pos) = PREV(beg); 
    NEXT(gc_rec_last) = beg; 
    PREV(beg) = gc_rec_last; 
    gc_rec_last = new_rec_last; 
    NEXT(gc_rec_last) = 0; 
  } 
 
  if (beg->frameflags & GC_WEAK_REF) { 
    beg->frameflags &= ~GC_WEAK_REF; 
    pos->frameflags |= GC_WEAK_REF; 
    CYCLE_DEBUG_MSG(get_marker(pos->data), "> rotate_rec_list, moved weak flag"); 
  } 
 
#ifdef GC_STACK_DEBUG 
  fprintf(stderr,"Stack after:\n"); 
  for (l = gc_rec_top; l; l = l->back) { 
    fprintf(stderr, "  %p ", l); 
    describe_gc_frame(l); 
    fputc('\n', stderr); 
  } 
#endif 
} 
 
int gc_cycle_push(void *x, struct marker *m, int weak) 
{ 
  struct marker *last = find_marker(gc_rec_last->data); 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && m && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_cycle_push() in pass %d.\n", x, Pike_in_gc); 
  } 
 
  debug_malloc_touch(x); 
 
  if (!x) Pike_fatal("Got null pointer.\n"); 
  if (m->data != x) 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(x, 0, "gc_cycle_push() called for untouched thing.\n"); 
  if ((!(m->flags & GC_NOT_REFERENCED) || m->flags & GC_MARKED) && 
      *(INT32 *) x) 
    gc_fatal(x, 1, "Got a referenced marker to gc_cycle_push.\n"); 
  if (m->flags & GC_XREFERENCED) 
    gc_fatal(x, 1, "Doing cycle check in externally referenced thing " 
             "missed in mark pass.\n"); 
  if (weak && gc_rec_last == &rec_list) 
    gc_fatal(x, 1, "weak is %d when on top of stack.\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 != &empty_array; a = a->next) 
      if(a == (struct array *) x) goto on_gc_internal_lists; 
    for(o = gc_internal_object; o; o = o->next) 
      if(o == (struct object *) x) goto on_gc_internal_lists; 
    for(p = gc_internal_program; p; p = p->next) 
      if(p == (struct program *) x) goto on_gc_internal_lists; 
    for(m = gc_internal_mapping; m; m = m->next) 
      if(m == (struct mapping *) x) goto on_gc_internal_lists; 
    for(l = gc_internal_multiset; l; l = l->next) 
      if(l == (struct multiset *) x) goto on_gc_internal_lists; 
    gc_fatal(x, 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 (last->flags & GC_LIVE_RECURSE) { 
#ifdef PIKE_DEBUG 
    if (!(last->flags & GC_LIVE)) 
      gc_fatal(x, 0, "Doing live recursion from a dead thing.\n"); 
#endif 
 
    if (m->flags & GC_CYCLE_CHECKED) { 
      if (!(m->flags & GC_LIVE)) { 
        /* Only recurse through things already handled; we'll get to the 
         * other later in the normal recursion. */ 
#ifdef PIKE_DEBUG 
      if (m->flags & GC_LIVE_RECURSE) 
          gc_fatal(x, 0, "Mark live recursion attempted twice into thing.\n"); 
#endif 
      goto live_recurse; 
      } 
      CYCLE_DEBUG_MSG(m, "gc_cycle_push, no live recurse"); 
    } 
 
    else { 
      /* We'll get here eventually in the normal recursion. Pop off 
       * the remaining live recurse frames for the last thing. */ 
      CYCLE_DEBUG_MSG(m, "gc_cycle_push, no live recurse"); 
      last->flags &= ~GC_LIVE_RECURSE; 
      while (1) { 
        struct gc_frame *l = gc_rec_top; 
#ifdef PIKE_DEBUG 
      if (!gc_rec_top) 
          Pike_fatal("Expected a gc_cycle_pop entry in gc_rec_top.\n"); 
#endif 
      gc_rec_top = l->back; 
        if (l->frameflags & GC_POP_FRAME) { 
          gc_rec_last = PREV(l); 
          debug_really_free_gc_frame(l); 
          break; 
        } 
        debug_really_free_gc_frame(l); 
      } 
#ifdef GC_CYCLE_DEBUG 
      gc_cycle_indent -= 2; 
      CYCLE_DEBUG_MSG(m, "> gc_cycle_push, unwound live rec"); 
#endif 
    } 
 
    return 0; 
  } 
 
#ifdef PIKE_DEBUG 
  if (weak < 0 && gc_rec_last->frameflags & GC_FOLLOWED_NONSTRONG) 
    gc_fatal(x, 0, "Followed strong link too late.\n"); 
  if (weak >= 0) gc_rec_last->frameflags |= GC_FOLLOWED_NONSTRONG; 
#endif 
 
  if (m->frame && !(m->frame->frameflags & GC_ON_KILL_LIST)) { 
    /* A cyclic reference is found. */ 
#ifdef PIKE_DEBUG 
    if (gc_rec_last == &rec_list) 
      gc_fatal(x, 0, "Cyclic ref involves dummy rec_list marker.\n"); 
    CHECK_POP_FRAME(gc_rec_last); 
    CHECK_POP_FRAME(m->frame); 
#endif 
 
    if (m != last) { 
      struct gc_frame *p, *weak_ref = 0, *nonstrong_ref = 0; 
      if (!weak) { 
        struct gc_frame *q; 
        CYCLE_DEBUG_MSG(m, "gc_cycle_push, search normal"); 
        /* Find the last weakly linked thing and the one before the 
         * first strongly linked thing. */ 
        for (q = m->frame, p = NEXT(q);; q = p, p = NEXT(p)) { 
          CHECK_POP_FRAME(p); 
          if (p->frameflags & (GC_WEAK_REF|GC_STRONG_REF)) { 
            if (p->frameflags & GC_WEAK_REF) weak_ref = p; 
            else if (!nonstrong_ref) nonstrong_ref = q; 
          } 
          if (p == gc_rec_last) break; 
        } 
      } 
 
      else if (weak < 0) { 
        CYCLE_DEBUG_MSG(m, "gc_cycle_push, search strong"); 
        /* Find the last weakly linked thing and the last one which 
         * isn't strongly linked. */ 
        for (p = NEXT(m->frame);; p = NEXT(p)) { 
          CHECK_POP_FRAME(p); 
          if (p->frameflags & GC_WEAK_REF) weak_ref = p; 
          if (!(p->frameflags & GC_STRONG_REF)) nonstrong_ref = p; 
          if (p == gc_rec_last) break; 
        } 
#ifdef PIKE_DEBUG 
      if (p == gc_rec_last && !nonstrong_ref) { 
          fprintf(stderr, "Only strong links in cycle:\n"); 
          for (p = m->frame;; p = NEXT(p)) { 
            describe(p->data); 
            locate_references(p->data); 
            if (p == gc_rec_last) break; 
            fprintf(stderr, "========= next =========\n"); 
          } 
          gc_fatal(0, 0, "Only strong links in cycle.\n"); 
        } 
#endif 
      } 
 
      else { 
        struct gc_frame *q; 
        CYCLE_DEBUG_MSG(m, "gc_cycle_push, search weak"); 
        /* Find the thing before the first strongly linked one. */ 
        for (q = m->frame, p = NEXT(q);; q = p, p = NEXT(p)) { 
          CHECK_POP_FRAME(p); 
          if (!(p->frameflags & GC_WEAK_REF) && !nonstrong_ref) 
            nonstrong_ref = q; 
          if (p == gc_rec_last) break; 
        } 
      } 
 
      if (weak_ref) { 
        /* 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 ensure that a sequence of several weak links 
         * are broken at the last one). */ 
        CYCLE_DEBUG_MSG(find_marker(weak_ref->data), 
                        "gc_cycle_push, weak break"); 
        rotate_rec_list(m->frame, weak_ref); 
      } 
 
      else if (weak < 0) { 
        /* The backward link is strong. Must break the cycle at the 
         * last nonstrong link. */ 
        CYCLE_DEBUG_MSG(find_marker(nonstrong_ref->data), 
                        "gc_cycle_push, nonstrong break"); 
        rotate_rec_list(m->frame, nonstrong_ref); 
        NEXT(nonstrong_ref)->frameflags = 
          (NEXT(nonstrong_ref)->frameflags & ~GC_WEAK_REF) | GC_STRONG_REF; 
      } 
 
      else if (nonstrong_ref) { 
        /* Either a nonweak cycle with a strong link in it or a weak 
         * cycle with a nonweak link in it. Break before the first 
         * link that's stronger than the others. */ 
        if (nonstrong_ref != m->frame) { 
          CYCLE_DEBUG_MSG(find_marker(nonstrong_ref->data), 
                          "gc_cycle_push, weaker break"); 
          rotate_rec_list(m->frame, nonstrong_ref); 
        } 
      } 
 
      else if (!weak) { 
        /* A normal cycle which will be destructed in arbitrary 
         * order. For reasons having to do with strong links we 
         * can't mark weak cycles this way. */ 
        unsigned cycle = CYCLE(m->frame) ? CYCLE(m->frame) : ++last_cycle; 
        if (cycle == CYCLE(gc_rec_last)) 
          CYCLE_DEBUG_MSG(m, "gc_cycle_push, old cycle"); 
        else { 
          CYCLE_DEBUG_MSG(m, "gc_cycle_push, cycle"); 
          for (p = m->frame;; p = NEXT(p)) { 
            CYCLE(p) = cycle; 
            CYCLE_DEBUG_MSG(find_marker(p->data), "> gc_cycle_push, mark cycle"); 
            if (p == gc_rec_last) break; 
          }}}}}                 /* Mmm.. lisp ;) */ 
 
  else 
    if (!(m->flags & GC_CYCLE_CHECKED)) { 
      struct gc_frame *l; 
#ifdef PIKE_DEBUG 
      cycle_checked++; 
      if (m->frame) 
        gc_fatal(x, 0, "Marker already got a frame.\n"); 
      if (NEXT(gc_rec_last)) 
        gc_fatal(gc_rec_last->data, 0, "Not at end of list.\n"); 
#endif 
 
      NEXT(gc_rec_last) = m->frame = l = gc_cycle_enqueue_pop(x); 
      m->flags |= GC_CYCLE_CHECKED | (last->flags & GC_LIVE); 
      debug_malloc_touch(x); 
      if (weak) { 
        if (weak > 0) l->frameflags |= GC_WEAK_REF; 
        else l->frameflags |= GC_STRONG_REF; 
      } 
 
#ifdef GC_CYCLE_DEBUG 
      if (weak > 0) CYCLE_DEBUG_MSG(m, "gc_cycle_push, recurse weak"); 
      else if (weak < 0) CYCLE_DEBUG_MSG(m, "gc_cycle_push, recurse strong"); 
      else CYCLE_DEBUG_MSG(m, "gc_cycle_push, recurse"); 
      gc_cycle_indent += 2; 
#endif 
      gc_rec_last = l; 
      return 1; 
    } 
 
  /* Should normally not recurse now, but got to do that anyway if we 
   * must mark live things. */ 
  if (!(last->flags & GC_LIVE) || m->flags & GC_LIVE) { 
    CYCLE_DEBUG_MSG(m, "gc_cycle_push, no recurse"); 
    return 0; 
  } 
 
live_recurse: 
#ifdef PIKE_DEBUG 
  if (m->flags & GC_LIVE) 
    Pike_fatal("Shouldn't live recurse when there's nothing to do.\n"); 
#endif 
  m->flags |= GC_LIVE|GC_LIVE_RECURSE; 
  debug_malloc_touch(x); 
 
  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(x); 
    if (!--*(INT32 *) x) { 
#ifdef PIKE_DEBUG 
      gc_fatal(x, 0, "Thing got zero refs after removing the dead gc ref.\n"); 
#endif 
    } 
  } 
 
  { 
    /* Recurse without linking onto rec_list. */ 
    struct gc_frame *l = gc_cycle_enqueue_pop(x); 
#ifdef GC_CYCLE_DEBUG 
    CYCLE_DEBUG_MSG(m, "gc_cycle_push, live recurse"); 
    gc_cycle_indent += 2; 
#endif 
    gc_rec_last = l; 
  } 
 
#ifdef PIKE_DEBUG 
  live_rec++; 
#endif 
  return 1; 
} 
 
static void gc_cycle_pop(void *a) 
{ 
  struct marker *m = find_marker(a); 
  struct gc_frame *here, *base, *p; 
 
#ifdef PIKE_DEBUG 
  if (gc_is_watching && m && m->flags & GC_WATCHED) { 
    /* This is useful to set breakpoints on. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_cycle_pop() in pass %d.\n", a, Pike_in_gc); 
  } 
  if (!a) Pike_fatal("Got null pointer.\n"); 
  if (Pike_in_gc != GC_PASS_CYCLE) 
    Pike_fatal("GC cycle pop attempted in invalid pass.\n"); 
  if (!(m->flags & GC_CYCLE_CHECKED)) 
    gc_fatal(a, 0, "Marker being popped doesn't have GC_CYCLE_CHECKED.\n"); 
  if ((!(m->flags & GC_NOT_REFERENCED) || m->flags & GC_MARKED) && 
      *(INT32 *) a) 
    gc_fatal(a, 1, "Got a referenced marker to gc_cycle_pop.\n"); 
  if (m->flags & GC_XREFERENCED) 
    gc_fatal(a, 1, "Doing cycle check in externally referenced thing " 
             "missed in mark pass.\n"); 
#endif 
#ifdef GC_CYCLE_DEBUG 
  gc_cycle_indent -= 2; 
#endif 
 
  if (m->flags & GC_LIVE_RECURSE) { 
    m->flags &= ~GC_LIVE_RECURSE; 
    CYCLE_DEBUG_MSG(m, "gc_cycle_pop_live"); 
    gc_rec_last = PREV(gc_rec_top); 
    debug_really_free_gc_frame(gc_rec_top); 
    return; 
  } 
 
  here = m->frame; 
#ifdef PIKE_DEBUG 
  if (!here || here->data != a) 
    gc_fatal(a, 0, "Marker being popped has no or invalid frame.\n"); 
  CHECK_POP_FRAME(here); 
  CHECK_POP_FRAME(gc_rec_last); 
  if (here->frameflags & GC_OFF_STACK) 
    gc_fatal(a, 0, "Marker being popped isn't on stack.\n"); 
  here->back = (struct gc_frame *)(ptrdiff_t) -1; 
#endif 
  here->frameflags |= GC_OFF_STACK; 
 
  for (base = PREV(here), p = here;; base = p, p = NEXT(p)) { 
    if (base == here) { 
      /* Part of a cycle; wait until the cycle is complete before 
       * unlinking it from rec_list. */ 
      DO_IF_DEBUG(m->frame->back = (struct gc_frame *)(ptrdiff_t) -1); 
      CYCLE_DEBUG_MSG(m, "gc_cycle_pop, keep cycle"); 
      return; 
    } 
    CHECK_POP_FRAME(p); 
    if (!(CYCLE(p) && CYCLE(p) == CYCLE(base))) 
      break; 
  } 
 
  gc_rec_last = base; 
  while ((p = NEXT(base))) { 
    struct marker *pm = find_marker(p->data); 
#ifdef PIKE_DEBUG 
    if (pm->frame != p) 
      gc_fatal(p->data, 0, "Bogus marker for thing being popped.\n"); 
#endif 
    p->frameflags &= ~(GC_WEAK_REF|GC_STRONG_REF); 
    if (pm->flags & GC_LIVE_OBJ) { 
      /* 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 (!(pm->flags & GC_GOT_DEAD_REF)) 
        gc_add_extra_ref(p->data); 
      base = p; 
      p->frameflags |= GC_ON_KILL_LIST; 
      DO_IF_DEBUG(PREV(p) = (struct gc_frame *)(ptrdiff_t) -1); 
      CYCLE_DEBUG_MSG(pm, "gc_cycle_pop, put on kill list"); 
    } 
    else { 
      if (!(pm->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 (!(pm->flags & GC_GOT_DEAD_REF)) { 
          gc_add_extra_ref(pm->data); 
          pm->flags |= GC_GOT_DEAD_REF; 
        } 
      } 
#ifdef PIKE_DEBUG 
      else 
        if (pm->flags & GC_GOT_DEAD_REF) 
          gc_fatal(p->data, 0, "Didn't expect a dead extra ref.\n"); 
#endif 
      NEXT(base) = NEXT(p); 
      CYCLE_DEBUG_MSG(pm, "gc_cycle_pop, pop off"); 
      pm->frame = 0; 
      debug_really_free_gc_frame(p); 
    } 
  } 
 
  if (base != gc_rec_last) { 
    NEXT(base) = kill_list; 
    kill_list = NEXT(gc_rec_last); 
    NEXT(gc_rec_last) = 0; 
  } 
} 
 
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. */ 
    fprintf(stderr, "## Watched thing %p found in " 
            "gc_do_free() in pass %d.\n", a, Pike_in_gc); 
  } 
  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. */ 
 
#ifdef PIKE_DEBUG 
  if (*(INT32 *) a > !!(m->flags & GC_GOT_EXTRA_REF)) { 
    if (!(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((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); 
} 
 
static void free_obj_arr(void *oa) 
{ 
  struct array *obj_arr = *((struct array **)oa); 
 
  if (obj_arr) free_array(obj_arr); 
  free(oa); 
} 
 
/*! @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() 
{ 
  /* 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)). 
   */ 
  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_frame *p; 
    unsigned cycle = 0; 
    *obj_arr_ = allocate_array(0); 
 
    for (p = kill_list; p;) { 
      if ((cycle = CYCLE(p))) { 
        push_object((struct object *) p->data); 
        dmalloc_touch_svalue(Pike_sp-1); 
        *obj_arr_ = append_array(*obj_arr_, --Pike_sp); 
      } 
      p = NEXT(p); 
      if (p ? ((unsigned)(CYCLE(p) != cycle)) : cycle) { 
        if ((*obj_arr_)->size >= 2) { 
          push_constant_text("gc"); 
          push_constant_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); 
} 
 
size_t do_gc(void *ignored, int explicit_call) 
{ 
  size_t start_num_objs, start_allocs, unreferenced; 
  cpu_time_t gc_start_time; 
  ptrdiff_t objs, pre_kill_objs; 
#ifdef PIKE_DEBUG 
  unsigned destroy_count, obj_count; 
  ONERROR uwp; 
#endif 
 
  if(Pike_in_gc) return 0; 
 
  if (gc_enabled <= 0 && (gc_enabled < 0 || !explicit_call)) { 
    num_allocs = 0; 
    alloc_threshold = GC_MAX_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; 
  gc_start_time = get_cpu_time(); 
  gc_debug = d_flag; 
#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; 
  last_cycle = 0; 
 
  if(GC_VERBOSE_DO(1 ||) gc_trace) { 
    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; 
  live_rec = frame_rot = 0; 
#endif 
  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) 
      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)); 
  } 
 
  Pike_in_gc=GC_PASS_CHECK; 
  gc_ext_weak_refs = 0; 
  /* First we count internal references */ 
  ACCEPT_UNFINISHED_TYPE_FIELDS { 
    gc_check_all_arrays(); 
    gc_check_all_multisets(); 
    gc_check_all_mappings(); 
    gc_check_all_programs(); 
    gc_check_all_objects(); 
  } END_ACCEPT_UNFINISHED_TYPE_FIELDS; 
 
#ifdef PIKE_DEBUG 
  if(master_object) 
    gc_mark_external (master_object, " as master_object"); 
 
  { 
    extern struct mapping *builtin_constants; 
    if(builtin_constants) 
      gc_mark_external (builtin_constants, " as builtin_constants"); 
  } 
#endif 
 
  /* These callbacks are mainly for the check pass, but can also 
   * do things that are normally associated with the mark pass 
   */ 
  call_callback(& gc_callbacks, NULL); 
 
  GC_VERBOSE_DO(fprintf(stderr, "| check: %u references in %d things, " 
                        "counted %"PRINTSIZET"u weak refs\n", 
                        checked, num_objects, gc_ext_weak_refs)); 
 
  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 = empty_array.next; 
  gc_internal_multiset = first_multiset; 
  gc_internal_mapping = first_mapping; 
  gc_internal_program = first_program; 
  gc_internal_object = first_object; 
 
  /* 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 { 
    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 */ 
  } 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; 
    max_gc_frames = 0; 
#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 (gc_rec_top) 
      Pike_fatal("gc_rec_top not empty at end of cycle check pass.\n"); 
    if (NEXT(&rec_list) || gc_rec_last != &rec_list || gc_rec_top) 
      Pike_fatal("Recurse list not empty or inconsistent after 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, %u cycle ids used,\n" 
                          "|        %u weak references freed, %d more things to free,\n" 
                          "|        %u live recursed frames, %u frame rotations,\n" 
                          "|        space for %u gc frames used\n", 
                          cycle_checked, last_cycle, weak_freed, 
                          delayed_freed - obj_count, 
                          live_rec, frame_rot, max_gc_frames)); 
  } 
 
  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; 
    /* Zap weak references from external to internal things. That 
     * doesn't occur very often; only when something have 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(); 
#ifdef PIKE_NEW_MULTISETS 
    gc_zap_ext_weak_refs_in_multisets(); 
#else 
    /* Multisets handled as arrays. */ 
#endif 
    gc_zap_ext_weak_refs_in_objects(); 
    gc_zap_ext_weak_refs_in_programs(); 
    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_MIDDLETOUCH; 
    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) 
      Pike_fatal("Object count wrong in gc; expected %d, got %d.\n", num_objects, n); 
    get_marker(rec_list.data)->flags |= GC_MIDDLETOUCHED; 
#if 0 /* Temporarily disabled - Hubbe */ 
#ifdef PIKE_DEBUG 
#ifdef DEBUG_MALLOC 
    PTR_HASH_LOOP(marker, i, m) 
      if (!(m->flags & (GC_MIDDLETOUCHED|GC_WEAK_FREED)) && 
          dmalloc_is_invalid_memory_block(m->data)) { 
        fprintf(stderr, "Found a stray marker after middletouch 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_MIDDLETOUCH; 
        Pike_fatal("Fatal in garbage collector.\n"); 
      } 
#endif 
#endif 
#endif 
    GC_VERBOSE_DO(fprintf(stderr, "| middletouch\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 != &weak_empty_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(); 
 
  /* 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 gc_frame *next = free_extra_list->back; 
    union anything u; 
    u.refs = (INT32 *) free_extra_list->data; 
    gc_free_extra_ref(u.refs); 
    free_short_svalue(&u, free_extra_list->u.free_extra_type); 
    debug_really_free_gc_frame(free_extra_list); 
    free_extra_list = next; 
  } 
 
  GC_VERBOSE_DO(fprintf(stderr, "| free: %d 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 (last_cycle && Pike_interpreter.evaluator_stack) { 
    objs -= num_objects; 
    warn_bad_cycles(); 
    objs += num_objects; 
  } 
#ifdef PIKE_DEBUG 
  destroy_count = 0; 
#endif 
  while (kill_list) { 
    struct gc_frame *next = NEXT(kill_list); 
    struct object *o = (struct object *) kill_list->data; 
#ifdef PIKE_DEBUG 
    if (!(kill_list->frameflags & GC_ON_KILL_LIST)) 
      gc_fatal(o, 0, "Kill list element hasn't got the proper flag.\n"); 
    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) { 
        INT32 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); 
    ); 
    destruct(o); 
    free_object(o); 
    gc_free_extra_ref(o); 
#ifdef PIKE_DEBUG 
    destroy_count++; 
#endif 
    debug_really_free_gc_frame(kill_list); 
    kill_list = next; 
  } 
 
  GC_VERBOSE_DO(fprintf(stderr, "| kill: %u objects killed, %d 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)); 
 
  if (gc_debug) { 
    unsigned n; 
    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(); 
    /* gc_touch_all_strings(); */ 
    if (n != (unsigned) num_objects) 
      Pike_fatal("Object count wrong after gc; expected %d, got %d.\n", num_objects, n); 
    GC_VERBOSE_DO(fprintf(stderr, "| posttouch: %u things\n", n)); 
  } 
#ifdef PIKE_DEBUG 
  if (gc_extra_refs) { 
    size_t e; 
    struct marker *m; 
    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++) 
      for (m = marker_hash_table[e]; m; m = m->next) 
        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); 
        } 
    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 
 
  Pike_in_gc=0; 
  exit_gc(); 
 
  /* Calculate the next alloc_threshold. */ 
  { 
    double multiplier, new_threshold; 
    cpu_time_t last_non_gc_time, last_gc_time; 
 
    /* 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 appropriately weight this last instance. */ 
    multiplier=pow(gc_average_slowness, 
                   (double) start_allocs / (double) alloc_threshold); 
 
    /* Comparisons to avoid that overflows mess up the statistics. */ 
    if (gc_start_time > last_gc_end_time) { 
      last_non_gc_time = gc_start_time - last_gc_end_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_time = get_cpu_time(); 
    if (last_gc_end_time > gc_start_time) { 
      last_gc_time = last_gc_end_time - gc_start_time; 
      gc_time = gc_time * multiplier + 
        last_gc_time * (1.0 - multiplier); 
    } 
    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); 
 
    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; 
    } 
    else { 
      new_threshold = (objects_alloced+1.0) * 
        (gc_garbage_ratio_high * start_num_objs) / (objects_freed+1.0); 
      last_garbage_strategy = GARBAGE_RATIO_HIGH; 
    } 
 
#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) 
      new_threshold = (double) GC_MIN_ALLOC_THRESHOLD; 
    else if(new_threshold > GC_MAX_ALLOC_THRESHOLD) 
      new_threshold = (double) GC_MAX_ALLOC_THRESHOLD; 
 
    alloc_threshold = (ptrdiff_t)new_threshold; 
 
    if (!explicit_call && last_gc_time != (cpu_time_t) -1) { 
#if CPU_TIME_IS_THREAD_LOCAL == PIKE_YES 
      Pike_interpreter.thread_state->auto_gc_time += last_gc_time; 
#elif CPU_TIME_IS_THREAD_LOCAL == PIKE_NO 
      auto_gc_time += last_gc_time; 
#endif 
    } 
 
    if(GC_VERBOSE_DO(1 ||) gc_trace) 
    { 
      if (last_gc_time != (cpu_time_t) -1) 
        fprintf(stderr, "done (%"PRINTSIZET"d of %"PRINTSIZET"d " 
                "was unreferenced), %ld ms.\n", 
                unreferenced, start_num_objs, 
                (long) (last_gc_time / (CPU_TIME_TICKS / 1000))); 
      else 
        fprintf(stderr, "done (%"PRINTSIZET"d of %"PRINTSIZET"d " 
                "was unreferenced)\n", 
                unreferenced, start_num_objs); 
    } 
  } 
 
#ifdef PIKE_DEBUG 
  UNSET_ONERROR (uwp); 
  if (max_gc_frames > max_tot_gc_frames) max_tot_gc_frames = max_gc_frames; 
  tot_cycle_checked += cycle_checked; 
  tot_live_rec += live_rec, tot_frame_rot += frame_rot; 
#endif 
 
#ifdef ALWAYS_GC 
  ADD_GC_CALLBACK(); 
#else 
  if(d_flag > 3) ADD_GC_CALLBACK(); 
#endif 
 
  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 CPU milliseconds spent outside the 
 *!       garbage collector. 
 *!     @member int "gc_time" 
 *!       Decaying average over the CPU milliseconds spent inside the 
 *!       garbage collector. 
 *!     @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" or "garbage_ratio_high", which 
 *!       corresponds to the gc parameters with the same names in 
 *!       @[Pike.gc_parameters]. 
 *!     @member int "last_gc" 
 *!       Time when the garbage-collector last ran. 
 *!   @endmapping 
 *! 
 *! @seealso 
 *!   @[gc()], @[Pike.gc_parameters()] 
 */ 
void f__gc_status(INT32 args) 
{ 
  int size = 0; 
 
  pop_n_elems(args); 
 
  push_constant_text("num_objects"); 
  push_int(num_objects); 
  size++; 
 
  push_constant_text("num_allocs"); 
  push_int(num_allocs); 
  size++; 
 
  push_constant_text("alloc_threshold"); 
  push_int64(alloc_threshold); 
  size++; 
 
  push_constant_text("projected_garbage"); 
  push_float(DO_NOT_WARN((FLOAT_TYPE)(objects_freed * (double) num_allocs / 
                                      (double) alloc_threshold))); 
  size++; 
 
  push_constant_text("objects_alloced"); 
  push_int64(DO_NOT_WARN((INT64)objects_alloced)); 
  size++; 
 
  push_constant_text("objects_freed"); 
  push_int64(DO_NOT_WARN((INT64)objects_freed)); 
  size++; 
 
  push_constant_text("last_garbage_ratio"); 
  push_float(DO_NOT_WARN((FLOAT_TYPE) last_garbage_ratio)); 
  size++; 
 
  push_constant_text("non_gc_time"); 
  push_int64(DO_NOT_WARN((INT64) non_gc_time)); 
  size++; 
 
  push_constant_text("gc_time"); 
  push_int64(DO_NOT_WARN((INT64) gc_time)); 
  size++; 
 
  push_constant_text ("last_garbage_strategy"); 
  switch (last_garbage_strategy) { 
    case GARBAGE_RATIO_LOW: push_constant_text ("garbage_ratio_low"); break; 
    case GARBAGE_RATIO_HIGH: push_constant_text ("garbage_ratio_high"); break; 
#ifdef PIKE_DEBUG 
    default: Pike_fatal ("Unknown last_garbage_strategy %d\n", last_garbage_strategy); 
#endif 
  } 
  size++; 
 
  push_constant_text("last_gc"); 
  push_int64(last_gc); 
  size++; 
 
  f_aggregate_mapping(size * 2); 
} 
 
void dump_gc_info(void) 
{ 
  fprintf(stderr,"Current number of things   : %d\n",num_objects); 
  fprintf(stderr,"Allocations since last gc  : %d\n",num_allocs); 
  fprintf(stderr,"Threshold for next gc      : %"PRINTPTRDIFFT"d\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 "CPU_TIME_UNIT" between gc      : %f\n", non_gc_time); 
  fprintf(stderr,"Avg cpu "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" : 
          "???"); 
 
#ifdef PIKE_DEBUG 
  fprintf(stderr,"Max used gc frames         : %u\n", max_tot_gc_frames); 
  fprintf(stderr,"Live recursed ratio        : %g\n", 
          (double) tot_live_rec / 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 */ 
}