1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
621
  
622
  
623
  
624
  
625
  
626
  
627
  
628
  
629
  
630
  
631
  
632
  
633
  
634
  
635
  
636
  
637
  
638
  
639
  
640
  
641
  
642
  
643
  
644
  
645
  
646
  
647
  
648
  
649
  
650
  
651
  
652
  
653
  
654
  
655
  
656
  
657
  
658
  
659
  
660
  
661
  
662
  
663
  
664
  
665
  
666
  
667
  
668
  
669
  
670
  
671
  
672
  
673
  
674
  
675
  
676
  
677
  
678
  
679
  
680
  
681
  
682
  
683
  
684
  
685
  
686
  
687
  
688
  
689
  
690
  
691
  
692
  
693
  
694
  
695
  
696
  
697
  
698
  
699
  
700
  
701
  
702
  
703
  
704
  
705
  
706
  
707
  
708
  
709
  
710
  
711
  
712
  
713
  
714
  
715
  
716
  
717
  
718
  
719
  
720
  
721
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
776
  
777
  
778
  
779
  
780
  
781
  
782
  
783
  
784
  
785
  
786
  
787
  
788
  
789
  
790
  
791
  
792
  
793
  
794
  
795
  
796
  
797
  
798
  
799
  
800
  
801
  
802
  
803
  
804
  
805
  
806
  
807
  
808
  
809
  
810
  
811
  
812
  
813
  
814
  
815
  
816
  
817
  
818
  
819
  
820
  
821
  
822
  
823
  
824
  
825
  
826
  
827
  
828
  
829
  
830
  
831
  
832
  
833
  
834
  
835
  
836
  
837
  
838
  
839
  
840
  
841
  
842
  
843
  
844
  
845
  
846
  
847
  
848
  
849
  
850
  
851
  
852
  
853
  
854
  
855
  
856
  
857
  
858
  
859
  
860
  
861
  
862
  
863
  
864
  
865
  
866
  
867
  
868
  
869
  
870
  
871
  
872
  
873
  
874
  
875
  
876
  
877
  
878
  
879
  
880
  
881
  
882
  
883
  
884
  
885
  
886
  
887
  
888
  
889
  
890
  
891
  
892
  
893
  
894
  
895
  
896
  
897
  
898
  
899
  
900
  
901
  
902
  
903
  
904
  
905
  
906
  
907
  
908
  
909
  
910
  
911
  
912
  
913
  
914
  
915
  
916
  
917
  
918
  
919
  
920
  
921
  
922
  
923
  
924
  
925
  
926
  
927
  
928
  
929
  
930
  
931
  
932
  
933
  
934
  
935
  
936
  
937
  
938
  
939
  
940
  
941
  
942
  
943
  
944
  
945
  
946
  
947
  
948
  
949
  
950
  
951
  
952
  
953
  
954
  
955
  
956
  
957
  
958
  
959
  
960
  
961
  
962
  
963
  
964
  
965
  
966
  
967
  
968
  
969
  
970
  
971
  
972
  
973
  
974
  
975
  
976
  
977
  
978
  
979
  
980
  
981
  
982
  
983
  
984
  
985
  
986
  
987
  
988
  
989
  
990
  
991
  
992
  
993
  
994
  
995
  
996
  
997
  
998
  
999
  
1000
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
1243
  
1244
  
1245
  
1246
  
1247
  
1248
  
1249
  
1250
  
1251
  
1252
  
1253
  
1254
  
1255
  
1256
  
1257
  
1258
  
1259
  
1260
  
1261
  
1262
  
1263
  
1264
  
1265
  
1266
  
1267
  
1268
  
1269
  
1270
  
1271
  
1272
  
1273
  
1274
  
1275
  
1276
  
1277
  
1278
  
1279
  
1280
  
1281
  
1282
  
1283
  
1284
  
1285
  
1286
  
1287
  
1288
  
1289
  
1290
  
1291
  
1292
  
1293
  
1294
  
1295
  
1296
  
1297
  
1298
  
1299
  
1300
  
1301
  
1302
  
1303
  
1304
  
1305
  
1306
  
1307
  
1308
  
1309
  
1310
  
1311
  
1312
  
1313
  
1314
  
1315
  
1316
  
1317
  
1318
  
1319
  
1320
  
1321
  
1322
  
1323
  
1324
  
1325
  
1326
  
1327
  
1328
  
1329
  
1330
  
1331
  
1332
  
1333
  
1334
  
1335
  
1336
  
1337
  
1338
  
1339
  
1340
  
1341
  
1342
  
1343
  
1344
  
1345
  
1346
  
1347
  
1348
  
1349
  
1350
  
1351
  
1352
  
1353
  
1354
  
1355
  
1356
  
1357
  
1358
  
1359
  
1360
  
1361
  
1362
  
1363
  
1364
  
1365
  
1366
  
1367
  
1368
  
1369
  
1370
  
1371
  
1372
  
1373
  
1374
  
1375
  
1376
  
1377
  
1378
  
1379
  
1380
  
1381
  
1382
  
1383
  
1384
  
1385
  
1386
  
1387
  
1388
  
1389
  
1390
  
1391
  
1392
  
1393
  
1394
  
1395
  
1396
  
1397
  
1398
  
1399
  
1400
  
1401
  
1402
  
1403
  
1404
  
1405
  
1406
  
1407
  
1408
  
1409
  
1410
  
1411
  
1412
  
1413
  
1414
  
1415
  
1416
  
1417
  
1418
  
1419
  
1420
  
1421
  
1422
  
1423
  
1424
  
1425
  
1426
  
1427
  
1428
  
1429
  
1430
  
1431
  
1432
  
1433
  
1434
  
1435
  
1436
  
1437
  
1438
  
1439
  
1440
  
1441
  
1442
  
1443
  
1444
  
1445
  
1446
  
1447
  
1448
  
1449
  
1450
  
1451
  
1452
  
1453
  
1454
  
1455
  
1456
  
1457
  
1458
  
1459
  
1460
  
1461
  
1462
  
1463
  
1464
  
1465
  
1466
  
1467
  
1468
  
1469
  
1470
  
1471
  
1472
  
1473
  
1474
  
1475
  
1476
  
1477
  
1478
  
1479
  
1480
  
1481
  
1482
  
1483
  
1484
  
1485
  
1486
  
1487
  
1488
  
1489
  
1490
  
1491
  
1492
  
1493
  
1494
  
1495
  
1496
  
1497
  
1498
  
1499
  
1500
  
1501
  
1502
  
1503
  
1504
  
1505
  
1506
  
1507
  
1508
  
1509
  
1510
  
1511
  
1512
  
1513
  
1514
  
1515
  
1516
  
1517
  
1518
  
1519
  
1520
  
1521
  
1522
  
1523
  
1524
  
1525
  
1526
  
1527
  
1528
  
1529
  
1530
  
1531
  
1532
  
1533
  
1534
  
1535
  
1536
  
1537
  
1538
  
1539
  
1540
  
1541
  
1542
  
1543
  
1544
  
1545
  
1546
  
1547
  
1548
  
1549
  
1550
  
1551
  
1552
  
1553
  
1554
  
1555
  
1556
  
1557
  
1558
  
1559
  
1560
  
1561
  
1562
  
1563
  
1564
  
1565
  
1566
  
1567
  
1568
  
1569
  
1570
  
1571
  
1572
  
1573
  
1574
  
1575
  
1576
  
1577
  
1578
  
1579
  
1580
  
1581
  
1582
  
1583
  
1584
  
1585
  
1586
  
1587
  
1588
  
1589
  
1590
  
1591
  
1592
  
1593
  
1594
  
1595
  
1596
  
1597
  
1598
  
1599
  
1600
  
1601
  
1602
  
1603
  
1604
  
1605
  
1606
  
1607
  
1608
  
1609
  
1610
  
1611
  
1612
  
1613
  
1614
  
1615
  
1616
  
1617
  
1618
  
1619
  
1620
  
1621
  
1622
  
1623
  
1624
  
1625
  
1626
  
1627
  
1628
  
1629
  
1630
  
1631
  
1632
  
1633
  
1634
  
1635
  
1636
  
1637
  
1638
  
1639
  
1640
  
1641
  
1642
  
1643
  
1644
  
1645
  
1646
  
1647
  
1648
  
1649
  
1650
  
1651
  
1652
  
1653
  
1654
  
1655
  
1656
  
1657
  
1658
  
1659
  
1660
  
1661
  
1662
  
1663
  
1664
  
1665
  
1666
  
1667
  
1668
  
1669
  
1670
  
1671
  
1672
  
1673
  
1674
  
1675
  
1676
  
1677
  
1678
  
1679
  
1680
  
1681
  
1682
  
1683
  
1684
  
1685
  
1686
  
1687
  
1688
  
1689
  
1690
  
1691
  
1692
  
1693
  
1694
  
1695
  
1696
  
1697
  
1698
  
1699
  
1700
  
1701
  
1702
  
1703
  
1704
  
1705
  
1706
  
1707
  
1708
  
1709
  
1710
  
1711
  
1712
  
1713
  
1714
  
1715
  
1716
  
1717
  
1718
  
1719
  
1720
  
1721
  
1722
  
1723
  
1724
  
1725
  
1726
  
1727
  
1728
  
1729
  
1730
  
1731
  
1732
  
1733
  
1734
  
1735
  
1736
  
1737
  
1738
  
1739
  
1740
  
1741
  
1742
  
1743
  
1744
  
1745
  
1746
  
1747
  
1748
  
1749
  
1750
  
1751
  
1752
  
1753
  
1754
  
1755
  
1756
  
1757
  
1758
  
1759
  
1760
  
1761
  
1762
  
1763
  
1764
  
1765
  
1766
  
1767
  
1768
  
1769
  
1770
  
1771
  
1772
  
1773
  
1774
  
1775
  
1776
  
1777
  
1778
  
1779
  
1780
  
1781
  
1782
  
1783
  
1784
  
1785
  
1786
  
1787
  
1788
  
1789
  
1790
  
1791
  
1792
  
1793
  
1794
  
1795
  
1796
  
1797
  
1798
  
1799
  
1800
  
1801
  
1802
  
1803
  
1804
  
1805
  
1806
  
1807
  
1808
  
1809
  
1810
  
1811
  
1812
  
1813
  
1814
  
1815
  
1816
  
1817
  
1818
  
1819
  
1820
  
1821
  
1822
  
1823
  
1824
  
1825
  
1826
  
1827
  
1828
  
1829
  
1830
  
1831
  
1832
  
1833
  
1834
  
1835
  
1836
  
1837
  
1838
  
1839
  
1840
  
1841
  
1842
  
1843
  
1844
  
1845
  
1846
  
1847
  
1848
  
1849
  
1850
  
1851
  
1852
  
1853
  
1854
  
1855
  
1856
  
1857
  
1858
  
1859
  
1860
  
1861
  
1862
  
1863
  
1864
  
1865
  
1866
  
1867
  
1868
  
1869
  
1870
  
1871
  
1872
  
1873
  
1874
  
1875
  
1876
  
1877
  
1878
  
1879
  
1880
  
1881
  
1882
  
1883
  
1884
  
1885
  
1886
  
1887
  
1888
  
1889
  
1890
  
1891
  
1892
  
1893
  
1894
  
1895
  
1896
  
1897
  
1898
  
1899
  
1900
  
1901
  
1902
  
1903
  
1904
  
1905
  
1906
  
1907
  
1908
  
1909
  
1910
  
1911
  
1912
  
1913
  
1914
  
1915
  
1916
  
1917
  
1918
  
1919
  
1920
  
1921
  
1922
  
1923
  
1924
  
1925
  
1926
  
1927
  
1928
  
1929
  
1930
  
1931
  
1932
  
1933
  
1934
  
1935
  
1936
  
1937
  
1938
  
1939
  
1940
  
1941
  
1942
  
1943
  
1944
  
1945
  
1946
  
1947
  
1948
  
1949
  
1950
  
1951
  
1952
  
1953
  
1954
  
1955
  
1956
  
1957
  
1958
  
1959
  
1960
  
1961
  
1962
  
1963
  
1964
  
1965
  
1966
  
1967
  
1968
  
1969
  
1970
  
1971
  
1972
  
1973
  
1974
  
1975
  
1976
  
1977
  
1978
  
1979
  
1980
  
1981
  
1982
  
1983
  
1984
  
1985
  
1986
  
1987
  
1988
  
1989
  
1990
  
1991
  
1992
  
1993
  
1994
  
1995
  
1996
  
1997
  
1998
  
1999
  
2000
  
2001
  
2002
  
2003
  
2004
  
2005
  
2006
  
2007
  
2008
  
2009
  
2010
  
2011
  
2012
  
2013
  
2014
  
2015
  
2016
  
2017
  
2018
  
2019
  
2020
  
2021
  
2022
  
2023
  
2024
  
2025
  
2026
  
2027
  
2028
  
2029
  
2030
  
2031
  
2032
  
2033
  
2034
  
2035
  
2036
  
2037
  
2038
  
2039
  
2040
  
2041
  
2042
  
2043
  
2044
  
2045
  
2046
  
2047
  
2048
  
2049
  
2050
  
2051
  
2052
  
2053
  
2054
  
2055
  
2056
  
2057
  
2058
  
2059
  
2060
  
2061
  
2062
  
2063
  
2064
  
2065
  
2066
  
2067
  
2068
  
2069
  
2070
  
2071
  
2072
  
2073
  
2074
  
2075
  
2076
  
2077
  
2078
  
2079
  
2080
  
2081
  
2082
  
2083
  
2084
  
2085
  
2086
  
2087
  
2088
  
2089
  
2090
  
2091
  
2092
  
2093
  
2094
  
2095
  
2096
  
2097
  
2098
  
2099
  
2100
  
2101
  
2102
  
2103
  
2104
  
2105
  
2106
  
2107
  
2108
  
2109
  
2110
  
2111
  
2112
  
2113
  
2114
  
2115
  
2116
  
2117
  
2118
  
2119
  
2120
  
2121
  
2122
  
2123
  
2124
  
2125
  
2126
  
2127
  
2128
  
2129
  
2130
  
2131
  
2132
  
2133
  
2134
  
2135
  
2136
  
2137
  
2138
  
2139
  
2140
  
2141
  
2142
  
2143
  
2144
  
2145
  
2146
  
2147
  
2148
  
2149
  
2150
  
2151
  
2152
  
2153
  
2154
  
2155
  
2156
  
2157
  
2158
  
2159
  
2160
  
2161
  
2162
  
2163
  
2164
  
2165
  
2166
  
2167
  
2168
  
2169
  
2170
  
2171
  
2172
  
2173
  
2174
  
2175
  
2176
  
2177
  
2178
  
2179
  
2180
  
2181
  
2182
  
2183
  
2184
  
2185
  
2186
  
2187
  
2188
  
2189
  
2190
  
2191
  
2192
  
2193
  
2194
  
2195
  
2196
  
2197
  
2198
  
2199
  
2200
  
2201
  
2202
  
2203
  
2204
  
2205
  
2206
  
2207
  
2208
  
2209
  
2210
  
2211
  
2212
  
2213
  
2214
  
2215
  
2216
  
2217
  
2218
  
2219
  
2220
  
2221
  
2222
  
2223
  
2224
  
2225
  
2226
  
2227
  
2228
  
2229
  
2230
  
2231
  
2232
  
2233
  
2234
  
2235
  
2236
  
2237
  
2238
  
2239
  
2240
  
2241
  
2242
  
2243
  
2244
  
2245
  
2246
  
2247
  
2248
  
2249
  
2250
  
2251
  
2252
  
2253
  
2254
  
2255
  
2256
  
2257
  
2258
  
2259
  
2260
  
2261
  
2262
  
2263
  
2264
  
2265
  
2266
  
2267
  
2268
  
2269
  
2270
  
2271
  
2272
  
2273
  
2274
  
2275
  
2276
  
2277
  
2278
  
2279
  
2280
  
2281
  
2282
  
2283
  
2284
  
2285
  
2286
  
2287
  
2288
  
2289
  
2290
  
2291
  
2292
  
2293
  
2294
  
2295
  
2296
  
2297
  
2298
  
2299
  
2300
  
2301
  
2302
  
2303
  
2304
  
2305
  
2306
  
2307
  
2308
  
2309
  
2310
  
2311
  
2312
  
2313
  
2314
  
2315
  
2316
  
2317
  
2318
  
2319
  
2320
  
2321
  
2322
  
2323
  
2324
  
2325
  
2326
  
2327
  
2328
  
2329
  
2330
  
2331
  
2332
  
2333
  
2334
  
2335
  
2336
  
2337
  
2338
  
2339
  
2340
  
2341
  
2342
  
2343
  
2344
  
2345
  
2346
  
2347
  
2348
  
2349
  
2350
  
2351
  
2352
  
2353
  
2354
  
2355
  
2356
  
2357
  
2358
  
2359
  
2360
  
2361
  
2362
  
2363
  
2364
  
2365
  
2366
  
2367
  
2368
  
2369
  
2370
  
2371
  
2372
  
2373
  
2374
  
2375
  
2376
  
2377
  
2378
  
2379
  
2380
  
2381
  
2382
  
2383
  
2384
  
2385
  
2386
  
2387
  
2388
  
2389
  
2390
  
2391
  
2392
  
2393
  
2394
  
2395
  
2396
  
2397
  
2398
  
2399
  
2400
  
2401
  
2402
  
2403
  
2404
  
2405
  
2406
  
2407
  
2408
  
2409
  
2410
  
2411
  
2412
  
2413
  
2414
  
2415
  
2416
  
2417
  
2418
  
2419
  
2420
  
2421
  
2422
  
2423
  
2424
  
2425
  
2426
  
2427
  
2428
  
2429
  
2430
  
2431
  
2432
  
2433
  
2434
  
2435
  
2436
  
2437
  
2438
  
2439
  
2440
  
2441
  
2442
  
2443
  
2444
  
2445
  
2446
  
2447
  
2448
  
2449
  
2450
  
2451
  
2452
  
2453
  
2454
  
2455
  
2456
  
2457
  
2458
  
2459
  
2460
  
2461
  
2462
  
2463
  
2464
  
2465
  
2466
  
2467
  
2468
  
2469
  
2470
  
2471
  
2472
  
2473
  
2474
  
2475
  
2476
  
2477
  
2478
  
2479
  
2480
  
2481
  
2482
  
2483
  
2484
  
2485
  
2486
  
2487
  
2488
  
2489
  
2490
  
2491
  
2492
  
2493
  
2494
  
2495
  
2496
  
2497
  
2498
  
2499
  
2500
  
2501
  
2502
  
2503
  
2504
  
2505
  
2506
  
2507
  
2508
  
2509
  
2510
  
2511
  
2512
  
2513
  
2514
  
2515
  
2516
  
2517
  
2518
  
2519
  
2520
  
2521
  
2522
  
2523
  
2524
  
2525
  
2526
  
2527
  
2528
  
2529
  
2530
  
2531
  
2532
  
2533
  
2534
  
2535
  
2536
  
2537
  
2538
  
2539
  
2540
  
2541
  
2542
  
2543
  
2544
  
2545
  
2546
  
2547
  
2548
  
2549
  
2550
  
2551
  
2552
  
2553
  
2554
  
2555
  
2556
  
2557
  
2558
  
2559
  
2560
  
2561
  
2562
  
2563
  
2564
  
2565
  
2566
  
2567
  
2568
  
2569
  
2570
  
2571
  
2572
  
2573
  
2574
  
2575
  
2576
  
2577
  
2578
  
2579
  
2580
  
2581
  
2582
  
2583
  
2584
  
2585
  
2586
  
2587
  
2588
  
2589
  
2590
  
2591
  
2592
  
2593
  
2594
  
2595
  
2596
  
2597
  
2598
  
2599
  
2600
  
2601
  
2602
  
2603
  
2604
  
2605
  
2606
  
2607
  
2608
  
2609
  
2610
  
2611
  
2612
  
2613
  
2614
  
2615
  
2616
  
2617
  
2618
  
2619
  
2620
  
2621
  
2622
  
2623
  
2624
  
2625
  
2626
  
2627
  
2628
  
2629
  
2630
  
2631
  
2632
  
2633
  
2634
  
2635
  
2636
  
2637
  
2638
  
2639
  
2640
  
2641
  
2642
  
2643
  
2644
  
2645
  
2646
  
2647
  
2648
  
2649
  
2650
  
2651
  
2652
  
2653
  
2654
  
2655
  
2656
  
2657
  
2658
  
2659
  
2660
  
2661
  
2662
  
2663
  
2664
  
2665
  
2666
  
2667
  
2668
  
2669
  
2670
  
2671
  
2672
  
2673
  
2674
  
2675
  
2676
  
2677
  
2678
  
2679
  
2680
  
2681
  
2682
  
2683
  
2684
  
2685
  
2686
  
2687
  
2688
  
2689
  
2690
  
2691
  
2692
  
2693
  
2694
  
2695
  
2696
  
2697
  
2698
  
2699
  
2700
  
2701
  
2702
  
2703
  
2704
  
2705
  
2706
  
2707
  
2708
  
2709
  
2710
  
2711
  
2712
  
2713
  
2714
  
2715
  
2716
  
2717
  
2718
  
2719
  
2720
  
2721
  
2722
  
2723
  
2724
  
2725
  
2726
  
2727
  
2728
  
2729
  
2730
  
2731
  
2732
  
2733
  
2734
  
2735
  
2736
  
2737
  
2738
  
2739
  
2740
  
2741
  
2742
  
2743
  
2744
  
2745
  
2746
  
2747
  
2748
  
2749
  
2750
  
2751
  
2752
  
2753
  
2754
  
2755
  
2756
  
2757
  
2758
  
2759
  
2760
  
2761
  
2762
  
2763
  
2764
  
2765
  
2766
  
2767
  
2768
  
2769
  
2770
  
2771
  
2772
  
2773
  
2774
  
2775
  
2776
  
2777
  
2778
  
2779
  
2780
  
2781
  
2782
  
2783
  
2784
  
2785
  
2786
  
2787
  
2788
  
2789
  
2790
  
2791
  
2792
  
2793
  
2794
  
2795
  
2796
  
2797
  
2798
  
2799
  
2800
  
2801
  
2802
  
2803
  
2804
  
2805
  
2806
  
2807
  
2808
  
2809
  
2810
  
2811
  
2812
  
2813
  
2814
  
2815
  
2816
  
2817
  
2818
  
2819
  
2820
  
2821
  
2822
  
2823
  
2824
  
2825
  
2826
  
2827
  
2828
  
2829
  
2830
  
2831
  
2832
  
2833
  
2834
  
2835
  
2836
  
2837
  
2838
  
2839
  
2840
  
2841
  
2842
  
2843
  
2844
  
2845
  
2846
  
2847
  
2848
  
2849
  
2850
  
2851
  
2852
  
2853
  
2854
  
2855
  
2856
  
2857
  
2858
  
2859
  
2860
  
2861
  
2862
  
2863
  
2864
  
2865
  
2866
  
2867
  
2868
  
2869
  
2870
  
2871
  
2872
  
2873
  
2874
  
2875
  
2876
  
2877
  
2878
  
2879
  
2880
  
2881
  
2882
  
2883
  
2884
  
2885
  
2886
  
2887
  
2888
  
2889
  
2890
  
2891
  
2892
  
2893
  
2894
  
2895
  
2896
  
2897
  
2898
  
2899
  
2900
  
2901
  
2902
  
2903
  
2904
  
2905
  
2906
  
2907
  
2908
  
2909
  
2910
  
2911
  
2912
  
2913
  
2914
  
2915
  
2916
  
2917
  
2918
  
2919
  
2920
  
2921
  
2922
  
2923
  
2924
  
2925
  
2926
  
2927
  
2928
  
2929
  
2930
  
2931
  
2932
  
2933
  
2934
  
2935
  
2936
  
2937
  
2938
  
2939
  
2940
  
2941
  
2942
  
2943
  
2944
  
2945
  
2946
  
2947
  
2948
  
2949
  
2950
  
2951
  
2952
  
2953
  
2954
  
2955
  
2956
  
2957
  
2958
  
2959
  
2960
  
2961
  
2962
  
2963
  
2964
  
2965
  
2966
  
2967
  
2968
  
2969
  
2970
  
2971
  
2972
  
2973
  
2974
  
2975
  
2976
  
2977
  
2978
  
2979
  
2980
  
2981
  
2982
  
2983
  
2984
  
2985
  
2986
  
2987
  
2988
  
2989
  
2990
  
2991
  
2992
  
2993
  
2994
  
2995
  
2996
  
2997
  
2998
  
2999
  
3000
  
3001
  
3002
  
3003
  
3004
  
3005
  
3006
  
3007
  
3008
  
3009
  
3010
  
3011
  
3012
  
3013
  
3014
  
3015
  
3016
  
3017
  
3018
  
3019
  
3020
  
3021
  
3022
  
3023
  
3024
  
3025
  
3026
  
3027
  
3028
  
3029
  
3030
  
3031
  
3032
  
3033
  
3034
  
3035
  
3036
  
3037
  
3038
  
3039
  
3040
  
3041
  
3042
  
3043
  
3044
  
3045
  
3046
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "main.h" 
#include "object.h" 
#include "mapping.h" 
#include "svalue.h" 
#include "array.h" 
#include "pike_macros.h" 
#include "pike_error.h" 
#include "pike_memory.h" 
#include "pike_types.h" 
#include "dynamic_buffer.h" 
#include "interpret.h" 
#include "las.h" 
#include "gc.h" 
#include "stralloc.h" 
#include "pike_security.h" 
#include "block_allocator.h" 
#include "opcodes.h" 
#include "stuff.h" 
 
#define AVG_LINK_LENGTH 4 
#define MIN_LINK_LENGTH 1 
#define MAP_SLOTS(X) ((X)?((X)+((X)>>4)+8):0) 
 
struct mapping *first_mapping; 
 
struct mapping *gc_internal_mapping = 0; 
static struct mapping *gc_mark_mapping_pos = 0; 
 
#define unlink_mapping_data(M) do{                            \ 
 struct mapping_data *md_=(M);                                  \ 
 if(md_->hardlinks) { md_->hardlinks--; md_->valrefs--; }       \ 
 free_mapping_data(M);                                          \ 
}while(0) 
 
#define MAPPING_DATA_SIZE(HSIZE, KEYPAIRS) \ 
   PTR_TO_INT(MD_KEYPAIRS(0, HSIZE) + KEYPAIRS) 
 
static struct block_allocator mapping_allocator = BA_INIT_PAGES(sizeof(struct mapping), 2); 
void count_memory_in_mappings(size_t * num, size_t * size) { 
    struct mapping *m; 
    double datasize = 0.0; 
    ba_count_all(&mapping_allocator, num, size); 
    for(m=first_mapping;m;m=m->next) { 
        datasize+=MAPPING_DATA_SIZE(m->data->hashsize, m->data->num_keypairs) / (double) m->data->refs; 
    } 
    *size += (size_t) datasize; 
} 
 
void really_free_mapping(struct mapping * m) { 
#ifdef PIKE_DEBUG 
  if (m->refs) { 
# ifdef DEBUG_MALLOC 
    describe_something(m, T_MAPPING, 0,2,0, NULL); 
# endif 
    Pike_fatal("really free mapping on mapping with %d refs.\n", m->refs); 
  } 
#endif 
  FREE_PROT(m); 
  unlink_mapping_data(m->data); 
  DOUBLEUNLINK(first_mapping, m); 
  GC_FREE(m); 
  ba_free(&mapping_allocator, m); 
} 
 
ATTRIBUTE((malloc)) 
static struct mapping * alloc_mapping(void) { 
    return ba_alloc(&mapping_allocator); 
} 
 
void free_all_mapping_blocks(void) { 
    ba_destroy(&mapping_allocator); 
} 
 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
#define IF_ELSE_KEYPAIR_LOOP(X, Y)    Y 
#define FREE_KEYPAIR(md, k) do {      \ 
    k->next = md->free_list;            \ 
    md->free_list = k;                  \ 
  } while(0) 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
#define IF_ELSE_KEYPAIR_LOOP(X, Y)    X 
#define FREE_KEYPAIR(md, k) do {                      \ 
    md->free_list--;                                    \ 
    if (k != md->free_list) {                           \ 
      struct keypair **prev_;                           \ 
      unsigned INT32 h_;                                \ 
      INT32 e;                                          \ 
      /* Move the last keypair to the new hole. */      \ 
      *k = *(md->free_list);                            \ 
      h_ = k->hval & ( md->hashsize - 1);               \ 
      prev_ = md->hash + h_;                            \ 
      DO_IF_DEBUG(                                      \ 
        if (!*prev_) {                                  \ 
          Pike_fatal("Node to move not found!\n");      \ 
        }                                               \ 
      );                                                \ 
      while (*prev_ != md->free_list) {                 \ 
        prev_ = &((*prev_)->next);                      \ 
        DO_IF_DEBUG(                                    \ 
          if (!*prev_) {                                \ 
            Pike_fatal("Node to move not found!\n");    \ 
          }                                             \ 
        );                                              \ 
      }                                                 \ 
      *prev_ = k;                                       \ 
    }                                                   \ 
  } while(0) 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
 
void mapping_free_keypair(struct mapping_data *md, struct keypair *k) 
{ 
  FREE_KEYPAIR(md, k); 
} 
 
static INLINE int check_type_contains(TYPE_FIELD types, const struct svalue * s) { 
    return (TYPEOF(*s) == PIKE_T_OBJECT || types & (BIT_OBJECT|(1 << TYPEOF(*s)))); 
} 
 
static INLINE int check_type_overlaps(TYPE_FIELD t1, TYPE_FIELD t2) { 
    return (!t1 && !t2) || t1 & t2 || (t1|t2) & BIT_OBJECT; 
} 
 
#ifdef PIKE_DEBUG 
 
/** This function checks that the type field isn't lacking any bits. 
 * It is used for debugging purposes only. 
 */ 
static void check_mapping_type_fields(const struct mapping *m) 
{ 
  INT32 e; 
  const struct keypair *k=0; 
  const struct mapping_data *md; 
  TYPE_FIELD ind_types, val_types; 
 
  ind_types=val_types=0; 
 
  md = m->data; 
  NEW_MAPPING_LOOP(md) 
    { 
      if (TYPEOF(k->val) > MAX_TYPE) 
        Pike_fatal("Invalid mapping keypair value type: %s\n", 
                   get_name_of_type(TYPEOF(k->val))); 
      val_types |= 1 << TYPEOF(k->val); 
      if (TYPEOF(k->ind) > MAX_TYPE) 
        Pike_fatal("Invalid maping keypair index type: %s\n", 
                   get_name_of_type(TYPEOF(k->ind))); 
      ind_types |= 1 << TYPEOF(k->ind); 
    } 
 
  if(val_types & ~(m->data->val_types)) 
    Pike_fatal("Mapping value types out of order!\n"); 
 
  if(ind_types & ~(m->data->ind_types)) 
    Pike_fatal("Mapping indices types out of order!\n"); 
} 
#endif 
 
static struct mapping_data empty_data = 
  { PIKE_CONSTANT_MEMOBJ_INIT(1, T_MAPPING_DATA), 1, 0,0,0,0,0,0, 0, 
    IF_ELSE_KEYPAIR_LOOP((struct keypair *)&empty_data.hash, 0), {0}}; 
static struct mapping_data weak_ind_empty_data = 
  { PIKE_CONSTANT_MEMOBJ_INIT(1, T_MAPPING_DATA), 1, 0,0,0,0,0,0, MAPPING_WEAK_INDICES, 
    IF_ELSE_KEYPAIR_LOOP((struct keypair *)&weak_ind_empty_data.hash, 0), {0}}; 
static struct mapping_data weak_val_empty_data = 
  { PIKE_CONSTANT_MEMOBJ_INIT(1, T_MAPPING_DATA), 1, 0,0,0,0,0,0, MAPPING_WEAK_VALUES, 
    IF_ELSE_KEYPAIR_LOOP((struct keypair *)&weak_val_empty_data.hash, 0), {0}}; 
static struct mapping_data weak_both_empty_data = 
  { PIKE_CONSTANT_MEMOBJ_INIT(1, T_MAPPING_DATA), 1, 0,0,0,0,0,0, MAPPING_WEAK, 
    IF_ELSE_KEYPAIR_LOOP((struct keypair *)&weak_both_empty_data.hash, 0), {0}}; 
 
/** This function allocates the hash table and svalue space for a mapping 
 * struct. The size is the max number of indices that can fit in the 
 * allocated space. 
 */ 
static void init_mapping(struct mapping *m, 
                         INT32 size, 
                         INT16 flags) 
{ 
  struct mapping_data *md; 
  ptrdiff_t e; 
  INT32 hashsize; 
 
  debug_malloc_touch(m); 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc > GC_PASS_PREPARE && Pike_in_gc < GC_PASS_ZAP_WEAK) 
    Pike_fatal("Can't allocate a new mapping_data inside gc.\n"); 
  if(size < 0) Pike_fatal("init_mapping with negative value.\n"); 
#endif 
  if(size) 
  { 
    hashsize=find_next_power(size / AVG_LINK_LENGTH + 1); 
 
    if (size < hashsize) size = hashsize; 
 
    e=MAPPING_DATA_SIZE(hashsize, size); 
 
    md=xcalloc(1,e); 
 
    m->data=md; 
    md->hashsize=hashsize; 
 
    md->free_list=MD_KEYPAIRS(md, hashsize); 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
    for(e=1;e<size;e++) 
    { 
      md->free_list[e-1].next = md->free_list + e; 
      mark_free_svalue (&md->free_list[e-1].ind); 
      mark_free_svalue (&md->free_list[e-1].val); 
    } 
    /* md->free_list[e-1].next=0; */ 
    mark_free_svalue (&md->free_list[e-1].ind); 
    mark_free_svalue (&md->free_list[e-1].val); 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
    /* md->ind_types = 0; */ 
    /* md->val_types = 0; */ 
    md->flags = flags; 
    /* md->size = 0; */ 
    /* md->refs=0; */ 
    /* md->valrefs=0; */ 
    /* md->hardlinks=0; */ 
    md->num_keypairs=size; 
  }else{ 
    switch (flags & MAPPING_WEAK) { 
      case 0: md = &empty_data; break; 
      case MAPPING_WEAK_INDICES: md = &weak_ind_empty_data; break; 
      case MAPPING_WEAK_VALUES: md = &weak_val_empty_data; break; 
      default: md = &weak_both_empty_data; break; 
    } 
  } 
  add_ref(md); 
  m->data=md; 
#ifdef MAPPING_SIZE_DEBUG 
  m->debug_size = md->size; 
#endif 
} 
 
/** This function allocates an empty mapping with initial room 
 * for 'size' values. 
 * 
 * @param size initial number of values 
 * @return the newly allocated mapping 
 * @see do_free_mapping 
 * @see free_mapping 
 */ 
PMOD_EXPORT struct mapping *debug_allocate_mapping(int size) 
{ 
  struct mapping *m; 
 
  m=alloc_mapping(); 
 
  GC_ALLOC(m); 
 
  INITIALIZE_PROT(m); 
  init_mapping(m,size,0); 
 
  m->refs = 0; 
  add_ref(m);   /* For DMALLOC... */ 
 
  DOUBLELINK(first_mapping, m); 
 
  return m; 
} 
 
 
PMOD_EXPORT void really_free_mapping_data(struct mapping_data *md) 
{ 
  INT32 e; 
  struct keypair *k; 
  debug_malloc_touch(md); 
 
#ifdef PIKE_DEBUG 
  if (md->refs) { 
    Pike_fatal("really_free_mapping_data(): md has non-zero refs: %d\n", 
               md->refs); 
  } 
 
  if (!md->size) { 
    /* Paranoia and keep gcc happy. */ 
    if (md == &empty_data) { 
      Pike_fatal("really_free_mapping_data(): md is empty_data!\n"); 
    } 
    if (md == &weak_ind_empty_data) { 
      Pike_fatal("really_free_mapping_data(): md is weak_ind_empty_data!\n"); 
    } 
    if (md == &weak_val_empty_data) { 
      Pike_fatal("really_free_mapping_data(): md is weak_val_empty_data!\n"); 
    } 
    if (md == &weak_both_empty_data) { 
      Pike_fatal("really_free_mapping_data(): md is weak_both_empty_data!\n"); 
    } 
  } 
#endif /* PIKE_DEBUG */ 
 
  NEW_MAPPING_LOOP(md) 
  { 
    free_svalue(& k->val); 
    free_svalue(& k->ind); 
  } 
 
  free(md); 
  GC_FREE_BLOCK(md); 
} 
 
PMOD_EXPORT void do_free_mapping(struct mapping *m) 
{ 
  if (m) 
    inl_free_mapping(m); 
} 
 
/* This function is used to rehash a mapping without losing the internal 
 * order in each hash chain. This is to prevent mappings from becoming 
 * inefficient just after being rehashed. 
 */ 
/* Evil: Steal the svalues from the old md. */ 
static void mapping_rehash_backwards_evil(struct mapping_data *md, 
                                          struct keypair *from) 
{ 
  unsigned INT32 h; 
  struct keypair *k, *prev = NULL, *next; 
 
  if(!(k = from)) return; 
 
  /* Reverse the hash chain. */ 
  while ((next = k->next)) { 
    k->next = prev; 
    prev = k; 
    k = next; 
  } 
  k->next = prev; 
 
  prev = k; 
 
  /* Rehash and reverse the hash chain. */ 
  while ((from = prev)) { 
    /* Reverse */ 
    prev = from->next; 
    from->next = next; 
    next = from; 
 
    if (md->flags & MAPPING_WEAK) { 
 
      switch(md->flags & MAPPING_WEAK) { 
      default: 
        Pike_fatal("Instable mapping data flags.\n"); 
      case MAPPING_WEAK_INDICES: 
        if (!REFCOUNTED_TYPE(TYPEOF(from->ind)) || 
            (*from->ind.u.refs > 1)) { 
          goto keep_keypair; 
        } 
        break; 
      case MAPPING_WEAK_VALUES: 
        if (!REFCOUNTED_TYPE(TYPEOF(from->val)) || 
            (*from->val.u.refs > 1)) { 
          goto keep_keypair; 
        } 
        break; 
      case MAPPING_WEAK: 
        /* NB: Compat: Unreference counted values are counted 
         *             as multi-referenced here. 
         */ 
        if ((!REFCOUNTED_TYPE(TYPEOF(from->ind)) || 
             (*from->ind.u.refs > 1)) && 
            (!REFCOUNTED_TYPE(TYPEOF(from->val)) || 
             (*from->val.u.refs > 1))) { 
          goto keep_keypair; 
        } 
        break; 
      } 
 
      /* Free. 
       * Note that we don't need to free or unlink the keypair, 
       * since that will be done by the caller anyway. */ 
      free_svalue(&from->ind); 
      free_svalue(&from->val); 
 
      continue; 
    } 
  keep_keypair: 
 
    /* unlink */ 
    k=md->free_list; 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
#ifdef PIKE_DEBUG 
    if(!k) Pike_fatal("Error in rehash: not enough keypairs.\n"); 
#endif 
    md->free_list=k->next; 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
    md->free_list++; 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
 
    /* initialize */ 
    *k=*from; 
 
    /* link */ 
    h=k->hval; 
    h&=md->hashsize - 1; 
    k->next=md->hash[h]; 
    md->hash[h]=k; 
 
    /* update */ 
    md->ind_types |= 1<< (TYPEOF(k->ind)); 
    md->val_types |= 1<< (TYPEOF(k->val)); 
    md->size++; 
  } 
} 
 
/* Good: Copy the svalues from the old md. */ 
static void mapping_rehash_backwards_good(struct mapping_data *md, 
                                          struct keypair *from) 
{ 
  unsigned INT32 h; 
  struct keypair *k, *prev = NULL, *next; 
 
  if(!(k = from)) return; 
 
  /* Reverse the hash chain. */ 
  while ((next = k->next)) { 
    k->next = prev; 
    prev = k; 
    k = next; 
  } 
  k->next = prev; 
 
  prev = k; 
 
  /* Rehash and reverse the hash chain. */ 
  while ((from = prev)) { 
    /* Reverse */ 
    prev = from->next; 
    from->next = next; 
    next = from; 
 
    if (md->flags & MAPPING_WEAK) { 
 
      switch(md->flags & MAPPING_WEAK) { 
      default: 
        Pike_fatal("Instable mapping data flags.\n"); 
      case MAPPING_WEAK_INDICES: 
        if (REFCOUNTED_TYPE(TYPEOF(from->ind)) && 
            (*from->ind.u.refs > 1)) { 
          goto keep_keypair; 
        } 
        break; 
      case MAPPING_WEAK_VALUES: 
        if (REFCOUNTED_TYPE(TYPEOF(from->val)) && 
            (*from->val.u.refs > 1)) { 
          goto keep_keypair; 
        } 
        break; 
      case MAPPING_WEAK: 
        /* NB: Compat: Unreference counted values are counted 
         *             as multi-referenced here. 
         */ 
        if ((!REFCOUNTED_TYPE(TYPEOF(from->ind)) || 
             (*from->ind.u.refs > 1)) && 
            (!REFCOUNTED_TYPE(TYPEOF(from->val)) || 
             (*from->val.u.refs > 1))) { 
          goto keep_keypair; 
        } 
        break; 
      } 
 
      /* Skip copying of this keypair. 
       * 
       * NB: We can't mess with the original md here, 
       *     since it might be in use by an iterator 
       *     or similar. 
       */ 
 
      continue; 
    } 
  keep_keypair: 
 
    /* unlink */ 
    k=md->free_list; 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
#ifdef PIKE_DEBUG 
    if(!k) Pike_fatal("Error in rehash: not enough keypairs.\n"); 
#endif 
    md->free_list=k->next; 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
    md->free_list++; 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
 
    /* initialize */ 
    k->hval=from->hval; 
    assign_svalue_no_free(&k->ind, &from->ind); 
    assign_svalue_no_free(&k->val, &from->val); 
 
    /* link */ 
    h=k->hval; 
    h&=md->hashsize - 1; 
    k->next=md->hash[h]; 
    md->hash[h]=k; 
 
    /* update */ 
    md->ind_types |= 1<< (TYPEOF(k->ind)); 
    md->val_types |= 1<< (TYPEOF(k->val)); 
    md->size++; 
  } 
} 
 
/** This function re-allocates a mapping. It adjusts the max no. of 
 * values can be fitted into the mapping. It takes a bit of time to 
 * run, but is used seldom enough not to degrade preformance significantly. 
 * 
 * @param m the mapping to be rehashed 
 * @param new_size new mappingsize 
 * @return the rehashed mapping 
 */ 
static struct mapping *rehash(struct mapping *m, int new_size) 
{ 
  struct mapping_data *md, *new_md; 
#ifdef PIKE_DEBUG 
  INT32 tmp=m->data->size; 
#endif 
  INT32 e; 
 
  md=m->data; 
  debug_malloc_touch(md); 
#ifdef PIKE_DEBUG 
  if(md->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
 
  if(d_flag>1)  check_mapping(m); 
#endif 
 
  /* FIXME: The special case below seems suspect. 
   *    /grubba 2011-09-04 
   */ 
  if ((md->hashsize == new_size) && (md->refs == 1)) return m; 
 
  init_mapping(m, new_size, md->flags); 
  debug_malloc_touch(m); 
  new_md=m->data; 
 
  /* This operation is now 100% atomic - no locking required */ 
  if(md->refs>1) 
  { 
    /* good */ 
    /* More than one reference to the md ==> We need to 
     * keep it afterwards. 
     */ 
    for(e=0;e<md->hashsize;e++) 
      mapping_rehash_backwards_good(new_md, md->hash[e]); 
 
    unlink_mapping_data(md); 
  }else{ 
    /* evil */ 
    /* We have the only reference to the md, 
     * so we can just copy the svalues without 
     * bothering about type checking. 
     */ 
    for(e=0;e<md->hashsize;e++) 
      mapping_rehash_backwards_evil(new_md, md->hash[e]); 
 
    free(md); 
    GC_FREE_BLOCK(md); 
  } 
 
#ifdef PIKE_DEBUG 
  if((m->data->size != tmp) && 
     ((m->data->size > tmp) || !(m->data->flags & MAPPING_WEAK))) 
    Pike_fatal("Rehash failed, size not same any more (%ld != %ld).\n", 
               (long)m->data->size, (long)tmp); 
#endif 
#ifdef MAPPING_SIZE_DEBUG 
  m->debug_size = m->data->size; 
#endif 
 
#ifdef PIKE_DEBUG 
  if(d_flag>1)  check_mapping(m); 
#endif 
 
  return m; 
} 
 
/* 
 * No need for super efficiency, should not be called very often 
 * -Hubbe 
 */ 
#define LOW_RELOC(X) X= (void *) ((char *)(X) + off); 
#define RELOC(X) if(X) LOW_RELOC(X) 
 
struct mapping_data *copy_mapping_data(struct mapping_data *md) 
{ 
  long e; 
  ptrdiff_t size, off; 
  struct mapping_data *nmd; 
  struct keypair *keypairs; 
 
#ifdef PIKE_DEBUG 
  if (Pike_in_gc > GC_PASS_PREPARE && Pike_in_gc < GC_PASS_ZAP_WEAK) 
    Pike_fatal("Can't allocate a new mapping_data inside gc.\n"); 
#endif 
 
  debug_malloc_touch(md); 
 
  size=MAPPING_DATA_SIZE(md->hashsize, md->num_keypairs); 
 
  nmd=(struct mapping_data *)xalloc(size); 
  memcpy(nmd, md, size); 
  off=((char *)nmd) - ((char *)md); 
 
  RELOC(nmd->free_list); 
  for(e=0;e<nmd->hashsize;e++) RELOC(nmd->hash[e]); 
 
  keypairs=MD_KEYPAIRS(nmd, nmd->hashsize); 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
  for(e=0;e<nmd->num_keypairs;e++) 
  { 
    RELOC(keypairs[e].next); 
    add_ref_svalue(& keypairs[e].ind); 
    add_ref_svalue(& keypairs[e].val); 
  } 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
  for(e=0;e<nmd->size;e++) 
  { 
    RELOC(keypairs[e].next); 
    add_ref_svalue(& keypairs[e].ind); 
    add_ref_svalue(& keypairs[e].val); 
  } 
#endif /* PIKE_MAPPING_KEYPAIR_LOOP */ 
 
  nmd->refs=0; 
  add_ref(nmd); /* For DMALLOC... */ 
  nmd->valrefs=0; 
  nmd->hardlinks=0; 
 
  /* FIXME: What about nmd->flags? */ 
 
  if(md->hardlinks) 
  { 
#ifdef PIKE_DEBUG 
    if(md->refs <= 0 || md->valrefs<=0) 
      Pike_fatal("Hardlink without refs/valrefs!\n"); 
#endif 
    md->hardlinks--; 
    md->valrefs--; 
  } 
  sub_ref(md); 
 
  return nmd; 
} 
 
#define MAPPING_DATA_IN_USE(MD) ((MD)->refs != (MD)->hardlinks + 1) 
 
#define LOW_FIND(FUN, KEY, FOUND, NOT_FOUND) do {             \ 
  md=m->data;                                                   \ 
  add_ref(md);                                                  \ 
  if(md->hashsize)                                              \ 
  {                                                             \ 
    h=h2 & (md->hashsize - 1);                                  \ 
    DO_IF_DEBUG( if(d_flag > 1) check_mapping_type_fields(m); ) \ 
    if(check_type_contains(md->ind_types, key))                 \ 
    {                                                           \ 
      for(prev= md->hash + h;(k=*prev);prev=&k->next)           \ 
      {                                                         \ 
        if(h2 == k->hval && FUN(& k->ind, KEY))                 \ 
        {                                                       \ 
          FOUND;                                                \ 
        }                                                       \ 
      }                                                         \ 
    }                                                           \ 
  }                                                             \ 
  NOT_FOUND;                                                    \ 
}while(0) 
 
 
#define LOW_FIND2(FUN, KEY, FOUND, NOT_FOUND) do {            \ 
  struct keypair *k2;                                           \ 
  md=m->data;                                                   \ 
  add_ref(md);                                                  \ 
  if(md->hashsize)                                              \ 
  {                                                             \ 
    h=h2 & (md->hashsize-1);                                    \ 
    DO_IF_DEBUG( if(d_flag > 1) check_mapping_type_fields(m); ) \ 
    if(check_type_contains(md->ind_types, key))                 \ 
    {                                                           \ 
      k2=omd->hash[h2 & (omd->hashsize - 1)];                           \ 
      prev= md->hash + h;                                       \ 
      for(;(k=*prev) && k2;(prev=&k->next),(k2=k2->next))       \ 
        if(!(h2 == k->hval && is_identical(&k2->ind, &k->ind))) \ 
           break;                                               \ 
      for(;(k=*prev);prev=&k->next)                             \ 
      {                                                         \ 
        if(FUN(& k->ind, KEY))                                  \ 
        {                                                       \ 
          FOUND;                                                \ 
        }                                                       \ 
      }                                                         \ 
    }                                                           \ 
  }                                                             \ 
  NOT_FOUND;                                                    \ 
}while(0) 
 
 
#define SAME_DATA(SAME,NOT_SAME)      \ 
  if(m->data == md)                             \ 
  {                                             \ 
    SAME;                                       \ 
  }else{                                        \ 
    NOT_SAME;                                   \ 
  } 
 
 
/* FIXME: the 'no action' might need to be fixed.... */ 
#define FIND() do{                                            \ 
  LOW_FIND(is_eq, key,                                          \ 
           omd=md;                                              \ 
           SAME_DATA( break,                                    \ 
                     struct svalue *tmp=&k->ind;                \ 
                     LOW_FIND(is_identical, tmp, break, ;);     \ 
                     free_mapping_data(omd)),                   \ 
           break);                                              \ 
    free_mapping_data(md);                                      \ 
}while(0) 
 
       
/* Assumes md is *NOT* locked */ 
#define COPYMAP2() do {                               \ 
  ptrdiff_t off;                                \ 
  m->data=copy_mapping_data(m->data);           \ 
  debug_malloc_touch(m->data);                  \ 
  DO_IF_DEBUG( if(d_flag>1)  check_mapping(m); ) \ 
  off=((char *)m->data)-((char *)md);           \ 
  LOW_RELOC(k);                                 \ 
  LOW_RELOC(prev);                              \ 
  md=m->data;                                   \ 
}while(0) 
 
#define PREPARE_FOR_DATA_CHANGE2() \ 
 if(md->valrefs) COPYMAP2() 
 
#define PREPARE_FOR_INDEX_CHANGE2() \ 
  if(md->refs>1) COPYMAP2() 
 
#define PROPAGATE() do {                      \ 
   if(md->refs==1)                              \ 
   {                                            \ 
     h=h2 & (md->hashsize - 1);                 \ 
     *prev=k->next;                             \ 
     k->next=md->hash[h];                       \ 
     md->hash[h]=k;                             \ 
   }                                            \ 
 }while(0) 
 
 
/* Assumes md is locked */ 
#define COPYMAP() do {                                \ 
  ptrdiff_t off;                                \ 
  m->data=copy_mapping_data(m->data);           \ 
  debug_malloc_touch(m->data);                  \ 
  off=((char *)m->data)-((char *)md);           \ 
  LOW_RELOC(k);                                 \ 
  free_mapping_data(md);                        \ 
  md=m->data;                                   \ 
  add_ref(md);                                   \ 
}while(0) 
 
#define PREPARE_FOR_DATA_CHANGE() \ 
 if(md->valrefs) COPYMAP() 
 
#define PREPARE_FOR_INDEX_CHANGE() \ 
 if(md->refs>1) COPYMAP() 
 
/** This function brings the type fields in the mapping up to speed. 
 * It should be used only when the type fields MUST be correct, which is not 
 * very often. 
 */ 
PMOD_EXPORT void mapping_fix_type_field(struct mapping *m) 
{ 
  INT32 e; 
  struct keypair *k; 
  TYPE_FIELD ind_types, val_types; 
 
  val_types = ind_types = 0; 
 
  NEW_MAPPING_LOOP(m->data) 
    { 
      val_types |= 1 << TYPEOF(k->val); 
      ind_types |= 1 << TYPEOF(k->ind); 
    } 
 
#ifdef PIKE_DEBUG 
  if(val_types & ~(m->data->val_types)) 
    Pike_fatal("Mapping value types out of order!\n"); 
 
  if(ind_types & ~(m->data->ind_types)) 
    Pike_fatal("Mapping indices types out of order!\n"); 
#endif 
  m->data->val_types = val_types; 
  m->data->ind_types = ind_types; 
} 
 
PMOD_EXPORT void mapping_set_flags(struct mapping *m, int flags) 
{ 
  struct mapping_data *md = m->data; 
 
  if ((md->flags != flags) && (md->refs > 1)) { 
    struct keypair *k = NULL, *prev = NULL; 
    COPYMAP2(); 
    md = m->data; 
  } 
#ifdef PIKE_DEBUG 
  if(flags & MAPPING_WEAK) 
  { 
    debug_malloc_touch(m); 
    debug_malloc_touch(md); 
  } 
  else 
  { 
    debug_malloc_touch(m); 
    debug_malloc_touch(md); 
  } 
#endif 
  md->flags = flags; 
} 
 
PMOD_EXPORT void low_mapping_insert(struct mapping *m, 
                                    const struct svalue *key, 
                                    const struct svalue *val, 
                                    int overwrite) 
{ 
  unsigned INT32 h,h2; 
  struct keypair *k, **prev; 
  struct mapping_data *md, *omd; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  h2=hash_svalue(key); 
   
#ifdef PIKE_DEBUG 
  if(d_flag>1)  check_mapping(m); 
#endif 
 
  LOW_FIND(is_eq, key, 
           struct svalue *tmp=&k->ind; 
           SAME_DATA(goto mi_set_value, 
                     omd=md; 
                     LOW_FIND(is_identical, tmp, 
                              free_mapping_data(omd); 
                              goto mi_set_value, 
                              free_mapping_data(omd); 
                              goto mi_do_nothing)), 
           Z: 
           SAME_DATA(goto mi_insert, 
                     omd=md; 
                     LOW_FIND2(is_eq, key, 
                               free_mapping_data(omd); 
                               goto mi_do_nothing, 
                               free_mapping_data(omd); 
                               goto Z))); 
 
 mi_do_nothing: 
  free_mapping_data(md); 
  return; 
 
 mi_set_value: 
#ifdef PIKE_DEBUG 
  if(m->data != md) 
    Pike_fatal("Wrong dataset in mapping_insert!\n"); 
  if(d_flag>1)  check_mapping(m); 
#endif 
  free_mapping_data(md); 
  if(!overwrite) return; 
  PREPARE_FOR_DATA_CHANGE2(); 
  PROPAGATE(); /* propagate after preparing */ 
  md->val_types |= 1 << TYPEOF(*val); 
  if (overwrite == 2 && TYPEOF(*key) == T_OBJECT) 
    /* Should replace the index too. It's only for objects that it's 
     * possible to tell the difference. */ 
    assign_svalue (&k->ind, key); 
  assign_svalue(& k->val, val); 
#ifdef PIKE_DEBUG 
  if(d_flag>1)  check_mapping(m); 
#endif 
  return; 
 
 mi_insert: 
#ifdef PIKE_DEBUG 
  if(m->data != md) 
    Pike_fatal("Wrong dataset in mapping_insert!\n"); 
  if(d_flag>1)  check_mapping(m); 
#endif 
  free_mapping_data(md); 
  /* We do a re-hash here instead of copying the mapping. */ 
  if( 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
     (!md->free_list) || 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
     (md->size >= md->num_keypairs) || 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
     md->refs>1) 
  { 
    debug_malloc_touch(m); 
    rehash(m, md->size * 2 + 2); 
    md=m->data; 
  } 
  h=h2 & ( md->hashsize - 1); 
 
  /* no need to lock here since we are not calling is_eq - Hubbe */ 
 
  k=md->free_list; 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
  md->free_list=k->next; 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
  md->free_list++; 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
  k->next=md->hash[h]; 
  md->hash[h]=k; 
  md->ind_types |= 1 << TYPEOF(*key); 
  md->val_types |= 1 << TYPEOF(*val); 
  assign_svalue_no_free(& k->ind, key); 
  assign_svalue_no_free(& k->val, val); 
  k->hval = h2; 
  md->size++; 
#ifdef MAPPING_SIZE_DEBUG 
  if(m->data ==md) 
    m->debug_size++; 
#endif 
 
#ifdef PIKE_DEBUG 
  if(d_flag>1)  check_mapping(m); 
#endif 
} 
 
PMOD_EXPORT void mapping_insert(struct mapping *m, 
                                const struct svalue *key, 
                                const struct svalue *val) 
{ 
  low_mapping_insert(m,key,val,1); 
} 
 
/* Inline the above in this file. */ 
#define mapping_insert(M, KEY, VAL) low_mapping_insert ((M), (KEY), (VAL), 1) 
 
PMOD_EXPORT union anything *mapping_get_item_ptr(struct mapping *m, 
                                     const struct svalue *key, 
                                     TYPE_T t) 
{ 
  unsigned INT32 h, h2; 
  struct keypair *k, **prev; 
  struct mapping_data *md,*omd; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
 
  if(d_flag>1)  check_mapping(m); 
 
  debug_malloc_touch(m); 
  debug_malloc_touch(m->data); 
#endif 
 
  h2=hash_svalue(key); 
 
  LOW_FIND(is_eq, key, 
           struct svalue *tmp=&k->ind; 
           SAME_DATA(goto mg_set_value, 
                     omd=md; 
                     LOW_FIND(is_identical, tmp, 
                              free_mapping_data(omd); 
                              goto mg_set_value, 
                              free_mapping_data(omd); 
                              goto mg_do_nothing)), 
           Z: 
           SAME_DATA(goto mg_insert, 
                     omd=md; 
                     LOW_FIND2(is_eq, key, 
                               free_mapping_data(omd); 
                               goto mg_do_nothing, 
                               free_mapping_data(omd); 
                               goto Z))); 
 
 
 
 mg_do_nothing: 
  free_mapping_data(md); 
  return 0; 
 
 mg_set_value: 
#ifdef PIKE_DEBUG 
  if(m->data != md) 
    Pike_fatal("Wrong dataset in mapping_get_item_ptr!\n"); 
  if(d_flag) 
    check_mapping(m); 
#endif 
  free_mapping_data(md); 
  if(TYPEOF(k->val) == t) 
  { 
    PREPARE_FOR_DATA_CHANGE2(); 
    PROPAGATE(); /* prepare then propagate */ 
     
    return & ( k->val.u ); 
  } 
   
  return 0; 
   
 mg_insert: 
#ifdef PIKE_DEBUG 
  if(m->data != md) 
    Pike_fatal("Wrong dataset in mapping_get_item_ptr!\n"); 
  if(d_flag) 
    check_mapping(m); 
#endif 
  free_mapping_data(md); 
 
  if(t != T_INT) return 0; 
 
  /* no need to call PREPARE_* because we re-hash instead */ 
  if( 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
     !(md->free_list) || 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
     (md->size >= md->num_keypairs) || 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
     md->refs>1) 
  { 
    debug_malloc_touch(m); 
    rehash(m, md->size * 2 + 2); 
    md=m->data; 
  } 
  h=h2 & ( md->hashsize - 1); 
 
  k=md->free_list; 
#ifndef PIKE_MAPPING_KEYPAIR_LOOP 
  md->free_list=k->next; 
#else /* PIKE_MAPPING_KEYPAIR_LOOP */ 
  md->free_list++; 
#endif /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
  k->next=md->hash[h]; 
  md->hash[h]=k; 
  assign_svalue_no_free(& k->ind, key); 
  SET_SVAL(k->val, T_INT, NUMBER_NUMBER, integer, 0); 
  k->hval = h2; 
  md->ind_types |= 1 << TYPEOF(*key); 
  md->val_types |= BIT_INT; 
  md->size++; 
#ifdef MAPPING_SIZE_DEBUG 
  if(m->data ==md) 
    m->debug_size++; 
#endif 
 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) check_mapping_type_fields(m); 
#endif 
 
  return & ( k->val.u ); 
} 
 
PMOD_EXPORT void map_delete_no_free(struct mapping *m, 
                        const struct svalue *key, 
                        struct svalue *to) 
{ 
  unsigned INT32 h,h2; 
  struct keypair *k, **prev; 
  struct mapping_data *md,*omd; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
  if(d_flag>1)  check_mapping(m); 
  debug_malloc_touch(m); 
#endif 
 
  h2=hash_svalue(key); 
 
  LOW_FIND(is_eq, key, 
           struct svalue *tmp=&k->ind; 
           SAME_DATA(goto md_remove_value, 
                     omd=md; 
                     LOW_FIND(is_identical, tmp, 
                              free_mapping_data(omd); 
                              goto md_remove_value, 
                              free_mapping_data(omd); 
                              goto md_do_nothing)), 
           goto md_do_nothing); 
 
 md_do_nothing: 
  debug_malloc_touch(m); 
  free_mapping_data(md); 
  if(to) 
  { 
    SET_SVAL(*to, T_INT, NUMBER_UNDEFINED, integer, 0); 
  } 
  return; 
 
 md_remove_value: 
#ifdef PIKE_DEBUG 
  if(md->refs <= 1) 
    Pike_fatal("Too few refs i mapping->data\n"); 
  if(m->data != md) 
    Pike_fatal("Wrong dataset in mapping_delete!\n"); 
  if(d_flag>1)  check_mapping(m); 
  debug_malloc_touch(m); 
#endif 
  free_mapping_data(md); 
  PREPARE_FOR_INDEX_CHANGE2(); 
  /* No need to propagate */ 
  *prev=k->next; 
  free_svalue(& k->ind); 
  if(to) 
    move_svalue (to, &k->val); 
  else 
    free_svalue(& k->val); 
 
  FREE_KEYPAIR(md, k); 
 
  mark_free_svalue (&md->free_list->ind); 
  mark_free_svalue (&md->free_list->val); 
 
  md->size--; 
#ifdef MAPPING_SIZE_DEBUG 
  if(m->data ==md) 
    m->debug_size--; 
#endif 
   
  if(md->size < (md->hashsize + 1) * MIN_LINK_LENGTH) 
  { 
    debug_malloc_touch(m); 
    rehash(m, MAP_SLOTS(m->data->size)); 
  } 
 
#ifdef PIKE_DEBUG 
  if(d_flag>1)  check_mapping(m); 
#endif 
  return; 
} 
 
PMOD_EXPORT void check_mapping_for_destruct(struct mapping *m) 
{ 
  INT32 e; 
  struct keypair *k, **prev; 
  TYPE_FIELD ind_types, val_types; 
  struct mapping_data *md=m->data; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
  if(d_flag>1)  check_mapping(m); 
  debug_malloc_touch(m); 
  if (Pike_in_gc > GC_PASS_PREPARE && Pike_in_gc < GC_PASS_FREE) 
    Pike_fatal("check_mapping_for_destruct called in invalid pass inside gc.\n"); 
#endif 
 
  /* no is_eq -> no locking */ 
 
  if(!md->size) return; 
 
  if((md->ind_types | md->val_types) & (BIT_OBJECT | BIT_FUNCTION)) 
  { 
    val_types = ind_types = 0; 
    md->val_types |= BIT_INT; 
    for(e=0;e<md->hashsize;e++) 
    { 
      for(prev= md->hash + e;(k=*prev);) 
      { 
        check_destructed(& k->val); 
         
        if((TYPEOF(k->ind) == T_OBJECT || TYPEOF(k->ind) == T_FUNCTION) && 
           !k->ind.u.object->prog) 
        { 
          debug_malloc_touch(m); 
          debug_malloc_touch(md); 
          PREPARE_FOR_INDEX_CHANGE2(); 
          *prev=k->next; 
          free_svalue(& k->ind); 
          free_svalue(& k->val); 
          FREE_KEYPAIR(md, k); 
          mark_free_svalue (&md->free_list->ind); 
          mark_free_svalue (&md->free_list->val); 
          md->size--; 
 
#ifdef MAPPING_SIZE_DEBUG 
          if(m->data ==md) 
          { 
            m->debug_size++; 
            debug_malloc_touch(m); 
          } 
#endif 
          debug_malloc_touch(md); 
        }else{ 
          val_types |= 1 << TYPEOF(k->val); 
          ind_types |= 1 << TYPEOF(k->ind); 
          prev=&k->next; 
        } 
      } 
    } 
 
    md->val_types = val_types; 
    md->ind_types = ind_types; 
 
    if(MAP_SLOTS(md->size) < md->hashsize * MIN_LINK_LENGTH) 
    { 
      debug_malloc_touch(m); 
      rehash(m, MAP_SLOTS(md->size)); 
    } 
 
#ifdef PIKE_DEBUG 
    if(d_flag>1)  check_mapping(m); 
#endif 
  } 
} 
 
PMOD_EXPORT struct svalue *low_mapping_lookup(struct mapping *m, 
                                              const struct svalue *key) 
{ 
  unsigned INT32 h,h2; 
  struct keypair *k=0, **prev=0; 
  struct mapping_data *md, *omd; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
  if(d_flag>1)  check_mapping(m); 
#endif 
 
  h2=hash_svalue(key); 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) check_mapping_type_fields(m); 
#endif 
  FIND(); 
  if(k) 
  { 
    PROPAGATE(); 
    return &k->val; 
  } 
  return 0; 
} 
 
PMOD_EXPORT struct svalue *low_mapping_string_lookup(struct mapping *m, 
                                                     struct pike_string *p) 
{ 
  struct svalue tmp; 
  SET_SVAL(tmp, T_STRING, 0, string, p); 
  return low_mapping_lookup(m, &tmp); 
} 
 
PMOD_EXPORT void mapping_string_insert(struct mapping *m, 
                                       struct pike_string *p, 
                                       const struct svalue *val) 
{ 
  struct svalue tmp; 
  SET_SVAL(tmp, T_STRING, 0, string, p); 
  mapping_insert(m, &tmp, val); 
} 
 
PMOD_EXPORT void mapping_string_insert_string(struct mapping *m, 
                                  struct pike_string *p, 
                                  struct pike_string *val) 
{ 
  struct svalue tmp; 
  SET_SVAL(tmp, T_STRING, 0, string, val); 
  mapping_string_insert(m, p, &tmp); 
} 
 
PMOD_EXPORT struct svalue *simple_mapping_string_lookup(struct mapping *m, 
                                                        const char *p) 
{ 
  struct pike_string *tmp; 
  if((tmp=findstring(p))) 
    return low_mapping_string_lookup(m,tmp); 
  return 0; 
} 
 
/* Lookup in a mapping of mappings */ 
PMOD_EXPORT struct svalue *mapping_mapping_lookup(struct mapping *m, 
                                                  const struct svalue *key1, 
                                                  const struct svalue *key2, 
                                                  int create) 
{ 
  struct svalue tmp; 
  struct mapping *m2; 
  struct svalue *s=low_mapping_lookup(m, key1); 
  debug_malloc_touch(m); 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  if(!s || TYPEOF(*s) != T_MAPPING) 
  { 
    if(!create) return 0; 
    SET_SVAL(tmp, T_MAPPING, 0, mapping, allocate_mapping(5)); 
    mapping_insert(m, key1, &tmp); 
    debug_malloc_touch(m); 
    debug_malloc_touch(tmp.u.mapping); 
    free_mapping(tmp.u.mapping); /* There is one ref in 'm' */ 
    s=&tmp; 
  } 
 
  m2=s->u.mapping; 
  debug_malloc_touch(m2); 
  s=low_mapping_lookup(m2, key2); 
  if(s) return s; 
  if(!create) return 0; 
 
  SET_SVAL(tmp, T_INT, NUMBER_UNDEFINED, integer, 0); 
 
  mapping_insert(m2, key2, &tmp); 
  debug_malloc_touch(m2); 
 
  return low_mapping_lookup(m2, key2); 
} 
 
 
PMOD_EXPORT struct svalue *mapping_mapping_string_lookup(struct mapping *m, 
                                      struct pike_string *key1, 
                                      struct pike_string *key2, 
                                      int create) 
{ 
  struct svalue k1,k2; 
  SET_SVAL(k1, T_STRING, 0, string, key1); 
  SET_SVAL(k2, T_STRING, 0, string, key2); 
  return mapping_mapping_lookup(m,&k1,&k2,create); 
} 
 
 
 
PMOD_EXPORT void mapping_index_no_free(struct svalue *dest, 
                           struct mapping *m, 
                           const struct svalue *key) 
{ 
  struct svalue *p; 
 
  if(!IS_DESTRUCTED (key) && (p=low_mapping_lookup(m,key))) 
  { 
    assign_svalue_no_free(dest, p); 
 
    /* Never return NUMBER_UNDEFINED for existing entries. */ 
    /* Note: There is code that counts on storing UNDEFINED in mapping 
     * values (using e.g. low_mapping_lookup to get them), so we have 
     * to fix the subtype here rather than in mapping_insert. */ 
    if(TYPEOF(*p) == T_INT) 
      SET_SVAL_SUBTYPE(*dest, NUMBER_NUMBER); 
  }else{ 
    SET_SVAL(*dest, T_INT, NUMBER_UNDEFINED, integer, 0); 
  } 
} 
 
PMOD_EXPORT struct array *mapping_indices(struct mapping *m) 
{ 
  INT32 e; 
  struct array *a; 
  struct svalue *s; 
  struct keypair *k; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  check_mapping_for_destruct(m); 
 
  a=allocate_array(m->data->size); 
  s=ITEM(a); 
 
  /* no locking required */ 
  NEW_MAPPING_LOOP(m->data) assign_svalue(s++, & k->ind); 
 
  a->type_field = m->data->ind_types; 
   
#ifdef PIKE_DEBUG 
  if(d_flag > 1) check_mapping_type_fields(m); 
#endif 
 
  return a; 
} 
 
PMOD_EXPORT struct array *mapping_values(struct mapping *m) 
{ 
  INT32 e; 
  struct keypair *k; 
  struct array *a; 
  struct svalue *s; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  check_mapping_for_destruct(m); 
 
  a=allocate_array(m->data->size); 
  s=ITEM(a); 
 
  /* no locking required */ 
  NEW_MAPPING_LOOP(m->data) assign_svalue(s++, & k->val); 
 
  a->type_field = m->data->val_types; 
 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) check_mapping_type_fields(m); 
#endif 
   
  return a; 
} 
 
PMOD_EXPORT struct array *mapping_to_array(struct mapping *m) 
{ 
  INT32 e; 
  struct keypair *k; 
  struct array *a; 
  struct svalue *s; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  a=allocate_array(m->data->size); 
  s=ITEM(a); 
 
  /* no locking required */ 
  NEW_MAPPING_LOOP(m->data) 
    { 
      struct array *b=allocate_array(2); 
      assign_svalue(b->item+0, & k->ind); 
      assign_svalue(b->item+1, & k->val); 
      b->type_field = (1 << TYPEOF(k->ind)) | (1 << TYPEOF(k->val)); 
      SET_SVAL(*s, T_ARRAY, 0, array, b); 
      s++; 
    } 
  a->type_field = BIT_ARRAY; 
 
  return a; 
} 
 
PMOD_EXPORT void mapping_replace(struct mapping *m,struct svalue *from, struct svalue *to) 
{ 
  INT32 e; 
  struct keypair *k; 
  struct mapping_data *md; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  md=m->data; 
  if(md->size) 
  { 
    add_ref(md); 
    NEW_MAPPING_LOOP(md) 
      { 
        if(is_eq(& k->val, from)) 
        { 
          PREPARE_FOR_DATA_CHANGE(); 
          assign_svalue(& k->val, to); 
          md->val_types |= 1<<TYPEOF(*to); 
        } 
      } 
    free_mapping_data(md); 
  } 
 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) check_mapping_type_fields(m); 
#endif 
} 
 
PMOD_EXPORT struct mapping *mkmapping(struct array *ind, struct array *val) 
{ 
  struct mapping *m; 
  struct svalue *i,*v; 
  INT32 e; 
 
#ifdef PIKE_DEBUG 
  if(ind->size != val->size) 
    Pike_fatal("mkmapping on different sized arrays.\n"); 
#endif 
 
  m=allocate_mapping(MAP_SLOTS(ind->size)); 
  i=ITEM(ind); 
  v=ITEM(val); 
  for(e=0;e<ind->size;e++) low_mapping_insert(m, i++, v++, 2); 
 
  return m; 
} 
 
/* deferred mapping copy! */ 
PMOD_EXPORT struct mapping *copy_mapping(struct mapping *m) 
{ 
  struct mapping *n; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  n=allocate_mapping(0); 
  debug_malloc_touch(n->data); 
  free_mapping_data(n->data); 
  n->data=m->data; 
#ifdef MAPPING_SIZE_DEBUG 
  n->debug_size=n->data->size; 
#endif 
  add_ref(n->data); 
  n->data->valrefs++; 
  n->data->hardlinks++; 
  debug_malloc_touch(n->data); 
  return n; 
} 
 
/* copy_mapping() for when destructive operations are ok. 
 * 
 * Note: It destructive operations on the resulting mapping *will* 
 *       affect eg NEW_MAPPING_LOOP() on the original mapping. 
 */ 
static struct mapping *destructive_copy_mapping(struct mapping *m) 
{ 
  if ((m->refs == 1) && !m->data->hardlinks && 
      !(m->data->flags & MAPPING_WEAK)) { 
    /* We may perform destructive operations on the mapping. */ 
    add_ref(m); 
    return m; 
  } 
  return copy_mapping(m); 
} 
 
/* NOTE: May perform destructive operations on either of the arguments 
 *       if it has only a single reference. 
 */ 
static struct mapping *subtract_mappings(struct mapping *a, struct mapping *b) 
{ 
  struct mapping *res; 
  struct keypair *k; 
  struct mapping_data *a_md = a->data; 
  struct mapping_data *b_md = b->data; 
  INT32 e; 
  ONERROR err; 
 
  /* First some special cases. */ 
  if (!a_md->size || !b_md->size || !a_md->hashsize || !b_md->hashsize) { 
    return destructive_copy_mapping(a); 
  } 
  if (a_md == b_md) { 
    return allocate_mapping(0); 
  } 
  /* FIXME: The break-even point should probably be researched. */ 
  if (a_md->size < b_md->size) { 
    /* Add the elements in a that aren't in b. */ 
    res = allocate_mapping(a_md->size); 
    SET_ONERROR(err, do_free_mapping, res); 
    NEW_MAPPING_LOOP(a_md) { 
      size_t h = k->hval & ( b_md->hashsize - 1); 
      struct keypair *k2; 
      for (k2 = b_md->hash[h]; k2; k2 = k2->next) { 
        if ((k2->hval == k->hval) && is_eq(&k2->ind, &k->ind)) { 
          break; 
        } 
      } 
      if (!k2) { 
        mapping_insert(res, &k->ind, &k->val); 
      } 
    } 
  } else { 
    /* Remove the elements in a that are in b. */ 
    res = destructive_copy_mapping(a); 
    SET_ONERROR(err, do_free_mapping, res); 
    NEW_MAPPING_LOOP(b_md) { 
      map_delete(res, &k->ind); 
    } 
  } 
  UNSET_ONERROR(err); 
  return res; 
} 
 
/* NOTE: May perform destructive operations on either of the arguments 
 *       if it has only a single reference. 
 */ 
static struct mapping *and_mappings(struct mapping *a, struct mapping *b) 
{ 
  struct mapping *res; 
  struct keypair *k; 
  struct mapping_data *a_md = a->data; 
  struct mapping_data *b_md = b->data; 
  INT32 e; 
  ONERROR err; 
 
  /* First some special cases. */ 
  if (!a_md->size || !b_md->size) return allocate_mapping(0); 
  if (a_md == b_md) return destructive_copy_mapping(a); 
 
  /* Copy the second mapping. */ 
  res = copy_mapping(b); 
  SET_ONERROR(err, do_free_mapping, res); 
 
  /* Remove elements in res that aren't in a. */ 
  NEW_MAPPING_LOOP(b_md) { 
    size_t h = k->hval & ( a_md->hashsize - 1); 
    struct keypair *k2; 
    for (k2 = a_md->hash[h]; k2; k2 = k2->next) { 
      if ((k2->hval == k->hval) && is_eq(&k2->ind, &k->ind)) { 
        break; 
      } 
    } 
    if (!k2) { 
      map_delete(res, &k->ind); 
    } 
  } 
  UNSET_ONERROR(err); 
  return res; 
} 
 
/* NOTE: May perform destructive operations on either of the arguments 
 *       if it has only a single reference. 
 */ 
static struct mapping *or_mappings(struct mapping *a, struct mapping *b) 
{ 
  struct mapping *res; 
  struct keypair *k; 
  struct mapping_data *a_md = a->data; 
  struct mapping_data *b_md = b->data; 
  INT32 e; 
  ONERROR err; 
 
  /* First some special cases. */ 
  if (!a_md->size) return destructive_copy_mapping(b); 
  if (!b_md->size) return destructive_copy_mapping(a); 
  if (a_md == b_md) return destructive_copy_mapping(a); 
 
  if (a_md->size <= b_md->size) { 
    /* Copy the second mapping. */ 
    res = destructive_copy_mapping(b); 
    SET_ONERROR(err, do_free_mapping, res); 
 
    if (!b_md->hashsize) { 
      Pike_fatal("Invalid hashsize.\n"); 
    } 
 
    /* Add elements in a that aren't in b. */ 
    NEW_MAPPING_LOOP(a_md) { 
      size_t h = k->hval & ( b_md->hashsize - 1); 
      struct keypair *k2; 
      for (k2 = b_md->hash[h]; k2; k2 = k2->next) { 
        if ((k2->hval == k->hval) && is_eq(&k2->ind, &k->ind)) { 
          break; 
        } 
      } 
      if (!k2) { 
        mapping_insert(res, &k->ind, &k->val); 
        b_md = b->data; 
      } 
    } 
    UNSET_ONERROR(err); 
  } else { 
    /* Copy the first mapping. */ 
    res = destructive_copy_mapping(a); 
    SET_ONERROR(err, do_free_mapping, res); 
 
    /* Add all elements in b. */ 
    NEW_MAPPING_LOOP(b_md) { 
      mapping_insert(res, &k->ind, &k->val); 
    } 
    UNSET_ONERROR(err); 
  } 
  return res; 
} 
 
/* NOTE: May perform destructive operations on either of the arguments 
 *       if it has only a single reference. 
 */ 
static struct mapping *xor_mappings(struct mapping *a, struct mapping *b) 
{ 
  struct mapping *res; 
  struct keypair *k; 
  struct mapping_data *a_md = a->data; 
  struct mapping_data *b_md = b->data; 
  INT32 e; 
  ONERROR err; 
 
  /* First some special cases. */ 
  if (!a_md->size) return destructive_copy_mapping(b); 
  if (!b_md->size) return destructive_copy_mapping(a); 
  if (a_md == b_md) return allocate_mapping(0); 
 
  /* Copy the largest mapping. */ 
  if (a_md->size > b_md->size) { 
    struct mapping *tmp = a; 
    a = b; 
    b = tmp; 
    a_md = b_md; 
    b_md = b->data; 
  }     
  res = destructive_copy_mapping(b); 
  SET_ONERROR(err, do_free_mapping, res); 
 
  /* Add elements in a that aren't in b, and remove those that are. */ 
  NEW_MAPPING_LOOP(a_md) { 
    size_t h = k->hval & ( b_md->hashsize - 1); 
    struct keypair *k2; 
    for (k2 = b_md->hash[h]; k2; k2 = k2->next) { 
      if ((k2->hval == k->hval) && is_eq(&k2->ind, &k->ind)) { 
        break; 
      } 
    } 
    if (!k2) { 
      mapping_insert(res, &k->ind, &k->val); 
    } else { 
      map_delete(res, &k2->ind); 
    } 
    b_md = b->data; 
  } 
  UNSET_ONERROR(err); 
  return res; 
} 
 
/* NOTE: May perform destructive operations on either of the arguments 
 *       if it has only a single reference. 
 */ 
PMOD_EXPORT struct mapping *merge_mappings(struct mapping *a, struct mapping *b, INT32 op) 
{ 
  ONERROR r1,r2,r3,r4; 
  struct array *ai, *av; 
  struct array *bi, *bv; 
  struct array *ci, *cv; 
  INT32 *zipper; 
  struct mapping *m; 
 
#ifdef PIKE_DEBUG 
  if(a->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
  if(b->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  switch (op) { 
  case PIKE_ARRAY_OP_SUB: 
    return subtract_mappings(a, b); 
  case PIKE_ARRAY_OP_AND: 
    return and_mappings(a, b); 
  case PIKE_ARRAY_OP_OR: 
    return or_mappings(a, b); 
  case PIKE_ARRAY_OP_XOR: 
    return xor_mappings(a, b); 
  } 
 
  ai=mapping_indices(a); 
  SET_ONERROR(r1,do_free_array,ai); 
 
  av=mapping_values(a); 
  SET_ONERROR(r2,do_free_array,av); 
 
  if(ai->size > 1) 
  { 
    zipper=get_set_order(ai); 
    order_array(ai, zipper); 
    order_array(av, zipper); 
    free(zipper); 
  } 
 
  bi=mapping_indices(b); 
  SET_ONERROR(r3,do_free_array,bi); 
 
  bv=mapping_values(b); 
  SET_ONERROR(r4,do_free_array,bv); 
 
  if(bi->size > 1) 
  { 
    zipper=get_set_order(bi); 
    order_array(bi, zipper); 
    order_array(bv, zipper); 
    free(zipper); 
  } 
 
  zipper=merge(ai,bi,op); 
 
  ci=array_zip(ai,bi,zipper); 
 
  UNSET_ONERROR(r4); free_array(bi); 
  UNSET_ONERROR(r3); free_array(ai); 
 
  cv=array_zip(av,bv,zipper); 
 
  UNSET_ONERROR(r2); free_array(bv); 
  UNSET_ONERROR(r1); free_array(av); 
 
  free(zipper); 
 
  m=mkmapping(ci, cv); 
  free_array(ci); 
  free_array(cv); 
 
  return m; 
} 
 
/* FIXME: What are the semantics for this function? 
 * FIXME: It ought to be optimized just like the unordered variant. 
 *      /grubba 2003-11-12 
 */ 
PMOD_EXPORT struct mapping *merge_mapping_array_ordered(struct mapping *a,  
                                            struct array *b, INT32 op) 
{ 
  ONERROR r1,r2; 
  struct array *ai, *av; 
  struct array *ci = NULL, *cv = NULL; 
  INT32 *zipper = NULL; 
  struct mapping *m; 
 
  ai=mapping_indices(a); 
  SET_ONERROR(r1,do_free_array,ai); 
  av=mapping_values(a); 
  SET_ONERROR(r2,do_free_array,av); 
  if(ai->size > 1) 
  { 
    zipper=get_set_order(ai); 
    order_array(ai, zipper); 
    order_array(av, zipper); 
    free(zipper); 
  } 
 
  switch (op) /* no elements from »b» may be selected */ 
  { 
     case PIKE_ARRAY_OP_AND: 
        zipper=merge(b,ai,op); 
        ci=array_zip(b,ai,zipper); /* b must not be used */ 
        cv=array_zip(b,av,zipper); /* b must not be used */ 
        break; 
     case PIKE_ARRAY_OP_SUB: 
        zipper=merge(ai,b,op); 
        ci=array_zip(ai,b,zipper); /* b must not be used */ 
        cv=array_zip(av,b,zipper); /* b must not be used */ 
        break; 
     default: 
        Pike_fatal("merge_mapping_array on other than AND or SUB\n"); 
  } 
 
  UNSET_ONERROR(r2); free_array(av); 
  UNSET_ONERROR(r1); free_array(ai); 
   
  free(zipper); 
 
  m=mkmapping(ci, cv); 
  free_array(ci); 
  free_array(cv); 
 
  return m; 
} 
 
PMOD_EXPORT struct mapping *merge_mapping_array_unordered(struct mapping *a,  
                                              struct array *b, INT32 op) 
{ 
  ONERROR r1; 
  struct array *b_temp; 
  INT32 *zipper; 
  struct mapping *m; 
 
  if (b->size>1) 
  { 
    zipper=get_set_order(b); 
    b_temp=reorder_and_copy_array(b,zipper); 
    free (zipper); 
    SET_ONERROR(r1,do_free_array,b_temp); 
    m=merge_mapping_array_ordered(a,b_temp,op); 
    UNSET_ONERROR(r1); free_array(b_temp); 
  } 
  else 
    m=merge_mapping_array_ordered(a,b,op); 
 
  return m; 
} 
 
void o_append_mapping( INT32 args ) 
{ 
  struct svalue *lval = Pike_sp - args; 
  struct svalue *val = lval + 2; 
  int lvalue_type; 
 
#ifdef PIKE_DEBUG 
  if (args < 3) { 
    Pike_fatal("Too few arguments to o_append_mapping(): %d\n", args); 
  } 
#endif 
  args -= 3; 
  /* Note: val should always be a zero here! */ 
  lvalue_type = lvalue_to_svalue_no_free(val, lval); 
 
  if ((TYPEOF(*val) == T_MAPPING) && (lvalue_type != PIKE_T_GET_SET)) { 
    struct mapping *m = val->u.mapping; 
    if( m->refs == 2 ) 
    { 
      int i; 
      /* fprintf( stderr, "map_refs==2\n" ); */ 
      for( i=0; i<args; i+=2 ) 
        low_mapping_insert( m, Pike_sp-(i+2), Pike_sp-(i+1), 2 ); 
      stack_pop_n_elems_keep_top(2+args); 
      return; 
    } 
  } 
 
  f_aggregate_mapping(args); 
  f_add(2); 
  assign_lvalue(lval, val); 
  stack_pop_2_elems_keep_top(); 
} 
 
/* NOTE: May perform destructive operations on either of the arguments 
 *       if it has only a single reference. 
 */ 
PMOD_EXPORT struct mapping *add_mappings(struct svalue *argp, INT32 args) 
{ 
  INT32 e,d; 
  struct mapping *ret=0; 
  struct keypair *k; 
 
  for(e=d=0;d<args;d++) 
  { 
    struct mapping *m = argp[d].u.mapping; 
#ifdef PIKE_DEBUG 
    if(d_flag>1) check_mapping(m); 
#endif 
    e += m->data->size; 
  } 
 
  if(!e) return allocate_mapping(0); 
 
  d=0; 
 
  for(;d<args;d++) 
  { 
    struct mapping *m=argp[d].u.mapping; 
    struct mapping_data *md=m->data; 
 
    if(md->size == 0) continue; 
 
    if(!(md->flags  & MAPPING_WEAK)) 
    { 
#if 1 /* major optimization */ 
      if(e==md->size) 
        return copy_mapping(m); 
#endif 
     
      if(m->refs == 1 && !md->hardlinks) 
      { 
        add_ref( ret=m ); 
        d++; 
        break; 
      } 
    } 
    ret=allocate_mapping(MAP_SLOTS(e)); 
    break; 
 
  } 
 
  for(;d<args;d++) 
  { 
    struct mapping *m=argp[d].u.mapping; 
    struct mapping_data *md=m->data; 
     
    add_ref(md); 
    NEW_MAPPING_LOOP(md) 
      low_mapping_insert(ret, &k->ind, &k->val, 2); 
    free_mapping_data(md); 
  } 
#ifdef PIKE_DEBUG 
  if(!ret) 
    Pike_fatal("add_mappings is confused!\n"); 
#endif 
  return ret; 
} 
 
PMOD_EXPORT int mapping_equal_p(struct mapping *a, struct mapping *b, struct processing *p) 
{ 
  struct processing curr; 
  struct keypair *k; 
  struct mapping_data *md; 
  INT32 e,eq=1; 
 
#ifdef PIKE_DEBUG 
  if(a->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
  if(b->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  if(a==b) return 1; 
 
  if (a->data == b->data) return 1; 
 
  /* If either is weak, they're different. */ 
  if ((a->data->flags | b->data->flags) & MAPPING_WEAK) return 0; 
 
  check_mapping_for_destruct(a); 
  check_mapping_for_destruct(b); 
 
  if(m_sizeof(a) != m_sizeof(b)) return 0; 
 
  if (!check_type_overlaps(a->data->ind_types, b->data->ind_types) || 
      !check_type_overlaps(a->data->val_types, b->data->val_types)) return 0; 
 
  curr.pointer_a = a; 
  curr.pointer_b = b; 
  curr.next = p; 
 
  for( ;p ;p=p->next) 
    if(p->pointer_a == (void *)a && p->pointer_b == (void *)b) 
      return 1; 
 
  md=a->data; 
  md->valrefs++; 
  add_ref(md); 
 
  NEW_MAPPING_LOOP(md) 
  { 
    struct svalue *s; 
    if((s=low_mapping_lookup(b, & k->ind))) 
    { 
      if(!low_is_equal(s, &k->val, &curr)) 
      { 
        eq=0; 
        break; 
      } 
    }else{ 
      INT32 d; 
      struct mapping_data *bmd = b->data; 
      struct keypair *kp; 
 
      /* This is neither pretty nor fast, but it should 
       * perform a bit more like expected... -Hubbe 
       */ 
 
      bmd->valrefs++; 
      add_ref(bmd); 
 
      eq=0; 
      for(d=0;d<(bmd)->hashsize;d++) 
      { 
        for(kp=bmd->hash[d];kp;kp=kp->next) 
        { 
          if(low_is_equal(&k->ind, &kp->ind, &curr) && 
             low_is_equal(&k->val, &kp->val, &curr)) 
          { 
            eq=1; 
            break; 
          } 
        } 
      } 
 
      bmd->valrefs--; 
      free_mapping_data(bmd); 
 
      if(!eq) break; 
    } 
  } 
  md->valrefs--; 
  free_mapping_data(md); 
  return eq; 
} 
 
void describe_mapping(struct mapping *m,struct processing *p,int indent) 
{ 
  struct processing doing; 
  struct array *a; 
  JMP_BUF catch; 
  ONERROR err; 
  INT32 e,d; 
  char buf[40]; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  if(! m->data->size) 
  { 
    my_strcat("([ ])"); 
    return; 
  } 
 
  doing.next=p; 
  doing.pointer_a=(void *)m; 
  for(e=0;p;e++,p=p->next) 
  { 
    if(p->pointer_a == (void *)m) 
    { 
      sprintf(buf,"@%ld",(long)e); 
      my_strcat(buf); 
      return; 
    } 
  } 
 
  if (Pike_in_gc > GC_PASS_PREPARE && Pike_in_gc < GC_PASS_FREE) { 
    /* Have to do without any temporary allocations. */ 
    struct keypair *k; 
    int notfirst = 0; 
 
    if (m->data->size == 1) { 
      my_strcat("([ /* 1 element */\n"); 
    } else { 
      sprintf(buf, "([ /* %ld elements */\n", (long)m->data->size); 
      my_strcat(buf); 
    } 
 
    NEW_MAPPING_LOOP(m->data) { 
      if (notfirst) my_strcat(",\n"); 
      else notfirst = 1; 
      for(d = 0; d < indent; d++) 
        my_putchar(' '); 
      describe_svalue(&k->ind, indent+2, &doing); 
      my_strcat (": "); 
      describe_svalue(&k->val, indent+2, &doing); 
    } 
 
    my_putchar('\n'); 
    for(e=2; e<indent; e++) my_putchar(' '); 
    my_strcat("])"); 
    return; 
  } 
 
  a = mapping_indices(m); 
  SET_ONERROR(err, do_free_array, a); 
 
  if(! m->data->size) {             /* mapping_indices may remove elements */ 
    my_strcat("([ ])"); 
  } 
  else { 
    int save_t_flag = Pike_interpreter.trace_level; 
    dynamic_buffer save_buf; 
 
    if (m->data->size == 1) { 
      my_strcat("([ /* 1 element */\n"); 
    } else { 
      sprintf(buf, "([ /* %ld elements */\n", (long)m->data->size); 
      my_strcat(buf); 
    } 
 
    save_buffer (&save_buf); 
    Pike_interpreter.trace_level = 0; 
    if(SETJMP(catch)) { 
      free_svalue(&throw_value); 
      mark_free_svalue (&throw_value); 
    } 
    else 
      sort_array_destructively(a); 
    UNSETJMP(catch); 
    Pike_interpreter.trace_level = save_t_flag; 
    restore_buffer (&save_buf); 
 
    for(e = 0; e < a->size; e++) 
    { 
      struct svalue *tmp; 
      if(e) 
        my_strcat(",\n"); 
     
      for(d = 0; d < indent; d++) 
        my_putchar(' '); 
 
      describe_svalue(ITEM(a)+e, indent+2, &doing); 
      my_strcat (": "); 
 
      { 
        int save_t_flag=Pike_interpreter.trace_level; 
        Pike_interpreter.trace_level=0; 
         
        tmp=low_mapping_lookup(m, ITEM(a)+e); 
         
        Pike_interpreter.trace_level=save_t_flag; 
      } 
      if(tmp) 
        describe_svalue(tmp, indent+2, &doing); 
      else 
        my_strcat("** gone **"); 
    } 
 
    my_putchar('\n'); 
    for(e=2; e<indent; e++) my_putchar(' '); 
    my_strcat("])"); 
  } 
 
  UNSET_ONERROR(err); 
  free_array(a); 
} 
 
node *make_node_from_mapping(struct mapping *m) 
{ 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
  mapping_fix_type_field(m); 
 
  if(!mapping_is_constant(m,0)) 
  { 
    struct array *ind, *val; 
    node *n; 
    ind=mapping_indices(m); 
    val=mapping_values(m); 
    n=mkefuncallnode("mkmapping", 
                     mknode(F_ARG_LIST, 
                            make_node_from_array(ind), 
                            make_node_from_array(val))); 
    free_array(ind); 
    free_array(val); 
    return n; 
  }else{ 
    struct svalue s; 
 
    if(!m->data->size) 
      return mkefuncallnode("aggregate_mapping",0); 
 
    SET_SVAL(s, T_MAPPING, 0, mapping, m); 
    return mkconstantsvaluenode(&s); 
  } 
} 
 
/*! @decl mapping aggregate_mapping(mixed ... elems) 
 *! 
 *! Construct a mapping. 
 *! 
 *! Groups the arguments together two and two in key-index pairs and 
 *! creates a mapping of those pairs. Generally, the mapping literal 
 *! syntax is handier: @expr{([ key1:val1, key2:val2, ... ])@} 
 *! 
 *! @seealso 
 *!   @[sizeof()], @[mappingp()], @[mkmapping()] 
 */ 
PMOD_EXPORT void f_aggregate_mapping(INT32 args) 
{ 
  INT32 e; 
  struct mapping *m; 
 
  if(args & 1) 
    Pike_error("Uneven number of arguments to aggregate_mapping.\n"); 
 
  m=allocate_mapping(MAP_SLOTS(args / 2)); 
 
  for(e=-args;e<0;e+=2) { 
    STACK_LEVEL_START(-e); 
    low_mapping_insert(m, Pike_sp+e, Pike_sp+e+1, 2); 
    STACK_LEVEL_DONE(-e); 
  } 
  pop_n_elems(args); 
#ifdef PIKE_DEBUG 
  if(d_flag) 
    check_mapping(m); 
#endif 
  push_mapping(m); 
} 
 
PMOD_EXPORT struct mapping *copy_mapping_recursively(struct mapping *m, 
                                                     struct mapping *p) 
{ 
  int not_complex; 
  struct mapping *ret; 
  INT32 e; 
  struct keypair *k; 
  struct mapping_data  *md; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
#ifdef PIKE_DEBUG 
  if(d_flag > 1) check_mapping_type_fields(m); 
#endif 
 
  if((m->data->val_types | m->data->ind_types) & BIT_COMPLEX) { 
    not_complex = 0; 
    ret=allocate_mapping(MAP_SLOTS(m->data->size)); 
  } 
  else { 
    not_complex = 1; 
    ret = copy_mapping(m); 
  } 
 
  if (p) { 
    struct svalue aa, bb; 
    SET_SVAL(aa, T_MAPPING, 0, mapping, m); 
    SET_SVAL(bb, T_MAPPING, 0, mapping, ret); 
    mapping_insert(p, &aa, &bb); 
  } 
 
  if (not_complex) 
    return ret; 
 
  ret->data->flags = m->data->flags; 
 
  check_stack(2); 
 
  md=m->data; 
  md->valrefs++; 
  add_ref(md); 
  NEW_MAPPING_LOOP(md) 
  { 
    /* Place holders. 
     * 
     * NOTE: copy_svalues_recursively_no_free() may store stuff in 
     *       the destination svalue, and then call stuff that uses 
     *       the stack (eg itself). 
     */ 
    push_int(0); 
    push_int(0); 
    copy_svalues_recursively_no_free(Pike_sp-2,&k->ind, 1, p); 
    copy_svalues_recursively_no_free(Pike_sp-1,&k->val, 1, p); 
     
    low_mapping_insert(ret, Pike_sp-2, Pike_sp-1, 2); 
    pop_n_elems(2); 
  } 
  md->valrefs--; 
  free_mapping_data(md); 
 
  return ret; 
} 
 
 
PMOD_EXPORT void mapping_search_no_free(struct svalue *to, 
                            struct mapping *m, 
                            const struct svalue *look_for, 
                            const struct svalue *key /* start */) 
{ 
  struct mapping_data *md, *omd; 
 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
  md=m->data; 
 
  if(md->size) 
  { 
    unsigned INT32 h2,h=0; 
    struct keypair *k=md->hash[0], **prev; 
 
    if(key) 
    { 
      h2=hash_svalue(key); 
       
      FIND(); 
       
      if(!k) 
      { 
        SET_SVAL(*to, T_INT, NUMBER_UNDEFINED, integer, 0); 
        return; 
      } 
      k=k->next; 
    } 
     
     
    md=m->data; 
    if(md->size) 
    { 
      md->valrefs++; 
      add_ref(md); 
       
      if(h < (unsigned INT32)md->hashsize) 
      { 
        while(1) 
        { 
          while(k) 
          { 
            if(is_eq(look_for, &k->val)) 
            { 
              assign_svalue_no_free(to,&k->ind); 
               
              md->valrefs--; 
              free_mapping_data(md); 
              return; 
            } 
            k=k->next; 
          } 
          h++; 
          if(h>= (unsigned INT32)md->hashsize) 
            break; 
          k=md->hash[h]; 
        } 
      } 
    } 
     
    md->valrefs--; 
    free_mapping_data(md); 
  } 
 
  SET_SVAL(*to, T_INT, NUMBER_UNDEFINED, integer, 0); 
} 
 
#ifdef PIKE_DEBUG 
 
void check_mapping(const struct mapping *m) 
{ 
  int e,num; 
  struct keypair *k; 
  const struct mapping_data *md; 
 
  static int in_check_mapping; 
  if(in_check_mapping) return; 
  in_check_mapping=1; 
 
  md=m->data; 
 
  if(m->refs <=0) 
    Pike_fatal("Mapping has zero refs.\n"); 
 
  if(!m->data) 
    Pike_fatal("Mapping has no data block.\n"); 
 
  if (!m->data->refs) 
    Pike_fatal("Mapping data block has zero refs.\n"); 
 
  if(m->next && m->next->prev != m) 
    Pike_fatal("Mapping ->next->prev != mapping.\n"); 
 
#ifdef MAPPING_SIZE_DEBUG 
  if(m->debug_size != md->size) 
  { 
    if(Pike_in_gc) 
    { 
      fprintf(stderr,"Pike was in GC stage %d when this fatal occurred:\n",Pike_in_gc); 
      Pike_in_gc=0; 
    } 
     
    fprintf(stderr,"--MAPPING ZAPPING (%d!=%d), mapping:\n",m->debug_size,md->size); 
    describe(m); 
    fprintf(stderr,"--MAPPING ZAPPING (%d!=%d), mapping data:\n",m->debug_size,md->size); 
    describe(md); 
    Pike_fatal("Mapping zapping detected (%d != %d)!\n",m->debug_size,md->size); 
  } 
#endif 
 
  if(m->prev) 
  { 
    if(m->prev->next != m) 
      Pike_fatal("Mapping ->prev->next != mapping.\n"); 
  }else{ 
    if(first_mapping != m) 
      Pike_fatal("Mapping ->prev == 0 but first_mapping != mapping.\n"); 
  } 
 
  if(md->valrefs <0) 
    Pike_fatal("md->valrefs  < 0\n"); 
 
  if(md->hardlinks <0) 
    Pike_fatal("md->valrefs  < 0\n"); 
 
  if(md->refs < md->valrefs+1) 
    Pike_fatal("md->refs < md->valrefs+1\n"); 
 
  if(md->valrefs < md->hardlinks) 
    Pike_fatal("md->refs < md->valrefs+1\n"); 
 
  if(md->hashsize < 0) 
    Pike_fatal("Assert: I don't think he's going to make it Jim.\n"); 
 
  if(md->size < 0) 
    Pike_fatal("Core breach, evacuate ship!\n"); 
 
  if(md->num_keypairs < 0) 
    Pike_fatal("Starboard necell on fire!\n"); 
 
  if(md->size > md->num_keypairs) 
    Pike_fatal("Pretty mean hashtable there buster!\n"); 
 
  if(md->hashsize & (md->hashsize - 1)) 
    Pike_fatal("Invalid hashtable size: 0x%08lx\n", (long)md->hashsize); 
 
  if(md->hashsize > md->num_keypairs) 
    Pike_fatal("Pretty mean hashtable there buster %d > %d (2)!\n",md->hashsize,md->num_keypairs); 
 
  if(md->num_keypairs > (md->hashsize + 3) * AVG_LINK_LENGTH) 
    Pike_fatal("Mapping from hell detected, attempting to send it back...\n"); 
   
  if(md->size > 0 && (!md->ind_types || !md->val_types)) 
    Pike_fatal("Mapping type fields are... wrong.\n"); 
 
  num=0; 
  NEW_MAPPING_LOOP(md) 
    { 
      num++; 
 
      if (TYPEOF(k->ind) > MAX_TYPE) 
        Pike_fatal("Invalid maping keypair index type: %s\n", 
                   get_name_of_type(TYPEOF(k->ind))); 
      if(! ( (1 << TYPEOF(k->ind)) & (md->ind_types) )) 
        Pike_fatal("Mapping indices type field lies.\n"); 
 
      if (TYPEOF(k->val) > MAX_TYPE) 
        Pike_fatal("Invalid mapping keypair value type: %s\n", 
                   get_name_of_type(TYPEOF(k->val))); 
      if(! ( (1 << TYPEOF(k->val)) & (md->val_types) )) 
        Pike_fatal("Mapping values type field lies.\n"); 
 
      check_svalue(& k->ind); 
      check_svalue(& k->val); 
 
      /* FIXME add check for k->hval 
       * beware that hash_svalue may be threaded and locking 
       * is required!! 
       */ 
    } 
   
  if(md->size != num) 
    Pike_fatal("Shields are failing, hull integrity down to 20%%\n"); 
 
  in_check_mapping=0; 
} 
 
void check_all_mappings(void) 
{ 
  struct mapping *m; 
  for(m=first_mapping;m;m=m->next) 
    check_mapping(m); 
} 
#endif 
 
static void visit_mapping_data (struct mapping_data *md, int action, 
                                void *extra) 
{ 
  visit_enter(md, T_MAPPING_DATA, extra); 
  switch (action) { 
#ifdef PIKE_DEBUG 
    default: 
      Pike_fatal ("Unknown visit action %d.\n", action); 
    case VISIT_NORMAL: 
    case VISIT_COMPLEX_ONLY: 
      break; 
#endif 
    case VISIT_COUNT_BYTES: 
      mc_counted_bytes += MAPPING_DATA_SIZE (md->hashsize, md->num_keypairs); 
      break; 
  } 
 
  if ((md->ind_types | md->val_types) & 
      (action & VISIT_COMPLEX_ONLY ? BIT_COMPLEX : BIT_REF_TYPES)) { 
    int ind_ref_type = 
      md->flags & MAPPING_WEAK_INDICES ? REF_TYPE_WEAK : REF_TYPE_NORMAL; 
    int val_ref_type = 
      md->flags & MAPPING_WEAK_VALUES ? REF_TYPE_WEAK : REF_TYPE_NORMAL; 
    INT32 e; 
    struct keypair *k; 
    NEW_MAPPING_LOOP (md) { 
      visit_svalue (&k->ind, ind_ref_type, extra); 
      visit_svalue (&k->val, val_ref_type, extra); 
    } 
  } 
  visit_leave(md, T_MAPPING_DATA, extra); 
} 
 
PMOD_EXPORT void visit_mapping (struct mapping *m, int action, void *extra) 
{ 
  visit_enter(m, T_MAPPING, extra); 
  switch (action) { 
#ifdef PIKE_DEBUG 
    default: 
      Pike_fatal ("Unknown visit action %d.\n", action); 
    case VISIT_NORMAL: 
    case VISIT_COMPLEX_ONLY: 
      break; 
#endif 
    case VISIT_COUNT_BYTES: 
      mc_counted_bytes += sizeof (struct mapping); 
      break; 
  } 
 
  visit_ref (m->data, REF_TYPE_INTERNAL, 
             (visit_thing_fn *) &visit_mapping_data, extra); 
  visit_leave(m, T_MAPPING, extra); 
} 
 
#ifdef MAPPING_SIZE_DEBUG 
#define DO_IF_MAPPING_SIZE_DEBUG(x) x 
#else 
#define DO_IF_MAPPING_SIZE_DEBUG(x) 
#endif 
 
#define GC_RECURSE_MD_IN_USE(MD, RECURSE_FN, IND_TYPES, VAL_TYPES) do { \ 
  INT32 e;                                                              \ 
  struct keypair *k;                                                    \ 
  IND_TYPES = MD->ind_types;                                            \ 
  NEW_MAPPING_LOOP(MD) {                                                \ 
    if (!IS_DESTRUCTED(&k->ind) && RECURSE_FN(&k->ind, 1)) {            \ 
      DO_IF_DEBUG(Pike_fatal("Didn't expect an svalue zapping now.\n")); \ 
    }                                                                   \ 
    RECURSE_FN(&k->val, 1);                                             \ 
    VAL_TYPES |= 1 << TYPEOF(k->val);                                   \ 
  }                                                                     \ 
} while (0) 
 
#ifdef PIKE_MAPPING_KEYPAIR_LOOP 
#error Broken code below! 
#define GC_RECURSE(M, MD, REC_KEYPAIR, TYPE, IND_TYPES, VAL_TYPES) do {       \ 
    int remove;                                                         \ 
    struct keypair *k,**prev_;                                          \ 
    /* no locking required (no is_eq) */                                \ 
    for(k = MD_KEYPAIRS(md, md->hashsize);k < MD->free_list;)           \ 
    {                                                                   \ 
      REC_KEYPAIR(remove,                                               \ 
                  PIKE_CONCAT(TYPE, _svalues),                          \ 
                  PIKE_CONCAT(TYPE, _weak_svalues),                     \ 
                  PIKE_CONCAT(TYPE, _without_recurse),                  \ 
                  PIKE_CONCAT(TYPE, _weak_without_recurse));            \ 
      if (remove) {                                                     \ 
        /* Find and unlink k. */                                        \ 
        unsigned INT32 h_;                                              \ 
        h_ = k->hval & ( md->hashsize - 1);                             \ 
        prev_ = md->hash + h_;                                          \ 
        DO_IF_DEBUG(                                                    \ 
                    if (!*prev_) {                                      \ 
                      Pike_fatal("Node to unlink not found!\n");        \ 
                    }                                                   \ 
                    );                                                  \ 
        while (*prev_ != k) {                                           \ 
          prev_ = &((*prev_)->next);                                    \ 
          DO_IF_DEBUG(                                                  \ 
                      if (!*prev_) {                                    \ 
                        Pike_fatal("Node to unlink not found!\n");      \ 
                      }                                                 \ 
                      );                                                \ 
        }                                                               \ 
        (*prev_)->next = k->next;                                       \ 
        FREE_KEYPAIR(MD, k);                                            \ 
        mark_free_svalue (&MD->free_list->ind);                         \ 
        mark_free_svalue (&MD->free_list->val);                         \ 
        MD->size--;                                                     \ 
        DO_IF_MAPPING_SIZE_DEBUG(                                       \ 
          if(M->data ==MD)                                              \ 
            M->debug_size--;                                            \ 
        );                                                              \ 
      } else {                                                          \ 
        VAL_TYPES |= 1 << TYPEOF(k->val);                               \ 
        IND_TYPES |= 1 << TYPEOF(k->ind);                               \ 
        k++;                                                            \ 
      }                                                                 \ 
    }                                                                   \ 
  } while (0) 
#else /* !PIKE_MAPPING_KEYPAIR_LOOP */ 
#define GC_RECURSE(M, MD, REC_KEYPAIR, TYPE, IND_TYPES, VAL_TYPES) do {       \ 
  INT32 e;                                                              \ 
  int remove;                                                           \ 
  struct keypair *k,**prev;                                             \ 
  /* no locking required (no is_eq) */                                  \ 
  for(e=0;e<MD->hashsize;e++)                                           \ 
  {                                                                     \ 
    for(prev= MD->hash + e;(k=*prev);)                                  \ 
    {                                                                   \ 
      REC_KEYPAIR(remove,                                               \ 
                  PIKE_CONCAT(TYPE, _svalues),                          \ 
                  PIKE_CONCAT(TYPE, _weak_svalues),                     \ 
                  PIKE_CONCAT(TYPE, _without_recurse),                  \ 
                  PIKE_CONCAT(TYPE, _weak_without_recurse));            \ 
      if (remove)                                                       \ 
      {                                                                 \ 
        *prev=k->next;                                                  \ 
        FREE_KEYPAIR(MD, k);                                            \ 
        mark_free_svalue (&MD->free_list->ind);                         \ 
        mark_free_svalue (&MD->free_list->val);                         \ 
        MD->size--;                                                     \ 
        DO_IF_MAPPING_SIZE_DEBUG(                                       \ 
          if(M->data ==MD)                                              \ 
            M->debug_size--;                                            \ 
        );                                                              \ 
      }else{                                                            \ 
        VAL_TYPES |= 1 << TYPEOF(k->val);                               \ 
        IND_TYPES |= 1 << TYPEOF(k->ind);                               \ 
        prev=&k->next;                                                  \ 
      }                                                                 \ 
    }                                                                   \ 
  }                                                                     \ 
} while (0) 
#endif /* PIKE_MAPPING_KEYPAIR_LOOP */ 
 
#define GC_REC_KP(REMOVE, N_REC, W_REC, N_TST, W_TST) do {            \ 
  if ((REMOVE = N_REC(&k->ind, 1)))                                     \ 
    gc_free_svalue(&k->val);                                            \ 
  else                                                                  \ 
    N_REC(&k->val, 1);                                                  \ 
} while (0) 
 
#define GC_REC_KP_IND(REMOVE, N_REC, W_REC, N_TST, W_TST) do {                \ 
  if ((REMOVE = W_REC(&k->ind, 1)))                                     \ 
    gc_free_svalue(&k->val);                                            \ 
  else                                                                  \ 
    N_REC(&k->val, 1);                                                  \ 
} while (0) 
 
#define GC_REC_KP_VAL(REMOVE, N_REC, W_REC, N_TST, W_TST) do {                \ 
  if ((REMOVE = N_TST(&k->ind))) /* Don't recurse now. */               \ 
    gc_free_svalue(&k->val);                                            \ 
  else if ((REMOVE = W_REC(&k->val, 1)))                                \ 
    gc_free_svalue(&k->ind);                                            \ 
  else                                                                  \ 
    N_REC(&k->ind, 1);          /* Now we can recurse the index. */     \ 
} while (0) 
 
#define GC_REC_KP_BOTH(REMOVE, N_REC, W_REC, N_TST, W_TST) do {               \ 
  if ((REMOVE = W_TST(&k->ind))) /* Don't recurse now. */               \ 
    gc_free_svalue(&k->val);                                            \ 
  else if ((REMOVE = W_REC(&k->val, 1)))                                \ 
    gc_free_svalue(&k->ind);                                            \ 
  else                                                                  \ 
    W_REC(&k->ind, 1);          /* Now we can recurse the index. */     \ 
} while (0) 
 
void gc_mark_mapping_as_referenced(struct mapping *m) 
{ 
#ifdef PIKE_DEBUG 
  if(m->data->refs <=0) 
    Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
  debug_malloc_touch(m); 
  debug_malloc_touch(m->data); 
 
  if(gc_mark(m, T_MAPPING)) 
    GC_ENTER (m, T_MAPPING) { 
      struct mapping_data *md = m->data; 
 
      if (m == gc_mark_mapping_pos) 
        gc_mark_mapping_pos = m->next; 
      if (m == gc_internal_mapping) 
        gc_internal_mapping = m->next; 
      else { 
        DOUBLEUNLINK(first_mapping, m); 
        DOUBLELINK(first_mapping, m); /* Linked in first. */ 
      } 
 
      if(gc_mark(md, T_MAPPING_DATA) && 
         ((md->ind_types | md->val_types) & BIT_COMPLEX)) { 
        TYPE_FIELD ind_types = 0, val_types = 0; 
        if (MAPPING_DATA_IN_USE(md)) { 
          /* Must leave the mapping data untouched if it's busy. */ 
          debug_malloc_touch(m); 
          debug_malloc_touch(md); 
          GC_RECURSE_MD_IN_USE(md, gc_mark_svalues, ind_types, val_types); 
          gc_assert_checked_as_nonweak(md); 
        } 
        else 
          switch (md->flags & MAPPING_WEAK) { 
            case 0: 
              debug_malloc_touch(m); 
              debug_malloc_touch(md); 
              GC_RECURSE(m, md, GC_REC_KP, gc_mark, ind_types, val_types); 
              gc_assert_checked_as_nonweak(md); 
              break; 
            case MAPPING_WEAK_INDICES: 
              debug_malloc_touch(m); 
              debug_malloc_touch(md); 
              GC_RECURSE(m, md, GC_REC_KP_IND, gc_mark, ind_types, val_types); 
              gc_assert_checked_as_weak(md); 
              break; 
            case MAPPING_WEAK_VALUES: 
              debug_malloc_touch(m); 
              debug_malloc_touch(md); 
              GC_RECURSE(m, md, GC_REC_KP_VAL, gc_mark, ind_types, val_types); 
              gc_assert_checked_as_weak(md); 
              break; 
            default: 
              debug_malloc_touch(m); 
              debug_malloc_touch(md); 
              GC_RECURSE(m, md, GC_REC_KP_BOTH, gc_mark, ind_types, val_types); 
              gc_assert_checked_as_weak(md); 
              break; 
          } 
        md->val_types = val_types; 
        md->ind_types = ind_types; 
      } 
    } GC_LEAVE; 
} 
 
void real_gc_cycle_check_mapping(struct mapping *m, int weak) 
{ 
  GC_CYCLE_ENTER(m, T_MAPPING, weak) { 
    struct mapping_data *md = m->data; 
    debug_malloc_touch(m); 
    debug_malloc_touch(md); 
 
#ifdef PIKE_DEBUG 
    if(md->refs <=0) 
      Pike_fatal("Zero refs in mapping->data\n"); 
#endif 
 
    if ((md->ind_types | md->val_types) & BIT_COMPLEX) { 
      TYPE_FIELD ind_types = 0, val_types = 0; 
      if (MAPPING_DATA_IN_USE(md)) { 
        /* Must leave the mapping data untouched if it's busy. */ 
        debug_malloc_touch(m); 
        debug_malloc_touch(md); 
        GC_RECURSE_MD_IN_USE(md, gc_cycle_check_svalues, ind_types, val_types); 
        gc_assert_checked_as_nonweak(md); 
      } 
      else 
        switch (md->flags & MAPPING_WEAK) { 
          case 0: 
            debug_malloc_touch(m); 
            debug_malloc_touch(md); 
            GC_RECURSE(m, md, GC_REC_KP, gc_cycle_check, ind_types, val_types); 
            gc_assert_checked_as_nonweak(md); 
            break; 
          case MAPPING_WEAK_INDICES: 
            debug_malloc_touch(m); 
            debug_malloc_touch(md); 
            GC_RECURSE(m, md, GC_REC_KP_IND, gc_cycle_check, ind_types, val_types); 
            gc_assert_checked_as_weak(md); 
            break; 
          case MAPPING_WEAK_VALUES: 
            debug_malloc_touch(m); 
            debug_malloc_touch(md); 
            GC_RECURSE(m, md, GC_REC_KP_VAL, gc_cycle_check, ind_types, val_types); 
            gc_assert_checked_as_weak(md); 
            break; 
          default: 
            debug_malloc_touch(m); 
            debug_malloc_touch(md); 
            GC_RECURSE(m, md, GC_REC_KP_BOTH, gc_cycle_check, ind_types, val_types); 
            gc_assert_checked_as_weak(md); 
            break; 
        } 
      md->val_types = val_types; 
      md->ind_types = ind_types; 
    } 
  } GC_CYCLE_LEAVE; 
} 
 
static void gc_check_mapping(struct mapping *m) 
{ 
  struct mapping_data *md = m->data; 
 
  if((md->ind_types | md->val_types) & BIT_COMPLEX) 
    GC_ENTER (m, T_MAPPING) { 
      INT32 e; 
      struct keypair *k; 
 
      if(!debug_gc_check (md, " as mapping data block of a mapping")) { 
        if (!(md->flags & MAPPING_WEAK) || MAPPING_DATA_IN_USE(md)) 
          /* Disregard the weak flag if the mapping data is busy; we must 
           * leave it untouched in that case anyway. */ 
          NEW_MAPPING_LOOP(md) 
          { 
            debug_gc_check_svalues(&k->ind, 1, " as mapping index"); 
            debug_gc_check_svalues(&k->val, 1, " as mapping value"); 
          } 
        else { 
          switch (md->flags & MAPPING_WEAK) { 
            case MAPPING_WEAK_INDICES: 
              NEW_MAPPING_LOOP(md) 
              { 
                debug_gc_check_weak_svalues(&k->ind, 1, " as mapping index"); 
                debug_gc_check_svalues(&k->val, 1, " as mapping value"); 
              } 
              break; 
            case MAPPING_WEAK_VALUES: 
              NEW_MAPPING_LOOP(md) 
              { 
                debug_gc_check_svalues(&k->ind, 1, " as mapping index"); 
                debug_gc_check_weak_svalues(&k->val, 1, " as mapping value"); 
              } 
              break; 
            default: 
              NEW_MAPPING_LOOP(md) 
              { 
                debug_gc_check_weak_svalues(&k->ind, 1, " as mapping index"); 
                debug_gc_check_weak_svalues(&k->val, 1, " as mapping value"); 
              } 
              break; 
          } 
          gc_checked_as_weak(md); 
        } 
      } 
    } GC_LEAVE; 
} 
 
unsigned gc_touch_all_mappings(void) 
{ 
  unsigned n = 0; 
  struct mapping *m; 
  if (first_mapping && first_mapping->prev) 
    Pike_fatal("Error in mapping link list.\n"); 
  for (m = first_mapping; m; m = m->next) { 
    debug_gc_touch(m); 
    n++; 
    if (m->next && m->next->prev != m) 
      Pike_fatal("Error in mapping link list.\n"); 
  } 
  return n; 
} 
 
void gc_check_all_mappings(void) 
{ 
  struct mapping *m; 
 
  for(m=first_mapping;m;m=m->next) 
  { 
#ifdef DEBUG_MALLOC 
    if (((int) PTR_TO_INT (m->data)) == 0x55555555) { 
      fprintf(stderr, "** Zapped mapping in list of active mappings!\n"); 
      describe_something(m, T_MAPPING, 0,2,0, NULL); 
      Pike_fatal("Zapped mapping in list of active mappings!\n"); 
    } 
#endif /* DEBUG_MALLOC */ 
 
    gc_check_mapping(m); 
#ifdef PIKE_DEBUG 
    if(d_flag > 1) check_mapping_type_fields(m); 
#endif 
  } 
} 
 
void gc_mark_all_mappings(void) 
{ 
  gc_mark_mapping_pos = gc_internal_mapping; 
  while (gc_mark_mapping_pos) { 
    struct mapping *m = gc_mark_mapping_pos; 
    gc_mark_mapping_pos = m->next; 
 
    debug_malloc_touch(m); 
    debug_malloc_touch(m->data); 
 
    if(gc_is_referenced(m)) 
      gc_mark_mapping_as_referenced(m); 
  } 
} 
 
void gc_cycle_check_all_mappings(void) 
{ 
  struct mapping *m; 
  for (m = gc_internal_mapping; m; m = m->next) { 
    real_gc_cycle_check_mapping(m, 0); 
    gc_cycle_run_queue(); 
  } 
} 
 
void gc_zap_ext_weak_refs_in_mappings(void) 
{ 
  gc_mark_mapping_pos = first_mapping; 
  while (gc_mark_mapping_pos != gc_internal_mapping && gc_ext_weak_refs) { 
    struct mapping *m = gc_mark_mapping_pos; 
    gc_mark_mapping_pos = m->next; 
    gc_mark_mapping_as_referenced(m); 
  } 
  gc_mark_discard_queue(); 
} 
 
size_t gc_free_all_unreferenced_mappings(void) 
{ 
  struct mapping *m,*next; 
  struct mapping_data *md; 
  size_t unreferenced = 0; 
 
  for(m=gc_internal_mapping;m;m=next) 
  { 
    debug_malloc_touch(m); 
    debug_malloc_touch(m->data); 
 
    if(gc_do_free(m)) 
    { 
      /* Got an extra ref from gc_cycle_pop(). */ 
      md = m->data; 
 
      debug_malloc_touch(m); 
      debug_malloc_touch(md); 
 
      /* Protect against unlink_mapping_data() recursing too far. */ 
      m->data=&empty_data; 
      add_ref(m->data); 
 
      unlink_mapping_data(md); 
#ifdef MAPPING_SIZE_DEBUG 
      m->debug_size=0; 
#endif 
      gc_free_extra_ref(m); 
      SET_NEXT_AND_FREE(m, free_mapping); 
    } 
    else 
    { 
      next=m->next; 
    } 
    unreferenced++; 
  } 
 
  return unreferenced; 
} 
 
#ifdef PIKE_DEBUG 
 
void simple_describe_mapping(struct mapping *m) 
{ 
  dynamic_buffer save_buf; 
  char *s; 
  init_buf(&save_buf); 
  describe_mapping(m,0,2); 
  s=simple_free_buf(&save_buf); 
  fprintf(stderr,"%s\n",s); 
  free(s); 
} 
 
 
void debug_dump_mapping(struct mapping *m) 
{ 
  fprintf(stderr, "Refs=%d, next=%p, prev=%p", 
          m->refs, m->next, m->prev); 
  if (((ptrdiff_t)m->data) & 3) { 
    fprintf(stderr, ", data=%p (unaligned)\n", m->data); 
  } else { 
    fprintf(stderr, ", flags=0x%x, size=%d, hashsize=%d\n", 
            m->data->flags, m->data->size, m->data->hashsize); 
    fprintf(stderr, "Indices type field ="); 
    debug_dump_type_field(m->data->ind_types); 
    fprintf(stderr, "\n"); 
    fprintf(stderr, "Values type field ="); 
    debug_dump_type_field(m->data->val_types); 
    fprintf(stderr, "\n"); 
    simple_describe_mapping(m); 
  } 
} 
#endif 
 
int mapping_is_constant(struct mapping *m, 
                        struct processing *p) 
{ 
  int ret=1; 
  INT32 e; 
  struct keypair *k; 
  struct mapping_data *md=m->data; 
 
  if( (md->ind_types | md->val_types) & ~(BIT_INT|BIT_FLOAT|BIT_STRING)) 
  { 
    md->valrefs++; 
    add_ref(md); 
    NEW_MAPPING_LOOP(md) 
      { 
        if(!svalues_are_constant(&k->ind, 1, md->ind_types, p) || 
           !svalues_are_constant(&k->val, 1, md->val_types, p)) 
        { 
          ret=0; 
          e=md->hashsize; 
          break; 
        } 
      } 
    md->valrefs--; 
    free_mapping_data(md); 
  } 
  return ret; 
}