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
|
/* poolamc.c: AUTOMATIC MOSTLY-COPYING MEMORY POOL CLASS
*
* $Id$
* Copyright (c) 2001-2020 Ravenbrook Limited. See end of file for license.
* Portions copyright (C) 2002 Global Graphics Software.
*
* .sources: <design/poolamc>.
*/
#include "mpscamc.h"
#include "locus.h"
#include "bt.h"
#include "mpm.h"
#include "nailboard.h"
SRCID(poolamc, "$Id$");
typedef struct AMCStruct *AMC;
typedef struct amcGenStruct *amcGen;
/* Function returning TRUE if block in nailboarded segment is pinned. */
typedef Bool (*amcPinnedFunction)(AMC amc, Nailboard board, Addr base, Addr limit);
/* forward declarations */
static void amcSegBufferEmpty(Seg seg, Buffer buffer);
static Res amcSegWhiten(Seg seg, Trace trace);
static Res amcSegScan(Bool *totalReturn, Seg seg, ScanState ss);
static void amcSegReclaim(Seg seg, Trace trace);
static Bool amcSegHasNailboard(Seg seg);
static Nailboard amcSegNailboard(Seg seg);
static Bool AMCCheck(AMC amc);
static Res amcSegFix(Seg seg, ScanState ss, Ref *refIO);
static Res amcSegFixEmergency(Seg seg, ScanState ss, Ref *refIO);
static void amcSegWalk(Seg seg, Format format, FormattedObjectsVisitor f,
void *p, size_t s);
/* local class declarations */
typedef AMC AMCZPool;
#define AMCZPoolCheck AMCCheck
DECLARE_CLASS(Pool, AMCZPool, AbstractCollectPool);
typedef AMC AMCPool;
DECLARE_CLASS(Pool, AMCPool, AMCZPool);
DECLARE_CLASS(Buffer, amcBuf, SegBuf);
DECLARE_CLASS(Seg, amcSeg, MutatorSeg);
/* amcGenStruct -- pool AMC generation descriptor */
#define amcGenSig ((Sig)0x519A3C9E) /* SIGnature AMC GEn */
typedef struct amcGenStruct {
PoolGenStruct pgen;
RingStruct amcRing; /* link in list of gens in pool */
Buffer forward; /* forwarding buffer */
Sig sig; /* design.mps.sig.field.end.outer */
} amcGenStruct;
#define amcGenAMC(amcgen) MustBeA(AMCZPool, (amcgen)->pgen.pool)
#define amcGenPool(amcgen) ((amcgen)->pgen.pool)
#define amcGenNr(amcgen) ((amcgen)->pgen.nr)
#define RAMP_RELATION(X) \
X(RampOUTSIDE, "outside ramp") \
X(RampBEGIN, "begin ramp") \
X(RampRAMPING, "ramping") \
X(RampFINISH, "finish ramp") \
X(RampCOLLECTING, "collecting ramp")
#define RAMP_ENUM(e, s) e,
enum {
RAMP_RELATION(RAMP_ENUM)
RampLIMIT
};
#undef RAMP_ENUM
/* amcSegStruct -- AMC-specific fields appended to GCSegStruct
*
* .seg.accounted-as-buffered: The "accountedAsBuffered" flag is TRUE
* if the segment has an atached buffer and is accounted against the
* pool generation's bufferedSize. But note that if this is FALSE, the
* segment might still have an attached buffer -- this happens if the
* segment was condemned while the buffer was attached.
*
* .seg.old: The "old" flag is TRUE if the segment has been collected
* at least once, and so its size is accounted against the pool
* generation's oldSize.
*
* .seg.deferred: The "deferred" flag is TRUE if its size accounting
* in the pool generation has been deferred. This is set if the
* segment was created in ramping mode (and so we don't want it to
* contribute to the pool generation's newSize and so provoke a
* collection via TracePoll), and by hash array allocations (where we
* don't want the allocation to provoke a collection that makes the
* location dependency stale immediately).
*/
typedef struct amcSegStruct *amcSeg;
#define amcSegSig ((Sig)0x519A3C59) /* SIGnature AMC SeG */
typedef struct amcSegStruct {
GCSegStruct gcSegStruct; /* superclass fields must come first */
amcGen gen; /* generation this segment belongs to */
Nailboard board; /* nailboard for this segment or NULL if none */
Size forwarded[TraceLIMIT]; /* size of objects forwarded for each trace */
BOOLFIELD(accountedAsBuffered); /* .seg.accounted-as-buffered */
BOOLFIELD(old); /* .seg.old */
BOOLFIELD(deferred); /* .seg.deferred */
Sig sig; /* design.mps.sig.field.end.outer */
} amcSegStruct;
ATTRIBUTE_UNUSED
static Bool amcSegCheck(amcSeg amcseg)
{
CHECKS(amcSeg, amcseg);
CHECKD(GCSeg, &amcseg->gcSegStruct);
CHECKU(amcGen, amcseg->gen);
if (amcseg->board) {
CHECKD(Nailboard, amcseg->board);
CHECKL(SegNailed(MustBeA(Seg, amcseg)) != TraceSetEMPTY);
}
/* CHECKL(BoolCheck(amcseg->accountedAsBuffered)); <design/type#.bool.bitfield.check> */
/* CHECKL(BoolCheck(amcseg->old)); <design/type#.bool.bitfield.check> */
/* CHECKL(BoolCheck(amcseg->deferred)); <design/type#.bool.bitfield.check> */
return TRUE;
}
/* AMCSegInit -- initialise an AMC segment */
ARG_DEFINE_KEY(amc_seg_gen, Pointer);
#define amcKeySegGen (&_mps_key_amc_seg_gen)
static Res AMCSegInit(Seg seg, Pool pool, Addr base, Size size, ArgList args)
{
amcGen amcgen;
amcSeg amcseg;
Res res;
ArgStruct arg;
ArgRequire(&arg, args, amcKeySegGen);
amcgen = arg.val.p;
/* Initialize the superclass fields first via next-method call */
res = NextMethod(Seg, amcSeg, init)(seg, pool, base, size, args);
if(res != ResOK)
return res;
amcseg = CouldBeA(amcSeg, seg);
amcseg->gen = amcgen;
amcseg->board = NULL;
amcseg->accountedAsBuffered = FALSE;
amcseg->old = FALSE;
amcseg->deferred = FALSE;
SetClassOfPoly(seg, CLASS(amcSeg));
amcseg->sig = amcSegSig;
AVERC(amcSeg, amcseg);
return ResOK;
}
/* amcSegFinish -- finish an AMC segment */
static void amcSegFinish(Inst inst)
{
Seg seg = MustBeA(Seg, inst);
amcSeg amcseg = MustBeA(amcSeg, seg);
amcseg->sig = SigInvalid;
/* finish the superclass fields last */
NextMethod(Inst, amcSeg, finish)(inst);
}
/* AMCSegSketch -- summarise the segment state for a human reader
*
* Write a short human-readable text representation of the segment
* state into storage indicated by pbSketch+cbSketch.
*
* A typical sketch is "bGW_", meaning the seg has a nailboard, has
* some Grey and some White objects, and has no buffer attached.
*/
static void AMCSegSketch(Seg seg, char *pbSketch, size_t cbSketch)
{
Buffer buffer;
AVER(pbSketch);
AVER(cbSketch >= 5);
if(SegNailed(seg) == TraceSetEMPTY) {
pbSketch[0] = 'm'; /* mobile */
} else if (amcSegHasNailboard(seg)) {
pbSketch[0] = 'b'; /* boarded */
} else {
pbSketch[0] = 's'; /* stuck */
}
if(SegGrey(seg) == TraceSetEMPTY) {
pbSketch[1] = '_';
} else {
pbSketch[1] = 'G'; /* Grey */
}
if(SegWhite(seg) == TraceSetEMPTY) {
pbSketch[2] = '_';
} else {
pbSketch[2] = 'W'; /* White */
}
if (!SegBuffer(&buffer, seg)) {
pbSketch[3] = '_';
} else {
Bool mut = BufferIsMutator(buffer);
Bool flipped = ((buffer->mode & BufferModeFLIPPED) != 0);
Bool trapped = BufferIsTrapped(buffer);
Bool limitzeroed = (buffer->ap_s.limit == 0);
pbSketch[3] = 'X'; /* I don't know what's going on! */
if((flipped == trapped) && (trapped == limitzeroed)) {
if(mut) {
if(flipped) {
pbSketch[3] = 's'; /* stalo */
} else {
pbSketch[3] = 'n'; /* neo */
}
} else {
if(!flipped) {
pbSketch[3] = 'f'; /* forwarding */
}
}
} else {
/* I don't know what's going on! */
}
}
pbSketch[4] = '\0';
AVER(4 < cbSketch);
}
/* AMCSegDescribe -- describe the contents of a segment
*
* <design/poolamc#.seg-describe>.
*/
static Res AMCSegDescribe(Inst inst, mps_lib_FILE *stream, Count depth)
{
amcSeg amcseg = CouldBeA(amcSeg, inst);
Seg seg = CouldBeA(Seg, amcseg);
Res res;
Pool pool;
Addr i, p, base, limit, init;
Align step;
Size row;
char abzSketch[5];
Buffer buffer;
if (!TESTC(amcSeg, amcseg))
return ResPARAM;
if (stream == NULL)
return ResPARAM;
/* Describe the superclass fields first via next-method call */
res = NextMethod(Inst, amcSeg, describe)(inst, stream, depth);
if (res != ResOK)
return res;
pool = SegPool(seg);
step = PoolAlignment(pool);
row = step * 64;
base = SegBase(seg);
p = AddrAdd(base, pool->format->headerSize);
limit = SegLimit(seg);
if (amcSegHasNailboard(seg)) {
res = WriteF(stream, depth + 2, "Boarded\n", NULL);
} else if (SegNailed(seg) == TraceSetEMPTY) {
res = WriteF(stream, depth + 2, "Mobile\n", NULL);
} else {
res = WriteF(stream, depth + 2, "Stuck\n", NULL);
}
if(res != ResOK)
return res;
res = WriteF(stream, depth + 2,
"Map: *===:object @+++:nails bbbb:buffer\n", NULL);
if (res != ResOK)
return res;
if (SegBuffer(&buffer, seg))
init = BufferGetInit(buffer);
else
init = limit;
for (i = base; i < limit; i = AddrAdd(i, row)) {
Addr j;
char c;
res = WriteF(stream, depth + 2, "$A ", (WriteFA)i, NULL);
if (res != ResOK)
return res;
/* @@@@ This misses a header-sized pad at the end. */
for (j = i; j < AddrAdd(i, row); j = AddrAdd(j, step)) {
if (j >= limit)
c = ' '; /* if seg is not a whole number of print rows */
else if (j >= init)
c = 'b';
else {
Bool nailed = amcSegHasNailboard(seg)
&& NailboardGet(amcSegNailboard(seg), j);
if (j == p) {
c = (nailed ? '@' : '*');
p = (pool->format->skip)(p);
} else {
c = (nailed ? '+' : '=');
}
}
res = WriteF(stream, 0, "$C", (WriteFC)c, NULL);
if (res != ResOK)
return res;
}
res = WriteF(stream, 0, "\n", NULL);
if (res != ResOK)
return res;
}
AMCSegSketch(seg, abzSketch, NELEMS(abzSketch));
res = WriteF(stream, depth + 2, "Sketch: $S\n", (WriteFS)abzSketch, NULL);
if(res != ResOK)
return res;
return ResOK;
}
/* amcSegClass -- Class definition for AMC segments */
DEFINE_CLASS(Seg, amcSeg, klass)
{
INHERIT_CLASS(klass, amcSeg, MutatorSeg);
SegClassMixInNoSplitMerge(klass); /* no support for this (yet) */
klass->instClassStruct.describe = AMCSegDescribe;
klass->instClassStruct.finish = amcSegFinish;
klass->size = sizeof(amcSegStruct);
klass->init = AMCSegInit;
klass->bufferEmpty = amcSegBufferEmpty;
klass->whiten = amcSegWhiten;
klass->scan = amcSegScan;
klass->fix = amcSegFix;
klass->fixEmergency = amcSegFixEmergency;
klass->reclaim = amcSegReclaim;
klass->walk = amcSegWalk;
AVERT(SegClass, klass);
}
/* amcSegHasNailboard -- test whether the segment has a nailboard
*
* <design/poolamc#.fix.nail.distinguish>.
*/
static Bool amcSegHasNailboard(Seg seg)
{
amcSeg amcseg = MustBeA(amcSeg, seg);
return amcseg->board != NULL;
}
/* amcSegNailboard -- get the nailboard for this segment */
static Nailboard amcSegNailboard(Seg seg)
{
amcSeg amcseg = MustBeA(amcSeg, seg);
AVER(amcSegHasNailboard(seg));
return amcseg->board;
}
/* amcSegGen -- get the generation structure for this segment */
static amcGen amcSegGen(Seg seg)
{
amcSeg amcseg = MustBeA(amcSeg, seg);
return amcseg->gen;
}
/* AMCStruct -- pool AMC descriptor
*
* <design/poolamc#.struct>.
*/
#define AMCSig ((Sig)0x519A3C99) /* SIGnature AMC */
typedef struct AMCStruct { /* <design/poolamc#.struct> */
PoolStruct poolStruct; /* generic pool structure */
RankSet rankSet; /* rankSet for entire pool */
RingStruct genRing; /* ring of generations */
Bool gensBooted; /* used during boot (init) */
size_t gens; /* number of generations */
amcGen *gen; /* (pointer to) array of generations */
amcGen nursery; /* the default mutator generation */
amcGen rampGen; /* the ramp generation */
amcGen afterRampGen; /* the generation after rampGen */
unsigned rampCount; /* <design/poolamc#.ramp.count> */
int rampMode; /* <design/poolamc#.ramp.mode> */
amcPinnedFunction pinned; /* function determining if block is pinned */
Size extendBy; /* segment size to extend pool by */
Size largeSize; /* min size of "large" segments */
Sig sig; /* design.mps.sig.field.end.outer */
} AMCStruct;
/* amcGenCheck -- check consistency of a generation structure */
ATTRIBUTE_UNUSED
static Bool amcGenCheck(amcGen gen)
{
AMC amc;
CHECKS(amcGen, gen);
CHECKD(PoolGen, &gen->pgen);
amc = amcGenAMC(gen);
CHECKU(AMC, amc);
CHECKD(Buffer, gen->forward);
CHECKD_NOSIG(Ring, &gen->amcRing);
return TRUE;
}
/* amcBufStruct -- AMC Buffer subclass
*
* This subclass of SegBuf records a link to a generation.
*/
#define amcBufSig ((Sig)0x519A3CBF) /* SIGnature AMC BuFfer */
typedef struct amcBufStruct *amcBuf;
typedef struct amcBufStruct {
SegBufStruct segbufStruct; /* superclass fields must come first */
amcGen gen; /* The AMC generation */
Bool forHashArrays; /* allocates hash table arrays, see AMCBufferFill */
Sig sig; /* design.mps.sig.field.end.outer */
} amcBufStruct;
/* amcBufCheck -- check consistency of an amcBuf */
ATTRIBUTE_UNUSED
static Bool amcBufCheck(amcBuf amcbuf)
{
CHECKS(amcBuf, amcbuf);
CHECKD(SegBuf, &amcbuf->segbufStruct);
if(amcbuf->gen != NULL)
CHECKD(amcGen, amcbuf->gen);
CHECKL(BoolCheck(amcbuf->forHashArrays));
/* hash array buffers only created by mutator */
CHECKL(BufferIsMutator(MustBeA(Buffer, amcbuf)) || !amcbuf->forHashArrays);
return TRUE;
}
/* amcBufGen -- Return the AMC generation of an amcBuf */
static amcGen amcBufGen(Buffer buffer)
{
return MustBeA(amcBuf, buffer)->gen;
}
/* amcBufSetGen -- Set the AMC generation of an amcBuf */
static void amcBufSetGen(Buffer buffer, amcGen gen)
{
amcBuf amcbuf = MustBeA(amcBuf, buffer);
if (gen != NULL)
AVERT(amcGen, gen);
amcbuf->gen = gen;
}
ARG_DEFINE_KEY(ap_hash_arrays, Bool);
#define amcKeyAPHashArrays (&_mps_key_ap_hash_arrays)
/* AMCBufInit -- Initialize an amcBuf */
static Res AMCBufInit(Buffer buffer, Pool pool, Bool isMutator, ArgList args)
{
AMC amc = MustBeA(AMCZPool, pool);
amcBuf amcbuf;
Res res;
Bool forHashArrays = FALSE;
ArgStruct arg;
if (ArgPick(&arg, args, amcKeyAPHashArrays))
forHashArrays = arg.val.b;
res = NextMethod(Buffer, amcBuf, init)(buffer, pool, isMutator, args);
if(res != ResOK)
return res;
amcbuf = CouldBeA(amcBuf, buffer);
if (BufferIsMutator(buffer)) {
/* Set up the buffer to be allocating in the nursery. */
amcbuf->gen = amc->nursery;
} else {
/* No gen yet -- see <design/poolamc#.gen.forward>. */
amcbuf->gen = NULL;
}
amcbuf->forHashArrays = forHashArrays;
SetClassOfPoly(buffer, CLASS(amcBuf));
amcbuf->sig = amcBufSig;
AVERC(amcBuf, amcbuf);
BufferSetRankSet(buffer, amc->rankSet);
return ResOK;
}
/* AMCBufFinish -- Finish an amcBuf */
static void AMCBufFinish(Inst inst)
{
Buffer buffer = MustBeA(Buffer, inst);
amcBuf amcbuf = MustBeA(amcBuf, buffer);
amcbuf->sig = SigInvalid;
NextMethod(Inst, amcBuf, finish)(inst);
}
/* amcBufClass -- The class definition */
DEFINE_CLASS(Buffer, amcBuf, klass)
{
INHERIT_CLASS(klass, amcBuf, SegBuf);
klass->instClassStruct.finish = AMCBufFinish;
klass->size = sizeof(amcBufStruct);
klass->init = AMCBufInit;
AVERT(BufferClass, klass);
}
/* amcGenCreate -- create a generation */
static Res amcGenCreate(amcGen *genReturn, AMC amc, GenDesc gen)
{
Pool pool = MustBeA(AbstractPool, amc);
Arena arena;
Buffer buffer;
amcGen amcgen;
Res res;
void *p;
arena = pool->arena;
res = ControlAlloc(&p, arena, sizeof(amcGenStruct));
if(res != ResOK)
goto failControlAlloc;
amcgen = (amcGen)p;
res = BufferCreate(&buffer, CLASS(amcBuf), pool, FALSE, argsNone);
if(res != ResOK)
goto failBufferCreate;
res = PoolGenInit(&amcgen->pgen, gen, pool);
if(res != ResOK)
goto failGenInit;
RingInit(&amcgen->amcRing);
amcgen->forward = buffer;
amcgen->sig = amcGenSig;
AVERT(amcGen, amcgen);
RingAppend(&amc->genRing, &amcgen->amcRing);
*genReturn = amcgen;
return ResOK;
failGenInit:
BufferDestroy(buffer);
failBufferCreate:
ControlFree(arena, p, sizeof(amcGenStruct));
failControlAlloc:
return res;
}
/* amcGenDestroy -- destroy a generation */
static void amcGenDestroy(amcGen gen)
{
Arena arena;
AVERT(amcGen, gen);
arena = PoolArena(amcGenPool(gen));
gen->sig = SigInvalid;
RingRemove(&gen->amcRing);
RingFinish(&gen->amcRing);
PoolGenFinish(&gen->pgen);
BufferDestroy(gen->forward);
ControlFree(arena, gen, sizeof(amcGenStruct));
}
/* amcGenDescribe -- describe an AMC generation */
static Res amcGenDescribe(amcGen gen, mps_lib_FILE *stream, Count depth)
{
Res res;
if(!TESTT(amcGen, gen))
return ResFAIL;
if (stream == NULL)
return ResFAIL;
res = WriteF(stream, depth,
"amcGen $P {\n", (WriteFP)gen,
" buffer $P\n", (WriteFP)gen->forward, NULL);
if (res != ResOK)
return res;
res = PoolGenDescribe(&gen->pgen, stream, depth + 2);
if (res != ResOK)
return res;
res = WriteF(stream, depth, "} amcGen $P\n", (WriteFP)gen, NULL);
return res;
}
/* amcSegCreateNailboard -- create nailboard for segment */
static Res amcSegCreateNailboard(Seg seg)
{
amcSeg amcseg = MustBeA(amcSeg, seg);
Pool pool = SegPool(seg);
Nailboard board;
Arena arena;
Res res;
AVER(!amcSegHasNailboard(seg));
arena = PoolArena(pool);
res = NailboardCreate(&board, arena, pool->alignment,
SegBase(seg), SegLimit(seg));
if (res != ResOK)
return res;
amcseg->board = board;
return ResOK;
}
/* amcPinnedInterior -- block is pinned by any nail */
static Bool amcPinnedInterior(AMC amc, Nailboard board, Addr base, Addr limit)
{
Size headerSize = MustBeA(AbstractPool, amc)->format->headerSize;
return !NailboardIsResRange(board, AddrSub(base, headerSize),
AddrSub(limit, headerSize));
}
/* amcPinnedBase -- block is pinned only if base is nailed */
static Bool amcPinnedBase(AMC amc, Nailboard board, Addr base, Addr limit)
{
UNUSED(amc);
UNUSED(limit);
return NailboardGet(board, base);
}
/* amcVarargs -- decode obsolete varargs */
static void AMCVarargs(ArgStruct args[MPS_ARGS_MAX], va_list varargs)
{
args[0].key = MPS_KEY_FORMAT;
args[0].val.format = va_arg(varargs, Format);
args[1].key = MPS_KEY_CHAIN;
args[1].val.chain = va_arg(varargs, Chain);
args[2].key = MPS_KEY_ARGS_END;
AVERT(ArgList, args);
}
/* amcInitComm -- initialize AMC/Z pool
*
* <design/poolamc#.init>.
* Shared by AMCInit and AMCZinit.
*/
static Res amcInitComm(Pool pool, Arena arena, PoolClass klass,
RankSet rankSet, ArgList args)
{
AMC amc;
Res res;
Index i;
size_t genArraySize;
size_t genCount;
Bool interior = AMC_INTERIOR_DEFAULT;
Chain chain;
Size extendBy = AMC_EXTEND_BY_DEFAULT;
Size largeSize = AMC_LARGE_SIZE_DEFAULT;
ArgStruct arg;
AVER(pool != NULL);
AVERT(Arena, arena);
AVERT(ArgList, args);
AVERT(PoolClass, klass);
AVER(IsSubclass(klass, AMCZPool));
if (ArgPick(&arg, args, MPS_KEY_CHAIN))
chain = arg.val.chain;
else
chain = ArenaGlobals(arena)->defaultChain;
if (ArgPick(&arg, args, MPS_KEY_INTERIOR))
interior = arg.val.b;
if (ArgPick(&arg, args, MPS_KEY_EXTEND_BY))
extendBy = arg.val.size;
if (ArgPick(&arg, args, MPS_KEY_LARGE_SIZE))
largeSize = arg.val.size;
AVERT(Chain, chain);
AVER(chain->arena == arena);
AVER(extendBy > 0);
AVER(largeSize > 0);
/* TODO: it would be nice to be able to manage large objects that
* are smaller than the extendBy, but currently this results in
* unacceptable fragmentation due to the padding objects. This
* assertion catches this bad case. */
AVER(largeSize >= extendBy);
res = NextMethod(Pool, AMCZPool, init)(pool, arena, klass, args);
if (res != ResOK)
goto failNextInit;
amc = CouldBeA(AMCZPool, pool);
/* Ensure a format was supplied in the argument list. */
AVER(pool->format != NULL);
pool->alignment = pool->format->alignment;
pool->alignShift = SizeLog2(pool->alignment);
amc->rankSet = rankSet;
RingInit(&amc->genRing);
/* amc gets checked before the generations get created, but they */
/* do get created later in this function. */
amc->gen = NULL;
amc->nursery = NULL;
amc->rampGen = NULL;
amc->afterRampGen = NULL;
amc->gensBooted = FALSE;
amc->rampCount = 0;
amc->rampMode = RampOUTSIDE;
if (interior) {
amc->pinned = amcPinnedInterior;
} else {
amc->pinned = amcPinnedBase;
}
/* .extend-by.aligned: extendBy is aligned to the arena alignment. */
amc->extendBy = SizeArenaGrains(extendBy, arena);
amc->largeSize = largeSize;
SetClassOfPoly(pool, klass);
amc->sig = AMCSig;
AVERC(AMCZPool, amc);
/* Init generations. */
genCount = ChainGens(chain);
{
void *p;
/* One gen for each one in the chain plus dynamic gen. */
genArraySize = sizeof(amcGen) * (genCount + 1);
res = ControlAlloc(&p, arena, genArraySize);
if(res != ResOK)
goto failGensAlloc;
amc->gen = p;
for (i = 0; i <= genCount; ++i) {
res = amcGenCreate(&amc->gen[i], amc, ChainGen(chain, i));
if (res != ResOK)
goto failGenAlloc;
}
/* Set up forwarding buffers. */
for(i = 0; i < genCount; ++i) {
amcBufSetGen(amc->gen[i]->forward, amc->gen[i+1]);
}
/* Dynamic gen forwards to itself. */
amcBufSetGen(amc->gen[genCount]->forward, amc->gen[genCount]);
}
amc->nursery = amc->gen[0];
amc->rampGen = amc->gen[genCount-1]; /* last ephemeral gen */
amc->afterRampGen = amc->gen[genCount];
amc->gensBooted = TRUE;
AVERT(AMC, amc);
if(rankSet == RankSetEMPTY)
EVENT2(PoolInitAMCZ, pool, pool->format);
else
EVENT2(PoolInitAMC, pool, pool->format);
return ResOK;
failGenAlloc:
while(i > 0) {
--i;
amcGenDestroy(amc->gen[i]);
}
ControlFree(arena, amc->gen, genArraySize);
failGensAlloc:
NextMethod(Inst, AMCZPool, finish)(MustBeA(Inst, pool));
failNextInit:
AVER(res != ResOK);
return res;
}
/* TODO: AMCInit should call AMCZInit (its superclass) then
specialize, but amcInitComm creates forwarding buffers that copy
the rank set from the pool, making this awkward. */
static Res AMCInit(Pool pool, Arena arena, PoolClass klass, ArgList args)
{
UNUSED(klass); /* used for debug pools only */
return amcInitComm(pool, arena, CLASS(AMCPool), RankSetSingle(RankEXACT), args);
}
static Res AMCZInit(Pool pool, Arena arena, PoolClass klass, ArgList args)
{
UNUSED(klass); /* used for debug pools only */
return amcInitComm(pool, arena, CLASS(AMCZPool), RankSetEMPTY, args);
}
/* AMCFinish -- finish AMC pool
*
* <design/poolamc#.finish>.
*/
static void AMCFinish(Inst inst)
{
Pool pool = MustBeA(AbstractPool, inst);
AMC amc = MustBeA(AMCZPool, pool);
Ring ring;
Ring node, nextNode;
/* @@@@ Make sure that segments aren't buffered by forwarding */
/* buffers. This is a hack which allows the pool to be destroyed */
/* while it is collecting. Note that there aren't any mutator */
/* buffers by this time. */
RING_FOR(node, &amc->genRing, nextNode) {
amcGen gen = RING_ELT(amcGen, amcRing, node);
BufferDetach(gen->forward, pool);
}
ring = PoolSegRing(pool);
RING_FOR(node, ring, nextNode) {
Seg seg = SegOfPoolRing(node);
amcGen gen = amcSegGen(seg);
amcSeg amcseg = MustBeA(amcSeg, seg);
AVERT(amcSeg, amcseg);
AVER(!amcseg->accountedAsBuffered);
PoolGenFree(&gen->pgen, seg,
0,
amcseg->old ? SegSize(seg) : 0,
amcseg->old ? 0 : SegSize(seg),
amcseg->deferred);
}
/* Disassociate forwarding buffers from gens before they are */
/* destroyed. */
ring = &amc->genRing;
RING_FOR(node, ring, nextNode) {
amcGen gen = RING_ELT(amcGen, amcRing, node);
amcBufSetGen(gen->forward, NULL);
}
RING_FOR(node, ring, nextNode) {
amcGen gen = RING_ELT(amcGen, amcRing, node);
amcGenDestroy(gen);
}
amc->sig = SigInvalid;
NextMethod(Inst, AMCZPool, finish)(inst);
}
/* AMCBufferFill -- refill an allocation buffer
*
* <design/poolamc#.fill>.
*/
static Res AMCBufferFill(Addr *baseReturn, Addr *limitReturn,
Pool pool, Buffer buffer, Size size)
{
Seg seg;
AMC amc = MustBeA(AMCZPool, pool);
Res res;
Addr base, limit;
Arena arena;
Size grainsSize;
amcGen gen;
PoolGen pgen;
amcBuf amcbuf = MustBeA(amcBuf, buffer);
AVER(baseReturn != NULL);
AVER(limitReturn != NULL);
AVERT(Buffer, buffer);
AVER(BufferIsReset(buffer));
AVER(size > 0);
AVER(SizeIsAligned(size, PoolAlignment(pool)));
arena = PoolArena(pool);
gen = amcBufGen(buffer);
AVERT(amcGen, gen);
pgen = &gen->pgen;
/* Create and attach segment. The location of this segment is */
/* expressed via the pool generation. We rely on the arena to */
/* organize locations appropriately. */
if (size < amc->extendBy) {
grainsSize = amc->extendBy; /* .extend-by.aligned */
} else {
grainsSize = SizeArenaGrains(size, arena);
}
MPS_ARGS_BEGIN(args) {
MPS_ARGS_ADD_FIELD(args, amcKeySegGen, p, gen);
res = PoolGenAlloc(&seg, pgen, CLASS(amcSeg), grainsSize, args);
} MPS_ARGS_END(args);
if(res != ResOK)
return res;
AVER(grainsSize == SegSize(seg));
/* <design/seg#.field.rankSet.start> */
if(BufferRankSet(buffer) == RankSetEMPTY)
SegSetRankAndSummary(seg, BufferRankSet(buffer), RefSetEMPTY);
else
SegSetRankAndSummary(seg, BufferRankSet(buffer), RefSetUNIV);
/* If ramping, or if the buffer is intended for allocating hash
* table arrays, defer the size accounting. */
if ((amc->rampMode == RampRAMPING
&& buffer == amc->rampGen->forward
&& gen == amc->rampGen)
|| amcbuf->forHashArrays)
{
MustBeA(amcSeg, seg)->deferred = TRUE;
}
base = SegBase(seg);
if (size < amc->largeSize) {
/* Small or Medium segment: give the buffer the entire seg. */
limit = AddrAdd(base, grainsSize);
AVER(limit == SegLimit(seg));
} else {
/* Large segment: ONLY give the buffer the size requested, and */
/* pad the remainder of the segment: see job001811. */
Size padSize;
limit = AddrAdd(base, size);
AVER(limit <= SegLimit(seg));
padSize = grainsSize - size;
AVER(SizeIsAligned(padSize, PoolAlignment(pool)));
AVER(AddrAdd(limit, padSize) == SegLimit(seg));
if(padSize > 0) {
ShieldExpose(arena, seg);
(*pool->format->pad)(limit, padSize);
ShieldCover(arena, seg);
}
}
PoolGenAccountForFill(pgen, SegSize(seg));
MustBeA(amcSeg, seg)->accountedAsBuffered = TRUE;
*baseReturn = base;
*limitReturn = limit;
return ResOK;
}
/* amcSegBufferEmpty -- free from buffer to segment
*
* <design/poolamc#.flush>.
*/
static void amcSegBufferEmpty(Seg seg, Buffer buffer)
{
amcSeg amcseg = MustBeA(amcSeg, seg);
Pool pool = SegPool(seg);
Arena arena = PoolArena(pool);
AMC amc = MustBeA(AMCZPool, pool);
Addr base, init, limit;
TraceId ti;
Trace trace;
AVERT(Seg, seg);
AVERT(Buffer, buffer);
base = BufferBase(buffer);
init = BufferGetInit(buffer);
limit = BufferLimit(buffer);
AVER(SegBase(seg) <= base);
AVER(base <= init);
AVER(init <= limit);
if(SegSize(seg) < amc->largeSize) {
/* Small or Medium segment: buffer had the entire seg. */
AVER(limit == SegLimit(seg));
} else {
/* Large segment: buffer had only the size requested; job001811. */
AVER(limit <= SegLimit(seg));
}
/* <design/poolamc#.flush.pad> */
if (init < limit) {
ShieldExpose(arena, seg);
(*pool->format->pad)(init, AddrOffset(init, limit));
ShieldCover(arena, seg);
}
/* Any allocation in the buffer (including the padding object just
* created) is white, so needs to be accounted as condemned for all
* traces for which this segment is white. */
TRACE_SET_ITER(ti, trace, seg->white, arena)
GenDescCondemned(amcseg->gen->pgen.gen, trace,
AddrOffset(base, limit));
TRACE_SET_ITER_END(ti, trace, seg->white, arena);
if (amcseg->accountedAsBuffered) {
/* Account the entire buffer (including the padding object) as used. */
PoolGenAccountForEmpty(&amcseg->gen->pgen, SegSize(seg), 0,
amcseg->deferred);
amcseg->accountedAsBuffered = FALSE;
}
}
/* AMCRampBegin -- note an entry into a ramp pattern */
static void AMCRampBegin(Pool pool, Buffer buf, Bool collectAll)
{
AMC amc = MustBeA(AMCZPool, pool);
AVERT(Buffer, buf);
AVERT(Bool, collectAll);
UNUSED(collectAll); /* obsolete */
AVER(amc->rampCount < UINT_MAX);
++amc->rampCount;
if(amc->rampCount == 1) {
if(amc->rampMode != RampFINISH)
amc->rampMode = RampBEGIN;
}
}
/* AMCRampEnd -- note an exit from a ramp pattern */
static void AMCRampEnd(Pool pool, Buffer buf)
{
AMC amc = MustBeA(AMCZPool, pool);
AVERT(Buffer, buf);
AVER(amc->rampCount > 0);
--amc->rampCount;
if(amc->rampCount == 0) {
PoolGen pgen = &amc->rampGen->pgen;
Ring node, nextNode;
switch(amc->rampMode) {
case RampRAMPING:
/* We were ramping, so clean up. */
amc->rampMode = RampFINISH;
break;
case RampBEGIN:
/* short-circuit for short ramps */
amc->rampMode = RampOUTSIDE;
break;
case RampCOLLECTING:
/* we have finished a circuit of the state machine */
amc->rampMode = RampOUTSIDE;
break;
case RampFINISH:
/* stay in FINISH because we need to pass through COLLECTING */
break;
default:
/* can't get here if already OUTSIDE */
NOTREACHED;
}
/* Now all the segments in the ramp generation contribute to the
* pool generation's sizes. */
RING_FOR(node, PoolSegRing(pool), nextNode) {
Seg seg = SegOfPoolRing(node);
amcSeg amcseg = MustBeA(amcSeg, seg);
if(amcSegGen(seg) == amc->rampGen
&& amcseg->deferred
&& SegWhite(seg) == TraceSetEMPTY)
{
if (!amcseg->accountedAsBuffered)
PoolGenUndefer(pgen,
amcseg->old ? SegSize(seg) : 0,
amcseg->old ? 0 : SegSize(seg));
amcseg->deferred = FALSE;
}
}
}
}
/* amcSegPoolGen -- get pool generation for a segment */
static PoolGen amcSegPoolGen(Pool pool, Seg seg)
{
amcSeg amcseg = MustBeA(amcSeg, seg);
AVERT(Pool, pool);
AVER(pool == SegPool(seg));
return &amcseg->gen->pgen;
}
/* amcSegWhiten -- condemn the segment for the trace
*
* If the segment has a mutator buffer on it, we nail the buffer,
* because we can't scan or reclaim uncommitted buffers.
*/
static Res amcSegWhiten(Seg seg, Trace trace)
{
Size condemned = 0;
amcGen gen;
Buffer buffer;
amcSeg amcseg = MustBeA(amcSeg, seg);
Pool pool = SegPool(seg);
AMC amc = MustBeA(AMCZPool, pool);
Res res;
AVERT(Trace, trace);
if (SegBuffer(&buffer, seg)) {
AVERT(Buffer, buffer);
if(!BufferIsMutator(buffer)) { /* forwarding buffer */
AVER(BufferIsReady(buffer));
BufferDetach(buffer, pool);
} else { /* mutator buffer */
if(BufferScanLimit(buffer) == SegBase(seg)) {
/* There's nothing but the buffer, don't condemn. */
return ResOK;
}
/* [The following else-if section is just a comment added in */
/* 1998-10-08. It has never worked. RHSK 2007-01-16] */
/* else if (BufferScanLimit(buffer) == BufferLimit(buffer)) { */
/* The buffer is full, so it won't be used by the mutator. */
/* @@@@ We should detach it, but can't for technical */
/* reasons. */
/* BufferDetach(buffer, pool); */
/* } */
else {
Addr bufferScanLimit = BufferScanLimit(buffer);
/* There is an active buffer, make sure it's nailed. */
if(!amcSegHasNailboard(seg)) {
if(SegNailed(seg) == TraceSetEMPTY) {
res = amcSegCreateNailboard(seg);
if(res != ResOK) {
/* Can't create nailboard, don't condemn. */
return ResOK;
}
if (bufferScanLimit != BufferLimit(buffer)) {
NailboardSetRange(amcSegNailboard(seg),
bufferScanLimit,
BufferLimit(buffer));
}
STATISTIC(++trace->nailCount);
SegSetNailed(seg, TraceSetSingle(trace));
} else {
/* Segment is nailed already, cannot create a nailboard */
/* (see .nail.new), just give up condemning. */
return ResOK;
}
} else {
/* We have a nailboard, the buffer must be nailed already. */
AVER(bufferScanLimit == BufferLimit(buffer)
|| NailboardIsSetRange(amcSegNailboard(seg),
bufferScanLimit,
BufferLimit(buffer)));
/* Nail it for this trace as well. */
SegSetNailed(seg, TraceSetAdd(SegNailed(seg), trace));
}
/* Move the buffer's base up to the scan limit, so that we can
* detect allocation that happens during the trace, and
* account for it correctly in amcSegBufferEmpty and
* amcSegReclaimNailed. */
buffer->base = bufferScanLimit;
/* We didn't condemn the buffer, subtract it from the count. */
/* Relies on unsigned arithmetic wrapping round */
/* on under- and overflow (which it does). */
condemned -= AddrOffset(BufferBase(buffer), BufferLimit(buffer));
}
}
}
gen = amcSegGen(seg);
AVERT(amcGen, gen);
if (!amcseg->old) {
amcseg->old = TRUE;
if (amcseg->accountedAsBuffered) {
/* Note that the segment remains buffered but the buffer contents
* are accounted as old. See .seg.accounted-as-buffered. */
amcseg->accountedAsBuffered = FALSE;
PoolGenAccountForAge(&gen->pgen, SegSize(seg), 0, amcseg->deferred);
} else
PoolGenAccountForAge(&gen->pgen, 0, SegSize(seg), amcseg->deferred);
}
amcseg->forwarded[trace->ti] = 0;
SegSetWhite(seg, TraceSetAdd(SegWhite(seg), trace));
GenDescCondemned(gen->pgen.gen, trace, condemned + SegSize(seg));
/* Ensure we are forwarding into the right generation. */
/* see <design/poolamc#.gen.ramp> */
/* This switching needs to be more complex for multiple traces. */
AVER(TraceSetIsSingle(PoolArena(pool)->busyTraces));
if(amc->rampMode == RampBEGIN && gen == amc->rampGen) {
BufferDetach(gen->forward, pool);
amcBufSetGen(gen->forward, gen);
amc->rampMode = RampRAMPING;
} else if(amc->rampMode == RampFINISH && gen == amc->rampGen) {
BufferDetach(gen->forward, pool);
amcBufSetGen(gen->forward, amc->afterRampGen);
amc->rampMode = RampCOLLECTING;
}
return ResOK;
}
/* amcSegScanNailedRange -- make one scanning pass over a range of
* addresses in a nailed segment.
*
* *totalReturn is set to FALSE if not all the objects between base and
* limit have been scanned. It is not touched otherwise.
*/
static Res amcSegScanNailedRange(Bool *totalReturn, Bool *moreReturn,
ScanState ss, AMC amc, Nailboard board,
Addr base, Addr limit)
{
Format format;
Size headerSize;
Addr p, clientLimit;
Pool pool = MustBeA(AbstractPool, amc);
format = pool->format;
headerSize = format->headerSize;
p = AddrAdd(base, headerSize);
clientLimit = AddrAdd(limit, headerSize);
while (p < clientLimit) {
Addr q;
q = (*format->skip)(p);
if ((*amc->pinned)(amc, board, p, q)) {
Res res = TraceScanFormat(ss, p, q);
if(res != ResOK) {
*totalReturn = FALSE;
*moreReturn = TRUE;
return res;
}
} else {
*totalReturn = FALSE;
}
AVER(p < q);
p = q;
}
AVER(p == clientLimit);
return ResOK;
}
/* amcSegScanNailedOnce -- make one scanning pass over a nailed segment
*
* *totalReturn is set to TRUE iff all objects in segment scanned.
* *moreReturn is set to FALSE only if there are no more objects
* on the segment that need scanning (which is normally the case).
* It is set to TRUE if scanning had to be abandoned early on, and
* also if during emergency fixing any new marks got added to the
* nailboard.
*/
static Res amcSegScanNailedOnce(Bool *totalReturn, Bool *moreReturn,
ScanState ss, Seg seg, AMC amc)
{
Addr p, limit;
Nailboard board;
Res res;
Buffer buffer;
*totalReturn = TRUE;
board = amcSegNailboard(seg);
NailboardClearNewNails(board);
p = SegBase(seg);
while (SegBuffer(&buffer, seg)) {
limit = BufferScanLimit(buffer);
if(p >= limit) {
AVER(p == limit);
goto returnGood;
}
res = amcSegScanNailedRange(totalReturn, moreReturn,
ss, amc, board, p, limit);
if (res != ResOK)
return res;
p = limit;
}
limit = SegLimit(seg);
/* @@@@ Shouldn't p be set to BufferLimit here?! */
res = amcSegScanNailedRange(totalReturn, moreReturn,
ss, amc, board, p, limit);
if (res != ResOK)
return res;
returnGood:
*moreReturn = NailboardNewNails(board);
return ResOK;
}
/* amcSegScanNailed -- scan a nailed segment */
static Res amcSegScanNailed(Bool *totalReturn, ScanState ss, Pool pool,
Seg seg, AMC amc)
{
Bool total, moreScanning;
size_t loops = 0;
do {
Res res;
res = amcSegScanNailedOnce(&total, &moreScanning, ss, seg, amc);
if(res != ResOK) {
*totalReturn = FALSE;
return res;
}
loops += 1;
} while(moreScanning);
if(loops > 1) {
RefSet refset;
AVER(ArenaEmergency(PoolArena(pool)));
/* Looped: fixed refs (from 1st pass) were seen by MPS_FIX1
* (in later passes), so the "ss.unfixedSummary" is _not_
* purely unfixed. In this one case, unfixedSummary is not
* accurate, and cannot be used to verify the SegSummary (see
* impl/trace/#verify.segsummary). Use ScanStateSetSummary to
* store ScanStateSummary in ss.fixedSummary and reset
* ss.unfixedSummary. See job001548.
*/
refset = ScanStateSummary(ss);
/* A rare event, which might prompt a rare defect to appear. */
EVENT6(AMCScanNailed, loops, SegSummary(seg), ScanStateWhite(ss),
ScanStateUnfixedSummary(ss), ss->fixedSummary, refset);
ScanStateSetSummary(ss, refset);
}
*totalReturn = total;
return ResOK;
}
/* amcSegScan -- scan a single seg, turning it black
*
* <design/poolamc#.seg-scan>.
*/
static Res amcSegScan(Bool *totalReturn, Seg seg, ScanState ss)
{
Addr base, limit;
Format format;
Pool pool;
AMC amc;
Res res;
Buffer buffer;
AVER(totalReturn != NULL);
AVERT(Seg, seg);
AVERT(ScanState, ss);
pool = SegPool(seg);
amc = MustBeA(AMCZPool, pool);
format = pool->format;
if(amcSegHasNailboard(seg)) {
return amcSegScanNailed(totalReturn, ss, pool, seg, amc);
}
base = AddrAdd(SegBase(seg), format->headerSize);
/* <design/poolamc#.seg-scan.loop> */
while (SegBuffer(&buffer, seg)) {
limit = AddrAdd(BufferScanLimit(buffer),
format->headerSize);
if(base >= limit) {
/* @@@@ Are we sure we don't need scan the rest of the */
/* segment? */
AVER(base == limit);
*totalReturn = TRUE;
return ResOK;
}
res = TraceScanFormat(ss, base, limit);
if(res != ResOK) {
*totalReturn = FALSE;
return res;
}
base = limit;
}
/* <design/poolamc#.seg-scan.finish> @@@@ base? */
limit = AddrAdd(SegLimit(seg), format->headerSize);
AVER(SegBase(seg) <= base);
AVER(base <= AddrAdd(SegLimit(seg), format->headerSize));
if(base < limit) {
res = TraceScanFormat(ss, base, limit);
if(res != ResOK) {
*totalReturn = FALSE;
return res;
}
}
*totalReturn = TRUE;
return ResOK;
}
/* amcSegFixInPlace -- fix a reference without moving the object
*
* Usually this function is used for ambiguous references, but during
* emergency tracing may be used for references of any rank.
*
* If the segment has a nailboard then we use that to record the fix.
* Otherwise we simply grey and nail the entire segment.
*/
static void amcSegFixInPlace(Seg seg, ScanState ss, Ref *refIO)
{
Addr ref;
ref = (Addr)*refIO;
/* An ambiguous reference can point before the header. */
AVER(SegBase(seg) <= ref);
/* .ref-limit: A reference passed to Fix can't be beyond the */
/* segment, because then TraceFix would not have picked this */
/* segment. */
AVER(ref < SegLimit(seg));
if(amcSegHasNailboard(seg)) {
Bool wasMarked = NailboardSet(amcSegNailboard(seg), ref);
/* If there are no new marks (i.e., no new traces for which we */
/* are marking, and no new mark bits set) then we can return */
/* immediately, without changing colour. */
if(TraceSetSub(ss->traces, SegNailed(seg)) && wasMarked)
return;
} else if(TraceSetSub(ss->traces, SegNailed(seg))) {
return;
}
SegSetNailed(seg, TraceSetUnion(SegNailed(seg), ss->traces));
/* AMCZ segments don't contain references and so don't need to */
/* become grey */
if(SegRankSet(seg) != RankSetEMPTY)
SegSetGrey(seg, TraceSetUnion(SegGrey(seg), ss->traces));
}
/* amcSegFixEmergency -- fix a reference, without allocating
*
* <design/poolamc#.emergency.fix>.
*/
static Res amcSegFixEmergency(Seg seg, ScanState ss, Ref *refIO)
{
Arena arena;
Addr newRef;
Pool pool;
AVERT(Seg, seg);
AVERT(ScanState, ss);
AVER(refIO != NULL);
pool = SegPool(seg);
arena = PoolArena(pool);
if(ss->rank == RankAMBIG)
goto fixInPlace;
ShieldExpose(arena, seg);
newRef = (*pool->format->isMoved)(*refIO);
ShieldCover(arena, seg);
if(newRef != (Addr)0) {
/* Object has been forwarded already, so snap-out pointer. */
/* TODO: Implement weak pointer semantics in emergency fixing. This
would be a good idea since we really want to reclaim as much as
possible in an emergency. */
*refIO = newRef;
return ResOK;
}
fixInPlace: /* see <design/poolamc>.Nailboard.emergency */
amcSegFixInPlace(seg, ss, refIO);
return ResOK;
}
/* amcSegFix -- fix a reference to the segment
*
* <design/poolamc#.fix>.
*/
static Res amcSegFix(Seg seg, ScanState ss, Ref *refIO)
{
Arena arena;
Pool pool;
AMC amc;
Res res;
Format format; /* cache of pool->format */
Size headerSize; /* cache of pool->format->headerSize */
Ref ref; /* reference to be fixed */
Addr base; /* base address of reference */
Ref newRef; /* new location, if moved */
Addr newBase; /* base address of new copy */
Size length; /* length of object to be relocated */
Buffer buffer; /* buffer to allocate new copy into */
amcGen gen; /* generation of old copy of object */
TraceSet grey; /* greyness of object being relocated */
Seg toSeg; /* segment to which object is being relocated */
TraceId ti;
Trace trace;
/* <design/trace#.fix.noaver> */
AVERT_CRITICAL(ScanState, ss);
AVERT_CRITICAL(Seg, seg);
AVER_CRITICAL(refIO != NULL);
/* If the reference is ambiguous, set up the datastructures for */
/* managing a nailed segment. This involves marking the segment */
/* as nailed, and setting up a per-word mark table */
if(ss->rank == RankAMBIG) {
/* .nail.new: Check to see whether we need a Nailboard for */
/* this seg. We use "SegNailed(seg) == TraceSetEMPTY" */
/* rather than "!amcSegHasNailboard(seg)" because this avoids */
/* setting up a new nailboard when the segment was nailed, but */
/* had no nailboard. This must be avoided because otherwise */
/* assumptions in amcSegFixEmergency will be wrong (essentially */
/* we will lose some pointer fixes because we introduced a */
/* nailboard). */
if(SegNailed(seg) == TraceSetEMPTY) {
res = amcSegCreateNailboard(seg);
if(res != ResOK)
return res;
STATISTIC(++ss->nailCount);
SegSetNailed(seg, TraceSetUnion(SegNailed(seg), ss->traces));
}
amcSegFixInPlace(seg, ss, refIO);
return ResOK;
}
pool = SegPool(seg);
amc = MustBeA_CRITICAL(AMCZPool, pool);
AVERT_CRITICAL(AMC, amc);
format = pool->format;
headerSize = format->headerSize;
ref = *refIO;
AVER_CRITICAL(AddrAdd(SegBase(seg), headerSize) <= ref);
base = AddrSub(ref, headerSize);
AVER_CRITICAL(AddrIsAligned(base, PoolAlignment(pool)));
AVER_CRITICAL(ref < SegLimit(seg)); /* see .ref-limit */
arena = pool->arena;
/* .exposed.seg: Statements tagged ".exposed.seg" below require */
/* that "seg" (that is: the 'from' seg) has been ShieldExposed. */
ShieldExpose(arena, seg);
newRef = (*format->isMoved)(ref); /* .exposed.seg */
if(newRef == (Addr)0) {
Addr clientQ;
clientQ = (*format->skip)(ref);
/* If object is nailed already then we mustn't copy it: */
if (SegNailed(seg) != TraceSetEMPTY
&& !(amcSegHasNailboard(seg)
&& !(*amc->pinned)(amc, amcSegNailboard(seg), ref, clientQ)))
{
/* Segment only needs greying if there are new traces for */
/* which we are nailing. */
if(!TraceSetSub(ss->traces, SegNailed(seg))) {
if(SegRankSet(seg) != RankSetEMPTY) /* not for AMCZ */
SegSetGrey(seg, TraceSetUnion(SegGrey(seg), ss->traces));
SegSetNailed(seg, TraceSetUnion(SegNailed(seg), ss->traces));
}
res = ResOK;
goto returnRes;
} else if(ss->rank == RankWEAK) {
/* Object is not preserved (neither moved, nor nailed) */
/* hence, reference should be splatted. */
goto updateReference;
}
/* Object is not preserved yet (neither moved, nor nailed) */
/* so should be preserved by forwarding. */
ss->wasMarked = FALSE; /* <design/fix#.was-marked.not> */
/* Get the forwarding buffer from the object's generation. */
gen = amcSegGen(seg);
buffer = gen->forward;
AVER_CRITICAL(buffer != NULL);
length = AddrOffset(ref, clientQ); /* .exposed.seg */
STATISTIC(++ss->forwardedCount);
do {
res = BUFFER_RESERVE(&newBase, buffer, length);
if (res != ResOK)
goto returnRes;
newRef = AddrAdd(newBase, headerSize);
toSeg = BufferSeg(buffer);
ShieldExpose(arena, toSeg);
/* Since we're moving an object from one segment to another, */
/* union the greyness and the summaries together. */
grey = SegGrey(seg);
if(SegRankSet(seg) != RankSetEMPTY) { /* not for AMCZ */
grey = TraceSetUnion(grey, ss->traces);
SegSetSummary(toSeg, RefSetUnion(SegSummary(toSeg), SegSummary(seg)));
} else {
AVER_CRITICAL(SegRankSet(toSeg) == RankSetEMPTY);
}
SegSetGrey(toSeg, TraceSetUnion(SegGrey(toSeg), grey));
/* <design/trace#.fix.copy> */
(void)AddrCopy(newBase, base, length); /* .exposed.seg */
ShieldCover(arena, toSeg);
} while (!BUFFER_COMMIT(buffer, newBase, length));
STATISTIC(ss->copiedSize += length);
TRACE_SET_ITER(ti, trace, ss->traces, ss->arena)
MustBeA(amcSeg, seg)->forwarded[ti] += length;
TRACE_SET_ITER_END(ti, trace, ss->traces, ss->arena);
(*format->move)(ref, newRef); /* .exposed.seg */
} else {
/* reference to broken heart (which should be snapped out -- */
/* consider adding to (non-existent) snap-out cache here) */
STATISTIC(++ss->snapCount);
}
/* .fix.update: update the reference to whatever the above code */
/* decided it should be */
updateReference:
*refIO = newRef;
res = ResOK;
returnRes:
ShieldCover(arena, seg); /* .exposed.seg */
return res;
}
/* amcSegReclaimNailed -- reclaim what you can from a nailed segment */
static void amcSegReclaimNailed(Pool pool, Trace trace, Seg seg)
{
Addr p, limit;
Arena arena;
Format format;
STATISTIC_DECL(Size bytesReclaimed = (Size)0)
Count preservedInPlaceCount = (Count)0;
Size preservedInPlaceSize = (Size)0;
AMC amc = MustBeA(AMCZPool, pool);
PoolGen pgen;
Size headerSize;
Addr padBase; /* base of next padding object */
Size padLength; /* length of next padding object */
Buffer buffer;
/* All arguments AVERed by AMCReclaim */
format = pool->format;
arena = PoolArena(pool);
AVERT(Arena, arena);
/* see <design/poolamc#.nailboard.limitations> for improvements */
headerSize = format->headerSize;
ShieldExpose(arena, seg);
p = SegBase(seg);
limit = SegBufferScanLimit(seg);
padBase = p;
padLength = 0;
while(p < limit) {
Addr clientP, q, clientQ;
Size length;
Bool preserve;
clientP = AddrAdd(p, headerSize);
clientQ = (*format->skip)(clientP);
q = AddrSub(clientQ, headerSize);
length = AddrOffset(p, q);
if(amcSegHasNailboard(seg)) {
preserve = (*amc->pinned)(amc, amcSegNailboard(seg), clientP, clientQ);
} else {
/* There's no nailboard, so preserve everything that hasn't been
* forwarded. In this case, preservedInPlace* become somewhat
* overstated. */
preserve = !(*format->isMoved)(clientP);
}
if(preserve) {
++preservedInPlaceCount;
preservedInPlaceSize += length;
if (padLength > 0) {
/* Replace run of forwarding pointers and unreachable objects
* with a padding object. */
(*format->pad)(padBase, padLength);
STATISTIC(bytesReclaimed += padLength);
padLength = 0;
}
padBase = q;
} else {
padLength += length;
}
AVER(p < q);
p = q;
}
AVER(p == limit);
AVER(AddrAdd(padBase, padLength) == limit);
if (padLength > 0) {
/* Replace final run of forwarding pointers and unreachable
* objects with a padding object. */
(*format->pad)(padBase, padLength);
STATISTIC(bytesReclaimed += padLength);
}
ShieldCover(arena, seg);
SegSetNailed(seg, TraceSetDel(SegNailed(seg), trace));
SegSetWhite(seg, TraceSetDel(SegWhite(seg), trace));
if(SegNailed(seg) == TraceSetEMPTY && amcSegHasNailboard(seg)) {
NailboardDestroy(amcSegNailboard(seg), arena);
MustBeA(amcSeg, seg)->board = NULL;
}
STATISTIC(AVER(bytesReclaimed <= SegSize(seg)));
STATISTIC(trace->reclaimSize += bytesReclaimed);
STATISTIC(trace->preservedInPlaceCount += preservedInPlaceCount);
pgen = &amcSegGen(seg)->pgen;
if (SegBuffer(&buffer, seg)) {
/* Any allocation in the buffer was white, so needs to be
* accounted as condemned now. */
GenDescCondemned(pgen->gen, trace,
AddrOffset(BufferBase(buffer), BufferLimit(buffer)));
}
GenDescSurvived(pgen->gen, trace, MustBeA(amcSeg, seg)->forwarded[trace->ti],
preservedInPlaceSize);
/* Free the seg if we can; fixes .nailboard.limitations.middle. */
if(preservedInPlaceCount == 0
&& (!SegHasBuffer(seg))
&& (SegNailed(seg) == TraceSetEMPTY)) {
/* We may not free a buffered seg. */
AVER(!SegHasBuffer(seg));
PoolGenFree(pgen, seg, 0, SegSize(seg), 0, MustBeA(amcSeg, seg)->deferred);
}
}
/* amcSegReclaim -- recycle a segment if it is still white
*
* <design/poolamc#.reclaim>.
*/
static void amcSegReclaim(Seg seg, Trace trace)
{
amcSeg amcseg = MustBeA_CRITICAL(amcSeg, seg);
Pool pool = SegPool(seg);
AMC amc = MustBeA_CRITICAL(AMCZPool, pool);
amcGen gen;
AVERT_CRITICAL(Trace, trace);
gen = amcSegGen(seg);
AVERT_CRITICAL(amcGen, gen);
/* This switching needs to be more complex for multiple traces. */
AVER_CRITICAL(TraceSetIsSingle(PoolArena(pool)->busyTraces));
if(amc->rampMode == RampCOLLECTING) {
if(amc->rampCount > 0) {
/* Entered ramp mode before previous one was cleaned up */
amc->rampMode = RampBEGIN;
} else {
amc->rampMode = RampOUTSIDE;
}
}
if(SegNailed(seg) != TraceSetEMPTY) {
amcSegReclaimNailed(pool, trace, seg);
return;
}
/* We may not free a buffered seg. (But all buffered + condemned */
/* segs should have been nailed anyway). */
AVER(!SegHasBuffer(seg));
STATISTIC(trace->reclaimSize += SegSize(seg));
GenDescSurvived(gen->pgen.gen, trace, amcseg->forwarded[trace->ti], 0);
PoolGenFree(&gen->pgen, seg, 0, SegSize(seg), 0, amcseg->deferred);
}
/* amcSegWalk -- Apply function to (black) objects in segment */
static void amcSegWalk(Seg seg, Format format, FormattedObjectsVisitor f,
void *p, size_t s)
{
AVERT(Seg, seg);
AVERT(Format, format);
AVER(FUNCHECK(f));
/* p and s are arbitrary closures so can't be checked */
/* Avoid applying the function to grey or white objects. */
/* White objects might not be alive, and grey objects */
/* may have pointers to old-space. */
/* NB, segments containing a mix of colours (i.e., nailed segs) */
/* are not handled properly: No objects are walked. See */
/* job001682. */
if(SegWhite(seg) == TraceSetEMPTY && SegGrey(seg) == TraceSetEMPTY
&& SegNailed(seg) == TraceSetEMPTY)
{
Addr object, nextObject, limit;
Pool pool = SegPool(seg);
limit = AddrAdd(SegBufferScanLimit(seg), format->headerSize);
object = AddrAdd(SegBase(seg), format->headerSize);
while(object < limit) {
/* Check not a broken heart. */
AVER((*format->isMoved)(object) == NULL);
(*f)(object, format, pool, p, s);
nextObject = (*format->skip)(object);
AVER(nextObject > object);
object = nextObject;
}
AVER(object == limit);
}
}
/* amcWalkAll -- Apply a function to all (black) objects in a pool */
static void amcWalkAll(Pool pool, FormattedObjectsVisitor f, void *p, size_t s)
{
Arena arena;
Ring ring, next, node;
Format format = NULL;
Bool b;
AVER(IsA(AMCZPool, pool));
b = PoolFormat(&format, pool);
AVER(b);
arena = PoolArena(pool);
ring = PoolSegRing(pool);
node = RingNext(ring);
RING_FOR(node, ring, next) {
Seg seg = SegOfPoolRing(node);
ShieldExpose(arena, seg);
amcSegWalk(seg, format, f, p, s);
ShieldCover(arena, seg);
}
}
/* AMCAddrObject -- return base pointer from interior pointer
*
* amcAddrObjectSearch implements the scan for an object containing
* the interior pointer by skipping using format methods.
*
* AMCAddrObject locates the segment containing the interior pointer
* and wraps amcAddrObjectSearch in the necessary shield operations to
* give it access.
*/
static Res amcAddrObjectSearch(Addr *pReturn,
Pool pool,
Addr objBase,
Addr searchLimit,
Addr addr)
{
Format format;
Size hdrSize;
AVER(pReturn != NULL);
AVERT(Pool, pool);
AVER(objBase <= searchLimit);
format = pool->format;
hdrSize = format->headerSize;
while (objBase < searchLimit) {
Addr objRef = AddrAdd(objBase, hdrSize);
Addr objLimit = AddrSub((*format->skip)(objRef), hdrSize);
AVER(objBase < objLimit);
if (addr < objLimit) {
AVER(objBase <= addr);
AVER(addr < objLimit);
/* Don't return base pointer if object is moved */
if (NULL == (*format->isMoved)(objRef)) {
*pReturn = objRef;
return ResOK;
}
break;
}
objBase = objLimit;
}
return ResFAIL;
}
static Res AMCAddrObject(Addr *pReturn, Pool pool, Addr addr)
{
Res res;
Arena arena;
Addr base, limit;
Buffer buffer;
Seg seg;
AVER(pReturn != NULL);
AVERT(Pool, pool);
arena = PoolArena(pool);
if (!SegOfAddr(&seg, arena, addr) || SegPool(seg) != pool)
return ResFAIL;
base = SegBase(seg);
if (SegBuffer(&buffer, seg))
/* We use BufferGetInit here (and not BufferScanLimit) because we
* want to be able to find objects that have been allocated and
* committed since the last flip. These objects lie between the
* addresses returned by BufferScanLimit (which returns the value
* of init at the last flip) and BufferGetInit.
*
* Strictly speaking we only need a limit that is at least the
* maximum of the objects on the segments. This is because addr
* *must* point inside a live object and we stop skipping once we
* have found it. The init pointer serves this purpose.
*/
limit = BufferGetInit(buffer);
else
limit = SegLimit(seg);
ShieldExpose(arena, seg);
res = amcAddrObjectSearch(pReturn, pool, base, limit, addr);
ShieldCover(arena, seg);
return res;
}
/* AMCTotalSize -- total memory allocated from the arena */
static Size AMCTotalSize(Pool pool)
{
AMC amc = MustBeA(AMCZPool, pool);
Size size = 0;
Ring node, nextNode;
RING_FOR(node, &amc->genRing, nextNode) {
amcGen gen = RING_ELT(amcGen, amcRing, node);
AVERT(amcGen, gen);
size += gen->pgen.totalSize;
}
return size;
}
/* AMCFreeSize -- free memory (unused by client program) */
static Size AMCFreeSize(Pool pool)
{
AMC amc = MustBeA(AMCZPool, pool);
Size size = 0;
Ring node, nextNode;
RING_FOR(node, &amc->genRing, nextNode) {
amcGen gen = RING_ELT(amcGen, amcRing, node);
AVERT(amcGen, gen);
size += gen->pgen.freeSize;
}
return size;
}
/* AMCDescribe -- describe the contents of the AMC pool
*
* <design/poolamc#.describe>.
*/
static Res AMCDescribe(Inst inst, mps_lib_FILE *stream, Count depth)
{
Pool pool = CouldBeA(AbstractPool, inst);
AMC amc = CouldBeA(AMCZPool, pool);
Res res;
Ring node, nextNode;
const char *rampmode;
if (!TESTC(AMCZPool, amc))
return ResPARAM;
if (stream == NULL)
return ResPARAM;
res = NextMethod(Inst, AMCZPool, describe)(inst, stream, depth);
if (res != ResOK)
return res;
switch(amc->rampMode) {
#define RAMP_DESCRIBE(e, s) \
case e: \
rampmode = s; \
break;
RAMP_RELATION(RAMP_DESCRIBE)
#undef RAMP_DESCRIBE
default:
rampmode = "unknown ramp mode";
break;
}
res = WriteF(stream, depth + 2,
rampmode, " ($U)\n", (WriteFU)amc->rampCount,
NULL);
if(res != ResOK)
return res;
RING_FOR(node, &amc->genRing, nextNode) {
amcGen gen = RING_ELT(amcGen, amcRing, node);
res = amcGenDescribe(gen, stream, depth + 2);
if(res != ResOK)
return res;
}
if (0) {
/* SegDescribes */
RING_FOR(node, &pool->segRing, nextNode) {
Seg seg = RING_ELT(Seg, poolRing, node);
res = SegDescribe(seg, stream, depth + 2);
if(res != ResOK)
return res;
}
}
return ResOK;
}
/* AMCZPoolClass -- the class definition */
DEFINE_CLASS(Pool, AMCZPool, klass)
{
INHERIT_CLASS(klass, AMCZPool, AbstractCollectPool);
klass->instClassStruct.describe = AMCDescribe;
klass->instClassStruct.finish = AMCFinish;
klass->size = sizeof(AMCStruct);
klass->attr |= AttrMOVINGGC;
klass->varargs = AMCVarargs;
klass->init = AMCZInit;
klass->bufferFill = AMCBufferFill;
klass->rampBegin = AMCRampBegin;
klass->rampEnd = AMCRampEnd;
klass->segPoolGen = amcSegPoolGen;
klass->bufferClass = amcBufClassGet;
klass->totalSize = AMCTotalSize;
klass->freeSize = AMCFreeSize;
klass->addrObject = AMCAddrObject;
AVERT(PoolClass, klass);
}
/* AMCPoolClass -- the class definition */
DEFINE_CLASS(Pool, AMCPool, klass)
{
INHERIT_CLASS(klass, AMCPool, AMCZPool);
klass->init = AMCInit;
AVERT(PoolClass, klass);
}
/* mps_class_amc -- return the pool class descriptor to the client */
mps_pool_class_t mps_class_amc(void)
{
return (mps_pool_class_t)CLASS(AMCPool);
}
/* mps_class_amcz -- return the pool class descriptor to the client */
mps_pool_class_t mps_class_amcz(void)
{
return (mps_pool_class_t)CLASS(AMCZPool);
}
/* mps_amc_apply -- apply function to all objects in pool
*
* The iterator that is passed by the client is stored in a closure
* structure which is passed to a local iterator in order to ensure
* that any type conversion necessary between Addr and mps_addr_t
* happen. They are almost certainly the same on all platforms, but
* this is the correct way to do it.
*/
typedef struct mps_amc_apply_closure_s {
mps_amc_apply_stepper_t f;
void *p;
size_t s;
} mps_amc_apply_closure_s;
static void mps_amc_apply_iter(Addr addr, Format format, Pool pool,
void *p, size_t s)
{
mps_amc_apply_closure_s *closure = p;
/* Can't check addr */
AVERT(Format, format);
AVERT(Pool, pool);
/* We could check that s is the sizeof *p, but it would be slow */
UNUSED(format);
UNUSED(pool);
UNUSED(s);
(*closure->f)(addr, closure->p, closure->s);
}
void mps_amc_apply(mps_pool_t mps_pool,
mps_amc_apply_stepper_t f,
void *p, size_t s)
{
Pool pool = (Pool)mps_pool;
mps_amc_apply_closure_s closure_s;
Arena arena;
AVER(TESTT(Pool, pool));
arena = PoolArena(pool);
ArenaEnter(arena);
AVERT(Pool, pool);
closure_s.f = f;
closure_s.p = p;
closure_s.s = s;
amcWalkAll(pool, mps_amc_apply_iter, &closure_s, sizeof(closure_s));
ArenaLeave(arena);
}
/* AMCCheck -- check consistency of the AMC pool
*
* <design/poolamc#.check>.
*/
ATTRIBUTE_UNUSED
static Bool AMCCheck(AMC amc)
{
CHECKS(AMC, amc);
CHECKC(AMCZPool, amc);
CHECKD(Pool, MustBeA(AbstractPool, amc));
CHECKL(RankSetCheck(amc->rankSet));
CHECKD_NOSIG(Ring, &amc->genRing);
CHECKL(BoolCheck(amc->gensBooted));
if(amc->gensBooted) {
CHECKD(amcGen, amc->nursery);
CHECKL(amc->gen != NULL);
CHECKD(amcGen, amc->rampGen);
CHECKD(amcGen, amc->afterRampGen);
}
CHECKL(amc->rampMode >= RampOUTSIDE);
CHECKL(amc->rampMode <= RampCOLLECTING);
/* if OUTSIDE, count must be zero. */
CHECKL((amc->rampCount == 0) || (amc->rampMode != RampOUTSIDE));
/* if BEGIN or RAMPING, count must not be zero. */
CHECKL((amc->rampCount != 0) || ((amc->rampMode != RampBEGIN) &&
(amc->rampMode != RampRAMPING)));
return TRUE;
}
/* C. COPYRIGHT AND LICENSE
*
* Copyright (C) 2001-2020 Ravenbrook Limited <https://www.ravenbrook.com/>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
|