File: Dkbox.c

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

#include "Dk.h"
#include <assert.h>

/*#define DV_UNAME_UNIT_DEBUG*/
/*#define DV_UNAME_STATS*/

#ifndef DOUBLE_ALIGN
#error boxes must be aligned at 8
#endif

#ifdef _DEBUG
long box_types_alloc[256];	/* implicit zero-fill assumed */
long box_types_free[256];	/* implicit zero-fill assumed */
#endif

/* Hashtable of UNAMEs */

static dk_mutex_t *uname_mutex;


#if 0
#define UNAME_TABLE_SIZE 61
#define UNAME_LOCK_REFCOUNT 16
#else
#define UNAME_TABLE_SIZE 8191
#define UNAME_LOCK_REFCOUNT 256
#endif

typedef struct uname_chain_pair_s
{
  uname_blk_t *	unc_immortals;
  uname_blk_t *	unc_refcounted;
#ifdef DV_UNAME_UNIT_DEBUG
  long unc_count;
  long unc_refcount;
#endif

} uname_chain_pair_t;

static uname_chain_pair_t unames[UNAME_TABLE_SIZE];

uint32
big_endian_box_length (const void *box)
{
  const unsigned char *ptr = (const unsigned char *) box - 4;
  return ptr[0] + ((uint32) ptr[1] << 8) + ((uint32) ptr[2] << 16);
}


#ifdef DV_UNAME_UNIT_DEBUG
static void
box_dv_uname_audit_one (uint32 cpair_idx)
{
  uname_chain_pair_t *cpair;
  long ctr = 0, refctr = 0;
  uint32 hash;
  uname_blk_t *blk;
  cpair_idx = cpair_idx % UNAME_TABLE_SIZE;
  cpair = unames + cpair_idx;
  for (blk = cpair->unc_immortals; NULL != blk; blk = blk->unb_next)
    {
      ctr++;
      refctr += UNAME_LOCK_REFCOUNT;
      if (UNAME_LOCK_REFCOUNT > blk->unb_hdr[UNB_HDR_REFCTR])
	GPF_T1 ("Too small refcount in immortal");
#ifdef MALLOC_DEBUG
      if (DV_UNAME != box_tag (blk->unb_data_ptr))
	GPF_T1 ("non-UNAME in immortal unames");
      /* dk_check_tree_iter (blk->unb_data_ptr, NULL, known); */
      BYTE_BUFFER_HASH (hash, blk->unb_data_ptr, box_length_inline (blk->unb_data_ptr) - 1);
#else
      BYTE_BUFFER_HASH (hash, blk->unb_data, box_length_inline (blk->unb_data) - 1);
#endif
      if ((hash % UNAME_TABLE_SIZE) != cpair_idx)
	GPF_T1 ("Bad hash");
    }
  for (blk = cpair->unc_refcounted; NULL != blk; blk = blk->unb_next)
    {
      ctr++;
      refctr += blk->unb_hdr[UNB_HDR_REFCTR];
      if (0 >= blk->unb_hdr[UNB_HDR_REFCTR])
	GPF_T1 ("Negative refcount");
      if (UNAME_LOCK_REFCOUNT <= blk->unb_hdr[UNB_HDR_REFCTR])
	GPF_T1 ("Big refcount but not immortal");
#ifdef MALLOC_DEBUG
      if (DV_UNAME != box_tag (blk->unb_data_ptr))
	GPF_T1 ("non-UNAME in immortal unames");
      /* dk_check_tree_iter (blk->unb_data_ptr, NULL, known); */
      BYTE_BUFFER_HASH (hash, blk->unb_data_ptr, box_length_inline (blk->unb_data_ptr) - 1);
#else
      BYTE_BUFFER_HASH (hash, blk->unb_data, box_length_inline (blk->unb_data) - 1);
#endif
      if ((hash % UNAME_TABLE_SIZE) != cpair_idx)
	GPF_T1 ("Bad hash");
    }
  if (refctr != cpair->unc_refcount)
    GPF_T1 ("Mismatch in unc_refcount");
  if (ctr != cpair->unc_count)
    GPF_T1 ("Mismatch in unc_count");
}


static void
box_dv_uname_audit_table (void)
{
  uint32 idx;
  for (idx = 0; idx < UNAME_TABLE_SIZE; idx++)
    box_dv_uname_audit_one (idx);
}


#else
#define box_dv_uname_audit_one(CPAIR_IDX)
#define box_dv_uname_audit_table()
#endif

#ifdef MALLOC_DEBUG
#define dk_alloc_mmap(sz) dk_alloc (sz)
#define dk_free_munmap(ptr, sz) dk_free (ptr, sz)
#else

void *
dk_mmap_brk (size_t sz)
{
  return mm_large_alloc (sz);
}

#define dk_alloc_mmap(sz) \
  ((sz) >= box_min_mmap && (sz) < 0xffffff ? (caddr_t) dk_mmap_brk (sz) : dk_alloc (sz))
#define dk_free_munmap(ptr, sz)						\
  ((sz) >= box_min_mmap && (sz) < 0xffffff ? mm_free_sized (ptr, sz) : dk_free (ptr, sz))
#endif

#ifndef NDEBUG
#define dk_alloc_box_check_length(bytes,tag) do { \
  if ((bytes) & ~0xffffff) \
    GPF_T1 ("box to allocate is too large"); \
  switch ((tag)) { \
    case DV_SHORT_STRING_SERIAL: case DV_LONG_WIDE: \
      GPF_T1 ("misused tag of box to allocate"); \
      break; \
    case DV_WIDE: \
      if ((bytes) % sizeof (wchar_t)) \
        GPF_T1 ("DV_WIDE of length that is not divisible on sizeof (wchar_t)"); \
      if (0 == (bytes)) \
        GPF_T1 ("DV_WIDE with no room for trailing zero"); \
      break; \
    case DV_STRING: case DV_C_STRING: \
      if (0 == (bytes)) \
        GPF_T1 ("String with no room for trailing zero"); \
      break; \
    } \
} while (0)
#else
#define dk_alloc_box_check_length(bytes,tag) do { ; } while (0)
#endif


size_t box_min_mmap = (1024 * 100 ) - 8;
#undef dk_alloc_box
box_t
dk_alloc_box (size_t bytes, dtp_t tag)
{
  unsigned char *ptr;
  size_t align_bytes;
  dk_alloc_box_check_length (bytes, tag);
  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  ptr = (unsigned char *) dk_alloc_mmap (align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

#ifdef _DEBUG
  box_types_alloc[(unsigned) tag]++;
#endif
  WRITE_BOX_HEADER (ptr, bytes, tag);
  return (box_t) ptr;
}


#undef dk_alloc_box_long
box_t
dk_alloc_box_long (size_t bytes, dtp_t tag)
{
  unsigned char *ptr;
  size_t align_bytes;

#ifdef MALLOC_DEBUG
  if (bytes > 1000000000)
    GPF_T1 ("Box over 1G, suspect, assertion in malloc debug mode only");
#endif

  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  ptr = (unsigned char *) dk_alloc_mmap (align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

#ifdef _DEBUG
  box_types_alloc[(unsigned) tag]++;
#endif
  if (bytes > 0xffffff)
    bytes = 0xffffff;				 /* safety.  If overflowed, large box would be confused with small, now only the length is off, which is OK if length known elsewhere.  Like in cluster message serialization  */
  WRITE_BOX_HEADER (ptr, bytes, tag);
  return (box_t) ptr;
}


#undef dk_try_alloc_box
box_t
dk_try_alloc_box (size_t bytes, dtp_t tag)
{
  unsigned char *ptr = NULL;
  size_t align_bytes;

  dk_alloc_box_check_length (bytes, tag);
  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  if (align_bytes >= box_min_mmap)
    ptr = (unsigned char *) dk_alloc_mmap (align_bytes);
  else
  ptr = (unsigned char *) dk_try_alloc (align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef _DEBUG
  box_types_alloc[(unsigned) tag]++;
#endif

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

  WRITE_BOX_HEADER (ptr, bytes, tag);
  /* memset (ptr, 0, bytes); */
  return (box_t) ptr;
}


#undef dk_alloc_box_zero
box_t
dk_alloc_box_zero (size_t bytes, dtp_t tag)
{
  unsigned char *ptr;
  size_t align_bytes;
  dk_alloc_box_check_length (bytes, tag);
  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  ptr = (unsigned char *) dk_alloc_mmap (align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

#ifdef _DEBUG
  box_types_alloc[tag]++;
#endif

  WRITE_BOX_HEADER (ptr, bytes, tag);
  memset (ptr, 0, bytes);

  return (box_t) ptr;
}


#ifdef MALLOC_DEBUG
box_t
dbg_dk_alloc_box (DBG_PARAMS size_t bytes, dtp_t tag)
{
  unsigned char *ptr;
  uint32 align_bytes;
  dk_alloc_box_check_length (bytes, tag);
  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  ptr = (unsigned char *) dbg_malloc (file, line, align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

#ifdef _DEBUG
  box_types_alloc[tag]++;
#endif

  WRITE_BOX_HEADER (ptr, bytes, tag);
  /* memset (ptr, 0x00, bytes); */

  return (box_t) ptr;
}


box_t
dbg_dk_alloc_box_long (DBG_PARAMS size_t bytes, dtp_t tag)
{
  unsigned char *ptr;
  uint32 align_bytes;
  dk_alloc_box_check_length (bytes, tag);
  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  ptr = (unsigned char *) dbg_malloc (file, line, align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

#ifdef _DEBUG
  box_types_alloc[tag]++;
#endif
  if (bytes > 0xffffff)
    bytes = 0xffffff;				 /* safety.  If overflowed, large box would be confused with small, now only the length is off, which is OK if length known elsewhere.  Like in cluster message serialization  */

  WRITE_BOX_HEADER (ptr, bytes, tag);
  /* memset (ptr, 0x00, bytes); */

  return (box_t) ptr;
}


box_t
dbg_dk_try_alloc_box (DBG_PARAMS size_t bytes, dtp_t tag)
{
  unsigned char *ptr;
  uint32 align_bytes;
  dk_alloc_box_check_length (bytes, tag);
  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  ptr = (unsigned char *) dbg_malloc (file, line, align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

#ifdef _DEBUG
  box_types_alloc[tag]++;
#endif

  WRITE_BOX_HEADER (ptr, bytes, tag);
  /* memset (ptr, 0x00, bytes); */

  return (box_t) ptr;
}


box_t
dbg_dk_alloc_box_zero (DBG_PARAMS size_t bytes, dtp_t tag)
{
  unsigned char *ptr;
  uint32 align_bytes;
  dk_alloc_box_check_length (bytes, tag);
  /* This assumes dk_alloc aligns at least at 4 */
#ifdef DOUBLE_ALIGN
  align_bytes = 8 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_8 (bytes));
#else
  align_bytes = 4 + (IS_STRING_ALIGN_DTP (tag) ? ALIGN_STR (bytes) : ALIGN_4 (bytes));
#endif

  ptr = (unsigned char *) dbg_malloc (file, line, align_bytes);
  if (!ptr)
    return (box_t) ptr;

#ifdef DOUBLE_ALIGN
  ptr += 4;
#endif

#ifdef _DEBUG
  box_types_alloc[tag]++;
#endif

  WRITE_BOX_HEADER (ptr, bytes, tag);
  memset (ptr, 0x0, bytes);

  return (box_t) ptr;
}
#endif /* MALLOC_DEBUG */


box_destr_f box_destr[256];
box_copy_f box_copier[256];
box_tmp_copy_f box_tmp_copier[256];
char box_can_appear_twice_in_tree[256];


caddr_t
box_non_copiable (caddr_t b)
{
  return NULL;
}


caddr_t
box_copy_non_box (caddr_t b)
{
  GPF_T;
  return NULL;
}


caddr_t
box_mp_copy_non_box (mem_pool_t * mp, caddr_t b)
{
  GPF_T;
  return NULL;
}


void
dk_mem_hooks (dtp_t tag, box_copy_f c, box_destr_f d, int bcatit)
{
  if (box_destr[tag] && d && d != box_destr[tag]) GPF_T1 ("redefining mem hooks");
  box_destr[tag] = d;
  box_copier[tag] = c;
  box_tmp_copier[tag] = NULL;
  box_can_appear_twice_in_tree[tag] = bcatit;
}


void
dk_mem_hooks_2 (dtp_t tag, box_copy_f c, box_destr_f d, int bcatit, box_tmp_copy_f t_c)
{
  if (box_destr[tag] && d && d != box_destr[tag]) GPF_T1 ("redefining mem hooks");
  box_destr[tag] = d;
  box_copier[tag] = c;
  box_can_appear_twice_in_tree[tag] = bcatit;
  box_tmp_copier[tag] = t_c;
}


int
dk_free_box (box_t box)
{
  uint32 len;
  dtp_t *ptr;
  dtp_t tag;

  if (!IS_BOX_POINTER (box))
    return 0;

  ptr = (unsigned char *) box;

  len = box_length_inline (ptr);
  tag = box_tag (ptr);

  switch (tag)
    {
    case DV_WIDE:
#ifndef NDEBUG
      if ((len % sizeof (wchar_t)) || (0 != ((wchar_t *)box)[len/sizeof (wchar_t) - 1]))
        GPF_T1 ("Free of a damaged wide string");
#endif
#ifdef DOUBLE_ALIGN
      len = ALIGN_8 (len);
#else
      len = ALIGN_4 (len);
#endif
      break;
    case DV_STRING:
    case DV_C_STRING:
    case DV_SHORT_STRING_SERIAL:
    case DV_SYMBOL:
    case DV_BIN:
      len = ALIGN_STR (len);
      break;

    case DV_UNAME:
      {
	uint32 hash;
	uname_chain_pair_t *cpair;
	uname_blk_t *blk;
#ifdef MALLOC_DEBUG
	uname_blk_t **blk_ptr;
	mutex_enter (uname_mutex);
	BYTE_BUFFER_HASH (hash, box, len - 1);
	cpair = unames + (hash % UNAME_TABLE_SIZE);
	for (blk = cpair->unc_immortals; NULL != blk; blk = blk->unb_next)
	  {
	    if (blk->unb_data_ptr != box)
	      continue;
	    mutex_leave (uname_mutex);
	    return 0;
	  }
#ifdef DV_UNAME_UNIT_DEBUG
	cpair->unc_refcount--;
#endif
	for (blk_ptr = &(cpair->unc_refcounted); (NULL != (blk = blk_ptr[0])); blk_ptr = &(blk->unb_next))
	  {
	    if (blk->unb_data_ptr != box)
	      continue;
	    if (0 < (--(blk->unb_hdr[UNB_HDR_REFCTR])))
	      {
		box_dv_uname_audit_one (hash);
		mutex_leave (uname_mutex);
		return 0;
	      }
#ifdef DV_UNAME_UNIT_DEBUG
	    cpair->unc_count--;
#endif
	    blk_ptr[0] = blk->unb_next;
#ifndef DV_UNAME_KEEP_ALL
	    box_tag_modify_impl (box, DV_NULL);
	    dk_free_box (box);
	    dk_free (blk, sizeof (uname_blk_t));
#endif
	    box_dv_uname_audit_one (hash);
	    mutex_leave (uname_mutex);
	    return 0;
	  }
	GPF_T1 ("Can't free broken UNAME");
#else
	blk = UNAME_TO_UNAME_BLK (box);
	if (UNAME_LOCK_REFCOUNT <= blk->unb_hdr[UNB_HDR_REFCTR])
	  return 0;
	mutex_enter (uname_mutex);
#ifdef DV_UNAME_UNIT_DEBUG
	unames[blk->unb_hdr[UNB_HDR_HASH] % UNAME_TABLE_SIZE].unc_refcount--;
#endif
	if ((UNAME_LOCK_REFCOUNT <= blk->unb_hdr[UNB_HDR_REFCTR]) ||
	    (0 < (--(blk->unb_hdr[UNB_HDR_REFCTR]))))
	  {
	    box_dv_uname_audit_one (blk->unb_hdr[UNB_HDR_HASH]);
	    mutex_leave (uname_mutex);
	    return 0;
	  }
	hash = blk->unb_hdr[UNB_HDR_HASH];
	cpair = unames + (hash % UNAME_TABLE_SIZE);
#ifdef DV_UNAME_UNIT_DEBUG
	cpair->unc_count--;
#endif
	if (blk == cpair->unc_refcounted)
	  cpair->unc_refcounted = blk->unb_next;
	else
	  {
	    uname_blk_t *prev_in_chain = cpair->unc_refcounted;
	    while (prev_in_chain->unb_next != blk)
	      prev_in_chain = prev_in_chain->unb_next;
	    prev_in_chain->unb_next = blk->unb_next;
	  }
#ifndef DV_UNAME_KEEP_ALL
	dk_free (blk, sizeof (uname_blk_t) + (len - sizeof (ptrlong)));
#endif
	box_dv_uname_audit_one (hash);
	mutex_leave (uname_mutex);
#endif
	return 0;
      }

    case DV_REFERENCE:
      return 0;

    case TAG_FREE:
      GPF_T1 ("Double free");

    case TAG_BAD:
      GPF_T1 ("free of box marked bad");

    default:
      if (box_destr[tag])
	if (0 != box_destr[tag] (box))
	  return 0;
#ifdef DOUBLE_ALIGN
      len = ALIGN_8 (len);
#else
      len = ALIGN_4 (len);
#endif
    }

#ifndef NDEBUG
  ptr[-1] = TAG_FREE;
#endif

#ifdef DOUBLE_ALIGN
#ifdef MALLOC_DEBUG
  if (len >= 0xffffff)
    {
      dbg_free_sized (__FILE__, __LINE__, ptr - 8, len);
      return 0;
    }
#endif
  dk_free_munmap (ptr - 8, len + 8);
#else
  dk_free_munmap (ptr - 4, len + 4);
#endif
#ifdef _DEBUG
  box_types_free[tag]++;
#endif
  return (0);
}


void
dkbox_terminate_module (void)
{
  int uname_cpair_ctr;
  for (uname_cpair_ctr = UNAME_TABLE_SIZE; uname_cpair_ctr--; /* no step */ )
    {
      uname_chain_pair_t *cpair = unames + uname_cpair_ctr;
      while (NULL != cpair->unc_immortals)
	{
	  uname_blk_t *first_imm = cpair->unc_immortals;
	  cpair->unc_immortals = first_imm->unb_next;
	  first_imm->unb_hdr[UNB_HDR_REFCTR] = 1;
	  first_imm->unb_next = cpair->unc_refcounted;
	  cpair->unc_refcounted = first_imm;
	}
      while (NULL != cpair->unc_refcounted)
	{
	  cpair->unc_refcounted->unb_hdr[UNB_HDR_REFCTR] = 1;
#ifdef MALLOC_DEBUG
	  dk_free_box (cpair->unc_refcounted->unb_data_ptr);
#else
	  dk_free_box (cpair->unc_refcounted->unb_data);
#endif
	}
    }
}

#if defined (WIN32) || defined (SOLARIS)
#define __builtin_prefetch(m)
#endif

int
dk_free_tree (box_t box)
{
  uint32 len;
  dtp_t *ptr;
  dtp_t tag;

  if (!IS_BOX_POINTER (box))
    return 0;

  ptr = (unsigned char *) box;

  len = box_length_inline (ptr);
  tag = box_tag (ptr);

  switch (tag)
    {
    case DV_WIDE:
#ifndef NDEBUG
      if ((len % sizeof (wchar_t)) || (0 != ((wchar_t *)box)[len/sizeof (wchar_t) - 1]))
        GPF_T1 ("Free of a tree with a damaged wide string");
#endif
#ifdef DOUBLE_ALIGN
      len = ALIGN_8 (len);
#else
      len = ALIGN_4 (len);
#endif
      break;
    case DV_STRING:
    case DV_C_STRING:
    case DV_SHORT_STRING_SERIAL:
    case DV_SYMBOL:
    case DV_BIN:
      len = ALIGN_STR (len);
      break;

#ifdef SIGNAL_DEBUG
    case DV_ERROR_REPORT:
      log_error_report_event (box, 0, "FREE");
#endif
    case DV_ARRAY_OF_POINTER:
    case DV_LIST_OF_POINTER:
    case DV_ARRAY_OF_XQVAL:
    case DV_XTREE_HEAD:
    case DV_XTREE_NODE:
      {
	uint32 count = len / sizeof (box_t), inx = 0;
	box_t *obj = (box_t *) box;
	if (count > 3)
	  {
	    for (inx = 0; inx < count - 3; inx += 2)
	      {
		__builtin_prefetch (obj[inx + 2]);
		__builtin_prefetch (obj[inx + 3]);
		dk_free_tree (obj[inx]);
		dk_free_tree (obj[inx + 1]);
	      }
	  }
	for (inx = inx; inx < count; inx++)
	  dk_free_tree (obj[inx]);
#ifdef MALLOC_DEBUG
	if (len != ALIGN_4 (len))
	  GPF_T;
#endif
	break;
      }

    case DV_UNAME:
      dk_free_box (box);
      return 0;

    case DV_REFERENCE:
      return 0;

#ifndef NDEBUG
    case TAG_FREE:
      GPF_T1 ("Double free");

    case TAG_BAD:
      GPF_T1 ("Free of box marked bad");
#endif

    default:
      if (box_destr[tag])
	if (0 != box_destr[tag] (box))
	  return 0;
#ifdef DOUBLE_ALIGN
      len = ALIGN_8 (len);
#else
      len = ALIGN_4 (len);
#endif
    }

#ifndef NDEBUG
  ptr[-1] = TAG_FREE;
#endif

#ifdef DOUBLE_ALIGN
  dk_free_munmap (ptr - 8, len + 8);
#else
  dk_free_munmap (ptr - 4, len + 4);
#endif
#ifdef _DEBUG
  box_types_free[tag]++;
#endif

  return (0);

}


void
box_reuse (caddr_t box, ccaddr_t data, size_t len, dtp_t dtp)
{
  dk_alloc_box_assert (box);
#ifndef NDEBUG
  if (DV_WIDE == DV_TYPE_OF (box))
    {
      if ((len % sizeof (wchar_t)) || (0 != ((wchar_t *)box)[len/sizeof (wchar_t) - 1]))
        GPF_T1 ("Reuse of a damaged wide string");
    }
  if (DV_WIDE == dtp)
    {
      if ((len % sizeof (wchar_t)) || (0 != ((wchar_t *)data)[len/sizeof (wchar_t) - 1]))
        GPF_T1 ("Reuse of a box for a damaged wide string");
    }
#endif
  box_tag_modify (box, dtp);
  ((dtp_t *) box)[-4] = (dtp_t) (len & 0xff);
  ((dtp_t *) box)[-3] = (dtp_t) (len >> 8);
  ((dtp_t *) box)[-2] = (dtp_t) (len >> 16);
  if (DV_STRING == dtp) len --; /* the length of string box is always +1 but actual data is one char less */
  if (box != data)
  memcpy (box, data, len);
}

#ifdef DK_ALLOC_BOX_DEBUG
void
dk_check_tree_iter (box_t box, box_t parent, dk_hash_t **known_ptr)
{
  uint32 count;
  dtp_t tag;
  if (!IS_BOX_POINTER (box))
    return;
  dk_alloc_box_assert (box);
  tag = box_tag (box);
  if ((DV_UNAME == tag) || (DV_REFERENCE == tag))
    return;
  if (TAG_FREE == tag)
    GPF_T1 ("Tree contains a pointer to a freed box");
  if (TAG_BAD == tag)
    GPF_T1 ("Tree contains a pointer to a box marked bad");
  if (known_ptr && !box_can_appear_twice_in_tree[tag])
    {
      box_t other_parent;
      if (NULL == known_ptr[0])
        known_ptr[0] = hash_table_allocate (4096);
      other_parent = (box_t) gethash (box, known_ptr[0]);
      if (NULL != other_parent)
	GPF_T;
      sethash (box, known_ptr[0], parent);
    }
  if (IS_NONLEAF_DTP (tag))
    {
      box_t *obj = (box_t *) box;
      int len = box_length (box) / sizeof (box_t);
      for (count = 0; count < len; count++)
        dk_check_tree_iter (obj[count], box, known_ptr);
    }
  return;
}

void
dk_check_tree (box_t box)
{
  dk_hash_t *known = NULL;
  dk_check_tree_iter (box, BADBEEF_BOX, &known);
  if (NULL != known)
    hash_table_free (known);
}

void
dk_check_tree_mp_or_plain_iter (box_t box, box_t parent, dk_hash_t ** known_ptr, int *fuel_ptr)
{
  uint32 count;
  dtp_t tag;
  if (!IS_BOX_POINTER (box))
    return;
  dk_alloc_box_assert_mp_or_plain (box);
  tag = box_tag (box);
  if ((DV_UNAME == tag) || (DV_REFERENCE == tag))
    return;
  if (TAG_FREE == tag)
    GPF_T1 ("Tree contains a pointer to a freed box");
  if (TAG_BAD == tag)
    GPF_T1 ("Tree contains a pointer to a box marked bad");
  if (known_ptr && !box_can_appear_twice_in_tree[tag] && !(box_flags (box) & BF_VALID_JSO))
    {
      box_t other_parent;
      if (NULL == known_ptr[0])
        known_ptr[0] = hash_table_allocate (MIN(fuel_ptr[0], 4096));
      other_parent = (box_t)gethash (box, known_ptr[0]);
      if (NULL != other_parent)
        GPF_T;
      sethash (box, known_ptr[0], parent);
    }
  if (IS_NONLEAF_DTP (tag))
    {
      box_t *obj = (box_t *) box;
      int len = box_length_inline (box) / sizeof (box_t);
      dk_hash_t ** sub_known_ptr = ((box_flags (box) & BF_VALID_JSO) ? NULL : known_ptr);
      for (count = 0; count < len; count++)
        {
          fuel_ptr[0]--;
          if (0 >= fuel_ptr[0])
            return;
          dk_check_tree_mp_or_plain_iter (obj[count], box, sub_known_ptr, fuel_ptr);
        }
    }
#ifdef VALGRIND
  else
    {
      int badbeefcount = 0;
      box_t *obj = (box_t *) box;
      int len = box_length_inline (box) / sizeof (box_t);
      dk_hash_t ** sub_known_ptr = ((box_flags (box) & BF_VALID_JSO) ? NULL : known_ptr);
      for (count = 0; count < len; count++)
        {
          if (obj[count] == BADBEEF_BOX)
            badbeefcount++;
        }
      if (badbeefcount)
        GPF_T1 ("BADBEEF in data");
    }
#endif
  return;
}

void
dk_check_tree_mp_or_plain (box_t box, int max_fuel)
{
  dk_hash_t *known = NULL;
  int fuel = max_fuel;
  if (!IS_BOX_POINTER (box))
    return;
  dk_check_tree_mp_or_plain_iter (box, BADBEEF_BOX, &known, &fuel);
  if (NULL != known)
    hash_table_free (known);
}

void
dk_check_tree_heads_iter (box_t box, box_t parent, dk_hash_t * known, int count_of_sample_children)
{
  uint32 count;
  dtp_t tag;
  if (!IS_BOX_POINTER (box))
    return;
  dk_alloc_box_assert (box);
  tag = box_tag (box);
  if ((DV_UNAME == tag) || (DV_REFERENCE == tag))
    return;
  if (TAG_FREE == tag)
    GPF_T1 ("Tree contains a pointer to a freed box");
  if (TAG_BAD == tag)
    GPF_T1 ("Tree contains a pointer to a box marked bad");
  if (tag < FIRST_DV_DTP)
    GPF_T1 ("Tree contains a pointer to a Box with weird tag");
  if (!box_can_appear_twice_in_tree[tag])
    {
      box_t other_parent = (box_t)gethash (box, known);
      if (NULL != other_parent)
	GPF_T;
      sethash (box, known, parent);
    }
  if (IS_NONLEAF_DTP (tag))
    {
      box_t *obj = (box_t *) box;
      count = box_length (box) / sizeof (box_t);
      if (count > count_of_sample_children)
        count = count_of_sample_children;
      for (/* no init*/; count; count--)
	dk_check_tree_heads_iter (*obj++, box, known, count_of_sample_children);
    }
  return;
}

void dk_check_tree_heads (box_t box, int count_of_sample_children)
{
  dk_hash_t *known;
  if (!IS_BOX_POINTER (box))
    return;
  known = hash_table_allocate (4096);
  dk_check_tree_heads_iter (box, BADBEEF_BOX, known, count_of_sample_children);
  hash_table_free (known);
}

void
dk_check_domain_of_connectivity_iter (box_t box, box_t parent, dk_hash_t ** known_ptr)
{
  uint32 count;
  dtp_t tag;
  box_t other_parent;
  if (!IS_BOX_POINTER (box))
    return;
  dk_alloc_box_assert (box);
  tag = box_tag (box);
  if ((DV_UNAME == tag) || (DV_REFERENCE == tag))
    return;
  if (TAG_FREE == tag)
    GPF_T1 ("Domain of connectivity contains a pointer to a freed box");
  if (TAG_BAD == tag)
    GPF_T1 ("Domain of connectivity contains a pointer to a box marked bad");
  if (IS_NONLEAF_DTP (tag))
    {
      box_t *obj = (box_t *) box;
      for (count = box_length_inline (box) / sizeof (box_t); count; count--)
	dk_check_domain_of_connectivity_iter (*obj++, box, known_ptr);
    }
  if (NULL == known_ptr[0])
    known_ptr[0] = hash_table_allocate (4096);
  other_parent = (box_t)gethash (box, known_ptr[0]);
  if (NULL != other_parent)
    return;
  sethash (box, known_ptr[0], parent);
  return;
}

void
dk_check_domain_of_connectivity (box_t box)
{
  dk_hash_t *known = NULL;
  dk_check_domain_of_connectivity_iter (box, BADBEEF_BOX, &known);
  if (NULL != known)
  hash_table_free (known);
}
#endif

/*
 * Free the box.
 * If the box is an array of pointer, free all number boxes
 * DV_LONG_INT referenced from it.
 */
int
dk_free_box_and_numbers (box_t box)
{
  if (IS_BOX_POINTER (box))
    {
      unsigned int tag = box_tag (box);	/* TAMMI mty */
      if (tag == TAG_FREE)
	return (0);
      if (IS_NONLEAF_DTP (tag))
	{
	  unsigned int n, tg;	/* TAMMI mty 2 below */
	  unsigned int length = box_length ((caddr_t) box) / sizeof (caddr_t);
	  /* box_tag (box) = TAG_BEING_FREED; */
	  for (n = 0; n < length; n++)
	    {
	      caddr_t data = ((caddr_t *) box)[n];
	      if (IS_BOX_POINTER (data) &&
		  ((tg = box_tag (data)) == DV_LONG_INT ||
		    tg == DV_C_STRING ||
		    tg == DV_DOUBLE_FLOAT ||
		    tg == DV_SINGLE_FLOAT))
		dk_free_box (data);
	    };
	  dk_free_box (box);
	}
      else
	dk_free_box (box);
    };
  return (0);
}


int
dk_free_box_and_int_boxes (box_t box)
{
  uint32 count;
  dtp_t tag;

  if (!IS_BOX_POINTER (box))
    return 0;

  tag = box_tag (box);
  if (IS_NONLEAF_DTP (tag))
    {
      box_t *obj = (box_t *) box;
      for (count = BOX_ELEMENTS (box); count; count--)
	{
	  if (IS_BOX_POINTER (*obj) && box_tag (*obj) == DV_LONG_INT)
	    dk_free_box (*obj);
	  obj++;
	}
    }

  dk_free_box (box);

  return 0;
}


/* Number Boxes */

boxint
unbox (ccaddr_t box)
{
  if (!IS_BOX_POINTER (box))
    return (boxint) (ptrlong) box;

  if (box_tag (box) == DV_LONG_INT)
    return *(boxint *) box;

  return (boxint) (ptrlong) box;
}


ptrlong
unbox_ptrlong (ccaddr_t box)
{
  if (!IS_BOX_POINTER (box))
    return (ptrlong) box;

  if (box_tag (box) == DV_LONG_INT)
    {
      boxint bi = *(boxint *) box;
#ifdef DEBUG
      if ((sizeof (ptrlong) < sizeof (boxint)))
	{
	  boxint upper_bits = bi >> (8 * sizeof (ptrlong));
	  if ((0 != upper_bits) && (-1 != upper_bits))
	    GPF_T1 ("ptrlong overflow in unbox_ptrlong");
	}
#endif
      return bi;
    }
  return (boxint) ((ptrlong) box);
}


int64
unbox_int64 (ccaddr_t box)
{
  if (!IS_BOX_POINTER (box))
    return (int64) (ptrlong) box;

  if (box_tag (box) == DV_LONG_INT)
    return *(boxint *) box;
  return (ptrlong) box;
}

box_t
DBG_NAME (dk_realloc_list) (DBG_PARAMS caddr_t ptr, int new_elements)
{
  int old_elements;
  caddr_t res;
  if (NULL == ptr)
    return DBG_NAME (dk_alloc_box_zero) (DBG_ARGS new_elements * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
  old_elements = BOX_ELEMENTS (ptr);
  if (old_elements == new_elements)
    return ptr;
  res = DBG_NAME (dk_alloc_box_zero) (DBG_ARGS new_elements * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
  memcpy (res, ptr, ((new_elements < old_elements) ? new_elements : old_elements) * sizeof (caddr_t));
  dk_free_box (ptr);
  return (box_t) res;
}

box_t
DBG_NAME (box_num) (DBG_PARAMS boxint n)
{
  box_t *box;
  if (!IS_BOXINT_POINTER (n))
    return (box_t) (ptrlong) n;
  box = (box_t *) DBG_NAME (dk_alloc_box) (DBG_ARGS sizeof (boxint), DV_LONG_INT);
  *(boxint *) box = n;
  return (box_t) (ptrlong) box;
}


box_t
DBG_NAME (box_num_nonull) (DBG_PARAMS boxint n)
{
  box_t *box;
  if (n && !IS_BOXINT_POINTER (n))
    return (box_t) (ptrlong) n;
  box = (box_t *) DBG_NAME (dk_alloc_box) (DBG_ARGS sizeof (boxint), DV_LONG_INT);
  *(boxint *) box = n;
  return (box_t) box;
}


box_t
DBG_NAME (box_iri_id) (DBG_PARAMS int64 n)
{
  iri_id_t * box = (iri_id_t*) DBG_NAME (dk_alloc_box) (DBG_ARGS sizeof (iri_id_t), DV_IRI_ID);
  *box = n;
  return (caddr_t) box;
}




/*
 * Box a null-terminated string into a
 * DV_<XX>_STRING tagged box
 */
#undef box_string

box_t
DBG_NAME (box_string) (DBG_PARAMS const char *string)
{
  uint32 len;
  box_t box;

  if (!string)
    return NULL;

  len = (uint32) strlen (string) + 1;
  box = DBG_NAME (dk_alloc_box) (DBG_ARGS len, DV_C_STRING);
  memcpy (box, string, len);

  return box;
}


box_t
DBG_NAME (box_copy) (DBG_PARAMS cbox_t box)
{
  dtp_t tag;
  uint32 len;
  box_t copy;

  if (!IS_BOX_POINTER (box))
    return (box_t) box;

  tag = box_tag (box);
  switch (tag)
    {
    case DV_WIDE:
#ifndef NDEBUG
      len = box_length (box);
      if ((len % sizeof (wchar_t)) || (0 != ((wchar_t *)box)[len/sizeof (wchar_t) - 1]))
        GPF_T1 ("Copy of a damaged wide string");
#endif
      break;
    case DV_STRING:
    case DV_ARRAY_OF_POINTER:
    case DV_LIST_OF_POINTER:
    case DV_ARRAY_OF_XQVAL:
    case DV_XTREE_HEAD:
    case DV_XTREE_NODE:
      break;

    case DV_UNAME:
      {
	uint32 hash;
	uname_chain_pair_t *cpair;
	uname_blk_t *blk;
#ifdef MALLOC_DEBUG
	uname_blk_t **blk_ptr;
	len = box_length (box) - 1;
	BYTE_BUFFER_HASH (hash, box, len);
	cpair = unames + (hash % UNAME_TABLE_SIZE);
	mutex_enter (uname_mutex);
	for (blk = cpair->unc_immortals; NULL != blk; blk = blk->unb_next)
	  {
	    if (blk->unb_data_ptr == box)
	      {
		mutex_leave (uname_mutex);
		return (box_t) box;
	      }
	  }
#ifdef DV_UNAME_UNIT_DEBUG
	cpair->unc_refcount++;
#endif
	for (blk_ptr = &cpair->unc_refcounted; (NULL != (blk = blk_ptr[0])); blk_ptr = &(blk->unb_next))
	  {
	    if (blk->unb_data_ptr != box)
	      continue;
	    if (UNAME_LOCK_REFCOUNT <= (++(blk->unb_hdr[UNB_HDR_REFCTR])))
	      {
		blk_ptr[0] = blk->unb_next;
		blk->unb_next = cpair->unc_immortals;
		cpair->unc_immortals = blk;
		blk->unb_hdr[UNB_HDR_REFCTR] = UNAME_LOCK_REFCOUNT;
	      }
	    box_dv_uname_audit_one (hash);
	    mutex_leave (uname_mutex);
	    return (box_t) box;
	  }
	GPF_T1 ("Can't copy broken UNAME");
#else
	blk = UNAME_TO_UNAME_BLK (box);
	if (UNAME_LOCK_REFCOUNT <= blk->unb_hdr[UNB_HDR_REFCTR])
	  return (box_t) box;
#ifdef DV_UNAME_UNIT_DEBUG
	unames[blk->unb_hdr[UNB_HDR_HASH] % UNAME_TABLE_SIZE].unc_refcount++;
#endif
	mutex_enter (uname_mutex);
	if (UNAME_LOCK_REFCOUNT <= blk->unb_hdr[UNB_HDR_REFCTR])
	  {
	    /* See if it is already immortal inside the mtx.  Else two threads can decide to make it immortal one after the other and the second gpfs cause the uname is no longer in the refcounted list */
	    mutex_leave (uname_mutex);
	    return (box_t) box;
	  }

	if (UNAME_LOCK_REFCOUNT > (++(blk->unb_hdr[UNB_HDR_REFCTR])))
	  {
	    box_dv_uname_audit_one (blk->unb_hdr[UNB_HDR_HASH]);
	    mutex_leave (uname_mutex);
	    return (box_t) box;
	  }
	hash = blk->unb_hdr[UNB_HDR_HASH];
	cpair = unames + (hash % UNAME_TABLE_SIZE);
	if (blk == cpair->unc_refcounted)
	  cpair->unc_refcounted = blk->unb_next;
	else
	  {
	    uname_blk_t *prev_in_chain = cpair->unc_refcounted;
	    while (prev_in_chain->unb_next != blk)
	      prev_in_chain = prev_in_chain->unb_next;
	    prev_in_chain->unb_next = blk->unb_next;
	  }
	blk->unb_next = cpair->unc_immortals;
	cpair->unc_immortals = blk;
	box_dv_uname_audit_one (hash);
	mutex_leave (uname_mutex);
#endif
	return (box_t) box;
      }

    case DV_REFERENCE:
      return (box_t) box;

#ifndef NDEBUG
    case TAG_FREE:
      GPF_T1 ("Copy of a freed box");

    case TAG_BAD:
      GPF_T1 ("Copy of a box marked bad");
#endif

    default:
#ifdef MALLOC_DEBUG
      if (tag < FIRST_DV_DTP)
        GPF_T1 ("Copy of a box with weird tag");
#endif
      if (box_copier[tag])
	return (box_copier[tag] ((caddr_t) box));
    }
  len = box_length (box);
  copy = DBG_NAME (dk_alloc_box) (DBG_ARGS len, tag);
  box_flags (copy) = box_flags (box);
  memcpy (copy, box, (uint32) len);
  return copy;
}


box_t DBG_NAME (box_copy_tree) (DBG_PARAMS cbox_t box)
{
  uint32 inx, len;
  box_t *copy;
  dtp_t tag;

  if (!IS_BOX_POINTER (box))
    return (box_t) box;

  tag = box_tag (box);
  switch (tag)
    {
    case DV_WIDE:
#ifndef NDEBUG
      len = box_length (box);
      if ((len % sizeof (wchar_t)) || (0 != ((wchar_t *)box)[len/sizeof (wchar_t) - 1]))
        GPF_T1 ("Copy of a tree with a damaged wide string");
#endif
      break;
    case DV_ARRAY_OF_POINTER:
    case DV_LIST_OF_POINTER:
    case DV_ARRAY_OF_XQVAL:
    case DV_XTREE_HEAD:
    case DV_XTREE_NODE:
#ifdef SIGNAL_DEBUG
    case DV_ERROR_REPORT:
#endif
      len = box_length (box);
      copy = (box_t *) DBG_NAME (dk_alloc_box) (DBG_ARGS len, tag);
      len /= sizeof (box_t);
      if (len > 1)
	{
	  for (inx = 0; inx < len - 1; inx++)
	    {
	      __builtin_prefetch (((box_t*)box)[inx + 1]);
	      copy[inx] = DBG_NAME (box_copy_tree) (DBG_ARGS ((box_t *) box)[inx]);
	    }
	copy[inx] = DBG_NAME (box_copy_tree) (DBG_ARGS ((box_t *) box)[inx]);
	}
      else if (len)
        {
          copy[0] = DBG_NAME (box_copy_tree) (DBG_ARGS ((box_t *) box)[0]);
        }
      return (box_t) copy;

    case DV_UNAME:
/* TBD later: macroexpand */
      return box_copy (box);

    case DV_REFERENCE:
      return (box_t) box;

#ifndef NDEBUG
    case TAG_FREE:
      GPF_T1 ("Copy of a freed box");

    case TAG_BAD:
      GPF_T1 ("Copy of a box marked bad");

#endif
    default:
#ifdef MALLOC_DEBUG
      if (tag < FIRST_DV_DTP)
        GPF_T1 ("Copy of a box with weird tag");
#endif
      if (box_copier[tag])
	return (box_copier[tag] ((caddr_t) box));
    }
  len = box_length (box);
  copy = (box_t *) DBG_NAME (dk_alloc_box) (DBG_ARGS len, tag);
  box_flags (copy) = box_flags (box);
  memcpy (copy, box, (uint32) len);
  return (box_t) copy;
}


#ifdef NO_DK_MALLOC_RESERVE
box_t
DBG_NAME (box_try_copy) (DBG_PARAMS cbox_t box, box_t stub)
{
  box_t copy;

  if (!IS_BOX_POINTER (box))
    return box;
  if (DK_ALLOC_ON_RESERVE)
    return stub;
  copy = DBG_NAME (box_copy) (DBG_ARGS box);
  if (!DK_ALLOC_ON_RESERVE)
    return copy;
  DBG_NAME (dk_free_box) (DBG_ARGS copy);
  return stub;
}


box_t
DBG_NAME (box_try_copy_tree) (DBG_PARAMS box_t box, box_t stub)
{
  uint32 inx, len;
  box_t *copy;
  dtp_t tag;

  if (!IS_BOX_POINTER (box))
    return (box_t) box;

  if (DK_ALLOC_ON_RESERVE)
    return stub;

  tag = box_tag (box);
  switch (tag)
    {
    case DV_WIDE:
#ifndef NDEBUG
      len = box_length (box);
      if ((len % sizeof (wchar_t)) || (0 != ((wchar_t *)box)[len/sizeof (wchar_t) - 1]))
        GPF_T1 ("Copy of a tree with a damaged wide string");
#endif
      break;
    case DV_ARRAY_OF_POINTER:
    case DV_LIST_OF_POINTER:
    case DV_ARRAY_OF_XQVAL:
    case DV_XTREE_HEAD:
    case DV_XTREE_NODE:
      len = box_length (box);
      copy = DBG_NAME (dk_try_alloc_box) (DBG_ARGS len, tag);
      if (NULL == copy)
	return stub;
      len /= sizeof (box_t);
      for (inx = 0; inx < len; inx++)
	{
	  copy[inx] = DBG_NAME (box_try_copy_tree) (DBG_ARGS ((box_t *) box)[inx], BADBEEF_BOX);
	  if (BADBEEF_BOX == copy[inx])
	    {
	      uint32 inx1;
	      for (inx1 = 0; inx1 < inx; inx1++)
		DBG_NAME (dk_free_tree) (DBG_ARGS copy[inx1]);
	      DBG_NAME (dk_free_box) (DBG_ARGS copy);
	      return stub;
	    }
	}
      return (box_t) copy;

    case DV_UNAME:
      return box_copy (box);

    case DV_REFERENCE:
      return (box_t) box;

#ifndef NDEBUG
    case TAG_FREE:
      GPF_T1 ("Copy of a freed box");

    case TAG_BAD:
      GPF_T1 ("Copy of a box marked bad");
#endif

    default:
#ifdef MALLOC_DEBUG
      if (tag < FIRST_DV_DTP)
        GPF_T1 ("Copy of a box with weird tag");
#endif
      if (box_copier[tag])
	return (box_copier[tag] (box));
    }
  len = box_length (box);
  copy = DBG_NAME (dk_alloc_box) (DBG_ARGS len, tag);
  box_flags (copy) = box_flags (box);
  memcpy (copy, box, (uint32) len);
  return (box_t) copy;
}


#else

box_t
DBG_NAME (box_try_copy) (DBG_PARAMS cbox_t box, box_t stub)
{
  return DBG_NAME (box_copy) (DBG_ARGS box);
}


box_t
DBG_NAME (box_try_copy_tree) (DBG_PARAMS cbox_t box, box_t stub)
{
  return DBG_NAME (box_copy_tree) (DBG_ARGS box);
}
#endif


box_t
DBG_NAME (box_dv_short_string) (DBG_PARAMS const char *string)
{
  uint32 len;
  box_t box;

  if (!string)
    return NULL;

  len = (uint32) strlen (string) + 1;
  box = DBG_NAME (dk_alloc_box) (DBG_ARGS len, DV_SHORT_STRING);
  memcpy (box, string, len);

  return box;
}


rdf_box_t *
rb_allocate (void)
{
  rdf_box_t *rb = (rdf_box_t *) dk_alloc_box_zero (sizeof (rdf_bigbox_t), DV_RDF);
  rb->rb_ref_count = 1;
  return rb;
}


rdf_bigbox_t *
rbb_allocate (void)
{
  rdf_bigbox_t *rbb = (rdf_bigbox_t *) dk_alloc_box_zero (sizeof (rdf_bigbox_t), DV_RDF);
  rbb->rbb_base.rb_ref_count = 1;
  return rbb;
}


caddr_t
rbb_from_id (int64 n)
{
  rdf_bigbox_t * rbb = rbb_allocate ();
  rbb->rbb_base.rb_ro_id = n;
  rbb->rbb_base.rb_is_outlined = 1;
#if 0
  rbb->rbb_base.rb_type = RDF_BOX_ILL_TYPE;
  rbb->rbb_base.rb_lang = RDF_BOX_ILL_LANG;
#else
  rbb->rbb_base.rb_type = RDF_BOX_DEFAULT_TYPE;
  rbb->rbb_base.rb_lang = RDF_BOX_DEFAULT_LANG;
#endif
  rbb->rbb_box_dtp = DV_STRING;
  return (caddr_t)rbb;
}


void
rdf_box_audit_impl (rdf_box_t * rb)
{
  if (0 >= rb->rb_ref_count)
    GPF_T1 ("RDF box has non-positive reference count");
#ifdef RDF_DEBUG
  if ((0 == rb->rb_ro_id) && (0 == rb->rb_is_complete))
    GPF_T1 ("RDF box is too incomplete");
#endif
  if ((rb->rb_type < RDF_BOX_MIN_TYPE) || (rb->rb_type > RDF_BOX_MAX_TYPE)) GPF_T1 ("rb type out of range");
  if ((rb->rb_lang < RDF_BOX_DEFAULT_LANG) || (rb->rb_lang > RDF_BOX_MAX_LANG)) GPF_T1 ("rb lang out of range");
  if (rb->rb_is_complete)
    rb_dt_lang_check(rb);
}


box_t
DBG_NAME (box_dv_short_nchars) (DBG_PARAMS const char *buf, size_t buf_len)
{
  caddr_t box;
  box = DBG_NAME (dk_alloc_box) (DBG_ARGS (uint32) (buf_len + 1), DV_SHORT_STRING);
  memcpy (box, buf, buf_len);
  box[buf_len] = '\0';
  return box;
}


box_t
DBG_NAME (box_dv_short_nchars_reuse) (DBG_PARAMS const char *buf, size_t buf_len, box_t replace)
{
  caddr_t res;
  size_t res_size = buf_len + 1;
  size_t aligned_res_size = ALIGN_STR (res_size);
  if ((DV_STRING == DV_TYPE_OF (replace)) && (ALIGN_STR (box_length (replace)) == aligned_res_size))
    {
      box_reuse (replace, (box_t) buf, res_size, DV_SHORT_STRING);
      ((caddr_t) replace)[buf_len] = '\0';
      return replace;
    }
  res = DBG_NAME (dk_alloc_box) (DBG_ARGS res_size, DV_SHORT_STRING);
  memcpy (res, buf, buf_len);
  res[buf_len] = '\0';
  dk_free_tree (replace);
  return res;
}


box_t
DBG_NAME (box_dv_short_substr) (DBG_PARAMS ccaddr_t str, int n1, int n2)
{
  int lstr = (int) (box_length (str)) - 1;
  int lres;
  char *res;
  if (n2 > lstr)
    n2 = lstr;
  lres = n2 - n1;
  if (lres <= 0)
    return (DBG_NAME (box_dv_short_string) (DBG_ARGS ""));
  res = DBG_NAME (dk_alloc_box) (DBG_ARGS lres + 1, DV_SHORT_STRING);
  memcpy (res, ((const char *) str) + n1, lres);
  res[lres] = 0;
  return res;
}


box_t
DBG_NAME (box_dv_short_concat) (DBG_PARAMS ccaddr_t box1, ccaddr_t box2)
{
  int len1 = box_length (box1) - 1;	/* Excluding trailing '\0' */
  int len2 = box_length (box2);	/* Including trailing '\0' */
  char *res = DBG_NAME (dk_alloc_box) (DBG_ARGS len1 + len2, DV_SHORT_STRING);
  memcpy (res, box1, len1);
  memcpy (res + len1, box2, len2);
  return res;
}


box_t
DBG_NAME (box_dv_short_strconcat) (DBG_PARAMS const char *str1, const char *str2)
{
  int len1 = strlen (str1);	/* Excluding trailing '\0' */
  int len2 = strlen (str2) + 1;	/* Including trailing '\0' */
  char *res = DBG_NAME (dk_alloc_box) (DBG_ARGS len1 + len2, DV_SHORT_STRING);
  memcpy (res, str1, len1);
  memcpy (res + len1, str2, len2);
  return res;
}


box_t
DBG_NAME (box_dv_wide_nchars) (DBG_PARAMS const wchar_t *buf, size_t buf_wchar_count)
{
  wchar_t *box;
  box = (wchar_t *)DBG_NAME (dk_alloc_box) (DBG_ARGS (uint32) ((buf_wchar_count + 1) * sizeof (wchar_t)), DV_WIDE);
  memcpy (box, buf, buf_wchar_count * sizeof (wchar_t));
  box[buf_wchar_count] = (wchar_t)'\0';
  return (box_t)box;
}


caddr_t
DBG_NAME (box_vsprintf) (DBG_PARAMS size_t buflen_eval, const char *format, va_list tail)
{
  char *tmpbuf;
  int res_len;
  caddr_t res;
  if (buflen_eval > 0xffff)
    buflen_eval = 0xffff;
  tmpbuf = (char *) dk_alloc (buflen_eval + 1);
  res_len = vsnprintf (tmpbuf, buflen_eval, format, tail);
  if (res_len < 0)
#ifdef DEBUG
    GPF_T1 ("formatting error in box_vsprintf");
#else
    res_len = 0;
#endif
  res = DBG_NAME (box_dv_short_nchars) (DBG_ARGS tmpbuf, MIN ((size_t) res_len, buflen_eval));
  dk_free (tmpbuf, buflen_eval + 1);
  return res;
}


#ifdef MALLOC_DEBUG
const char *box_sprintf_impl_file = "???";
int box_sprintf_impl_line;
#endif

caddr_t
box_sprintf_impl (size_t buflen_eval, const char *format, ...)
{
  va_list tail;
  caddr_t res;
  va_start (tail, format);
#ifdef MALLOC_DEBUG
  res = dbg_box_vsprintf (box_sprintf_impl_file, box_sprintf_impl_line, buflen_eval, format, tail);
#else
  res = box_vsprintf (buflen_eval, format, tail);
#endif
  va_end (tail);
  return res;
}


#ifdef MALLOC_DEBUG
box_sprintf_track_t *
box_sprintf_track (const char *file, int line)
{
  static box_sprintf_track_t ret = { box_sprintf_impl };
  box_sprintf_impl_file = file;
  box_sprintf_impl_line = line;
  return &ret;
}
#endif


box_t
DBG_NAME (box_double) (DBG_PARAMS double d)
{
  double *box = (double *) DBG_NAME (dk_alloc_box) (DBG_ARGS sizeof (double), DV_DOUBLE_FLOAT);
  *box = d;
  return (box_t) box;
}


box_t
DBG_NAME (box_float) (DBG_PARAMS float d)
{
  float *box = (float *) DBG_NAME (dk_alloc_box) (DBG_ARGS sizeof (float), DV_SINGLE_FLOAT);
  *box = d;
  return (box_t) box;
}


box_hash_cmp_func_t dtp_cmp_func[256];
box_hash_cmp_func_t dtp_strong_cmp_func[256];

void
dtp_set_cmp (dtp_t dtp, box_hash_cmp_func_t f)
{
  dtp_cmp_func[dtp] = f;
}

void
dtp_set_strong_cmp (dtp_t dtp, box_hash_cmp_func_t f)
{
  dtp_strong_cmp_func[dtp] = f;
}

int
box_equal (cbox_t b1, cbox_t b2)
{
  uint32 l1, l2, bf1, bf2;
  dtp_t b1_tag, b2_tag, nl1, nl2;
  boxint b1_long_val = 0, b2_long_val = 0;

  if (b1 == b2)
    return 1;

  if (!IS_BOX_POINTER (b1))
    {
      b1_tag = DV_LONG_INT;
      b1_long_val = (boxint) (ptrlong) b1;
    }
  else
    {
      b1_tag = box_tag (b1);
      if (b1_tag == DV_LONG_INT)
	b1_long_val = *(boxint *) b1;
    }

  if (!IS_BOX_POINTER (b2))
    {
      b2_tag = DV_LONG_INT;
      b2_long_val = (boxint) (ptrlong) b2;
    }
  else
    {
      b2_tag = box_tag (b2);
      if (b2_tag == DV_LONG_INT)
	b2_long_val = *(boxint *) b2;
    }
  if ((b1_tag == DV_RDF || b2_tag == DV_RDF) && dtp_cmp_func[DV_RDF])
    return dtp_cmp_func[DV_RDF] (b1, b2);
  if (b1_tag == DV_LONG_INT || b2_tag == DV_LONG_INT)
    {
      if (b1_tag != b2_tag)
	return 0;
      return b1_long_val == b2_long_val;
    }
  if (b1_tag == b2_tag && dtp_cmp_func[b1_tag])
    return dtp_cmp_func[b1_tag] (b1, b2);
  l1 = box_length (b1);
  l2 = box_length (b2);
  if (l1 != l2)
    return 0;
  if (DV_DB_NULL == b1_tag && DV_DB_NULL == b2_tag)
    return 1;
  if (DV_DB_NULL == b1_tag || DV_DB_NULL == b2_tag)
    return 0;
  nl1 = IS_NONLEAF_DTP (b1_tag);
  nl2 = IS_NONLEAF_DTP (b2_tag);
  if (nl1 && nl2)
    {
      uint32 inx;
      l1 /= sizeof (caddr_t);
      for (inx = 0; inx < l1; inx++)
	{
	  if (!box_equal (((box_t *) b1)[inx], ((box_t *) b2)[inx]))
	    return 0;
	}
      return 1;
    }
  if (nl1 || nl2)
    return 0;
  memcmp_8 (b1, b2, l1, neq);
  bf1 = box_flags (b1);
  bf2 = box_flags (b2);
  if (bf1 != bf2)
    {
      if (DV_UNAME == b1_tag)
        {
          b1_tag = DV_STRING;
          bf1 = BF_IRI;
        }
      if (DV_UNAME == b2_tag)
        {
          b2_tag = DV_STRING;
          bf2 = BF_IRI;
        }
      if ((b1_tag != b2_tag) || (bf1 != bf2))
        return 0;
    }
  return 1;
 neq:
  return 0;
  /*return (memcmp (b1, b2, l1) ? 0 : 1); */
}


int
box_strong_equal (cbox_t b1, cbox_t b2)
{
  uint32 l1, l2, bf1, bf2;
  dtp_t b1_tag, b2_tag, nl1, nl2;
  boxint b1_long_val = 0, b2_long_val = 0;

  if (b1 == b2)
    return 1;

  if (!IS_BOX_POINTER (b1))
    {
      b1_tag = DV_LONG_INT;
      b1_long_val = (boxint) (ptrlong) b1;
    }
  else
    {
      b1_tag = box_tag (b1);
      if (b1_tag == DV_LONG_INT)
	b1_long_val = *(boxint *) b1;
    }

  if (!IS_BOX_POINTER (b2))
    {
      b2_tag = DV_LONG_INT;
      b2_long_val = (boxint) (ptrlong) b2;
    }
  else
    {
      b2_tag = box_tag (b2);
      if (b2_tag == DV_LONG_INT)
	b2_long_val = *(boxint *) b2;
    }
  if (b1_tag == DV_RDF || b2_tag == DV_RDF)
    {
      if (b1_tag != DV_RDF || b2_tag != DV_RDF)
        return 0;
    }
  if (b1_tag == DV_LONG_INT || b2_tag == DV_LONG_INT)
    {
      if (b1_tag != b2_tag)
	return 0;
      return b1_long_val == b2_long_val;
    }
  if (b1_tag == b2_tag && dtp_strong_cmp_func[b1_tag])
    return dtp_strong_cmp_func[b1_tag] (b1, b2);
  l1 = box_length (b1);
  l2 = box_length (b2);
  if (l1 != l2)
    return 0;
  if (DV_DB_NULL == b1_tag && DV_DB_NULL == b2_tag)
    return 1;
  if (DV_DB_NULL == b1_tag || DV_DB_NULL == b2_tag)
    return 0;
  nl1 = IS_NONLEAF_DTP (b1_tag);
  nl2 = IS_NONLEAF_DTP (b2_tag);
  if (nl1 && nl2)
    {
      uint32 inx;
      l1 /= sizeof (caddr_t);
      for (inx = 0; inx < l1; inx++)
	{
	  if (!box_strong_equal (((box_t *) b1)[inx], ((box_t *) b2)[inx]))
	    return 0;
	}
      return 1;
    }
  if (nl1 || nl2)
    return 0;
  memcmp_8 (b1, b2, l1, neq);
  bf1 = box_flags (b1);
  bf2 = box_flags (b2);
  if (bf1 != bf2)
    {
      if (DV_UNAME == b1_tag)
        {
          b1_tag = DV_STRING;
          bf1 = BF_IRI;
        }
      if (DV_UNAME == b2_tag)
        {
          b2_tag = DV_STRING;
          bf2 = BF_IRI;
        }
      if ((b1_tag != b2_tag) || (bf1 != bf2))
        return 0;
    }
  return 1;
  return 1;
 neq:
  return 0;
}


#ifndef NDEBUG
void
dk_debug_dump_box (FILE * outfd, box_t box, int lvl)
{
  size_t count;
  box_t *arr;
  ptrlong *larr;
  double *darr;
  float *farr;
  dtp_t tag;

  fprintf (outfd, "%*.*s", lvl, lvl, "");

  if (!IS_POINTER (box))
    {
      fprintf (outfd, "immediate number " BOXINT_FMT "\n", unbox (box));
      return;
    }
  if (box == NULL)
    {
      fprintf (outfd, "NULL\n");
      return;
    }

  tag = box_tag (box);
  switch (tag)
    {
    case TAG_FREE:
      fprintf (outfd, "TAG_FREE\n");
      break;

    case DV_NULL:
      fprintf (outfd, "DV_NULL\n");
      break;

    case DV_STRING:
      fprintf (outfd, "DV_SHORT_STRING '%s'\n", (char *) box);
      break;

    case DV_C_STRING:
      fprintf (outfd, "DV_C_STRING '%s'\n", (char *) box);
      break;

    case DV_LONG_INT:
      fprintf (outfd, "DV_LONG_INT %ld\n", *(long *) box);
      break;

    case DV_SHORT_INT:
      fprintf (outfd, "DV_SHORT_INT %ld\n", *(long *) box);
      break;

    case DV_SINGLE_FLOAT:
      fprintf (outfd, "DV_SINGLE_FLOAT %f\n", (double) (*(float *) box));
      break;

    case DV_DOUBLE_FLOAT:
      fprintf (outfd, "DV_DOUBLE_FLOAT %f\n", *(double *) box);
      break;

    case DV_CHARACTER:
      fprintf (outfd, "DV_CHARACTER '%c'\n", *(char *) box);
      break;

    case DV_ARRAY_OF_POINTER:
      fprintf (outfd, "DV_ARRAY_OF_POINTER\n");
      count = BOX_ELEMENTS (box);
      arr = (box_t *) box;
      while (count--)
	dk_debug_dump_box (outfd, *arr++, lvl + 2);
      break;

    case DV_ARRAY_OF_LONG_PACKED:
      fprintf (outfd, "DV_ARRAY_OF_LONG_PACKED\n");
      count = BOX_ELEMENTS (box);
      larr = (ptrlong *) box;
      while (count--)
	fprintf (outfd, "%*.*s, %ld\n", lvl + 2, lvl + 2, "", *larr++);
      break;

    case DV_ARRAY_OF_FLOAT:
      fprintf (outfd, "DV_ARRAY_OF_FLOAT\n");
      count = BOX_ELEMENTS (box);
      farr = (float *) box;
      while (count--)
	fprintf (outfd, "%*.*s%f\n", lvl + 2, lvl + 2, "", (double) *farr++);
      break;

    case DV_ARRAY_OF_DOUBLE:
      fprintf (outfd, "DV_ARRAY_OF_DOUBLE\n");
      count = BOX_ELEMENTS (box);
      darr = (double *) box;
      while (count--)
	fprintf (outfd, "%*.*s%f\n", lvl + 2, lvl + 2, "", *darr++);
      break;

    case DV_ARRAY_OF_LONG:
      fprintf (outfd, "DV_ARRAY_OF_LONG\n");
      count = BOX_ELEMENTS (box);
      larr = (ptrlong *) box;
      while (count--)
	fprintf (outfd, "%*.*s%ld\n", lvl + 2, lvl + 2, "", *larr++);
      break;

    case DV_LIST_OF_POINTER:
      fprintf (outfd, "DV_LIST_OF_POINTER\n");
      count = BOX_ELEMENTS (box);
      arr = (box_t *) box;
      while (count--)
	dk_debug_dump_box (outfd, *arr++, lvl + 2);
      break;

    case DV_ARRAY_OF_XQVAL:
      fprintf (outfd, "DV_ARRAY_OF_XQVAL\n");
      count = BOX_ELEMENTS (box);
      arr = (box_t *) box;
      while (count--)
	dk_debug_dump_box (outfd, *arr++, lvl + 2);
      break;

    case DV_XTREE_HEAD:
      fprintf (outfd, "DV_XTREE_HEAD\n");
      count = BOX_ELEMENTS (box);
      arr = (box_t *) box;
      while (count--)
	dk_debug_dump_box (outfd, *arr++, lvl + 2);
      break;

    case DV_XTREE_NODE:
      fprintf (outfd, "DV_XTREE_NODE\n");
      count = BOX_ELEMENTS (box);
      arr = (box_t *) box;
      while (count--)
	dk_debug_dump_box (outfd, *arr++, lvl + 2);
      break;
    }
}
#endif


#ifdef dk_alloc_box_assert
#undef dk_alloc_box_assert
#endif
void
dk_alloc_box_assert (box_t box)
{
  if (TAG_FREE == box_tag (box))
    GPF_T1 ("Tree contains a pointer to a freed box");
#ifdef DOUBLE_ALIGN
  dk_alloc_assert (((char *) (box)) - 8);
#else
  dk_alloc_assert (((char *) (box)) - 4);
#endif
}

#ifdef dk_alloc_box_assert_mp_or_plain
#undef dk_alloc_box_assert_mp_or_plain
#endif
void
dk_alloc_box_assert_mp_or_plain (box_t box)
{
  if (TAG_FREE == box_tag (box))
    GPF_T1 ("Tree contains a pointer to a freed box");
#ifdef DOUBLE_ALIGN
  dk_alloc_assert_mp_or_plain (((char *) (box)) - 8);
#else
  dk_alloc_assert_mp_or_plain (((char *) (box)) - 4);
#endif
}

char *
DBG_NAME (box_dv_ubuf) (DBG_PARAMS size_t buf_strlen)
{
#ifdef MALLOC_DEBUG
  caddr_t uname;
  buf_strlen++;
  uname = DBG_NAME (dk_alloc_box) (DBG_ARGS buf_strlen, DV_NULL);
  box_tag_modify_impl (uname, DV_UNAME);
  return uname;
#else
  uname_blk_t *blk;
  caddr_t uname;
  caddr_t hd;
  buf_strlen++;
  blk = (uname_blk_t *) DK_ALLOC (sizeof (uname_blk_t) + (buf_strlen - sizeof (ptrlong)));
  uname = blk->unb_data;
  hd = uname - 4;
  WRITE_BOX_HEADER (hd, buf_strlen, DV_UNAME);
  return uname;
#endif
}


char *
DBG_NAME (box_dv_ubuf_or_null) (DBG_PARAMS size_t buf_strlen)
{
#ifdef MALLOC_DEBUG
  caddr_t uname;
  buf_strlen++;
  uname = DBG_NAME (dk_try_alloc_box) (DBG_ARGS buf_strlen, DV_NULL);
  if (NULL == uname)
    return NULL;
  box_tag_modify_impl (uname, DV_UNAME);
  return uname;
#else
  uname_blk_t *blk;
  caddr_t uname;
  caddr_t hd;
  buf_strlen++;
  blk = (uname_blk_t *) dk_try_alloc (sizeof (uname_blk_t) + (buf_strlen - sizeof (ptrlong)));
  if (NULL == blk)
    return NULL;
  uname = blk->unb_data;
  hd = uname - 4;
  WRITE_BOX_HEADER (hd, buf_strlen, DV_UNAME);
  return uname;
#endif
}


box_t
DBG_NAME (box_dv_uname_from_ubuf) (DBG_PARAMS char *text)
{
  uint32 hash;
  char *uname;
  size_t boxlen = box_length (text);
  uname_blk_t *blk, *old_persistent_chain_head;
  uname_chain_pair_t *cpair;
#ifdef DEBUG
  if (strlen (text) != boxlen - 1)
    GPF_T1 ("text length of a uname does not match its buffer size");
#endif
  BYTE_BUFFER_HASH (hash, text, boxlen - 1);
  cpair = unames + (hash % UNAME_TABLE_SIZE);
  old_persistent_chain_head = cpair->unc_immortals;
  for (blk = old_persistent_chain_head; NULL != blk; blk = blk->unb_next)
    {
      if ((blk->unb_hdr[UNB_HDR_HASH] != hash))
	continue;
#ifdef MALLOC_DEBUG
      uname = blk->unb_data_ptr;
#else
      uname = blk->unb_data;
#endif
      if (0 == memcmp (uname, text, boxlen))
	goto return_old_uname;
    }
  /* If not found, we should add some or increase a refcounter */
  mutex_enter (uname_mutex);
  /* This loop almost never runs: other thread puts probably same word into persistent */
  for (blk = cpair->unc_immortals; old_persistent_chain_head != blk; blk = blk->unb_next)
    {
      if ((blk->unb_hdr[UNB_HDR_HASH] != hash))
	continue;
#ifdef MALLOC_DEBUG
      uname = blk->unb_data_ptr;
#else
      uname = blk->unb_data;
#endif
      if (0 == memcmp (uname, text, boxlen))
	{
	  mutex_leave (uname_mutex);
	  goto return_old_uname;
	}
    }
#ifdef DV_UNAME_UNIT_DEBUG
  cpair->unc_refcount++;
#endif
  for (blk = cpair->unc_refcounted; NULL != blk; blk = blk->unb_next)
    {
      if ((blk->unb_hdr[UNB_HDR_HASH] != hash))
	continue;
#ifdef MALLOC_DEBUG
      uname = blk->unb_data_ptr;
#else
      uname = blk->unb_data;
#endif
      if (0 != memcmp (uname, text, boxlen))
	continue;
      if (UNAME_LOCK_REFCOUNT <= (++(blk->unb_hdr[UNB_HDR_REFCTR])))
	{					 /* Uname is popular enough to become immortal */
	  if (blk == cpair->unc_refcounted)
	    cpair->unc_refcounted = blk->unb_next;
	  else
	    {
	      uname_blk_t *prev_in_chain = cpair->unc_refcounted;
	      while (prev_in_chain->unb_next != blk)
		prev_in_chain = prev_in_chain->unb_next;
	      prev_in_chain->unb_next = blk->unb_next;
	    }
	  blk->unb_next = cpair->unc_immortals;
	  cpair->unc_immortals = blk;
	}
      box_dv_uname_audit_one (hash);
      mutex_leave (uname_mutex);
      goto return_old_uname;
    }
#ifdef DV_UNAME_UNIT_DEBUG
  cpair->unc_count++;
#endif
#ifdef MALLOC_DEBUG
  blk = (uname_blk_t *) DK_ALLOC (sizeof (uname_blk_t));
  blk->unb_data_ptr = text;
  {
    char *hd = ((char *) (blk->unb_hdr + UNB_HDR_BOXHEAD));
    WRITE_BOX_HEADER (hd, boxlen, DV_UNAME);
  }
#else
  blk = UNAME_TO_UNAME_BLK (text);
#endif
  blk->unb_next = cpair->unc_refcounted;
  cpair->unc_refcounted = blk;
  blk->unb_hdr[UNB_HDR_HASH] = hash;
  blk->unb_hdr[UNB_HDR_REFCTR] = 1;
  box_dv_uname_audit_one (hash);
  mutex_leave (uname_mutex);
  return text;

return_old_uname:
#ifdef MALLOC_DEBUG
  box_tag_modify_impl (text, DV_NULL);
  dk_free_box (text);
#else
  DK_FREE (UNAME_TO_UNAME_BLK (text), sizeof (uname_blk_t) + boxlen - sizeof (ptrlong));
#endif
  box_dv_uname_audit_one (hash);
  return (box_t) uname;
}


box_t
DBG_NAME (box_dv_uname_nchars) (DBG_PARAMS const char *text, size_t len)
{
  uint32 hash;
  char *uname;
  uname_blk_t *blk, *old_persistent_chain_head;
  uname_chain_pair_t *cpair;
  uint32 boxhead[2];
  boxhead[0] = 0;
  boxhead[1] = 0;
  BYTE_BUFFER_HASH (hash, text, len);
  {
    size_t bytes = len + 1;
    char *hd = ((char *) (&boxhead[1]));
    WRITE_BOX_HEADER (hd, bytes, DV_UNAME);
  }
  cpair = unames + (hash % UNAME_TABLE_SIZE);
  old_persistent_chain_head = cpair->unc_immortals;
  for (blk = old_persistent_chain_head; NULL != blk; blk = blk->unb_next)
    {
      if ((blk->unb_hdr[UNB_HDR_HASH] != hash))
	continue;
      if ((blk->unb_hdr[UNB_HDR_BOXHEAD] != boxhead[1]))
	continue;
#ifdef MALLOC_DEBUG
      uname = blk->unb_data_ptr;
#else
      uname = blk->unb_data;
#endif
      if (0 == memcmp (uname, text, len))
	return (box_t) uname;
    }
  /* If not found, we should add some or increase a refcounter */
  mutex_enter (uname_mutex);
  /* This loop almost never runs: other thread puts probably same word into persistent */
  for (blk = cpair->unc_immortals; old_persistent_chain_head != blk; blk = blk->unb_next)
    {
      if ((blk->unb_hdr[UNB_HDR_HASH] != hash))
	continue;
      if ((blk->unb_hdr[UNB_HDR_BOXHEAD] != boxhead[1]))
	continue;
#ifdef MALLOC_DEBUG
      uname = blk->unb_data_ptr;
#else
      uname = blk->unb_data;
#endif
      if (0 == memcmp (uname, text, len))
	{
	  box_dv_uname_audit_one (hash);
	  mutex_leave (uname_mutex);
	  return (box_t) uname;
	}
    }
#ifdef DV_UNAME_UNIT_DEBUG
  cpair->unc_refcount++;
#endif
  for (blk = cpair->unc_refcounted; NULL != blk; blk = blk->unb_next)
    {
      if ((blk->unb_hdr[UNB_HDR_HASH] != hash))
	continue;
      if ((blk->unb_hdr[UNB_HDR_BOXHEAD] != boxhead[1]))
	continue;
#ifdef MALLOC_DEBUG
      uname = blk->unb_data_ptr;
#else
      uname = blk->unb_data;
#endif
      if (0 != memcmp (uname, text, len))
	continue;
      if (UNAME_LOCK_REFCOUNT <= (++(blk->unb_hdr[UNB_HDR_REFCTR])))
	{					 /* Uname is popular enough to become immortal */
	  if (blk == cpair->unc_refcounted)
	    cpair->unc_refcounted = blk->unb_next;
	  else
	    {
	      uname_blk_t *prev_in_chain = cpair->unc_refcounted;
	      while (prev_in_chain->unb_next != blk)
		prev_in_chain = prev_in_chain->unb_next;
	      prev_in_chain->unb_next = blk->unb_next;
	    }
	  blk->unb_next = cpair->unc_immortals;
	  cpair->unc_immortals = blk;
	}
      box_dv_uname_audit_one (hash);
      mutex_leave (uname_mutex);
      return (box_t) uname;
    }
#ifdef DV_UNAME_UNIT_DEBUG
  cpair->unc_count++;
#endif
#ifdef MALLOC_DEBUG
  blk = (uname_blk_t *) DK_ALLOC (sizeof (uname_blk_t));
  uname = blk->unb_data_ptr = DBG_NAME (dk_alloc_box) (DBG_ARGS len + 1, DV_NULL);
  box_tag_modify_impl (uname, DV_UNAME);
#else
  blk = (uname_blk_t *) DK_ALLOC (sizeof (uname_blk_t) + (len + 1 - sizeof (ptrlong)));
  uname = blk->unb_data;
#endif
  blk->unb_next = cpair->unc_refcounted;
  cpair->unc_refcounted = blk;
  blk->unb_hdr[UNB_HDR_HASH] = hash;
  blk->unb_hdr[UNB_HDR_REFCTR] = 1;
  blk->unb_hdr[UNB_HDR_BOXFLAGS] = boxhead[0];
  blk->unb_hdr[UNB_HDR_BOXHEAD] = boxhead[1];
  memcpy (uname, text, len);
  uname[len] = '\0';
  box_dv_uname_audit_one (hash);
  mutex_leave (uname_mutex);
  return (box_t) uname;
}


void
box_dv_uname_make_immortal (caddr_t tree)
{
  size_t len;
  uint32 hash;
  uname_chain_pair_t *cpair;
  uname_blk_t *blk;
#ifdef MALLOC_DEBUG
  uname_blk_t **blk_ptr;
#endif
  switch (DV_TYPE_OF (tree))
    {
    case DV_UNAME:
      /*printf ("\nUNAME %s is about to become immortal", tree);*/
      mutex_enter (uname_mutex);
#ifdef MALLOC_DEBUG
      len = box_length (tree) - 1;
      BYTE_BUFFER_HASH (hash, tree, len);
      cpair = unames + (hash % UNAME_TABLE_SIZE);
      for (blk = cpair->unc_immortals; NULL != blk; blk = blk->unb_next)
	{
	  if (blk->unb_data_ptr == tree)
	    {
	      mutex_leave (uname_mutex);
	      return;
	    }
	}
      for (blk_ptr = &cpair->unc_refcounted; (NULL != (blk = blk_ptr[0])); blk_ptr = &(blk->unb_next))
	{
	  if (blk->unb_data_ptr == tree)
	    {
	      blk_ptr[0] = blk->unb_next;
	      blk->unb_next = cpair->unc_immortals;
	      cpair->unc_immortals = blk;
#ifdef DV_UNAME_UNIT_DEBUG
	      cpair->unc_refcount += (UNAME_LOCK_REFCOUNT - blk->unb_hdr[UNB_HDR_REFCTR]);
#endif
	      blk->unb_hdr[UNB_HDR_REFCTR] = UNAME_LOCK_REFCOUNT;
	      box_dv_uname_audit_one (hash);
	      mutex_leave (uname_mutex);
	      return;
	    }
	}
      GPF_T1 ("Can't make broken UNAME immortal");
#else
      blk = UNAME_TO_UNAME_BLK (tree);
      if (UNAME_LOCK_REFCOUNT <= blk->unb_hdr[UNB_HDR_REFCTR])
	{
	  mutex_leave (uname_mutex);
	  return;
	}
      hash = blk->unb_hdr[UNB_HDR_HASH];
      cpair = unames + (hash % UNAME_TABLE_SIZE);
#ifdef DV_UNAME_UNIT_DEBUG
      cpair->unc_refcount += (UNAME_LOCK_REFCOUNT - blk->unb_hdr[UNB_HDR_REFCTR]);
#endif
      if (blk == cpair->unc_refcounted)
	cpair->unc_refcounted = blk->unb_next;
      else
	{
	  uname_blk_t *prev_in_chain = cpair->unc_refcounted;
	  while (prev_in_chain->unb_next != blk)
	    prev_in_chain = prev_in_chain->unb_next;
	  prev_in_chain->unb_next = blk->unb_next;
	}
      blk->unb_next = cpair->unc_immortals;
      cpair->unc_immortals = blk;
      blk->unb_hdr[UNB_HDR_REFCTR] = UNAME_LOCK_REFCOUNT;
      box_dv_uname_audit_one (hash);
      mutex_leave (uname_mutex);
#endif
      return;

    case DV_ARRAY_OF_POINTER:
    case DV_ARRAY_OF_XQVAL:
      len = BOX_ELEMENTS (tree);
      while (len-- > 0)
	{
	  caddr_t sub = ((caddr_t *) tree)[len];
	  switch (DV_TYPE_OF (sub))
	    {
	    case DV_UNAME:
	    case DV_ARRAY_OF_POINTER:
	    case DV_ARRAY_OF_XQVAL:
	      box_dv_uname_make_immortal (sub);
	    }
	}
      break;

    default:
      break;
    }
}


void
box_dv_uname_make_immortal_all (void)
{
  uname_chain_pair_t *cpair;
#ifdef DV_UNAME_STATS
  int total_immortals = 0, total_refcounted = 0;
  FILE *stats = fopen ("unames.txt", "wt");
  mutex_enter (uname_mutex);
  for (cpair = unames + UNAME_TABLE_SIZE; unames <= --cpair; /* no step */ )
    {
      int chainlen;
      uname_blk_t *blk;
/* Write refcounted */
      blk = cpair->unc_refcounted;
      chainlen = 0;
      while (NULL != blk)
	{
#ifdef MALLOC_DEBUG
	  fprintf (stats, "_uname_,_R_,%s,%ld,0x%08x\n", blk->unb_data_ptr, (long) (blk->unb_hdr[UNB_HDR_REFCTR]), blk->unb_hdr[UNB_HDR_HASH]);
#else
	  fprintf (stats, "_uname_,_R_,%s,%ld,0x%08x\n", blk->unb_data, (long) (blk->unb_hdr[UNB_HDR_REFCTR]), blk->unb_hdr[UNB_HDR_HASH]);
#endif
	  total_refcounted++;
	  chainlen++;
	  blk = blk->unb_next;
	}
      if (NULL != cpair->unc_refcounted)
	fprintf (stats, "_chain_,_R_,0x%p,%d,0x%04x\n", cpair, chainlen, cpair - unames);
/* Write immortals */
      blk = cpair->unc_immortals;
      chainlen = 0;
      while (NULL != blk)
	{
#ifdef MALLOC_DEBUG
	  fprintf (stats, "_uname_,_I_,%s,%ld,0x%08x\n", blk->unb_data_ptr, (long) (blk->unb_hdr[UNB_HDR_REFCTR]), blk->unb_hdr[UNB_HDR_HASH]);
#else
	  fprintf (stats, "_uname_,_I_,%s,%ld,0x%08x\n", blk->unb_data, (long) (blk->unb_hdr[UNB_HDR_REFCTR]), blk->unb_hdr[UNB_HDR_HASH]);
#endif
	  total_immortals++;
	  chainlen++;
	  blk = blk->unb_next;
	}
      if (NULL != cpair->unc_immortals)
	fprintf (stats, "_chain_,_I_,0x%p,%d,0x%04x\n", cpair, chainlen, cpair - unames);
    }
  fprintf (stats, "_total_,_R_,,%d,\n", total_refcounted);
  fprintf (stats, "_total_,_I_,,%d,\n", total_immortals);
  fclose (stats);
  mutex_leave (uname_mutex);
#endif
  mutex_enter (uname_mutex);
  for (cpair = unames + UNAME_TABLE_SIZE; unames <= --cpair; /* no step */ )
    {
      uname_blk_t *blk = cpair->unc_refcounted;
      while (NULL != blk)
	{
	  uname_blk_t *nxt = blk->unb_next;
#ifdef DV_UNAME_UNIT_DEBUG
	  cpair->unc_refcount += (UNAME_LOCK_REFCOUNT - blk->unb_hdr[UNB_HDR_REFCTR]);
#endif
	  blk->unb_hdr[UNB_HDR_REFCTR] = UNAME_LOCK_REFCOUNT;
	  blk->unb_next = cpair->unc_immortals;
	  cpair->unc_immortals = blk;
	  blk = nxt;
	}
      cpair->unc_refcounted = NULL;
    }
  box_dv_uname_audit_table ();
  mutex_leave (uname_mutex);
}


box_t
DBG_NAME (box_dv_uname_string) (DBG_PARAMS const char *string)
{
  return DBG_NAME (box_dv_uname_nchars) (DBG_ARGS string, strlen (string));
}


box_t
DBG_NAME (box_dv_uname_substr) (DBG_PARAMS ccaddr_t str, int n1, int n2)
{
  int lstr = (int) (box_length (str)) - 1;
  int lres;
  if (n2 > lstr)
    n2 = lstr;
  lres = n2 - n1;
  if (lres <= 0)
    return uname___empty;
  return DBG_NAME (box_dv_uname_nchars) (DBG_ARGS ((char *) str) + n1, lres);
}


caddr_t
box_mem_wrapper_copy_hook (caddr_t mw_arg)
{
  dk_mem_wrapper_t *mw = (dk_mem_wrapper_t *) (mw_arg);
  if (mw->dmw_copy)
    return (caddr_t) (mw->dmw_copy (mw->dmw_data[0]));
  return NULL;
}


int
box_mem_wrapper_destr_hook (caddr_t mw_arg)
{
  dk_mem_wrapper_t *mw = (dk_mem_wrapper_t *) (mw_arg);
  if (mw->dmw_free)
    mw->dmw_free (mw->dmw_data[0]);
  return 0;
}

#ifdef SIGNAL_DEBUG
FILE *error_report_log = NULL;
dk_mutex_t *error_report_log_mutex = NULL;
void
log_error_report_event (caddr_t box, int print_full_content, const char *fmt, ...)
{
  jmp_buf_splice *ctx;
  va_list ap;
  va_start (ap, fmt);
  int ctx_cycle = 0;
  mutex_enter (error_report_log_mutex);
  fprintf (error_report_log, "\n{{{ERR %p ", box);
  if (!ERROR_REPORT_P (box))
    fprintf (error_report_log, "TAG %d ", DV_TYPE_OF (box));
  vfprintf (error_report_log, fmt, ap);
  if (print_full_content)
    fprintf (error_report_log, " {{%p} {%s} {%s}}", ((caddr_t *)box)[0], ((caddr_t *)box)[1], ((caddr_t *)box)[2]);
  for (ctx = THREAD_CURRENT_THREAD->thr_reset_ctx; NULL != ctx; ctx = ctx->j_parent)
    {
    fprintf (error_report_log, " {%s:%d}", ctx->j_file, ctx->j_line);
      if (ctx->j_parent == ctx)
        {
          fprintf (error_report_log, "{CYCLE!}");
          ctx_cycle = 1;
          break;
        }
    }
  fprintf (error_report_log, " }}}");
  fflush (error_report_log);
  if (ctx_cycle)
    {
      fclose (error_report_log);
      GPF_T1 ("Cycle in stack of jmp_buf_splices (missing POP_QR_RESET?); see the tail of error_report_events_NNN.log");
}
  mutex_leave (error_report_log_mutex);
}
#endif

#ifdef QUERY_DEBUG
FILE *query_log = NULL;
dk_mutex_t *query_log_mutex;
#endif

caddr_t uname___empty;

#ifdef MALLOC_DEBUG
extern dk_mutex_t *spare_rbufs_mtx;
#endif

void
dk_box_initialize (void)
{
#ifdef SIGNAL_DEBUG
#ifdef QUERY_DEBUG
  char query_and_error_log_fname[50];
#else
  char error_report_log_fname[50];
#endif
#else
#ifdef QUERY_DEBUG
  char query_log_fname[50];
#endif
#endif
  static int dk_box_is_initialized = 0;
  if (dk_box_is_initialized)
    return;
  dk_box_is_initialized = 1;
#ifdef SIGNAL_DEBUG
#ifdef QUERY_DEBUG
  sprintf (query_and_error_log_fname, "query_and_error_events_%d.log", getpid());
  error_report_log = query_log = fopen (query_and_error_log_fname, "w");
  error_report_log_mutex = query_log_mutex = mutex_allocate ();
#else
  sprintf (error_report_log_fname, "error_report_events_%d.log", getpid());
  error_report_log = fopen (error_report_log_fname, "w");
  error_report_log_mutex = mutex_allocate ();
#endif
#else
#ifdef QUERY_DEBUG
  sprintf (query_log_fname, "query_events_%d.log", getpid());
  query_log = fopen (query_log_fname, "w");
  query_log_mutex = mutex_allocate ();
#endif
#endif
  dk_mem_hooks (DV_MEM_WRAPPER, box_mem_wrapper_copy_hook, box_mem_wrapper_destr_hook, 0);
#ifdef MALLOC_DEBUG
  dk_mem_hooks_2 (DV_NON_BOX, box_copy_non_box, NULL, 0, box_mp_copy_non_box);
#endif
  dk_mem_hooks (DV_RBUF,  box_non_copiable, (box_destr_f)rbuf_free_cb, 0);
#ifdef MALLOC_DEBUG
  spare_rbufs_mtx = mutex_allocate ();
#endif
  uname_mutex = mutex_allocate ();
  if (NULL == uname_mutex)
    GPF_T;
  uname___empty = box_dv_uname_nchars ("", 0);
  box_dv_uname_make_immortal (uname___empty);
#ifndef DV_UNAME_STATS
#ifdef DEBUG
  /* Uname trick is... well, a trick. If broken, it will cause never-catch errors. Thus check it always. */
  {
    int ctr;
    caddr_t *list1 = (caddr_t *) dk_alloc_box (sizeof (caddr_t) * UNAME_TABLE_SIZE * 2, DV_ARRAY_OF_POINTER);
    caddr_t *list2 = (caddr_t *) dk_alloc_box (sizeof (caddr_t) * UNAME_TABLE_SIZE * 2, DV_ARRAY_OF_POINTER);
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* alloc 1 */
	char buf[20];
	sprintf (buf, "uname%d", ctr);
	list1[ctr] = box_dv_uname_string (buf);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* alloc 2a */
	char buf[20];
	sprintf (buf, "uname%d", ctr);
	list2[ctr] = box_dv_uname_string (buf);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {
	if (list1[ctr] != list2[ctr])
	  GPF_T;
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* alloc 3 */
	list2[ctr] = box_copy (list1[ctr]);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {
	if (list1[ctr] != list2[ctr])
	  GPF_T;
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* free 3 */
	dk_free_box (list2[ctr]);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {
	char buf[20];
	sprintf (buf, "uname%d", ctr);
	if (strcmp (list1[ctr], buf))
	  GPF_T;
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* free 2a */
	dk_free_box (list2[ctr]);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {
	char buf[20];
	sprintf (buf, "uname%d", ctr);
	if (strcmp (list1[ctr], buf))
	  GPF_T;
      }

    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {
	box_dv_uname_make_immortal (list1[ctr]);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* no effect */
	dk_free_box (list2[ctr]);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* no effect */
	dk_free_box (list2[ctr]);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* no effect */
	dk_free_box (list2[ctr]);
      }

    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {
	char buf[20];
	sprintf (buf, "uname%d", ctr);
	list2[ctr] = box_dv_uname_string (buf);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* alloc 2b */
	if (list1[ctr] != list2[ctr])
	  GPF_T;
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* free 2b */
	dk_free_box (list2[ctr]);
      }
    for (ctr = UNAME_TABLE_SIZE * 2; ctr--; /* no step */ )
      {						 /* free 1 */
	dk_free_box (list1[ctr]);
      }
  }
#endif
#endif
}

void
dk_box_finalize (void)
{
  static int dk_box_is_finalized = 0;
  if (dk_box_is_finalized)
    GPF_T;
  dk_box_is_finalized = 1;
#ifdef SIGNAL_DEBUG
  fclose (error_report_log);
  error_report_log = NULL;
#endif
#ifdef QUERY_DEBUG
#ifndef SIGNAL_DEBUG
  fclose (query_log);
#endif
  query_log = NULL;
#endif
}

/* Original signatures should exist for EXE-EXPORTed functions */
#ifdef MALLOC_DEBUG
/*
#undef dk_alloc_box
box_t dk_alloc_box (uint32 bytes, dtp_t type) { return dbg_dk_alloc_box (__FILE__, __LINE__, bytes, type); }
#undef dk_try_alloc_box
box_t dk_try_alloc_box (size_t bytes, dtp_t type) { return dbg_dk_try_alloc_box (__FILE__, __LINE__, bytes, type); }
#undef dk_alloc_box_zero
box_t dk_alloc_box_zero (size_t bytes, dtp_t type) { return dbg_dk_alloc_box_zero (__FILE__, __LINE__, bytes, type); }
*/
#undef box_string
box_t
box_string (const char *string)
{
  return dbg_box_string (__FILE__, __LINE__, string);
}


#undef box_dv_short_string
box_t
box_dv_short_string (const char *string)
{
  return dbg_box_dv_short_string (__FILE__, __LINE__, string);
}


#undef box_dv_short_nchars
box_t
box_dv_short_nchars (const char *buf, size_t buf_len)
{
  return dbg_box_dv_short_nchars (__FILE__, __LINE__, buf, buf_len);
}


#undef box_dv_short_nchars_reuse
box_t
box_dv_short_nchars_reuse (const char *buf, size_t buf_len, box_t replace)
{
  return dbg_box_dv_short_nchars_reuse (__FILE__, __LINE__, buf, buf_len, replace);
}


#undef box_dv_short_substr
box_t
box_dv_short_substr (ccaddr_t box, int n1, int n2)
{
  return dbg_box_dv_short_substr (__FILE__, __LINE__, box, n1, n2);
}


#undef box_dv_short_concat
box_t
box_dv_short_concat (ccaddr_t box1, ccaddr_t box2)
{
  return dbg_box_dv_short_concat (__FILE__, __LINE__, box1, box2);
}


#undef box_dv_short_strconcat
box_t
box_dv_short_strconcat (const char *str1, const char *str2)
{
  return dbg_box_dv_short_strconcat (__FILE__, __LINE__, str1, str2);
}


#undef box_copy
box_t
box_copy (cbox_t box)
{
  return dbg_box_copy (__FILE__, __LINE__, box);
}


#undef box_copy_tree
box_t
box_copy_tree (cbox_t box)
{
  return dbg_box_copy_tree (__FILE__, __LINE__, box);
}

#undef dk_realloc_list
box_t
dk_realloc_list (caddr_t ptr, int new_elements)
{
  return dbg_dk_realloc_list (__FILE__, __LINE__, ptr, new_elements);
}

#undef box_num
box_t
box_num (boxint n)
{
  return dbg_box_num (__FILE__, __LINE__, n);
}


#undef box_num_nonull
box_t
box_num_nonull (boxint n)
{
  return dbg_box_num_nonull (__FILE__, __LINE__, n);
}


#undef box_iri_id
box_t
box_iri_id (int64 n)
{
  return dbg_box_iri_id (__FILE__, __LINE__, n);
}


#undef box_dv_ubuf
char *
box_dv_ubuf (size_t buf_strlen)
{
  return dbg_box_dv_ubuf (__FILE__, __LINE__, buf_strlen);
}


#undef box_dv_ubuf_or_null
char *
box_dv_ubuf_or_null (size_t buf_strlen)
{
  return dbg_box_dv_ubuf_or_null (__FILE__, __LINE__, buf_strlen);
}


#undef box_dv_uname_from_ubuf
box_t
box_dv_uname_from_ubuf (char *ubuf)
{
  return dbg_box_dv_uname_from_ubuf (__FILE__, __LINE__, ubuf);
}


#undef box_dv_uname_string
box_t
box_dv_uname_string (const char *string)
{
  return dbg_box_dv_uname_string (__FILE__, __LINE__, string);
}


#undef box_dv_uname_nchars
box_t
box_dv_uname_nchars (const char *buf, size_t buf_len)
{
  return dbg_box_dv_uname_nchars (__FILE__, __LINE__, buf, buf_len);
}


#undef box_dv_uname_substr
box_t
box_dv_uname_substr (ccaddr_t box, int n1, int n2)
{
  return dbg_box_dv_uname_substr (__FILE__, __LINE__, box, n1, n2);
}


#undef box_double
box_t
box_double (double d)
{
  return dbg_box_double (__FILE__, __LINE__, d);
}


#undef box_float
box_t
box_float (float d)
{
  return dbg_box_float (__FILE__, __LINE__, d);
}


#undef box_dv_wide_nchars
box_t
box_dv_wide_nchars (const wchar_t *buf, size_t buf_wchar_count)
{
  return dbg_box_dv_wide_nchars (__FILE__, __LINE__, buf, buf_wchar_count);
}


#undef box_vsprintf
caddr_t
box_vsprintf (size_t buflen_eval, const char *format, va_list tail)
{
  return dbg_box_vsprintf (__FILE__, __LINE__, buflen_eval, format, tail);
}


#undef box_sprintf
caddr_t
box_sprintf (size_t buflen_eval, const char *format, ...)
{
  va_list tail;
  caddr_t res;
  va_start (tail, format);
  res = dbg_box_vsprintf (__FILE__, __LINE__, buflen_eval, format, tail);
  va_end (tail);
  return res;
}
#endif