File: gc.c

package info (click to toggle)
mlton 20061107-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,828 kB
  • ctags: 61,118
  • sloc: ansic: 11,446; makefile: 1,339; sh: 1,160; lisp: 900; pascal: 256; asm: 97
file content (4855 lines) | stat: -rw-r--r-- 193,353 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
/* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
 *    Jagannathan, and Stephen Weeks.
 * Copyright (C) 1997-2000 NEC Research Institute.
 *
 * MLton is released under a BSD-style license.
 * See the file MLton-LICENSE for details.
 */

#include "platform.h"

/* The mutator should maintain the invariants
 *
 *  function entry: stackTop + maxFrameSize <= endOfStack
 *  anywhere else: stackTop + 2 * maxFrameSize <= endOfStack
 * 
 * The latter will give it enough space to make a function call and always
 * satisfy the former.  The former will allow it to make a gc call at the
 * function entry limit.
 */

#ifndef DEBUG
#define DEBUG FALSE
#endif

#ifndef DEBUG_PROFILE
#define DEBUG_PROFILE FALSE
#endif

enum {
        BOGUS_EXN_STACK = 0xFFFFFFFF,
        CARD_SIZE_LOG2 = 8, /* must agree w/ cardSizeLog2 in ssa-to-rssa.fun */
        COPY_CHUNK_SIZE = 0x2000000, /* 32M */
        CROSS_MAP_EMPTY = 255,
        CURRENT_SOURCE_UNDEFINED = 0xFFFFFFFF,
        DEBUG_ARRAY = FALSE,
        DEBUG_CALL_STACK = FALSE,
        DEBUG_CARD_MARKING = FALSE,
        DEBUG_DETAILED = FALSE,
        DEBUG_ENTER_LEAVE = FALSE,
        DEBUG_GENERATIONAL = FALSE,
        DEBUG_MARK_COMPACT = FALSE,
        DEBUG_RESIZING = FALSE,
        DEBUG_SHARE = FALSE,
        DEBUG_SIZE = FALSE,
        DEBUG_STACKS = FALSE,
        DEBUG_THREADS = FALSE,
        DEBUG_WEAK = FALSE,
        DEBUG_WORLD = FALSE,
        FORCE_GENERATIONAL = FALSE,
        FORCE_MARK_COMPACT = FALSE,
        FORWARDED = 0xFFFFFFFF,
        STACK_HEADER_SIZE = WORD_SIZE,
};

typedef enum {
        MARK_MODE,
        UNMARK_MODE,
} MarkMode;

#define EMPTY_HEADER GC_objectHeader (EMPTY_TYPE_INDEX)
#define STACK_HEADER GC_objectHeader (STACK_TYPE_INDEX)
#define STRING_HEADER GC_objectHeader (STRING_TYPE_INDEX)
#define THREAD_HEADER GC_objectHeader (THREAD_TYPE_INDEX)
#define WEAK_GONE_HEADER GC_objectHeader (WEAK_GONE_TYPE_INDEX)
#define WORD8_VECTOR_HEADER GC_objectHeader (WORD8_TYPE_INDEX)

#define SPLIT_HEADER()                                                          \
        do {                                                                    \
                int objectTypeIndex;                                            \
                GC_ObjectType *t;                                               \
                                                                                \
                assert (1 == (header & 1));                                     \
                objectTypeIndex = (header & TYPE_INDEX_MASK) >> 1;              \
                assert (0 <= objectTypeIndex                                    \
                                and objectTypeIndex < s->objectTypesSize);      \
                t = &s->objectTypes [objectTypeIndex];                          \
                tag = t->tag;                                                   \
                hasIdentity = t->hasIdentity;                                   \
                numNonPointers = t->numNonPointers;                             \
                numPointers = t->numPointers;                                   \
                if (DEBUG_DETAILED)                                             \
                        fprintf (stderr, "SPLIT_HEADER (0x%08x)  numNonPointers = %u  numPointers = %u\n", \
                                        (uint)header, numNonPointers, numPointers);     \
        } while (0)

static char* tagToString (GC_ObjectTypeTag t) {
        switch (t) {
        case ARRAY_TAG:
        return "ARRAY";
        case NORMAL_TAG:
        return "NORMAL";
        case STACK_TAG:
        return "STACK";
        case WEAK_TAG:
        return "WEAK";
        default:
        die ("bad tag %u", t);
        }
}

static inline ulong meg (uint n) {
        return n / (1024ul * 1024ul);
}

static inline uint toBytes (uint n) {
        return n << 2;
}

static inline W64 min64 (W64 x, W64 y) {
        return ((x < y) ? x : y);
}

static inline W64 max64 (W64 x, W64 y) {
        return ((x > y) ? x : y);
}

static inline uint roundDown (uint a, uint b) {
        return a - (a % b);
}

static inline uint align (uint a, uint b) {
        assert (a >= 0);
        assert (b >= 1);
        a += b - 1;
        a -= a % b;
        return a;       
}

static inline W64 w64align (W64 a, uint b) {
        W64 res;

        assert (a >= 0);
        assert (b >= 1);
        res = a + b - 1;
        res = res - res % b;
        if (FALSE)
                fprintf (stderr, "%llu = w64Align (%llu, %u)\n", res, a, b);
        return res;
}

static bool isAligned (uint a, uint b) {
        return 0 == a % b;
}

#if ASSERT
static bool isAlignedFrontier (GC_state s, pointer p) {
        return isAligned ((uint)p + GC_NORMAL_HEADER_SIZE, s->alignment);
}

static bool isAlignedReserved (GC_state s, uint r) {
        return isAligned (STACK_HEADER_SIZE + sizeof (struct GC_stack) + r, 
                                s->alignment);
}
#endif

static inline uint pad (GC_state s, uint bytes, uint extra) {
        return align (bytes + extra, s->alignment) - extra;
}

static inline pointer alignFrontier (GC_state s, pointer p) {
        return (pointer) pad (s, (uint)p, GC_NORMAL_HEADER_SIZE);
}

pointer GC_alignFrontier (GC_state s, pointer p) {
        return alignFrontier (s, p);
}

static inline uint stackReserved (GC_state s, uint r) {
        uint res;

        res = pad (s, r, STACK_HEADER_SIZE + sizeof (struct GC_stack));
        if (DEBUG_STACKS)
                fprintf (stderr, "%s = stackReserved (%s)\n",
                                uintToCommaString (res),
                                uintToCommaString (r));
        return res;
}

static void sunlink (char *path) {
        unless (0 == unlink (path))
                diee ("unlink (%s) failed", path);
}

/* ---------------------------------------------------------------- */
/*                    Virtual Memory Management                     */
/* ---------------------------------------------------------------- */

static inline void *GC_mmapAnon (void *start, size_t length) {
        void *res;

        res = mmapAnon (start, length);
        if (DEBUG_MEM)
                fprintf (stderr, "0x%08x = mmapAnon (0x%08x, %s)\n",
                                        (uint)res,
                                        (uint)start, 
                                        uintToCommaString (length));
        return res;
}

void *smmap (size_t length) {
        void *result;

        result = GC_mmapAnon (NULL, length);
        if ((void*)-1 == result) {
                showMem ();
                die ("Out of memory.");
        }
        return result;
}

static inline void GC_release (void *base, size_t length) {
        if (DEBUG_MEM)
                fprintf (stderr, "release (0x%08x, %s)\n",
                                (uint)base, uintToCommaString (length));
        release (base, length);
}

static inline void GC_decommit (void *base, size_t length) {
        if (DEBUG_MEM)
                fprintf (stderr, "decommit (0x%08x, %s)\n",
                                (uint)base, uintToCommaString (length));
        decommit (base, length);
}

static inline void copy (pointer src, pointer dst, uint size) {
        uint    *to,
                *from,
                *limit;

        if (DEBUG_DETAILED)
                fprintf (stderr, "copy (0x%08x, 0x%08x, %u)\n",
                                (uint)src, (uint)dst, size);
        assert (isAligned ((uint)src, WORD_SIZE));
        assert (isAligned ((uint)dst, WORD_SIZE));
        assert (isAligned (size, WORD_SIZE));
        assert (dst <= src or src + size <= dst);
        if (src == dst)
                return;
        from = (uint*)src;
        to = (uint*)dst;
        limit = (uint*)(src + size);
        until (from == limit)
                *to++ = *from++;
}

/* ---------------------------------------------------------------- */
/*                              rusage                              */
/* ---------------------------------------------------------------- */

static inline void rusageZero (struct rusage *ru) {
        memset (ru, 0, sizeof (*ru));
}

static void rusagePlusMax (struct rusage *ru1,
                              struct rusage *ru2,
                              struct rusage *ru) {
        const int       million = 1000000;
        time_t          sec,
                        usec;

        sec = ru1->ru_utime.tv_sec + ru2->ru_utime.tv_sec;
        usec = ru1->ru_utime.tv_usec + ru2->ru_utime.tv_usec;
        sec += (usec / million);
        usec %= million;
        ru->ru_utime.tv_sec = sec;
        ru->ru_utime.tv_usec = usec;

        sec = ru1->ru_stime.tv_sec + ru2->ru_stime.tv_sec;
        usec = ru1->ru_stime.tv_usec + ru2->ru_stime.tv_usec;
        sec += (usec / million);
        usec %= million;
        ru->ru_stime.tv_sec = sec;
        ru->ru_stime.tv_usec = usec;
}

static void rusageMinusMax (struct rusage *ru1,
                                struct rusage *ru2,
                                struct rusage *ru) {
        const int       million = 1000000;
        time_t          sec,
                        usec;

        sec = (ru1->ru_utime.tv_sec - ru2->ru_utime.tv_sec) - 1;
        usec = ru1->ru_utime.tv_usec + million - ru2->ru_utime.tv_usec;
        sec += (usec / million);
        usec %= million;
        ru->ru_utime.tv_sec = sec;
        ru->ru_utime.tv_usec = usec;

        sec = (ru1->ru_stime.tv_sec - ru2->ru_stime.tv_sec) - 1;
        usec = ru1->ru_stime.tv_usec + million - ru2->ru_stime.tv_usec;
        sec += (usec / million);
        usec %= million;
        ru->ru_stime.tv_sec = sec;
        ru->ru_stime.tv_usec = usec;
}

static uint rusageTime (struct rusage *ru) {
        uint    result;

        result = 0;
        result += 1000 * ru->ru_utime.tv_sec;
        result += 1000 * ru->ru_stime.tv_sec;
        result += ru->ru_utime.tv_usec / 1000;
        result += ru->ru_stime.tv_usec / 1000;
        return result;
}

/* Return time as number of milliseconds. */
static uint currentTime () {
        struct rusage   ru;

        fixedGetrusage (RUSAGE_SELF, &ru);
        return rusageTime (&ru);
}

static inline void startTiming (struct rusage *ru_start) {
        fixedGetrusage (RUSAGE_SELF, ru_start);
}

static uint stopTiming (struct rusage *ru_start, struct rusage *ru_gc) {
        struct rusage ru_finish, ru_total;

        fixedGetrusage (RUSAGE_SELF, &ru_finish);
        rusageMinusMax (&ru_finish, ru_start, &ru_total);
        rusagePlusMax (ru_gc, &ru_total, ru_gc);
        return rusageTime (&ru_total);
}

/* ---------------------------------------------------------------- */
/*                            GC_display                            */
/* ---------------------------------------------------------------- */

void GC_display (GC_state s, FILE *stream) {
        fprintf (stream, "GC state\n"
                 "\tcardMap = 0x%08x\n"
                 "\toldGen = 0x%08x\n"
                 "\toldGenSize = %s\n"
                 "\toldGen + oldGenSize = 0x%08x\n"
                 "\tnursery = 0x%08x\n"
                 "\tfrontier = 0x%08x\n"
                 "\tfrontier - nursery = %td\n"
                 "\tlimitPlusSlop - frontier = %td\n",
                        (uint) s->cardMap,
                        (uint) s->heap.start,
                        uintToCommaString (s->oldGenSize),
                        (uint) s->heap.start + s->oldGenSize,
                        (uint) s->nursery, 
                        (uint) s->frontier,
                        s->frontier - s->nursery,
                        s->limitPlusSlop - s->frontier);
        fprintf (stream, "\tcanHandle = %d\n\tsignalsIsPending = %d\n", s->canHandle, s->signalIsPending);
        fprintf (stderr, "\tcurrentThread = 0x%08x\n", (uint) s->currentThread);
        fprintf (stream, "\tstackBottom = 0x%08x\n"
                 "\tstackTop - stackBottom = %td\n"
                 "\tstackLimit - stackTop = %td\n",
                        (uint)s->stackBottom,
                        s->stackTop - s->stackBottom,
                        (s->stackLimit - s->stackTop));
        fprintf (stream, "\texnStack = %u\n\tbytesNeeded = %u\n\treserved = %u\n\tused = %u\n",
                        s->currentThread->exnStack,
                        s->currentThread->bytesNeeded,
                        s->currentThread->stack->reserved,
                        s->currentThread->stack->used);
        if (DEBUG_GENERATIONAL and DEBUG_DETAILED) {
                int i;

                fprintf (stderr, "crossMap trues\n");
                for (i = 0; i < s->crossMapSize; ++i)
                        unless (CROSS_MAP_EMPTY == s->crossMap[i])
                                fprintf (stderr, "\t%u\n", i);
                fprintf (stderr, "\n");
        }               
}

static inline uint cardNumToSize (GC_state s, uint n) {
        return n << CARD_SIZE_LOG2;
}

static inline uint divCardSize (GC_state s, uint n) {
        return n >> CARD_SIZE_LOG2;
}

static inline pointer cardMapAddr (GC_state s, pointer p) {
        pointer res;

        res = &s->cardMapForMutator [divCardSize (s, (uint)p)];
        if (DEBUG_CARD_MARKING)
                fprintf (stderr, "0x%08x = cardMapAddr (0x%08x)\n",
                                (uint)res, (uint)p);
        return res;
}

static inline bool cardIsMarked (GC_state s, pointer p) {
        return *cardMapAddr (s, p);
}

static inline void markCard (GC_state s, pointer p) {
        if (DEBUG_CARD_MARKING)
                fprintf (stderr, "markCard (0x%08x)\n", (uint)p);
        if (s->mutatorMarksCards)
                *cardMapAddr (s, p) = '\001';
}

/* ---------------------------------------------------------------- */
/*                              Stacks                              */
/* ---------------------------------------------------------------- */

/* stackSlop returns the amount of "slop" space needed between the top of 
 * the stack and the end of the stack space.
 */
static inline uint stackSlop (GC_state s) {
        return 2 * s->maxFrameSize;
}

static inline uint initialStackSize (GC_state s) {
        return stackSlop (s);
}

static inline uint stackBytes (GC_state s, uint size) {
        uint res;

        res = align (STACK_HEADER_SIZE + sizeof (struct GC_stack) + size,
                        s->alignment);
        if (DEBUG_STACKS)
                fprintf (stderr, "%s = stackBytes (%s)\n",
                                uintToCommaString (res),
                                uintToCommaString (size));
        return res;
}

static inline pointer stackBottom (GC_state s, GC_stack stack) {
        pointer res;

        res = ((pointer)stack) + sizeof (struct GC_stack);
        assert (isAligned ((uint)res, s->alignment));
        return res;
}

/* Pointer to the topmost word in use on the stack. */
static inline pointer stackTop (GC_state s, GC_stack stack) {
        return stackBottom (s, stack) + stack->used;
}

/* Pointer to the end of stack. */
static inline pointer endOfStack (GC_state s, GC_stack stack) {
        return stackBottom (s, stack) + stack->reserved;
}

/* The maximum value stackTop may take on. */
static inline pointer stackLimit (GC_state s, GC_stack stack) {
        return endOfStack (s, stack) - stackSlop (s);
}

static inline bool stackIsEmpty (GC_stack stack) {
        return 0 == stack->used;
}

static inline uint getFrameIndex (GC_state s, word returnAddress) {
        uint res;

        res = s->returnAddressToFrameIndex (returnAddress);
        if (DEBUG_DETAILED)
                fprintf (stderr, "%u = getFrameIndex (0x%08x)\n",
                                returnAddress, res);
        return res;
}

static inline uint topFrameIndex (GC_state s) {
        uint res;

        assert (s->stackTop > s->stackBottom);
        res = getFrameIndex (s, *(word*)(s->stackTop - WORD_SIZE));
        if (DEBUG_PROFILE)
                fprintf (stderr, "topFrameIndex = %u\n", res);
        return res;
}

static inline uint topFrameSourceSeqIndex (GC_state s) {
        return s->frameSources[topFrameIndex (s)];
}

static inline GC_frameLayout * getFrameLayout (GC_state s, word returnAddress) {
        GC_frameLayout *layout;
        uint index;

        index = getFrameIndex (s, returnAddress);
        if (DEBUG_DETAILED)
                fprintf (stderr, "returnAddress = 0x%08x  index = %d  frameLayoutsSize = %d\n",
                                returnAddress, index, s->frameLayoutsSize);
        assert (0 <= index and index < s->frameLayoutsSize);
        layout = &(s->frameLayouts[index]);
        assert (layout->numBytes > 0);
        return layout;
}

static inline uint topFrameSize (GC_state s, GC_stack stack) {
        GC_frameLayout *layout;
        
        assert (not (stackIsEmpty (stack)));
        layout = getFrameLayout (s, *(word*)(stackTop (s, stack) - WORD_SIZE));
        return layout->numBytes;
}

static inline uint stackNeedsReserved (GC_state s, GC_stack stack) {
        return stack->used + stackSlop (s) - topFrameSize (s, stack);
}

#if ASSERT
static bool hasBytesFree (GC_state s, W32 oldGen, W32 nursery) {
        bool res;

        res = s->oldGenSize + oldGen 
                        + (s->canMinor ? 2 : 1) 
                                * (s->limitPlusSlop - s->nursery)
                        <= s->heap.size
                and nursery <= s->limitPlusSlop - s->frontier;
        if (DEBUG_DETAILED)
                fprintf (stderr, "%s = hasBytesFree (%s, %s)\n",
                                boolToString (res),
                                uintToCommaString (oldGen),
                                uintToCommaString (nursery));
        return res;
}
#endif

/* bytesRequested includes the header. */
static pointer object (GC_state s, uint header, W32 bytesRequested,
                                bool allocInOldGen,
                                Bool hasDouble) {
        pointer frontier;
        pointer result;

        if (DEBUG)
                fprintf (stderr, "object (0x%08x, %u, %s)\n",
                                header, 
                                (uint)bytesRequested,
                                boolToString (allocInOldGen));
        assert (isAligned (bytesRequested, s->alignment));
        assert (allocInOldGen
                        ? hasBytesFree (s, bytesRequested, 0)
                        : hasBytesFree (s, 0, bytesRequested));
        if (allocInOldGen) {
                frontier = s->heap.start + s->oldGenSize;
                s->oldGenSize += bytesRequested;
                s->bytesAllocated += bytesRequested;
        } else {
                if (DEBUG_DETAILED)
                        fprintf (stderr, "frontier changed from 0x%08x to 0x%08x\n",
                                        (uint)s->frontier, 
                                        (uint)(s->frontier + bytesRequested));
                frontier = s->frontier;
                s->frontier += bytesRequested;
        }
        GC_profileAllocInc (s, bytesRequested);
        *(uint*)(frontier) = header;
        result = frontier + GC_NORMAL_HEADER_SIZE;
        return result;
}

static GC_stack newStack (GC_state s, uint reserved, bool allocInOldGen) {
        GC_stack stack;

        reserved = stackReserved (s, reserved);
        if (reserved > s->maxStackSizeSeen)
                s->maxStackSizeSeen = reserved;
        stack = (GC_stack) object (s, STACK_HEADER, stackBytes (s, reserved),
                                        allocInOldGen, TRUE);
        stack->reserved = reserved;
        stack->used = 0;
        if (DEBUG_STACKS)
                fprintf (stderr, "0x%x = newStack (%u)\n", (uint)stack, 
                                reserved);
        return stack;
}

static void setStack (GC_state s) {
        GC_stack stack;

        s->exnStack = s->currentThread->exnStack;
        stack = s->currentThread->stack;
        s->stackBottom = stackBottom (s, stack);
        s->stackTop = stackTop (s, stack);
        s->stackLimit = stackLimit (s, stack);
        /* We must card mark the stack because it will be updated by the mutator.
         */
        markCard (s, (pointer)stack);
}

static void stackCopy (GC_state s, GC_stack from, GC_stack to) {
        assert (from->used <= to->reserved);
        to->used = from->used;
        if (DEBUG_STACKS)
                fprintf (stderr, "stackCopy from 0x%08x to 0x%08x of length %u\n",
                                (uint) stackBottom (s, from), 
                                (uint) stackBottom (s, to),
                                from->used);
        memcpy (stackBottom (s, to), stackBottom (s, from), from->used);
}

/* Number of bytes used by the stack. */
static inline uint currentStackUsed (GC_state s) {
        return s->stackTop - s->stackBottom;
}

/* ---------------------------------------------------------------- */
/*                          foreachGlobal                           */
/* ---------------------------------------------------------------- */

typedef void (*GC_pointerFun) (GC_state s, pointer *p);

static inline void maybeCall (GC_pointerFun f, GC_state s, pointer *pp) {
        if (GC_isPointer (*pp))
                f (s, pp);
}

/* Apply f to each global pointer into the heap. */
static inline void foreachGlobal (GC_state s, GC_pointerFun f) {
        int i;

        for (i = 0; i < s->globalsSize; ++i) {
                if (DEBUG_DETAILED)
                        fprintf (stderr, "foreachGlobal %u\n", i);
                maybeCall (f, s, &s->globals [i]);
        }
        if (DEBUG_DETAILED)
                fprintf (stderr, "foreachGlobal threads\n");
        maybeCall (f, s, (pointer*)&s->callFromCHandler);
        maybeCall (f, s, (pointer*)&s->currentThread);
        maybeCall (f, s, (pointer*)&s->savedThread);
        maybeCall (f, s, (pointer*)&s->signalHandler);
}

#if ASSERT
static pointer arrayPointer (GC_state s, 
                                pointer a, 
                                uint arrayIndex, 
                                uint pointerIndex) {
        Bool hasIdentity;
        word header;
        uint numPointers;
        uint numNonPointers;
        uint tag;

        header = GC_getHeader (a);
        SPLIT_HEADER();
        assert (tag == ARRAY_TAG);
        return a 
                + arrayIndex * (numNonPointers + toBytes (numPointers))
                + numNonPointers
                + pointerIndex * POINTER_SIZE;
}
#endif

/* The number of bytes in an array, not including the header. */
static inline uint arrayNumBytes (GC_state s,
                                        pointer p, 
                                        uint numPointers,
                                        uint numNonPointers) {
        uint bytesPerElement;
        uint numElements;
        uint result;
        
        numElements = GC_arrayNumElements (p);
        bytesPerElement = numNonPointers + toBytes (numPointers);
        result = numElements * bytesPerElement;
        /* Empty arrays have POINTER_SIZE bytes for the forwarding pointer */
        if (0 == result) 
                result = POINTER_SIZE;
        return pad (s, result, GC_ARRAY_HEADER_SIZE);
}

/* ---------------------------------------------------------------- */
/*                      foreachPointerInObject                      */
/* ---------------------------------------------------------------- */
/* foreachPointerInObject (s, p,f, ws) applies f to each pointer in the object
 * pointer to by p.
 * Returns pointer to the end of object, i.e. just past object.
 *
 * If ws, then the object pointer in weak objects is skipped.
 */

static inline pointer foreachPointerInObject (GC_state s, pointer p,
                                                Bool skipWeaks,
                                                GC_pointerFun f) {
        Bool hasIdentity;
        word header;
        uint numPointers;
        uint numNonPointers;
        uint tag;

        header = GC_getHeader (p);
        SPLIT_HEADER();
        if (DEBUG_DETAILED)
                fprintf (stderr, "foreachPointerInObject p = 0x%x  header = 0x%x  tag = %s  numNonPointers = %d  numPointers = %d\n", 
                        (uint)p, header, tagToString (tag), 
                        numNonPointers, numPointers);
        if (NORMAL_TAG == tag) {
                pointer max;

                p += toBytes (numNonPointers);
                max = p + toBytes (numPointers);
                /* Apply f to all internal pointers. */
                for ( ; p < max; p += POINTER_SIZE) {
                        if (DEBUG_DETAILED)
                                fprintf (stderr, "p = 0x%08x  *p = 0x%08x\n",
                                                (uint)p, *(uint*)p);
                        maybeCall (f, s, (pointer*)p);
                }
        } else if (WEAK_TAG == tag) {
                if (not skipWeaks and 1 == numPointers)
                        maybeCall (f, s, (pointer*)&(((GC_weak)p)->object));
                p += sizeof (struct GC_weak);
        } else if (ARRAY_TAG == tag) {
                uint bytesPerElement;
                uint dataBytes;
                pointer max;
                uint numElements;

                numElements = GC_arrayNumElements (p);
                bytesPerElement = numNonPointers + toBytes (numPointers);
                dataBytes = numElements * bytesPerElement;
                /* Must check 0 == dataBytes before 0 == numPointers to correctly
                 * handle arrays when both are true.
                 */
                if (0 == dataBytes)
                        /* Empty arrays have space for forwarding pointer. */
                        dataBytes = POINTER_SIZE;
                else if (0 == numPointers)
                        /* No pointers to process. */
                        ;
                else {
                        max = p + dataBytes;
                        if (0 == numNonPointers)
                                /* Array with only pointers. */
                                for (; p < max; p += POINTER_SIZE)
                                        maybeCall (f, s, (pointer*)p);
                        else {
                                /* Array with a mix of pointers and non-pointers.
                                 */
                                uint numBytesPointers;
                        
                                numBytesPointers = toBytes (numPointers);
                                /* For each array element. */
                                while (p < max) {
                                        pointer max2;

                                        /* Skip the non-pointers. */
                                        p += numNonPointers;
                                        max2 = p + numBytesPointers;
                                        /* For each internal pointer. */
                                        for ( ; p < max2; p += POINTER_SIZE) 
                                                maybeCall (f, s, (pointer*)p);
                                }
                        }
                        assert (p == max);
                        p -= dataBytes;
                }
                p += pad (s, dataBytes, GC_ARRAY_HEADER_SIZE);
        } else { /* stack */
                GC_stack stack;
                pointer top, bottom;
                int i;
                word returnAddress;
                GC_frameLayout *layout;
                GC_offsets frameOffsets;

                assert (STACK_TAG == tag);
                stack = (GC_stack)p;
                bottom = stackBottom (s, stack);
                top = stackTop (s, stack);
                assert (stack->used <= stack->reserved);
                while (top > bottom) {
                        /* Invariant: top points just past a "return address". */
                        returnAddress = *(word*) (top - WORD_SIZE);
                        if (DEBUG) {
                                fprintf (stderr, "  top = %td  return address = ",
                                                top - bottom);
                                fprintf (stderr, "0x%08x.\n", returnAddress);
                        }
                        layout = getFrameLayout (s, returnAddress); 
                        frameOffsets = layout->offsets;
                        top -= layout->numBytes;
                        for (i = 0 ; i < frameOffsets[0] ; ++i) {
                                if (DEBUG)
                                        fprintf(stderr, 
                                                "    offset %u  address 0x%08x\n", 
                                                frameOffsets[i + 1],
                                                (uint)(*(pointer*)(top + frameOffsets[i + 1])));
                                maybeCall(f, s, 
                                          (pointer*)
                                          (top + frameOffsets[i + 1]));
                        }
                }
                assert(top == bottom);
                p += sizeof (struct GC_stack) + stack->reserved;
        }
        return p;
}

/* ---------------------------------------------------------------- */
/*                              toData                              */
/* ---------------------------------------------------------------- */

/* If p points at the beginning of an object, then toData p returns a pointer 
 * to the start of the object data.
 */
static inline pointer toData (GC_state s, pointer p) {
        word header;
        pointer res;

        assert (isAlignedFrontier (s, p));
        header = *(word*)p;
        if (0 == header)
                /* Looking at the counter word in an array. */
                res = p + GC_ARRAY_HEADER_SIZE;
        else
                /* Looking at a header word. */
                res = p + GC_NORMAL_HEADER_SIZE;
        assert (isAligned ((uint)res, s->alignment));
        return res;
}

/* ---------------------------------------------------------------- */
/*                      foreachPointerInRange                       */
/* ---------------------------------------------------------------- */

/* foreachPointerInRange (s, front, back, ws, f)
 * Apply f to each pointer between front and *back, which should be a 
 * contiguous sequence of objects, where front points at the beginning of
 * the first object and *back points just past the end of the last object.
 * f may increase *back (for example, this is done by forward).
 * foreachPointerInRange returns a pointer to the end of the last object it
 * visits.
 *
 * If ws, then the object pointer in weak objects is skipped.
 */

static inline pointer foreachPointerInRange (GC_state s, 
                                                pointer front, 
                                                pointer *back,
                                                Bool skipWeaks,
                                                GC_pointerFun f) {
        pointer b;

        assert (isAlignedFrontier (s, front));
        if (DEBUG_DETAILED)
                fprintf (stderr, "foreachPointerInRange  front = 0x%08x  *back = 0x%08x\n",
                                (uint)front, *(uint*)back);
        b = *back;
        assert (front <= b);
        while (front < b) {
                while (front < b) {
                        assert (isAligned ((uint)front, WORD_SIZE));
                        if (DEBUG_DETAILED)
                                fprintf (stderr, "front = 0x%08x  *back = 0x%08x\n",
                                                (uint)front, *(uint*)back);
                        front = foreachPointerInObject 
                                        (s, toData (s, front), skipWeaks, f);
                }
                b = *back;
        }
        return front;
}

/* ---------------------------------------------------------------- */
/*                            invariant                             */
/* ---------------------------------------------------------------- */

static bool mutatorFrontierInvariant (GC_state s) {
        return (s->currentThread->bytesNeeded <= 
                        s->limitPlusSlop - s->frontier);
}

static bool mutatorStackInvariant (GC_state s) {
        return (stackTop (s, s->currentThread->stack) <= 
                        stackLimit (s, s->currentThread->stack) + 
                        topFrameSize (s, s->currentThread->stack));
}

static bool ratiosOk (GC_state s) {
        return 1.0 < s->growRatio
                        and 1.0 < s->nurseryRatio
                        and 1.0 < s->markCompactRatio
                        and s->markCompactRatio <= s->copyRatio
                        and s->copyRatio <= s->liveRatio;
}

static inline bool isInNursery (GC_state s, pointer p) {
        return s->nursery <= p and p < s->frontier;
}

static inline bool isInOldGen (GC_state s, pointer p) {
        return s->heap.start <= p and p < s->heap.start + s->oldGenSize;
}

#if ASSERT

static inline bool isInFromSpace (GC_state s, pointer p) {
        return (isInOldGen (s, p) or isInNursery (s, p));
}

static inline void assertIsInFromSpace (GC_state s, pointer *p) {
#if ASSERT
        unless (isInFromSpace (s, *p))
                die ("gc.c: assertIsInFromSpace p = 0x%08x  *p = 0x%08x;\n",
                        (uint)p, *(uint*)p);
        /* The following checks that intergenerational pointers have the
         * appropriate card marked.  Unfortunately, it doesn't work because
         * for stacks, the card containing the beginning of the stack is marked,
         * but any remaining cards aren't.
         */
        if (FALSE and s->mutatorMarksCards 
                and isInOldGen (s, (pointer)p) 
                and isInNursery (s, *p)
                and not cardIsMarked (s, (pointer)p)) {
                GC_display (s, stderr);
                die ("gc.c: intergenerational pointer from 0x%08x to 0x%08x with unmarked card.\n",
                        (uint)p, *(uint*)p);
        }
#endif
}

static inline bool isInToSpace (GC_state s, pointer p) {
        return (not (GC_isPointer (p))
                        or (s->toSpace <= p and p < s->toLimit));
}

static bool invariant (GC_state s) {
        int i;
        pointer back;
        GC_stack stack;

        if (DEBUG)
                fprintf (stderr, "invariant\n");
        assert (ratiosOk (s));
        /* Frame layouts */
        for (i = 0; i < s->frameLayoutsSize; ++i) {
                GC_frameLayout *layout;

                layout = &(s->frameLayouts[i]);
                if (layout->numBytes > 0) {
                        GC_offsets offsets;
//                      int j;

                        assert (layout->numBytes <= s->maxFrameSize);
                        offsets = layout->offsets;
// No longer correct, since handler frames have a "size" (i.e. return address)
// pointing into the middle of the frame.
//                      for (j = 0; j < offsets[0]; ++j)
//                              assert (offsets[j + 1] < layout->numBytes);
                }
        }
        if (s->mutatorMarksCards) {
                assert (s->cardMap == 
                                &s->cardMapForMutator[divCardSize(s, (uint)s->heap.start)]);
                assert (&s->cardMapForMutator[divCardSize (s, (uint)s->heap.start + s->heap.size - WORD_SIZE)]
                                < s->cardMap + s->cardMapSize);
        }
        /* Heap */
        assert (isAligned (s->heap.size, s->pageSize));
        assert (isAligned ((uint)s->heap.start, s->cardSize));
        assert (isAlignedFrontier (s, s->heap.start + s->oldGenSize));
        assert (isAlignedFrontier (s, s->nursery));
        assert (isAlignedFrontier (s, s->frontier));
        assert (s->nursery <= s->frontier);
        unless (0 == s->heap.size) {
                assert (s->nursery <= s->frontier);
                assert (s->frontier <= s->limitPlusSlop);
                assert (s->limit == s->limitPlusSlop - LIMIT_SLOP);
                assert (hasBytesFree (s, 0, 0));
        }
        assert (s->heap2.start == NULL or s->heap.size == s->heap2.size);
        /* Check that all pointers are into from space. */
        foreachGlobal (s, assertIsInFromSpace);
        back = s->heap.start + s->oldGenSize;
        if (DEBUG_DETAILED)
                fprintf (stderr, "Checking old generation.\n");
        foreachPointerInRange (s, alignFrontier (s, s->heap.start), &back, FALSE,
                                assertIsInFromSpace);
        if (DEBUG_DETAILED)
                fprintf (stderr, "Checking nursery.\n");
        foreachPointerInRange (s, s->nursery, &s->frontier, FALSE,
                                assertIsInFromSpace);
        /* Current thread. */
        stack = s->currentThread->stack;
        assert (isAlignedReserved (s, stack->reserved));
        assert (s->stackBottom == stackBottom (s, stack));
        assert (s->stackTop == stackTop (s, stack));
        assert (s->stackLimit == stackLimit (s, stack));
        assert (stack->used == currentStackUsed (s));
        assert (stack->used <= stack->reserved);
        assert (s->stackBottom <= s->stackTop);
        if (DEBUG)
                fprintf (stderr, "invariant passed\n");
        return TRUE;
}

static bool mutatorInvariant (GC_state s, bool frontier, bool stack) {
        if (DEBUG)
                GC_display (s, stderr);
        if (frontier)
                assert (mutatorFrontierInvariant(s));
        if (stack)
                assert (mutatorStackInvariant(s));
        assert (invariant (s));
        return TRUE;
}
#endif /* #if ASSERT */

/* ---------------------------------------------------------------- */
/*                         enter and leave                          */
/* ---------------------------------------------------------------- */

static inline void atomicBegin (GC_state s) {
        s->canHandle++;
        if (0 == s->limit)
                s->limit = s->limitPlusSlop - LIMIT_SLOP;
}

static inline void atomicEnd (GC_state s) {
        s->canHandle--;
        if (0 == s->canHandle and s->signalIsPending)
                s->limit = 0;
}

/* enter and leave should be called at the start and end of every GC function
 * that is exported to the outside world.  They make sure that the function
 * is run in a critical section and check the GC invariant.
 */
static void enter (GC_state s) {
        if (DEBUG)
                fprintf (stderr, "enter\n");
        /* used needs to be set because the mutator has changed s->stackTop. */
        s->currentThread->stack->used = currentStackUsed (s);
        s->currentThread->exnStack = s->exnStack;
        if (DEBUG) 
                GC_display (s, stderr);
        atomicBegin (s);
        assert (invariant (s));
        if (DEBUG)
                fprintf (stderr, "enter ok\n");
}

static void leave (GC_state s) {
        if (DEBUG)
                fprintf (stderr, "leave\n");
        /* The mutator frontier invariant may not hold
         * for functions that don't ensureBytesFree.
         */
        assert (mutatorInvariant (s, FALSE, TRUE));
        atomicEnd (s);
        if (DEBUG)
                fprintf (stderr, "leave ok\n");
}

/* ---------------------------------------------------------------- */
/*                              Heaps                               */
/* ---------------------------------------------------------------- */

/* heapDesiredSize (s, l, c) returns the desired heap size for a heap with
 * l bytes live, given that the current heap size is c.
 */
static W32 heapDesiredSize (GC_state s, W64 live, W32 currentSize) {
        W32 res;
        float ratio;

        ratio = (float)s->ram / (float)live;
        if (ratio >= s->liveRatio + s->growRatio) {
                /* Cheney copying fits in RAM with desired liveRatio. */
                res = live * s->liveRatio;
                /* If the heap is currently close in size to what we want, leave
                 * it alone.  Favor growing over shrinking.
                 */
                unless (res >= 1.1 * currentSize 
                                or res <= .5 * currentSize)
                        res = currentSize;
        } else if (s->growRatio >= s->copyRatio
                        and ratio >= 2 * s->copyRatio) {
                /* Split RAM in half.  Round down by pageSize so that the total
                 * amount of space taken isn't greater than RAM once rounding
                 * happens.  This is so resizeHeap2 doesn't get confused and
                 * free a semispace in a misguided attempt to avoid paging.
                 */
                res = roundDown (s->ram / 2, s->pageSize) ;
        } else if (ratio >= s->copyRatio + s->growRatio) {
                /* Cheney copying fits in RAM. */
                res = s->ram - s->growRatio * live;
                /* If the heap isn't too much smaller than what we want, leave
                 * it alone.  On the other hand, if it is bigger we want to
                 * leave res as is so that the heap is shrunk, to try to avoid
                 * paging.
                 */
                if (0.9 * res <= currentSize and currentSize <= res)
                        res = currentSize;
        } else if (ratio >= s->markCompactRatio) {
                /* Mark compact fits in ram.  It doesn't matter what the current
                 * size is.  If the heap is currently smaller, we are using
                 * copying and should switch to mark-compact.  If the heap is
                 * currently bigger, we want to shrink back to ram size to avoid
                 * paging.
                 */
                res = s->ram;
        } else { /* Required live ratio. */
                res = live * s->markCompactRatio;
                /* If the current heap is bigger than res, the shrinking always
                 * sounds like a good idea.  However, depending on what pages
                 * the VM keeps around, growing could be very expensive, if it
                 * involves paging the entire heap.  Hopefully the copy loop
                 * in growFromSpace will make the right thing happen.
                 */ 
        }
        if (s->fixedHeap > 0) {
                if (res > s->fixedHeap / 2)
                        res = s->fixedHeap;
                else
                        res = s->fixedHeap / 2;
                if (res < live)
                        die ("Out of memory with fixed heap size %s.",
                                uintToCommaString (s->fixedHeap));
        } else if (s->maxHeap > 0) {
                if (res > s->maxHeap)
                        res = s->maxHeap;
                if (res < live)
                        die ("Out of memory with max heap size %s.",
                                uintToCommaString (s->maxHeap));
        }
        if (DEBUG_RESIZING)
                fprintf (stderr, "%s = heapDesiredSize (%s)\n",
                                uintToCommaString (res),
                                ullongToCommaString (live));
        assert (res >= live);
        return res;
}

static inline void heapInit (GC_heap h) {
        h->size = 0;
        h->start = NULL;
}

static inline bool heapIsInit (GC_heap h) {
        return 0 == h->size;
}

static void heapRelease (GC_state s, GC_heap h) {
        if (NULL == h->start)
                return;
        if (DEBUG or s->messages)
                fprintf (stderr, "Releasing heap at 0x%08x of size %s.\n", 
                                (uint)h->start, 
                                uintToCommaString (h->size));
        GC_release (h->start, h->size);
        heapInit (h);
}

static void heapShrink (GC_state s, GC_heap h, W32 keep) {
        assert (keep <= h->size);
        if (0 == keep) {
                heapRelease (s, h);
                return;
        }
        keep = align (keep, s->pageSize);
        if (keep < h->size) {
                if (DEBUG or s->messages)
                        fprintf (stderr, 
                                "Shrinking heap at 0x%08x of size %s to %s bytes.\n",
                                (uint)h->start, 
                                uintToCommaString (h->size),
                                uintToCommaString (keep));
                GC_decommit (h->start + keep, h->size - keep);
                h->size = keep;
        }
}

static void clearCardMap (GC_state s) {
        memset (s->cardMap, 0, s->cardMapSize);
}

static void setNursery (GC_state s, W32 oldGenBytesRequested,
                                W32 nurseryBytesRequested) {
        GC_heap h;
        uint nurserySize;

        if (DEBUG_DETAILED)
                fprintf (stderr, "setNursery.  oldGenBytesRequested = %s  frontier = 0x%08x\n",  
                                uintToCommaString (oldGenBytesRequested),
                                (uint)s->frontier);
        h = &s->heap;
        assert (isAlignedFrontier (s, h->start + s->oldGenSize 
                                        + oldGenBytesRequested));
        nurserySize = h->size - s->oldGenSize - oldGenBytesRequested;
        s->limitPlusSlop = h->start + h->size;
        s->limit = s->limitPlusSlop - LIMIT_SLOP;
        assert (isAligned (nurserySize, WORD_SIZE));
        if (    /* The mutator marks cards. */
                s->mutatorMarksCards
                /* There is enough space in the nursery. */
                and (nurseryBytesRequested 
                        <= s->limitPlusSlop
                                - alignFrontier (s, s->limitPlusSlop
                                                        - nurserySize/2 + 2))
                /* The nursery is large enough to be worth it. */
                and (((float)(h->size - s->bytesLive) 
                        / (float)nurserySize) <= s->nurseryRatio)
                and /* There is a reason to use generational GC. */
                (
                /* We must use it for debugging pruposes. */
                FORCE_GENERATIONAL
                /* We just did a mark compact, so it will be advantageous to
                 * to use it.
                 */
                or (s->lastMajor == GC_MARK_COMPACT)
                /* The live ratio is low enough to make it worthwhile. */
                or (float)h->size / (float)s->bytesLive 
                        <= (h->size < s->ram
                                ? s->copyGenerationalRatio
                                : s->markCompactGenerationalRatio)
                )) {
                s->canMinor = TRUE;
                nurserySize /= 2;
                unless (isAligned (nurserySize, WORD_SIZE))
                        nurserySize -= 2;
                clearCardMap (s);
        } else {
                unless (nurseryBytesRequested 
                                <= s->limitPlusSlop
                                        - alignFrontier (s, s->limitPlusSlop
                                                                - nurserySize))
                        die ("Out of memory.  Insufficient space in nursery.");
                s->canMinor = FALSE;
        }
        assert (nurseryBytesRequested 
                        <= s->limitPlusSlop
                                - alignFrontier (s, s->limitPlusSlop 
                                                        - nurserySize));
        s->nursery = alignFrontier (s, s->limitPlusSlop - nurserySize);
        s->frontier = s->nursery;
        assert (nurseryBytesRequested <= s->limitPlusSlop - s->frontier);
        assert (isAlignedFrontier (s, s->nursery));
        assert (hasBytesFree (s, oldGenBytesRequested, nurseryBytesRequested));
}

static inline void clearCrossMap (GC_state s) {
        if (DEBUG_GENERATIONAL and DEBUG_DETAILED)
                fprintf (stderr, "clearCrossMap ()\n");
        s->crossMapValidSize = 0;
        memset (s->crossMap, CROSS_MAP_EMPTY, s->crossMapSize);
}

static void setCardMapForMutator (GC_state s) {
        unless (s->mutatorMarksCards)
                return;
        /* It's OK if the subtraction below underflows because all the 
         * subsequent additions to mark the cards will overflow and put us
         * in the right place.
         */
        s->cardMapForMutator = s->cardMap - divCardSize (s, (uint)s->heap.start);
        if (DEBUG_CARD_MARKING)
                fprintf (stderr, "cardMapForMutator = 0x%08x\n",
                                (uint)s->cardMapForMutator);
}

static void createCardMapAndCrossMap (GC_state s) {
        GC_heap h;

        unless (s->mutatorMarksCards) {
                s->cardMapSize = 0;
                s->cardMap = NULL;
                s->cardMapForMutator = NULL;
                s->crossMapSize = 0;
                s->crossMap = NULL;
                return;
        }
        h = &s->heap;
        assert (isAligned (h->size, s->cardSize));
        s->cardMapSize = align (divCardSize (s, h->size), s->pageSize);
        s->crossMapSize = s->cardMapSize;
        if (DEBUG_MEM)
                fprintf (stderr, "Creating card/cross map of size %s\n",
                                uintToCommaString
                                        (s->cardMapSize + s->crossMapSize));
        s->cardMap = smmap (s->cardMapSize + s->crossMapSize);
        s->crossMap = (uchar *)s->cardMap + s->cardMapSize;
        if (DEBUG_CARD_MARKING)
                fprintf (stderr, "cardMap = 0x%08x  crossMap = 0x%08x\n", 
                                (uint)s->cardMap,
                                (uint)s->crossMap);
        setCardMapForMutator (s);
        clearCrossMap (s);
}

/* heapCreate (s, h, need, minSize) allocates a heap of the size necessary to
 * work with need live data, and ensures that at least minSize is available.
 * It returns TRUE if it is able to allocate the space, and returns FALSE if it
 * is unable.  If a reasonable size to space is already there, then heapCreate
 * leaves it.
 */
static bool heapCreate (GC_state s, GC_heap h, W32 desiredSize, W32 minSize) {
        W32 backoff;

        if (DEBUG_MEM)
                fprintf (stderr, "heapCreate  desired size = %s  min size = %s\n",
                                uintToCommaString (desiredSize),
                                uintToCommaString (minSize));
        assert (heapIsInit (h));
        if (desiredSize < minSize)
                desiredSize = minSize;
        desiredSize = align (desiredSize, s->pageSize);
        assert (0 == h->size and NULL == h->start);
        backoff = (desiredSize - minSize) / 20;
        if (0 == backoff)
                backoff = 1; /* enough to terminate the loop below */
        backoff = align (backoff, s->pageSize);
        /* mmap toggling back and forth between high and low addresses to
         * decrease the chance of virtual memory fragmentation causing an mmap
         * to fail.  This is important for large heaps.
         */
        for (h->size = desiredSize; h->size >= minSize; h->size -= backoff) {
                static int direction = 1;
                int i;

                assert (isAligned (h->size, s->pageSize));
                for (i = 0; i < 32; i++) {
                        unsigned long address;

                        address = i * 0x08000000ul;
                        if (direction)
                                address = 0xf8000000ul - address;
                        h->start = GC_mmapAnon ((void*)address, h->size);
                        if ((void*)-1 == h->start)
                                h->start = (void*)NULL;
                        unless ((void*)NULL == h->start) {
                                direction = (0 == direction);
                                if (h->size > s->maxHeapSizeSeen)
                                        s->maxHeapSizeSeen = h->size;
                                if (DEBUG or s->messages)
                                        fprintf (stderr, "Created heap of size %s at 0x%08x.\n",
                                                        uintToCommaString (h->size),
                                                        (uint)h->start);
                                assert (h->size >= minSize);
                                return TRUE;
                        }
                }
                if (s->messages)
                        fprintf(stderr, "[Requested %luM cannot be satisfied, backing off by %luM (min size = %luM).\n",
                                meg (h->size), meg (backoff), meg (minSize));
        }
        h->size = 0;
        return FALSE;
}

static inline uint objectSize (GC_state s, pointer p) {
        Bool hasIdentity;
        uint headerBytes, objectBytes;
        word header;
        uint tag, numPointers, numNonPointers;

        header = GC_getHeader (p);
        SPLIT_HEADER();
        if (NORMAL_TAG == tag) { /* Fixed size object. */
                headerBytes = GC_NORMAL_HEADER_SIZE;
                objectBytes = toBytes (numPointers + numNonPointers);
        } else if (ARRAY_TAG == tag) {
                headerBytes = GC_ARRAY_HEADER_SIZE;
                objectBytes = arrayNumBytes (s, p, numPointers, numNonPointers);
        } else if (WEAK_TAG == tag) {
                headerBytes = GC_NORMAL_HEADER_SIZE;
                objectBytes = sizeof (struct GC_weak);
        } else { /* Stack. */
                assert (STACK_TAG == tag);
                headerBytes = STACK_HEADER_SIZE;
                objectBytes = sizeof (struct GC_stack) + ((GC_stack)p)->reserved;
        }
        return headerBytes + objectBytes;
}

/* ---------------------------------------------------------------- */
/*                    Cheney Copying Collection                     */
/* ---------------------------------------------------------------- */

/* forward (s, pp) forwards the object pointed to by *pp and updates *pp to 
 * point to the new object. 
 * It also updates the crossMap.
 */
static inline void forward (GC_state s, pointer *pp) {
        pointer p;
        word header;
        word tag;

        if (DEBUG_DETAILED)
                fprintf (stderr, "forward  pp = 0x%x  *pp = 0x%x\n", (uint)pp, *(uint*)pp);
        assert (isInFromSpace (s, *pp));
        p = *pp;
        header = GC_getHeader (p);
        if (DEBUG_DETAILED and FORWARDED == header)
                fprintf (stderr, "already FORWARDED\n");
        if (header != FORWARDED) { /* forward the object */
                Bool hasIdentity;
                uint headerBytes, objectBytes, size, skip;
                uint numPointers, numNonPointers;

                /* Compute the space taken by the header and object body. */
                SPLIT_HEADER();
                if (NORMAL_TAG == tag) { /* Fixed size object. */
                        headerBytes = GC_NORMAL_HEADER_SIZE;
                        objectBytes = toBytes (numPointers + numNonPointers);
                        skip = 0;
                } else if (ARRAY_TAG == tag) {
                        headerBytes = GC_ARRAY_HEADER_SIZE;
                        objectBytes = arrayNumBytes (s, p, numPointers,
                                                        numNonPointers);
                        skip = 0;
                } else if (WEAK_TAG == tag) {
                        headerBytes = GC_NORMAL_HEADER_SIZE;
                        objectBytes = sizeof (struct GC_weak);
                        skip = 0;
                } else { /* Stack. */
                        GC_stack stack;

                        assert (STACK_TAG == tag);
                        headerBytes = STACK_HEADER_SIZE;
                        stack = (GC_stack)p;

                        if (s->currentThread->stack == stack) {
                                /* Shrink stacks that don't use a lot 
                                 * of their reserved space;
                                 * but don't violate the stack invariant.
                                 */
                                if (stack->used <= stack->reserved / 4) {
                                        uint new = stackReserved (s, max (stack->reserved / 2,
                                                                                stackNeedsReserved (s, stack)));
                                        /* It's possible that new > stack->reserved if
                                         * the stack invariant is violated. In that case, 
                                         * we want to leave the stack alone, because some 
                                         * other part of the gc will grow the stack.  We 
                                         * cannot do any growing here because we may run 
                                         * out of to space.
                                         */
                                        if (new <= stack->reserved) {
                                                stack->reserved = new;
                                                if (DEBUG_STACKS)
                                                        fprintf (stderr, "Shrinking stack to size %s.\n",
                                                                        uintToCommaString (stack->reserved));
                                        }
                                }
                        } else {
                                /* Shrink heap stacks.
                                 */
                                stack->reserved = stackReserved (s, max(s->threadShrinkRatio * stack->reserved, 
                                                                        stack->used));
                                if (DEBUG_STACKS)
                                        fprintf (stderr, "Shrinking stack to size %s.\n",
                                                        uintToCommaString (stack->reserved));
                        }
                        objectBytes = sizeof (struct GC_stack) + stack->used;
                        skip = stack->reserved - stack->used;
                }
                size = headerBytes + objectBytes;
                assert (s->back + size + skip <= s->toLimit);
                /* Copy the object. */
                copy (p - headerBytes, s->back, size);
                /* If the object has a valid weak pointer, link it into the weaks
                 * for update after the copying GC is done.
                 */
                if (WEAK_TAG == tag and 1 == numPointers) {
                        GC_weak w;

                        w = (GC_weak)(s->back + GC_NORMAL_HEADER_SIZE);
                        if (DEBUG_WEAK)
                                fprintf (stderr, "forwarding weak 0x%08x ",
                                                (uint)w);
                        if (GC_isPointer (w->object)
                                and (not s->amInMinorGC
                                        or isInNursery (s, w->object))) {
                                if (DEBUG_WEAK)
                                        fprintf (stderr, "linking\n");
                                w->link = s->weaks;
                                s->weaks = w;
                        } else {
                                if (DEBUG_WEAK)
                                        fprintf (stderr, "not linking\n");
                        }
                }
                /* Store the forwarding pointer in the old object. */
                *(word*)(p - WORD_SIZE) = FORWARDED;
                *(pointer*)p = s->back + headerBytes;
                /* Update the back of the queue. */
                s->back += size + skip;
                assert (isAligned ((uint)s->back + GC_NORMAL_HEADER_SIZE,
                                        s->alignment));
        }
        *pp = *(pointer*)p;
        assert (isInToSpace (s, *pp));
}

static void updateWeaks (GC_state s) {
        GC_weak w;

        for (w = s->weaks; w != NULL; w = w->link) {
                assert ((pointer)BOGUS_POINTER != w->object);

                if (DEBUG_WEAK)
                        fprintf (stderr, "updateWeaks  w = 0x%08x  ", (uint)w);
                if (FORWARDED == GC_getHeader ((pointer)w->object)) {
                        if (DEBUG_WEAK)
                                fprintf (stderr, "forwarded from 0x%08x to 0x%08x\n",
                                                (uint)w->object,
                                                (uint)*(pointer*)w->object);
                        w->object = *(pointer*)w->object;
                } else {
                        if (DEBUG_WEAK)
                                fprintf (stderr, "cleared\n");
                        *(GC_getHeaderp((pointer)w)) = WEAK_GONE_HEADER;
                        w->object = (pointer)BOGUS_POINTER;
                }
        }
        s->weaks = NULL;
}

static void swapSemis (GC_state s) {
        struct GC_heap h;

        h = s->heap2;
        s->heap2 = s->heap;
        s->heap = h;
        setCardMapForMutator (s);
}

static inline bool detailedGCTime (GC_state s) {
        return s->summary;
}

static void cheneyCopy (GC_state s) {
        struct rusage ru_start;
        pointer toStart;

        assert (s->heap2.size >= s->oldGenSize);
        if (detailedGCTime (s))
                startTiming (&ru_start);
        s->numCopyingGCs++;
        s->toSpace = s->heap2.start;
        s->toLimit = s->heap2.start + s->heap2.size;
        if (DEBUG or s->messages) {
                fprintf (stderr, "Major copying GC.\n");
                fprintf (stderr, "fromSpace = 0x%08x of size %s\n", 
                                (uint) s->heap.start,
                                uintToCommaString (s->heap.size));
                fprintf (stderr, "toSpace = 0x%08x of size %s\n",
                                (uint) s->heap2.start,
                                uintToCommaString (s->heap2.size));
        }
        assert (s->heap2.start != (void*)NULL);
        /* The next assert ensures there is enough space for the copy to succeed.
         * It does not assert (s->heap2.size >= s->heap.size) because that
         * is too strong.
         */
        assert (s->heap2.size >= s->oldGenSize);
        toStart = alignFrontier (s, s->heap2.start);
        s->back = toStart;
        foreachGlobal (s, forward);
        foreachPointerInRange (s, toStart, &s->back, TRUE, forward);
        updateWeaks (s);
        s->oldGenSize = s->back - s->heap2.start;
        s->bytesCopied += s->oldGenSize;
        if (DEBUG)
                fprintf (stderr, "%s bytes live.\n", 
                                uintToCommaString (s->oldGenSize));
        swapSemis (s);
        clearCrossMap (s);
        s->lastMajor = GC_COPYING;
        if (detailedGCTime (s))
                stopTiming (&ru_start, &s->ru_gcCopy);          
        if (DEBUG or s->messages)
                fprintf (stderr, "Major copying GC done.\n");
}

/* ---------------------------------------------------------------- */
/*                     Minor copying collection                     */
/* ---------------------------------------------------------------- */

#if ASSERT

static inline pointer crossMapCardStart (GC_state s, pointer p) {
        /* The p - 1 is so that a pointer to the beginning of a card
         * falls into the index for the previous crossMap entry.
         */
        return (p == s->heap.start)
                ? s->heap.start
                : (p - 1) - ((uint)(p - 1) % s->cardSize);
}

/* crossMapIsOK is a slower, but easier to understand, way of computing the
 * crossMap.  updateCrossMap (below) incrementally updates the crossMap, checking
 * only the part of the old generation that it hasn't seen before.  crossMapIsOK
 * simply walks through the entire old generation.  It is useful to check that
 * the incremental update is working correctly.
 */
static bool crossMapIsOK (GC_state s) {
        pointer back;
        uint cardIndex;
        pointer cardStart;
        pointer front;
        uint i;
        static uchar *m;

        if (DEBUG)
                fprintf (stderr, "crossMapIsOK ()\n");
        m = smmap (s->crossMapSize);
        memset (m, CROSS_MAP_EMPTY, s->crossMapSize);
        back = s->heap.start + s->oldGenSize;
        cardIndex = 0;
        front = alignFrontier (s, s->heap.start);
loopObjects:
        assert (front <= back);
        cardStart = crossMapCardStart (s, front);
        cardIndex = divCardSize (s, cardStart - s->heap.start);
        m[cardIndex] = (front - cardStart) / WORD_SIZE;
        if (front < back) {
                front += objectSize (s, toData (s, front));
                goto loopObjects;
        }
        for (i = 0; i < cardIndex; ++i)
                assert (m[i] == s->crossMap[i]);
        GC_release (m, s->crossMapSize);
        return TRUE;
}

#endif /* ASSERT */

static void updateCrossMap (GC_state s) {
        GC_heap h;
        pointer cardEnd;
        uint cardIndex;
        pointer cardStart;
        pointer next;
        pointer objectStart;
        pointer oldGenEnd;

        h = &(s->heap);
        if (s->crossMapValidSize == s->oldGenSize)
                goto done;
        oldGenEnd = h->start + s->oldGenSize;
        objectStart = h->start + s->crossMapValidSize;
        if (objectStart == h->start) {
                cardIndex = 0;
                objectStart = alignFrontier (s, objectStart);
        } else
                cardIndex = divCardSize (s, (uint)(objectStart - 1 - h->start));
        cardStart = h->start + cardNumToSize (s, cardIndex);
        cardEnd = cardStart + s->cardSize;
loopObjects:
        assert (objectStart < oldGenEnd);
        assert ((objectStart == h->start or cardStart < objectStart)
                        and objectStart <= cardEnd);
        next = objectStart + objectSize (s, toData (s, objectStart));
        if (next > cardEnd) {
                /* We're about to move to a new card, so we are looking at the
                 * last object boundary in the current card.  Store it in the 
                 * crossMap.
                 */
                uint offset;

                offset = (objectStart - cardStart) / WORD_SIZE;
                assert (offset < CROSS_MAP_EMPTY);
                if (DEBUG_GENERATIONAL)
                        fprintf (stderr, "crossMap[%u] = %u\n", 
                                        cardIndex, offset);
                s->crossMap[cardIndex] = offset;
                cardIndex = divCardSize (s, next - 1 - h->start);
                cardStart = h->start + cardNumToSize (s, cardIndex);
                cardEnd = cardStart + s->cardSize;
        }
        objectStart = next;
        if (objectStart < oldGenEnd) 
                goto loopObjects;
        assert (objectStart == oldGenEnd);
        s->crossMap[cardIndex] = (oldGenEnd - cardStart) / WORD_SIZE;
        s->crossMapValidSize = s->oldGenSize;
done:
        assert (s->crossMapValidSize == s->oldGenSize);
        assert (crossMapIsOK (s));
}

static inline void forwardIfInNursery (GC_state s, pointer *pp) {
        pointer p;

        p = *pp;
        if (p < s->nursery)
                return;
        if (DEBUG_GENERATIONAL)
                fprintf (stderr, "intergenerational pointer from 0x%08x to 0x%08x\n",
                        (uint)pp, *(uint*)pp);
        assert (s->nursery <= p and p < s->limitPlusSlop);
        forward (s, pp);
}


/* Walk through all the cards and forward all intergenerational pointers. */
static void forwardInterGenerationalPointers (GC_state s) {
        pointer cardMap;
        uint cardNum;
        pointer cardStart;
        uchar *crossMap;
        GC_heap h;
        uint numCards;
        pointer objectStart;
        pointer oldGenStart;
        pointer oldGenEnd;

        if (DEBUG_GENERATIONAL)
                fprintf (stderr, "Forwarding inter-generational pointers.\n");
        updateCrossMap (s);
        h = &s->heap;
        /* Constants. */
        cardMap = s->cardMap;
        crossMap = s->crossMap;
        numCards = divCardSize (s, align (s->oldGenSize, s->cardSize));
        oldGenStart = s->heap.start;
        oldGenEnd = oldGenStart + s->oldGenSize;
        /* Loop variables*/
        objectStart = alignFrontier (s, s->heap.start);
        cardNum = 0;
        cardStart = oldGenStart;
checkAll:
        assert (cardNum <= numCards);
        assert (isAlignedFrontier (s, objectStart));
        if (cardNum == numCards)
                goto done;
checkCard:
        if (DEBUG_GENERATIONAL)
                fprintf (stderr, "checking card %u  objectStart = 0x%08x  cardEnd = 0x%08x\n",
                                cardNum, 
                                (uint)objectStart,
                                (uint)oldGenStart + cardNumToSize (s, cardNum + 1));
        assert (objectStart < oldGenStart + cardNumToSize (s, cardNum + 1));
        if (cardMap[cardNum]) {
                pointer cardEnd;
                pointer orig;
                uint size;

                s->markedCards++;
                if (DEBUG_GENERATIONAL)
                        fprintf (stderr, "card %u is marked  objectStart = 0x%08x\n", 
                                        cardNum, (uint)objectStart);
                orig = objectStart;
skipObjects:
                assert (isAlignedFrontier (s, objectStart));
                size = objectSize (s, toData (s, objectStart));
                if (objectStart + size < cardStart) {
                        objectStart += size;
                        goto skipObjects;
                }
                s->minorBytesSkipped += objectStart - orig;
                cardEnd = cardStart + s->cardSize;
                if (oldGenEnd < cardEnd) 
                        cardEnd = oldGenEnd;
                assert (objectStart < cardEnd);
                orig = objectStart;
                /* If we ever add Weak.set, then there could be intergenerational
                 * weak pointers, in which case we would need to link the weak
                 * objects into s->weaks.  But for now, since there is no 
                 * Weak.set, the foreachPointerInRange will do the right thing
                 * on weaks, since the weak pointer will never be into the 
                 * nursery.
                 */
                objectStart = 
                        foreachPointerInRange (s, objectStart, &cardEnd, FALSE,
                                                forwardIfInNursery);
                s->minorBytesScanned += objectStart - orig;
                if (objectStart == oldGenEnd)
                        goto done;
                cardNum = divCardSize (s, objectStart - oldGenStart);
                cardStart = oldGenStart + cardNumToSize (s, cardNum);
                goto checkCard;
        } else {
                unless (CROSS_MAP_EMPTY == crossMap[cardNum])
                        objectStart = cardStart + crossMap[cardNum] * WORD_SIZE;
                if (DEBUG_GENERATIONAL)
                        fprintf (stderr, "card %u is not marked  crossMap[%u] == %u  objectStart = 0x%08x\n", 
                                        cardNum,
                                        cardNum, 
                                        crossMap[cardNum] * WORD_SIZE,
                                        (uint)objectStart);
                cardNum++;
                cardStart += s->cardSize;
                goto checkAll;
        }
        assert (FALSE);
done:
        if (DEBUG_GENERATIONAL)
                fprintf (stderr, "Forwarding inter-generational pointers done.\n");
}

static void minorGC (GC_state s) {
        W32 bytesAllocated;
        W32 bytesCopied;
        struct rusage ru_start;

        if (DEBUG_GENERATIONAL)
                fprintf (stderr, "minorGC  nursery = 0x%08x  frontier = 0x%08x\n", 
                                (uint)s->nursery,
                                (uint)s->frontier);
        assert (invariant (s));
        bytesAllocated = s->frontier - s->nursery;
        if (bytesAllocated == 0)
                return;
        s->bytesAllocated += bytesAllocated;
        if (not s->canMinor) {
                s->oldGenSize += bytesAllocated;
                bytesCopied = 0;
        } else {
                if (DEBUG_GENERATIONAL or s->messages)
                        fprintf (stderr, "Minor GC.\n");
                if (detailedGCTime (s))
                        startTiming (&ru_start);
                s->amInMinorGC = TRUE;
                s->toSpace = s->heap.start + s->oldGenSize;
                if (DEBUG_GENERATIONAL)
                        fprintf (stderr, "toSpace = 0x%08x\n",
                                        (uint)s->toSpace);
                assert (isAlignedFrontier (s, s->toSpace));
                s->toLimit = s->toSpace + bytesAllocated;
                assert (invariant (s));
                s->numMinorGCs++;
                s->numMinorsSinceLastMajor++;
                s->back = s->toSpace;
                /* Forward all globals.  Would like to avoid doing this once all
                 * the globals have been assigned.
                 */
                foreachGlobal (s, forwardIfInNursery);
                forwardInterGenerationalPointers (s);
                foreachPointerInRange (s, s->toSpace, &s->back, TRUE,
                                        forwardIfInNursery);
                updateWeaks (s);
                bytesCopied = s->back - s->toSpace;
                s->bytesCopiedMinor += bytesCopied;
                s->oldGenSize += bytesCopied;
                s->amInMinorGC = FALSE;
                if (detailedGCTime (s))
                        stopTiming (&ru_start, &s->ru_gcMinor);
                if (DEBUG_GENERATIONAL or s->messages)
                        fprintf (stderr, "Minor GC done.  %s bytes copied.\n",
                                        uintToCommaString (bytesCopied));
        }
}

/* ---------------------------------------------------------------- */
/*                       Object hash consing                        */
/* ---------------------------------------------------------------- */

/* Hashing based on Introduction to Algorithms by Cormen Leiserson, and Rivest.
 * Section numbers in parens.
 * k is key to be hashed.
 * table is of size 2^p  (it must be a power of two)
 * Open addressing (12.4), meaning that we stick the entries directly in the 
 *   table and probe until we find what we want.
 * Multiplication method (12.3.2), meaning that we compute the hash by 
 *   multiplying by a magic number, chosen by Knuth, and take the high-order p
 *   bits of the low order 32 bits.
 * Double hashing (12.4), meaning that we use two hash functions, the first to
 *   decide where to start looking and a second to decide at what offset to
 *   probe.  The second hash must be relatively prime to the table size, which
 *   we ensure by making it odd and keeping the table size as a power of 2.
 */

static GC_ObjectHashTable newTable (GC_state s) {
        int i;
        uint maxElementsSize;
        pointer regionStart;
        pointer regionEnd;
        GC_ObjectHashTable t;

        NEW (GC_ObjectHashTable, t);
        // Try to use space in the heap for the elements.
        if (not (heapIsInit (&s->heap2))) {
                if (DEBUG_SHARE)
                        fprintf (stderr, "using heap2\n");
                // We have all of heap2 available.  Use it.
                regionStart = s->heap2.start;
                regionEnd = s->heap2.start + s->heap2.size;
        } else if (s->amInGC or not s->canMinor) {
                if (DEBUG_SHARE)
                        fprintf (stderr, "using end of heap\n");
                regionStart = s->frontier;
                regionEnd = s->limitPlusSlop;
        } else {
                if (DEBUG_SHARE)
                        fprintf (stderr, "using minor space\n");
                // Use the space available for a minor GC.
                assert (s->canMinor);
                regionStart = s->heap.start + s->oldGenSize;
                regionEnd = s->nursery;
        }
        maxElementsSize = (regionEnd - regionStart) / sizeof (*(t->elements));
        if (DEBUG_SHARE)
                fprintf (stderr, "maxElementsSize = %u\n", maxElementsSize);
        t->elementsSize = 64;  // some small power of two
        t->log2ElementsSize = 6;  // and its log base 2
        if (maxElementsSize < t->elementsSize) {
                if (DEBUG_SHARE)
                        fprintf (stderr, "too small -- using malloc\n");
                t->elementsIsInHeap = FALSE;
                ARRAY (struct GC_ObjectHashElement *, t->elements, t->elementsSize);
        } else {
                t->elementsIsInHeap = TRUE;
                t->elements = (struct GC_ObjectHashElement*)regionStart;
                // Find the largest power of two that fits.
                for (; t->elementsSize <= maxElementsSize; 
                        t->elementsSize <<= 1, t->log2ElementsSize++)
                        ; // nothing
                t->elementsSize >>= 1;
                t->log2ElementsSize--;
                assert (t->elementsSize <= maxElementsSize);
                for (i = 0; i < t->elementsSize; ++i)
                        t->elements[i].object = NULL;
        }
        t->numElements = 0;
        t->mayInsert = TRUE;
        if (DEBUG_SHARE) {
                fprintf (stderr, "elementsIsInHeap = %s\n", 
                                boolToString (t->elementsIsInHeap));
                fprintf (stderr, "elementsSize = %u\n", t->elementsSize);
                fprintf (stderr, "0x%08x = newTable ()\n", (uint)t);
        }
        return t;
}

static void destroyTable (GC_ObjectHashTable t) {
        unless (t->elementsIsInHeap)
                free (t->elements);
        free (t);
}

static inline Pointer tableInsert 
        (GC_state s, GC_ObjectHashTable t, W32 hash, Pointer object, 
                Bool mightBeThere, Header header, W32 tag, Pointer max) {
        GC_ObjectHashElement e;
        Header header2;
        static Bool init = FALSE;
        static int maxNumProbes = 0;
        static W64 mult; // magic multiplier for hashing
        int numProbes;
        W32 probe;
        word *p;
        word *p2;
        W32 slot; // slot in hash table we are considering

        if (DEBUG_SHARE)
                fprintf (stderr, "tableInsert (%u, 0x%08x, %s, 0x%08x, 0x%08x)\n",
                                (uint)hash, (uint)object, 
                                boolToString (mightBeThere),
                                (uint)header, (uint)max);
        if (! init) {
                init = TRUE;
                mult = floor (((sqrt (5.0) - 1.0) / 2.0)
                                * (double)0x100000000llu);
        }
        slot = (W32)(mult * (W64)hash) >> (32 - t->log2ElementsSize);
        probe = (1 == slot % 2) ? slot : slot - 1;
        if (DEBUG_SHARE)
                fprintf (stderr, "probe = 0x%08x\n", (uint)probe);
        assert (1 == probe % 2);
        numProbes = 0;
look:
        if (DEBUG_SHARE)
                fprintf (stderr, "slot = 0x%08x\n", (uint)slot);
        assert (0 <= slot and slot < t->elementsSize);
        numProbes++;
        e = &t->elements[slot];
        if (NULL == e->object) {
                /* It's not in the table.  Add it. */
                unless (t->mayInsert) {
                        if (DEBUG_SHARE)
                                fprintf (stderr, "not inserting\n");
                        return object;
                }
                e->hash = hash;
                e->object = object;
                t->numElements++;
                if (numProbes > maxNumProbes) {
                        maxNumProbes = numProbes;
                        if (DEBUG_SHARE)
                                fprintf (stderr, "numProbes = %d\n", numProbes);
                }
                return object;
        }
        unless (hash == e->hash) {
lookNext:
                slot = (slot + probe) % t->elementsSize;
                goto look;
        }
        unless (mightBeThere)
                goto lookNext;
        if (DEBUG_SHARE)
                fprintf (stderr, "comparing 0x%08x to 0x%08x\n",
                                (uint)object, (uint)e->object);
        /* Compare object to e->object. */
        unless (object == e->object) {
                header2 = GC_getHeader (e->object);
                unless (header == header2)
                        goto lookNext;
                for (p = (word*)object, p2 = (word*)e->object; 
                                p < (word*)max; 
                                ++p, ++p2)
                        unless (*p == *p2)
                                goto lookNext;
                if (ARRAY_TAG == tag
                        and (GC_arrayNumElements (object)
                                != GC_arrayNumElements (e->object)))
                        goto lookNext;
        }
        /* object is equal to e->object. */
        return e->object;
}

static void maybeGrowTable (GC_state s, GC_ObjectHashTable t) { 
        int i;
        GC_ObjectHashElement oldElement;
        struct GC_ObjectHashElement *oldElements;
        uint oldSize;
        uint newSize;

        if (not t->mayInsert or t->numElements * 2 <= t->elementsSize)
                return;
        oldElements = t->elements;
        oldSize = t->elementsSize;
        newSize = oldSize * 2;
        if (DEBUG_SHARE)
                fprintf (stderr, "trying to grow table to size %d\n", newSize);
        // Try to alocate the new table.
        ARRAY_UNSAFE (struct GC_ObjectHashElement *, t->elements, newSize);
        if (NULL == t->elements) {
                t->mayInsert = FALSE;
                t->elements = oldElements;
                if (DEBUG_SHARE)
                        fprintf (stderr, "unable to grow table\n");
                return;
        }
        t->elementsSize = newSize;
        t->log2ElementsSize++;
        for (i = 0; i < oldSize; ++i) {
                oldElement = &oldElements[i];
                unless (NULL == oldElement->object)
                        tableInsert (s, t, oldElement->hash, oldElement->object,
                                        FALSE, 0, 0, 0);
        }
        if (t->elementsIsInHeap)
                t->elementsIsInHeap = FALSE;
        else
                free (oldElements);
        if (DEBUG_SHARE)
                fprintf (stderr, "done growing table\n");
}

static Pointer hashCons (GC_state s, Pointer object, Bool countBytesHashConsed) {
        Bool hasIdentity;
        Word32 hash;
        Header header;
        pointer max;
        uint numNonPointers;
        uint numPointers;
        word *p;
        Pointer res;
        GC_ObjectHashTable t;
        uint tag;

        if (DEBUG_SHARE)
                fprintf (stderr, "hashCons (0x%08x)\n", (uint)object);
        t = s->objectHashTable;
        header = GC_getHeader (object);
        SPLIT_HEADER ();
        if (hasIdentity) {
                /* Don't hash cons. */
                res = object;
                goto done;
        }
        assert (ARRAY_TAG == tag or NORMAL_TAG == tag);
        max = object
                + (ARRAY_TAG == tag
                        ? arrayNumBytes (s, object,
                                                numPointers, numNonPointers)
                        : toBytes (numPointers + numNonPointers));
        // Compute the hash.
        hash = header;
        for (p = (word*)object; p < (word*)max; ++p)
                hash = hash * 31 + *p;
        /* Insert into table. */
        res = tableInsert (s, t, hash, object, TRUE, header, tag, (Pointer)max);
        maybeGrowTable (s, t);
        if (countBytesHashConsed and res != object) {
                uint amount;

                amount = max - object;
                if (ARRAY_TAG == tag)
                        amount += GC_ARRAY_HEADER_SIZE;
                else
                        amount += GC_NORMAL_HEADER_SIZE;
                s->bytesHashConsed += amount;
        }
done:
        if (DEBUG_SHARE)
                fprintf (stderr, "0x%08x = hashCons (0x%08x)\n", 
                                (uint)res, (uint)object);
        return res;
}

static inline void markIntergenerational (GC_state s, Pointer *pp) {
        if (s->mutatorMarksCards
                and isInOldGen (s, (pointer)pp)
                and isInNursery (s, *pp))
                markCard (s, (pointer)pp);
}

static inline void maybeSharePointer (GC_state s,
                                        Pointer *pp, 
                                        Bool shouldHashCons) {
        unless (shouldHashCons)
                return;
        if (DEBUG_SHARE)
                fprintf (stderr, "maybeSharePointer  pp = 0x%08x  *pp = 0x%08x\n",
                                (uint)pp, (uint)*pp);
        *pp = hashCons (s, *pp, FALSE); 
        markIntergenerational (s, pp);
}

/* ---------------------------------------------------------------- */
/*                       Depth-first Marking                        */
/* ---------------------------------------------------------------- */

static inline uint *arrayCounterp (pointer a) {
        return ((uint*)a - 3);
}

static inline uint arrayCounter (pointer a) {
        return *(arrayCounterp (a));
}

static inline bool isMarked (pointer p) {
        return MARK_MASK & GC_getHeader (p);
}

static bool modeEqMark (MarkMode m, pointer p) {
        return (MARK_MODE == m) ? isMarked (p): not isMarked (p);
}

/* mark (s, p, m) sets all the mark bits in the object graph pointed to by p. 
 * If m is MARK_MODE, it sets the bits to 1.
 * If m is UNMARK_MODE, it sets the bits to 0.
 *
 * It returns the total size in bytes of the objects marked.
 */
W32 mark (GC_state s, pointer root, MarkMode mode, Bool shouldHashCons) {
        uint arrayIndex;
        pointer cur;  /* The current object being marked. */
        GC_offsets frameOffsets;
        Bool hasIdentity;
        Header* headerp;
        Header header;
        uint index; /* The i'th pointer in the object (element) being marked. */
        GC_frameLayout *layout;
        Header mark; /* Used to set or clear the mark bit. */
        pointer next; /* The next object to mark. */
        Header *nextHeaderp;
        Header nextHeader;
        uint numNonPointers;
        uint numPointers;
        pointer prev; /* The previous object on the mark stack. */
        W32 size; /* Total number of bytes marked. */
        uint tag;
        pointer todo; /* A pointer to the pointer in cur to next. */
        pointer top; /* The top of the next stack frame to mark. */

        if (modeEqMark (mode, root))
                /* Object has already been marked. */
                return 0;
        mark = (MARK_MODE == mode) ? MARK_MASK : 0;
        size = 0;
        cur = root;
        prev = NULL;
        headerp = GC_getHeaderp (cur);
        header = *(Header*)headerp;
        goto mark;      
markNext:
        /* cur is the object that was being marked.
         * prev is the mark stack.
         * next is the unmarked object to be marked.
         * nextHeaderp points to the header of next.
         * nextHeader is the header of next.
         * todo is a pointer to the pointer inside cur that points to next.
         */
        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "markNext  cur = 0x%08x  next = 0x%08x  prev = 0x%08x  todo = 0x%08x\n",
                                (uint)cur, (uint)next, (uint)prev, (uint)todo);
        assert (not modeEqMark (mode, next));
        assert (nextHeaderp == GC_getHeaderp (next));
        assert (nextHeader == GC_getHeader (next));
        assert (*(pointer*) todo == next);
        headerp = nextHeaderp;
        header = nextHeader;
        *(pointer*)todo = prev;
        prev = cur;
        cur = next;
mark:
        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "mark  cur = 0x%08x  prev = 0x%08x  mode = %s\n",
                                (uint)cur, (uint)prev,
                                (mode == MARK_MODE) ? "mark" : "unmark");
        /* cur is the object to mark. 
         * prev is the mark stack.
         * headerp points to the header of cur.
         * header is the header of cur.
         */
        assert (not modeEqMark (mode, cur));
        assert (header == GC_getHeader (cur));
        assert (headerp == GC_getHeaderp (cur));
        header ^= 0x80000000;
        /* Store the mark.  In the case of an object that contains a pointer to
         * itself, it is essential that we store the marked header before marking
         * the internal pointers (markInNormal below).  If we didn't, then we
         * would see the object as unmarked and traverse it again.
         */
        *headerp = header;
        SPLIT_HEADER();
        if (NORMAL_TAG == tag) {
                if (0 == numPointers) {
                        /* There is nothing to mark. */
                        size += GC_NORMAL_HEADER_SIZE + toBytes (numNonPointers);
normalDone:
                        if (shouldHashCons)
                                cur = hashCons (s, cur, TRUE);
                        goto ret;
                }
                todo = cur + toBytes (numNonPointers);
                size += todo + toBytes (numPointers) - (pointer)headerp;
                index = 0;
markInNormal:
                if (DEBUG_MARK_COMPACT)
                        fprintf (stderr, "markInNormal  index = %d\n", index);
                assert (index < numPointers);
                next = *(pointer*)todo;
                if (not GC_isPointer (next)) {
markNextInNormal:
                        assert (index < numPointers);
                        index++;
                        if (index == numPointers) {
                                *headerp = header & ~COUNTER_MASK;
                                goto normalDone;
                        }
                        todo += POINTER_SIZE;
                        goto markInNormal;
                }
                nextHeaderp = GC_getHeaderp (next);
                nextHeader = *nextHeaderp;
                if (mark == (nextHeader & MARK_MASK)) {
                        maybeSharePointer (s, (pointer*)todo, shouldHashCons);
                        goto markNextInNormal;
                }
                *headerp = (header & ~COUNTER_MASK) |
                                (index << COUNTER_SHIFT);
                goto markNext;
        } else if (WEAK_TAG == tag) {
                /* Store the marked header and don't follow any pointers. */
                goto ret;
        } else if (ARRAY_TAG == tag) {
                /* When marking arrays:
                 *   arrayIndex is the index of the element to mark.
                 *   cur is the pointer to the array.
                 *   index is the index of the pointer within the element
                 *     (i.e. the i'th pointer is at index i).
                 *   todo is the start of the element.
                 */
                size += GC_ARRAY_HEADER_SIZE
                        + arrayNumBytes (s, cur, numPointers, numNonPointers);
                if (0 == numPointers or 0 == GC_arrayNumElements (cur)) {
                        /* There is nothing to mark. */
arrayDone:
                        if (shouldHashCons)
                                cur = hashCons (s, cur, TRUE);
                        goto ret;
                }
                /* Begin marking first element. */
                arrayIndex = 0;
                todo = cur;
markArrayElt:
                assert (arrayIndex < GC_arrayNumElements (cur));
                index = 0;
                /* Skip to the first pointer. */
                todo += numNonPointers;
markInArray:
                if (DEBUG_MARK_COMPACT)
                        fprintf (stderr, "markInArray arrayIndex = %u index = %u\n",
                                        arrayIndex, index);
                assert (arrayIndex < GC_arrayNumElements (cur));
                assert (index < numPointers);
                assert (todo == arrayPointer (s, cur, arrayIndex, index));
                next = *(pointer*)todo;
                if (not (GC_isPointer (next))) {
markNextInArray:
                        assert (arrayIndex < GC_arrayNumElements (cur));
                        assert (index < numPointers);
                        assert (todo == arrayPointer (s, cur, arrayIndex, index));
                        todo += POINTER_SIZE;
                        index++;
                        if (index < numPointers)
                                goto markInArray;
                        arrayIndex++;
                        if (arrayIndex < GC_arrayNumElements (cur))
                                goto markArrayElt;
                        /* Done.  Clear out the counters and return. */
                        *arrayCounterp (cur) = 0;
                        *headerp = header & ~COUNTER_MASK;
                        goto arrayDone;
                }
                nextHeaderp = GC_getHeaderp (next);
                nextHeader = *nextHeaderp;
                if (mark == (nextHeader & MARK_MASK)) {
                        maybeSharePointer (s, (pointer*)todo, shouldHashCons);
                        goto markNextInArray;
                }
                /* Recur and mark next. */
                *arrayCounterp (cur) = arrayIndex;
                *headerp = (header & ~COUNTER_MASK) |
                                (index << COUNTER_SHIFT);
                goto markNext;
        } else {
                assert (STACK_TAG == tag);
                size += stackBytes (s, ((GC_stack)cur)->reserved);
                top = stackTop (s, (GC_stack)cur);
                assert (((GC_stack)cur)->used <= ((GC_stack)cur)->reserved);
markInStack:
                /* Invariant: top points just past the return address of the
                 * frame to be marked.
                 */
                assert (stackBottom (s, (GC_stack)cur) <= top);
                if (DEBUG_MARK_COMPACT)
                        fprintf (stderr, "markInStack  top = %td\n",
                                        top - stackBottom (s, (GC_stack)cur));
                                        
                if (top == stackBottom (s, (GC_stack)(cur)))
                        goto ret;
                index = 0;
                layout = getFrameLayout (s, *(word*) (top - WORD_SIZE));
                frameOffsets = layout->offsets;
                ((GC_stack)cur)->markTop = top;
markInFrame:
                if (index == frameOffsets [0]) {
                        top -= layout->numBytes;
                        goto markInStack;
                }
                todo = top - layout->numBytes + frameOffsets [index + 1];
                next = *(pointer*)todo;
                if (DEBUG_MARK_COMPACT)
                        fprintf (stderr, 
                                "    offset %u  todo 0x%08x  next = 0x%08x\n", 
                                frameOffsets [index + 1], 
                                (uint)todo, (uint)next);
                if (not GC_isPointer (next)) {
                        index++;
                        goto markInFrame;
                }
                nextHeaderp = GC_getHeaderp (next);
                nextHeader = *nextHeaderp;
                if (mark == (nextHeader & MARK_MASK)) {
                        index++;
                        maybeSharePointer (s, (pointer*)todo, shouldHashCons);
                        goto markInFrame;
                }
                ((GC_stack)cur)->markIndex = index;             
                goto markNext;
        }
        assert (FALSE);
ret:
        /* Done marking cur, continue with prev.
         * Need to set the pointer in the prev object that pointed to cur 
         * to point back to prev, and restore prev.
         */
        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "return  cur = 0x%08x  prev = 0x%08x\n",
                                (uint)cur, (uint)prev);
        assert (modeEqMark (mode, cur));
        if (NULL == prev)
                return size;
        next = cur;
        cur = prev;
        headerp = GC_getHeaderp (cur);
        header = *headerp;
        SPLIT_HEADER();
        /* It's impossible to get a WEAK_TAG here, since we would never follow
         * the weak object pointer.
         */
        assert (WEAK_TAG != tag);
        if (NORMAL_TAG == tag) {
                todo = cur + toBytes (numNonPointers);
                index = (header & COUNTER_MASK) >> COUNTER_SHIFT;
                todo += index * POINTER_SIZE;
                prev = *(pointer*)todo;
                *(pointer*)todo = next;
                if (shouldHashCons)
                        markIntergenerational (s, (pointer*)todo);
                goto markNextInNormal;
        } else if (ARRAY_TAG == tag) {
                arrayIndex = arrayCounter (cur);
                todo = cur + arrayIndex * (numNonPointers 
                                                + toBytes (numPointers));
                index = (header & COUNTER_MASK) >> COUNTER_SHIFT;
                todo += numNonPointers + index * POINTER_SIZE;
                prev = *(pointer*)todo;
                *(pointer*)todo = next;
                if (shouldHashCons)
                        markIntergenerational (s, (pointer*)todo);
                goto markNextInArray;
        } else {
                assert (STACK_TAG == tag);
                index = ((GC_stack)cur)->markIndex;
                top = ((GC_stack)cur)->markTop;
                layout = getFrameLayout (s, *(word*) (top - WORD_SIZE));
                frameOffsets = layout->offsets;
                todo = top - layout->numBytes + frameOffsets [index + 1];
                prev = *(pointer*)todo;
                *(pointer*)todo = next;
                if (shouldHashCons)
                        markIntergenerational (s, (pointer*)todo);
                index++;
                goto markInFrame;
        }
        assert (FALSE);
}

static void bytesHashConsedMessage (GC_state s, ullong total) {
        fprintf (stderr, "%s bytes hash consed (%.1f%%).\n",
                ullongToCommaString (s->bytesHashConsed),
                100.0 * ((double)s->bytesHashConsed / (double)total));
}

void GC_share (GC_state s, Pointer object) {
        W32 total;

        if (DEBUG_SHARE)
                fprintf (stderr, "GC_share 0x%08x\n", (uint)object);
        if (DEBUG_SHARE or s->messages)
                s->bytesHashConsed = 0;
        // Don't hash cons during the first round of marking.
        total = mark (s, object, MARK_MODE, FALSE);
        s->objectHashTable = newTable (s);
        // Hash cons during the second round of marking.
        mark (s, object, UNMARK_MODE, TRUE);
        destroyTable (s->objectHashTable);
        if (DEBUG_SHARE or s->messages)
                bytesHashConsedMessage (s, total);
}

/* ---------------------------------------------------------------- */
/*                 Jonkers Mark-compact Collection                  */
/* ---------------------------------------------------------------- */

static inline void markGlobalTrue (GC_state s, pointer *pp) {
        mark (s, *pp, MARK_MODE, TRUE);
}

static inline void markGlobalFalse (GC_state s, pointer *pp) {
        mark (s, *pp, MARK_MODE, FALSE);
}

static inline void unmarkGlobal (GC_state s, pointer *pp) {
        mark (s, *pp, UNMARK_MODE, FALSE);
}

static inline void threadInternal (GC_state s, pointer *pp) {
        Header *headerp;

        if (FALSE)
                fprintf (stderr, "threadInternal pp = 0x%08x  *pp = 0x%08x  header = 0x%08x\n",
                                (uint)pp, *(uint*)pp, (uint)GC_getHeader (*pp));
        headerp = GC_getHeaderp (*pp);
        *(Header*)pp = *headerp;
        *headerp = (Header)pp;
}

/* If p is weak, the object pointer was valid, and points to an unmarked object,
 * then clear the object pointer.
 */
static inline void maybeClearWeak (GC_state s, pointer p) {
        Bool hasIdentity;
        Header header;
        Header *headerp;
        uint numPointers;
        uint numNonPointers;
        uint tag;

        headerp = GC_getHeaderp (p);
        header = *headerp;
        SPLIT_HEADER();
        if (WEAK_TAG == tag and 1 == numPointers) { 
                Header h2;

                if (DEBUG_WEAK)
                        fprintf (stderr, "maybeClearWeak (0x%08x)  header = 0x%08x\n",
                                        (uint)p, (uint)header);
                h2 = GC_getHeader (((GC_weak)p)->object);
                /* If it's unmarked not threaded, clear the weak pointer. */
                if (1 == ((MARK_MASK | 1) & h2)) {
                        ((GC_weak)p)->object = (pointer)BOGUS_POINTER;
                        header = WEAK_GONE_HEADER | MARK_MASK;
                        if (DEBUG_WEAK)
                                fprintf (stderr, "cleared.  new header = 0x%08x\n",
                                                (uint)header);
                        *headerp = header;
                }
        }
}

static void updateForwardPointers (GC_state s) {
        pointer back;
        pointer front;
        uint gap;
        pointer endOfLastMarked;
        Header header;
        Header *headerp;
        pointer p;
        uint size;

        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "Update forward pointers.\n");
        front = alignFrontier (s, s->heap.start);
        back = s->heap.start + s->oldGenSize;
        endOfLastMarked = front;
        gap = 0;
updateObject:
        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "updateObject  front = 0x%08x  back = 0x%08x\n",
                                (uint)front, (uint)back);
        if (front == back)
                goto done;
        headerp = (Header*)front;
        header = *headerp;
        if (0 == header) {
                /* We're looking at an array.  Move to the header. */
                p = front + 3 * WORD_SIZE;
                headerp = (Header*)(p - WORD_SIZE);
                header = *headerp;
        } else 
                p = front + WORD_SIZE;
        if (1 == (1 & header)) {
                /* It's a header */
                if (MARK_MASK & header) {
                        /* It is marked, but has no forward pointers. 
                         * Thread internal pointers.
                         */
thread:
                        maybeClearWeak (s, p);
                        size = objectSize (s, p);
                        if (DEBUG_MARK_COMPACT)
                                fprintf (stderr, "threading 0x%08x of size %u\n", 
                                                (uint)p, size);
                        if (front - endOfLastMarked >= 4 * WORD_SIZE) {
                                /* Compress all of the unmarked into one string.
                                 * We require 4 * WORD_SIZE space to be available
                                 * because that is the smallest possible array.
                                 * You cannot use 3 * WORD_SIZE because even
                                 * zero-length arrays require an extra word for
                                 * the forwarding pointer.  If you did use
                                 * 3 * WORD_SIZE, updateBackwardPointersAndSlide
                                 * would skip the extra word and be completely
                                 * busted.
                                 */
                                if (DEBUG_MARK_COMPACT)
                                        fprintf (stderr, "compressing from 0x%08x to 0x%08x "
                                                 "(length = %td)\n",
                                                        (uint)endOfLastMarked,
                                                        (uint)front,
                                                        front - endOfLastMarked);
                                *(uint*)endOfLastMarked = 0;
                                *(uint*)(endOfLastMarked + WORD_SIZE) = 
                                        front - endOfLastMarked - 3 * WORD_SIZE;
                                *(uint*)(endOfLastMarked + 2 * WORD_SIZE) =
                                        GC_objectHeader (STRING_TYPE_INDEX);
                        }
                        front += size;
                        endOfLastMarked = front;
                        foreachPointerInObject (s, p, FALSE, threadInternal);
                        goto updateObject;
                } else {
                        /* It's not marked. */
                        size = objectSize (s, p);
                        gap += size;
                        front += size;
                        goto updateObject;
                }
        } else {
                pointer new;

                assert (0 == (3 & header));
                /* It's a pointer.  This object must be live.  Fix all the
                 * forward pointers to it, store its header, then thread
                 * its internal pointers.
                 */
                new = p - gap;
                do {
                        pointer cur;

                        cur = (pointer)header;
                        header = *(word*)cur;
                        *(word*)cur = (word)new;
                } while (0 == (1 & header));
                *headerp = header;
                goto thread;
        }
        assert (FALSE);
done:
        return;
}

static void updateBackwardPointersAndSlide (GC_state s) {
        pointer back;
        pointer front;
        uint gap;
        Header header;
        pointer p;
        uint size;

        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "Update backward pointers and slide.\n");
        front = alignFrontier (s, s->heap.start);
        back = s->heap.start + s->oldGenSize;
        gap = 0;
updateObject:
        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "updateObject  front = 0x%08x  back = 0x%08x\n",
                                (uint)front, (uint)back);
        if (front == back)
                goto done;
        header = *(word*)front;
        if (0 == header) {
                /* We're looking at an array.  Move to the header. */
                p = front + 3 * WORD_SIZE;
                header = *(Header*)(p - WORD_SIZE);
        } else 
                p = front + WORD_SIZE;
        if (1 == (1 & header)) {
                /* It's a header */
                if (MARK_MASK & header) {
                        /* It is marked, but has no backward pointers to it.
                         * Unmark it.
                         */
unmark:
                        *GC_getHeaderp (p) = header & ~MARK_MASK;
                        size = objectSize (s, p);
                        if (DEBUG_MARK_COMPACT)
                                fprintf (stderr, "unmarking 0x%08x of size %u\n", 
                                                (uint)p, size);
                        /* slide */
                        if (DEBUG_MARK_COMPACT)
                                fprintf (stderr, "sliding 0x%08x down %u\n",
                                                (uint)front, gap);
                        copy (front, front - gap, size);
                        front += size;
                        goto updateObject;
                } else {
                        /* It's not marked. */
                        size = objectSize (s, p);
                        if (DEBUG_MARK_COMPACT)
                                fprintf (stderr, "skipping 0x%08x of size %u\n",
                                                (uint)p, size);
                        gap += size;
                        front += size;
                        goto updateObject;
                }
        } else {
                pointer new;

                /* It's a pointer.  This object must be live.  Fix all the
                 * backward pointers to it.  Then unmark it.
                 */
                new = p - gap;
                do {
                        pointer cur;

                        assert (0 == (3 & header));
                        cur = (pointer)header;
                        header = *(word*)cur;
                        *(word*)cur = (word)new;
                } while (0 == (1 & header));
                /* The header will be stored by unmark. */
                goto unmark;
        }
        assert (FALSE);
done:
        s->oldGenSize = front - gap - s->heap.start;
        if (DEBUG_MARK_COMPACT)
                fprintf (stderr, "bytesLive = %u\n", s->bytesLive);
        return;
}

static void markCompact (GC_state s) {
        struct rusage ru_start;

        if (DEBUG or s->messages)
                fprintf (stderr, "Major mark-compact GC.\n");
        if (detailedGCTime (s))
                startTiming (&ru_start);
        s->numMarkCompactGCs++;
        if (s->hashConsDuringGC) {
                s->bytesHashConsed = 0;
                s->numHashConsGCs++;
                s->objectHashTable = newTable (s);
        }
        foreachGlobal (s, s->hashConsDuringGC 
                                ? markGlobalTrue 
                                : markGlobalFalse);
        if (s->hashConsDuringGC)
                destroyTable (s->objectHashTable);
        foreachGlobal (s, threadInternal);
        updateForwardPointers (s);
        updateBackwardPointersAndSlide (s);
        clearCrossMap (s);
        s->bytesMarkCompacted += s->oldGenSize;
        s->lastMajor = GC_MARK_COMPACT;
        if (detailedGCTime (s))
                stopTiming (&ru_start, &s->ru_gcMarkCompact);
        if (DEBUG or s->messages) {
                fprintf (stderr, "Major mark-compact GC done.\n");
                if (s->hashConsDuringGC)
                        bytesHashConsedMessage 
                                (s, s->bytesHashConsed + s->oldGenSize);
        }
}

/* ---------------------------------------------------------------- */
/*                          translateHeap                           */
/* ---------------------------------------------------------------- */

static void translatePointer (GC_state s, pointer *p) {
        if (s->translateUp)
                *p += s->translateDiff;
        else
                *p -= s->translateDiff;
}

/* Translate all pointers to the heap from within the stack and the heap for
 * a heap that has moved from from to to.
 */
static void translateHeap (GC_state s, pointer from, pointer to, uint size) {
        pointer limit;

        if (DEBUG or s->messages)
                fprintf (stderr, "Translating heap of size %s from 0x%08x to 0x%08x.\n",
                                uintToCommaString (size),
                                (uint)from, (uint)to);
        if (from == to)
                return;
        else if (to > from) {
                s->translateDiff = to - from;
                s->translateUp = TRUE;
        } else {
                s->translateDiff = from - to;
                s->translateUp = FALSE;
        }
        /* Translate globals and heap. */
        foreachGlobal (s, translatePointer);
        limit = to + size;
        foreachPointerInRange (s, alignFrontier (s, to), &limit, FALSE,
                                translatePointer);
}

/* ---------------------------------------------------------------- */
/*                            heapRemap                             */
/* ---------------------------------------------------------------- */

bool heapRemap (GC_state s, GC_heap h, W32 desired, W32 minSize) {
        W32 backoff;
        W32 size;

#if not HAS_REMAP
        return FALSE;
#endif
        assert (minSize <= desired);
        assert (desired >= h->size);
        desired = align (desired, s->pageSize);
        backoff = (desired - minSize) / 20;
        if (0 == backoff)
                backoff = 1; /* enough to terminate the loop below */
        backoff = align (backoff, s->pageSize);
        for (size = desired; size >= minSize; size -= backoff) {
                pointer new;

                new = remap (h->start, h->size, size);
                unless ((void*)-1 == new) {
                        h->start = new;
                        h->size = size;
                        if (h->size > s->maxHeapSizeSeen)
                                s->maxHeapSizeSeen = h->size;
                        assert (minSize <= h->size and h->size <= desired);
                        return TRUE;
                }
        }
        return FALSE;
}

/* ---------------------------------------------------------------- */
/*                             heapGrow                             */
/* ---------------------------------------------------------------- */

static void growHeap (GC_state s, W32 desired, W32 minSize) {
        GC_heap h;
        struct GC_heap h2;
        pointer old;
        uint size;

        h = &s->heap;
        assert (desired >= h->size);
        if (DEBUG_RESIZING)
                fprintf (stderr, "Growing heap at 0x%08x of size %s to %s bytes.\n",
                                (uint)h->start,
                                uintToCommaString (h->size),
                                uintToCommaString (desired));
        old = s->heap.start;
        size = s->oldGenSize;
        assert (size <= h->size);
        if (heapRemap (s, h, desired, minSize))
                goto done;
        heapShrink (s, h, size);
        heapInit (&h2);
        /* Allocate a space of the desired size. */
        if (heapCreate (s, &h2, desired, minSize)) {
                pointer from;
                uint remaining;
                pointer to;

                from = old + size;
                to = h2.start + size;
                remaining = size;
copy:                   
                assert (remaining == from - old 
                                and from >= old and to >= h2.start);
                if (remaining < COPY_CHUNK_SIZE) {
                        copy (old, h2.start, remaining);
                } else {
                        remaining -= COPY_CHUNK_SIZE;
                        from -= COPY_CHUNK_SIZE;
                        to -= COPY_CHUNK_SIZE;
                        copy (from, to, COPY_CHUNK_SIZE);
                        heapShrink (s, h, remaining);
                        goto copy;
                }
                heapRelease (s, h);
                *h = h2;
        } else {
                /* Write the heap to a file and try again. */
                int fd;
                FILE *stream;
                char template[80];
                char *tmpDefault;
                char *tmpDir;
                char *tmpVar;

#if (defined (__MSVCRT__))
                tmpVar = "TEMP";
                tmpDefault = "C:/WINNT/TEMP";
#else
                tmpVar = "TMPDIR";
                tmpDefault = "/tmp";
#endif
                tmpDir = getenv (tmpVar);
                strcpy (template, (NULL == tmpDir) ? tmpDefault : tmpDir);
                strcat (template, "/FromSpaceXXXXXX");
                fd = smkstemp (template);
                sclose (fd);
                if (s->messages)
                        fprintf (stderr, "Paging from space to %s.\n", 
                                        template);
                stream = sfopen (template, "wb");
                sfwrite (old, 1, size, stream);
                sfclose (stream);
                heapRelease (s, h);
                if (heapCreate (s, h, desired, minSize)) {
                        stream = sfopen (template, "rb");
                        sfread (h->start, 1, size, stream);
                        sfclose (stream);
                        sunlink (template);
                } else {
                        sunlink (template);
                        if (s->messages)
                                showMem ();
                        die ("Out of memory.  Unable to allocate %s bytes.\n",
                                uintToCommaString (minSize));
                }
        }
done:
        unless (old == s->heap.start) {
                translateHeap (s, old, s->heap.start, s->oldGenSize);
                setCardMapForMutator (s);
        }
}


/* ---------------------------------------------------------------- */
/*                     resizeCardMapAndCrossMap                     */
/* ---------------------------------------------------------------- */

static void resizeCardMapAndCrossMap (GC_state s) {
        if (s->mutatorMarksCards 
                and s->cardMapSize != 
                        align (divCardSize (s, s->heap.size), s->pageSize)) {
                pointer oldCardMap;
                uchar *oldCrossMap;
                uint oldCardMapSize;
                uint oldCrossMapSize;

                oldCardMap = s->cardMap;
                oldCardMapSize = s->cardMapSize;
                oldCrossMap = s->crossMap;
                oldCrossMapSize = s->crossMapSize;
                createCardMapAndCrossMap (s);
                copy ((pointer)oldCrossMap, (pointer)s->crossMap,
                        min (s->crossMapSize, oldCrossMapSize));
                if (DEBUG_MEM)
                        fprintf (stderr, "Releasing card/cross map.\n");
                GC_release (oldCardMap, oldCardMapSize + oldCrossMapSize);
        }
}

/* ---------------------------------------------------------------- */
/*                            resizeHeap                            */
/* ---------------------------------------------------------------- */
/* Resize from space and to space, guaranteeing that at least 'need' bytes are
 * available in from space.
 */
static void resizeHeap (GC_state s, W64 need) {
        W32 desired;

        if (DEBUG_RESIZING)
                fprintf (stderr, "resizeHeap  need = %s fromSize = %s\n",
                                ullongToCommaString (need), 
                                uintToCommaString (s->heap.size));
        desired = heapDesiredSize (s, need, s->heap.size);
        assert (need <= desired);
        if (desired <= s->heap.size)
                heapShrink (s, &s->heap, desired);
        else {
                heapRelease (s, &s->heap2);
                growHeap (s, desired, need);
        }
        resizeCardMapAndCrossMap (s);
        assert (s->heap.size >= need);
}

/* Guarantee that heap2 is either the same size as heap or is unmapped. */
static void resizeHeap2 (GC_state s) {
        uint size;
        uint size2;

        size = s->heap.size;
        size2 = s->heap2.size;
        if (DEBUG_RESIZING)
                fprintf (stderr, "resizeHeap2\n");
        if (0 == size2)
                return;
        if (2 * size > s->ram)
                /* Holding on to heap2 might cause paging.  So don't. */
                heapRelease (s, &s->heap2);
        else if (size2 < size) {
                unless (heapRemap (s, &s->heap2, size, size))
                        heapRelease (s, &s->heap2);
        } else if (size2 > size)
                heapShrink (s, &s->heap2, size);
        assert (0 == s->heap2.size or s->heap.size == s->heap2.size);
}

static inline uint growStackSize (GC_state s) {
        return max (2 * s->currentThread->stack->reserved, 
                        stackNeedsReserved (s, s->currentThread->stack));
}

static void growStack (GC_state s) {
        uint size;
        GC_stack stack;

        size = growStackSize (s);
        if (DEBUG_STACKS or s->messages)
                fprintf (stderr, "Growing stack to size %s.\n",
                                uintToCommaString (stackBytes (s, size)));
        assert (hasBytesFree (s, stackBytes (s, size), 0));
        stack = newStack (s, size, TRUE);
        stackCopy (s, s->currentThread->stack, stack);
        s->currentThread->stack = stack;
        markCard (s, (pointer)s->currentThread);
}

/* ---------------------------------------------------------------- */
/*                        Garbage Collection                        */
/* ---------------------------------------------------------------- */

static bool heapAllocateSecondSemi (GC_state s, W32 size) {
        if ((s->fixedHeap > 0 and s->heap.size + size > s->fixedHeap)
                or (s->maxHeap > 0 and s->heap.size + size > s->maxHeap))
                return FALSE;
        return heapCreate (s, &s->heap2, size, s->oldGenSize);
}

static void majorGC (GC_state s, W32 bytesRequested, bool mayResize) {
        s->numMinorsSinceLastMajor = 0;
        if (0 < (s->numCopyingGCs + s->numMarkCompactGCs)
                and ((float)s->numHashConsGCs 
                        / (float)(s->numCopyingGCs + s->numMarkCompactGCs)
                        < s->hashConsFrequency))
                s->hashConsDuringGC = TRUE;
        if ((not FORCE_MARK_COMPACT)
                and not s->hashConsDuringGC // only markCompact can hash cons
                and s->heap.size < s->ram
                and (not heapIsInit (&s->heap2)
                        or heapAllocateSecondSemi (s, heapDesiredSize (s, (W64)s->bytesLive + bytesRequested, 0))))
                cheneyCopy (s);
        else
                markCompact (s);
        s->hashConsDuringGC = FALSE;
        s->bytesLive = s->oldGenSize;
        if (s->bytesLive > s->maxBytesLive)
                s->maxBytesLive = s->bytesLive;
        /* Notice that the s->bytesLive below is different than the s->bytesLive
         * used as an argument to heapAllocateSecondSemi above.  Above, it was 
         * an estimate.  Here, it is exactly how much was live after the GC.
         */
        if (mayResize)
                resizeHeap (s, (W64)s->bytesLive + bytesRequested);
        resizeHeap2 (s);
        assert (s->oldGenSize + bytesRequested <= s->heap.size);
}

static inline void enterGC (GC_state s) {
        if (s->profilingIsOn) {
                /* We don't need to profileEnter for count profiling because it
                 * has already bumped the counter.  If we did allow the bump,
                 * then the count would look like function(s) had run an extra
                 * time.
                 */  
                if (s->profileStack and not (PROFILE_COUNT == s->profileKind))
                        GC_profileEnter (s);
                s->amInGC = TRUE;
        }
}

static inline void leaveGC (GC_state s) {
        if (s->profilingIsOn) {
                if (s->profileStack and not (PROFILE_COUNT == s->profileKind))
                        GC_profileLeave (s);
                s->amInGC = FALSE;
        }
}

static inline bool needGCTime (GC_state s) {
        return DEBUG or s->summary or s->messages or s->rusageMeasureGC;
}

static void doGC (GC_state s, 
                        W32 oldGenBytesRequested,
                        W32 nurseryBytesRequested, 
                        bool forceMajor,
                        bool mayResize) {
        uint gcTime;
        bool stackTopOk;
        W64 stackBytesRequested;
        struct rusage ru_start;
        W64 totalBytesRequested;
        
        enterGC (s);
        if (DEBUG or s->messages)
                fprintf (stderr, "Starting gc.  Request %s nursery bytes and %s old gen bytes.\n",
                                uintToCommaString (nurseryBytesRequested),
                                uintToCommaString (oldGenBytesRequested));
        assert (invariant (s));
        if (needGCTime (s))
                startTiming (&ru_start);
        minorGC (s);
        stackTopOk = mutatorStackInvariant (s);
        stackBytesRequested = 
                stackTopOk ? 0 : stackBytes (s, growStackSize (s));
        totalBytesRequested = 
                (W64)oldGenBytesRequested 
                + stackBytesRequested
                + nurseryBytesRequested;
        if (forceMajor 
                or totalBytesRequested > s->heap.size - s->oldGenSize)
                majorGC (s, totalBytesRequested, mayResize);
        setNursery (s, oldGenBytesRequested + stackBytesRequested,
                        nurseryBytesRequested);
        assert (hasBytesFree (s, oldGenBytesRequested + stackBytesRequested,
                                        nurseryBytesRequested));
        unless (stackTopOk)
                growStack (s);
        setStack (s);
        if (needGCTime (s)) {
                gcTime = stopTiming (&ru_start, &s->ru_gc);
                s->maxPause = max (s->maxPause, gcTime);
        } else
                gcTime = 0;  /* Assign gcTime to quell gcc warning. */
        if (DEBUG or s->messages) {
                fprintf (stderr, "Finished gc.\n");
                fprintf (stderr, "time: %s ms\n", intToCommaString (gcTime));
                fprintf (stderr, "old gen size: %s bytes (%.1f%%)\n", 
                                intToCommaString (s->oldGenSize),
                                100.0 * ((double) s->oldGenSize) 
                                        / s->heap.size);
        }
        /* Send a GC signal. */
        if (s->handleGCSignal and s->signalHandler != BOGUS_THREAD) {
                if (DEBUG_SIGNALS)
                        fprintf (stderr, "GC Signal pending.\n");
                s->gcSignalIsPending = TRUE;
                unless (s->inSignalHandler) 
                        s->signalIsPending = TRUE;
        }
        if (DEBUG) 
                GC_display (s, stderr);
        assert (hasBytesFree (s, oldGenBytesRequested, nurseryBytesRequested));
        assert (invariant (s));
        leaveGC (s);
}

static inline void ensureMutatorInvariant (GC_state s, bool force) {
        if (force
                or not (mutatorFrontierInvariant(s))
                or not (mutatorStackInvariant(s))) {
                /* This GC will grow the stack, if necessary. */
                doGC (s, 0, s->currentThread->bytesNeeded, force, TRUE);
        }
        assert (mutatorFrontierInvariant(s));
        assert (mutatorStackInvariant(s));
}

/* ensureFree (s, b) ensures that upon return
 *      b <= s->limitPlusSlop - s->frontier
 */
static inline void ensureFree (GC_state s, uint b) {
        assert (s->frontier <= s->limitPlusSlop);
        if (b > s->limitPlusSlop - s->frontier)
                doGC (s, 0, b, FALSE, TRUE);
        assert (b <= s->limitPlusSlop - s->frontier);
}

static void switchToThread (GC_state s, GC_thread t) {
        if (DEBUG_THREADS)
                fprintf (stderr, "switchToThread (0x%08x)  used = %u  reserved = %u\n", 
                                (uint)t, t->stack->used, t->stack->reserved);
        s->currentThread = t;
        setStack (s);
}

/* GC_startHandler does not do an enter()/leave(), even though it is exported.
 * The basis library uses it via _import, not _prim, and so does not treat it
 * as a runtime call -- so the invariant in enter would fail miserably.  It is
 * OK because GC_startHandler must be called from within a critical section.
 *
 * Don't make it inline, because it is also called in basis/Thread.c, and when
 * compiling with COMPILE_FAST, they may appear out of order.
 */
void GC_startHandler (GC_state s) {
        /* Switch to the signal handler thread. */
        if (DEBUG_SIGNALS) {
                fprintf (stderr, "switching to signal handler\n");
                GC_display (s, stderr);
        }
        assert (s->canHandle == 1);
        assert (s->signalIsPending);
        s->signalIsPending = FALSE;
        s->inSignalHandler = TRUE;
        s->savedThread = s->currentThread;
        /* Set s->canHandle to 2 when switching to the signal handler thread;
         * leaving the runtime will decrement s->canHandle to 1,
         * the signal handler will then run atomically and will finish by
         * switching to the thread to continue with, which will decrement
         * s->canHandle to 0.
         */
        s->canHandle = 2;
}

static inline void maybeSwitchToHandler (GC_state s) {
        if (s->canHandle == 1 and s->signalIsPending) {
                GC_startHandler (s);
                switchToThread (s, s->signalHandler);
        }
}

void GC_switchToThread (GC_state s, GC_thread t, uint ensureBytesFree) {
        if (DEBUG_THREADS)
                fprintf (stderr, "GC_switchToThread (0x%08x, %u)\n", (uint)t, ensureBytesFree);
        if (FALSE) {
                /* This branch is slower than the else branch, especially 
                 * when debugging is turned on, because it does an invariant
                 * check on every thread switch.
                 * So, we'll stick with the else branch for now.
                 */
                enter (s);
                s->currentThread->bytesNeeded = ensureBytesFree;
                switchToThread (s, t);
                s->canHandle--;
                maybeSwitchToHandler (s);
                ensureMutatorInvariant (s, FALSE);
                assert (mutatorFrontierInvariant(s));
                assert (mutatorStackInvariant(s));
                leave (s);
        } else {
                /* BEGIN: enter(s); */
                s->currentThread->stack->used = currentStackUsed (s);
                s->currentThread->exnStack = s->exnStack;
                atomicBegin (s);
                /* END: enter(s); */
                s->currentThread->bytesNeeded = ensureBytesFree;
                switchToThread (s, t);
                s->canHandle--;
                maybeSwitchToHandler (s);
                /* BEGIN: ensureMutatorInvariant */
                if (not (mutatorFrontierInvariant(s))
                        or not (mutatorStackInvariant(s))) {
                        /* This GC will grow the stack, if necessary. */
                        doGC (s, 0, s->currentThread->bytesNeeded, FALSE, TRUE);
                } 
                /* END: ensureMutatorInvariant */
                /* BEGIN: leave(s); */
                atomicEnd (s);
                /* END: leave(s); */
        }
        assert (mutatorFrontierInvariant(s));
        assert (mutatorStackInvariant(s));
}

void GC_gc (GC_state s, uint bytesRequested, bool force,
                string file, int line) {
        if (DEBUG or s->messages)
                fprintf (stderr, "%s %d: GC_gc\n", file, line);
        enter (s);
        /* When the mutator requests zero bytes, it may actually need as much
         * as LIMIT_SLOP.
         */
        if (0 == bytesRequested)
                bytesRequested = LIMIT_SLOP;
        s->currentThread->bytesNeeded = bytesRequested;
        maybeSwitchToHandler (s);
        ensureMutatorInvariant (s, force);
        assert (mutatorFrontierInvariant(s));
        assert (mutatorStackInvariant(s));
        leave (s);
}

/* ---------------------------------------------------------------- */
/*                         GC_arrayAllocate                         */
/* ---------------------------------------------------------------- */

pointer GC_arrayAllocate (GC_state s, W32 ensureBytesFree, W32 numElts, 
                                W32 header) {
        W64 arraySize64;
        W32 arraySize;
        uint eltSize;
        W32 *frontier;
        Bool hasIdentity;
        Pointer last;
        uint numPointers;
        uint numNonPointers;
        pointer res;
        uint tag;

        SPLIT_HEADER();
        if (DEBUG)
                fprintf (stderr, "GC_arrayAllocate (0x%08x, %u, %u, 0x%08x)\n",
                                (uint)s, (uint)ensureBytesFree, 
                                (uint)numElts, (uint)header);
        eltSize = numPointers * POINTER_SIZE + numNonPointers;
        arraySize64 = 
                w64align ((W64)eltSize * (W64)numElts + GC_ARRAY_HEADER_SIZE,
                                s->alignment);
        if (arraySize64 >= 0x100000000llu)
                die ("Out of memory: cannot allocate array with %s bytes.",
                        ullongToCommaString (arraySize64));
        arraySize = (W32)arraySize64;
        if (arraySize < GC_ARRAY_HEADER_SIZE + WORD_SIZE)
                /* Create space for forwarding pointer. */
                arraySize = GC_ARRAY_HEADER_SIZE + WORD_SIZE;
        if (DEBUG_ARRAY)
                fprintf (stderr, "array with %s elts of size %u and total size %s.  Ensure %s bytes free.\n",
                        uintToCommaString (numElts), 
                        (uint)eltSize, 
                        uintToCommaString (arraySize),
                        uintToCommaString (ensureBytesFree));
        if (arraySize >= s->oldGenArraySize) {
                enter (s);
                doGC (s,  arraySize, ensureBytesFree, FALSE, TRUE);
                leave (s);
                frontier = (W32*)(s->heap.start + s->oldGenSize);
                last = (pointer)frontier + arraySize;
                s->oldGenSize += arraySize;
                s->bytesAllocated += arraySize;
        } else {
                W32 require;

                require = arraySize + ensureBytesFree;
                if (require > s->limitPlusSlop - s->frontier) {
                        enter (s);
                        doGC (s, 0, require, FALSE, TRUE);
                        leave (s);
                }
                frontier = (W32*)s->frontier;
                last = (pointer)frontier + arraySize;
                assert (isAlignedFrontier (s, last));
                s->frontier = last;
        }
        *frontier++ = 0; /* counter word */
        *frontier++ = numElts;
        *frontier++ = header;
        res = (pointer)frontier;
        /* Initialize all pointers with BOGUS_POINTER. */
        if (1 <= numPointers and 0 < numElts) {
                pointer p;

                if (0 == numNonPointers)
                        for (p = (pointer)frontier; 
                                p < last; 
                                p += POINTER_SIZE)
                                *(Pointer*)p = (Pointer)BOGUS_POINTER;
                else
                        for (p = (Pointer)frontier; p < last; ) {
                                pointer next;

                                p += numNonPointers;
                                next = p + numPointers * POINTER_SIZE;
                                assert (next <= last);
                                while (p < next) {
                                        *(Pointer*)p = (Pointer)BOGUS_POINTER;
                                        p += POINTER_SIZE;
                                }       
                        }
        }
        GC_profileAllocInc (s, arraySize);
        if (DEBUG_ARRAY) {
                fprintf (stderr, "GC_arrayAllocate done.  res = 0x%x  frontier = 0x%x\n",
                                (uint)res, (uint)s->frontier);
                GC_display (s, stderr);
        }
        assert (ensureBytesFree <= s->limitPlusSlop - s->frontier);
        /* Unfortunately, the invariant isn't quite true here, because unless we
         * did the GC, we never set s->currentThread->stack->used to reflect
         * what the mutator did with stackTop.
         */
        return res;
}       

/* ---------------------------------------------------------------- */
/*                             Threads                              */
/* ---------------------------------------------------------------- */

static inline uint threadBytes (GC_state s) {
        uint res;

        res = GC_NORMAL_HEADER_SIZE + sizeof (struct GC_thread);
        /* The following assert depends on struct GC_thread being the right
         * size.  Right now, it happens that res = 16, which is aligned mod 4
         * and mod 8, which is all that we need.  If the struct every changes
         * (possible) or we need more alignment (doubtful), we may need to put
         * some padding at the beginning.
         */
        assert (isAligned (res, s->alignment));
        return res;
}

static GC_thread newThreadOfSize (GC_state s, uint stackSize) {
        GC_stack stack;
        GC_thread t;

        ensureFree (s, stackBytes (s, stackSize) + threadBytes (s));
        stack = newStack (s, stackSize, FALSE);
        t = (GC_thread) object (s, THREAD_HEADER, threadBytes (s), FALSE, FALSE);
        t->bytesNeeded = 0;
        t->exnStack = BOGUS_EXN_STACK;
        t->stack = stack;
        if (DEBUG_THREADS)
                fprintf (stderr, "0x%x = newThreadOfSize (%u)\n",
                                (uint)t, stackSize);;
        return t;
}

static GC_thread copyThread (GC_state s, GC_thread from, uint size) {
        GC_thread to;

        if (DEBUG_THREADS)
                fprintf (stderr, "copyThread (0x%08x)\n", (uint)from);
        /* newThreadOfSize may do a GC, which invalidates from.  
         * Hence we need to stash from where the GC can find it.
         */
        s->savedThread = from;
        to = newThreadOfSize (s, size); 
        from = s->savedThread;
        s->savedThread = BOGUS_THREAD;
        if (DEBUG_THREADS) {
                fprintf (stderr, "free space = %td\n",
                                s->limitPlusSlop - s->frontier);
                fprintf (stderr, "0x%08x = copyThread (0x%08x)\n", 
                                (uint)to, (uint)from);
        }
        stackCopy (s, from->stack, to->stack);
        to->exnStack = from->exnStack;
        return to;
}

void GC_copyCurrentThread (GC_state s) {
        GC_thread res;
        GC_thread t;
        
        if (DEBUG_THREADS)
                fprintf (stderr, "GC_copyCurrentThread\n");
        enter (s);
        t = s->currentThread;
        res = copyThread (s, t, t->stack->used);
/* The following assert is no longer true, since alignment restrictions can force
 * the reserved to be slightly larger than the used.
 */
/*      assert (res->stack->reserved == res->stack->used); */
        assert (res->stack->reserved >= res->stack->used);
        leave (s);
        if (DEBUG_THREADS)
                fprintf (stderr, "0x%08x = GC_copyCurrentThread\n", (uint)res);
        s->savedThread = res;
}

pointer GC_copyThread (GC_state s, pointer thread) {
        GC_thread res;
        GC_thread t;

        if (DEBUG_THREADS)
                fprintf (stderr, "GC_copyThread (0x%08x)\n", (uint)thread);
        enter (s);
        t = (GC_thread)thread;
/* The following assert is no longer true, since alignment restrictions can force
 * the reserved to be slightly larger than the used.
 */
/*      assert (t->stack->reserved == t->stack->used); */
        assert (t->stack->reserved >= t->stack->used);
        res = copyThread (s, t, t->stack->used);
/* The following assert is no longer true, since alignment restrictions can force
 * the reserved to be slightly larger than the used.
 */
/*      assert (res->stack->reserved == res->stack->used); */
        assert (res->stack->reserved >= res->stack->used);
        leave (s);
        if (DEBUG_THREADS)
                fprintf (stderr, "0x%08x = GC_copyThread (0x%08x)\n", (uint)res, (uint)thread);
        return (pointer)res;
}

/* ---------------------------------------------------------------- */
/*                            Profiling                             */
/* ---------------------------------------------------------------- */

/* Apply f to the frame index of each frame in the current thread's stack. */
void GC_foreachStackFrame (GC_state s, void (*f) (GC_state s, uint i)) {
        pointer bottom;
        word index;
        GC_frameLayout *layout;
        word returnAddress;
        pointer top;

        if (DEBUG_PROFILE)
                fprintf (stderr, "walking stack");
        bottom = stackBottom (s, s->currentThread->stack);
        if (DEBUG_PROFILE)
                fprintf (stderr, "  bottom = 0x%08x  top = 0x%08x.\n",
                                (uint)bottom, (uint)s->stackTop);
        for (top = s->stackTop; top > bottom; top -= layout->numBytes) {
                returnAddress = *(word*)(top - WORD_SIZE);
                index = getFrameIndex (s, returnAddress);
                if (DEBUG_PROFILE)
                        fprintf (stderr, "top = 0x%08x  index = %u\n",
                                        (uint)top, index);
                unless (0 <= index and index < s->frameLayoutsSize)
                        die ("top = 0x%08x  returnAddress = 0x%08x  index = %u\n",
                                        (uint)top, returnAddress, index);
                f (s, index);
                layout = &(s->frameLayouts[index]);
                assert (layout->numBytes > 0);
        }
        if (DEBUG_PROFILE)
                fprintf (stderr, "done walking stack\n");
}

static int numStackFrames;
static int *callStack;

static void addToCallStack (GC_state s, uint i) {
        if (DEBUG_CALL_STACK)
                fprintf (stderr, "addToCallStack (%u)\n", i);
        callStack[numStackFrames] = i;
        numStackFrames++;
}

void GC_callStack (GC_state s, Pointer p) {
        if (DEBUG_CALL_STACK)
                fprintf (stderr, "GC_callStack\n");
        numStackFrames = 0;
        callStack = (int*)p;
        GC_foreachStackFrame (s, addToCallStack);
}

uint * GC_frameIndexSourceSeq (GC_state s, int frameIndex) {
        uint *res;

        res = s->sourceSeqs[s->frameSources[frameIndex]];
        if (DEBUG_CALL_STACK)
                fprintf (stderr, "0x%08x = GC_frameIndexSourceSeq (%u)\n",
                                (uint)res, frameIndex);
        return res;
}

static void bumpStackFrameCount (GC_state s, uint i) {
        numStackFrames++;
}

int GC_numStackFrames (GC_state s) {
        numStackFrames = 0;
        GC_foreachStackFrame (s, bumpStackFrameCount);
        if (DEBUG_CALL_STACK)
                fprintf (stderr, "%u = GC_numStackFrames\n", numStackFrames);
        return numStackFrames;
}

inline string GC_sourceName (GC_state s, uint i) {
        if (i < s->sourcesSize)
                return s->sourceNames[s->sources[i].nameIndex];
        else
                return s->sourceNames[i - s->sourcesSize];
}

static inline GC_profileStack profileStackInfo (GC_state s, uint i) {
        assert (s->profile != NULL);
        return &(s->profile->stack[i]);
}

static inline uint profileMaster (GC_state s, uint i) {
        return s->sources[i].nameIndex + s->sourcesSize;
}

static inline void removeFromStack (GC_state s, uint i) {
        GC_profile p;
        GC_profileStack ps;
        ullong totalInc;

        p = s->profile;
        ps = profileStackInfo (s, i);
        totalInc = p->total - ps->lastTotal;
        if (DEBUG_PROFILE)
                fprintf (stderr, "removing %s from stack  ticksInc = %llu  ticksInGCInc = %llu\n",
                                GC_sourceName (s, i), totalInc,
                                p->totalGC - ps->lastTotalGC);
        ps->ticks += totalInc;
        ps->ticksInGC += p->totalGC - ps->lastTotalGC;
}

static void setProfTimer (long usec) {
        struct itimerval iv;

        iv.it_interval.tv_sec = 0;
        iv.it_interval.tv_usec = usec;
        iv.it_value.tv_sec = 0;
        iv.it_value.tv_usec = usec;
        unless (0 == setitimer (ITIMER_PROF, &iv, NULL))
                die ("setProfTimer failed");
}

void GC_profileDone (GC_state s) {
        GC_profile p;
        uint sourceIndex;

        if (DEBUG_PROFILE) 
                fprintf (stderr, "GC_profileDone ()\n");
        assert (s->profilingIsOn);
        if (PROFILE_TIME_FIELD == s->profileKind
            or PROFILE_TIME_LABEL == s->profileKind)
                setProfTimer (0);
        s->profilingIsOn = FALSE;
        p = s->profile;
        if (s->profileStack) {
                for (sourceIndex = 0; 
                        sourceIndex < s->sourcesSize + s->sourceNamesSize;
                        ++sourceIndex) {
                        if (p->stack[sourceIndex].numOccurrences > 0) {
                                if (DEBUG_PROFILE)
                                        fprintf (stderr, "done leaving %s\n", 
                                                        GC_sourceName (s, sourceIndex));
                                removeFromStack (s, sourceIndex);
                        }
                }
        }
}

static int profileDepth = 0;

static void profileIndent () {
        int i;

        for (i = 0; i < profileDepth; ++i)
                fprintf (stderr, " ");
}

static inline void profileEnterSource (GC_state s, uint i) {
        GC_profile p;
        GC_profileStack ps;

        p = s->profile;
        ps = profileStackInfo (s, i);
        if (0 == ps->numOccurrences) {
                ps->lastTotal = p->total;
                ps->lastTotalGC = p->totalGC;
        }
        ps->numOccurrences++;
}

static void profileEnter (GC_state s, uint sourceSeqIndex) {
        int i;
        GC_profile p;
        uint sourceIndex;
        uint *sourceSeq;

        if (DEBUG_PROFILE)
                fprintf (stderr, "profileEnter (%u)\n", sourceSeqIndex);
        assert (s->profileStack);
        assert (sourceSeqIndex < s->sourceSeqsSize);
        p = s->profile;
        sourceSeq = s->sourceSeqs[sourceSeqIndex];
        for (i = 1; i <= sourceSeq[0]; ++i) {
                sourceIndex = sourceSeq[i];
                if (DEBUG_ENTER_LEAVE or DEBUG_PROFILE) {
                        profileIndent ();
                        fprintf (stderr, "(entering %s\n", 
                                        GC_sourceName (s, sourceIndex));
                        profileDepth++;
                }
                profileEnterSource (s, sourceIndex);
                profileEnterSource (s, profileMaster (s, sourceIndex));
        }
}

static void enterFrame (GC_state s, uint i) {
        profileEnter (s, s->frameSources[i]);
}

static inline void profileLeaveSource (GC_state s, uint i) {
        GC_profile p;
        GC_profileStack ps;

        if (DEBUG_PROFILE)
                fprintf (stderr, "profileLeaveSource (%u)\n", i);
        p = s->profile;
        ps = profileStackInfo (s, i);
        assert (ps->numOccurrences > 0);
        ps->numOccurrences--;
        if (0 == ps->numOccurrences)
                removeFromStack (s, i);
}

static void profileLeave (GC_state s, uint sourceSeqIndex) {
        int i;
        GC_profile p;
        uint sourceIndex;
        uint *sourceSeq;

        if (DEBUG_PROFILE)
                fprintf (stderr, "profileLeave (%u)\n", sourceSeqIndex);
        assert (s->profileStack);
        assert (sourceSeqIndex < s->sourceSeqsSize);
        p = s->profile;
        sourceSeq = s->sourceSeqs[sourceSeqIndex];
        for (i = sourceSeq[0]; i > 0; --i) {
                sourceIndex = sourceSeq[i];
                if (DEBUG_ENTER_LEAVE or DEBUG_PROFILE) {
                        profileDepth--;
                        profileIndent ();
                        fprintf (stderr, "leaving %s)\n",
                                        GC_sourceName (s, sourceIndex));
                }
                profileLeaveSource (s, sourceIndex);
                profileLeaveSource (s, profileMaster (s, sourceIndex));
        }
}

static inline void profileInc (GC_state s, W32 amount, uint sourceSeqIndex) {
        uint *sourceSeq;
        uint topSourceIndex;

        if (DEBUG_PROFILE)
                fprintf (stderr, "profileInc (%u, %u)\n", 
                                (uint)amount, sourceSeqIndex);
        assert (sourceSeqIndex < s->sourceSeqsSize);
        sourceSeq = s->sourceSeqs[sourceSeqIndex];
        topSourceIndex = sourceSeq[0] > 0
                ? sourceSeq[sourceSeq[0]]
                : SOURCES_INDEX_UNKNOWN;
        if (DEBUG_PROFILE) {
                profileIndent ();
                fprintf (stderr, "bumping %s by %u\n",
                                GC_sourceName (s, topSourceIndex), (uint)amount);
        }
        s->profile->countTop[topSourceIndex] += amount;
        s->profile->countTop[profileMaster (s, topSourceIndex)] += amount;
        if (s->profileStack)
                profileEnter (s, sourceSeqIndex);
        if (SOURCES_INDEX_GC == topSourceIndex)
                s->profile->totalGC += amount;
        else
                s->profile->total += amount;
        if (s->profileStack)
                profileLeave (s, sourceSeqIndex);
}

void GC_profileEnter (GC_state s) {
        profileEnter (s, topFrameSourceSeqIndex (s));
}

void GC_profileLeave (GC_state s) {
        profileLeave (s, topFrameSourceSeqIndex (s));
}

void GC_profileInc (GC_state s, W32 amount) {
        if (DEBUG_PROFILE)
                fprintf (stderr, "GC_profileInc (%u)\n", (uint)amount);
        profileInc (s, amount, 
                         s->amInGC
                                ? SOURCE_SEQ_GC 
                                : topFrameSourceSeqIndex (s));
}

void GC_profileAllocInc (GC_state s, W32 amount) {
        if (s->profilingIsOn and (PROFILE_ALLOC == s->profileKind)) {
                if (DEBUG_PROFILE)
                        fprintf (stderr, "GC_profileAllocInc (%u)\n", (uint)amount);
                GC_profileInc (s, amount);
        }
}

static void showProf (GC_state s) {
        int i;
        int j;

        fprintf (stdout, "0x%08x\n", s->magic);
        fprintf (stdout, "%u\n", s->sourceNamesSize);
        for (i = 0; i < s->sourceNamesSize; ++i)
                fprintf (stdout, "%s\n", s->sourceNames[i]);
        fprintf (stdout, "%u\n", s->sourcesSize);
        for (i = 0; i < s->sourcesSize; ++i)
                fprintf (stdout, "%u %u\n", 
                                s->sources[i].nameIndex,
                                s->sources[i].successorsIndex);
        fprintf (stdout, "%u\n", s->sourceSeqsSize);
        for (i = 0; i < s->sourceSeqsSize; ++i) {
                uint *sourceSeq;

                sourceSeq = s->sourceSeqs[i];
                for (j = 1; j <= sourceSeq[0]; ++j)
                        fprintf (stdout, "%u ", sourceSeq[j]);
                fprintf (stdout, "\n");
        }
}

GC_profile GC_profileNew (GC_state s) {
        GC_profile p;
        uint size;

        NEW (GC_profile, p);
        p->total = 0;
        p->totalGC = 0;
        size = s->sourcesSize + s->sourceNamesSize;
        ARRAY (ullong*, p->countTop, size);
        if (s->profileStack)
                ARRAY (struct GC_profileStack *, p->stack, size);
        if (DEBUG_PROFILE)
                fprintf (stderr, "0x%08x = GC_profileNew ()\n", (uint)p);
        return p;
}

void GC_profileFree (GC_state s, GC_profile p) {
        free (p->countTop);
        if (s->profileStack)
                free (p->stack);
        free (p);
}

static void writeString (int fd, string s) {
        swrite (fd, s, strlen(s));
}

static void writeUint (int fd, uint u) {
        char buf[20];

        sprintf (buf, "%u", u);
        writeString (fd, buf);
}

static void writeUllong (int fd, ullong u) {
        char buf[20];

        sprintf (buf, "%llu", u);
        writeString (fd, buf);
}

static void writeWord (int fd, word w) {
        char buf[20];

        sprintf (buf, "0x%08x", w);
        writeString (fd, buf);
}

static inline void newline (int fd) {
        writeString (fd, "\n");
}

static void profileWriteCount (GC_state s, GC_profile p, int fd, uint i) {
        writeUllong (fd, p->countTop[i]);
        if (s->profileStack) {
                GC_profileStack ps;
        
                ps = &(p->stack[i]);
                writeString (fd, " ");
                writeUllong (fd, ps->ticks);
                writeString (fd, " ");
                writeUllong (fd, ps->ticksInGC);
        }
        newline (fd);
}

void GC_profileWrite (GC_state s, GC_profile p, int fd) {
        int i;
        string kind;

        if (DEBUG_PROFILE)
                fprintf (stderr, "GC_profileWrite\n");
        writeString (fd, "MLton prof\n");
        kind = "";
        switch (s->profileKind) {
        case PROFILE_ALLOC:
                kind = "alloc\n";
        break;
        case PROFILE_COUNT:
                kind = "count\n";
        break;
        case PROFILE_NONE:
                die ("impossible PROFILE_NONE");
        break;
        case PROFILE_TIME_FIELD:
                kind = "time\n";
        break;
        case PROFILE_TIME_LABEL:
                kind = "time\n";
        break;
        }
        writeString (fd, kind);
        writeString (fd, s->profileStack 
                                ? "stack\n" : "current\n");
        writeWord (fd, s->magic);
        newline (fd);
        writeUllong (fd, p->total);
        writeString (fd, " ");
        writeUllong (fd, p->totalGC);
        newline (fd);
        writeUint (fd, s->sourcesSize);
        newline (fd);
        for (i = 0; i < s->sourcesSize; ++i)
                profileWriteCount (s, p, fd, i);
        writeUint (fd, s->sourceNamesSize);
        newline (fd);
        for (i = 0; i < s->sourceNamesSize; ++i)
                profileWriteCount (s, p, fd, i + s->sourcesSize);
}

#if not HAS_TIME_PROFILING

/* No time profiling on this platform.  There is a check in mlton/main/main.fun
 * to make sure that time profiling is never turned on.
 */
static void profileTimeInit (GC_state s) {
        die ("no time profiling");
}

#else

static GC_state catcherState;

void GC_handleSigProf (pointer pc) {
        uint frameIndex;
        GC_state s;
        uint sourceSeqsIndex;

        s = catcherState;
        if (DEBUG_PROFILE)
                fprintf (stderr, "GC_handleSigProf (0x%08x)\n", (uint)pc);
        if (s->amInGC)
                sourceSeqsIndex = SOURCE_SEQ_GC;
        else {
                frameIndex = topFrameIndex (s);
                if (s->frameLayouts[frameIndex].isC)
                        sourceSeqsIndex = s->frameSources[frameIndex];
                else {
                        if (PROFILE_TIME_LABEL == s->profileKind) {
                                if (s->textStart <= pc and pc < s->textEnd)
                                        sourceSeqsIndex = 
                                                s->textSources [pc - s->textStart];
                                else {
                                        if (DEBUG_PROFILE)
                                                fprintf (stderr, "pc out of bounds\n");
                                        sourceSeqsIndex = SOURCE_SEQ_UNKNOWN;
                                }
                        } else {
                                sourceSeqsIndex = s->curSourceSeqsIndex;
                        }
                }
        }
        profileInc (s, 1, sourceSeqsIndex);
}

static int compareProfileLabels (const void *v1, const void *v2) {
        GC_profileLabel l1;
        GC_profileLabel l2;

        l1 = (GC_profileLabel)v1;
        l2 = (GC_profileLabel)v2;
        return (int)l1->label - (int)l2->label;
}

static void profileTimeInit (GC_state s) {
        int i;
        pointer p;
        struct sigaction sa;
        uint sourceSeqsIndex;

        s->profile = GC_profileNew (s);
        if (PROFILE_TIME_LABEL == s->profileKind) {
        /* Sort sourceLabels by address. */
        qsort (s->sourceLabels, s->sourceLabelsSize, sizeof (*s->sourceLabels),
                compareProfileLabels);
        if (0 == s->sourceLabels[s->sourceLabelsSize - 1].label)
                die ("Max profile label is 0 -- something is wrong.");
        if (DEBUG_PROFILE)
                for (i = 0; i < s->sourceLabelsSize; ++i)
                        fprintf (stderr, "0x%08x  %u\n",
                                        (uint)s->sourceLabels[i].label,
                                        s->sourceLabels[i].sourceSeqsIndex);
        if (ASSERT)
                for (i = 1; i < s->sourceLabelsSize; ++i)
                        assert (s->sourceLabels[i-1].label
                                <= s->sourceLabels[i].label);
        /* Initialize s->textSources. */
        s->textEnd = (pointer)(getTextEnd());
        s->textStart = (pointer)(getTextStart());
        if (ASSERT)
                for (i = 0; i < s->sourceLabelsSize; ++i) {
                        pointer label;

                        label = s->sourceLabels[i].label;
                        assert (0 == label
                                or (s->textStart <= label 
                                        and label < s->textEnd));
                }
        ARRAY (uint*, s->textSources, s->textEnd - s->textStart);
        p = s->textStart;
        sourceSeqsIndex = SOURCE_SEQ_UNKNOWN;
        for (i = 0; i < s->sourceLabelsSize; ++i) {
                for ( ; p < s->sourceLabels[i].label; ++p)
                        s->textSources[p - s->textStart] = sourceSeqsIndex;
                sourceSeqsIndex = s->sourceLabels[i].sourceSeqsIndex;
        }
        for ( ; p < s->textEnd; ++p)
                s->textSources[p - s->textStart] = sourceSeqsIndex;
        } else {
        s->curSourceSeqsIndex = SOURCE_SEQ_UNKNOWN;
        }
        /*
         * Install catcher, which handles SIGPROF and calls MLton_Profile_inc.
         * 
         * One thing I should point out that I discovered the hard way: If
         * the call to sigaction does NOT specify the SA_ONSTACK flag, then
         * even if you have called sigaltstack(), it will NOT switch stacks,
         * so you will probably die.  Worse, if the call to sigaction DOES
         * have SA_ONSTACK and you have NOT called sigaltstack(), it still
         * switches stacks (to location 0) and you die of a SEGV.  Thus the
         * sigaction() call MUST occur after the call to sigaltstack(), and
         * in order to have profiling cover as much as possible, you want it
         * to occur right after the sigaltstack() call.
         */
        catcherState = s;
        sigemptyset (&sa.sa_mask);
        setSigProfHandler (&sa);
        unless (sigaction (SIGPROF, &sa, NULL) == 0)
                diee ("sigaction() failed");
        /* Start the SIGPROF timer. */
        setProfTimer (10000);
}

#endif

/* profileEnd is for writing out an mlmon.out file even if the C code terminates
 * abnormally, e.g. due to running out of memory.  It will only run if the usual
 * SML profile atExit cleanup code did not manage to run.
 */
static GC_state profileEndState;

static void profileEnd () {
        int fd;
        GC_state s;

        if (DEBUG_PROFILE)
                fprintf (stderr, "profileEnd ()\n");
        s = profileEndState;
        if (s->profilingIsOn) {
                fd = creat ("mlmon.out", 0666);
                if (fd < 0)
                        diee ("Cannot create mlmon.out");
                GC_profileWrite (s, s->profile, fd);
        }
}

/* ---------------------------------------------------------------- */
/*                          Initialization                          */
/* ---------------------------------------------------------------- */

static void initSignalStack (GC_state s) {
#if HAS_SIGALTSTACK
        static stack_t altstack;
        size_t ss_size = align (SIGSTKSZ, s->pageSize);
        size_t psize = s->pageSize;
        void *ss_sp = ssmmap (2 * ss_size, psize, psize);
        altstack.ss_sp = ss_sp + ss_size;
        altstack.ss_size = ss_size;
        altstack.ss_flags = 0;
        sigaltstack (&altstack, NULL);
#endif
}

#if FALSE
static bool stringToBool (string s) {
        if (0 == strcmp (s, "false"))
                return FALSE;
        if (0 == strcmp (s, "true"))
                return TRUE;
        die ("Invalid @MLton bool: %s.", s);
}
#endif

static float stringToFloat (string s) {
        float f;

        unless (1 == sscanf (s, "%f", &f))
                die ("Invalid @MLton float: %s.", s);
        return f;
}

static uint stringToBytes (string s) {
        double d;
        char *endptr;
        uint factor;
        
        d = strtod (s, &endptr);
        if (0.0 == d and s == endptr)
                goto bad;
        switch (*endptr++) {
        case 'g':
        case 'G':
                factor = 1024 * 1024 * 1024;
        break;
        case 'k':
        case 'K':
                factor = 1024;
        break;
        case 'm':
        case 'M':
                factor = 1024 * 1024;
        break;
        default:
                goto bad;
        }
        d *= factor;
        unless (strlen (s) == endptr - s
                        and (double)INT_MIN <= d 
                        and d <= (double)INT_MAX)
                goto bad;
        return (uint)d;
bad:
        die ("Invalid @MLton memory amount: %s.", s);
}

static void setInitialBytesLive (GC_state s) {
        int i;
        int numBytes;
        int numElements;

        s->bytesLive = 0;
        for (i = 0; i < s->intInfInitsSize; ++i) {
                numElements = strlen (s->intInfInits[i].mlstr);
                s->bytesLive +=
                        align (GC_ARRAY_HEADER_SIZE 
                                + WORD_SIZE // for the sign
                                + numElements,
                                s->alignment);
        }
        for (i = 0; i < s->vectorInitsSize; ++i) {
                numBytes = s->vectorInits[i].bytesPerElement
                        * s->vectorInits[i].numElements;
                s->bytesLive +=
                        align (GC_ARRAY_HEADER_SIZE
                                + ((0 == numBytes) 
                                        ? POINTER_SIZE
                                        : numBytes),
                                s->alignment);
        }
}

/*
 * For each entry { globalIndex, mlstr } in the inits array (which is terminated
 * by one with an mlstr of NULL), set
 *      state->globals[globalIndex]
 * to the corresponding IntInf.int value.
 * On exit, the GC_state pointed to by s is adjusted to account for any
 * space used.
 */
static void initIntInfs (GC_state s) {
        struct GC_intInfInit *inits;
        pointer frontier;
        char    *str;
        uint    slen,
                llen,
                alen,
                i,
                index;
        bool    neg,
                hex;
        bignum  *bp;
        uchar   *cp;

        assert (isAlignedFrontier (s, s->frontier));
        frontier = s->frontier;
        for (index = 0; index < s->intInfInitsSize; ++index) {
                inits = &s->intInfInits[index];
                str = inits->mlstr;
                assert (inits->globalIndex < s->globalsSize);
                neg = *str == '~';
                if (neg)
                        ++str;
                slen = strlen (str);
                hex = str[0] == '0' && str[1] == 'x';
                if (hex) {
                        str += 2;
                        slen -= 2;
                        llen = (slen + 7) / 8;
                } else
                        llen = (slen + 8) / 9;
                assert (slen > 0);
                bp = (bignum *)frontier;
                cp = (uchar *)&bp->limbs[llen];
                for (i = 0; i != slen; ++i)
                        if ('0' <= str[i] && str[i] <= '9')
                                cp[i] = str[i] - '0' + 0;
                        else if ('a' <= str[i] && str[i] <= 'f')
                                cp[i] = str[i] - 'a' + 0xa;
                        else {
                                assert('A' <= str[i] && str[i] <= 'F');
                                cp[i] = str[i] - 'A' + 0xA;
                        }
                alen = mpn_set_str (bp->limbs, cp, slen, hex ? 0x10 : 10);
                assert (alen <= llen);
                if (alen <= 1) {
                        uint    val,
                                ans;

                        if (alen == 0)
                                val = 0;
                        else
                                val = bp->limbs[0];
                        if (neg) {
                                /*
                                 * We only fit if val in [1, 2^30].
                                 */
                                ans = - val;
                                val = val - 1;
                        } else
                                /*
                                 * We only fit if val in [0, 2^30 - 1].
                                 */
                                ans = val;
                        if (val < (uint)1<<30) {
                                s->globals[inits->globalIndex] = 
                                        (pointer)(ans<<1 | 1);
                                continue;
                        }
                }
                s->globals[inits->globalIndex] = (pointer)&bp->isneg;
                bp->counter = 0;
                bp->card = alen + 1;
                bp->magic = BIGMAGIC;
                bp->isneg = neg;
                frontier = alignFrontier (s, (pointer)&bp->limbs[alen]);
        }
        assert (isAlignedFrontier (s, frontier));
        s->frontier = frontier;
        GC_profileAllocInc (s, frontier - s->frontier);
        s->bytesAllocated += frontier - s->frontier;
}

static void initStrings (GC_state s) {
        struct GC_vectorInit *inits;
        pointer frontier;
        int i;

        assert (isAlignedFrontier (s, s->frontier));
        inits = s->vectorInits;
        frontier = s->frontier;
        for (i = 0; i < s->vectorInitsSize; ++i) {
                uint bytesPerElement;
                uint numBytes;
                uint objectSize;
                uint typeIndex;

                bytesPerElement = inits[i].bytesPerElement;
                numBytes = bytesPerElement * inits[i].numElements;
                objectSize = align (GC_ARRAY_HEADER_SIZE
                                        + ((0 == numBytes) 
                                                ? POINTER_SIZE
                                                : numBytes),
                                        s->alignment);
                assert (objectSize <= s->heap.start + s->heap.size - frontier);
                *(word*)frontier = 0; /* counter word */
                *(word*)(frontier + WORD_SIZE) = inits[i].numElements;
                switch (bytesPerElement) {
                case 1:
                        typeIndex = WORD8_VECTOR_TYPE_INDEX;
                break;
                case 2:
                        typeIndex = WORD16_VECTOR_TYPE_INDEX;
                break;
                case 4:
                        typeIndex = WORD32_VECTOR_TYPE_INDEX;
                break;
                default:
                        die ("unknown bytes per element in vectorInit: %d",
                                bytesPerElement);
                }
                *(word*)(frontier + 2 * WORD_SIZE) = GC_objectHeader (typeIndex);
                s->globals[inits[i].globalIndex] = 
                        frontier + GC_ARRAY_HEADER_SIZE;
                if (DEBUG_DETAILED)
                        fprintf (stderr, "allocated string at 0x%x\n",
                                        (uint)s->globals[inits[i].globalIndex]);
                memcpy (frontier + GC_ARRAY_HEADER_SIZE, inits[i].bytes, 
                                numBytes);
                frontier += objectSize;
        }
        if (DEBUG_DETAILED)
                fprintf (stderr, "frontier after string allocation is 0x%08x\n",
                                (uint)frontier);
        GC_profileAllocInc (s, frontier - s->frontier);
        s->bytesAllocated += frontier - s->frontier;
        assert (isAlignedFrontier (s, frontier));
        s->frontier = frontier;
}

static void newWorld (GC_state s) {
        int i;
        pointer start;

        for (i = 0; i < s->globalsSize; ++i)
                s->globals[i] = (pointer)BOGUS_POINTER;
        setInitialBytesLive (s);
        heapCreate (s, &s->heap, heapDesiredSize (s, s->bytesLive, 0),
                        s->bytesLive);
        createCardMapAndCrossMap (s);
        start = alignFrontier (s, s->heap.start);
        s->frontier = start;
        initIntInfs (s);
        initStrings (s);
        assert (s->frontier - start <= s->bytesLive);
        s->oldGenSize = s->frontier - s->heap.start;
        setNursery (s, 0, 0);
        switchToThread (s, newThreadOfSize (s, initialStackSize (s)));
}

/* worldTerminator is used to separate the human readable messages at the 
 * beginning of the world file from the machine readable data.
 */
static const char worldTerminator = '\000';

static void loadWorld (GC_state s, char *fileName) {
        FILE *file;
        uint magic;
        pointer oldGen;
        int c;
        
        if (DEBUG_WORLD)
                fprintf (stderr, "loadWorld (%s)\n", fileName);
        file = sfopen (fileName, "rb");
        until ((c = fgetc (file)) == worldTerminator or EOF == c);
        if (EOF == c) die ("Invalid world.");
        magic = sfreadUint (file);
        unless (s->magic == magic)
                die ("Invalid world: wrong magic number.");
        oldGen = (pointer) sfreadUint (file);
        s->oldGenSize = sfreadUint (file);
        s->callFromCHandler = (GC_thread) sfreadUint (file);
        s->canHandle = sfreadUint (file);
        s->currentThread = (GC_thread) sfreadUint (file);
        s->signalHandler = (GC_thread) sfreadUint (file);
        heapCreate (s, &s->heap, heapDesiredSize (s, s->oldGenSize, 0),
                        s->oldGenSize);
        createCardMapAndCrossMap (s);
        sfread (s->heap.start, 1, s->oldGenSize, file);
        (*s->loadGlobals) (file);
        unless (EOF == fgetc (file))
                die ("Invalid world: junk at end of file.");
        fclose (file);
        /* translateHeap must occur after loading the heap and globals, since it
         * changes pointers in all of them.
         */
        translateHeap (s, oldGen, s->heap.start, s->oldGenSize);
        setNursery (s, 0, 0);
        setStack (s);
}

/* ---------------------------------------------------------------- */
/*                             GC_init                              */
/* ---------------------------------------------------------------- */

Bool MLton_Platform_CygwinUseMmap;

static int processAtMLton (GC_state s, int argc, char **argv, 
                                string *worldFile) {
        int i;

        i = 1;
        while (s->mayProcessAtMLton 
                and i < argc 
                and (0 == strcmp (argv [i], "@MLton"))) {
                bool done;

                i++;
                done = FALSE;
                while (!done) {
                        if (i == argc)
                                die ("Missing -- at end of @MLton args.");
                        else {
                                string arg;

                                arg = argv[i];
                                if (0 == strcmp (arg, "copy-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton copy-ratio missing argument.");
                                        s->copyRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp(arg, "fixed-heap")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton fixed-heap missing argument.");
                                        s->fixedHeap = 
                                                align (stringToBytes (argv[i++]),
                                                        2 * s->pageSize);
                                } else if (0 == strcmp (arg, "gc-messages")) {
                                        ++i;
                                        s->messages = TRUE;
                                } else if (0 == strcmp (arg, "gc-summary")) {
                                        ++i;
#if (defined (__MINGW32__))
                                        fprintf (stderr, "Warning: MinGW doesn't yet support gc-summary\n");
#else
                                        s->summary = TRUE;
#endif
                                } else if (0 == strcmp (arg, "copy-generational-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton copy-generational-ratio missing argument.");
                                        s->copyGenerationalRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "grow-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton grow-ratio missing argument.");
                                        s->growRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "hash-cons")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton hash-cons missing argument.");
                                        s->hashConsFrequency =
                                                stringToFloat (argv[i++]);
                                        unless (0.0 <= s->hashConsFrequency
                                                and s->hashConsFrequency <= 1.0)
                                                die ("@MLton hash-cons argument must be between 0.0 and 1.0");
                                } else if (0 == strcmp (arg, "live-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton live-ratio missing argument.");
                                        s->liveRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "load-world")) {
                                        unless (s->mayLoadWorld)
                                                die ("May not load world.");
                                        ++i;
                                        s->isOriginal = FALSE;
                                        if (i == argc) 
                                                die ("@MLton load-world missing argument.");
                                        *worldFile = argv[i++];
                                } else if (0 == strcmp (arg, "max-heap")) {
                                        ++i;
                                        if (i == argc) 
                                                die ("@MLton max-heap missing argument.");
                                        s->maxHeap = align (stringToBytes (argv[i++]),
                                                                2 * s->pageSize);
                                } else if (0 == strcmp (arg, "mark-compact-generational-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton mark-compact-generational-ratio missing argument.");
                                        s->markCompactGenerationalRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "mark-compact-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton mark-compact-ratio missing argument.");
                                        s->markCompactRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "no-load-world")) {
                                        ++i;
                                        s->mayLoadWorld = FALSE;
                                } else if (0 == strcmp (arg, "nursery-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton nursery-ratio missing argument.");
                                        s->nurseryRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "ram-slop")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton ram-slop missing argument.");
                                        s->ramSlop =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "show-prof")) {
                                        showProf (s);
                                        exit (0);
                                } else if (0 == strcmp (arg, "stop")) {
                                        ++i;
                                        s->mayProcessAtMLton = FALSE;
                                } else if (0 == strcmp (arg, "thread-shrink-ratio")) {
                                        ++i;
                                        if (i == argc)
                                                die ("@MLton thread-shrink-ratio missing argument.");
                                        s->threadShrinkRatio =
                                                stringToFloat (argv[i++]);
                                } else if (0 == strcmp (arg, "use-mmap")) {
                                        ++i;
                                        MLton_Platform_CygwinUseMmap = TRUE;
                                } else if (0 == strcmp (arg, "--")) {
                                        ++i;
                                        done = TRUE;
                                } else if (i > 1)
                                        die ("Strange @MLton arg: %s", argv[i]);
                                else done = TRUE;
                        }
                }
        }
        return i;
}

int GC_init (GC_state s, int argc, char **argv) {
        char *worldFile;
        int i;

        assert (isAligned (sizeof (struct GC_stack), s->alignment));
        assert (isAligned (GC_NORMAL_HEADER_SIZE + sizeof (struct GC_thread),
                                s->alignment));
        assert (isAligned (GC_NORMAL_HEADER_SIZE + sizeof (struct GC_weak),
                                s->alignment));
        MLton_Platform_CygwinUseMmap = TRUE;
        s->amInGC = TRUE;
        s->amInMinorGC = FALSE;
        s->bytesAllocated = 0;
        s->bytesCopied = 0;
        s->bytesCopiedMinor = 0;
        s->bytesMarkCompacted = 0;
        s->callFromCHandler = BOGUS_THREAD;
        s->canHandle = 0;
        s->cardSize = 0x1 << CARD_SIZE_LOG2;
        s->copyRatio = 4.0;
        s->copyGenerationalRatio = 4.0;
        s->currentThread = BOGUS_THREAD;
        s->fixedHeap = 0.0;
        s->gcSignalIsPending = FALSE;
        s->growRatio = 8.0;
        s->handleGCSignal = FALSE;
        s->hashConsDuringGC = FALSE;
        s->hashConsFrequency = 0.0;
        s->inSignalHandler = FALSE;
        s->isOriginal = TRUE;
        s->lastMajor = GC_COPYING;
        s->liveRatio = 8.0;
        s->markCompactRatio = 1.04;
        s->markCompactGenerationalRatio = 8.0;
        s->markedCards = 0;
        s->maxBytesLive = 0;
        s->maxHeap = 0;
        s->maxHeapSizeSeen = 0;
        s->maxPause = 0;
        s->maxStackSizeSeen = 0;
        s->mayLoadWorld = TRUE;
        s->mayProcessAtMLton = TRUE;
        s->messages = FALSE;
        s->minorBytesScanned = 0;
        s->minorBytesSkipped = 0;
        s->numCopyingGCs = 0;
        s->numLCs = 0;
        s->numHashConsGCs = 0;
        s->numMarkCompactGCs = 0;
        s->numMinorGCs = 0;
        s->numMinorsSinceLastMajor = 0;
        s->nurseryRatio = 10.0;
        s->oldGenArraySize = 0x100000;
        s->pageSize = getpagesize ();
        s->ramSlop = 0.5;
        s->rusageMeasureGC = FALSE;
        s->savedThread = BOGUS_THREAD;
        s->signalHandler = BOGUS_THREAD;
        s->signalIsPending = FALSE;
        s->startTime = currentTime ();
        s->summary = FALSE;
        s->threadShrinkRatio = 0.5;
        s->weaks = NULL;
        heapInit (&s->heap);
        heapInit (&s->heap2);
        sigemptyset (&s->signalsHandled);
        initSignalStack (s);
        sigemptyset (&s->signalsPending);
        rusageZero (&s->ru_gc);
        rusageZero (&s->ru_gcCopy);
        rusageZero (&s->ru_gcMarkCompact);
        rusageZero (&s->ru_gcMinor);
        worldFile = NULL;
        unless (isAligned (s->pageSize, s->cardSize))
                die ("Page size must be a multiple of card size.");
        processAtMLton (s, s->atMLtonsSize, s->atMLtons, &worldFile);
        i = processAtMLton (s, argc, argv, &worldFile);
        if (s->fixedHeap > 0 and s->maxHeap > 0)
                die ("Cannot use both fixed-heap and max-heap.\n");
        unless (ratiosOk (s))
                die ("invalid ratios");
        s->totalRam = totalRam (s);
        /* We align s->ram by pageSize so that we can test whether or not we
         * we are using mark-compact by comparing heap size to ram size.  If 
         * we didn't round, the size might be slightly off.
         */
        s->ram = align (s->ramSlop * s->totalRam, s->pageSize);
        if (DEBUG or DEBUG_RESIZING or s->messages)
                fprintf (stderr, "total RAM = %s  RAM = %s\n",
                                uintToCommaString (s->totalRam), 
                                uintToCommaString (s->ram));
        if (DEBUG_PROFILE) {
                int i;
                        for (i = 0; i < s->frameSourcesSize; ++i) {
                        int j;
                        uint *sourceSeq;
                                fprintf (stderr, "%d\n", i);
                        sourceSeq = s->sourceSeqs[s->frameSources[i]];
                        for (j = 1; j <= sourceSeq[0]; ++j)
                                fprintf (stderr, "\t%s\n",
                                                s->sourceNames[s->sources[sourceSeq[j]].nameIndex]);
                }
        }
        /* Initialize profiling.  This must occur after processing command-line 
         * arguments, because those may just be doing a show prof, in which 
         * case we don't want to initialize the atExit.
         */
        if (PROFILE_NONE == s->profileKind)
                s->profilingIsOn = FALSE;
        else {
                s->profilingIsOn = TRUE;
                assert (s->frameSourcesSize == s->frameLayoutsSize);
                switch (s->profileKind) {
                case PROFILE_ALLOC:
                case PROFILE_COUNT:
                        s->profile = GC_profileNew (s);
                break;
                case PROFILE_NONE:
                        die ("impossible PROFILE_NONE");
                case PROFILE_TIME_FIELD:
                case PROFILE_TIME_LABEL:
                        profileTimeInit (s);
                break;
                }
                profileEndState = s;
                atexit (profileEnd);
        }
        if (s->isOriginal) {
                newWorld (s);
                /* The mutator stack invariant doesn't hold,
                 * because the mutator has yet to run.
                 */
                assert (mutatorInvariant (s, TRUE, FALSE));
        } else {
                loadWorld (s, worldFile);
                if (s->profilingIsOn and s->profileStack)
                        GC_foreachStackFrame (s, enterFrame);
                assert (mutatorInvariant (s, TRUE, TRUE));
        }
        s->amInGC = FALSE;
        return i;
}

extern char **environ; /* for Posix_ProcEnv_environ */

void MLton_init (int argc, char **argv, GC_state s) {
        int start;

        Posix_ProcEnv_environ = (CstringArray)environ;
        start = GC_init (s, argc, argv);
        /* Setup argv and argc that SML sees. */
        /* start is now the index of the first real arg. */
        CommandLine_commandName = (uint)(argv[0]);
        CommandLine_argc = argc - start;
        CommandLine_argv = (uint)(argv + start);
}

static void displayCol (FILE *out, int width, string s) {
        int extra;
        int i;
        int len;

        len = strlen (s);
        if (len < width) {
                extra = width - len;
                for (i = 0; i < extra; ++i)
                        fprintf (out, " ");
        }
        fprintf (out, "%s\t", s);
}

static void displayCollectionStats (FILE *out, string name, struct rusage *ru, 
                                        uint num, ullong bytes) {
        uint ms;

        ms = rusageTime (ru);
        fprintf (out, "%s", name);
        displayCol (out, 7, uintToCommaString (ms));
        displayCol (out, 7, uintToCommaString (num));
        displayCol (out, 15, ullongToCommaString (bytes));
        displayCol (out, 15, 
                        (ms > 0)
                        ? uintToCommaString (1000.0 * (float)bytes/(float)ms)
                        : "-");
        fprintf (out, "\n");
}

void GC_done (GC_state s) {
        FILE *out;

        enter (s);
        minorGC (s);
        out = stderr;
        if (s->summary) {
                double time;
                uint gcTime;

                gcTime = rusageTime (&s->ru_gc);
                fprintf (out, "GC type\t\ttime ms\t number\t\t  bytes\t      bytes/sec\n");
                fprintf (out, "-------------\t-------\t-------\t---------------\t---------------\n");
                displayCollectionStats
                        (out, "copying\t\t", &s->ru_gcCopy, s->numCopyingGCs, 
                                s->bytesCopied);
                displayCollectionStats
                        (out, "mark-compact\t", &s->ru_gcMarkCompact, 
                                s->numMarkCompactGCs, s->bytesMarkCompacted);
                displayCollectionStats
                        (out, "minor\t\t", &s->ru_gcMinor, s->numMinorGCs, 
                                s->bytesCopiedMinor);
                time = (double)(currentTime () - s->startTime);
                fprintf (out, "total GC time: %s ms (%.1f%%)\n",
                                intToCommaString (gcTime), 
                                (0.0 == time) 
                                        ? 0.0 
                                        : 100.0 * ((double) gcTime) / time);
                fprintf (out, "max pause: %s ms\n",
                                uintToCommaString (s->maxPause));
                fprintf (out, "total allocated: %s bytes\n",
                                ullongToCommaString (s->bytesAllocated));
                fprintf (out, "max live: %s bytes\n",
                                uintToCommaString (s->maxBytesLive));
                fprintf (out, "max semispace: %s bytes\n", 
                                uintToCommaString (s->maxHeapSizeSeen));
                fprintf (out, "max stack size: %s bytes\n", 
                                uintToCommaString (s->maxStackSizeSeen));
                fprintf (out, "marked cards: %s\n", 
                                ullongToCommaString (s->markedCards));
                fprintf (out, "minor scanned: %s bytes\n",
                                uintToCommaString (s->minorBytesScanned));
                fprintf (out, "minor skipped: %s bytes\n", 
                                uintToCommaString (s->minorBytesSkipped));
        }
        heapRelease (s, &s->heap);
        heapRelease (s, &s->heap2);
}

void GC_finishHandler (GC_state s) {
        if (DEBUG_SIGNALS)
                fprintf (stderr, "GC_finishHandler ()\n");
        assert (s->canHandle == 1);
        s->inSignalHandler = FALSE;     
}

/* GC_handler sets s->limit = 0 so that the next limit check will fail. 
 * Signals need to be blocked during the handler (i.e. it should run atomically)
 * because sigaddset does both a read and a write of s->signalsPending.
 * The signals are blocked by Posix_Signal_handle (see Posix/Signal/Signal.c).
 */
void GC_handler (GC_state s, int signum) {
        if (DEBUG_SIGNALS)
                fprintf (stderr, "GC_handler signum = %d\n", signum);
        assert (sigismember (&s->signalsHandled, signum));
        if (s->canHandle == 0)
                s->limit = 0;
        s->signalIsPending = TRUE;
        sigaddset (&s->signalsPending, signum);
        if (DEBUG_SIGNALS)
                fprintf (stderr, "GC_handler done\n");
}

uint GC_size (GC_state s, pointer root) {
        uint res;

        if (DEBUG_SIZE)
                fprintf (stderr, "GC_size marking\n");
        res = mark (s, root, MARK_MODE, FALSE);
        if (DEBUG_SIZE)
                fprintf (stderr, "GC_size unmarking\n");
        mark (s, root, UNMARK_MODE, FALSE);
        return res;
}

void GC_saveWorld (GC_state s, int fd) {
        char buf[80];

        if (DEBUG_WORLD)
                fprintf (stderr, "GC_saveWorld (%d).\n", fd);
        enter (s);
        /* Compact the heap. */
        doGC (s, 0, 0, TRUE, TRUE);
        sprintf (buf,
                "Heap file created by MLton.\nheap.start = 0x%08x\nbytesLive = %u\n",
                (uint)s->heap.start, (uint)s->bytesLive);
        swrite (fd, buf, 1 + strlen(buf)); /* +1 to get the '\000' */
        swriteUint (fd, s->magic);
        swriteUint (fd, (uint)s->heap.start);
        swriteUint (fd, (uint)s->oldGenSize);
        swriteUint (fd, (uint)s->callFromCHandler);
        /* canHandle must be saved in the heap, because the saveWorld may be
         * run in the context of a critical section, which will expect to be in 
         * the same context when it is restored.
         */
        swriteUint (fd, s->canHandle);
        swriteUint (fd, (uint)s->currentThread);
        swriteUint (fd, (uint)s->signalHandler);
        swrite (fd, s->heap.start, s->oldGenSize);
        (*s->saveGlobals) (fd);
        leave (s);
}

void GC_pack (GC_state s) {
        uint keep;

        enter (s);
        if (DEBUG or s->messages)
                fprintf (stderr, "Packing heap of size %s.\n",
                                uintToCommaString (s->heap.size));
        /* Could put some code here to skip the GC if there hasn't been much
         * allocated since the last collection.  But you would still need to 
         * do a minor GC to make all objects contiguous.
         */
        doGC (s, 0, 0, TRUE, FALSE);
        keep = s->oldGenSize * 1.1;
        if (keep <= s->heap.size) {
                heapShrink (s, &s->heap, keep);
                setNursery (s, 0, 0);
                setStack (s);
        }
        heapRelease (s, &s->heap2);
        if (DEBUG or s->messages)
                fprintf (stderr, "Packed heap to size %s.\n",
                                uintToCommaString (s->heap.size));
        leave (s);
}

void GC_unpack (GC_state s) {
        enter (s);
        if (DEBUG or s->messages)
                fprintf (stderr, "Unpacking heap of size %s.\n",
                                uintToCommaString (s->heap.size));
        /* The enterGC is needed here because minorGC and resizeHeap might move
         * the stack, and the SIGPROF catcher would then see a bogus stack.  The
         * leaveGC has to happen after the setStack.
         */
        enterGC (s);
        minorGC (s);
        resizeHeap (s, s->oldGenSize);
        resizeHeap2 (s);
        setNursery (s, 0, 0);
        setStack (s);
        leaveGC (s);
        if (DEBUG or s->messages)
                fprintf (stderr, "Unpacked heap to size %s.\n",
                                uintToCommaString (s->heap.size));
        leave (s);
}

/* ------------------------------------------------- */
/*                     GC_weak*                      */
/* ------------------------------------------------- */

/* A weak object is a header followed by two words.
 *
 * The object type indexed by the header determines whether the weak is valid
 * or not.  If the type has numPointers == 1, then the weak pointer is valid.  
 * Otherwise, the type has numPointers == 0 and the weak pointer is not valid.
 *
 * The first word is used to chain the live weaks together during a copying gc
 * and is otherwise unused.
 *
 * The second word is the weak pointer.
 */ 

bool GC_weakCanGet (pointer p) {
        Bool res;

        res = WEAK_GONE_HEADER != GC_getHeader (p);
        if (DEBUG_WEAK)
                fprintf (stderr, "%s = GC_weakCanGet (0x%08x)\n",
                                boolToString (res), (uint)p);
        return res;
}

Pointer GC_weakGet (Pointer p) {
        pointer res;

        res = ((GC_weak)p)->object;
        if (DEBUG_WEAK)
                fprintf (stderr, "0x%08x = GC_weakGet (0x%08x)\n",
                                (uint)res, (uint)p);
        return res;
}

Pointer GC_weakNew (GC_state s, Word32 header, Pointer p) {
        pointer res;

        res = object (s, header, GC_NORMAL_HEADER_SIZE + 3 * WORD_SIZE, 
                        FALSE, FALSE);
        ((GC_weak)res)->object = p;
        if (DEBUG_WEAK)
                fprintf (stderr, "0x%08x = GC_weakNew (0x%08x, 0x%08x)\n",
                                (uint)res, (uint)header, (uint)p);
        return res;
}