Source

xemacs-beta / src / dumper.c

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
/* Portable data dumper for XEmacs.
   Copyright (C) 1999-2000,2004 Olivier Galibert
   Copyright (C) 2001 Martin Buchholz
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2010 Ben Wing.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with: Not in FSF. */

/* This file has been Mule-ized, Ben Wing, 10-10-04. */

/* #### Put in much more assertions.  Whenever we store fixups in the
   process or writing out data, make sure the fixups (offsets) point to the
   beginning of an object, i.e. are registered.  Same whenever we read in
   -- verify offsets as registered, and when compute a fixup, verify the
   pointer is pointing within the pdump area.  registered and check within
   pdump area.  For specific types of pointers (e.g. to Lisp_Objects),
   check if they're pointing to the right kinds of types.  It should be
   possible to check that a putative Lisp_Object is really a Lisp_Object
   since it will follow a strict format in its header. */

#include <config.h>
#include "lisp.h"

#include "specifier.h"
#include "file-coding.h"
#include "elhash.h"
#include "lstream.h"
#include "sysfile.h"
#include "console-stream.h"

#ifdef WIN32_NATIVE
#include "syswindows.h"
#else
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
#ifdef DUMP_IN_EXEC
#include "dump-data.h"
#endif
#endif

typedef struct
{
  const void *blockaddr;
  Bytecount size;
  const struct memory_description *desc;
} pdump_root_block;

typedef struct
{
  Dynarr_declare (pdump_root_block);
} pdump_root_block_dynarr;

typedef struct
{
  void **ptraddress;
  const struct sized_memory_description *desc;
} pdump_root_block_ptr;

typedef struct
{
  Dynarr_declare (pdump_root_block_ptr);
} pdump_root_block_ptr_dynarr;

typedef struct
{
  const void *object;
  void *data;
  Bytecount size;
  EMACS_INT offset;
  EMACS_INT dest_offset;
  EMACS_INT save_offset;
  const struct opaque_convert_functions *fcts;
} pdump_cv_data_info;

typedef struct 
{
  Dynarr_declare (pdump_cv_data_info);
} pdump_cv_data_info_dynarr;

typedef struct
{
  EMACS_INT dest_offset;
  EMACS_INT save_offset;
  Bytecount size;
} pdump_cv_data_dump_info;

typedef struct
{
  const void *object;
  void *data;
  Bytecount size;
  EMACS_INT index;
  EMACS_INT save_offset;
  const struct opaque_convert_functions *fcts;
} pdump_cv_ptr_info;

typedef struct 
{
  Dynarr_declare (pdump_cv_ptr_info);
} pdump_cv_ptr_info_dynarr;

typedef struct
{
  EMACS_INT save_offset;
  Bytecount size;
} pdump_cv_ptr_dump_info;

typedef struct
{
  EMACS_INT save_offset;
  Bytecount size;
  void *adr;
} pdump_cv_ptr_load_info;

typedef struct
{
  Lisp_Object *address;
  Lisp_Object value;
} pdump_static_Lisp_Object;

typedef struct
{
  Rawbyte **address; /* Rawbyte * for ease of doing relocation */
  Rawbyte * value;
} pdump_static_pointer;

static pdump_root_block_dynarr *pdump_root_blocks;
static pdump_root_block_ptr_dynarr *pdump_root_block_ptrs;
static Lisp_Object_ptr_dynarr *pdump_root_lisp_objects;
static Lisp_Object_ptr_dynarr *pdump_weak_object_chains;
static pdump_cv_data_info_dynarr *pdump_cv_data;
static pdump_cv_ptr_info_dynarr *pdump_cv_ptr;

/* Mark SIZE bytes at non-heap address BLOCKADDR for dumping, described
   by DESC.  Called by outside callers during XEmacs initialization.  */

void
dump_add_root_block (const void *blockaddr, Bytecount size,
		     const struct memory_description *desc)
{
  pdump_root_block info;
  info.blockaddr = blockaddr;
  info.size = size;
  info.desc = desc;
  if (pdump_root_blocks == NULL)
    pdump_root_blocks = Dynarr_new (pdump_root_block);
  Dynarr_add (pdump_root_blocks, info);
}

/* Mark the block described by DESC and pointed to by the pointer at
   non-heap address PTRADDRESS for dumping.
   All the objects reachable from this pointer will also be dumped.
   Called by outside callers during XEmacs initialization. */
void
dump_add_root_block_ptr (void *ptraddress,
			 const struct sized_memory_description *desc)
{
  pdump_root_block_ptr info;
  info.ptraddress = (void **) ptraddress;
  info.desc = desc;
  if (pdump_root_block_ptrs == NULL)
    pdump_root_block_ptrs = Dynarr_new (pdump_root_block_ptr);
  Dynarr_add (pdump_root_block_ptrs, info);
}

/* Mark the Lisp_Object at non-heap address VARADDRESS for dumping.
   All the objects reachable from this var will also be dumped.
   Called by outside callers during XEmacs initialization.  */
void
dump_add_root_lisp_object (Lisp_Object *varaddress)
{
  if (pdump_root_lisp_objects == NULL)
    pdump_root_lisp_objects = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
  Dynarr_add (pdump_root_lisp_objects, varaddress);
}

/* Mark the list pointed to by the Lisp_Object at VARADDRESS for dumping.
   Called by outside callers during XEmacs initialization.  */
void
dump_add_weak_object_chain (Lisp_Object *varaddress)
{
  if (pdump_weak_object_chains == NULL)
    pdump_weak_object_chains = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
  Dynarr_add (pdump_weak_object_chains, varaddress);
}


inline static void
pdump_align_stream (FILE *stream, Bytecount alignment)
{
  long offset = ftell (stream);
  long adjustment = ALIGN_SIZE (offset, alignment) - offset;
  if (adjustment)
    fseek (stream, adjustment, SEEK_CUR);
}

#define PDUMP_ALIGN_OUTPUT(type) pdump_align_stream (pdump_out, ALIGNOF (type))

#define PDUMP_WRITE(type, object) \
retry_fwrite (&object, sizeof (object), 1, pdump_out);

#define PDUMP_WRITE_ALIGNED(type, object) do {	\
  PDUMP_ALIGN_OUTPUT (type);			\
  PDUMP_WRITE (type, object);			\
} while (0)

#define PDUMP_READ(ptr, type) \
(((type *) (ptr = (Rawbyte *) (((type *) ptr) + 1)))[-1])

#define PDUMP_READ_ALIGNED(ptr, type) \
((ptr = (Rawbyte *) ALIGN_PTR (ptr, type)), PDUMP_READ (ptr, type))



typedef struct
{
  const struct memory_description *desc;
  int count;
} pdump_reloc_table;

static Rawbyte *pdump_rt_list = 0;

#ifndef NEW_GC
void
pdump_objects_unmark (void)
{
  int i;
  Rawbyte *p = pdump_rt_list;
  if (p)
    for (;;)
      {
	pdump_reloc_table *rt = (pdump_reloc_table *)p;
	p += sizeof (pdump_reloc_table);
	if (rt->desc)
	  {
	    for (i=0; i<rt->count; i++)
	      {
		struct lrecord_header *lh = * (struct lrecord_header **) p;
#ifdef ALLOC_TYPE_STATS
		if (C_READONLY_RECORD_HEADER_P (lh))
		  tick_lrecord_stats (lh, ALLOC_IN_USE);

		else
		  {
		    tick_lrecord_stats (lh, MARKED_RECORD_HEADER_P (lh) ?
					ALLOC_IN_USE : ALLOC_ON_FREE_LIST);
		    UNMARK_RECORD_HEADER (lh);
		  }
#else /* not ALLOC_TYPE_STATS */
		if (! C_READONLY_RECORD_HEADER_P (lh))
		  UNMARK_RECORD_HEADER (lh);
#endif /* (not) ALLOC_TYPE_STATS */
		p += sizeof (EMACS_INT);
	      }
	  } else
	    break;
      }
}
#endif /* not NEW_GC */


#ifdef NEW_GC
/* The structure of the dump file looks like this:
 0		- header
		- dumped objects
 stab_offset	- mc allocation table (count, size, address) for individual
		  allocation and  relocation at load time.
        	- nb_cv_data*struct(dest, adr) for in-object externally
		  represented data
		- nb_cv_ptr*(adr) for pointed-to externally represented data
            	- relocation table
             	- nb_root_struct_ptrs*struct(void *, adr)
		  for global pointers to structures
		- nb_root_blocks*struct(void *, size, info) for global
		  objects to restore
		- root lisp object address/value couples with the count
		  preceding the list
 */
#else /* not NEW_GC */
/* The structure of the dump file looks like this:
 0		- header
		- dumped objects
 stab_offset	- nb_cv_data*struct(dest, adr) for in-object externally
		  represented data
		- nb_cv_ptr*(adr) for pointed-to externally represented data
 		- nb_root_block_ptrs*struct(void *, adr)
		  for global pointers to heap blocks
		- nb_root_blocks*struct(void *, size, info) for global
		  data-segment blocks to restore
		- relocation table
		- root lisp object address/value couples with the count
		  preceding the list
 */
#endif /* not NEW_GC */


#define PDUMP_SIGNATURE "XEmacsDP"
#define PDUMP_SIGNATURE_LEN (sizeof (PDUMP_SIGNATURE) - 1)

typedef struct
{
  char signature[PDUMP_SIGNATURE_LEN];
  unsigned int id;
  EMACS_UINT stab_offset;
  EMACS_UINT reloc_address;
  int nb_root_block_ptrs;
  int nb_root_blocks;
  int nb_cv_data;
  int nb_cv_ptr;
} pdump_header;

Rawbyte *pdump_start;
Rawbyte *pdump_end;
static Bytecount pdump_length;

static pdump_cv_data_dump_info *pdump_loaded_cv_data;
static pdump_cv_ptr_load_info  *pdump_loaded_cv_ptr;

#ifdef WIN32_NATIVE
/* Handle for the dump file */
static HANDLE pdump_hFile = INVALID_HANDLE_VALUE;
/* Handle for the file mapping object for the dump file */
static HANDLE pdump_hMap = INVALID_HANDLE_VALUE;
#endif

static void (*pdump_free) (void);

static unsigned char pdump_align_table[] =
{
  64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
  16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
  32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
  16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1
};

static inline int
pdump_size_to_align (Bytecount size)
{
  return pdump_align_table[size % countof (pdump_align_table)];
}

/************************************************************************/
/*                     Registering memory blocks                        */
/************************************************************************/

/* "Registering" or recording a heap memory block (which will need to be
   written out, reloaded and relocated, and to which there may be pointers
   from other heap blocks or from the data segment) happens both in a list
   and in a hash table.  There is a single hash table covering all
   registered blocks, but different lists for different kinds of blocks.
   There is one list for "opaque data" (stuff identified as
   XD_OPAQUE_DATA_PTR, XD_ASCII_STRING, XD_DOC_STRING), one list for each
   type of Lisp object, and one list for each different memory descriptor.
   This lets similar-sized and aligned objects be grouped together when
   they are written out, to save space.

   pdump_block_list is a list keeping track of registered memory blocks.
   pdump_block_list_elt is a single entry through the list, and the list is
   threaded through the NEXT pointer.  The information in this list
   associated with a particular block of memory is

   -- address of the beginning
   -- number of elements at that address
   -- size of each element
   -- offset to this block in the dumped data

   pdump_desc_list is a list keeping track of the various descriptions
   that we've seen.  The primary purpose of this is so that memory blocks
   can be grouped depending on the particular memory description
   appropriate for them.  The format of the list is different from
   pdump_block_list -- a single array is used. (#### Dynarr should have
   been used!!!).  The information in this list associated with a
   description is

   -- pointer to the description
   -- a pdump_block_list of blocks using that description

   Functions for working with lists of memory blocks:

   -- Add a memory block to a list using pdump_add_block()

   -- Get a memory block from a pointer to its beginning using
      pdump_get_block().  This uses the hash table, which lists everything.

   -- Return the memory-block list (pdump_block_list) associated with a
      descriptor, using pdump_get_block_list().  If no entry found in the
      pdump_desc_list, add a new one.

*/ 

typedef struct pdump_block_list_elt
{
  struct pdump_block_list_elt *next;
  const void *obj;
  Bytecount size;
  int count;
  EMACS_INT save_offset;
} pdump_block_list_elt;

typedef struct
{
  pdump_block_list_elt *first;
  int align;
  int count;
} pdump_block_list;

typedef struct pdump_desc_list_elt
{
  pdump_block_list list;
  const struct memory_description *desc;
} pdump_desc_list_elt;

typedef struct
{
  pdump_desc_list_elt *list;
  int count;
  int size;
} pdump_desc_list;

static pdump_block_list *pdump_object_table;
static pdump_block_list pdump_opaque_data_list;
static pdump_desc_list pdump_desc_table;

static int *pdump_alert_undump_object;

static unsigned long cur_offset;
static Bytecount max_size;
static int pdump_fd;
static void *pdump_buf;
static FILE *pdump_out;

#ifdef NEW_GC
/* PDUMP_HASHSIZE is a large prime. */
#define PDUMP_HASHSIZE        1000003
/* Nothing special about PDUMP_HASH_MULTIPLIER: arbitrary odd integer
   smaller than PDUMP_HASHSIZE. */
#define PDUMP_HASH_MULTIPLIER   12347
/* Nothing special about PDUMP_HASH_STEP: arbitrary integer for linear
   probing. */
#define PDUMP_HASH_STEP        574853
#else /* not NEW_GC */
#define PDUMP_HASHSIZE 200001
#endif /* not NEW_GC */

static pdump_block_list_elt **pdump_hash;

#ifndef NEW_GC
/* Since most pointers are eight bytes aligned, the >>3 allows for a better hash */
#endif /* not NEW_GC */
static int
pdump_make_hash (const void *obj)
{
#ifdef NEW_GC
  return ((unsigned long)(obj) * PDUMP_HASH_MULTIPLIER) % PDUMP_HASHSIZE;
#else /* not NEW_GC */
  return ((unsigned long)(obj)>>3) % PDUMP_HASHSIZE;
#endif /* not NEW_GC */
}

/* Return the entry for an already-registered memory block at OBJ,
   or NULL if none. */

static pdump_block_list_elt *
pdump_get_block (const void *obj)
{
  int pos = pdump_make_hash (obj);
  pdump_block_list_elt *e;

  assert (obj != 0);

  while ((e = pdump_hash[pos]) != 0)
    {
      if (e->obj == obj)
	return e;

      pos++;
      if (pos == PDUMP_HASHSIZE)
	pos = 0;
    }
  return 0;
}

/* Register a new memory block on Return the entry for an already-registered heap (?) memory block at OBJ,
   or NULL if none. */

static void
pdump_add_block (pdump_block_list *list, const void *obj, Bytecount size,
		 int count)
{
  pdump_block_list_elt *e;
  int pos = pdump_make_hash (obj);

  while ((e = pdump_hash[pos]) != 0)
    {
      if (e->obj == obj)
	return;

      pos++;
      if (pos == PDUMP_HASHSIZE)
	pos = 0;
    }

  e = xnew (pdump_block_list_elt);

  e->next = list->first;
  e->obj = obj;
  e->size = size;
  e->count = count;
  list->first = e;

  list->count += count;
  pdump_hash[pos] = e;

  {
    int align = pdump_size_to_align (size);

    if (align < list->align)
      list->align = align;
  }
}

#ifdef NEW_GC
typedef struct mc_addr_elt
{
  const void *obj;
  EMACS_INT addr;
} mc_addr_elt;

static mc_addr_elt *pdump_mc_hash;

/* Return the entry for an already-registered memory block at OBJ,
   or NULL if none. */
static EMACS_INT
pdump_get_mc_addr (const void *obj)
{
  int pos = pdump_make_hash (obj);
  mc_addr_elt *mc_addr;

  assert (obj != 0);

  while (((mc_addr = &pdump_mc_hash[pos]) != 0) && (mc_addr->obj != 0))
    {
      if (mc_addr->obj == obj)
	return mc_addr->addr;

      pos += PDUMP_HASH_STEP;
      if (pos >= PDUMP_HASHSIZE)
	pos -= PDUMP_HASHSIZE;
    }

  /* If this code is reached, an heap address occurred which has not
     been written to the lookup table before.
     This is a bug! */
  ABORT();
  return 0;
}

/* For indirect address lookups, needed for convertibles: Ptr points
   to an address within an object. Indirect gives the offset by how
   many bytes the address of the object has to be adjusted to do a
   lookup in the mc_addr translation table and get the new location of
   the data. */
#define pdump_get_indirect_mc_addr(ptr, indirect) \
  pdump_get_mc_addr ((void *)((ptr) - indirect)) + indirect

static void
pdump_put_mc_addr (const void *obj, EMACS_INT addr)
{
  mc_addr_elt *mc_addr;
  int pos = pdump_make_hash (obj);

  while (((mc_addr = &pdump_mc_hash[pos]) != 0) && (mc_addr->obj != 0))
    {
      if (mc_addr->obj == obj)
	return;

      pos += PDUMP_HASH_STEP;
      if (pos >= PDUMP_HASHSIZE)
	pos -= PDUMP_HASHSIZE;
    }

  pdump_mc_hash[pos].obj = obj;
  pdump_mc_hash[pos].addr = addr;
}
#endif /* NEW_GC */

static pdump_block_list *
pdump_get_block_list (const struct memory_description *desc)
{
  int i;
  for (i=0; i<pdump_desc_table.count; i++)
    if (pdump_desc_table.list[i].desc == desc)
      return &pdump_desc_table.list[i].list;

  if (pdump_desc_table.size <= pdump_desc_table.count)
    {
      if (pdump_desc_table.size == -1)
	pdump_desc_table.size = 10;
      else
	pdump_desc_table.size = pdump_desc_table.size * 2;
      pdump_desc_table.list = (pdump_desc_list_elt *)
	xrealloc (pdump_desc_table.list,
		  pdump_desc_table.size * sizeof (pdump_desc_list_elt));
    }
  pdump_desc_table.list[pdump_desc_table.count].list.first = 0;
  pdump_desc_table.list[pdump_desc_table.count].list.align = ALIGNOF (max_align_t);
  pdump_desc_table.list[pdump_desc_table.count].list.count = 0;
  pdump_desc_table.list[pdump_desc_table.count].desc = desc;

  return &pdump_desc_table.list[pdump_desc_table.count++].list;
}

static pdump_cv_ptr_info *
pdump_find_in_cv_ptr_dynarr(const void *object)
{
  int i;
  for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++)
    if (Dynarr_at (pdump_cv_ptr, i).object == object)
      return Dynarr_atp (pdump_cv_ptr, i);
  return 0;
}

#define BACKTRACE_MAX 65536

static struct
{
  struct lrecord_header *obj;
  int position;
  int offset;
} backtrace[BACKTRACE_MAX];

static int pdump_depth;

void
pdump_backtrace (void)
{
  int i;
  stderr_out ("pdump backtrace :\n");
  for (i = 0; i < pdump_depth; i++)
    {
      if (!backtrace[i].obj)
	stderr_out ("  - ind. (%d, %d)\n",
		    backtrace[i].position,
		    backtrace[i].offset);
      else
	{
	  stderr_out ("  - %s (%d, %d)\n",
		      LHEADER_IMPLEMENTATION (backtrace[i].obj)->name,
		      backtrace[i].position,
		      backtrace[i].offset);
	}
    }
}

static void
pdump_unsupported_dump_type (enum memory_description_type type,
			     int do_backtrace)
{
  stderr_out ("Unsupported dump type : %d\n", type);
#ifdef WIN32_NATIVE
  stderr_out ("Are you compiling with SUPPORT_EDIT_AND_CONTINUE?\n");
  stderr_out ("See the PROBLEMS file.\n");
#endif
  if (do_backtrace)
    pdump_backtrace ();
  ABORT ();
}

static void
pdump_bump_depth (void)
{
  int me = pdump_depth++;
  if (me >= BACKTRACE_MAX)
    {
      stderr_out ("Backtrace overflow, loop ?\n");
      ABORT ();
    }
  backtrace[me].obj = 0;
  backtrace[me].position = 0;
  backtrace[me].offset = 0;
}

static void pdump_register_object (Lisp_Object obj);
#ifdef NEW_GC
static void pdump_register_object_array (Lisp_Object data,
					 Bytecount size,
					 const struct memory_description *desc,
					 int count);
#endif /* NEW_GC */
static void pdump_register_block_contents (const void *data,
					   Bytecount size,
					   const struct memory_description *
					   desc,
					   int count);
static void pdump_register_block (const void *data,
				  Bytecount size,
				  const struct memory_description *desc,
				  int count);

static void
pdump_register_sub (const void *data, const struct memory_description *desc)
{
  int pos;
  int me = pdump_depth - 1;

  for (pos = 0; desc[pos].type != XD_END; pos++)
    {
      const struct memory_description *desc1 = &desc[pos];
      EMACS_INT offset = lispdesc_indirect_count (desc1->offset, desc,
						  data);
      const void *rdata = (const Rawbyte *) data + offset;

      backtrace[me].position = pos;
      backtrace[me].offset = offset;

    union_switcheroo:

      /* If the flag says don't dump, then don't dump. */
      if ((desc1->flags) & XD_FLAG_NO_PDUMP)
	continue;

      switch (desc1->type)
	{
	case XD_BYTECOUNT:
	case XD_ELEMCOUNT:
	case XD_HASHCODE:
	case XD_INT:
	case XD_LONG:
	case XD_INT_RESET:
	case XD_LO_LINK:
	  break;
	case XD_OPAQUE_DATA_PTR:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);

	    pdump_add_block (&pdump_opaque_data_list,
			     *(void **)rdata, count, 1);
	    break;
	  }
	case XD_ASCII_STRING:
	  {
	    const Ascbyte *str = * (const Ascbyte **) rdata;
	    if (str)
	      pdump_add_block (&pdump_opaque_data_list, str, strlen (str) + 1,
			       1);
	    break;
	  }
	case XD_DOC_STRING:
	  {
	    const Ascbyte *str = * (const Ascbyte **) rdata;
	    if ((EMACS_INT) str > 0)
	      pdump_add_block (&pdump_opaque_data_list, str, strlen (str) + 1,
			       1);
	    break;
	  }
	case XD_LISP_OBJECT:
	  {
	    const Lisp_Object *pobj = (const Lisp_Object *) rdata;

	    assert (desc1->data1 == 0);

	    backtrace[me].offset =
	      (const Rawbyte *) pobj - (const Rawbyte *) data;
	    pdump_register_object (*pobj);
	    break;
	  }
	case XD_LISP_OBJECT_ARRAY:
	  {
	    int i;
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);

	    for (i = 0; i < count; i++)
	      {
		const Lisp_Object *pobj = ((const Lisp_Object *) rdata) + i;
		Lisp_Object dobj = *pobj;

		backtrace[me].offset =
		  (const Rawbyte *) pobj - (const Rawbyte *) data;
		pdump_register_object (dobj);
	      }
	    break;
	  }
#ifdef NEW_GC
	case XD_INLINE_LISP_OBJECT_BLOCK_PTR:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);
	    const Lisp_Object *pobj = (const Lisp_Object *) rdata;
	    if (pobj)
	      pdump_register_object_array 
		(*pobj, sdesc->size, sdesc->description, count);
	    break;
	  }
#endif /* NEW_GC */
	case XD_BLOCK_PTR:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);
	    const Rawbyte *dobj = *(const Rawbyte **)rdata;
	    if (dobj)
	      pdump_register_block (dobj, sdesc->size, sdesc->description,
				    count);
	    break;
	  }
	case XD_BLOCK_ARRAY:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);

	    pdump_register_block_contents (rdata, sdesc->size,
					   sdesc->description, count);
	    break;
	  }
	case XD_UNION:
	case XD_UNION_DYNAMIC_SIZE:
	  desc1 = lispdesc_process_xd_union (desc1, desc, data);
	  if (desc1)
	    goto union_switcheroo;
	  break;
	case XD_OPAQUE_PTR_CONVERTIBLE:
	  {
	    pdump_cv_ptr_info info;
	    info.object = *(void **)rdata;
	    info.fcts = desc1->data2.funcs;
	    if (!pdump_find_in_cv_ptr_dynarr (info.object))
	      {
		info.fcts->convert(info.object, &info.data, &info.size);
		Dynarr_add (pdump_cv_ptr, info);
	      }
	    break;
	  }
	case XD_OPAQUE_DATA_CONVERTIBLE:
	  {
	    pdump_cv_data_info info;
	    info.object = data;
	    info.offset = offset;
	    info.fcts = desc1->data2.funcs;

	    info.fcts->convert(rdata, &info.data, &info.size);
	    Dynarr_add (pdump_cv_data, info);
	    break;
	  }

	default:
	  pdump_unsupported_dump_type (desc1->type, 1);
	}
    }
}

static void
pdump_register_object (Lisp_Object obj)
{
  struct lrecord_header *objh;
  const struct lrecord_implementation *imp;

  if (!POINTER_TYPE_P (XTYPE (obj)))
    return;

  objh = XRECORD_LHEADER (obj);
  if (!objh)
    return;

  if (pdump_get_block (objh))
    return;

  imp = LHEADER_IMPLEMENTATION (objh);

  if (imp->description
#ifdef NEW_GC
      /* Objects with finalizers cannot be dumped with the new
	 allocator's asynchronous finalization strategy. */
      && !imp->finalizer
#endif /* not NEW_GC */
      && RECORD_DUMPABLE (objh))
    {
      pdump_bump_depth ();
      backtrace[pdump_depth - 1].obj = objh;
      pdump_add_block (pdump_object_table + objh->type,
		       objh, detagged_lisp_object_size (objh), 1);
      pdump_register_sub (objh, imp->description);
      --pdump_depth;
    }
  else
    {
      pdump_alert_undump_object[objh->type]++;
      stderr_out ("Undumpable object type : %s\n", imp->name);
      pdump_backtrace ();
    }
}

#ifdef NEW_GC
static void
pdump_register_object_array (Lisp_Object obj,
			     Bytecount size,
			     const struct memory_description *desc,
			     int count)
{
  struct lrecord_header *objh;
  const struct lrecord_implementation *imp;

  if (!POINTER_TYPE_P (XTYPE (obj)))
    return;

  objh = XRECORD_LHEADER (obj);
  if (!objh)
    return;

  if (pdump_get_block (objh))
    return;

  imp = LHEADER_IMPLEMENTATION (objh);

  if (imp->description
      && RECORD_DUMPABLE (objh))
    {
      pdump_bump_depth ();
      backtrace[pdump_depth - 1].obj = objh;
      pdump_add_block (pdump_object_table + objh->type,
		       objh, lispdesc_block_size_1 (objh, size, desc), count);
      pdump_register_block_contents (objh, size, desc, count);
      --pdump_depth;
    }
  else
    {
      pdump_alert_undump_object[objh->type]++;
      stderr_out ("Undumpable object type : %s\n", imp->name);
      pdump_backtrace ();
    }
}
#endif /* NEW_GC */

/* Register the referenced objects in the array of COUNT blocks located at
   DATA; each block is described by SIZE and DESC.  "Block" here simply
   means any block of memory.

   This does not register the block of memory itself; it may, for
   example, be an array of structures inlined in another memory block
   and thus should not be registered.  See pdump_register_block(),
   which does register the memory block. */

static void
pdump_register_block_contents (const void *data,
			       Bytecount size,
			       const struct memory_description *desc,
			       int count)
{
  int i;
  Bytecount elsize;

  pdump_bump_depth ();
  elsize = lispdesc_block_size_1 (data, size, desc);
  for (i = 0; i < count; i++)
    {
      pdump_register_sub (((Rawbyte *) data) + elsize * i, desc);
    }
  --pdump_depth;
}

/* Register the array of COUNT blocks located at DATA; each block is
   described by SDESC.  "Block" here simply means any block of memory,
   which is more accurate and less confusing than terms like `struct' and
   `object'.  A `block' need not actually be a C "struct".  It could be a
   single integer or Lisp_Object, for example, as long as the description
   is accurate.

   This is like pdump_register_block_contents() but also registers
   the memory block itself. */

static void
pdump_register_block (const void *data,
		      Bytecount size,
		      const struct memory_description *desc,
		      int count)
{
  if (data && !pdump_get_block (data))
    {
      pdump_add_block (pdump_get_block_list (desc), data,
		       lispdesc_block_size_1 (data, size, desc), count);
      pdump_register_block_contents (data, size, desc, count);
    }
}


/* Store the already-calculated new pointer offsets for all pointers in the
   COUNT contiguous blocks of memory, each described by DESC and of size
   SIZE, whose original is located at ORIG_DATA and the modifiable copy at
   DATA.  We examine the description to figure out where the pointers are,
   and then look up the replacement values using pdump_get_block().

   This is done just before writing the modified block of memory to the
   dump file.  The new pointer offsets have been carefully calculated so
   that the data being pointed gets written at that offset in the dump
   file.  That way, the dump file is a correct memory image except perhaps
   for a constant that needs to be added to all pointers. (#### In fact, we
   SHOULD be starting up a dumped XEmacs, seeing where the dumped file gets
   loaded into memory, and then rewriting the dumped file after relocating
   all the pointers relative to this memory location.  That way, if the
   file gets loaded again at the same location, which will be common, we
   don't have to do any relocating, which is both faster at startup and
   allows the read-only part of the dumped data to be shared read-only
   between different invocations of XEmacs.)

   #### Do we distinguish between read-only and writable dumped data?
   Should we?  It's tricky because the dumped data, once loaded again,
   cannot really be free()d or garbage collected since it's all stored in
   one contiguous block of data with no malloc() headers, and we don't keep
   track of the pointers used internally in malloc() and the Lisp allocator
   to track allocated blocks of memory. */

static void
pdump_store_new_pointer_offsets (int count, void *data, const void *orig_data,
				 const struct memory_description *desc,
				 int size)
{
  int pos, i;
  /* Process each block one by one */
  for (i = 0; i < count; i++)
    {
      /* CUR points to the beginning of each block in the new data. */
      Rawbyte *cur = ((Rawbyte *)data) + i * size;
      /* Scan each line of the description for relocatable pointers */
      for (pos = 0; desc[pos].type != XD_END; pos++)
	{
	  /* RDATA points to the beginning of each element in the new data. */
	  const struct memory_description *desc1 = &desc[pos];
	  /* #### Change ORIG_DATA to DATA.  See below. */
	  void *rdata = cur + lispdesc_indirect_count (desc1->offset, desc,
						       orig_data);
	union_switcheroo:

	  /* If the flag says don't dump, then don't dump. */
	  if ((desc1->flags) & XD_FLAG_NO_PDUMP)
	    continue;

	  switch (desc1->type)
	    {
	    case XD_BYTECOUNT:
	    case XD_ELEMCOUNT:
	    case XD_HASHCODE:
	    case XD_INT:
	    case XD_LONG:
	      break;
	    case XD_INT_RESET:
	      {
		EMACS_INT val = lispdesc_indirect_count (desc1->data1, desc,
							 orig_data);
		* (int *) rdata = val;
		break;
	      }
#ifdef NEW_GC
	    case XD_INLINE_LISP_OBJECT_BLOCK_PTR:
#endif /* NEW_GC */
	    case XD_OPAQUE_DATA_PTR:
	    case XD_ASCII_STRING:
	    case XD_BLOCK_PTR:
	      {
		void *ptr = * (void **) rdata;
		if (ptr)
		  * (EMACS_INT *) rdata = pdump_get_block (ptr)->save_offset;
		break;
	      }
	    case XD_LO_LINK:
	      {
		/* As described in lrecord.h, this is a weak link.
                   Thus, we need to link this object not (necessarily)
                   to the object directly pointed to, but to the next
                   referenced object in the chain.  None of the
                   intermediate objects will be written out, so we
                   traverse down the chain of objects until we find a
                   referenced one. (The Qnil or Qunbound that ends the
                   chain will always be a referenced object.) */
		Lisp_Object obj = * (Lisp_Object *) rdata;
		pdump_block_list_elt *elt1;
		/* #### Figure out how to handle indirect offsets here.
		   #### In general, when computing indirect counts, do we
		   really need to use the orig_data pointer?  Why not just
		   use the new stuff?

		   No, we don't usually need orig_data.  We only need it
		   when fetching pointers out of the data, not integers.
		   This currently occurs only with description maps.  We
		   should change the other places to DATA to emphasize
		   this. */
		assert (!XD_IS_INDIRECT (desc1->offset));
		for (;;)
		  {
		    elt1 = pdump_get_block (XRECORD_LHEADER (obj));
		    if (elt1)
		      break;
		    obj = * (Lisp_Object *) (desc1->offset +
					     (Rawbyte *)
					     (XRECORD_LHEADER (obj)));
		  }
		* (EMACS_INT *) rdata = elt1->save_offset;
		break;
	      }
	    case XD_LISP_OBJECT:
	      {
		Lisp_Object *pobj = (Lisp_Object *) rdata;

		assert (desc1->data1 == 0);

		if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
		  * (EMACS_INT *) pobj =
		    pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset;
		break;
	      }
	    case XD_LISP_OBJECT_ARRAY:
	      {
		EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
							 orig_data);
		int j;

		for (j = 0; j < num; j++)
		  {
		    Lisp_Object *pobj = ((Lisp_Object *) rdata) + j;
		    if (POINTER_TYPE_P (XTYPE (*pobj)) &&
			XRECORD_LHEADER (*pobj))
		      * (EMACS_INT *) pobj =
			pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset;
		  }
		break;
	      }
	    case XD_DOC_STRING:
	      {
		EMACS_INT str = *(EMACS_INT *)rdata;
		if (str > 0)
		  * (EMACS_INT *) rdata =
		    pdump_get_block ((void *)str)->save_offset;
		break;
	      }
	    case XD_BLOCK_ARRAY:
	      {
		EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
							 orig_data);
		const struct sized_memory_description *sdesc =
		  lispdesc_indirect_description (orig_data, desc1->data2.descr);

		pdump_store_new_pointer_offsets
		  (num, rdata,
		   ((Rawbyte *) rdata - (Rawbyte *) data) +
		   (Rawbyte *) orig_data,
		   sdesc->description,
		   lispdesc_block_size
		   (((Rawbyte *) rdata - (Rawbyte *) data) +
		    (Rawbyte *) orig_data, sdesc));
		break;
	      }
	    case XD_UNION:
	    case XD_UNION_DYNAMIC_SIZE:
	      desc1 = lispdesc_process_xd_union (desc1, desc, orig_data);
	      if (desc1)
		goto union_switcheroo;
	      break;

	    case XD_OPAQUE_PTR_CONVERTIBLE:
	      *(EMACS_INT *)rdata = pdump_find_in_cv_ptr_dynarr (*(void **)rdata)->index;
	      break;

	    case XD_OPAQUE_DATA_CONVERTIBLE:
	      /* in-object, nothing to do */
	      break;

	    default:
	      pdump_unsupported_dump_type (desc1->type, 0);
	    }
	}
    }
}

/* Write out to global file descriptor PDUMP_OUT the element (one or
   more contiguous blocks of identical size/description) recorded in
   ELT and described by DESC.  The element is first copied to a buffer
   and then all pointers (this includes Lisp_Objects other than
   integer/character) are relocated to the (pre-computed) offset in
   the dump file. */

static void
pdump_dump_data (pdump_block_list_elt *elt,
		 const struct memory_description *desc)
{
  Bytecount size = elt->size;
  int count = elt->count;
  if (desc)
    {
      /* Copy to temporary buffer */
      memcpy (pdump_buf, elt->obj, size*count);

      /* Store new offsets into all pointers in block */
      pdump_store_new_pointer_offsets (count, pdump_buf, elt->obj, desc, size);
    }
  retry_fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out);
}

#ifdef NEW_GC
/* To be able to relocate during load time, more information about the
   dumped objects are needed: The count (for array-like data
   structures), the size of the object, and the location in the dumped
   data.
 */
static void
pdump_dump_mc_data (pdump_block_list_elt *elt,
		   const struct memory_description *UNUSED(desc))
{
  EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset;
  int j;
  PDUMP_WRITE_ALIGNED (int, elt->count);
  PDUMP_WRITE_ALIGNED (Bytecount, elt->size);
  for (j = 0; j < elt->count; j++)
    {
      PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
      rdata += elt->size;
    }
}

static void
pdump_scan_lisp_objects_by_alignment (void (*f)
				      (pdump_block_list_elt *,
				       const struct memory_description *))
{
  int align;

  for (align = ALIGNOF (max_align_t); align; align>>=1)
    {
      int i;
      pdump_block_list_elt *elt;

      for (i=0; i<lrecord_type_count; i++)
	if (pdump_object_table[i].align == align)
	  for (elt = pdump_object_table[i].first; elt; elt = elt->next)
	    {
	      f (elt, lrecord_implementations_table[i]->description);
	    }
    }
}

static void
pdump_scan_non_lisp_objects_by_alignment (void (*f)
					  (pdump_block_list_elt *,
					   const struct memory_description *))
{
  int align;

  for (align = ALIGNOF (max_align_t); align; align>>=1)
    {
      int i;
      pdump_block_list_elt *elt;

      for (i=0; i<pdump_desc_table.count; i++)
	{
	  pdump_desc_list_elt list = pdump_desc_table.list[i];
	  if (list.list.align == align)
	    for (elt = list.list.first; elt; elt = elt->next)
	      f (elt, list.desc);
	}

      for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
	if (pdump_size_to_align (elt->size) == align)
	  f (elt, 0);
    }
}



static void
pdump_reloc_one_mc (void *data, const struct memory_description *desc)
{
  int pos;

  for (pos = 0; desc[pos].type != XD_END; pos++)
    {
      const struct memory_description *desc1 = &desc[pos];
      void *rdata =
	(Rawbyte *) data + lispdesc_indirect_count (desc1->offset,
							desc, data);

    union_switcheroo:

      /* If the flag says don't dump, then don't dump. */
      if ((desc1->flags) & XD_FLAG_NO_PDUMP)
	continue;

      switch (desc1->type)
	{
	case XD_BYTECOUNT:
	case XD_ELEMCOUNT:
	case XD_HASHCODE:
	case XD_INT:
	case XD_LONG:
	case XD_INT_RESET:
	  break;
	case XD_INLINE_LISP_OBJECT_BLOCK_PTR:
	case XD_OPAQUE_DATA_PTR:
	case XD_ASCII_STRING:
	case XD_BLOCK_PTR:
	case XD_LO_LINK:
	  {
	    EMACS_INT ptr = *(EMACS_INT *) rdata;
	    if (ptr)
	      *(EMACS_INT *) rdata = pdump_get_mc_addr ((void *) ptr);
	    break;
	  }
	case XD_LISP_OBJECT:
	  {
	    Lisp_Object *pobj = (Lisp_Object *) rdata;

	    assert (desc1->data1 == 0);

	    if (POINTER_TYPE_P (XTYPE (*pobj))
		&& ! EQ (*pobj, Qnull_pointer))
	      *pobj = wrap_pointer_1 ((Rawbyte *) pdump_get_mc_addr 
				      (XPNTR (*pobj)));
	    break;
	  }
	case XD_LISP_OBJECT_ARRAY:
	  {
	    EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
						     data);
	    int j;

	    for (j=0; j<num; j++)
	      {
		Lisp_Object *pobj = (Lisp_Object *) rdata + j;

		if (POINTER_TYPE_P (XTYPE (*pobj))
		    && ! EQ (*pobj, Qnull_pointer))
		  *pobj = wrap_pointer_1 ((Rawbyte *) pdump_get_mc_addr 
					  (XPNTR (*pobj)));
	      }
	    break;
	  }
	case XD_DOC_STRING:
	  {
	    EMACS_INT str = *(EMACS_INT *) rdata;
	    if (str > 0)
	      *(EMACS_INT *) rdata = pdump_get_mc_addr ((void *) str);
	    break;
	  }
	case XD_BLOCK_ARRAY:
	  {
	    EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
						     data);
	    int j;
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);
	    Bytecount size = lispdesc_block_size (rdata, sdesc);

	    /* Note: We are recursing over data in the block itself */
	    for (j = 0; j < num; j++)
	      pdump_reloc_one_mc ((Rawbyte *) rdata + j * size,
				  sdesc->description);

	    break;
	  }
	case XD_UNION:
	case XD_UNION_DYNAMIC_SIZE:
	  desc1 = lispdesc_process_xd_union (desc1, desc, data);
	  if (desc1)
	    goto union_switcheroo;
	  break;

	case XD_OPAQUE_PTR_CONVERTIBLE:
	  {
	    pdump_cv_ptr_load_info *p = pdump_loaded_cv_ptr + *(EMACS_INT *)rdata;
	    if (!p->adr)
	      p->adr = desc1->data2.funcs->deconvert(0, 
						     pdump_start + p->save_offset,
						     p->size);
	    *(void **)rdata = p->adr;
	    break;
	  }

	case XD_OPAQUE_DATA_CONVERTIBLE:
	  {
	    EMACS_INT dest_offset = (EMACS_INT) rdata;
	    EMACS_INT indirect = 
	      lispdesc_indirect_count (desc1->offset, desc, data);
	    pdump_cv_data_dump_info *p;

	    for(p = pdump_loaded_cv_data; 
		pdump_get_indirect_mc_addr (p->dest_offset, indirect)
		  != dest_offset; 
		p++);

	    desc1->data2.funcs->deconvert(rdata, pdump_start + p->save_offset,
					  p->size);
	    break;
	  }

	default:
	  pdump_unsupported_dump_type (desc1->type, 0);
	}
    }
}
#else /* not NEW_GC */
/* Relocate a single memory block at DATA, described by DESC, from its
   assumed load location to its actual one by adding DELTA to all pointers
   in the block.  Does not recursively relocate any other memory blocks
   pointed to. (We already have a list of all memory blocks in the dump
   file.)  This is used once the dump data has been loaded back in, both
   for blocks sitting in the dumped data (former heap blocks) and in global
   data-sgment blocks whose contents have been restored from the dumped
   data. */

static void
pdump_reloc_one (void *data, EMACS_INT delta,
		 const struct memory_description *desc)
{
  int pos;

  for (pos = 0; desc[pos].type != XD_END; pos++)
    {
      const struct memory_description *desc1 = &desc[pos];
      void *rdata =
	(Rawbyte *) data + lispdesc_indirect_count (desc1->offset,
							desc, data);

    union_switcheroo:

      /* If the flag says don't dump, then don't dump. */
      if ((desc1->flags) & XD_FLAG_NO_PDUMP)
	continue;

      switch (desc1->type)
	{
	case XD_BYTECOUNT:
	case XD_ELEMCOUNT:
	case XD_HASHCODE:
	case XD_INT:
	case XD_LONG:
	case XD_INT_RESET:
	  break;
	case XD_OPAQUE_DATA_PTR:
	case XD_ASCII_STRING:
	case XD_BLOCK_PTR:
	case XD_LO_LINK:
	  {
	    EMACS_INT ptr = *(EMACS_INT *)rdata;
	    if (ptr)
	      *(EMACS_INT *)rdata = ptr+delta;
	    break;
	  }
	case XD_LISP_OBJECT:
	  {
	    Lisp_Object *pobj = (Lisp_Object *) rdata;

	    assert (desc1->data1 == 0);

	    if (POINTER_TYPE_P (XTYPE (*pobj))
		&& ! EQ (*pobj, Qnull_pointer))
	      *pobj = wrap_pointer_1 ((Rawbyte *) XPNTR (*pobj) + delta);

	    break;
	  }
	case XD_LISP_OBJECT_ARRAY:
	  {
	    EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
						     data);
	    int j;

	    for (j=0; j<num; j++)
	      {
		Lisp_Object *pobj = (Lisp_Object *) rdata + j;

		if (POINTER_TYPE_P (XTYPE (*pobj))
		    && ! EQ (*pobj, Qnull_pointer))
		  *pobj = wrap_pointer_1 ((Rawbyte *) XPNTR (*pobj) +
					  delta);
	      }
	    break;
	  }
	case XD_DOC_STRING:
	  {
	    EMACS_INT str = *(EMACS_INT *)rdata;
	    if (str > 0)
	      *(EMACS_INT *)rdata = str + delta;
	    break;
	  }
	case XD_BLOCK_ARRAY:
	  {
	    EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
						     data);
	    int j;
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);
	    Bytecount size = lispdesc_block_size (rdata, sdesc);

	    /* Note: We are recursing over data in the block itself */
	    for (j = 0; j < num; j++)
	      pdump_reloc_one ((Rawbyte *) rdata + j * size, delta,
			       sdesc->description);

	    break;
	  }
	case XD_UNION:
	case XD_UNION_DYNAMIC_SIZE:
	  desc1 = lispdesc_process_xd_union (desc1, desc, data);
	  if (desc1)
	    goto union_switcheroo;
	  break;

	case XD_OPAQUE_PTR_CONVERTIBLE:
	  {
	    pdump_cv_ptr_load_info *p = pdump_loaded_cv_ptr + *(EMACS_INT *)rdata;
	    if (!p->adr)
	      p->adr = desc1->data2.funcs->deconvert(0, pdump_start +
						     p->save_offset, p->size);
	    *(void **)rdata = p->adr;
	    break;
	  }

	case XD_OPAQUE_DATA_CONVERTIBLE:
	  {
	    EMACS_INT dest_offset = (Rawbyte *)rdata - pdump_start;
	    pdump_cv_data_dump_info *p;

	    for(p = pdump_loaded_cv_data; p->dest_offset != dest_offset; p++);

	    desc1->data2.funcs->deconvert(rdata, pdump_start + p->save_offset,
					  p->size);
	    break;
	  }

	default:
	  pdump_unsupported_dump_type (desc1->type, 0);
	}
    }
}
#endif /* not NEW_GC */

static void
pdump_allocate_offset (pdump_block_list_elt *elt,
		       const struct memory_description *UNUSED (desc))
{
  Bytecount size = elt->count * elt->size;
  elt->save_offset = cur_offset;
  if (size > max_size)
    max_size = size;
  cur_offset += size;
}

/* Write out to global file descriptor PDUMP_OUT the result of an
   external element.  It's just opaque data. */

static void
pdump_dump_cv_data (pdump_cv_data_info *elt)
{
  retry_fwrite (elt->data, elt->size, 1, pdump_out);
}

static void
pdump_dump_cv_ptr (pdump_cv_ptr_info *elt)
{
  retry_fwrite (elt->data, elt->size, 1, pdump_out);
}

static void
pdump_allocate_offset_cv_data (pdump_cv_data_info *elt)
{
  elt->save_offset = cur_offset;
  if (elt->size>max_size)
    max_size = elt->size;
  cur_offset += elt->size;
}

static void
pdump_allocate_offset_cv_ptr (pdump_cv_ptr_info *elt)
{
  elt->save_offset = cur_offset;
  if (elt->size>max_size)
    max_size = elt->size;
  cur_offset += elt->size;
}

/* Traverse through all the heap blocks, once the "register" stage of
   dumping has finished.  To compress space as much as possible, we
   logically sort all blocks by alignment, hitting all blocks with
   alignment == the maximum (which may be 8 bytes, for doubles), then
   all blocks with the next lower alignment (4 bytes), etc.

   Within each alignment we hit

   -- first the Lisp objects, type-by-type

   -- then the heap memory blocks that are not Lisp objects, description-by-
      description -- i.e. all blocks with the same description will be
      placed together

   -- then the "opaque" data objects declared as XD_OPAQUE_DATA_PTR,
      XD_ASCII_STRING and XD_DOC_STRING.

   The idea is to have as little blank space as possible in the laid-out
   data.

   For each item that we have hit, we process it by calling F, the function
   passed it.  In dumper.c, pdump_scan_by_alignment() is called twice with
   two different functions -- pdump_allocate_offset() in stage 2 to compute
   the offset to each block, and pdump_dump_data() in stage 3 to
   successively write each block to disk.

   It's extremely important that the SAME traversal order gets invoked
   in both stage 2 and 3.
*/

static void
pdump_scan_by_alignment (void (*f)(pdump_block_list_elt *,
				   const struct memory_description *),
			 void (*g)(pdump_cv_data_info *),
			 void (*h)(pdump_cv_ptr_info *))
{
  int align;

  for (align = ALIGNOF (max_align_t); align; align>>=1)
    {
      int i;
      pdump_block_list_elt *elt;

      for (i=0; i<lrecord_type_count; i++)
	if (pdump_object_table[i].align == align)
	  for (elt = pdump_object_table[i].first; elt; elt = elt->next)
	    f (elt, lrecord_implementations_table[i]->description);

      for (i=0; i<pdump_desc_table.count; i++)
	{
	  pdump_desc_list_elt list = pdump_desc_table.list[i];
	  if (list.list.align == align)
	    for (elt = list.list.first; elt; elt = elt->next)
	      f (elt, list.desc);
	}

      for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
	if (pdump_size_to_align (elt->size) == align)
	  f (elt, 0);

      for (i=0; i < Dynarr_length (pdump_cv_data); i++)
	if (pdump_size_to_align (Dynarr_atp (pdump_cv_data, i)->size) == align)
	  g (Dynarr_atp (pdump_cv_data, i));

      for (i=0; i < Dynarr_length (pdump_cv_ptr); i++)
	if (pdump_size_to_align (Dynarr_atp (pdump_cv_ptr, i)->size) == align)
	  h (Dynarr_atp (pdump_cv_ptr, i));
    }
}

static void
pdump_dump_cv_data_info (void)
{
  int i;
  Elemcount count = Dynarr_length (pdump_cv_data);
  pdump_cv_data_dump_info *data = alloca_array (pdump_cv_data_dump_info, count);
  for (i = 0; i < count; i++)
    {
      data[i].dest_offset = Dynarr_at (pdump_cv_data, i).dest_offset;
      data[i].save_offset = Dynarr_at (pdump_cv_data, i).save_offset;
      data[i].size        = Dynarr_at (pdump_cv_data, i).size;
    }

  PDUMP_ALIGN_OUTPUT (pdump_cv_data_dump_info);
  retry_fwrite (data, sizeof (pdump_cv_data_dump_info), count, pdump_out); 
}

static void
pdump_dump_cv_ptr_info (void)
{
  int i;
  Elemcount count = Dynarr_length (pdump_cv_ptr);
  pdump_cv_ptr_dump_info *data = alloca_array (pdump_cv_ptr_dump_info, count);
  for (i = 0; i < count; i++)
    {
      data[i].save_offset = Dynarr_at (pdump_cv_ptr, i).save_offset;
      data[i].size        = Dynarr_at (pdump_cv_ptr, i).size;
    }

  PDUMP_ALIGN_OUTPUT (pdump_cv_ptr_dump_info);
  retry_fwrite (data, sizeof (pdump_cv_ptr_dump_info), count, pdump_out); 
}

/* Dump out the root block pointers, part of stage 3 (the "WRITE" stage) of
   dumping.  For each pointer we dump out a structure containing the
   location of the pointer and its value, replaced by the appropriate
   offset into the dumped data. */

static void
pdump_dump_root_block_ptrs (void)
{
  int i;
  Elemcount count = Dynarr_length (pdump_root_block_ptrs);
  pdump_static_pointer *data = alloca_array (pdump_static_pointer, count);
  for (i = 0; i < count; i++)
    {
      data[i].address =
	(Rawbyte **) Dynarr_atp (pdump_root_block_ptrs, i)->ptraddress;
      data[i].value   =
	(Rawbyte *) pdump_get_block (* data[i].address)->save_offset;
    }
  PDUMP_ALIGN_OUTPUT (pdump_static_pointer);
  retry_fwrite (data, sizeof (pdump_static_pointer), count, pdump_out);
}

/* Dump out the root blocks, part of stage 3 (the "WRITE" stage) of
   dumping.  For each block we dump a structure containing info about the
   block (its location, size and description) and then the block itself,
   with its pointers replaced with offsets into the dump data. */

static void
pdump_dump_root_blocks (void)
{
  int i;
  for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
    {
      pdump_root_block info = Dynarr_at (pdump_root_blocks, i);
      PDUMP_WRITE_ALIGNED (pdump_root_block, info);

      if (info.desc)
	{
	  /* Copy to temporary buffer */
	  memcpy (pdump_buf, info.blockaddr, info.size);

	  /* Store new offsets into all pointers in block */
	  pdump_store_new_pointer_offsets (1, pdump_buf, info.blockaddr,
					   info.desc, info.size);
	}
      retry_fwrite (info.desc ? pdump_buf : info.blockaddr,
		    info.size, 1, pdump_out);
    }
}

static void
pdump_dump_rtables (void)
{
  int i;
  pdump_block_list_elt *elt;
  pdump_reloc_table rt;

  for (i=0; i<lrecord_type_count; i++)
    {
      elt = pdump_object_table[i].first;
      if (!elt)
	continue;
      rt.desc = lrecord_implementations_table[i]->description;
      rt.count = pdump_object_table[i].count;
      PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
      while (elt)
	{
	  EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset;
#ifdef NEW_GC
	  int j;
	  for (j=0; j<elt->count; j++)
	    {
	      PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
	      rdata += elt->size;
	    }
#else /* not NEW_GC */
	  PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
#endif /* not NEW_GC */
	  elt = elt->next;
	}
    }

  rt.desc = 0;
  rt.count = 0;
  PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);

  for (i=0; i<pdump_desc_table.count; i++)
    {
      elt = pdump_desc_table.list[i].list.first;
      rt.desc = pdump_desc_table.list[i].desc;
      rt.count = pdump_desc_table.list[i].list.count;
      PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
      while (elt)
	{
	  EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset;
	  int j;
	  for (j=0; j<elt->count; j++)
	    {
	      PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
	      rdata += elt->size;
	    }
	  elt = elt->next;
	}
    }
  rt.desc = 0;
  rt.count = 0;
  PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
}

static void
pdump_dump_root_lisp_objects (void)
{
  Elemcount count = (Dynarr_length (pdump_root_lisp_objects) +
			 Dynarr_length (pdump_weak_object_chains));
  Elemcount i;

  PDUMP_WRITE_ALIGNED (Elemcount, count);
  PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object);

  for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++)
    {
      pdump_static_Lisp_Object obj;
      obj.address = Dynarr_at (pdump_root_lisp_objects, i);
      obj.value   = * obj.address;

      if (POINTER_TYPE_P (XTYPE (obj.value)))
	obj.value =
	  wrap_pointer_1 ((void *) pdump_get_block (XRECORD_LHEADER
						    (obj.value))->save_offset);

      PDUMP_WRITE (pdump_static_Lisp_Object, obj);
    }

  for (i = 0; i < Dynarr_length (pdump_weak_object_chains); i++)
    {
      pdump_block_list_elt *elt;
      pdump_static_Lisp_Object obj;

      obj.address = Dynarr_at (pdump_weak_object_chains, i);
      obj.value   = * obj.address;

      for (;;)
	{
	  const struct memory_description *desc;
	  int pos;
	  elt = pdump_get_block (XRECORD_LHEADER (obj.value));
	  if (elt)
	    break;
	  desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description;
	  for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
	    assert (desc[pos].type != XD_END);

	  /* #### Figure out how to handle indirect offsets here. */
	  assert (!XD_IS_INDIRECT (desc[pos].offset));
	  obj.value =
	    * (Lisp_Object *) (desc[pos].offset +
			       (Rawbyte *) (XRECORD_LHEADER (obj.value)));
	}
      obj.value = wrap_pointer_1 ((void *) elt->save_offset);

      PDUMP_WRITE (pdump_static_Lisp_Object, obj);
    }
}


/*########################################################################
  #                             Pdump                                    #
  ########################################################################

  [ben]

  DISCUSSION OF DUMPING:

  The idea of dumping is to record the state of XEmacs in a file, so that
  it can be reloaded later.  This avoids having to reload all of the basic
  Lisp code each time XEmacs is run, which is a rather time-consuming
  process.  (Less so on new machines, but still noticeable.  As an example
  of a program with similar issues but which does not have a dumping
  process and as a result has a slow startup time, consider Adobe Photoshop
  5.0 or Adobe Photoshop Elements 2.0.)

  We don't actually record ALL the state of XEmacs (some of it, for example,
  is dependent on the run-time environment and needs to be initialized
  whenever XEmacs is run), but whatever state we don't record needs to be
  reinitialized every time XEmacs is run.

  The old way of dumping was to make a new executable file with the data
  segment expanded to contain the heap and written out from memory.  This
  is what the unex* files do.  Unfortunately this process is extremely
  system-specific and breaks easily with OS changes.

  Another simple, more portable trick, the "static heap" method, involves
  replacing the allocator with our own allocator which allocates all space
  out of a very large array declared in our data segment until we run out,
  then uses the underlying malloc() to start allocating on the heap.  If we
  ensure that the large array is big enough to hold all data allocated
  during the dump stage, then all of the data we need to save is in the
  data segment, and it's easy to calculate the location and size of the
  data segment we want to save (we don't want to record and reinitialize
  the data segment of library functions) by using appropriately declared
  variables in the first and last file linked.  This method is known as the
  "static heap" method, and is used by the non-pdump version of the dumper
  under Cygwin, and was also used under VMS and in Win-Emacs.

  The "static heap" method works well in practice.  Nonetheless, a more
  complex method of dumping was written by Olivier Galibert, which requires
  that structural descriptions of all data allocated in the heap be provided
  and the roots of all pointers into the heap be noted through function calls
  to the pdump API.  This way, all the heap data can be traversed and written
  out to a file, and then reloaded at run-time and the pointers relocated to
  point at the new location of the loaded data.  This is the "pdump" method
  used in this file.

  There are two potential advantages of "pdump" over the "static heap":

  (1) It doesn't require any tricks to calculate the beginning and end of
      the data segment, or even that the XEmacs section of the data segment
      be contiguous. (It's not clear whether this is an issue in practice.)
  (2) Potentially, it could handle an OS that does not always load the
      static data segment at a predictable location.  The "static heap"
      method by its nature needs the data segment to stay in the same place
      from invocation to invocation, since it simply dumps out memory and
      reloads it, without any pointer relocation.  I say "potentially"
      because as it is currently written pdump does assume that the data
      segment is never relocated.  However, changing pdump to remove this
      assumption is probably not difficult, as all the mechanism to handle
      pointer relocation is already present.


  DISCUSSION OF PDUMP WORKINGS:

  See man/internals/internals.texi for more information.

  NOTE that we have two kinds of memory to handle: memory on the heap
  (i.e. allocated through malloc()) or the like, and static memory in the
  data segment of the program, i.e. stuff declared as global or static.
  All heap memory needs to be written out to the dump file and reproduced
  (i.e. reloaded and any necessary relocations performed).  Data-segment
  memory that is not statically initialized (i.e. through declarations in
  the C code) needs either to be written out and reloaded, or
  reinitialized.  In addition, any pointers in data-segment memory to heap
  memory must be written out, reloaded and relocated.

  NOTE that we currently don't handle relocation of pointers into data-
  segment memory. (See overview discussion above.) These are treated in
  the descriptions as opaque data not needing relocation.  If this becomes a
  problem, it can be fixed through new kinds of types in
  enum memory_description_type.

  Three basic steps to dumping out:

  (1) "REGISTER":
      Starting with all sources of relocatable memory (currently this means
      all data-segment pointers to heap memory -- see above about pointers
      to data-segment memory), recursively traverse the tree of pointers
      and "register" (make a note of) every memory block seen.

  (2) "LAYOUT":
      Go through all of the registered blocks and compute the location of
      each one in the dump data (i.e. the "offset" that will be added to
      the address corresponding to start of the loaded-in data to get the
      new pointer referring to this block).  The blocks will be laid out
      sequentially according to the order we traverse them.  Also note the
      maximum-sized block for use in step 3.

  (3) "WRITE":
      After writing some header stuff, go through all of the registered
      blocks and write out each one to the dump file.  Note that we are
      simply writing out the blocks sequentially as we see them, and our
      traversal path is identical to that in step 2, so blocks will end up
      at the locations computed for them.  In order to write out a block,
      first copy it to a temporary location (hence the maximum-block-size
      computation in the previous step), then for each relocatable pointer
      in the block, write in its place the offset to the heap block in the
      dump data.  When the dump data is loaded, the address of the
      beginning of the dump data will be added to the offset in each
      pointer, and thence become accurate.

  --ben
*/

void
pdump (void)
{
  int i;
  Lisp_Object t_console, t_device, t_frame;
  int none;
  pdump_header header;

  in_pdump = 1;

  pdump_object_table = xnew_array (pdump_block_list, lrecord_type_count);
  pdump_alert_undump_object = xnew_array (int, lrecord_type_count);

  assert (ALIGNOF (max_align_t) <= pdump_align_table[0]);

  for (i = 0; i < countof (pdump_align_table); i++)
    if (pdump_align_table[i] > ALIGNOF (max_align_t))
      pdump_align_table[i] = ALIGNOF (max_align_t);

  flush_all_buffer_local_cache ();

  /* These appear in a DEFVAR_LISP, which does a staticpro() */
  t_console = Vterminal_console; Vterminal_console = Qnil;
  t_frame   = Vterminal_frame;   Vterminal_frame   = Qnil;
  t_device  = Vterminal_device;  Vterminal_device  = Qnil;

  dump_add_opaque (&lrecord_implementations_table,
		   lrecord_type_count *
		   sizeof (lrecord_implementations_table[0]));
#ifdef USE_KKCC  
  dump_add_opaque (&lrecord_memory_descriptions,
		   lrecord_type_count 
		   * sizeof (lrecord_memory_descriptions[0]));
#else /* not USE_KKCC */
  dump_add_opaque (&lrecord_markers,
		   lrecord_type_count * sizeof (lrecord_markers[0]));
#endif /* not USE_KKCC */

  pdump_hash = xnew_array_and_zero (pdump_block_list_elt *, PDUMP_HASHSIZE);

  for (i = 0; i<lrecord_type_count; i++)
    {
      pdump_object_table[i].first = 0;
      pdump_object_table[i].align = ALIGNOF (max_align_t);
      pdump_object_table[i].count = 0;
      pdump_alert_undump_object[i] = 0;
    }
  pdump_desc_table.count = 0;
  pdump_desc_table.size = -1;

  pdump_opaque_data_list.first = 0;
  pdump_opaque_data_list.align = ALIGNOF (max_align_t);
  pdump_opaque_data_list.count = 0;
  pdump_depth = 0;

  pdump_cv_data = Dynarr_new2 (pdump_cv_data_info_dynarr, pdump_cv_data_info);
  pdump_cv_ptr  = Dynarr_new2 (pdump_cv_ptr_info_dynarr,  pdump_cv_ptr_info);

  /* (I) The "register" stage: Note all heap memory blocks to be relocated
     */

  /* Try various roots of accessibility: */

  /* (1) Lisp objects, both those declared using DEFVAR_LISP*() and  those
         staticpro()d. */
  for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++)
    pdump_register_object (* Dynarr_at (pdump_root_lisp_objects, i));

  none = 1;
  for (i = 0; i < lrecord_type_count; i++)
    if (pdump_alert_undump_object[i])
      {
	if (none)
	  stderr_out ("Undumpable types list :\n");
	none = 0;
	stderr_out ("  - %s (%d)\n", lrecord_implementations_table[i]->name,
		    pdump_alert_undump_object[i]);
      }
  if (!none)
    {
      in_pdump = 0;
      return;
    }

  /* (2) Register out the data-segment pointer variables to heap blocks */
  for (i = 0; i < Dynarr_length (pdump_root_block_ptrs); i++)
    {
      pdump_root_block_ptr info = Dynarr_at (pdump_root_block_ptrs, i);
      pdump_register_block (*(info.ptraddress), info.desc->size,
			    info.desc->description, 1);
    }

  /* (3) Register out the data-segment blocks, maybe with pointers to heap
     blocks */
  for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
    {
      pdump_root_block *info = Dynarr_atp (pdump_root_blocks, i);
      if (info->desc)
	{
	  /* Size may have been given as 0 meaning "compute later".
	     Compute now and update.  If no DESC, size must always be
	     correct as there is no other way of computing it. */
	  info->size = lispdesc_block_size_1 (info->blockaddr, info->size,
					      info->desc);
	  pdump_register_block_contents (info->blockaddr, info->size,
					 info->desc, 1);
	}
    }

  /* (II) The "layout" stage: Compute the offsets and max-size */

  /* (1) Determine header size */
  memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN);
  header.id = dump_id;
  header.reloc_address = 0;
  header.nb_root_block_ptrs = Dynarr_length (pdump_root_block_ptrs);
  header.nb_root_blocks = Dynarr_length (pdump_root_blocks);
  header.nb_cv_data = Dynarr_length (pdump_cv_data);
  header.nb_cv_ptr =  Dynarr_length (pdump_cv_ptr);

  cur_offset = MAX_ALIGN_SIZE (sizeof (header));
  max_size = 0;

  /* (2) Traverse all heap blocks and compute their offsets; keep track
         of maximum block size seen */
  pdump_scan_by_alignment (pdump_allocate_offset,
			   pdump_allocate_offset_cv_data,
			   pdump_allocate_offset_cv_ptr);
  cur_offset = MAX_ALIGN_SIZE (cur_offset);
  header.stab_offset = cur_offset;

  /* (3) Update maximum size based on root (data-segment) blocks */
  for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
    {
      pdump_root_block info = Dynarr_at (pdump_root_blocks, i);

      /* If no DESC, no relocation needed and we copy directly instead of
	 into a temp buffer. */
      if (info.desc)
	{
	  if (info.size > max_size)
	    max_size = info.size;
	}
    }

  /* (III) The "write "stage: Dump out the data, storing the offsets in
           place of pointers whenever we write out memory blocks */

  pdump_buf = xmalloc (max_size);
  /* EMACS_PROGNAME is entirely ASCII so this should be Mule-safe */
  pdump_fd = open (EMACS_PROGNAME ".dmp",
		   O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666);
  if (pdump_fd < 0)
    report_file_error ("Unable to open dump file",
		       build_ascstring (EMACS_PROGNAME ".dmp"));
  pdump_out = fdopen (pdump_fd, "w");
  if (pdump_out < 0)
    report_file_error ("Unable to open dump file for writing",
		       build_ascstring (EMACS_PROGNAME ".dmp"));

  retry_fwrite (&header, sizeof (header), 1, pdump_out);
  PDUMP_ALIGN_OUTPUT (max_align_t);

  for (i = 0; i < Dynarr_length (pdump_cv_data); i++)
    {
      pdump_cv_data_info *elt = Dynarr_atp (pdump_cv_data, i);
      elt->dest_offset =
	pdump_get_block (elt->object)->save_offset + elt->offset;
    }

  for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++)
    Dynarr_at (pdump_cv_ptr, i).index = i;

  pdump_scan_by_alignment (pdump_dump_data, pdump_dump_cv_data, pdump_dump_cv_ptr);

  for (i = 0; i < Dynarr_length (pdump_cv_data); i++)
    {
      pdump_cv_data_info *elt = Dynarr_atp (pdump_cv_data, i);
      if(elt->fcts->convert_free)
	elt->fcts->convert_free(elt->object, elt->data, elt->size);
    }

  for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++)
    {
      pdump_cv_ptr_info *elt = Dynarr_atp (pdump_cv_ptr, i);
      if(elt->fcts->convert_free)
	elt->fcts->convert_free(elt->object, elt->data, elt->size);
    }

  fseek (pdump_out, header.stab_offset, SEEK_SET);

#ifdef NEW_GC
  {
    EMACS_INT zero = 0;
    pdump_scan_lisp_objects_by_alignment (pdump_dump_mc_data);
    PDUMP_WRITE_ALIGNED (EMACS_INT, zero);
    pdump_scan_non_lisp_objects_by_alignment (pdump_dump_mc_data);
    PDUMP_WRITE_ALIGNED (EMACS_INT, zero);
  }
#endif /* NEW_GC */
  pdump_dump_cv_data_info ();
  pdump_dump_cv_ptr_info ();
#ifdef NEW_GC
  pdump_dump_rtables ();
#endif /* NEW_GC */
  pdump_dump_root_block_ptrs ();
  pdump_dump_root_blocks ();
#ifndef NEW_GC
  pdump_dump_rtables ();
#endif /* not NEW_GC */
  pdump_dump_root_lisp_objects ();

  retry_fclose (pdump_out);
  /* pdump_fd is already closed by the preceding call to fclose.
  retry_close (pdump_fd); */

  free (pdump_buf);

  free (pdump_hash);

  Vterminal_console = t_console;
  Vterminal_frame   = t_frame;
  Vterminal_device  = t_device;
  in_pdump = 0;
}

static int
pdump_load_check (void)
{
  return (!memcmp (((pdump_header *) pdump_start)->signature,
		   PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN)
	  && ((pdump_header *)pdump_start)->id == dump_id);
}

/*----------------------------------------------------------------------*/
/*			Reading the dump file				*/
/*----------------------------------------------------------------------*/
static int
pdump_load_finish (void)
{
  int i;
  Rawbyte *p;
  EMACS_INT delta;
  EMACS_INT count;
  pdump_header *header = (pdump_header *) pdump_start;

#ifdef NEW_GC
  /* This is a DEFVAR_BOOL and gets dumped, but the actual value was
     already determined by vdb_install_signal_handler () in
     vdb-mprotect.c, which could be different from the value in the
     dump file. So store it here and restore it after loading the dump
     file. */
  int allow_inc_gc = allow_incremental_gc;
#endif /* NEW_GC */
  pdump_end = pdump_start + pdump_length;

  delta = ((EMACS_INT) pdump_start) - header->reloc_address;
  p = pdump_start + header->stab_offset;

#ifdef NEW_GC
  pdump_mc_hash = xnew_array_and_zero (mc_addr_elt, PDUMP_HASHSIZE);

  /* Allocate space for each object individually. First the
     Lisp_Objects, then the blocks. */
  count = 2;
  for (;;)
    {
      EMACS_INT elt_count = PDUMP_READ_ALIGNED (p, EMACS_INT);
      if (elt_count)
	{
	  Rawbyte *mc_addr = 0;
	  Bytecount size = PDUMP_READ_ALIGNED (p, Bytecount);
	  for (i = 0; i < elt_count; i++)
	    {
	      EMACS_INT rdata = PDUMP_READ_ALIGNED (p, EMACS_INT);
	  
	      if (i == 0)
		{
		  Bytecount real_size = size * elt_count;
		  if (count == 2)
		    {
		      if (elt_count <= 1)
			mc_addr = (Rawbyte *) mc_alloc (real_size);
		      else 
			mc_addr = (Rawbyte *) mc_alloc_array (size, elt_count);
#ifdef ALLOC_TYPE_STATS
		      inc_lrecord_stats (real_size, 
					 (const struct lrecord_header *) 
					 ((Rawbyte *) rdata + delta));
#endif /* ALLOC_TYPE_STATS */
		    }
		  else
		    mc_addr = (Rawbyte *) xmalloc_and_zero (real_size);
		}
	      else
		mc_addr += size;
	  
	      pdump_put_mc_addr ((void *) rdata, (EMACS_INT) mc_addr);
	      memcpy (mc_addr, (Rawbyte *) rdata + delta, size);
	    }
	}
      else if (!(--count))
	break;
    }
#endif /* NEW_GC */

  /* Get the cv_data array */
  p = (Rawbyte *) ALIGN_PTR (p, pdump_cv_data_dump_info);
  pdump_loaded_cv_data = (pdump_cv_data_dump_info *)p;
  p += header->nb_cv_data*sizeof(pdump_cv_data_dump_info);

  /* Build the cv_ptr array */
  p = (Rawbyte *) ALIGN_PTR (p, pdump_cv_ptr_dump_info);
  pdump_loaded_cv_ptr =
    alloca_array (pdump_cv_ptr_load_info, header->nb_cv_ptr);
  for (i = 0; i < header->nb_cv_ptr; i++)
    {
      pdump_cv_ptr_dump_info info = PDUMP_READ (p, pdump_cv_ptr_dump_info);
      pdump_loaded_cv_ptr[i].save_offset = info.save_offset;
      pdump_loaded_cv_ptr[i].size        = info.size;
      pdump_loaded_cv_ptr[i].adr         = 0;
    }

#ifdef NEW_GC
  /* Relocate the heap objects */
  pdump_rt_list = p;
  count = 2;
  for (;;)
    {
      pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
      p = (Rawbyte *) ALIGN_PTR (p, Rawbyte *);
      if (rt.desc)
	{
	  Rawbyte **reloc = (Rawbyte **) p;
	  for (i = 0; i < rt.count; i++)
	    {
	      reloc[i] = (Rawbyte *) pdump_get_mc_addr (reloc[i]);
	      pdump_reloc_one_mc (reloc[i], rt.desc);
	    }
	  p += rt.count * sizeof (Rawbyte *);
	}
      else if (!(--count))
	  break;
    }
#endif /* NEW_GC */

  /* Put back the pdump_root_block_ptrs */
  p = (Rawbyte *) ALIGN_PTR (p, pdump_static_pointer);
  for (i = 0; i < header->nb_root_block_ptrs; i++)
    {
      pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer);
#ifdef NEW_GC
      (* ptr.address) = (Rawbyte *) pdump_get_mc_addr (ptr.value);
#else /* not NEW_GC */
      (* ptr.address) = ptr.value + delta;
#endif /* not NEW_GC */
    }

  /* Put back the pdump_root_blocks and relocate */
  for (i = 0; i < header->nb_root_blocks; i++)
    {
      pdump_root_block info = PDUMP_READ_ALIGNED (p, pdump_root_block);
      memcpy ((void *) info.blockaddr, p, info.size);
      if (info.desc)
#ifdef NEW_GC
	pdump_reloc_one_mc ((void *) info.blockaddr, info.desc);
#else /* not NEW_GC */
	pdump_reloc_one ((void *) info.blockaddr, delta, info.desc);
#endif /* not NEW_GC */
      p += info.size;
    }

#ifndef NEW_GC
  /* Relocate the heap objects */
  pdump_rt_list = p;
  count = 2;
  for (;;)
    {
      pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
      p = (Rawbyte *) ALIGN_PTR (p, Rawbyte *);
      if (rt.desc)
	{
	  Rawbyte **reloc = (Rawbyte **) p;
	  for (i = 0; i < rt.count; i++)
	    {
	      reloc[i] += delta;
	      pdump_reloc_one (reloc[i], delta, rt.desc);
	    }
	  p += rt.count * sizeof (Rawbyte *);
	}
      else if (!(--count))
	  break;
    }
#endif /* not NEW_GC */

  /* Put the pdump_root_lisp_objects variables in place */
  i = PDUMP_READ_ALIGNED (p, Elemcount);
  p = (Rawbyte *) ALIGN_PTR (p, pdump_static_Lisp_Object);
  while (i--)
    {
      pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object);

      if (POINTER_TYPE_P (XTYPE (obj.value)))
#ifdef NEW_GC
	obj.value = wrap_pointer_1 ((Rawbyte *) pdump_get_mc_addr 
				    (XPNTR (obj.value)));
#else /* not NEW_GC */
        obj.value = wrap_pointer_1 ((Rawbyte *) XPNTR (obj.value) + delta);
#endif /* not NEW_GC */

      (* obj.address) = obj.value;
    }

  /* Final cleanups */
  /*   reorganize hash tables */
  p = pdump_rt_list;
  for (;;)
    {
      pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
      p = (Rawbyte *) ALIGN_PTR (p, Lisp_Object);
      if (!rt.desc)
	break;
      if (rt.desc == hash_table_description)
	{
	  for (i = 0; i < rt.count; i++)
	    pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object));
	  break;
	}
      else
	p += sizeof (Lisp_Object) * rt.count;
    }

#ifdef NEW_GC
  xfree (pdump_mc_hash);
#endif /* NEW_GC */

#ifdef NEW_GC
  allow_incremental_gc = allow_inc_gc;
#endif /* NEW_GC */

  return 1;
}

#ifdef WIN32_NATIVE
/* Free the mapped file if we decide we don't want it after all */
static void
pdump_file_unmap (void)
{
  UnmapViewOfFile (pdump_start);
  CloseHandle (pdump_hFile);
  CloseHandle (pdump_hMap);
}

static int
pdump_file_get (const Wexttext *wpath)
{
  Extbyte *path;
  if (XEUNICODE_P)
    path = (Extbyte *) wpath;
  else
    path = WEXTTEXT_TO_MULTIBYTE (wpath);

  pdump_hFile =
    qxeCreateFile (path,
		   GENERIC_READ + GENERIC_WRITE,  /* Required for copy on
						     write */
		   0,		            /* Not shared */
		   NULL,		    /* Not inheritable */
		   OPEN_EXISTING,
		   FILE_ATTRIBUTE_NORMAL,
		   NULL);		    /* No template file */
  if (pdump_hFile == INVALID_HANDLE_VALUE)
    return 0;

  pdump_length = GetFileSize (pdump_hFile, NULL);
  pdump_hMap =
    qxeCreateFileMapping (pdump_hFile,
			  NULL,           /* No security attributes */
			  PAGE_WRITECOPY, /* Copy on write */
			  0,              /* Max size, high half */
			  0,              /* Max size, low half */
			  NULL);          /* Unnamed */
  if (pdump_hMap == INVALID_HANDLE_VALUE)
    return 0;

  pdump_start =
    (Rawbyte *) MapViewOfFile (pdump_hMap,
				   FILE_MAP_COPY, /* Copy on write */
				   0,	      /* Start at zero */
				   0,
				   0);	      /* Map all of it */
  pdump_free = pdump_file_unmap;
  return 1;
}

/* pdump_resource_free is called (via the pdump_free pointer) to release
   any resources allocated by pdump_resource_get.  Since the Windows API
   specs specifically state that you don't need to (and shouldn't) free the
   resources allocated by FindResource, LoadResource, and LockResource this
   routine does nothing.  */
static void
pdump_resource_free (void)
{
}

static int
pdump_resource_get (void)
{
  HRSRC hRes;			/* Handle to dump resource */
  HRSRC hResLoad;		/* Handle to loaded dump resource */

  /* See Q126630 which describes how Windows NT and 95 trap writes to
     resource sections and duplicate the page to allow the write to proceed.
     It also describes how to make the resource section read/write (and hence
     private to each process).  Doing this avoids the exceptions and related
     overhead, but causes the resource section to be private to each process
     that is running XEmacs.  Since the resource section contains little
     other than the dumped data, which should be private to each process, we
     make the whole resource section read/write so we don't have to copy it. */

  hRes = FindResourceA (NULL, MAKEINTRESOURCE (101), "DUMP");
  if (hRes == NULL)
    return 0;

  /* Found it, use the data in the resource */
  hResLoad = (HRSRC) LoadResource (NULL, hRes);
  if (hResLoad == NULL)
    return 0;

  pdump_start = (Rawbyte *) LockResource (hResLoad);
  if (pdump_start == NULL)
    return 0;

  pdump_free = pdump_resource_free;
  pdump_length = SizeofResource (NULL, hRes);
  if (pdump_length <= (Bytecount) sizeof (pdump_header))
    {
      pdump_start = 0;
      return 0;
    }

  return 1;
}

#else /* !WIN32_NATIVE */

static void
pdump_file_free (void)
{
  xfree (pdump_start);
}

#ifdef HAVE_MMAP
static void
pdump_file_unmap (void)
{
  munmap (pdump_start, pdump_length);
}
#endif

static int
pdump_file_get (const Wexttext *path)
{
  int fd = wext_retry_open (path, O_RDONLY | OPEN_BINARY);
  if (fd < 0)
    return 0;

  pdump_length = lseek (fd, 0, SEEK_END);
  if (pdump_length < (Bytecount) sizeof (pdump_header))
    {
      retry_close (fd);
      return 0;
    }

  lseek (fd, 0, SEEK_SET);

#ifdef HAVE_MMAP
/* Unix 98 requires that sys/mman.h define MAP_FAILED,
   but many earlier implementations don't. */
# ifndef MAP_FAILED
#  define MAP_FAILED ((void *) -1L)
# endif
  pdump_start =
    (Rawbyte *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE,
			  fd, 0);
  if (pdump_start != (Rawbyte *) MAP_FAILED)
    {
      pdump_free = pdump_file_unmap;
      retry_close (fd);
      return 1;
    }
#endif /* HAVE_MMAP */

  pdump_start = xnew_array (Rawbyte, pdump_length);
  pdump_free = pdump_file_free;
  retry_read (fd, pdump_start, pdump_length);

  retry_close (fd);
  return 1;
}

#ifdef DUMP_IN_EXEC
static int
pdump_ram_try (void)
{
  pdump_start = dumped_data_get ();
  pdump_length = dumped_data_size ();

  return pdump_load_check ();
}
#endif

#endif /* !WIN32_NATIVE */


static int
pdump_file_try (Wexttext *exe_path)
{
  Wexttext *w = exe_path + wext_strlen (exe_path);

  /* We look for various names, including those with the version and dump ID,
     those with just the dump ID, and those without either.  We first try
     adding directly to the executable name, then lopping off any extension
     (e.g. .exe) or version name in the executable (xemacs-21.5.18). */
  do
    {
      wext_sprintf (w, WEXTSTRING ("-%s-%08x.dmp"), WEXTSTRING (EMACS_VERSION),
		    dump_id);
      if (pdump_file_get (exe_path))
	{
	  if (pdump_load_check ())
	    return 1;
	  pdump_free ();
	}

      wext_sprintf (w, WEXTSTRING ("-%08x.dmp"), dump_id);
      if (pdump_file_get (exe_path))
	{
	  if (pdump_load_check ())
	    return 1;
	  pdump_free ();
	}

      wext_sprintf (w, WEXTSTRING (".dmp"));
      if (pdump_file_get (exe_path))
	{
	  if (pdump_load_check ())
	    return 1;
	  pdump_free ();
	}

      do
	w--;
      /* !!#### See comment below about how this is unsafe. */
      while (w > exe_path && !IS_DIRECTORY_SEP (*w) && (*w != '-') &&
	     (*w != '.'));
    }
  while (w > exe_path && !IS_DIRECTORY_SEP (*w));
  return 0;
}

#define DUMP_SLACK 100 /* Enough to include dump ID, version name, .DMP */

int
pdump_load (const Wexttext *argv0)
{
#ifdef WIN32_NATIVE
  Wexttext *exe_path = NULL;
  int bufsize = 4096;
  int cchpathsize;

  /* Copied from mswindows_get_module_file_name ().  Not clear if it's
     kosher to malloc() yet. */
  while (1)
    {
      exe_path = alloca_array (Wexttext, bufsize);
      cchpathsize = qxeGetModuleFileName (NULL, (Extbyte *) exe_path,
					  bufsize);
      if (!cchpathsize)
	goto fail;
      if (cchpathsize + DUMP_SLACK <= bufsize)
	break;
      bufsize *= 2;
    }

  if (!XEUNICODE_P)
    {
      Wexttext *wexe = MULTIBYTE_TO_WEXTTEXT ((Extbyte *) exe_path);
      wext_strcpy (exe_path, wexe);
    }
#else /* !WIN32_NATIVE */
  Wexttext *exe_path;
  Wexttext *w;
  const Wexttext *dir, *p;

#ifdef DUMP_IN_EXEC
  if (pdump_ram_try ())
    {
      pdump_load_finish ();
      in_pdump = 0;
      return 1;
    }
#endif

  in_pdump = 1;
  dir = argv0;
  if (dir[0] == '-')
    {
      /* XEmacs as a login shell, oh goody! */
      dir = wext_getenv ("SHELL"); /* not egetenv -- not yet initialized and we
				      want external-format data */
    }

  p = dir + wext_strlen (dir);
  /* !!#### This is bad as it may fail with certain non-ASCII-compatible
     external formats such as JIS.  Maybe we should be using the mb*()
     routines in libc?  But can we reliably trust them on all Unix
     platforms?  (We can't convert to internal since those conversion
     routines aren't yet initialized) */
  while (p != dir && !IS_ANY_SEP (p[-1]))
    p--;

  if (p != dir)
    {
      /* invocation-name includes a directory component -- presumably it
	 is relative to cwd, not $PATH. */
      exe_path = alloca_array (Wexttext, 1 + wext_strlen (dir) + DUMP_SLACK);
      wext_strcpy (exe_path, dir);
    }
  else
    {
      const Wexttext *path = wext_getenv ("PATH"); /* not egetenv --
                                                      not yet init. */
      const Wexttext *name = p;
      exe_path = alloca_array (Wexttext,
			       1 + DUMP_SLACK + max (wext_strlen (name),
                                                     wext_strlen (path)));
      for (;;)
	{
	  p = path;
	  while (*p && *p != SEPCHAR)
	    p++;
	  if (p == path)
	    {
	      exe_path[0] = '.';
	      w = exe_path + 1;
	    }
	  else
	    {
	      memcpy (exe_path, path, (p - path) * sizeof (Wexttext));
	      w = exe_path + (p - path);
	    }
	  if (!IS_DIRECTORY_SEP (w[-1]))
	    *w++ = '/';
	  wext_strcpy (w, name);

	  {
	    struct stat statbuf;
	    if (wext_access (exe_path, X_OK) == 0
		&& wext_stat (exe_path, &statbuf) == 0
		&& ! S_ISDIR (statbuf.st_mode))
	      break;
	  }

	  if (!*p)
	    {
	      /* Oh well, let's have some kind of default */
	      wext_sprintf (exe_path, "./%s", name);
	      break;
	    }
	  path = p + 1;
	}
    }
#endif /* WIN32_NATIVE */

  if (pdump_file_try (exe_path))
    {
      pdump_load_finish ();
      in_pdump = 0;
#ifdef NEW_GC
      pdump_free ();
#endif /* NEW_GC */
      return 1;
    }

#ifdef WIN32_NATIVE
  if (pdump_resource_get ())
    {
      if (pdump_load_check ())
	{
	  pdump_load_finish ();
	  in_pdump = 0;
#ifdef NEW_GC
	  pdump_free ();
#endif /* NEW_GC */
	  return 1;
	}
      pdump_free ();
    }

fail:
#endif

  in_pdump = 0;
  return 0;
}