DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (fbecf6c8a86f)

VCS Links

Line Code
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
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is [Open Source Virtual Machine.].
 *
 * The Initial Developer of the Original Code is
 * Adobe System Incorporated.
 * Portions created by the Initial Developer are Copyright (C) 2004-2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Adobe AS3 Team
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "MMgc.h"

#ifdef AVMPLUS_SAMPLER
namespace avmplus
{
	void recordAllocationSample(const void* item, size_t size);
	void recordDeallocationSample(const void* item, size_t size);	
}
#endif

#if defined MMGC_POLICY_PROFILING && !defined AVMSHELL_BUILD
extern void RedirectLogOutput(void (*)(const char*));
static FILE* fp = NULL;

void logToFile(const char* s)
{
	fprintf(fp, "%s", s);
	fflush(fp);
}

static void startGCLogToFile()
{
	fp = fopen("gcbehavior.txt", "w");
	if (fp != NULL)
		RedirectLogOutput(logToFile);
}

static void endGCLogToFile()
{
	RedirectLogOutput(NULL);
	if (fp != NULL) {
		fclose(fp);
		fp = NULL;
	}
}
#endif // MMGC_POLICY_PROFILING && !AVMSHELL_BUILD

namespace MMgc
{
	GCHeap *GCHeap::instance = NULL;
	bool GCHeap::instanceEnterLockInitialized = false;
	vmpi_spin_lock_t GCHeap::instanceEnterLock;

	// GCHeap instance has the C++ runtime call dtor which causes problems
	AVMPLUS_ALIGN8(uint8_t) heapSpace[sizeof(GCHeap)];

	size_t GCHeap::leakedBytes;

#ifdef MMGC_MEMORY_PROFILER
	MemoryProfiler* GCHeap::profiler = (MemoryProfiler*)-1;
#endif

	GCHeapConfig::GCHeapConfig() : 
		initialSize(512), 
		heapLimit(kDefaultHeapLimit), 
		heapSoftLimit(0),
		OOMExitCode(0),
		useVirtualMemory(VMPI_useVirtualMemory()),
		trimVirtualMemory(true),
		mergeContiguousRegions(VMPI_canMergeContiguousRegions()),
		verbose(false),
		returnMemory(true),
		gcstats(false), // tracking
		autoGCStats(false), // auto printing
#ifdef AVMSHELL_BUILD
		gcbehavior(false),	// controlled by command line switch
#else
		gcbehavior(true),	// unconditional, if MMGC_POLICY_PROFILING is on
#endif
		eagerSweeping(false),
#ifdef MMGC_HEAP_GRAPH
		dumpFalsePositives(false),
#endif
		gcLoad(2.0),
		gcLoadCeiling(1.0),	// 1.0 is probably OK for desktop, maybe less so for mobile - more experiments needed
		gcEfficiency(0.25)
	{
#ifdef MMGC_64BIT
		trimVirtualMemory = false; // no need
#endif
		const char *envValue = VMPI_getenv("MMGC_HEAP_LIMIT");
		if(envValue)
			heapLimit = VMPI_strtol(envValue, 0, 10);
		envValue = VMPI_getenv("MMGC_HEAP_SOFT_LIMIT");
		if(envValue)
			heapSoftLimit = VMPI_strtol(envValue, 0, 10);
	}

	void GCHeap::Init(const GCHeapConfig& config)
	{
		GCAssert(instance == NULL);
		void *p = (void*)heapSpace;
		instance = new (p) GCHeap(config);
	}

	size_t GCHeap::Destroy()
	{
		EnterLock();	
		GCAssert(instance != NULL);
		instance->DestroyInstance();
		instance = NULL;
		EnterRelease();		
		return leakedBytes;
	}

	GCHeap::GCHeap(const GCHeapConfig& c)
		: kNativePageSize(VMPI_getVMPageSize()),
		  lastRegion(NULL),
		  freeRegion(NULL),
		  nextRegion(NULL),
		  blocks(NULL),
		  blocksLen(0),
		  numDecommitted(0),
		  numRegionBlocks(0),
		  numAlloc(0),
		  codeMemory(0),
		  externalPressure(0),
		  config(c),
 		  status(kMemNormal),
		  statusNotificationBeingSent(false),
		  enterCount(0),
	#ifdef MMGC_MEMORY_PROFILER
		  hasSpy(false),
	#endif
		  maxTotalHeapSize(0),
	#ifdef MMGC_POLICY_PROFILING
 		  maxPrivateMemory(0),
	#endif
    #ifdef MMGC_HOOKS
		  hooksEnabled(false),
    #endif
		  entryChecksEnabled(true),
		  abortStatusNotificationSent(false)
	{		
		VMPI_lockInit(&m_spinlock);
		VMPI_lockInit(&list_lock);
		VMPI_lockInit(&gclog_spinlock);

		// Initialize free lists
		HeapBlock *block = freelists;
		for (uint32_t i=0; i<kNumFreeLists; i++) {
			block->FreelistInit();
			block++;
		}
		
		// Create the initial heap
		{
			MMGC_LOCK(m_spinlock);
			ExpandHeap((int)config.initialSize);
		}

		fixedMalloc.InitInstance(this);

		instance = this;

#ifdef MMGC_MEMORY_PROFILER
		//create profiler if turned on and if it is not already created
		if(!IsProfilerInitialized())
		{
			InitProfiler();
		}

		if(profiler)
		{
			hooksEnabled = true; // set only after creating profiler
			hasSpy = VMPI_spySetup();
		}
#endif
		
#ifdef MMGC_MEMORY_INFO
		hooksEnabled = true; // always track allocs in DEBUG builds
#endif

#if defined MMGC_POLICY_PROFILING && !defined AVMSHELL_BUILD
		startGCLogToFile();
#endif
	}

	void GCHeap::DestroyInstance()
	{
#if defined MMGC_POLICY_PROFILING && !defined AVMSHELL_BUILD
		endGCLogToFile();
#endif

		gcManager.destroy();
		callbacks.Destroy();

		leakedBytes = GetFixedMalloc()->GetBytesInUse();
		fixedMalloc.DestroyInstance();
		GCAssertMsg(leakedBytes == 0 || GetStatus() == kMemAbort, "Leaks!");

		instance = NULL;

		size_t internalNum = AddrToBlock(blocks)->size + numRegionBlocks;		
		
		// numAlloc should just be the size of the HeapBlock's space
		if(numAlloc != internalNum && status != kMemAbort)
		{
			for (unsigned int i=0; i<blocksLen; i++) 
			{
				HeapBlock *block = &blocks[i];
				if(block->inUse() && block->baseAddr && block->baseAddr != (char*)blocks)
				{
#ifndef DEBUG
					if (config.verbose)
#endif
					{
						GCLog("Block 0x%x not freed\n", block->baseAddr);
					}
#if defined(MMGC_MEMORY_PROFILER) && defined(MMGC_MEMORY_INFO)
					if(block->allocTrace)
						PrintStackTrace(block->allocTrace);
#endif
				}
			}	
			GCAssert(false);
		}

#ifdef MMGC_MEMORY_PROFILER
		hooksEnabled = false;
		if(profiler)
			delete profiler;
		if(hasSpy)
			VMPI_spyTeardown();
		profiler = NULL;
#endif

		FreeAll();
		
		//  Acquire all the locks before destroying them to make absolutely sure we're the last consumers.
		VMPI_lockAcquire(&m_spinlock);
		VMPI_lockDestroy(&m_spinlock);

		VMPI_lockAcquire(&gclog_spinlock);
		VMPI_lockDestroy(&gclog_spinlock);
		
		VMPI_lockAcquire(&list_lock);
		VMPI_lockDestroy(&list_lock);
		
		if(enterFrame)
			enterFrame->Destroy();
	}

	void* GCHeap::Alloc(size_t size, uint32_t flags)
	{
		GCAssert(size > 0);

		char *baseAddr = 0;
		bool zero = (flags & kZero) != 0;
		// nested block to keep memset/memory commit out of critical section
		{
			MMGC_LOCK(m_spinlock);

			HeapBlock *block = AllocBlock(size, zero);

			if(!block) {
				if((flags & kExpand) == 0)
					return NULL;

				ExpandHeap(size, (flags & kCanFail) != 0);
				block = AllocBlock(size, zero);
				if(!block) {
					if (!(flags & kCanFail))
					{
						GCAssertMsg(0, "Internal error: ExpandHeap should have aborted if it can't satisfy the request");
						SignalInconsistentHeapState("Failed to abort");
						/*NOTREACHED*/
					}
					return NULL;
				}
			}

			GCAssert(block->size == size);
			
			numAlloc += size;
			
			// copy baseAddr to a stack variable to fix :
			// http://flashqa.macromedia.com/bugapp/detail.asp?ID=125938
			baseAddr = block->baseAddr;
			
#if defined(MMGC_MEMORY_PROFILER) && defined(MMGC_MEMORY_INFO)
			if(profiler)
				block->allocTrace = profiler->GetStackTrace();
#endif
			
#ifdef MMGC_MEMORY_PROFILER
			if((flags & kProfile) && HooksEnabled() && profiler) {
				profiler->RecordAllocation(baseAddr, size * kBlockSize, size * kBlockSize);
			}
#endif
			if((flags & kCanFail) == 0)
				CheckForSoftLimitExceeded(size);
		}

		// Zero out the memory, if requested to do so
		if (zero) {
			VMPI_memset(baseAddr, 0, size * kBlockSize);
		}

		// fail the allocation if we hit soft limit and canFail
		if(status == kMemSoftLimit && (flags & kCanFail) != 0) {
			FreeInternal(baseAddr, true);
			return NULL;
		}				   
		
		return baseAddr;
	}

	void GCHeap::FreeInternal(const void *item, bool profile)
	{
		(void)profile;
		MMGC_LOCK(m_spinlock);

		HeapBlock *block = AddrToBlock(item);
		GCAssertMsg(block != NULL, "Bogus item");

		// Update metrics
		GCAssert(numAlloc >= (unsigned int)block->size);
		numAlloc -= block->size;
		
#if defined(MMGC_MEMORY_PROFILER) && defined(MMGC_MEMORY_INFO)
		if(profiler)
			block->freeTrace = profiler->GetStackTrace();
#endif
	
#ifdef MMGC_MEMORY_PROFILER
		if(profile && HooksEnabled() && profiler) {
			profiler->RecordDeallocation(item, block->size * kBlockSize);
		}
#endif

		FreeBlock(block);
	}

	void GCHeap::Decommit()
	{
		// keep at least initialSize free 
		if(!config.returnMemory)
			return;
		
		size_t heapSize = GetTotalHeapSize();
		size_t freeSize = GetFreeHeapSize();
		
		size_t decommitSize;
		// commit if > kDecommitThresholdPercentage is free
		if(freeSize * 100 > heapSize * kDecommitThresholdPercentage)
			decommitSize = int((freeSize * 100 - heapSize * kDecommitThresholdPercentage) / 100);
		else
			return;
				
		//  Don't decommit more than our initial config size.
		if (heapSize - decommitSize < config.initialSize)
		{
			decommitSize = heapSize - config.initialSize;
		}

		// don't trifle
		if(decommitSize < (size_t)kMinHeapIncrement)
			return;

		MMGC_LOCK(m_spinlock);

	restart:

		// search from the end of the free list so we decommit big blocks
		HeapBlock *freelist = freelists+kNumFreeLists-1;

		HeapBlock *endOfBigFreelists = &freelists[GetFreeListIndex(1)];

		for (; freelist >= endOfBigFreelists && decommitSize > 0; freelist--)
		{
			HeapBlock *block = freelist;
			while ((block = block->prev) != freelist && decommitSize > 0)
			{
				// decommitting already decommitted blocks doesn't help
				// temporary replacement for commented out conditional below
				GCAssert(block->size != 0);
				if(!block->committed /*|| block->size == 0*/)
					continue;

				if(config.useVirtualMemory)
				{
					RemoveFromList(block);
					if((size_t)block->size > decommitSize)
					{
						HeapBlock *newBlock = Split(block, (int)decommitSize);
						AddToFreeList(newBlock);
					}
					
					Region *region = AddrToRegion(block->baseAddr);
					if(config.trimVirtualMemory &&
					   freeSize * 100 > heapSize * kReleaseThresholdPercentage &&
					   // if block is as big or bigger than a region, free the whole region
					   block->baseAddr <= region->baseAddr && 
					   region->reserveTop <= block->endAddr() )
	   				{

						if(block->baseAddr < region->baseAddr)
						{
							HeapBlock *newBlock = Split(block, int((region->baseAddr - block->baseAddr) / kBlockSize));
							AddToFreeList(block);
							block = newBlock;
						}
						if(block->endAddr() > region->reserveTop)
						{
							HeapBlock *newBlock = Split(block, int((region->reserveTop - block->baseAddr) / kBlockSize));
							AddToFreeList(newBlock);
						}

						decommitSize -= block->size;
						RemoveBlock(block);
						goto restart;
					}
					else if(VMPI_decommitMemory(block->baseAddr, block->size * kBlockSize))
					{
						block->committed = false;
						block->dirty = false;
						decommitSize -= block->size;
						if(config.verbose) {
							GCLog("decommitted %d page block from %p\n", block->size, block->baseAddr);
						}
					}
					else
					{
#ifdef MMGC_MAC
						// this can happen on mac where we release and re-reserve the memory and another thread may steal it from us
						RemovePartialBlock(block);
						goto restart;
#else
						// if the VM API's fail us bail
						VMPI_abort(); 
#endif
					}
					
					numDecommitted += block->size;
					
					// merge with previous/next if not in use and not committed
					HeapBlock *prev = block - block->sizePrevious;
					if(block->sizePrevious != 0 && !prev->committed && !prev->inUse()) {
						RemoveFromList(prev);
						
						prev->size += block->size;
						
						block->size = 0;
						block->sizePrevious = 0;
						block->baseAddr = 0;
						
						block = prev;
					}
					
					HeapBlock *next = block + block->size;
					if(next->size != 0 && !next->committed && !next->inUse()) {
						RemoveFromList(next);
						
						block->size += next->size;
						
						next->size = 0;
						next->sizePrevious = 0;
						next->baseAddr = 0;
					}
					
					next = block + block->size;
					next->sizePrevious = block->size;
					
					// add this block to the back of the bus to make sure we consume committed memory
					// first
					HeapBlock *backOfTheBus = &freelists[kNumFreeLists-1];
					HeapBlock *pointToInsert = backOfTheBus;
					while ((pointToInsert = pointToInsert->next) !=  backOfTheBus) {
						if (pointToInsert->size >= block->size && !pointToInsert->committed) {
							break;
						}
					}
					AddToFreeList(block, pointToInsert);
					
					// so we keep going through freelist properly
					block = freelist;

				} else { // not using virtual memory

					// if we aren't using mmap we can only do something if the block maps to a region
					// that is completely empty
					Region *region = AddrToRegion(block->baseAddr);
					if(block->baseAddr == region->baseAddr && // beginnings match
					   region->commitTop == block->baseAddr + block->size*kBlockSize) {
						
						RemoveFromList(block);
						
						RemoveBlock(block);
						
						goto restart;
					}
				}
			}
		}

		if(config.verbose)
			DumpHeapRep();
		CheckForStatusReturnToNormal();
	}

	// m_spinlock is held
	void GCHeap::CheckForHardLimitExceeded()
	{
		if (!HardLimitExceeded())
			return;
		
		// if we're already failing then fail hard
		if (status == kMemHardLimit)
			Abort();
			
		// bail on double faults
		if(statusNotificationBeingSent)
			Abort();
				
		// invoke callbacks to free up memory
		StatusChangeNotify(kMemHardLimit);
	}

	// m_spinlock is held
	void GCHeap::CheckForSoftLimitExceeded(size_t request)
	{
		if(config.heapSoftLimit == 0 || status != kMemNormal || !SoftLimitExceeded())
			return;

		size_t externalBlocks = externalPressure / kBlockSize;
		GCDebugMsg(false, "*** Alloc exceeded softlimit: ask for %u, usedheapsize =%u, totalHeap =%u, of which external =%u\n", 
				   unsigned(request), 
				   unsigned(GetUsedHeapSize() + externalBlocks), 
				   unsigned(GetTotalHeapSize() + externalBlocks),
				   unsigned(externalBlocks));
		StatusChangeNotify(kMemSoftLimit);
	}
	
	// m_spinlock is held
	void GCHeap::CheckForStatusReturnToNormal()
	{
		if(!statusNotificationBeingSent && statusNotNormalOrAbort())
		{
			size_t externalBlocks = externalPressure / kBlockSize;
			size_t total = GetTotalHeapSize() + externalBlocks;

			// return to normal if we drop below heapSoftLimit
			if(config.heapSoftLimit != 0 && status == kMemSoftLimit)
			{
				if (!SoftLimitExceeded())
				{
					size_t used = GetUsedHeapSize() + externalBlocks;
					GCDebugMsg(false, "### Alloc dropped below softlimit: usedheapsize =%u, totalHeap =%u, of which external =%u\n", 
							   unsigned(used), 
							   unsigned(total),
							   unsigned(externalBlocks) );
					StatusChangeNotify(kMemNormal);
				}
			}
			// or if we shrink to below %10 of the max
			else if ((maxTotalHeapSize / kBlockSize + externalBlocks) * 9 > total * 10)
				StatusChangeNotify(kMemNormal);
		}
	}

#ifdef MMGC_MAC

	void GCHeap::RemovePartialBlock(HeapBlock *block)
	{
		if(config.verbose) {
			GCLog("Removing block %p %d\n", block->baseAddr, block->size);
			DumpHeapRep();
		}

		{
			Region *region = AddrToRegion(block->baseAddr);
			if(region->baseAddr == block->baseAddr && region->reserveTop == block->endAddr()) {
				RemoveBlock(block);
				return;
			}
		}

		while(AddrToRegion(block->baseAddr) != AddrToRegion(block->endAddr()-1)) {
			// split block into parts mapping to regions
			Region *r = AddrToRegion(block->baseAddr);
			size_t numBlocks = (r->commitTop - block->baseAddr) / kBlockSize;
			char *next = Split(block, numBlocks)->baseAddr;
			// remove it
			RemovePartialBlock(block);
			block = AddrToBlock(next);
		}

		Region *region = AddrToRegion(block->baseAddr);
		// save these off since we'll potentially shift region
		char *regionBaseAddr = region->baseAddr;
		size_t regionBlockId = region->blockId;

		// if we don't line up with beginning or end we need a new region
		if(block->baseAddr != region->baseAddr && region->commitTop != block->endAddr()) {
			if(nextRegion == NULL) {
				// usually this is handled in ExpandHeap but could run out here
				bool zero = false;
				block = AllocBlock(1, zero);
				if(block) {
					nextRegion = (Region*)(void *)block->baseAddr;	
				} else {
					ExpandHeap(1, false);
					// just calling ExpandHeap should set nextRegion
					GCAssertMsg(nextRegion != NULL, "ExpandHeap didn't set nextRegion");
				}
			}

			NewRegion(block->endAddr(), region->reserveTop, 
					  region->commitTop > block->endAddr() ? region->commitTop : block->endAddr(),
					  region->blockId + (block->endAddr() - region->baseAddr) / kBlockSize);
			
			if(region->baseAddr != block->baseAddr) {
				// end this region at the start of block going away
				region->reserveTop = block->baseAddr;			
				if(region->commitTop > block->baseAddr)
					region->commitTop = block->baseAddr;
			}

		} else if(region->baseAddr == block->baseAddr) {
			region->blockId += block->size;
			region->baseAddr = block->endAddr();
		} else if(region->commitTop == block->endAddr()) {
			// end this region at the start of block going away
			region->reserveTop = block->baseAddr;
			if(region->commitTop > block->baseAddr)
				region->commitTop = block->baseAddr;			
		} else {
			GCAssertMsg(false, "This shouldn't be possible");
		}


		// create temporary region for this block
		Region temp(this, block->baseAddr, block->endAddr(), block->endAddr(), regionBlockId +  (block->baseAddr - regionBaseAddr) / kBlockSize);

		RemoveBlock(block);
		
		// pop temp from freelist, put there by RemoveBlock
		freeRegion = *(Region**)freeRegion;

		

#ifdef DEBUG
		// doing this here is an extra validation step
		if(config.verbose) 
		{
			DumpHeapRep();
		}
#endif
	}

#endif

	void GCHeap::RemoveBlock(HeapBlock *block)
	{	
		Region *region = AddrToRegion(block->baseAddr);
		
		GCAssert(region->baseAddr == block->baseAddr);
		GCAssert(region->reserveTop == block->endAddr());
		
		size_t newBlocksLen = blocksLen - block->size;

		HeapBlock *nextBlock = block + block->size;

		bool need_sentinel = false;
		bool remove_sentinel = false;

		if( block->sizePrevious && nextBlock->size ) {
			// This block is contiguous with the blocks before and after it
			// so we need to add a sentinel
			need_sentinel = true;
		}
		else if ( !block->sizePrevious && !nextBlock->size ) {
			// the block is not contigous with the block before or after it - we need to remove a sentinel
			// since there would already be one on each side.
			remove_sentinel = true;
		}

		// update nextblock's sizePrevious
		nextBlock->sizePrevious = need_sentinel ? 0 : block->sizePrevious;
		
		// Add space for the sentinel - the remaining blocks won't be contiguous
		if(need_sentinel)
			++newBlocksLen;
		else if(remove_sentinel)
			--newBlocksLen;

		// just re-use blocks; small wastage possible
		HeapBlock *newBlocks = blocks;

		// the memmove will overwrite this so save it
		size_t blockSize = block->size;

		size_t offset = int(block-blocks);
		int32_t sen_offset = 0;
		HeapBlock *src = block + block->size;

		if( need_sentinel ) {
			offset = int(block-blocks)+1;
			sen_offset = 1;
			HeapBlock* sentinel = newBlocks + (block-blocks);
			sentinel->baseAddr = NULL;
			sentinel->size = 0;
			sentinel->sizePrevious = block->sizePrevious;
			sentinel->prev = NULL;
			sentinel->next = NULL;
#if defined(MMGC_MEMORY_PROFILER) && defined(MMGC_MEMORY_INFO)
			sentinel->allocTrace = 0;
#endif
		}
		else if( remove_sentinel ) {
			// skip trailing sentinel
			src++;
			sen_offset = -1;
		}
		
		// copy blocks after
		int lastChunkSize = int((blocks + blocksLen) - src);
		GCAssert(lastChunkSize + offset == newBlocksLen);
		memmove(newBlocks + offset, src, lastChunkSize * sizeof(HeapBlock));

		// Fix up the prev/next pointers of each freelist.  This is a little more complicated
		// than the similiar code in ExpandHeap because blocks after the one we are free'ing
		// are sliding down by block->size
		HeapBlock *fl = freelists;
		for (uint32_t i=0; i<kNumFreeLists; i++) {
			HeapBlock *temp = fl;
			do {
				if (temp->prev != fl) {
					if(temp->prev > block) {
						temp->prev = newBlocks + (temp->prev-blocks-blockSize) + sen_offset;
					}
				}
				if (temp->next != fl) {
					if(temp->next > block) {
						temp->next = newBlocks + (temp->next-blocks-blockSize) + sen_offset;
					}
				}
			} while ((temp = temp->next) != fl);
			fl++;
		}
		
		// Need to decrement blockId for regions in blocks after block
		Region *r = lastRegion;
		while(r) {
			if(r->blockId > region->blockId) {
				r->blockId -= (blockSize-sen_offset);
			}
			r = r->prev;
		}

		blocksLen = newBlocksLen;
		RemoveRegion(region);

		// make sure we did everything correctly
		CheckFreelist();
		ValidateHeapBlocks();
	}

	void GCHeap::ValidateHeapBlocks()
	{
#ifdef _DEBUG
		// iterate through HeapBlocks making sure:
		// non-contiguous regions have a sentinel
		HeapBlock *block = blocks;
		while(block - blocks < (intptr_t)blocksLen) {
			Region *r = AddrToRegion(block->baseAddr);
			if(r && r->baseAddr == block->baseAddr)
				GCAssert(r->blockId == (size_t)(block-blocks));

			HeapBlock *next = NULL;
			if(block->size) {
				next = block + block->size;
				GCAssert(next->sizePrevious == block->size);
			}
			HeapBlock *prev = NULL;
			if(block->sizePrevious) {
				prev = block - block->sizePrevious;
				GCAssert(prev->size == block->sizePrevious);
			} else if(block != blocks) {
				// I have no prev and I'm not the first, check sentinel
				HeapBlock *sentinel = block-1;
				GCAssert(sentinel->baseAddr == NULL);
				GCAssert(sentinel->size == 0);
				GCAssert(sentinel->sizePrevious != 0);
			}
			if(block->baseAddr) {
				if(prev)
					GCAssert(block->baseAddr == prev->baseAddr + (kBlockSize * prev->size));
				block = next;
				// we should always end on a sentinel
				GCAssert(next - blocks < (int)blocksLen);
			} else {
				// block is a sentinel
				GCAssert(block->size == 0);
				// FIXME: the following asserts are firing and we need to understand why, could be bugs
				// make sure last block ends at commitTop
				Region *prevRegion = AddrToRegion(prev->baseAddr + (prev->size*kBlockSize) - 1);
				GCAssert(prev->baseAddr + (prev->size*kBlockSize) == prevRegion->commitTop);
				block++;
				// either we've reached the end or the next isn't a sentinel
				GCAssert(block - blocks == (intptr_t)blocksLen || block->size != 0);
			}
		}
		GCAssert(block - blocks == (intptr_t)blocksLen);
#endif
	}

	GCHeap::Region *GCHeap::AddrToRegion(const void *item) const
	{
		// Linear search of regions list to find this address.
		// The regions list should usually be pretty short.
		for (Region *region = lastRegion;
			 region != NULL;
			 region = region->prev)
		{
			if (item >= region->baseAddr && item < region->reserveTop) {
				return region;
			}
		}
		return NULL;
	}

	GCHeap::HeapBlock* GCHeap::AddrToBlock(const void *item) const
	{
		Region *region = AddrToRegion(item);
		if(region) {
			size_t index = ((char*)item - region->baseAddr) / kBlockSize;
			HeapBlock *b = blocks + region->blockId + index;
			GCAssert(item >= b->baseAddr && item < b->baseAddr + b->size * GCHeap::kBlockSize);
			return b;
		}
		return NULL;
	}
	
	GCHeap::HeapBlock* GCHeap::AllocBlock(size_t size, bool& zero)
	{
		uint32_t startList = GetFreeListIndex(size);
		HeapBlock *freelist = &freelists[startList];

		HeapBlock *decommittedSuitableBlock = NULL;
		HeapBlock *blockToUse = NULL;

		for (uint32_t i = startList; i < kNumFreeLists; i++)
		{
			// Search for a big enough block in free list
			HeapBlock *block = freelist;
			while ((block = block->next) != freelist)
			{
				if (block->size >= size && block->committed) 
				{
					blockToUse = block;
					goto foundone;
				}

				if(config.useVirtualMemory)
				{
					// if the block isn't committed see if this request can be met with by committing
					// it and combining it with its neighbors
					if(!block->committed && !decommittedSuitableBlock)
					{
						size_t totalSize = block->size;
						
						// first try predecessors
						HeapBlock *firstFree = block;
						
						// loop because we could have interleaved committed/non-committed blocks
						while(totalSize < size && firstFree->sizePrevious != 0)
						{	
							HeapBlock *prevBlock = firstFree - firstFree->sizePrevious;
							if(!prevBlock->inUse() && prevBlock->size > 0) {
								totalSize += prevBlock->size;
								firstFree = prevBlock;
							} else {
								break;
							}
						}
						
						if(totalSize > size) {
							decommittedSuitableBlock = firstFree;
						} else {
							// now try successors
							HeapBlock *nextBlock = block + block->size;
							while(nextBlock->size > 0 && !nextBlock->inUse() && totalSize < size) {
								totalSize += nextBlock->size;
								nextBlock = nextBlock + nextBlock->size;
							}
							
							if(totalSize > size) {
								decommittedSuitableBlock = firstFree;
							}
						}
					}
				}
			}
			freelist++;
		}	
		
	foundone:

		if(blockToUse)
		{				
			RemoveFromList(blockToUse);

			if(blockToUse->size > size)
			{
				HeapBlock *newBlock = Split(blockToUse, size);
							
				// Add the newly created block to the free list
				AddToFreeList(newBlock);
			}
							
			CheckFreelist();
			
			zero = blockToUse->dirty && zero;
			
			#ifdef _DEBUG
			if (!blockToUse->dirty)
			{
				union {
					const char* base_c;
					const uint32_t* base_u;
				};
				base_c = blockToUse->baseAddr;
				GCAssert(*base_u == 0);
			}
			#endif
			return blockToUse;
		}

		if(config.useVirtualMemory && decommittedSuitableBlock)
		{
			// first handle case where its too big
			if(decommittedSuitableBlock->size > size)
			{				
				size_t toCommit = size > kMinHeapIncrement ? size : kMinHeapIncrement;

				if(toCommit > decommittedSuitableBlock->size)
					toCommit = decommittedSuitableBlock->size;

				RemoveFromList(decommittedSuitableBlock);
				
				// first split off part we're gonna commit
				if(decommittedSuitableBlock->size > toCommit) {
					HeapBlock *newBlock = Split(decommittedSuitableBlock, toCommit);

					// put the still uncommitted part back on freelist
					AddToFreeList(newBlock);
				}
				
				Commit(decommittedSuitableBlock);

				if(toCommit > size) {
					HeapBlock *newBlock = Split(decommittedSuitableBlock, size);
					AddToFreeList(newBlock);
				}
			}
			else // too small
			{
				// need to stitch blocks together committing uncommitted blocks
				HeapBlock *block = decommittedSuitableBlock;
				RemoveFromList(block);

				size_t amountRecommitted = block->committed ? 0 : block->size;
					
				while(block->size < size)
				{
					HeapBlock *nextBlock = block + block->size;

					RemoveFromList(nextBlock);
						
					// Increase size of current block
					block->size += nextBlock->size;
					amountRecommitted += nextBlock->committed ? 0 : nextBlock->size;

					nextBlock->size = 0;
					nextBlock->baseAddr = 0;
					nextBlock->sizePrevious = 0;

					block->dirty |= nextBlock->dirty;
				}

				GCAssert(amountRecommitted > 0);

				if(!VMPI_commitMemory(block->baseAddr, block->size * kBlockSize)) 
				{
					GCAssert(false);
				}
				if(config.verbose)
					GCLog("recommitted %d pages\n", amountRecommitted);
				numDecommitted -= amountRecommitted;
				block->committed = true;

				GCAssert(decommittedSuitableBlock->size >= size);

				// split last block
				if(block->size > size)
				{
					HeapBlock *newBlock = Split(block, size);
					AddToFreeList(newBlock);
				}
			}

			GCAssert(decommittedSuitableBlock->size == size);

			// update sizePrevious in next block
			HeapBlock *nextBlock = decommittedSuitableBlock + size;
			nextBlock->sizePrevious = size;

			CheckFreelist();

			return decommittedSuitableBlock;
		}
	
		CheckFreelist();
		return 0;
	}

	GCHeap::HeapBlock *GCHeap::Split(HeapBlock *block, size_t size)
	{
		GCAssert(block->size > size);
		HeapBlock *newBlock = block + size;
		newBlock->baseAddr = block->baseAddr + kBlockSize * size;

		newBlock->size = block->size - size;
		newBlock->sizePrevious = size;
		newBlock->committed = block->committed;
		newBlock->dirty = block->dirty;
		block->size = size;

		// Update sizePrevious in block after that
		HeapBlock *nextBlock = newBlock + newBlock->size;
		nextBlock->sizePrevious = newBlock->size;
	
		return newBlock;
	}

	void GCHeap::Commit(HeapBlock *block)
	{
		if(!block->committed)
		{
			if(!VMPI_commitMemory(block->baseAddr, block->size * kBlockSize)) 
			{
				GCAssert(false);
			}
			if(config.verbose) {
				GCLog("recommitted %d pages\n", block->size);
				DumpHeapRep();
			}
			numDecommitted -= block->size;
			block->committed = true;
			block->dirty = false;
		}
	}

#ifdef _DEBUG
	// Non-debug version in GCHeap.h
	void GCHeap::CheckFreelist()
	{
		HeapBlock *freelist = freelists;
		for (uint32_t i = 0; i < kNumFreeLists; i++)
		{
			HeapBlock *block = freelist;
			while((block = block->next) != freelist)
			{
				GCAssert(block != block->next);
				GCAssert(block != block->next->next || block->next == freelist);
				if(block->sizePrevious)
				{
					HeapBlock *prev = block - block->sizePrevious;
					GCAssert(block->sizePrevious == prev->size);
				}
			}
			freelist++;
		}
#if 0
// Debugging code to find problems with block/region layout
// This code is slow, but can be useful for tracking down issues
// It verifies that the memory for each block corresponds to one or more regions
// and that each region points to a valid starting block
  		Region* r = lastRegion;

		int block_idx = 0;
		bool errors =false;
		for(block_idx = 0; block_idx < blocksLen; ++block_idx){
			HeapBlock* b = blocks + block_idx;
			
			if( !b->size )
				continue;

			int contig_size = 0;
			r = lastRegion;

			while( r ){
				if(b->baseAddr >= r->baseAddr && b->baseAddr < r->reserveTop ) {
					// starts in this region
					char* end = b->baseAddr + b->size*kBlockSize;
					if(end > (r->reserveTop + contig_size) ){
						GCLog("error, block %d %p %d did not find a matching region\n", block_idx, b->baseAddr, b->size);
						GCLog("Started in region %p - %p, contig size: %d\n", r->baseAddr, r->reserveTop, contig_size);
						errors = true;
						break;
					}
				}
				else if( r->prev && r->prev->reserveTop==r->baseAddr){
					contig_size +=r->reserveTop - r->baseAddr;
				}
				else{
					contig_size = 0;
				}
						
				r = r->prev;
			}
		}

		while(r)
			{
				if(!blocks[r->blockId].size){
                    for( int i = r->blockId-1; i >= 0 ; --i )
                        if( blocks[i].size){
							//Look for spanning blocks
                            if( ((blocks[i].baseAddr + blocks[i].size*kBlockSize) <= r->baseAddr) ) {
                                GCLog("Invalid block id for region %p-%p %d\n", r->baseAddr, r->reserveTop, i);
								errors =true;
								break;
							}
                            else
                                break;
                        }
				}
				r = r->prev;
		   }
		if( errors ){
			r = lastRegion;
			while(r) {
				GCLog("%p - %p\n", r->baseAddr, r->reserveTop);
				r = r->prev;
			}
			for(int b = 0; b < blocksLen; ++b ){
				if(!blocks[b].size)
					continue;
				GCLog("%d %p %d\n", b, blocks[b].baseAddr, blocks[b].size); 
			}
			asm("int3");
		}
#endif	
	}
#endif // DEBUG

	bool GCHeap::BlocksAreContiguous(void *item1, void *item2)
	{
		Region *r1 = AddrToRegion(item1);
		Region *r2 = AddrToRegion(item2);
		return r1 == r2 || r1->reserveTop == r2->baseAddr;
	}

	void GCHeap::AddToFreeList(HeapBlock *block)
	{
		int index = GetFreeListIndex(block->size);
		HeapBlock *freelist = &freelists[index];

		HeapBlock *pointToInsert = freelist;
		
		// Note: We don't need to bother searching for the right
		// insertion point if we know all blocks on this free list
		// are the same size.
		if (block->size >= kUniqueThreshold) {
			while ((pointToInsert = pointToInsert->next) != freelist) {
				if (pointToInsert->size >= block->size) {
					break;
				}
			}
		}

		AddToFreeList(block, pointToInsert);
	}
		
	void GCHeap::AddToFreeList(HeapBlock *block, HeapBlock* pointToInsert)
	{
		CheckFreelist();

		block->next = pointToInsert;
		block->prev = pointToInsert->prev;
		block->prev->next = block;
		pointToInsert->prev = block;

		CheckFreelist();
	}						   

	void GCHeap::FreeBlock(HeapBlock *block)
	{
		GCAssert(block->inUse());
	
		// big block's that map to a region are free'd right away
		Region *r = AddrToRegion(block->baseAddr);
		if(block->baseAddr == r->baseAddr && block->endAddr() == r->reserveTop)
		{
			RemoveBlock(block);
			return;
		}
		
#ifdef _DEBUG
		// trash it. fb == free block
		VMPI_memset(block->baseAddr, 0xfb, block->size * kBlockSize);
#endif
		
		// Try to coalesce this block with its predecessor
		if(block->sizePrevious)
		{
			HeapBlock *prevBlock = block - block->sizePrevious;
			if (!prevBlock->inUse() && prevBlock->committed) 
			{
				// Remove predecessor block from free list
				RemoveFromList(prevBlock);
				
				// Increase size of predecessor block
				prevBlock->size += block->size;
				
				block->size = 0;
				block->sizePrevious = 0;
				block->baseAddr = 0;				
				
				block = prevBlock;
			}
		}

		// Try to coalesce this block with its successor
		HeapBlock *nextBlock = block + block->size;

		GCAssert(block->size != 0);

		if (!nextBlock->inUse() && nextBlock->committed) {
			// Remove successor block from free list
			RemoveFromList(nextBlock);

			// Increase size of current block
			block->size += nextBlock->size;
			nextBlock->size = 0;
			nextBlock->baseAddr = 0;
			nextBlock->sizePrevious = 0;
		}

		// Update sizePrevious in the next block
		nextBlock = block + block->size;
		nextBlock->sizePrevious = block->size;

		// Add this block to the right free list
		block->dirty = true;

		AddToFreeList(block);

		CheckFreelist();
	}

	void GCHeap::ExpandHeap(size_t askSize, bool canFail)
	{
		if (canFail) {
			// once we hit soft limit don't allow canFail allocs
			if(status == kMemSoftLimit)
				return;

			// random policy choice: don't invoke OOM callbacks for canFail allocs
			if (HardLimitExceeded())
				return;
		}
		
		// Check the hard limit, trigger cleanup if hit
		CheckForHardLimitExceeded();
		
		if (!ExpandHeapInternal(askSize))
			Abort();
		
		// The guard on instance being non-NULL is a hack, to be fixed later (now=2009-07-20).
		// Some VMPI layers (WinMo is at least one of them) try to grab the GCHeap instance to get
		// at the map of private pages.  But the GCHeap instance is not available during the initial
		// call to ExpandHeap.  So sidestep that problem here.

		if (instance != NULL) {
			// GetTotalHeapSize is probably fairly cheap; even so this strikes me
			// as a bit of a hack.
			size_t heapSizeNow = GetTotalHeapSize() * kBlockSize;
			if (heapSizeNow > maxTotalHeapSize) {
				maxTotalHeapSize = heapSizeNow;
#ifdef MMGC_POLICY_PROFILING
				maxPrivateMemory = VMPI_getPrivateResidentPageCount() * VMPI_getVMPageSize();
#endif
			}
		}
	} 

	bool GCHeap::HardLimitExceeded()
	{
		return GetTotalHeapSize() + externalPressure/kBlockSize > config.heapLimit;
	}
	
	bool GCHeap::SoftLimitExceeded()
	{
		return GetTotalHeapSize() + externalPressure/kBlockSize > config.heapSoftLimit;
	}
	
#define roundUp(_s, _inc) (((_s + _inc - 1) / _inc) * _inc)
	 
	bool GCHeap::ExpandHeapInternal(size_t askSize)
	{
		size_t size = askSize;

#ifdef _DEBUG
		// Turn this switch on to test bridging of contiguous
		// regions.
		bool test_bridging = false;
		size_t defaultReserve = test_bridging ? (size+kMinHeapIncrement) : kDefaultReserve;
#else
		const size_t defaultReserve = kDefaultReserve;
#endif

		if (HardLimitExceeded())
			return false;

		char *baseAddr = NULL;
		char *newRegionAddr = NULL;
		size_t newRegionSize = 0;
		bool contiguous = false;
		size_t commitAvail = 0;

		// Round up to the nearest kMinHeapIncrement
		size = roundUp(size, kMinHeapIncrement);

		// when we allocate a new region the space needed for the HeapBlocks, if it won't fit 
		// in existing space it must fit in new space so we may need to increase the new space

		HeapBlock *newBlocks = blocks;
		
		if(blocksLen != 0 || // first time through just take what we need out of initialSize instead of adjusting
		   config.initialSize == 0) // unless initializeSize is zero of course
		{
			int extraBlocks = 1; // for potential new sentinel
			if(nextRegion == NULL) {
				extraBlocks++; // may need a new page for regions
			}
			size_t curHeapBlocksSize = blocks ? AddrToBlock(blocks)->size : 0;
			size_t newHeapBlocksSize = numHeapBlocksToNumBlocks(blocksLen + size + extraBlocks);

			// size is based on newSize and vice versa, loop to settle (typically one loop, sometimes two)
			while(newHeapBlocksSize > curHeapBlocksSize) 
			{
				// use askSize so HeapBlock's can fit in rounding slop
				size = roundUp(askSize + newHeapBlocksSize + extraBlocks, kMinHeapIncrement);

				// tells us use new memory for blocks below
				newBlocks = NULL;

				// since newSize is based on size we have to repeat in case it changes
				curHeapBlocksSize = newHeapBlocksSize;
				newHeapBlocksSize = numHeapBlocksToNumBlocks(blocksLen + size + extraBlocks);
			}
		}		

		if(config.useVirtualMemory)
		{
			Region *region = lastRegion;
			if (region != NULL)
			{
				commitAvail = (int)((region->reserveTop - region->commitTop) / kBlockSize);
				
				// Can this request be satisfied purely by committing more memory that
				// is already reserved?
				if (size <= commitAvail) {
					if (VMPI_commitMemory(region->commitTop, size * kBlockSize))
					{
						// Succeeded!
						baseAddr = region->commitTop;

						// check for continuity, we can only be contiguous with the end since
						// we don't have a "block insert" facility
						HeapBlock *last = &blocks[blocksLen-1] - blocks[blocksLen-1].sizePrevious;
						contiguous = last->baseAddr + last->size * kBlockSize == baseAddr;
						
						// Update the commit top.
						region->commitTop += size*kBlockSize;
						
						// Go set up the block list.
						goto gotMemory;
					}
					else
					{
						// If we can't commit memory we've already reserved,
						// no other trick is going to work.  Fail.
						return false;
					}
				}
				
				// Try to reserve a region contiguous to the last region.
				
				// - Try for the "default reservation size" if it's larger than
				//   the requested block.
				if (defaultReserve > size) {
					newRegionAddr = (char*) VMPI_reserveMemoryRegion(region->reserveTop,
												  defaultReserve * kBlockSize);
					newRegionSize = defaultReserve;
				}
				
				// - If the default reservation size didn't work or isn't big
				//   enough, go for the exact amount requested, minus the
				//   committable space in the current region.
				if (newRegionAddr == NULL) {
					newRegionAddr = (char*) VMPI_reserveMemoryRegion(region->reserveTop,
												  (size - commitAvail)*kBlockSize);
					newRegionSize = size - commitAvail;
				}
				
				if (newRegionAddr != NULL) {
					// We were able to reserve some space.
					
					// Commit available space from the existing region.
					if (commitAvail != 0) {
						if (!VMPI_commitMemory(region->commitTop, commitAvail * kBlockSize))
						{
							// We couldn't commit even this space.  We're doomed.
							// Un-reserve the space we just reserved and fail.
							ReleaseMemory(newRegionAddr, newRegionSize);
							return false;
						}
					}
					
					// Commit needed space from the new region.
					if (!VMPI_commitMemory(newRegionAddr, (size - commitAvail) * kBlockSize))
					{
						// We couldn't commit this space.  We can't meet the
						// request.  Un-commit any memory we just committed,
						// un-reserve any memory we just reserved, and fail.
						if (commitAvail != 0) {
							VMPI_decommitMemory(region->commitTop,
										   commitAvail * kBlockSize);
						}
						ReleaseMemory(newRegionAddr,
									  (size-commitAvail)*kBlockSize);
						return false;
					}
					
					// We successfully reserved a new contiguous region
					// and committed the memory we need.  Finish up.
					baseAddr = region->commitTop;
					region->commitTop = lastRegion->reserveTop;

					// check for continuity, we can only be contiguous with the end since
					// we don't have a "block insert" facility
					HeapBlock *last = &blocks[blocksLen-1] - blocks[blocksLen-1].sizePrevious;
					contiguous = last->baseAddr + last->size * kBlockSize == baseAddr;
					
					goto gotMemory;
				}
			}

			// We were unable to allocate a contiguous region, or there
			// was no existing region to be contiguous to because this
			// is the first-ever expansion.  Allocate a non-contiguous region.
			
			// Don't use any of the available space in the current region.
			commitAvail = 0;
			
			// - Go for the default reservation size unless the requested
			//   size is bigger.
			if (size < defaultReserve) {
				newRegionAddr = (char*) VMPI_reserveMemoryRegion(NULL,
												  defaultReserve*kBlockSize);
				newRegionSize = defaultReserve;
			}
			
			// - If that failed or the requested size is bigger than default,
			//   go for the requested size exactly.
			if (newRegionAddr == NULL) {
				newRegionAddr = (char*) VMPI_reserveMemoryRegion(NULL,
											  size*kBlockSize);
				newRegionSize = size;
			}
			
			// - If that didn't work, give up.
			if (newRegionAddr == NULL) {
				return false;
			}
			
			// - Try to commit the memory.
			if (VMPI_commitMemory(newRegionAddr,
							 size*kBlockSize) == 0)
			{
				// Failed.  Un-reserve the memory and fail.
				ReleaseMemory(newRegionAddr, newRegionSize*kBlockSize);
				return false;
			}
			
			// If we got here, we've successfully allocated a
			// non-contiguous region.
			baseAddr = newRegionAddr;
			contiguous = false;

		}
		else
		{		
			// Allocate the requested amount of space as a new region.
			newRegionAddr = (char*)VMPI_allocateAlignedMemory(size * kBlockSize);
			baseAddr = newRegionAddr;
			newRegionSize = size;
			
			// If that didn't work, give up.
			if (newRegionAddr == NULL) {
				return false;
			}
		}

	gotMemory:

		// If we were able to allocate a contiguous block, remove
		// the old top sentinel.
		if (contiguous) {
			blocksLen--;
		}

		// Expand the block list.
		size_t newBlocksLen = blocksLen + size;

		// Add space for the "top" sentinel
		newBlocksLen++;

		if (!newBlocks) {
 			newBlocks = (HeapBlock*)(void *)baseAddr;
		}
		
		// Copy all the existing blocks.
		if (blocks && blocks != newBlocks) {
			VMPI_memcpy(newBlocks, blocks, blocksLen * sizeof(HeapBlock));

			// Fix up the prev/next pointers of each freelist.
			HeapBlock *freelist = freelists;
			for (uint32_t i=0; i<kNumFreeLists; i++) {
				HeapBlock *temp = freelist;
				do {
					if (temp->prev != freelist) {
						temp->prev = newBlocks + (temp->prev-blocks);
					}
					if (temp->next != freelist) {
						temp->next = newBlocks + (temp->next-blocks);
					}
				} while ((temp = temp->next) != freelist);
				freelist++;
			}
			CheckFreelist();
		}

		// Create a single free block for the new space,
		// and add it to the free list.
		HeapBlock *block = newBlocks+blocksLen;
		block->baseAddr = baseAddr;
		block->size = size;
		block->sizePrevious = 0;

		// link up contiguous blocks
		if(blocksLen && contiguous)
		{
			// search backwards for first real block
			HeapBlock *b = &blocks[blocksLen-1];
			while(b->size == 0) 
			{
				b--;
				GCAssert(b >= blocks);
			}
			block->sizePrevious = b->size;
			GCAssert((block - block->sizePrevious)->size == b->size);
		}
		block->prev = NULL;
		block->next = NULL;
		block->committed = true;
		block->dirty = config.useVirtualMemory ? VMPI_areNewPagesDirty() : true;

#if defined(MMGC_MEMORY_PROFILER) && defined(MMGC_MEMORY_INFO)
		block->allocTrace = 0;
		block->freeTrace = 0;
#endif

		// if baseAddr was used for HeapBlocks split
		if((char*)newBlocks == baseAddr)
		{
			size_t numBlocksNeededForHeapBlocks = numHeapBlocksToNumBlocks(newBlocksLen);
			HeapBlock *next = Split(block, numBlocksNeededForHeapBlocks);
			// this space counts as used space
			numAlloc += numBlocksNeededForHeapBlocks;
			block = next;
		}

		// get space for region allocations
		if(nextRegion == NULL) {
			nextRegion = (Region*)(void *)block->baseAddr;
			HeapBlock *next = Split(block, 1);
			// this space counts as used space
			numAlloc++;
			numRegionBlocks++;
			block = next;
		}

		AddToFreeList(block);

		// Initialize the rest of the new blocks to empty.
		size_t freeBlockSize = block->size;

		for (uint32_t i=1; i < freeBlockSize; i++) {
			block++;
			block->baseAddr = NULL;
			block->size = 0;
			block->sizePrevious = 0;
			block->prev = NULL;
			block->next = NULL;
			block->committed = false;
			block->dirty = false;
#if defined(MMGC_MEMORY_PROFILER) && defined(MMGC_MEMORY_INFO)
			block->allocTrace = 0;
			block->freeTrace = 0;
#endif
		}

		// Fill in the sentinel for the top of the heap.
		block++;
		block->baseAddr     = NULL;
		block->size         = 0;
		block->sizePrevious = freeBlockSize;
		block->prev         = NULL;
		block->next         = NULL;
		block->committed    = false;
		block->dirty        = false;
#if defined(MMGC_MEMORY_PROFILER) && defined(MMGC_MEMORY_INFO)
		block->allocTrace = 0;
		block->freeTrace = 0;
#endif

		// save for free'ing
		void *oldBlocks = blocks;

		blocks = newBlocks;
		blocksLen = newBlocksLen;

		// free old blocks space using new blocks (FreeBlock poisons blocks so can't use old blocks)
		if (oldBlocks && oldBlocks != newBlocks) {
			HeapBlock *oldBlocksHB = AddrToBlock(oldBlocks);
			numAlloc -= oldBlocksHB->size;
			FreeBlock(oldBlocksHB);
		}

		// If we created a new region, save the base address so we can free later.		
		if (newRegionAddr) {
			/*	The mergeContiguousRegions bit is broken, since we
				loop over all regions we may be contiguous with an
				existing older HeapBlock and we don't support inserting a
				new address range arbritrarily into the HeapBlock
				array (contiguous regions must be contiguous heap
				blocks vis-a-vie the region block id) 
			if(contiguous &&
				config.mergeContiguousRegions) {
				lastRegion->reserveTop += newRegionSize*kBlockSize;
				lastRegion->commitTop +=
				(size-commitAvail)*kBlockSize;
				} else
			*/ {
				Region *newRegion = NewRegion(newRegionAddr,  // baseAddr
											  newRegionAddr+newRegionSize*kBlockSize, // reserve top
											  newRegionAddr+(size-commitAvail)*kBlockSize, // commit top
											  newBlocksLen-(size-commitAvail)-1); // block id
				
				if(config.verbose)
					GCLog("reserved new region, %p - %p %s\n",
						  newRegion->baseAddr,
						  newRegion->reserveTop,
						  contiguous ? "contiguous" : "non-contiguous");
			}
		}

		CheckFreelist();
		
		if(config.verbose) {
			GCLog("heap expanded by %d pages\n", size);
			DumpHeapRep();
		}
		ValidateHeapBlocks();
			
		// Success!
		return true;
	}

	void GCHeap::RemoveRegion(Region *region)
	{
		Region **next = &lastRegion;
		while(*next != region) 
			next = &((*next)->prev);
		*next = region->prev;
		ReleaseMemory(region->baseAddr,
					  region->reserveTop-region->baseAddr);		
		if(config.verbose) {
			GCLog("unreserved region 0x%p - 0x%p (commitTop: %p)\n", region->baseAddr, region->reserveTop, region->commitTop);
			DumpHeapRep();
		}
		FreeRegion(region);
	}

	void GCHeap::FreeAll()
	{
		// Release all of the heap regions
		while (lastRegion != NULL) {
			Region *region = lastRegion;
			lastRegion = lastRegion->prev;
			ReleaseMemory(region->baseAddr,
						  region->reserveTop-region->baseAddr);
		}
	}
	
#ifdef MMGC_HOOKS
	void GCHeap::AllocHook(const void *item, size_t askSize, size_t gotSize)
	{
		(void)item;
		(void)askSize;
		(void)gotSize;
		{
#ifdef MMGC_MEMORY_PROFILER
			MMGC_LOCK(m_spinlock);
			if(hasSpy) {
				VMPI_spyCallback();
			}
			if(profiler)
				profiler->RecordAllocation(item, askSize, gotSize);
#endif

		}
#ifdef MMGC_MEMORY_INFO
		DebugDecorate(item, gotSize);
#endif
#ifdef AVMPLUS_SAMPLER
		// this can't be called with the heap lock locked.
		avmplus::recordAllocationSample(item, gotSize);
#endif
	}

	void GCHeap::FinalizeHook(const void *item, size_t size)
	{
		(void)item,(void)size;
		{
#ifdef MMGC_MEMORY_PROFILER
			MMGC_LOCK(m_spinlock);
			if(profiler)
				profiler->RecordDeallocation(item, size);
#endif
		}
		
#ifdef AVMPLUS_SAMPLER
		avmplus::recordDeallocationSample(item, size);
#endif
	}

	void GCHeap::FreeHook(const void *item, size_t size, int poison)
	{
		(void)poison,(void)item,(void)size;
#ifdef MMGC_MEMORY_INFO
		DebugFree(item, poison, size);
#endif
	}
#endif // MMGC_HOOKS

	EnterFrame::EnterFrame() : m_heap(NULL), m_gc(NULL), m_collectingGC(NULL)
	{
		GCHeap *heap = GCHeap::GetGCHeap();
		if(heap->GetStackEntryAddress() == NULL) {
			m_heap = heap;
			heap->Enter(this);
		}
	}
	
	// this is the first thing we run after the Abort longjmp
	EnterFrame::~EnterFrame()
	{
		if(m_heap) {
			GCHeap *heap = m_heap;
			// this prevents us from doing multiple jumps in case leave results in more allocations
			m_heap = NULL;
			heap->Leave();
		}
	}
	
#ifdef MMGC_USE_SYSTEM_MALLOC
	void GCHeap::SystemOOMEvent(size_t size, int attempt)
	{
		(void)size;
		if (attempt == 0 && !statusNotificationBeingSent)
			StatusChangeNotify(kMemHardLimit);
		else
			Abort();
	}
#endif

	/*static*/
	void GCHeap::SignalObjectTooLarge()
	{
		GCLog("Implementation limit exceeded: attempting to allocate too-large object\n");
		GetGCHeap()->Abort();
	}

	/*static*/
	void GCHeap::SignalInconsistentHeapState(const char* reason)
	{
		GCAssert(!"Inconsistent heap state; aborting");
		GCLog("Inconsistent heap state: %s\n", reason);
		GetGCHeap()->Abort();
	}
	
	/*static*/
	void GCHeap::SignalExternalAllocation(size_t nbytes)
	{
		GCHeap* heap = GetGCHeap();
		
		MMGC_LOCK(heap->m_spinlock);
		
		heap->externalPressure += nbytes;

		// cleanup actions if necessary
		heap->CheckForHardLimitExceeded();
			
		// check again - if we still can't allocate then fail hard
		if (heap->HardLimitExceeded())
			heap->Abort();

		heap->CheckForSoftLimitExceeded((nbytes + kBlockSize - 1) / kBlockSize);	// size only used for GC messages, OK to round up
	}
	
	/*static*/
	void GCHeap::SignalExternalDeallocation(size_t nbytes)
	{
		GCHeap* heap = GetGCHeap();
		
		MMGC_LOCK(heap->m_spinlock);
		
		heap->externalPressure -= nbytes;
		heap->CheckForStatusReturnToNormal();
	}

	// This can *always* be called.  It will clean up the state on the current thread
	// if appropriate, otherwise do nothing.  It *must* be called by host code if the
	// host code jumps past an MMGC_ENTER instance.  (The Flash player does that, in
	// some circumstances.)
	
	/*static*/
	void GCHeap::SignalImminentAbort()
	{
		if (instance == NULL)
			return;
		EnterFrame* ef = GetGCHeap()->GetEnterFrame();
		if (ef == NULL)
			return;

		// We don't know if we're holding the lock but we can release it anyhow,
		// on the assumption that this operation will not cause problems if the
		// lock is not held or is held by another thread.
		//
		// Release lock so we don't deadlock if exit or longjmp end up coming
		// back to GCHeap (all callers must have this lock).

		VMPI_lockRelease(&instance->m_spinlock);
		
		// If the current thread is holding a lock for a GC that's not currently active on the thread
		// then break the lock: the current thread is collecting in that GC, but the Abort has cancelled
		// the collection.

		if (ef->m_collectingGC)
		{
			ef->m_collectingGC->SignalImminentAbort();
			ef->m_collectingGC = NULL;
		}

		if(ef->m_gc)
			ef->m_gc->SignalImminentAbort();
		
		// Clear the enterFrame because we're jumping past MMGC_ENTER.
		GetGCHeap()->enterFrame = NULL;
	}
	
	void GCHeap::Abort()
	{
		status = kMemAbort;
		EnterFrame *ef = enterFrame;
		GCLog("error: out of memory\n");
		
		// release lock so we don't deadlock if exit or longjmp end up coming
		// back to GCHeap (all callers must have this lock)
		VMPI_lockRelease(&m_spinlock);
		
		// Lock must not be held when we call VMPI_exit, deadlocks ensue on Linux
		if(config.OOMExitCode != 0) 
		{
			VMPI_exit(config.OOMExitCode);
		}

		if (ef != NULL)
		{
			// If the current thread is holding a lock for a GC that's not currently active on the thread
			// then break the lock: the current thread is collecting in that GC, but the Abort has cancelled
			// the collection.
			if (ef->m_collectingGC)
			{
				ef->m_collectingGC->SignalImminentAbort();
				ef->m_collectingGC = NULL;
			}
			if(ef->m_gc)
			{
				ef->m_gc->SignalImminentAbort();
			}
			// Guard against repeated jumps: ef->m_heap doubles as a flag.  We go Abort->longjmp->~EnterFrame->Leave
			// and Leave calls StatusChangeNotify and the host code might do another allocation during shutdown
			// in which case we want to go to VMPI_abort instead.  At that point m_heap will be NULL and the right
			// thing happens.
			if (ef->m_heap != NULL)
			{
				VMPI_longjmpNoUnwind(ef->jmpbuf, 1);
			}
		}
		GCAssertMsg(false, "MMGC_ENTER missing or we allocated more memory trying to shutdown");
		VMPI_abort();
	}
	
	void GCHeap::Enter(EnterFrame *frame)
	{
		enterCount++;
		enterFrame = frame;
	}

	void GCHeap::Leave()
	{
		GCHeap *heapToDestroy=NULL;
		{
			MMGC_LOCK(m_spinlock);

			if(status == kMemAbort && !abortStatusNotificationSent) {
				abortStatusNotificationSent = true;
				StatusChangeNotify(kMemAbort);
			}
		}
	
		EnterLock();				
		// do this after StatusChangeNotify it affects ShouldNotEnter
		enterFrame = NULL;

		enterCount--;

		if(status == kMemAbort && enterCount == 0 && abortStatusNotificationSent) {
			// last one out of the pool pulls the plug
			heapToDestroy = instance;
			instance = NULL;
		}
		if(heapToDestroy != NULL) {
			// any thread can call this, just need to make sure all other
			// threads are done, hence the ref counting
			heapToDestroy->DestroyInstance();
		}
		EnterRelease();				
	}
	void GCHeap::log_percentage(const char *name, size_t bytes, size_t bytes_compare)
	{
		bytes_compare = size_t((bytes*100.0)/bytes_compare);
		if(bytes > 1<<20) {
			GCLog("%s %u (%.1fM) %u%%\n", name, (unsigned int)(bytes / GCHeap::kBlockSize), bytes * 1.0 / (1024*1024), (unsigned int)(bytes_compare));
		} else {
			GCLog("%s %u (%uK) %u%%\n", name, (unsigned int)(bytes / GCHeap::kBlockSize), (unsigned int)(bytes / 1024), (unsigned int)(bytes_compare));
		}
	}
	
	void GCHeap::DumpMemoryInfo()
	{
		MMGC_LOCK(m_spinlock);
		DumpMemoryInfoLocked();
	}

	void GCHeap::DumpMemoryInfoLocked()
	{
		size_t priv = VMPI_getPrivateResidentPageCount() * VMPI_getVMPageSize();
		size_t mmgc = GetTotalHeapSize() * GCHeap::kBlockSize;
		size_t unmanaged = GetFixedMalloc()->GetTotalSize() * GCHeap::kBlockSize;
		size_t fixed_alloced;
		size_t fixed_asksize;
		GetFixedMalloc()->GetUsageInfo(fixed_asksize, fixed_alloced);

		size_t gc_total=0;
		size_t gc_allocated_total =0;
		size_t gc_ask_total = 0;
		size_t gc_count = 0;
		BasicListIterator<GC*> iter(gcManager.gcs());
		GC* gc;
		while((gc = iter.next()) != NULL)
		{
#ifdef MMGC_MEMORY_PROFILER
			GCLog("[mem] GC 0x%p:%s\n", (void*)gc, GetAllocationName(gc));
#else
			GCLog("[mem] GC 0x%p\n", (void*)gc);
#endif
			gc->DumpMemoryInfo();
			
			size_t ask;
			size_t allocated;
			gc->GetUsageInfo(ask, allocated);
			gc_ask_total += ask;
			gc_allocated_total += allocated;
			gc_count += 1;
			
			gc_total += gc->GetNumBlocks() * kBlockSize;
		}

#ifdef MMGC_MEMORY_PROFILER
		fixedMalloc.DumpMemoryInfo();
#endif

		// Gross stats are not meaningful if the profiler is running, see bugzilla 490014.
		// Disabling their printing is just an expedient fix to avoid misleading data being
		// printed.  There are other, more complicated, fixes we should adopt.

		GCLog("[mem] ------- gross stats -----\n");
#ifdef MMGC_MEMORY_PROFILER
		if (GCHeap::GetGCHeap()->GetProfiler() == NULL)
#endif
		{
			log_percentage("[mem] private", priv, priv);
			log_percentage("[mem]\t mmgc", mmgc, priv);
			log_percentage("[mem]\t\t unmanaged", unmanaged, priv);
			log_percentage("[mem]\t\t managed", gc_total, priv);
			log_percentage("[mem]\t\t free",  (size_t)GetFreeHeapSize() * GCHeap::kBlockSize, priv);
			log_percentage("[mem]\t other",  priv - mmgc, priv);
			log_percentage("[mem] \tunmanaged overhead ", unmanaged-fixed_alloced, unmanaged);
			log_percentage("[mem] \tmanaged overhead ", gc_total - gc_allocated_total, gc_total);
#ifdef MMGC_MEMORY_PROFILER
			if(HooksEnabled())
			{
				log_percentage("[mem] \tunmanaged internal wastage", fixed_alloced - fixed_asksize, fixed_alloced);
				log_percentage("[mem] \tmanaged internal wastage", gc_allocated_total - gc_ask_total, gc_allocated_total);
			}
#endif
			GCLog("[mem] number of collectors %u\n", unsigned(gc_count));
		}
#ifdef MMGC_MEMORY_PROFILER
		else
			GCLog("[mem] No gross stats available when profiler is enabled.\n");
#endif
		GCLog("[mem] -------- gross stats end -----\n");

#ifdef MMGC_MEMORY_PROFILER
		if(hasSpy)
			DumpFatties();
#endif

		if (config.verbose)
			DumpHeapRep();
	}

	void GCHeap::LogChar(char c, size_t count)
	{
		char tmp[100];
		char* buf = count < 100 ? tmp : (char*)VMPI_alloc(count+1);
		if (buf == NULL)
			return;
		VMPI_memset(buf, c, count);
		buf[count] = '\0';

		GCLog(buf);
		if (buf != tmp)
			VMPI_free(buf);
	}

	void GCHeap::DumpHeapRep()
	{
		Region **regions = NULL;
		Region *r = lastRegion;
		int numRegions = 0;

		GCLog("Heap representation format: \n");
		GCLog("region base address - commitTop/reserveTop\n");
		GCLog("[0 == free, 1 == committed, - = uncommitted]*\n");

		// count and sort regions
		while(r) {
			numRegions++;
			r = r->prev;
		}
		regions = (Region**) VMPI_alloc(sizeof(Region*)*numRegions);
		if (regions == NULL)
			return;
		r = lastRegion;
		for(int i=0; i < numRegions; i++, r = r->prev) {
			int insert = i;
			for(int j=0; j < i; j++) {
				if(r->baseAddr < regions[j]->baseAddr) {
					memmove(&regions[j+1], &regions[j], sizeof(Region*) * (i - j));
					insert = j;
					break;
				}
			}
			regions[insert] = r;
		}

		HeapBlock *spanningBlock = NULL;
		for(int i=0; i < numRegions; i++)
		{
			r = regions[i];
			GCLog("0x%p -  0x%p/0x%p\n", r->baseAddr, r->commitTop, r->reserveTop);
			char c;
			char *addr = r->baseAddr;
			
			if(spanningBlock) {
				GCAssert(spanningBlock->baseAddr + (spanningBlock->size * kBlockSize) > r->baseAddr);
				GCAssert(spanningBlock->baseAddr < r->baseAddr);				
				char *end = spanningBlock->baseAddr + (spanningBlock->size * kBlockSize);
				if(end > r->reserveTop)
					end = r->reserveTop;

				LogChar(spanningBlock->inUse() ? '1' : '0', (end - addr)/kBlockSize);
				addr = end;

				if(addr == spanningBlock->baseAddr + (spanningBlock->size * kBlockSize))
					spanningBlock = NULL;
			}
			HeapBlock *hb;
			while(addr != r->commitTop && (hb = AddrToBlock(addr)) != NULL) {
				GCAssert(hb->size != 0);

				if(hb->inUse())
					c = '1';
				else if(hb->committed)
					c = '0';
				else 
					c = '-';
				size_t i, n;
				for(i=0, n=hb->size; i < n; i++, addr += GCHeap::kBlockSize) {
					if(addr == r->reserveTop) {
						// end of region!
						spanningBlock = hb;
						break;
					}
				}

				LogChar(c, i);
			}

			LogChar('-', (r->reserveTop - addr) / kBlockSize);

			GCLog("\n");
		}
		VMPI_free(regions);
	}
	
#ifdef MMGC_MEMORY_PROFILER

	/* static */
	void GCHeap::InitProfiler()
	{
		GCAssert(IsProfilerInitialized() == false);
		profiler = NULL;

#ifdef MMGC_MEMORY_INFO
		bool profilingEnabled = true;
#else
		bool profilingEnabled = VMPI_isMemoryProfilingEnabled();
#endif
		if(profilingEnabled)
		{
			profiler = new MemoryProfiler();
		}
	}

#endif //MMGC_MEMORY_PROFILER

#ifdef MMGC_MEMORY_PROFILER
#ifdef MMGC_USE_SYSTEM_MALLOC

	void GCHeap::TrackSystemAlloc(void *addr, size_t askSize)
	{
		MMGC_LOCK(m_spinlock);
		if(!IsProfilerInitialized())
			InitProfiler();
		if(profiler)
			profiler->RecordAllocation(addr, askSize, VMPI_size(addr));
	}

	void GCHeap::TrackSystemFree(void *addr)
	{
		MMGC_LOCK(m_spinlock);
		if(addr && profiler)
			profiler->RecordDeallocation(addr, VMPI_size(addr));
	}

#endif //MMGC_USE_SYSTEM_MALLOC
#endif // MMGC_MEMORY_PROFILER
	
	void GCHeap::ReleaseMemory(char *address, size_t size)
	{
		if(config.useVirtualMemory) {
			bool success = VMPI_releaseMemoryRegion(address, size);
			GCAssert(success);
			(void)success;
		} else {
			VMPI_releaseAlignedMemory(address);
		}
	}

	void GCManager::destroy()
	{
		collectors.Destroy();
	}
	
	void GCManager::signalStartCollection(GC* gc)
	{
		BasicListIterator<GC*> iter(collectors);
		GC* otherGC;
		while((otherGC = iter.next()) != NULL) 
			otherGC->policy.signalStartCollection(gc);
	}
	
	void GCManager::signalEndCollection(GC* gc)
	{
		BasicListIterator<GC*> iter(collectors);
		GC* otherGC;
		while((otherGC = iter.next()) != NULL) 
			otherGC->policy.signalStartCollection(gc);
	}

	/* this method is the heart of the OOM system.
	   its here that we call out to the mutator which may call
	   back in to free memory or try to get more.
	*/
	void GCHeap::StatusChangeNotify(MemoryStatus to)
	{
		statusNotificationBeingSent = true;
		MemoryStatus oldStatus = status;
		status = to;
		
		// unlock the heap so that memory operations are allowed

		// this isn't right, really what we want is to allow this
		// thread to call Free but keep other threads out, so what we
		// really want is a lock operation that allows repeated same
		// thread locks
		VMPI_lockRelease(&m_spinlock);
		
		BasicListIterator<OOMCallback*> iter(callbacks);
		OOMCallback *cb = NULL;
		do {
			{
				MMGC_LOCK(list_lock);
				cb = iter.next();
			}
			if(cb)
				cb->memoryStatusChange(oldStatus, to);
		} while(cb != NULL);

		statusNotificationBeingSent = false;

		VMPI_lockAcquire(&m_spinlock);

		CheckForStatusReturnToNormal();
	}

 	/*static*/
	bool GCHeap::ShouldNotEnter()
	{
		// don't enter if the heap is already gone or we're aborting but not on the aborting call stack in a nested enter call
		if(GetGCHeap() == NULL || 
		   (MMgc::GCHeap::GetGCHeap()->GetStatus() == MMgc::kMemAbort && MMgc::GCHeap::GetGCHeap()->GetEnterFrame() == NULL))
			return true;
		return false;
	}

	bool GCHeap::IsAddressInHeap(void *addr)
	{
		return AddrToBlock(addr) != NULL;
	}

	// Every new GC must register itself with the GCHeap.
	void GCHeap::AddGC(GC *gc)
	{ 
		bool bAdded = false;
		{
			MMGC_LOCK(list_lock);
			bAdded = gcManager.tryAddGC(gc);
		}
		if (!bAdded)
		{
			Abort();
		}
	}		
		
	// When the GC is destroyed it must remove itself from the GCHeap.
	void GCHeap::RemoveGC(GC *gc) 
	{ 
		MMGC_LOCK(list_lock);
		gcManager.removeGC(gc); 
		EnterFrame* ef = GetEnterFrame();
		if (ef && ef->GetActiveGC() == gc)
			ef->SetActiveGC(NULL);
	}
	
	void GCHeap::AddOOMCallback(OOMCallback *p) 
	{
		bool bAdded = false;
		{
			MMGC_LOCK(list_lock);
			bAdded = callbacks.TryAdd(p);
		}
		if (!bAdded)
		{
			Abort();
		}
	}
	
	void GCHeap::RemoveOOMCallback(OOMCallback *p) 
	{ 
		MMGC_LOCK(list_lock);
		callbacks.Remove(p); 
	}

	GCHeap::Region *GCHeap::NewRegion(char *baseAddr, char *rTop, char *cTop, size_t blockId)
	{
		Region *r = freeRegion;
		if(r) {
			freeRegion = *(Region**)freeRegion;
		} else {
			r = nextRegion++;
			if(roundUp((uintptr_t)nextRegion, kBlockSize) - (uintptr_t)nextRegion < sizeof(Region))
				nextRegion = NULL; // fresh page allocated in ExpandHeap
		}			
		new (r) Region(this, baseAddr, rTop, cTop, blockId);
		return r;
	}

	void GCHeap::FreeRegion(Region *r)
	{
		if(r == lastRegion)
			lastRegion = r->prev;
		*(Region**)r = freeRegion;
		freeRegion = r;		

	}
	
	/*static*/ 
	void GCHeap::EnterLockInit()
	{ 
		if (!instanceEnterLockInitialized)
		{
			instanceEnterLockInitialized = true; 
			VMPI_lockInit(&instanceEnterLock); 
		}
	}

	/*static*/ 
	void GCHeap::EnterLockDestroy()
	{  
		GCAssert(instanceEnterLockInitialized);
		VMPI_lockDestroy(&instanceEnterLock);
	}

	GCHeap::Region::Region(GCHeap *heap, char *baseAddr, char *rTop, char *cTop, size_t blockId)
		: prev(heap->lastRegion), 
		  baseAddr(baseAddr), 
		  reserveTop(rTop), 
		  commitTop(cTop), 
		  blockId(blockId)
	{
		heap->lastRegion = this;
	}
}