Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

executable file 4277 lines (3535 sloc) 118.62 kb
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
/* File: "mem.c" */

/* Copyright (c) 1994-2012 by Marc Feeley, All Rights Reserved. */

#define ___INCLUDED_FROM_MEM
#define ___VERSION 406006
#include "gambit.h"

#include "os_base.h"
#include "os_time.h"
#include "setup.h"
#include "mem.h"
#include "c_intf.h"

/* The following includes are needed for debugging. */

#include <stdlib.h>
#include <string.h>

/**********************************/
#ifdef ___DEBUG
#ifdef ___DEBUG_ALLOC_MEM_TRACE
#define ___alloc_mem(bytes) ___alloc_mem_debug(bytes,__LINE__,__FILE__)
#endif
#endif


/*---------------------------------------------------------------------------*/

#ifdef ___DEBUG

/*
* Defining the symbol ENABLE_CONSISTENCY_CHECKS will enable the GC to
* perform checks that detect when the heap is in an inconsistent
* state. This is useful to detect bugs in the GC and the rest of the
* system. To perform the consistency checks, the verbosity level in
* ___setup_params.debug_settings must be at least 1. The checks are
* very extensive and consequently are expensive. They should only be
* used for debugging.
*/

#define ENABLE_CONSISTENCY_CHECKS


/*
* Defining the symbol GATHER_STATS will cause the GC to gather
* statistics on the objects it encounters in the heap.
*/

#define GATHER_STATS


/*
* Defining the symbol SHOW_FRAMES will cause the GC to print out a
* trace of the continuation frames that are processed.
*/

#undef SHOW_FRAMES

#endif


/*---------------------------------------------------------------------------*/

/*
* Object representation.
*
* Memory allocated Scheme objects can be allocated using one of three
* allocation strategies:
*
* Permanently allocated:
* These objects, called 'permanent objects' for short, are never
* moved or reclaimed, and all pointers to memory allocated
* objects they contain must point to permanent objects. As a
* consequence, the GC does not have to scan permanent objects.
* Permanent objects can be allocated on the C heap, but they are
* typically allocated in C global variables and structures that
* are set up when the program starts up or when a module is
* dynamically loaded.
*
* Still dynamically allocated:
* These objects, called 'still objects' for short, are allocated
* on the C heap. Still objects are never moved but they can be
* reclaimed by the GC. A mark-and-sweep GC is used to
* garbage-collect still objects.
*
* Movable dynamically allocated:
* These objects, called 'movable objects' for short, are allocated
* in an area of memory that is managed by a compacting GC. The GC
* can move and reclaim movable objects.
*
* Scheme objects are encoded using integers of type ___WORD. A
* ___WORD either encodes an immediate value or encodes a pointer
* when the object is memory allocated. The two lower bits of a
* ___WORD contain a primary type tag for the object and the other
* bits contain the immediate value or the pointer. Because all
* memory allocated objects are aligned on ___WORD boundaries (and a
* ___WORD is either 4 or 8 bytes), the two lower bits of pointers
* are zero and can be used to store the tag without reducing the
* address space. The four tags are:
*
* immediate:
* ___tFIXNUM object is a small integer (fixnum)
* ___tSPECIAL object is a boolean, character, or other immediate
*
* memory allocated:
* if ___USE_SAME_TAG_FOR_PAIRS_AND_SUBTYPED is defined
* ___tMEM1 = ___tSUBTYPED = ___tPAIR subtyped object, possibly a pair
* ___tMEM2 contained object, or a pair
* otherwise
* ___tMEM1 = ___tSUBTYPED subtyped object, but not a pair
* ___tMEM2 = ___tPAIR a pair
*
* A special type of object exists to support object finalization:
* 'will' objects. Wills contain a weak reference to an object, the
* testator and a strong reference to a procedure, the action
* procedure. A will becomes executable when its testator object is
* not strongly reachable (i.e. the testator object is either
* unreachable or only reachable using paths from the roots that
* traverse at least one weak reference). When the GC detects that a
* will has become executable it is placed on a list of executable
* wills. Following the GC, this list is traversed to invoke the
* action procedures.
*
* All memory allocated objects, including pairs, are composed of at
* least a head and a body. The head is a single ___WORD that
* contains 3 "head" tag bits (the 3 lower bits), a subtype tag (the
* next 5 bits), and the length of the object in bytes (the remaining
* bits). The head immediately precedes the body of the object, which
* contains the rest of the information associated with the object.
* Depending on the subtype, the body can contain raw binary data
* (such as when the object is a string) and Scheme objects (such as
* when the object is a vector). Memory allocated objects have the
* following layout:
*
* _head_ _____body______
* / \ / \
* +--------+--------+--------+
* |llllssst| | |
* +--------+--------+--------+
* ^ ^ ^
* | | |
* length | |
* subtype head tag
*
* Of the 8 possible head tags, only 5 are currently used:
*
* ___PERM (P) the object is a permanent object
* ___STILL (S) the object is a still object
* ___MOVABLE0 (M) the object is a movable object in generation 0
* ___FORW (F) the object has been moved by the GC (counts as 2 tags)
*
* Permanent objects have the following layout:
*
* _head_ _____body______
* / \ / \
* +--------+--------+--------+
* | P| | |
* +--------+--------+--------+
*
* Still objects have the following layout:
*
* _link_ _ref__ length _mark_ _head_ _____body______
* / \ / count\ / \ / \ / \ / \
* +--------+--------+--------+--------+--------+--------+--------+
* | | | | | S| | |
* +--------+--------+--------+--------+--------+--------+--------+
*
* All still objects are linked in a list using the 'link' field. The
* 'refcount' field contains a reference count, which counts the
* number of pointers to this object that are hidden from the GC
* (typically these hidden pointers are in C data structures). When
* 'refcount' is zero, the object will survive a GC only if it is
* pointed to by a GC root or a live Scheme object. The 'length'
* field contains the length of the object and is only used to
* maintain statistics on the space allocated. The 'mark' field is
* used by the GC to indicate that the object has been marked (at the
* start of a GC it is set to -1). The 'mark' field links all objects
* that have been marked but have not yet been scanned. It contains a
* pointer to the next still object that needs to be scanned.
*
* Movable objects have the following layout:
*
* _head_ _____body______
* / \ / \
* +--------+--------+--------+
* | M| | |
* +--------+--------+--------+
*
* When a movable object is moved by the GC, the head is replaced
* with a pointer to the copy, tagged with ___FORW.
*
* Layout of body.
*
* _head_ __________body__________
* / \ / \
* +--------+--------+--------+--------+
* | | field_0| field_1| etc. |
* +--------+--------+--------+--------+
*
* Some types of objects have bodies that only contain pointers to
* other Scheme objects. For example, pairs have two fields (car and
* cdr) and vectors have one field per element. Other object types
* have bodies that only contain raw binary data (such as strings and
* bignums). The remaining object types have bodies that contain both
* pointers to Scheme objects and raw binary data. Their layout is
* summarized below.
*
* Symbols:
* subtype = ___sSYMBOL
* field_0 = name (a Scheme string)
* field_1 = hash code (fixnum)
* field_2 = C pointer to global variable (0 if none allocated)
*
* Note: interned symbols must be permanently allocated;
* uninterned symbols can be permanent, still or movable
*
* Keywords:
* subtype = ___sKEYWORD
* field_0 = name (a Scheme string) not including the trailing ':'
* field_1 = hash code (fixnum)
*
* Procedures:
*
* nonclosures (toplevel procedures)
* subtype = ___sPROCEDURE (length contains parameter descriptor)
* field_0 = C pointer to field_0 - ___BODY_OFS
* field_1 = C pointer to label (only when using gcc)
* field_2 = C pointer to host C procedure
*
* closures:
* subtype = ___sPROCEDURE
* field_0 = C pointer to field_0 of entry procedure - ___BODY_OFS
* field_1 = free variable 1
* field_2 = free variable 2
* ...
*
* Note: the entry procedure must be a nonclosure procedure
*
* Return points:
* subtype = ___sPROCEDURE
* field_0 = return frame descriptor
* field_1 = C pointer to label (only when using gcc)
* field_2 = C pointer to host C procedure
*
* Wills:
* subtype = ___sWEAK
* field_0 = next will in list with special tag in lower bits
* field_1 = testator object
* field_2 = action procedure
*
* Note: wills must be movable
*
* GC hash tables:
* subtype = ___sWEAK
* field_0 = next GC hash table in list with special tag in lower bits
* field_1 = flags
* field_2 = count*2 (twice number of active key-value entries)
* field_3 = used*2 (twice number of total entries including deleted)
* field_4 = key of entry #0
* field_5 = value of entry #0
* ...
*
* Continuations:
* subtype = ___sCONTINUATION
* field_0 = first frame (C pointer to stack at first and then Scheme obj)
* field_1 = dynamic environment (#f when continuation is delimited)
*
* Frame:
* subtype = ___sFRAME
* field_0 = return address
* field_1 = frame slot 1
* field_2 = frame slot 2
* ...
*/


/*---------------------------------------------------------------------------*/

/*
* Movable Scheme objects are allocated in an area of memory
* distributed in multiple noncontiguous sections (collectively
* called the "msections"). All sections are of the same size and are
* allocated through the '___alloc_mem' function. The number of
* sections can expand and contract to accommodate the needs of the
* program.
*/

typedef struct msect
  {
    int index; /* index in list of sections */
    int pos; /* position in msections's 'sections' array */
    ___WORD *alloc; /* heap allocation pointer, grows towards high addr. */
    struct msect *prev; /* previous section in list of sections */
    struct msect *next; /* next section in list of sections */
    ___WORD base[1]; /* content of section */
  } msection;

#define sizeof_msection(n) (sizeof (msection) + ((n)-1) * ___WS)

typedef struct
  {
    int max_nb_sections; /* actual size of 'sections' array */
    int nb_sections; /* number of sections */
    msection *head; /* head of doubly-linked list of sections */
    msection *tail; /* tail of doubly-linked list of sections */
    msection *sections[1]; /* each section ordered by address */
                           /* (increasing order if ___ALLOC_MEM_UP */
                           /* is defined otherwise decreasing order) */
  } msections;

#define sizeof_msections(n) (sizeof (msections) + ((n)-1) * sizeof (msection*))


/* size of heap in words (number of words that can be occupied) */
___HIDDEN long heap_size;

/*
* 'normal_overflow_reserve' is the number of words reserved in the
* heap in normal circumstances for handling heap overflows.
*/
___HIDDEN long normal_overflow_reserve;

/*
* 'overflow_reserve' is the number of words currently reserved in the
* heap for handling heap overflows. Initially 'overflow_reserve' is
* set to 'normal_overflow_reserve'. When a heap overflow occurs,
* some fraction of the 'overflow_reserve' is made available to the
* heap overflow handler. When a GC makes at least
* 'normal_overflow_reserve' free, then 'overflow_reserve' is reset to
* 'normal_overflow_reserve'.
*/
___HIDDEN long overflow_reserve;

/* words occupied by nonmovable objects */
___HIDDEN long words_nonmovable;

/* words occupied in heap by movable objects excluding current msections */
___HIDDEN long words_prev_msections;

/* words occupied in heap by movable objects including current msections */
#define WORDS_MOVABLE \
(2*(words_prev_msections + \
(alloc_stack_start - alloc_stack_ptr) + \
(alloc_heap_ptr - alloc_heap_start)))

/* words occupied in heap including current msections */
#define WORDS_OCCUPIED (words_nonmovable + WORDS_MOVABLE)

/* words usable in msections */
#define WORDS_MOVABLE_USABLE \
(2*the_msections->nb_sections*(long)((___MSECTION_SIZE>>1)-___MSECTION_FUDGE+1))

/* words available in heap */
#define WORDS_AVAILABLE \
(words_nonmovable + WORDS_MOVABLE_USABLE - \
overflow_reserve - 2*___MSECTION_FUDGE)

/* list of still objects */
___HIDDEN ___WORD still_objs;

/* still objects remaining to scan */
___HIDDEN ___WORD still_objs_to_scan;

/* the msections */
___HIDDEN msections *the_msections;

/* location of tospace in each msection */
___HIDDEN ___BOOL tospace_at_top;

/* number of msections used */
___HIDDEN int nb_msections_used;

/* last msection allocated */
___HIDDEN msection *alloc_msection;

/* msection where continuation frames are currently being allocated */
___HIDDEN msection *stack_msection;

/* start of allocation of continuation frames in stack_msection */
___HIDDEN ___WORD *alloc_stack_start;

/* allocation pointer for continuation frames in stack_msection */
___HIDDEN ___WORD *alloc_stack_ptr;

/* allocation limit for continuation frames in stack_msection */
___HIDDEN ___WORD *alloc_stack_limit;

/* msection where movable objects are currently being allocated */
___HIDDEN msection *heap_msection;

/* start of allocation of movable objects in heap_msection */
___HIDDEN ___WORD *alloc_heap_start;

/* allocation pointer for movable objects in heap_msection */
___HIDDEN ___WORD *alloc_heap_ptr;

/* allocation limit for movable objects in heap_msection */
___HIDDEN ___WORD *alloc_heap_limit;

/* msection currently being scanned */
___HIDDEN msection *scan_msection;

/* scan pointer in msection being scanned */
___HIDDEN ___WORD *scan_ptr;

/* indicates if weak references must be traversed */
___HIDDEN ___BOOL traverse_weak_refs;

/* GC hash tables reached by GC */
___HIDDEN ___WORD reached_gc_hash_tables;

#ifdef CALL_GC_FREQUENTLY
int ___gc_calls_to_punt = 2000; /* for GC stress test */
#endif

/*
* A given msection can be used for allocating movable objects, or for
* allocating continuation frames, or for both. The position of the
* various pointers is as follows.
*
* Msection only used for allocating movable objects:
*
* <-------------------------- ___MSECTION_SIZE/2 ------------------------->
* +----+----+---------------------------------------------------------------+
* |obj1|obj2| |<-___MSECTION_FUDGE->|
* +----+----+---------------------------------------------------------------+
* ^ ^ ^ ^
* | | | |
* | alloc_heap_ptr ___ps->heap_limit alloc_heap_limit
* alloc_heap_start
*
* Msection only used for allocating continuation frames:
*
* <-------------------------- ___MSECTION_SIZE/2 ------------------------->
* +-----------------------------------------------------------+------+------+
* |<-___MSECTION_FUDGE->| |frame2|frame1|
* +-----------------------------------------------------------+------+------+
* ^ ^ ^ ^
* | | | |
* alloc_stack_limit ___ps->stack_limit alloc_stack_ptr |
* alloc_stack_start
*
* Msection used for allocating movable objects and allocating
* continuation frames:
*
* <-------------------------- ___MSECTION_SIZE/2 ------------------------->
* +----+-------------------------------------------------------------+------+
* |objs| |<-___MSECTION_FUDGE->| O.R. |<-___MSECTION_FUDGE->| |frames|
* +----+-------------------------------------------------------------+------+
* ^ ^ ^ ^ ^ ^ ^ ^
* | | | | | | | |
* | | | alloc_heap_limit alloc_stack_limit | | |
* | | ___ps->heap_limit ___ps->stack_limit | |
* | alloc_heap_ptr alloc_stack_ptr |
* alloc_heap_start alloc_stack_start
*/


/*---------------------------------------------------------------------------*/

/* Constants related to representation of permanent and still objects: */

#ifdef ___USE_HANDLES
#define ___PERM_HAND_OFS 0
#define ___PERM_BODY_OFS 2
#else
#define ___PERM_HAND_OFS ___PERM_BODY_OFS
#define ___PERM_BODY_OFS 1
#endif

#define ___STILL_LINK_OFS 0
#define ___STILL_REFCOUNT_OFS 1
#define ___STILL_LENGTH_OFS 2
#define ___STILL_MARK_OFS 3
#ifdef ___USE_HANDLES
#define ___STILL_HAND_OFS 4
#define ___STILL_BODY_OFS 6
#else
#define ___STILL_HAND_OFS ___STILL_BODY_OFS
#define ___STILL_BODY_OFS (5+1)/************/
#endif


/*---------------------------------------------------------------------------*/

/* Allocation and reclamation of aligned blocks of memory. */


/*
* 'alloc_mem_aligned (words, multiplier, modulus)' allocates an
* aligned block of memory through the '___alloc_mem' function.
* 'words' is the size of the block in words and 'multiplier' and
* 'modulus' specify its alignment in words. 'multiplier' must be a
* power of two and 0<=modulus<multiplier. The pointer returned
* corresponds to an address that is equal to
* (i*multiplier+modulus)*sizeof (___WORD) for some 'i'.
*/

___HIDDEN void *alloc_mem_aligned
   ___P((long words,
         unsigned int multiplier,
         unsigned int modulus),
        (words,
         multiplier,
         modulus)
long words;
unsigned int multiplier;
unsigned int modulus;)
{
  void *container; /* pointer to block returned by ___alloc_mem */
  unsigned int extra; /* space for alignment to multiplier */

  /* Make sure alignment is sufficient for pointers */

  if (multiplier < sizeof (void*) / ___WS)
    multiplier = sizeof (void*) / ___WS;

  /* How many extra bytes are needed for padding */

  extra = (multiplier * ___WS) - 1;
  if (modulus < sizeof (void*) / ___WS)
    extra += sizeof (void*);

  container = ___alloc_mem (extra + (words+modulus) * ___WS);

  if (container == 0)
    return 0;
  else
    {
      void *ptr = ___CAST(void*,
                          (((___CAST(___WORD,container) + extra) &
                            -___CAST(___WORD,multiplier * ___WS)) +
                           modulus * ___WS));
      void **cptr = ___CAST(void**,
                            (___CAST(___WORD,ptr) - ___CAST(___WORD,sizeof (void*))) &
                            -___CAST(___WORD,sizeof (void*)));

      *cptr = container;
      return ptr;
    }
}


/*
* 'free_mem_aligned (ptr)' reclaims the aligned block of memory 'ptr'
* that was allocated using 'alloc_mem_aligned'.
*/

___HIDDEN void free_mem_aligned
   ___P((void *ptr),
        (ptr)
void *ptr;)
{
  void **cptr = ___CAST(void**,
                        (___CAST(___WORD,ptr) - ___CAST(___WORD,sizeof (void*))) &
                        -___CAST(___WORD,sizeof (void*)));
  ___free_mem (*cptr);
}


/*---------------------------------------------------------------------------*/

/* Allocation of reference counted blocks of memory. */

typedef struct rc_header_struct
  {
    struct rc_header_struct *prev;
    struct rc_header_struct *next;
    ___SCMOBJ refcount; /* integer but declared ___SCMOBJ for alignment */
    ___SCMOBJ data; /* needed for C closures */
  } rc_header;


___HIDDEN rc_header rc_head;


___HIDDEN void setup_rc ___PVOID
{
  rc_head.prev = &rc_head;
  rc_head.next = &rc_head;
  rc_head.refcount = 1;
  rc_head.data = ___FAL;
}

___HIDDEN void cleanup_rc ___PVOID
{
  rc_header *h = rc_head.next;

  rc_head.prev = &rc_head;
  rc_head.next = &rc_head;

  while (h != &rc_head)
    {
      rc_header *next = h->next;
      ___free_mem (h);
      h = next;
    }
}


___EXP_FUNC(void*,___alloc_rc)
   ___P((unsigned long bytes),
        (bytes)
unsigned long bytes;)
{
  rc_header *h = ___CAST(rc_header*,
                         ___alloc_mem (bytes + sizeof (rc_header)));

  if (h != 0)
    {
      rc_header *head = &rc_head;
      rc_header *tail = head->prev;

      h->prev = tail;
      h->next = head;
      head->prev = h;
      tail->next = h;

      h->refcount = 1;
      h->data = ___FAL;

      return ___CAST(void*,h+1);
    }

  return 0;
}


___EXP_FUNC(void,___release_rc)
   ___P((void *ptr),
        (ptr)
void *ptr;)
{
  if (ptr != 0)
    {
      rc_header *h = ___CAST(rc_header*,ptr) - 1;

      if (--h->refcount == 0)
        {
          rc_header *prev = h->prev;
          rc_header *next = h->next;

          next->prev = prev;
          prev->next = next;

          ___free_mem (h);
        }
    }
}


___EXP_FUNC(void,___addref_rc)
   ___P((void *ptr),
        (ptr)
void *ptr;)
{
  if (ptr != 0)
    {
      rc_header *h = ___CAST(rc_header*,ptr) - 1;
      h->refcount++;
    }
}


___EXP_FUNC(___SCMOBJ,___data_rc)
   ___P((void *ptr),
        (ptr)
void *ptr;)
{
  rc_header *h = ___CAST(rc_header*,ptr) - 1;
  return h->data;
}


___EXP_FUNC(void,___set_data_rc)
   ___P((void *ptr,
         ___SCMOBJ val),
        (ptr,
         val)
void *ptr;
___SCMOBJ val;)
{
  rc_header *h = ___CAST(rc_header*,ptr) - 1;
  h->data = val;
}


/*---------------------------------------------------------------------------*/

/* Allocation of movable objects. */

/*
* 'find_msection (ms, ptr)' finds the position in the 'ms->sections'
* array of the msection that contains the pointer 'ptr'. More
* precisely, if ___ALLOC_MEM_UP is defined, it returns the integer
* 'i' (-1<=i<=n-1) such that 'ptr' is between the start of section i
* and section i+1. -1 is returned if 'ptr' is lower than the lowest
* section and 'n' is returned if 'ptr' is not lower than the highest
* section. If ___ALLOC_MEM_UP is not defined, it returns the integer
* 'i' (0<=i<=n) such that 'ptr' is between the start of section i and
* section i-1. n is returned if 'ptr' is lower than the lowest
* section and 0 is returned if 'ptr' is not lower than the highest
* section.
*/

___HIDDEN int find_msection
   ___P((msections *ms,
         void *ptr),
        (ms,
         ptr)
msections *ms;
void *ptr;)
{
  int ns = ms->nb_sections;
  msection **sections = ms->sections;
  int lo, hi;

#ifdef ___ALLOC_MEM_UP
  if (ns == 0 ||
      ptr < ___CAST(void*,sections[0]))
    return -1;
#else
  if (ns == 0 ||
      ptr < ___CAST(void*,sections[ns-1]))
    return ns;
#endif

  /* binary search */

  lo = 0;
  hi = ns-1;

  /* loop invariant: lo <= find_msection (ms, ptr) <= hi */

  while (lo < hi)
    {
      int mid = (lo+hi) / 2; /* lo <= mid < hi */
#ifdef ___ALLOC_MEM_UP
      if (ptr < ___CAST(void*,sections[mid+1])) hi = mid; else lo = mid+1;
#else
      if (ptr < ___CAST(void*,sections[mid])) lo = mid+1; else hi = mid;
#endif
    }

  return lo;
}


/*
* 'adjust_msections (msp, n)' contracts or expands the msections
* pointed to by 'msp' so that it contains 'n' sections. When the
* msections is contracted, the last sections allocated (i.e. those at
* the end of the doubly-linked list of sections) will be reclaimed.
* When expanding the msections there may not be enough memory to
* allocate new sections so the operation may fail. However
* 'adjust_msections' will always leave the msections in a consistent
* state and there will be at least as many sections as when the
* expansion was started. Failure can be detected by checking the
* 'nb_sections' field.
*/

___HIDDEN void adjust_msections
   ___P((msections **msp,
         int n),
        (msp,
         n)
msections **msp;
int n;)
{
  int max_ns, ns;
  msections *ms = *msp;
  msection *hd;
  msection *tl;

  if (ms == 0)
    {
      max_ns = 0;
      ns = 0;
      hd = 0;
      tl = 0;
    }
  else
    {
      max_ns = ms->max_nb_sections;
      ns = ms->nb_sections;
      hd = ms->head;
      tl = ms->tail;
    }

  if (ms == 0 || n > max_ns)
    {
      /* must allocate a new msections structure */

      msections *new_ms;
      int i;

      while (n > max_ns) /* grow max_nb_sections until big enough */
        max_ns = 2*max_ns + 1;

      new_ms = ___CAST(msections*,
                       alloc_mem_aligned
                         (___WORDS(sizeof_msections(max_ns)),
                          1,
                          0));

      if (new_ms == 0)
        return;

      new_ms->max_nb_sections = max_ns;
      new_ms->nb_sections = ns;
      new_ms->head = hd;
      new_ms->tail = tl;

      for (i=ns-1; i>=0; i--)
        new_ms->sections[i] = ms->sections[i];

      if (ms != 0)
        free_mem_aligned (ms);

      ms = new_ms;

      *msp = ms;
    }

  if (n < ns)
    {
      /* contraction of the msections */

      int j;

      while (ns > n)
        {
          msection *s = tl;

          tl = tl->prev;

          if (tl == 0)
            hd = 0;
          else
            tl->next = 0;

          for (j=s->pos; j<ns-1; j++)
            {
              ms->sections[j] = ms->sections[j+1];
              ms->sections[j]->pos = j;
            }

          free_mem_aligned (s);

          ns--;
        }

      ms->nb_sections = ns;
      ms->head = hd;
      ms->tail = tl;

      /*
* Contraction of the msections structure is not performed
* because there is typically very little memory to be
* reclaimed.
*/
    }
  else
    {
      /* expansion of the msections */

      int i, j;

      while (ns < n)
        {
          msection *s = ___CAST(msection*,
                                alloc_mem_aligned
                                  (___WORDS(sizeof_msection(___MSECTION_SIZE)),
                                   1,
                                   0));

          if (s == 0)
            return;

          i = find_msection (ms, ___CAST(void*,s));

#ifdef ___ALLOC_MEM_UP
          i++;
#endif

          for (j=ns; j>i; j--)
            {
              ms->sections[j] = ms->sections[j-1];
              ms->sections[j]->pos = j;
            }

          ms->sections[i] = s;

          if (tl == 0)
            {
              hd = s;
              s->index = 0;
            }
          else
            {
              tl->next = s;
              s->index = tl->index + 1;
            }

          s->pos = i;
          s->prev = tl;
          s->next = 0;

          tl = s;

          ms->nb_sections = ++ns;
          ms->head = hd;
          ms->tail = tl;
        }
    }
}


/*
* 'free_msections (msp)' releases all memory associated with the
* msections pointed to by 'msp'.
*/

___HIDDEN void free_msections
   ___P((msections **msp),
        (msp)
msections **msp;)
{
  msections *ms = *msp;

  if (ms != 0)
    {
      int i;

      for (i=ms->nb_sections-1; i>=0; i--)
        free_mem_aligned (ms->sections[i]);

      free_mem_aligned (ms);

      *msp = 0;
    }
}


/*---------------------------------------------------------------------------*/

/* Allocation of permanent objects. */

/*
* Permanent objects are allocated in sections called "psections".
* Each section contains multiple objects. The sections are kept in a
* list so that the storage they occupy can be reclaimed when the
* program terminates.
*/

___HIDDEN void *psections; /* list of psections */
___HIDDEN ___WORD *palloc_ptr; /* allocation pointer in current psection */
___HIDDEN ___WORD *palloc_limit; /* allocation limit in current psection */


/*
* 'alloc_mem_aligned_psection (words, multiplier, modulus)' allocates
* an aligned block of memory inside a new psection. 'words' is the
* size of the block in words and 'multiplier' and 'modulus' specify
* its alignment in words. 'multiplier' must be a power of two and
* 0<=modulus<multiplier. The pointer returned corresponds to an
* address that is equal to (i*multiplier+modulus)*sizeof (___WORD) for
* some 'i'.
*/

___HIDDEN void *alloc_mem_aligned_psection
   ___P((long words,
         unsigned int multiplier,
         unsigned int modulus),
        (words,
         multiplier,
         modulus)
long words;
unsigned int multiplier;
unsigned int modulus;)
{
  void *container;

  /* Make sure alignment is sufficient for pointers */

  if (multiplier < sizeof (void*) / ___WS)
    multiplier = sizeof (void*) / ___WS;

  /* Make space for psection link and modulus */

  if (modulus < (sizeof (void*) + ___WS - 1) / ___WS)
    modulus += ((sizeof (void*) + multiplier * ___WS - 1) / ___WS) &
               -multiplier;

  /* Allocate container */

  container = alloc_mem_aligned (words+modulus, multiplier, 0);

  if (container == 0)
    return 0;

  *___CAST(void**,container) = psections;
  psections = container;
  return ___CAST(void*,___CAST(___WORD*,container) + modulus);
}


/*
* 'alloc_mem_aligned_perm (words, multiplier, modulus)' allocates an
* aligned block of memory inside a psection. If there is enough free
* space in a previously allocated psection that psection is used,
* otherwise a new psection is allocated. 'words' is the size of the
* block in words and 'multiplier' and 'modulus' specify its alignment
* in words. 'multiplier' must be a power of two and
* 0<=modulus<multiplier. The pointer returned corresponds to an
* address that is equal to (i*multiplier+modulus)*sizeof (___WORD) for
* some 'i'.
*/

___HIDDEN void *alloc_mem_aligned_perm
   ___P((long words,
         int multiplier,
         int modulus),
        (words,
         multiplier,
         modulus)
long words;
int multiplier;
int modulus;)
{
  long waste;
  ___WORD *base;

  /*
* Try to satisfy request in current psection.
*/

  if (palloc_ptr != 0)
    {
      ___WORD *new_palloc_ptr;

      base = ___CAST(___WORD*,
                     ___CAST(___WORD,palloc_ptr+multiplier-1-modulus) &
                     (multiplier * -___WS)) +
             modulus;

      new_palloc_ptr = base + words;

      if (new_palloc_ptr <= palloc_limit) /* did it fit in the psection? */
        {
          palloc_ptr = new_palloc_ptr;
          return base;
        }

      waste = palloc_limit - palloc_ptr;
    }
  else
    waste = 0;

  /*
* Request can't be satisfied in current psection so we must
* allocate a new psection.
*/

  if (waste > ___PSECTION_WASTE || words > ___PSECTION_SIZE)
    return alloc_mem_aligned_psection (words, multiplier, modulus);

  base = ___CAST(___WORD*,
                 alloc_mem_aligned_psection
                   (___PSECTION_SIZE,
                    multiplier,
                    modulus));

  if (base != 0)
    {
      palloc_ptr = base + words;
      palloc_limit = base + ___PSECTION_SIZE;
    }

  return base;
}


___HIDDEN void free_psections ___PVOID
{
  void *base = psections;

  psections = 0;

  while (base != 0)
    {
      void *link = *___CAST(void**,base);
      free_mem_aligned (base);
      base = link;
    }
}


___SCMOBJ ___alloc_global_var
   ___P((___glo_struct **glo),
        (glo)
___glo_struct **glo;)
{
  ___glo_struct *p = ___CAST(___glo_struct*,
                             alloc_mem_aligned_perm
                               (___WORDS(sizeof (___glo_struct)),
                                1,
                                0));
  if (p == 0)
    return ___FIX(___HEAP_OVERFLOW_ERR);
  *glo = p;
  return ___FIX(___NO_ERR);
}


/*---------------------------------------------------------------------------*/

/*
* '___still_obj_refcount_inc (obj)' increments the reference count of
* the still object 'obj'.
*/

___EXP_FUNC(void,___still_obj_refcount_inc)
   ___P((___WORD obj),
        (obj)
___WORD obj;)
{
  ___UNTAG(obj)[___BODY_OFS - ___STILL_BODY_OFS + ___STILL_REFCOUNT_OFS]++;
}


/*
* '___still_obj_refcount_dec (obj)' decrements the reference count of
* the still object 'obj'.
*/

___EXP_FUNC(void,___still_obj_refcount_dec)
   ___P((___WORD obj),
        (obj)
___WORD obj;)
{
  ___UNTAG(obj)[___BODY_OFS - ___STILL_BODY_OFS + ___STILL_REFCOUNT_OFS]--;
}


/*---------------------------------------------------------------------------*/

/*
* '___alloc_scmobj (subtype, bytes, kind)' allocates a permanent or
* still Scheme object (depending on 'kind') of subtype 'subtype' with
* a body containing 'bytes' bytes, and returns it as an encoded
* Scheme object. A permanent object is allocated when 'kind' =
* ___PERM and a still object is allocated when 'kind' = ___STILL.
* The initialization of the object's body must be done by the caller.
* In the case of still objects this initialization must be done
* before the next allocation is requested. The 'refcount' field of
* still objects is initially 1. A fixnum error code is returned when
* there is an error.
*/

___EXP_FUNC(___WORD,___alloc_scmobj)
   ___P((int subtype,
         long bytes,
         int kind),
        (subtype,
         bytes,
         kind)
int subtype;
long bytes;
int kind;)
{
  void *ptr;
  ___processor_state ___ps = ___PSTATE;
  long words = (kind==___PERM ? ___PERM_BODY_OFS : ___STILL_BODY_OFS)
               + ___WORDS(bytes);

  alloc_stack_ptr = ___ps->fp; /* needed by 'WORDS_OCCUPIED' */
  alloc_heap_ptr = ___ps->hp; /* needed by 'WORDS_OCCUPIED' */

  words_nonmovable += words;

  if (WORDS_OCCUPIED > heap_size
#ifdef CALL_GC_FREQUENTLY
      || --___gc_calls_to_punt < 0
#endif
     )
    {
      ___BOOL overflow;

      words_nonmovable -= words;

      overflow = ___garbage_collect (words);

      words_nonmovable += words;

      alloc_stack_ptr = ___ps->fp; /* needed by 'WORDS_OCCUPIED' */
      alloc_heap_ptr = ___ps->hp; /* needed by 'WORDS_OCCUPIED' */

      if (overflow || WORDS_OCCUPIED > heap_size)
        {
          words_nonmovable -= words;
          return ___FIX(___HEAP_OVERFLOW_ERR);
        }
    }

  /*
* Some objects, such as ___sFOREIGN, ___sS64VECTOR, ___sU64VECTOR,
* ___sF64VECTOR, ___sFLONUM and ___sBIGNUM, must have a body that
* is aligned on a multiple of 8 on some machines. Here, we force
* alignment to a multiple of 8 even if not necessary in all cases
* because it is typically more efficient due to a better
* utilization of the cache.
*/

  if (kind == ___PERM)
    ptr = alloc_mem_aligned_perm (words,
                                  8>>___LWS,
                                  (-___PERM_BODY_OFS)&((8>>___LWS)-1));
  else
    ptr = alloc_mem_aligned (words,
                             8>>___LWS,
                             (-___STILL_BODY_OFS)&((8>>___LWS)-1));

  if (ptr == 0)
    {
      words_nonmovable -= words;
      return ___FIX(___HEAP_OVERFLOW_ERR);
    }
  else if (kind == ___PERM)
    {
      ___WORD *base = ___CAST(___WORD*,ptr);

#ifdef ___USE_HANDLES
      base[___PERM_HAND_OFS] = ___CAST(___WORD,base+___PERM_BODY_OFS-___BODY_OFS);
#endif
      base[___PERM_BODY_OFS-1] = ___MAKE_HD(bytes, subtype, ___PERM);

      return ___TAG((base + ___PERM_HAND_OFS - ___BODY_OFS),
#if ___tPAIR == ___tSUBTYPED
                    ___tSUBTYPED
#else
                    (subtype == ___sPAIR ? ___tPAIR : ___tSUBTYPED)
#endif
                   );
    }
  else
    {
      ___WORD *base = ___CAST(___WORD*,ptr);

      base[___STILL_LINK_OFS] = still_objs;
      still_objs = ___CAST(___WORD,base);
      base[___STILL_REFCOUNT_OFS] = 1;
      base[___STILL_LENGTH_OFS] = words;
#ifdef ___USE_HANDLES
      base[___STILL_HAND_OFS] = ___CAST(___WORD,base+___STILL_BODY_OFS-___BODY_OFS);
#endif
      base[___STILL_BODY_OFS-1] = ___MAKE_HD(bytes, subtype, ___STILL);

      return ___TAG((base + ___STILL_HAND_OFS - ___BODY_OFS),
#if ___tPAIR == ___tSUBTYPED
                    ___tSUBTYPED
#else
                    (subtype == ___sPAIR ? ___tPAIR : ___tSUBTYPED)
#endif
                   );
    }
}


___EXP_FUNC(void,___release_scmobj)
   ___P((___WORD obj),
        (obj)
___WORD obj;)
{
  if (___MEM_ALLOCATED(obj) &&
      ___HD_TYP(___BODY(obj)[-1]) == ___STILL)
    ___still_obj_refcount_dec (obj);
}


/*
* '___make_pair (car, cdr, kind)' creates a Scheme pair having the
* values 'car' and 'cdr' in its CAR and CDR fields. The 'car' and
* 'cdr' arguments must not be movable objects and any still object
* must be reachable some other way or have a nonzero refcount. A
* permanent or still object is allocated, depending on 'kind'
* (___PERM for permanent object, ___STILL for still object). A
* fixnum error code is returned when there is an error.
*/

___EXP_FUNC(___WORD,___make_pair)
   ___P((___WORD car,
         ___WORD cdr,
         int kind),
        (car,
         cdr,
         kind)
___WORD car;
___WORD cdr;
int kind;)
{
  ___WORD obj = ___alloc_scmobj (___sPAIR, ___PAIR_SIZE<<___LWS, kind);

  if (!___FIXNUMP(obj))
    {
      ___PAIR_CAR(obj) = car;
      ___PAIR_CDR(obj) = cdr;
    }

  return obj;
}


/*
* '___make_vector (length, init, kind)' creates a Scheme vector of
* length 'length' and initialized with the value 'init'. The 'init'
* argument must not be a movable object and if it is a still object
* it must be reachable some other way or have a nonzero refcount. A
* permanent or still object is allocated, depending on 'kind'
* (___PERM for permanent object, ___STILL for still object). A
* fixnum error code is returned when there is an error.
*/

___EXP_FUNC(___WORD,___make_vector)
   ___P((long length,
         ___WORD init,
         int kind),
        (length,
         init,
         kind)
long length;
___WORD init;
int kind;)
{
  if (length > ___CAST(___WORD,___LMASK >> (___LF+___LWS)))
    return ___FIX(___HEAP_OVERFLOW_ERR);
  else
    {
      ___WORD obj = ___alloc_scmobj (___sVECTOR, length<<___LWS, kind);

      if (!___FIXNUMP(obj))
        {
          int i;
          for (i=0; i<length; i++)
            ___FIELD(obj, i) = init;
        }

      return obj;
    }
}


/*---------------------------------------------------------------------------*/

___HIDDEN ___WORD *start_of_fromspace
   ___P((msection *s),
        (s)
msection *s;)
{
  if (tospace_at_top)
    return s->base;
  else
    return s->base + (___MSECTION_SIZE>>1);
}


___HIDDEN ___WORD *start_of_tospace
   ___P((msection *s),
        (s)
msection *s;)
{
  if (tospace_at_top)
    return s->base + (___MSECTION_SIZE>>1);
  else
    return s->base;
}


___HIDDEN void fatal_heap_overflow ___PVOID
{
  char *msgs[2];
  msgs[0] = "Heap overflow";
  msgs[1] = 0;
  ___fatal_error (msgs);
}


___HIDDEN msection *next_msection
   ___P((msection *ms),
        (ms)
msection *ms;)
{
  msection *result;

  if (nb_msections_used == 0)
    result = the_msections->head;
  else
    result = alloc_msection->next;

  if (result == 0)
    {
      if (stack_msection == heap_msection)
        fatal_heap_overflow ();
      result = ms;
    }
  else
    {
      alloc_msection = result;
      nb_msections_used++;
    }

  return result;
}


___HIDDEN void next_stack_msection ___PVOID
{
  if (stack_msection != 0)
    words_prev_msections += alloc_stack_start - alloc_stack_ptr;

  stack_msection = next_msection (heap_msection);
  alloc_stack_limit = start_of_tospace (stack_msection);
  alloc_stack_start = alloc_stack_limit + (___MSECTION_SIZE>>1);
  alloc_stack_ptr = alloc_stack_start;
}


___HIDDEN void next_heap_msection ___PVOID
{
  if (heap_msection != 0)
    {
      words_prev_msections += alloc_heap_ptr - alloc_heap_start;
      heap_msection->alloc = alloc_heap_ptr;
    }

  heap_msection = next_msection (stack_msection);
  alloc_heap_start = start_of_tospace (heap_msection);
  alloc_heap_limit = alloc_heap_start + (___MSECTION_SIZE>>1);
  alloc_heap_ptr = alloc_heap_start;
}


/*---------------------------------------------------------------------------*/

#ifdef ___DEBUG

void print_string
   ___P((___SCMOBJ str),
        (str)
___SCMOBJ str;)
{
  int i;
  for (i=0; i<___INT(___STRINGLENGTH(str)); i++)
    ___printf ("%c", ___INT(___STRINGREF(str,___FIX(i))));
}

void print_subtyped
   ___P((___SCMOBJ val),
        (val)
___SCMOBJ val;)
{
  ___SCMOBJ ___temp;
  if (___SUBTYPEDP(val))
    {
      ___SCMOBJ sym;
      if (___PROCEDUREP(val) || ___RETURNP(val))
        {
          if (___PROCEDUREP(val))
            ___printf ("#<procedure ");
          else
            ___printf ("#<return ");
          if ((sym = find_global_var_bound_to (val)) != ___NUL)
            print_string (___FIELD(sym,___SYMKEY_NAME));
          else
            {
              if (___HD_TYP(___HEADER(val)) == ___PERM)
                {
                  ___SCMOBJ *start = ___CAST(___SCMOBJ*,val-___tSUBTYPED);
                  ___SCMOBJ *ptr = start;
                  while (!___TESTHEADERTAG(*ptr,___sVECTOR))
                    ptr -= ___LS;
                  ptr += ___LS;
                  if (ptr == start)
                    ___printf ("???");
                  else
                    {
                      ___printf ("%d in ", (start-ptr)/___LS);
                      print_subtyped (___TAG(ptr,___tSUBTYPED));
                    }
                }
              else
                ___printf ("???");
            }
          ___printf (">");
        }
      else if (___STRINGP(val))
        {
          ___printf ("\"");
          print_string (val);
          ___printf ("\"");
        }
      else
        {
          ___printf ("#<other ???>");
        }
    }
}

void print_value
   ___P((___SCMOBJ val),
        (val)
___SCMOBJ val;)
{
  ___SCMOBJ ___temp;
  if (___FIXNUMP(val))
    ___printf ("%d", ___INT(val));
  else if (val == ___FAL)
    ___printf ("#f");
  else if (val == ___TRU)
    ___printf ("#t");
  else if (val == ___NUL)
    ___printf ("()");
  else if (val == ___EOF)
    ___printf ("#!eof");
  else if (val == ___VOID)
    ___printf ("#!void");
  else if (___CHARP(val))
    ___printf ("#\\x%x", ___INT(val));
  else if (___PAIRP(val))
    ___printf ("0x%08x (... . ...)", val);
  else if (___SUBTYPEDP(val))
    {
      ___SCMOBJ sym;
      ___printf ("0x%08x ", val);
      print_subtyped (val);
    }
  else
    ___printf ("0x%08x other", val);
}

#endif


#ifdef ENABLE_CONSISTENCY_CHECKS

___HIDDEN int reference_location; /* where is offending reference located */

#define IN_OBJECT 0
#define IN_REGISTER 1
#define IN_GLOBAL_VAR 2
#define IN_WILL_LIST 3
#define IN_CONTINUATION 4
#define IN_RC 5

___HIDDEN ___WORD *container_body; /* pointer to body of object */
                                   /* containing offending reference */

___HIDDEN int mark_array_call_line;


___HIDDEN void print_prefix
   ___P((char *prefix,
         int indent),
        (prefix,
         indent)
char *prefix;
int indent;)
{
  int i;

  ___printf ("%s", prefix);

  for (i=0; i<indent; i++)
    ___printf (" ");
}


___HIDDEN void print_object
   ___P((___WORD obj,
         int max_depth,
         char *prefix,
         int indent),
        (obj,
         max_depth,
         prefix,
         indent)
___WORD obj;
int max_depth;
char *prefix;
int indent;)
{
  int typ = ___TYP(obj);

  print_prefix (prefix, indent);

  if (typ == ___tFIXNUM)
    ___printf ("%d\n", ___INT(obj));
  else if (typ == ___tSPECIAL)
    {
      if (obj >= 0)
        ___printf ("#\\%c\n", ___INT(obj));
      else if (obj == ___FAL)
        ___printf ("#f\n");
      else if (obj == ___TRU)
        ___printf ("#t\n");
      else if (obj == ___NUL)
        ___printf ("()\n");
      else if (obj == ___EOF)
        ___printf ("#!eof\n");
      else if (obj == ___VOID)
        ___printf ("#!void\n");
      else if (obj == ___ABSENT)
        ___printf ("#<absent>\n");
      else if (obj == ___UNB1)
        ___printf ("#<unbound1>\n");
      else if (obj == ___UNB2)
        ___printf ("#<unbound2>\n");
      else if (obj == ___OPTIONAL)
        ___printf ("#!optional\n");
      else if (obj == ___KEY_OBJ)
        ___printf ("#!key\n");
      else if (obj == ___REST)
        ___printf ("#!rest\n");
      else if (obj == ___UNUSED)
        ___printf ("#<unused>\n");
      else if (obj == ___DELETED)
        ___printf ("#<deleted>\n");
      else
        ___printf ("#<unknown>\n");
    }
  else
    {
      ___WORD* body = ___BODY(obj);
      ___WORD head = body[-1];
      int subtype = ___HD_SUBTYPE(head);

      switch (subtype)
        {
        case ___sVECTOR:
          if (max_depth > 0)
            {
              int i;
              ___printf ("#(\n");
              for (i=0; i<___CAST(int,___HD_WORDS(head)); i++)
                print_object (___FIELD(obj,i), max_depth-1, prefix, indent+2);
              print_prefix (prefix, indent);
              ___printf (")\n");
            }
          else
            ___printf ("#(...)\n");
          break;
        case ___sPAIR:
          if (max_depth > 0)
            {
              ___printf ("(\n");
              print_object (___CAR(obj), max_depth-1, prefix, indent+1);
              print_prefix (prefix, indent);
              ___printf (" .\n");
              print_object (___CDR(obj), max_depth-1, prefix, indent+1);
              print_prefix (prefix, indent);
              ___printf (")\n");
            }
          else
            ___printf ("(...)\n");
          break;
        case ___sRATNUM:
          ___printf ("RATNUM\n");
          break;
        case ___sCPXNUM:
          ___printf ("CPXNUM\n");
          break;
        case ___sSTRUCTURE:
          ___printf ("STRUCTURE\n");
          break;
        case ___sBOXVALUES:
          ___printf ("BOXVALUES\n");
          break;
        case ___sMEROON:
          ___printf ("MEROON\n");
          break;
        case ___sSYMBOL:
          ___printf ("SYMBOL\n");
          break;
        case ___sKEYWORD:
          ___printf ("KEYWORD\n");
          break;
        case ___sFRAME:
          ___printf ("FRAME\n");
          break;
        case ___sCONTINUATION:
          ___printf ("CONTINUATION\n");
          break;
        case ___sPROMISE:
          ___printf ("PROMISE\n");
          break;
        case ___sWEAK:
          ___printf ("WEAK\n");
          break;
        case ___sPROCEDURE:
          ___printf ("PROCEDURE\n");
          break;
        case ___sRETURN:
          ___printf ("RETURN\n");
          break;
        case ___sFOREIGN:
          ___printf ("FOREIGN\n");
          break;
        case ___sSTRING:
          ___printf ("STRING\n");
          break;
        case ___sS8VECTOR:
          ___printf ("S8VECTOR\n");
          break;
        case ___sU8VECTOR:
          ___printf ("U8VECTOR\n");
          break;
        case ___sS16VECTOR:
          ___printf ("S16VECTOR\n");
          break;
        case ___sU16VECTOR:
          ___printf ("U16VECTOR\n");
          break;
        case ___sS32VECTOR:
          ___printf ("S32VECTOR\n");
          break;
        case ___sU32VECTOR:
          ___printf ("U32VECTOR\n");
          break;
        case ___sF32VECTOR:
          ___printf ("F32VECTOR\n");
          break;
        case ___sS64VECTOR:
          ___printf ("S64VECTOR\n");
          break;
        case ___sU64VECTOR:
          ___printf ("U64VECTOR\n");
          break;
        case ___sF64VECTOR:
          ___printf ("F64VECTOR\n");
          break;
        case ___sFLONUM:
          ___printf ("FLONUM\n");
          break;
        case ___sBIGNUM:
          ___printf ("BIGNUM\n");
          break;
        default:
          ___printf ("UNKNOWN\n");
          break;
        }
    }
}


___HIDDEN void print_global_var_name
   ___P((___glo_struct *glo),
        (glo)
___glo_struct *glo;)
{
  ___SCMOBJ sym = ___NUL;
  int i;

  for (i = ___INT(___VECTORLENGTH(___GSTATE->symbol_table)) - 1; i>0; i--)
    {
      sym = ___FIELD(___GSTATE->symbol_table,i);

      while (sym != ___NUL)
        {
          ___SCMOBJ g = ___FIELD(sym,___SYMBOL_GLOBAL);

          if (g != ___FIX(0))
            {
              ___glo_struct *p = ___CAST(___glo_struct*,g);

              if (p == glo)
                {
                  ___SCMOBJ name = ___FIELD(sym,___SYMKEY_NAME);
                  for (i=0; i<___INT(___STRINGLENGTH(name)); i++)
                    ___printf ("%c", ___INT(___STRINGREF(name,___FIX(i))));
                  i = 0;
                  break;
                }
            }

          sym = ___FIELD(sym,___SYMKEY_NEXT);
        }
    }
}


___HIDDEN void dump_memory_map ___PVOID
{
  int ns = the_msections->nb_sections;
  msection **sections = the_msections->sections;
  int i;

  ___printf (">>> Memory map:\n");

  for (i=0; i<ns; i++)
    ___printf (">>> msection %2d: 0x%08x .. 0x%08x .. 0x%08x\n",
               i,
               sections[i]->base,
               sections[i]->base + (___MSECTION_SIZE>>1),
               sections[i]->base + ___MSECTION_SIZE);

  ___printf (">>> alloc_msection = 0x%08x\n", alloc_msection);
  ___printf (">>> stack_msection = 0x%08x\n", stack_msection);
  ___printf (">>> heap_msection = 0x%08x\n", heap_msection);
  ___printf (">>> scan_msection = 0x%08x\n", scan_msection);
  ___printf (">>> alloc_stack_ptr = 0x%08x\n", alloc_stack_ptr);
  ___printf (">>> alloc_stack_limit = 0x%08x\n", alloc_stack_limit);
  ___printf (">>> alloc_heap_limit = 0x%08x\n", alloc_heap_limit);
  ___printf (">>> alloc_heap_ptr = 0x%08x\n", alloc_heap_ptr);
  ___printf (">>> scan_ptr = 0x%08x\n", scan_ptr);
  ___printf (">>> scan_msection->alloc = 0x%08x\n", scan_msection->alloc);
}

___HIDDEN void explain_problem
   ___P((___WORD obj,
         char *msg),
        (obj,
         msg)
___WORD obj;
char *msg;)
{
  dump_memory_map ();

  ___printf (">>> The object 0x%08x %s\n", obj, msg);

  {
    int j;
    for (j=-1; j<10; j++)
      {
        ___printf (">>> body[%2d] = 0x%08x\n", j, ___BODY(obj)[j]);
        print_object (___BODY(obj)[j], 1, ">>> ", 0);
      }
  }

  switch (reference_location)
    {
    case IN_OBJECT:
      {
        ___WORD container;
        ___WORD head = container_body[-1];
        long words = ___HD_WORDS(head);
        int subtype = ___HD_SUBTYPE(head);
        int i;

#if ___tPAIR != ___tSUBTYPED
        if (subtype == ___sPAIR)
          container = ___TAG(container_body-___BODY_OFS,___tPAIR);
        else
#endif
          container = ___TAG(container_body-___BODY_OFS,___tSUBTYPED);

        ___printf (">>> The reference was found in ");
        if (___HD_TYP(head) == ___PERM)
          ___printf ("___PERM ");
        else if (___HD_TYP(head) == ___STILL)
          ___printf ("___STILL ");
        else if (___HD_TYP(head) == ___MOVABLE0)
          ___printf ("___MOVABLE0 ");
        else if (___TYP(head) == ___FORW)
          ___printf ("___FORW ");
        else
          ___printf ("UNKNOWN ");
        ___printf ("object with body at 0x%08x:\n", container_body);

        ___printf (">>> subtype = %d\n", subtype);
        ___printf (">>> length = %ld words\n", words);
        if (words <= 100)
          {
            for (i=0; i<words; i++)
              ___printf (">>> body[%2d] = 0x%08x\n", i, container_body[i]);
          }
        else
          {
            for (i=0; i<50; i++)
              ___printf (">>> body[%2d] = 0x%08x\n", i, container_body[i]);
            ___printf ("...\n");
            for (i=words-50; i<words; i++)
              ___printf (">>> body[%2d] = 0x%08x\n", i, container_body[i]);
          }
        ___printf (">>> container =\n");
        print_object (container, 4, ">>> ", 0);
        break;
      }

    case IN_REGISTER:
      ___printf (">>> The reference was found in a register\n");
      break;

    case IN_GLOBAL_VAR:
      ___printf (">>> The reference was found in a global variable\n");
      break;

    case IN_WILL_LIST:
      ___printf (">>> The reference was found in a will list\n");
      break;

    case IN_CONTINUATION:
      ___printf (">>> The reference was found in a continuation\n");
      break;

    case IN_RC:
      ___printf (">>> The reference was found in a reference counted object\n");
      break;
    }
}


___HIDDEN void bug
   ___P((___WORD obj,
         char *msg),
        (obj,
         msg)
___WORD obj;
char *msg;)
{
  char *msgs[2];
  ___printf (">>> The GC has detected the following inconsistency\n");
  ___printf (">>> during call of mark_array on line %d of mem.c:\n",
             mark_array_call_line);
  explain_problem (obj, msg);
  msgs[0] = "GC inconsistency detected";
  msgs[1] = 0;
  ___fatal_error (msgs);
}


___HIDDEN void validate_old_obj
   ___P((___WORD obj),
        (obj)
___WORD obj;)
{
  ___WORD *hd_ptr = ___BODY(obj)-1;
  ___WORD head;
  int i = find_msection (the_msections, hd_ptr);
  if (i >= 0 && i < the_msections->nb_sections)
    {
      long pos = hd_ptr - the_msections->sections[i]->base;
      if (pos >= 0 && pos < ___MSECTION_SIZE)
        {
          head = *hd_ptr;
          if (___TYP(head) == ___FORW)
            {
              ___WORD *hd_ptr2 = ___UNTAG_AS(head,___FORW)+___BODY_OFS-1;
              int i2 = find_msection (the_msections, hd_ptr2);
              if (i2 >= 0 && i2 < the_msections->nb_sections)
                {
                  long pos2 = hd_ptr2 - the_msections->sections[i2]->base;
                  if (tospace_at_top
                      ? (pos2 < ___MSECTION_SIZE>>1 ||
                         pos2 >= ___MSECTION_SIZE)
                      : (pos2 < 0 ||
                         pos2 >= ___MSECTION_SIZE>>1))
                    bug (obj, "was copied outside of tospace");
                  else if (___HD_TYP((*hd_ptr2)) != ___MOVABLE0)
                    bug (obj, "was copied and copy is not ___MOVABLE0");
                }
              else
                bug (obj, "was copied outside of tospace");
            }
          else if (___HD_TYP(head) != ___MOVABLE0)
            bug (obj, "should be ___MOVABLE0");
          else if (tospace_at_top
                   ? (pos >= ___MSECTION_SIZE>>1 &&
                      pos < ___MSECTION_SIZE)
                   : (pos >= 0 &&
                      pos < ___MSECTION_SIZE>>1))
            bug (obj, "is in tospace");
          return;
        }
    }
  head = *hd_ptr; /* this dereference will likely bomb if there is a bug */
  if (___HD_TYP(head) != ___PERM && ___HD_TYP(head) != ___STILL)
    bug (obj, "is not ___PERM or ___STILL");
}


#define ZAP_PATTERN ___CAST(___WORD,0xcafebabe)


___HIDDEN void zap_section
   ___P((___WORD *start,
         int words),
        (start,
         words)
___WORD *start;
int words;)
{
  while (words > 0)
    {
      *start++ = ZAP_PATTERN;
      words--;
    }
}


___HIDDEN int unzapped_words
   ___P((___WORD *start,
         int words),
        (start,
         words)
___WORD *start;
int words;)
{
  ___WORD *ptr = start;

  while (words > 0 && *ptr++ == ZAP_PATTERN)
    words--;

  return words;
}


___HIDDEN int stack_fudge_used; /* space used in msection stack fudge */
___HIDDEN int heap_fudge_used; /* space used in msection heap fudge */


___HIDDEN void check_fudge_used ___PVOID
{
  ___processor_state ___ps = ___PSTATE;
  int n;

  n = unzapped_words (___ps->stack_limit - ___MSECTION_FUDGE,
                      ___MSECTION_FUDGE);

  if (n > stack_fudge_used)
    stack_fudge_used = n;

#ifdef ___DEBUG_GARBAGE_COLLECT
  ___printf ("********* used stack fudge = %d\n", n);
#endif

  n = ___ps->hp - ___ps->heap_limit;

  if (n > heap_fudge_used)
    heap_fudge_used = n;

#ifdef ___DEBUG_GARBAGE_COLLECT
  ___printf ("********* used heap fudge = %d\n", n);
#endif
}


___HIDDEN void zap_fromspace ___PVOID
{
  int i;
  for (i=0; i<the_msections->nb_sections; i++)
    zap_section (start_of_fromspace (the_msections->sections[i]),
                 ___MSECTION_SIZE>>1);
}

#endif


/*---------------------------------------------------------------------------*/

#ifdef GATHER_STATS

#define MAX_STAT_SIZE 20

___HIDDEN long movable_pair_objs;
___HIDDEN long movable_subtyped_objs[MAX_STAT_SIZE+2];

#endif


/*---------------------------------------------------------------------------*/

#ifdef ENABLE_CONSISTENCY_CHECKS

#define mark_array(start,n) mark_array_debug (start, n, __LINE__)

___HIDDEN void mark_array_debug
   ___P((___WORD *start,
         ___WORD n,
         int line),
        (start,
         n,
         line)
___WORD *start;
___WORD n;
int line;)

#else

___HIDDEN void mark_array
   ___P((___WORD *start,
         ___WORD n),
        (start,
         n)
___WORD *start;
___WORD n;)

#endif
{
  ___WORD *alloc = alloc_heap_ptr;
  ___WORD *limit = alloc_heap_limit;

#ifdef ENABLE_CONSISTENCY_CHECKS
  mark_array_call_line = line;
#endif

  while (n > 0)
    {
      ___WORD obj = *start;

      if (___MEM_ALLOCATED(obj))
        {
          ___WORD *body;
          ___WORD head;
          int head_typ;
          int subtype;

#ifdef ENABLE_CONSISTENCY_CHECKS
          if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) >= 1)
            validate_old_obj (obj);
#endif

          body = ___UNTAG(obj) + ___BODY_OFS;
          head = body[-1];
          subtype = ___HD_SUBTYPE(head);
          head_typ = ___HD_TYP(head);

          if (head_typ == ___MOVABLE0)
            {
              long words = ___HD_WORDS(head);
#if ___WS == 4
              ___BOOL pad = 0;
              while (alloc + words + (subtype >= ___sS64VECTOR ? 2 : 1) >
                     limit)
#else
              while (alloc + words + 1 > limit)
#endif
                {
                  alloc_heap_ptr = alloc;
                  next_heap_msection ();
                  alloc = alloc_heap_ptr;
                  limit = alloc_heap_limit;
                }
#if ___WS != 8
              /*
* ___sS64VECTOR, ___sU64VECTOR, ___sF64VECTOR,
* ___sFLONUM and ___sBIGNUM need to be aligned on a
* multiple of 8.
*/
              if (subtype >= ___sS64VECTOR)
                {
                  if ((___CAST(___WORD,alloc) & (8-1)) == 0)
                    *alloc++ = ___MAKE_HD_WORDS(0, ___sVECTOR);
                  else
                    pad = 1;
                }
#endif
#ifdef GATHER_STATS
              if (subtype == ___sPAIR)
                movable_pair_objs++;
              else if (words <= MAX_STAT_SIZE)
                movable_subtyped_objs[words]++;
              else
                movable_subtyped_objs[MAX_STAT_SIZE+1]++;
#endif
              *alloc++ = head;
              *start = ___TAG((alloc - ___BODY_OFS), ___TYP(obj));
              body[-1] = ___TAG((alloc - ___BODY_OFS), ___FORW);
              while (words > 0)
                {
                  *alloc++ = *body++;
                  words--;
                }
#if ___WS == 4
              if (pad)
                *alloc++ = ___MAKE_HD_WORDS(0, ___sVECTOR);
#endif
            }
          else if (head_typ == ___STILL)
            {
              if (body[___STILL_MARK_OFS - ___STILL_BODY_OFS] == -1)
                {
                  body[___STILL_MARK_OFS - ___STILL_BODY_OFS]
                    = ___CAST(___WORD,still_objs_to_scan);
                  still_objs_to_scan
                    = ___CAST(___WORD,body - ___STILL_BODY_OFS);
                }
            }
          else if (___TYP(head_typ) == ___FORW)
            {
              ___WORD *copy_body = ___UNTAG_AS(head, ___FORW) + ___BODY_OFS;
              *start = ___TAG((copy_body - ___BODY_OFS), ___TYP(obj));
            }
#ifdef ENABLE_CONSISTENCY_CHECKS
          else if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) >= 1 &&
                   head_typ != ___PERM)
            bug (obj, "was not ___PERM, ___STILL, ___MOVABLE0 or ___FORW");
#endif
        }

      start++;
      n--;
    }

  alloc_heap_ptr = alloc;
}


___HIDDEN void mark_captured_continuation
   ___P((___WORD *orig_ptr),
        (orig_ptr)
___WORD *orig_ptr;)
{
  ___processor_state ___ps = ___PSTATE;
  ___WORD *ptr = orig_ptr;
  int fs, link, i;
  ___WORD *fp;
  ___WORD ra1;
  ___WORD ra2;
  ___WORD cf;

  cf = *ptr;

#if 0
  printf("-------------\n");;;;;;;;;;;;;;;;;;;;;;;;;;
  fflush(stdout);
#endif

  if (___TYP(cf) == ___tFIXNUM && cf != ___FIX(0))
    {
      /* continuation frame is in the stack */

      ___WORD *alloc = alloc_heap_ptr;
      ___WORD *limit = alloc_heap_limit;

      next_frame:

      fp = ___CAST(___WORD*,cf);

      ra1 = ___FP_STK(fp,-___FRAME_STACK_RA);

      if (ra1 == ___GSTATE->internal_return)
        {
          ___WORD actual_ra = ___FP_STK(fp,___RETI_RA);
          ___RETI_GET_FS_LINK(actual_ra,fs,link)
          ___COVER_MARK_CAPTURED_CONTINUATION_RETI;
        }
      else
        {
          ___RETN_GET_FS_LINK(ra1,fs,link)
          ___COVER_MARK_CAPTURED_CONTINUATION_RETN;
        }

#if 0
      printf("fp=0x%08lx ra1=0x%08lx fs=%d link=%d\n", fp, ra1, fs, link);;;;;;;;;;;;;;;;;;;;;;;;;;
      fflush(stdout);
#endif

      /* with reserve=1
bash-3.2$ gsi/gsi
-------------
Gambit v4.5.2

> -------------
fp=0x1006fff68 ra1=0x1001f9bc1 fs=3 link=0
fp=0x1006fff88 ra1=0x1002efc21 fs=7 link=0
fp=0x1006fffc8 ra1=0x1002efda1 fs=3 link=0
fp=0x1006fffe8 ra1=0x1001f4e01 fs=3 link=0
-------------
-------------
fp=0x1006fff68 ra1=0x1001f9bc1 fs=3 link=0
-------------
fp=0x1006fff68 ra1=0x1001f9bc1 fs=3 link=0
*/

      ___FP_ADJFP(fp,-___FRAME_SPACE(fs)); /* get base of frame */

      ra2 = ___FP_STK(fp,link+1);

      if (___TYP(ra2) == ___tFIXNUM)
        {
          ___COVER_MARK_CAPTURED_CONTINUATION_ALREADY_COPIED;
          *ptr = ra2; /* already copied, replace by forwarding pointer */
        }
      else
        {
          ___WORD forw;
          long words;

          ___COVER_MARK_CAPTURED_CONTINUATION_COPY;

          words = fs + ___FRAME_EXTRA_SLOTS;

          while (alloc + words + ___SUBTYPED_OVERHEAD > limit)
            {
              alloc_heap_ptr = alloc;
              next_heap_msection ();
              alloc = alloc_heap_ptr;
              limit = alloc_heap_limit;
            }

          *alloc++ = ___MAKE_HD_WORDS(words, ___sFRAME);
#if ___SUBTYPED_OVERHEAD != 1
#error "___SUBTYPED_OVERHEAD != 1"
#endif
          forw = ___TAG((alloc - ___BODY_OFS), ___tFIXNUM);
          *alloc++ = ra1;
#if ___FRAME_EXTRA_SLOTS != 1
#error "___FRAME_EXTRA_SLOTS != 1"
#endif

          for (i=fs; i>0; i--)
            *alloc++ = ___FP_STK(fp,i);

          if (ra2 == ___GSTATE->handler_break)
            {
              /* first frame of that section */

              ___COVER_MARK_CAPTURED_CONTINUATION_FIRST_FRAME;

              cf = ___FP_STK(fp,-___BREAK_FRAME_NEXT);
            }
          else
            {
              /* not the first frame of that section */

              ___COVER_MARK_CAPTURED_CONTINUATION_NOT_FIRST_FRAME;

              ___FP_SET_STK(fp,-___FRAME_STACK_RA,ra2)
              cf = ___CAST(___WORD,fp);
            }

          ___FP_SET_STK(alloc,link+1,cf)
          ___FP_SET_STK(fp,link+1,forw) /* leave a forwarding pointer */

          *ptr = forw;

          ptr = &___FP_STK(alloc,link+1);

          if (___TYP(cf) == ___tFIXNUM && cf != ___FIX(0))
            goto next_frame;
        }

      *orig_ptr = ___TAG(___UNTAG_AS(*orig_ptr, ___tFIXNUM), ___tSUBTYPED);

      alloc_heap_ptr = alloc;
    }
  else
    mark_array (orig_ptr, 1);
}


___HIDDEN void mark_frame
   ___P((___WORD *fp,
         int fs,
         ___WORD gcmap,
         ___WORD *nextgcmap),
        (fp,
         fs,
         gcmap,
         nextgcmap)
___WORD *fp;
int fs;
___WORD gcmap;
___WORD *nextgcmap;)
{
  int i = 1;

#ifdef SHOW_FRAMESzzz

  {
    int k = 1;
    while (k <= fs)
      {
        ___WORD obj = ___FP_STK(fp,k);
        ___printf (" %2d: ", k);
        print_value (obj);
        ___printf ("\n");
        k++;
      }
  }

#endif

  for (;;)
    {
      if (gcmap & 1)
        {
          int j = i;
          do
            {
              if (i == fs)
                {
#ifdef SHOW_FRAMES
                  {
                    int k = j;
                    while (k <= i)
                      {
                        ___WORD obj = ___FP_STK(fp,k);
                        ___printf (" %2d: ", k);
                        print_value (obj);
                        ___printf ("\n");
                        k++;
                      }
                  }
#endif
                  mark_array (&___FP_STK(fp,i), i-j+1);
                  return;
                }
              if ((i & (___WORD_WIDTH-1)) == 0)
                gcmap = *nextgcmap++;
              else
                gcmap >>= 1;
              i++;
            } while (gcmap & 1);
#ifdef SHOW_FRAMES
          {
            int k = j;
            while (k < i)
              {
                ___WORD obj = ___FP_STK(fp,k);
                ___printf (" %2d: ", k);
                print_value (obj);
                ___printf ("\n");
                k++;
              }
          }
#endif
          mark_array (&___FP_STK(fp,i-1), i-j);
        }
      if (i == fs)
        return;
      if ((i & (___WORD_WIDTH-1)) == 0)
        gcmap = *nextgcmap++;
      else
        gcmap >>= 1;
      i++;
    }
}


___HIDDEN void mark_continuation ___PVOID
{
  ___processor_state ___ps = ___PSTATE;
  int fs, link;
  ___WORD *fp;
  ___WORD ra1;
  ___WORD ra2;
  ___WORD gcmap;
  ___WORD *nextgcmap = 0;

  fp = ___ps->fp;

  if (fp != ___ps->stack_break)
    for (;;)
      {
        ra1 = ___FP_STK(fp,-___FRAME_STACK_RA);

#ifdef SHOW_FRAMES
        ___printf ("continuation frame, ");
#endif

        if (ra1 == ___GSTATE->internal_return)
          {
            ___WORD actual_ra = ___FP_STK(fp,___RETI_RA);
            ___RETI_GET_FS_LINK_GCMAP(actual_ra,fs,link,gcmap,nextgcmap)
            ___COVER_MARK_CONTINUATION_RETI;
          }
        else
          {
            ___RETN_GET_FS_LINK_GCMAP(ra1,fs,link,gcmap,nextgcmap)
            ___COVER_MARK_CONTINUATION_RETN;
          }

#ifdef SHOW_FRAMES
        ___printf ("fs=%d link=%d fp=0x%08x ra=", fs, link, ___CAST(___WORD,fp));
        print_value (ra1);
#endif

        ___FP_ADJFP(fp,-___FRAME_SPACE(fs)); /* get base of frame */

        ra2 = ___FP_STK(fp,link+1);

#ifdef SHOW_FRAMES
        if (fp == ___ps->stack_break)
          ___printf (" (first frame)\n");
        else
          ___printf (" (not first frame)\n");
#endif

        mark_frame (fp, fs, gcmap, nextgcmap);

        if (fp == ___ps->stack_break)
          break;

        ___FP_SET_STK(fp,-___FRAME_STACK_RA,ra2)
      }

  mark_captured_continuation (&___FP_STK(fp,-___BREAK_FRAME_NEXT));
}


___HIDDEN void mark_rc ___PVOID
{
  rc_header *h = rc_head.next;

  while (h != &rc_head)
    {
      rc_header *next = h->next;
      mark_array (&h->data, 1);
      h = next;
    }
}


#define UNMARKED_MOVABLE(obj) \
((unmarked_typ = ___HD_TYP((unmarked_body=___BODY(obj))[-1])) == ___MOVABLE0)

#define UNMARKED_STILL(obj) \
(unmarked_typ == ___STILL && \
unmarked_body[___STILL_MARK_OFS - ___STILL_BODY_OFS] == -1)

#define UNMARKED(obj) \
(UNMARKED_MOVABLE(obj) || UNMARKED_STILL(obj))


___HIDDEN long scan
   ___P((___WORD *body),
        (body)
___WORD *body;)
{
  ___WORD head = body[-1];
  long words = ___HD_WORDS(head);
  int subtype = ___HD_SUBTYPE(head);

#ifdef ENABLE_CONSISTENCY_CHECKS
  reference_location = IN_OBJECT;
  container_body = body;
#endif

  switch (subtype)
    {
    case ___sFOREIGN:
    case ___sSTRING:
    case ___sS8VECTOR:
    case ___sU8VECTOR:
    case ___sS16VECTOR:
    case ___sU16VECTOR:
    case ___sS32VECTOR:
    case ___sU32VECTOR:
    case ___sS64VECTOR:
    case ___sU64VECTOR:
    case ___sF32VECTOR:
    case ___sF64VECTOR:
    case ___sFLONUM:
    case ___sBIGNUM:
      break;

    case ___sWEAK:
      if (words == ___WILL_SIZE)
        {
          /* Object is a will */

          /*
* The will contains a weak reference to its testator object
* and a strong reference to the action procedure.
* Consequently, the action procedure must be marked and,
* only if traverse_weak_refs is true, the testator object
* is also marked. The link field is never scanned.
*/

          if (traverse_weak_refs)
            mark_array (body+1, 2); /* scan action and testator */
          else
            {
              mark_array (body+2, 1); /* scan action only */

              /*
* Remember that this will's testator object remains to
* be marked by the process_wills function.
*/

              body[0] = body[0] | ___UNMARKED_TESTATOR_WILL;
            }
        }
      else
        {
          /* Object is a GC hash table */

          int flags = ___INT(body[___GCHASHTABLE_FLAGS]);
          int i;

          if ((flags & ___GCHASHTABLE_FLAG_WEAK_KEYS) == 0 &&
              (flags & ___GCHASHTABLE_FLAG_MEM_ALLOC_KEYS))
            {
              for (i=words-2; i>=___GCHASHTABLE_KEY0; i-=2)
                mark_array (body+i, 1); /* mark objects in key fields */
            }

          if ((flags & ___GCHASHTABLE_FLAG_WEAK_VALS) == 0)
            {
              for (i=words-1; i>=___GCHASHTABLE_VAL0; i-=2)
                mark_array (body+i, 1); /* mark objects in value fields */
            }

          body[0] = reached_gc_hash_tables;
          reached_gc_hash_tables = ___TAG((body-1),0);
        }
      break;

    case ___sSYMBOL:
    case ___sKEYWORD:
      mark_array (body, 1); /* only scan name of symbols & keywords */
      break;

    case ___sCONTINUATION:
      mark_captured_continuation (&body[___CONTINUATION_FRAME]);
      mark_array (body+1, words-1); /* skip the frame pointer */
      break;

    case ___sFRAME:
      {
        int fs, link;
        ___WORD *fp = body + ___FRAME_EXTRA_SLOTS;
        ___WORD ra = body[0];
        ___WORD gcmap;
        ___WORD *nextgcmap = 0;
        ___WORD frame;

#ifdef SHOW_FRAMES
        ___printf ("___sFRAME object, ");
#endif

        if (ra == ___GSTATE->internal_return)
          {
            ___WORD actual_ra = body[___FRAME_RETI_RA];
            ___RETI_GET_FS_LINK_GCMAP(actual_ra,fs,link,gcmap,nextgcmap)
            ___COVER_SCAN_FRAME_RETI;
          }
        else
          {
            ___RETN_GET_FS_LINK_GCMAP(ra,fs,link,gcmap,nextgcmap)
            ___COVER_SCAN_FRAME_RETN;
          }

#ifdef SHOW_FRAMES
        ___printf ("fs=%d link=%d fp=0x%08x ra=", fs, link, ___CAST(___WORD,fp));
        print_value (ra);
        ___printf ("\n");
#endif

        fp += fs;

        frame = ___FP_STK(fp,link+1);

        if (___TYP(frame) == ___tFIXNUM && frame != ___FIX(0))
          ___FP_SET_STK(fp,link+1,___FAL)

        mark_frame (fp, fs, gcmap, nextgcmap);

        if (___TYP(frame) == ___tFIXNUM && frame != ___FIX(0))
          ___FP_SET_STK(fp,link+1,___TAG(___UNTAG_AS(frame, ___tFIXNUM), ___tSUBTYPED))

        mark_array (&body[0], 1);
      }
      break;

    case ___sPROCEDURE:
      if (___HD_TYP(head) != ___PERM) /* only scan closures */
        mark_array (body+1, words-1); /* only scan free variables */
      break;

    default:
      mark_array (body, words);
      break;
    }

  return words;
}


___HIDDEN void init_still_objs_to_scan ___PVOID
{
  ___WORD *base = ___CAST(___WORD*,still_objs);
  ___WORD *to_scan = 0;

  while (base != 0)
    {
      if (base[___STILL_REFCOUNT_OFS] == 0)
        base[___STILL_MARK_OFS] = -1;
      else
        {
          base[___STILL_MARK_OFS] = ___CAST(___WORD,to_scan);
          to_scan = base;
        }
      base = ___CAST(___WORD*,base[___STILL_LINK_OFS]);
    }

  still_objs_to_scan = ___CAST(___WORD,to_scan);
}


___HIDDEN void scan_still_objs_to_scan ___PVOID
{
  ___WORD *base;

  while ((base = ___CAST(___WORD*,still_objs_to_scan)) != 0)
    {
      still_objs_to_scan = base[___STILL_MARK_OFS];
      scan (base + ___STILL_BODY_OFS);
    };
}


___HIDDEN void scan_movable_objs_to_scan ___PVOID
{
  ___WORD *body;
  long words;

  for (;;)
    {
      if (scan_msection == heap_msection)
        {
          if (scan_ptr >= alloc_heap_ptr)
            break;
        }
      else if (scan_ptr >= scan_msection->alloc)
        {
          scan_msection = scan_msection->next;
          scan_ptr = start_of_tospace (scan_msection);
          continue;
        }
      body = scan_ptr + 1;
      words = scan (body);
      scan_ptr = body + words;
    };
}


___HIDDEN void free_unmarked_still_objs ___PVOID
{
  ___WORD *last = &still_objs;
  ___WORD *base = ___CAST(___WORD*,*last);

  while (base != 0)
    {
      ___WORD link = base[___STILL_LINK_OFS];
      if (base[___STILL_MARK_OFS] == -1)
        {
          ___WORD head = base[___STILL_BODY_OFS-1];
          if (___HD_SUBTYPE(head) == ___sFOREIGN)
            ___release_foreign
              (___TAG((base + ___STILL_BODY_OFS - ___BODY_OFS), ___tSUBTYPED));
          words_nonmovable -= base[___STILL_LENGTH_OFS];
          free_mem_aligned (base);
        }
      else
        {
          *last = ___CAST(___WORD,base);
          last = base + ___STILL_LINK_OFS;
        }
      base = ___CAST(___WORD*,link);
    }

  *last = 0;
}


___HIDDEN void free_still_objs ___PVOID
{
  ___WORD *base = ___CAST(___WORD*,still_objs);

  still_objs = 0;

  while (base != 0)
    {
      ___WORD link = base[___STILL_LINK_OFS];
      ___WORD head = base[___STILL_BODY_OFS-1];
      if (___HD_SUBTYPE(head) == ___sFOREIGN)
        ___release_foreign
          (___TAG((base + ___STILL_BODY_OFS - ___BODY_OFS), ___tSUBTYPED));
      free_mem_aligned (base);
      base = ___CAST(___WORD*,link);
    }
}


___HIDDEN long adjust_heap
   ___P((long avail,
         long live),
        (avail,
         live)
long avail;
long live;)
{
  long target;
  int live_percent;

  if (___setup_params.gc_hook != 0)
    return ___setup_params.gc_hook (avail, live);

  live_percent = ___setup_params.live_percent;

  if (live_percent <= 0 || live_percent > 100)
    live_percent = ___DEFAULT_LIVE_PERCENT;

  if (live_percent < 100)
    target = live / live_percent * 100;
  else
    target = live + ___MSECTION_BIGGEST;

  if (target < ___CAST(long,(___setup_params.min_heap >> ___LWS)))
    target = ___CAST(long,(___setup_params.min_heap >> ___LWS));

  if (___setup_params.max_heap > 0 &&
      target > ___CAST(long,(___setup_params.max_heap >> ___LWS)))
    target = ___CAST(long,(___setup_params.max_heap >> ___LWS));

  return target;
}


___HIDDEN void setup_pstate ___PVOID
{
  ___processor_state ___ps = ___PSTATE;
  long avail;
  long stack_avail;
  long stack_left_before_fudge;
  long heap_avail;
  long heap_left_before_fudge;

#ifdef CALL_GC_FREQUENTLY
  avail = 0;
#else
  if (heap_size < WORDS_OCCUPIED)
    avail = 0;
  else
    avail = (heap_size - WORDS_OCCUPIED) / 2;
#endif

  stack_avail = avail/2;
  stack_left_before_fudge = (alloc_stack_ptr - alloc_stack_limit)
                            - ___MSECTION_FUDGE;

  ___ps->fp = alloc_stack_ptr;
  ___ps->stack_limit = alloc_stack_ptr
                       - ((stack_avail < stack_left_before_fudge)
                          ? stack_avail
                          : stack_left_before_fudge);

  heap_avail = avail - stack_avail;
  heap_left_before_fudge = (alloc_heap_limit - alloc_heap_ptr)
                           - ___MSECTION_FUDGE;

  ___ps->hp = alloc_heap_ptr;
  ___ps->heap_limit = alloc_heap_ptr
                      + ((heap_avail < heap_left_before_fudge)
                         ? heap_avail
                         : heap_left_before_fudge);

  ___begin_interrupt_service ();
  ___end_interrupt_service (0);

#ifdef ENABLE_CONSISTENCY_CHECKS
  if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) >= 1)
    {
      ___WORD *end = ___ps->stack_limit;
      ___WORD *start = end - ___MSECTION_FUDGE;
      if (end > alloc_stack_ptr)
        end = alloc_stack_ptr;
      zap_section (start, end - start);
      if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) == 3)
        {
          ___printf ("heap_size = %d\n", heap_size);
          ___printf ("WORDS_OCCUPIED = %d\n", WORDS_OCCUPIED);
          ___printf ("avail = %d\n", avail);
          ___printf ("stack_avail = %d\n", stack_avail);
          ___printf ("heap_avail = %d\n", heap_avail);
          ___printf ("stack_msection = 0x%08x\n", stack_msection);
          ___printf ("heap_msection = 0x%08x\n", heap_msection);
          ___printf ("___ps->stack_start = 0x%08x\n", ___ps->stack_start);
          ___printf ("___ps->stack_break = 0x%08x\n", ___ps->stack_break);
          ___printf ("___ps->fp = 0x%08x\n", ___ps->fp);
          ___printf ("alloc_stack_ptr = 0x%08x\n", alloc_stack_ptr);
          ___printf ("___ps->stack_limit = 0x%08x\n", ___ps->stack_limit);
          ___printf ("alloc_stack_limit = 0x%08x\n", alloc_stack_limit);
          ___printf ("alloc_heap_limit = 0x%08x\n", alloc_heap_limit);
          ___printf ("___ps->heap_limit = 0x%08x\n", ___ps->heap_limit);
          ___printf ("___ps->hp = 0x%08x\n", ___ps->hp);
          ___printf ("alloc_heap_ptr = 0x%08x\n", alloc_heap_ptr);
          ___printf ("alloc_heap_start = 0x%08x\n", alloc_heap_start);
        }
    }
#endif
}


___SCMOBJ ___setup_mem ___PVOID
{
  ___processor_state ___ps = ___PSTATE;
  int init_nb_sections;

  /*
* It is important to initialize the following pointers first so
* that if the program terminates early the procedure ___cleanup_mem
* will not access dangling pointers.
*/

  the_msections = 0;
  psections = 0;
  still_objs = 0;

  setup_rc ();

  /*
* Set the overflow reserve so that the rest parameter handler can
* construct the rest parameter list without having to call the
* garbage collector.
*/

  normal_overflow_reserve = 2*((___MAX_NB_PARMS+___SUBTYPED_OVERHEAD) +
                               ___MAX_NB_ARGS*(___PAIR_SIZE+___PAIR_OVERHEAD));
  overflow_reserve = normal_overflow_reserve;

  /* Allocate heap */

  init_nb_sections = ((___setup_params.min_heap >> ___LWS) +
                      overflow_reserve + 2*___MSECTION_FUDGE +
                      2*((___MSECTION_SIZE>>1)-___MSECTION_FUDGE+1) - 1) /
                     (2*((___MSECTION_SIZE>>1)-___MSECTION_FUDGE+1));

  if (init_nb_sections < ___MIN_NB_MSECTIONS)
    init_nb_sections = ___MIN_NB_MSECTIONS;

  adjust_msections (&the_msections, init_nb_sections);

  if (the_msections == 0 ||
      the_msections->nb_sections != init_nb_sections)
    return ___FIX(___HEAP_OVERFLOW_ERR);

#ifdef ENABLE_CONSISTENCY_CHECKS
  if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) >= 1)
    {
      zap_fromspace ();
      stack_fudge_used = 0;
      heap_fudge_used = 0;
    }
#endif

  words_nonmovable = 0;
  words_prev_msections = 0;

  tospace_at_top = 0;
  stack_msection = 0;
  heap_msection = 0;
  nb_msections_used = 0;

  next_stack_msection ();
  next_heap_msection ();

  palloc_ptr = 0;

  /*
* Create "break frame" of initial top section.
*/

  ___ps->stack_start = alloc_stack_start;
  alloc_stack_ptr = alloc_stack_start;

  ___FP_ADJFP(alloc_stack_ptr,___BREAK_FRAME_SPACE)
  ___FP_SET_STK(alloc_stack_ptr,-___BREAK_FRAME_NEXT,0)

  ___ps->stack_break = alloc_stack_ptr;

  /*
* Setup will lists.
*/

  ___ps->executable_wills = ___TAG(0,___EXECUTABLE_WILL); /* tagged empty list */
  ___ps->nonexecutable_wills = ___TAG(0,0); /* tagged empty list */

  heap_size = WORDS_AVAILABLE;

#ifdef ___DEBUG_STACK_LIMIT
  ___ps->poll_line = 0;
  ___ps->stack_limit_line = 0;
#endif

#ifdef ___DEBUG_HEAP_LIMIT
  ___ps->check_heap_line = 0;
  ___ps->heap_limit_line = 0;
#endif

  setup_pstate ();

  /* Setup global state */

  ___GSTATE->nb_gcs = 0.0;
  ___GSTATE->gc_user_time = 0.0;
  ___GSTATE->gc_sys_time = 0.0;
  ___GSTATE->gc_real_time = 0.0;
  ___GSTATE->bytes_allocated_minus_occupied = 0.0;

  ___GSTATE->last_gc_real_time = 0.0;
  ___GSTATE->last_gc_heap_size = ___CAST(___F64,heap_size) * ___WS;
  ___GSTATE->last_gc_live = 0.0;
  ___GSTATE->last_gc_movable = 0.0;
  ___GSTATE->last_gc_nonmovable = 0.0;

  return ___FIX(___NO_ERR);
}


void ___cleanup_mem ___PVOID
{
  free_msections (&the_msections);
  free_psections ();
  free_still_objs ();
  cleanup_rc ();
}


___HIDDEN void determine_will_executability
   ___P((___WORD list),
        (list)
___WORD list;)
{
  while (___UNTAG(list) != 0)
    {
      ___WORD* will_body = ___BODY(list);
      ___WORD will_head = will_body[-1];
      ___WORD testator;

      ___WORD *unmarked_body; /* used by the UNMARKED macro */
      int unmarked_typ;

      if (___TYP(will_head) == ___FORW) /* was will forwarded? */
        will_body = ___BODY_AS(will_head,___FORW);

      list = will_body[0];

      testator = will_body[1];

      if (___MEM_ALLOCATED(testator) &&
          UNMARKED(testator)) /* testator was not marked? */
        {
          /*
* All paths to testator object from roots pass through
* weak references, so mark will as executable.
*/

          will_body[0] = list | ___EXECUTABLE_WILL;
        }
    }
}


___HIDDEN void process_wills ___PVOID
{
  ___processor_state ___ps = ___PSTATE;
  ___WORD* tail_exec;
  ___WORD* tail_nonexec;
  ___WORD curr;

#ifdef ENABLE_CONSISTENCY_CHECKS
  reference_location = IN_WILL_LIST;
#endif

  determine_will_executability (___ps->nonexecutable_wills);

  /*
* Finish scanning the wills whose testator object remains to be
* marked.
*
* The wills that have become executable are also transferred from
* the nonexecutable wills list to the executable wills list.
*/

  tail_exec = &___ps->executable_wills;
  curr = *tail_exec;

  while (___UNTAG(curr) != 0)
    {
      ___WORD will = ___TAG(___UNTAG(curr),___tSUBTYPED);

      mark_array (&will, 1);

      *tail_exec = ___TAG(___UNTAG(will),___EXECUTABLE_WILL);
      tail_exec = &___BODY_AS(will,___tSUBTYPED)[0];
      curr = *tail_exec;
      if (curr & ___UNMARKED_TESTATOR_WILL)
        mark_array (tail_exec+1, 1); /* mark testator object */
    }

  tail_nonexec = &___ps->nonexecutable_wills;
  curr = *tail_nonexec;

  while (___UNTAG(curr) != 0)
    {
      ___WORD will = ___TAG(___UNTAG(curr),___tSUBTYPED);

      mark_array (&will, 1);

      if (___BODY_AS(will,___tSUBTYPED)[0] & ___EXECUTABLE_WILL)
        {
          /* move will to executable will list */

          *tail_exec = ___TAG(___UNTAG(will),___EXECUTABLE_WILL);
          tail_exec = &___BODY_AS(will,___tSUBTYPED)[0];
          curr = *tail_exec;
          if (curr & ___UNMARKED_TESTATOR_WILL)
            mark_array (tail_exec+1, 1); /* mark testator object */
        }
      else
        {
          /* leave will in nonexecutable will list */

          *tail_nonexec = ___TAG(___UNTAG(will),0);
          tail_nonexec = &___BODY_AS(will,___tSUBTYPED)[0];
          curr = *tail_nonexec;
          if (curr & ___UNMARKED_TESTATOR_WILL)
            mark_array (tail_nonexec+1, 1); /* mark testator object */
        }
    }

  *tail_exec = ___TAG(0,___EXECUTABLE_WILL);
  *tail_nonexec = ___TAG(0,0);
}


___HIDDEN void process_gc_hash_tables ___PVOID
{
  ___WORD curr = reached_gc_hash_tables;

  while (curr != ___TAG(0,0))
    {
      ___WORD* body = ___BODY(curr);
      long words = ___HD_WORDS(body[-1]);
      int flags = ___INT(body[___GCHASHTABLE_FLAGS]);
      int i;

      curr = body[0];

      body[0] = ___FIX(0);

      if (((___GCHASHTABLE_FLAG_WEAK_KEYS | ___GCHASHTABLE_FLAG_MEM_ALLOC_KEYS)
           & flags) ==
          (___GCHASHTABLE_FLAG_WEAK_KEYS | ___GCHASHTABLE_FLAG_MEM_ALLOC_KEYS))
        {
          if (flags & ___GCHASHTABLE_FLAG_WEAK_VALS)
            {
              /*
* GC hash table is weak on keys and on values.
*/

              /*
* Eliminate GC hash table entries with an unmarked key
* or an unmarked value.
*/

              for (i=words-2; i>=___GCHASHTABLE_KEY0; i-=2)
                {
                  ___WORD *unmarked_body; /* used by the UNMARKED macro */
                  int unmarked_typ;

                  ___WORD key = body[i];
                  ___WORD val = body[i+1];

                  if (___MEM_ALLOCATED(key))
                    {
                      ___WORD key_head = ___BODY(key)[-1];

                      if (___TYP(key_head) == ___FORW)
                        {
                          /*
* The key is movable and has been
* forwarded.
*/

                          if (___MEM_ALLOCATED(val))
                            {
                              ___WORD val_head = ___BODY(val)[-1];

                              if (___TYP(val_head) == ___FORW)
                                {
                                  /*
* The key is movable and has been
* forwarded and the value is
* movable and has been forwarded,
* so update key field and value
* field and remember to rehash next
* time the GC hash table is
* accessed.
*/

                                  body[i] =
                                    ___TAG(___UNTAG_AS(key_head, ___FORW),
                                           ___TYP(key));
                                  body[i+1] =
                                    ___TAG(___UNTAG_AS(val_head, ___FORW),
                                           ___TYP(val));
                                  flags |= ___GCHASHTABLE_FLAG_KEY_MOVED;
                                }
                              else if (UNMARKED(val))
                                {
                                  /*
* Change the entry to indicate it
* has been deleted.
*/

                                  body[i] = ___DELETED;
                                  body[i+1] = ___UNUSED;
                                  body[___GCHASHTABLE_COUNT] =
                                    ___FIXSUB(body[___GCHASHTABLE_COUNT],
                                              ___FIX(1));
                                  flags |= ___GCHASHTABLE_FLAG_ENTRY_DELETED;
                                }
                              else
                                {
                                  /*
* The key is movable and has been
* forwarded and the value is not
* movable and is reachable, so
* update key field and remember to
* rehash next time the GC hash
* table is accessed.
*/

                                  body[i] =
                                    ___TAG(___UNTAG_AS(key_head, ___FORW),
                                           ___TYP(key));
                                  flags |= ___GCHASHTABLE_FLAG_KEY_MOVED;
                                }
                            }
                          else
                            {
                              /*
* The key is movable and has been
* forwarded, and the value is not
* memory allocated, so update key field
* and remember to rehash next time the
* GC hash table is accessed.
*/

                              body[i] =
                                ___TAG(___UNTAG_AS(key_head, ___FORW),
                                       ___TYP(key));
                              flags |= ___GCHASHTABLE_FLAG_KEY_MOVED;
                            }
                        }
                      else if (UNMARKED(key))
                        {
                          /*
* Change the entry to indicate it has been
* deleted.
*/

                          body[i] = ___DELETED;
                          body[i+1] = ___UNUSED;
                          body[___GCHASHTABLE_COUNT] =
                            ___FIXSUB(body[___GCHASHTABLE_COUNT],___FIX(1));
                          flags |= ___GCHASHTABLE_FLAG_ENTRY_DELETED;
                        }
                      else
                        {
                          /*
* The key is not movable and is reachable.
*/

                          if (___MEM_ALLOCATED(val))
                            {
                              ___WORD val_head = ___BODY(val)[-1];

                              if (___TYP(val_head) == ___FORW)
                                {
                                  /*
* The key is not movable and is
* reachable and the value is
* movable and has been forwarded,
* so update value field.
*/

                                  body[i+1] =
                                    ___TAG(___UNTAG_AS(val_head, ___FORW),
                                           ___TYP(val));
                                }
                              else if (UNMARKED(val))
                                {
                                  /*
* Change the entry to indicate it
* has been deleted.
*/

                                  body[i] = ___DELETED;
                                  body[i+1] = ___UNUSED;
                                  body[___GCHASHTABLE_COUNT] =
                                    ___FIXSUB(body[___GCHASHTABLE_COUNT],
                                              ___FIX(1));
                                  flags |= ___GCHASHTABLE_FLAG_ENTRY_DELETED;
                                }
                              else
                                {
                                  /*
* The key is not movable and is
* reachable and the value is not
* movable and is reachable, so
* leave fields untouched.
*/
                                }
                            }
                          else
                            {
                              /*
* The key is not movable and is
* reachable and the value is not memory
* allocated, so leave fields untouched.
*/
                            }
                        }
                    }
                  else
                    {
                      /*
* The key is not memory allocated.
*/

                      if (___MEM_ALLOCATED(val))
                        {
                          ___WORD val_head = ___BODY(val)[-1];

                          if (___TYP(val_head) == ___FORW)
                            {
                              /*
* The key is not memory allocated and
* the value is movable and has been
* forwarded, so update value field.
*/

                              body[i+1] =
                                ___TAG(___UNTAG_AS(val_head, ___FORW),
                                       ___TYP(val));
                            }
                          else if (UNMARKED(val))
                            {
                              /*
* Change the entry to indicate it
* has been deleted.
*/

                              body[i] = ___DELETED;
                              body[i+1] = ___UNUSED;
                              body[___GCHASHTABLE_COUNT] =
                                ___FIXSUB(body[___GCHASHTABLE_COUNT],
                                          ___FIX(1));
                              flags |= ___GCHASHTABLE_FLAG_ENTRY_DELETED;
                            }
                          else
                            {
                              /*
* The key is not memory allocated and
* the value is not movable and is
* reachable, so leave fields untouched.
*/
                            }
                        }
                      else
                        {
                          /*
* The key is not memory allocated and the
* value is not memory allocated, so leave
* fields untouched.
*/
                        }
                    }
                }
            }
          else
            {
              /*
* GC hash table is weak on keys only.
*/

              /*
* Eliminate GC hash table entries with an unmarked key.
*/

              for (i=words-2; i>=___GCHASHTABLE_KEY0; i-=2)
                {
                  ___WORD *unmarked_body; /* used by the UNMARKED macro */
                  int unmarked_typ;

                  ___WORD key = body[i];

                  if (___MEM_ALLOCATED(key))
                    {
                      ___WORD head = ___BODY(key)[-1];

                      if (___TYP(head) == ___FORW)
                        {
                          /*
* The key is movable and has been
* forwarded, so update key field and
* remember to rehash next time the
* GC hash table is accessed.
*/

                          body[i] = ___TAG(___UNTAG_AS(head, ___FORW),
                                           ___TYP(key));
                          flags |= ___GCHASHTABLE_FLAG_KEY_MOVED;
                        }
                      else if (UNMARKED(key))
                        {
                          /*
* Change the entry to indicate it has been
* deleted.
*/

                          body[i] = ___DELETED;
                          body[i+1] = ___UNUSED;
                          body[___GCHASHTABLE_COUNT] =
                            ___FIXSUB(body[___GCHASHTABLE_COUNT],___FIX(1));
                          flags |= ___GCHASHTABLE_FLAG_ENTRY_DELETED;
                        }
                    }
                }
            }
        }
      else
        {
          if (flags & ___GCHASHTABLE_FLAG_WEAK_VALS)
            {
              /*
* GC hash table is weak on values only.
*/

              /*
* Eliminate GC hash table entries with an unmarked value.
*/

              for (i=words-2; i>=___GCHASHTABLE_KEY0; i-=2)
                {
                  ___WORD *unmarked_body; /* used by the UNMARKED macro */
                  int unmarked_typ;

                  ___WORD val = body[i+1];

                  if (___MEM_ALLOCATED(val))
                    {
                      ___WORD head = ___BODY(val)[-1];

                      if (___TYP(head) == ___FORW)
                        {
                          /*
* The value is movable and has been
* forwarded, so update value field.
*/

                          body[i+1] = ___TAG(___UNTAG_AS(head, ___FORW),
                                             ___TYP(val));
                        }
                      else if (UNMARKED(val))
                        {
                          /*
* Change the entry to indicate it has been
* deleted.
*/

                          body[i] = ___DELETED;
                          body[i+1] = ___UNUSED;
                          body[___GCHASHTABLE_COUNT] =
                            ___FIXSUB(body[___GCHASHTABLE_COUNT],___FIX(1));
                          flags |= ___GCHASHTABLE_FLAG_ENTRY_DELETED;
                        }
                    }
                }
            }

          if (flags & ___GCHASHTABLE_FLAG_MEM_ALLOC_KEYS)
            flags |= ___GCHASHTABLE_FLAG_KEY_MOVED; /* assume worst case */
        }

      body[___GCHASHTABLE_FLAGS] = ___FIX(flags);
    }
}


void ___gc_hash_table_rehash_in_situ
   ___P((___SCMOBJ ht),
        (ht)
___SCMOBJ ht;)
{
  ___WORD* body = ___BODY_AS(ht,___tSUBTYPED);
  long words = ___HD_WORDS(body[-1]);
  int size2 = ___INT(___VECTORLENGTH(ht)) - ___GCHASHTABLE_KEY0;
  int i;

  ___FIELD(ht, ___GCHASHTABLE_FLAGS) =
    ___FIXAND(___FIELD(ht, ___GCHASHTABLE_FLAGS),
              ___FIXNOT(___FIX(___GCHASHTABLE_FLAG_KEY_MOVED)));

  if (___FIXZEROP(___FIXAND(body[___GCHASHTABLE_FLAGS],
                            ___FIX(___GCHASHTABLE_FLAG_MEM_ALLOC_KEYS))))
    {
      /*
* Free deleted entries and mark key field of all active
* entries.
*/

      for (i=___GCHASHTABLE_KEY0; i<words; i+=2)
        {
          ___WORD key = body[i];
          if (key == ___DELETED)
            {
              body[i] = ___UNUSED;
              body[___GCHASHTABLE_FREE] =
                ___FIXADD(body[___GCHASHTABLE_FREE], ___FIX(1));
            }
          else if (key != ___UNUSED)
            body[i] = ___MEM_ALLOCATED_SET(key);
        }

      /*
* Move the active entries.
*/

      for (i=___GCHASHTABLE_KEY0; i<words; i+=2)
        {
          ___WORD key = body[i];

          if (___MEM_ALLOCATED(key))
            {
              /* this is an active entry that has not been moved yet */

              ___SCMOBJ val = body[i+1];
              ___SCMOBJ obj;
              int probe2;
              int step2;

              body[i] = ___UNUSED;
              body[i+1] = ___UNUSED;

            chain_non_mem_alloc:
              key = ___MEM_ALLOCATED_CLEAR(key); /* recover true encoding */
              probe2 = ___GCHASHTABLE_HASH1(key,size2>>1) << 1;
              step2 = ___GCHASHTABLE_HASH2(key,size2>>1) << 1;

            next_non_mem_alloc:
              obj = body[probe2+___GCHASHTABLE_KEY0];

              if (obj == ___UNUSED)
                {
                  /* storing into an unused entry */

                  body[probe2+___GCHASHTABLE_KEY0] = key;
                  body[probe2+___GCHASHTABLE_VAL0] = val;
                }
              else if (___MEM_ALLOCATED(obj))
                {
                  /* storing into an active entry */

                  body[probe2+___GCHASHTABLE_KEY0] = key;
                  key = obj;
                  obj = body[probe2+___GCHASHTABLE_VAL0];
                  body[probe2+___GCHASHTABLE_VAL0] = val;
                  val = obj;
                  goto chain_non_mem_alloc; /* now move overwritten entry */
                }
              else
                {
                  /* an entry has been moved here, so keep looking */

                  probe2 -= step2;
                  if (probe2 < 0)
                    probe2 += size2;
                  goto next_non_mem_alloc;
                }
            }
        }
    }
  else
    {
      /*
* Free deleted entries and mark key field of all active
* entries.
*/

      for (i=___GCHASHTABLE_KEY0; i<words; i+=2)
        {
          ___WORD key = body[i];
          if (key == ___DELETED)
            {
              body[i] = ___UNUSED;
              body[___GCHASHTABLE_FREE] =
                ___FIXADD(body[___GCHASHTABLE_FREE], ___FIX(1));
            }
          else
            body[i] = ___MEM_ALLOCATED_CLEAR(key);
        }

      /*
* Move the active entries.
*/

      for (i=___GCHASHTABLE_KEY0; i<words; i+=2)
        {
          ___WORD key = body[i];

          if (key != ___UNUSED && !___MEM_ALLOCATED(key))
            {
              /* this is an active entry that has not been moved yet */

              ___SCMOBJ val = body[i+1];
              ___SCMOBJ obj;
              int probe2;
              int step2;

              body[i] = ___UNUSED;
              body[i+1] = ___UNUSED;

            chain_mem_alloc:
              key = ___MEM_ALLOCATED_SET(key); /* recover true encoding */
              probe2 = ___GCHASHTABLE_HASH1(key,size2>>1) << 1;
              step2 = ___GCHASHTABLE_HASH2(key,size2>>1) << 1;

            next_mem_alloc:
              obj = body[probe2+___GCHASHTABLE_KEY0];

              if (obj == ___UNUSED)
                {
                  /* storing into an unused entry */

                  body[probe2+___GCHASHTABLE_KEY0] = key;
                  body[probe2+___GCHASHTABLE_VAL0] = val;
                }
              else if (!___MEM_ALLOCATED(obj))
                {
                  /* storing into an active entry */

                  body[probe2+___GCHASHTABLE_KEY0] = key;
                  key = obj;
                  obj = body[probe2+___GCHASHTABLE_VAL0];
                  body[probe2+___GCHASHTABLE_VAL0] = val;
                  val = obj;
                  goto chain_mem_alloc; /* now move overwritten entry */
                }
              else
                {
                  /* an entry has been moved here, so keep looking */

                  probe2 -= step2;
                  if (probe2 < 0)
                    probe2 += size2;
                  goto next_mem_alloc;
                }
            }
        }
    }
}

___SCMOBJ ___gc_hash_table_ref
   ___P((___SCMOBJ ht,
         ___SCMOBJ key),
        (ht,
         key)
___SCMOBJ ht;
___SCMOBJ key;)
{
  int size2;
  int probe2;
  ___SCMOBJ obj;

  if (!___FIXZEROP(___FIXAND(___FIELD(ht, ___GCHASHTABLE_FLAGS),
                             ___FIX(___GCHASHTABLE_FLAG_KEY_MOVED))))
    ___gc_hash_table_rehash_in_situ (ht);

  size2 = ___INT(___VECTORLENGTH(ht)) - ___GCHASHTABLE_KEY0;
  probe2 = ___GCHASHTABLE_HASH1(key,size2>>1) << 1;
  obj = ___FIELD(ht, probe2+___GCHASHTABLE_KEY0);

  if (___EQP(obj,key))
    return ___FIELD(ht, probe2+___GCHASHTABLE_VAL0);
  else if (!___EQP(obj,___UNUSED))
    {
      int step2 = ___GCHASHTABLE_HASH2(key,size2>>1) << 1;

      for (;;)
        {
          probe2 -= step2;
          if (probe2 < 0)
            probe2 += size2;
          obj = ___FIELD(ht, probe2+___GCHASHTABLE_KEY0);

          if (___EQP(obj,key))
            return ___FIELD(ht, probe2+___GCHASHTABLE_VAL0);
          else if (___EQP(obj,___UNUSED))
            break;
        }
    }

  return ___UNUSED; /* key was not found */
}

___SCMOBJ ___gc_hash_table_set
   ___P((___SCMOBJ ht,
         ___SCMOBJ key,
         ___SCMOBJ val),
        (ht,
         key,
         val)
___SCMOBJ ht;
___SCMOBJ key;
___SCMOBJ val;)
{
  int size2;
  int probe2;
  ___SCMOBJ obj;

  if (!___FIXZEROP(___FIXAND(___FIELD(ht, ___GCHASHTABLE_FLAGS),
                             ___FIX(___GCHASHTABLE_FLAG_KEY_MOVED))))
    ___gc_hash_table_rehash_in_situ (ht);

  size2 = ___INT(___VECTORLENGTH(ht)) - ___GCHASHTABLE_KEY0;
  probe2 = ___GCHASHTABLE_HASH1(key,size2>>1) << 1;
  obj = ___FIELD(ht, probe2+___GCHASHTABLE_KEY0);

  if (!___EQP(val,___ABSENT))
    {
      /* trying to add or replace an entry */

      if (___EQP(obj,key))
        {
        replace_entry:
          ___FIELD(ht, probe2+___GCHASHTABLE_VAL0) = val;
        }
      else if (___EQP(obj,___UNUSED))
        {
        add_entry:
          ___FIELD(ht, probe2+___GCHASHTABLE_KEY0) = key;
          ___FIELD(ht, probe2+___GCHASHTABLE_VAL0) = val;
          ___FIELD(ht, ___GCHASHTABLE_COUNT) =
            ___FIXADD(___FIELD(ht, ___GCHASHTABLE_COUNT), ___FIX(1));
          if (___FIXNEGATIVEP(___FIELD(ht, ___GCHASHTABLE_FREE) =
                                ___FIXSUB(___FIELD(ht, ___GCHASHTABLE_FREE),
                                          ___FIX(1))))
            return ___TRU;
        }
      else
        {
          int step2 = ___GCHASHTABLE_HASH2(key,size2>>1) << 1;
          int deleted2 = -1;

          for (;;)
            {
              if (deleted2 < 0 && ___EQP(obj,___DELETED))
                deleted2 = probe2;

              probe2 -= step2;
              if (probe2 < 0)
                probe2 += size2;
              obj = ___FIELD(ht, probe2+___GCHASHTABLE_KEY0);

              if (___EQP(obj,key))
                goto replace_entry;

              if (___EQP(obj,___UNUSED))
                {
                  if (deleted2 < 0)
                    goto add_entry;

                  ___FIELD(ht, deleted2+___GCHASHTABLE_KEY0) = key;
                  ___FIELD(ht, deleted2+___GCHASHTABLE_VAL0) = val;
                  ___FIELD(ht, ___GCHASHTABLE_COUNT) =
                    ___FIXADD(___FIELD(ht, ___GCHASHTABLE_COUNT), ___FIX(1));

                  break;
                }
            }
        }
    }
  else
    {
      /* trying to delete an entry */

      if (___EQP(obj,key))
        {
        delete_entry:
          ___FIELD(ht, probe2+___GCHASHTABLE_KEY0) = ___DELETED;
          ___FIELD(ht, probe2+___GCHASHTABLE_VAL0) = ___UNUSED;
          ___FIELD(ht, ___GCHASHTABLE_COUNT) =
            ___FIXSUB(___FIELD(ht, ___GCHASHTABLE_COUNT),
                      ___FIX(1));
          if (___FIXLT(___FIELD(ht, ___GCHASHTABLE_COUNT),
                       ___FIELD(ht, ___GCHASHTABLE_MIN_COUNT)))
            return ___TRU;
        }
      else if (!___EQP(obj,___UNUSED))
        {
          int step2 = ___GCHASHTABLE_HASH2(key,size2>>1) << 1;

          for (;;)
            {
              probe2 -= step2;
              if (probe2 < 0)
                probe2 += size2;
              obj = ___FIELD(ht, probe2+___GCHASHTABLE_KEY0);

              if (___EQP(obj,key))
                goto delete_entry;

              if (___EQP(obj,___UNUSED))
                break;
            }
        }
    }

 /*
* Hash table does not need to be resized.
*/

  return ___FAL;
}

___SCMOBJ ___gc_hash_table_rehash
   ___P((___SCMOBJ ht_src,
         ___SCMOBJ ht_dst),
        (ht_src,
         ht_dst)
___SCMOBJ ht_src;
___SCMOBJ ht_dst;)
{
  ___WORD* body_src = ___BODY_AS(ht_src,___tSUBTYPED);
  long words = ___HD_WORDS(body_src[-1]);
  int i;

  for (i=___GCHASHTABLE_KEY0; i<words; i+=2)
    {
      ___WORD key = body_src[i];

      if (key != ___UNUSED &&
          key != ___DELETED)
        ___gc_hash_table_set (ht_dst, key, body_src[i+1]);
    }

  return ht_dst;
}

#ifdef ___DEBUG_GARBAGE_COLLECT

___BOOL ___garbage_collect_debug
   ___P((long nonmovable_words_needed,
         int line,
         char *file),
        (nonmovable_words_needed,
         line,
         file)
long nonmovable_words_needed;
int line;
char *file;)

#else

___BOOL ___garbage_collect
   ___P((long nonmovable_words_needed),
        (nonmovable_words_needed)
long nonmovable_words_needed;)

#endif
{
  long avail;
  int target_nb_sections;
  int stack_msection_index;
  ___BOOL overflow = 0;
  ___processor_state ___ps = ___PSTATE;
  ___F64 user_time_start, sys_time_start, real_time_start;
  ___F64 user_time_end, sys_time_end, real_time_end;
  ___F64 gc_user_time, gc_sys_time, gc_real_time;

  ___process_times (&user_time_start, &sys_time_start, &real_time_start);

  alloc_stack_ptr = ___ps->fp; /* needed by 'WORDS_OCCUPIED' */
  alloc_heap_ptr = ___ps->hp; /* needed by 'WORDS_OCCUPIED' */

#ifdef ___DEBUG_GARBAGE_COLLECT
  ___printf ("----------------------------------------- GC\n");
  ___printf ("heap_size = %d\n", heap_size);
  ___printf ("WORDS_OCCUPIED = %d\n", WORDS_OCCUPIED);
  ___printf ("___ps->stack_start = 0x%08x\n", ___ps->stack_start);
  ___printf ("___ps->stack_break = 0x%08x\n", ___ps->stack_break);
  ___printf ("___ps->fp = 0x%08x\n", ___ps->fp);
  ___printf ("___ps->stack_limit = 0x%08x\n", ___ps->stack_limit);
  ___printf ("___ps->heap_limit = 0x%08x\n", ___ps->heap_limit);
  ___printf ("___ps->hp = 0x%08x\n", ___ps->hp);
#endif

  words_nonmovable += nonmovable_words_needed;

  ___GSTATE->bytes_allocated_minus_occupied =
    ___GSTATE->bytes_allocated_minus_occupied +
    ___CAST(___F64,WORDS_OCCUPIED) * ___WS;

#ifdef GATHER_STATS
  movable_pair_objs = 0;
  {
    int i;
    for (i=0; i<=MAX_STAT_SIZE+1; i++)
      movable_subtyped_objs[i] = 0;
  }
#endif

  stack_msection_index = stack_msection->index;

  words_prev_msections = 0;

  tospace_at_top = !tospace_at_top;
  stack_msection = 0;
  heap_msection = 0;
  nb_msections_used = 0;

  next_heap_msection ();

  scan_msection = heap_msection;
  scan_ptr = alloc_heap_ptr;

  /* maintain list of GC hash tables reached by GC */

  reached_gc_hash_tables = ___TAG(0,0);

  /* trace externally referenced still objects */

  init_still_objs_to_scan ();

  /* trace registers */

#ifdef ENABLE_CONSISTENCY_CHECKS
  reference_location = IN_REGISTER;
#endif

  mark_array (&___ps->current_thread, 1);
  mark_array (&___ps->run_queue, 1);

  mark_array (___ps->r, ___NB_GVM_REGS);

  mark_array (&___GSTATE->symbol_table, 1);
  mark_array (&___GSTATE->keyword_table, 1);

  /* trace global variables */

#ifdef ENABLE_CONSISTENCY_CHECKS
  reference_location = IN_GLOBAL_VAR;
#endif

  {
    ___WORD p = ___ps->glo_list_head;

    while (p != 0)
      {
#ifdef ___DEBUG_GARBAGE_COLLECT
        print_global_var_name (p);
#endif
        mark_array (&___CAST(___glo_struct*,p)->val, 1);
        p = ___CAST(___glo_struct*,p)->next;
      }
  }

  /* trace continuation */

#ifdef ENABLE_CONSISTENCY_CHECKS
  reference_location = IN_CONTINUATION;
#endif

  mark_continuation ();

  /* trace reference counted objects */

#ifdef ENABLE_CONSISTENCY_CHECKS
  reference_location = IN_RC;
#endif

  mark_rc ();

  /* mark objects reachable from marked objects */

  traverse_weak_refs = 0; /* don't traverse weak references in first pass */

  again:

  if (___CAST(___WORD*,still_objs_to_scan) != 0)
    scan_still_objs_to_scan ();

  if (scan_msection != heap_msection ||
      scan_ptr < alloc_heap_ptr)
    {
      scan_movable_objs_to_scan ();
      goto again;
    }

  if (!traverse_weak_refs)
    {
      /*
* At this point all of the objects accessible from the roots
* without having to traverse a weak reference have been scanned
* by the GC.
*/

      traverse_weak_refs = 1;

      process_wills ();

      goto again;
    }

  process_gc_hash_tables ();

  free_unmarked_still_objs ();

  target_nb_sections = (adjust_heap (WORDS_AVAILABLE, WORDS_OCCUPIED)
                        - words_nonmovable
                        + normal_overflow_reserve
                        + 2*___MSECTION_FUDGE
                        + 2*((___MSECTION_SIZE>>1)-___MSECTION_FUDGE+1) - 1)
                       / (2*((___MSECTION_SIZE>>1)-___MSECTION_FUDGE+1));

  if (target_nb_sections < nb_msections_used)
    {
      target_nb_sections = the_msections->nb_sections;
      overflow = 1;
    }

  if (target_nb_sections < ___MIN_NB_MSECTIONS)
    target_nb_sections = ___MIN_NB_MSECTIONS;

  /* Move the stack */

  {
    ___WORD *start;
    long length;
    ___WORD *p1;
    ___WORD *p2;

    start = alloc_stack_ptr;
    length = (___ps->stack_break + ___BREAK_FRAME_SPACE) - start;

    if (stack_msection_index >= target_nb_sections)
      {
        /*
* The msection currently containing the stack is about to be
* reclaimed by the call to 'adjust_msections'. So we need to
* save the stack before moving it to its final destination.
*/

        p1 = start + length;
        p2 = start_of_fromspace (the_msections->head) + length;

        while (p1 != start)
          *--p2 = *--p1;

        start = p2;
      }

    adjust_msections (&the_msections, target_nb_sections);

    next_stack_msection ();

    p1 = start + length;
    p2 = alloc_stack_ptr;

    ___ps->stack_start = alloc_stack_start;
    ___ps->stack_break = p2 - ___BREAK_FRAME_SPACE;

    while (p1 != start)
      *--p2 = *--p1;

    alloc_stack_ptr = p2;
  }

#ifdef ENABLE_CONSISTENCY_CHECKS
  if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) >= 1)
    zap_fromspace ();
#endif

  if (alloc_heap_ptr > alloc_heap_limit - ___MSECTION_FUDGE)
    next_heap_msection ();

  avail = WORDS_AVAILABLE + overflow_reserve - WORDS_OCCUPIED;

  if (avail <= overflow_reserve + (WORDS_MOVABLE_USABLE >> 10))
    {
      overflow = 1;
      overflow_reserve >>= 5; /* make 96.875% of reserve usable */
      if (overflow_reserve == 0)
        fatal_heap_overflow ();
    }
  else if (avail >= normal_overflow_reserve)
    overflow_reserve = normal_overflow_reserve; /* restore overflow reserve */

  ___GSTATE->bytes_allocated_minus_occupied =
    ___GSTATE->bytes_allocated_minus_occupied -
    ___CAST(___F64,WORDS_OCCUPIED) * ___WS;

  words_nonmovable -= nonmovable_words_needed;

  heap_size = WORDS_AVAILABLE;

  setup_pstate ();

  ___process_times (&user_time_end, &sys_time_end, &real_time_end);

  gc_user_time = user_time_end - user_time_start;
  gc_sys_time = sys_time_end - sys_time_start;
  gc_real_time = real_time_end - real_time_start;

  ___GSTATE->nb_gcs = ___GSTATE->nb_gcs + 1.0;
  ___GSTATE->gc_user_time += gc_user_time;
  ___GSTATE->gc_sys_time += gc_sys_time;
  ___GSTATE->gc_real_time += gc_real_time;

  ___GSTATE->last_gc_user_time = gc_user_time;
  ___GSTATE->last_gc_sys_time = gc_sys_time;
  ___GSTATE->last_gc_real_time = gc_real_time;
  ___GSTATE->last_gc_heap_size = ___CAST(___F64,heap_size) * ___WS;
  ___GSTATE->last_gc_alloc =
    ___GSTATE->bytes_allocated_minus_occupied +
    ___CAST(___F64,WORDS_OCCUPIED) * ___WS;
  ___GSTATE->last_gc_live = ___CAST(___F64,WORDS_OCCUPIED) * ___WS;
  ___GSTATE->last_gc_movable = ___CAST(___F64,WORDS_MOVABLE) * ___WS;
  ___GSTATE->last_gc_nonmovable = ___CAST(___F64,words_nonmovable) * ___WS;

  ___raise_interrupt (___INTR_GC); /* raise gc interrupt */

  return overflow;
}


#ifdef ___DEBUG_STACK_LIMIT

___BOOL ___stack_limit_debug
   ___P((int line,
         char *file),
        (line,
         file)
int line;
char *file;)

#else

___BOOL ___stack_limit ___PVOID

#endif
{
  ___processor_state ___ps = ___PSTATE;
  long avail;

#ifdef ___DEBUG_STACK_LIMIT
  ___ps->stack_limit_line = line;
  ___ps->stack_limit_file = file;
  ___printf ("___POLL caused ___stack_limit call at %s:%d\n",
             ___ps->poll_file,
             ___ps->poll_line);
#endif

#ifdef ENABLE_CONSISTENCY_CHECKS
  if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) >= 1)
    check_fudge_used ();
#endif

  alloc_stack_ptr = ___ps->fp; /* needed by 'WORDS_OCCUPIED' */
  alloc_heap_ptr = ___ps->hp; /* needed by 'WORDS_OCCUPIED' */

  avail = (heap_size - WORDS_OCCUPIED) / 2;

  if (avail > ___MSECTION_WASTE
#ifdef CALL_GC_FREQUENTLY
      && --___gc_calls_to_punt >= 0
#endif
     )
    {
      if (alloc_stack_ptr < alloc_stack_limit + ___MSECTION_FUDGE)
        {
          ___WORD frame;

          if (alloc_stack_ptr != ___ps->stack_break)
            frame = ___CAST(___WORD,alloc_stack_ptr);
          else
            frame = ___FP_STK(alloc_stack_ptr,-___BREAK_FRAME_NEXT);

          next_stack_msection ();

          /*
* Create a "break frame" in the new stack msection.
*/

          ___ps->stack_start = alloc_stack_start;
          alloc_stack_ptr = alloc_stack_start;

          ___FP_ADJFP(alloc_stack_ptr,___BREAK_FRAME_SPACE)
          ___FP_SET_STK(alloc_stack_ptr,-___BREAK_FRAME_NEXT,frame)

          ___ps->stack_break = alloc_stack_ptr;
        }

      setup_pstate ();

      return 0;
    }

  return 1;
}


#ifdef ___DEBUG_HEAP_LIMIT

___BOOL ___heap_limit_debug
   ___P((int line,
         char *file),
        (line,
         file)
int line;
char *file;)

#else

___BOOL ___heap_limit ___PVOID

#endif
{
  ___processor_state ___ps = ___PSTATE;
  long avail;

#ifdef ___DEBUG_HEAP_LIMIT
  ___ps->heap_limit_line = line;
  ___ps->heap_limit_file = file;
#endif

#ifdef ENABLE_CONSISTENCY_CHECKS
  if (___DEBUG_SETTINGS_LEVEL(___setup_params.debug_settings) >= 1)
    check_fudge_used ();
#endif

  alloc_stack_ptr = ___ps->fp; /* needed by 'WORDS_OCCUPIED' */
  alloc_heap_ptr = ___ps->hp; /* needed by 'WORDS_OCCUPIED' */

  avail = (heap_size - WORDS_OCCUPIED) / 2;

  if (avail > ___MSECTION_WASTE
#ifdef CALL_GC_FREQUENTLY
      && --___gc_calls_to_punt >= 0
#endif
     )
    {
      if (alloc_heap_ptr > alloc_heap_limit - ___MSECTION_FUDGE)
        next_heap_msection ();

      setup_pstate ();

      return 0;
    }

  return 1;
}


/*---------------------------------------------------------------------------*/


___F64 ___bytes_allocated ___PVOID
{
  ___processor_state ___ps = ___PSTATE;

  alloc_stack_ptr = ___ps->fp; /* needed by 'WORDS_OCCUPIED' */
  alloc_heap_ptr = ___ps->hp; /* needed by 'WORDS_OCCUPIED' */

  return ___GSTATE->bytes_allocated_minus_occupied +
         ___CAST(___F64,WORDS_OCCUPIED) * ___WS;
}


/*---------------------------------------------------------------------------*/
Something went wrong with that request. Please try again.