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
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2017. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stddef.h> /* offsetof() */
#include "sys.h"
#include "erl_vm.h"
#include "erl_sys_driver.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#define ERL_WANT_HIPE_BIF_WRAPPER__
#include "bif.h"
#undef ERL_WANT_HIPE_BIF_WRAPPER__
#include "big.h"
#include "dist.h"
#include "erl_version.h"
#include "erl_binary.h"
#include "beam_bp.h"
#include "erl_db_util.h"
#include "register.h"
#include "erl_thr_progress.h"
#define ERTS_PTAB_WANT_BIF_IMPL__
#include "erl_ptab.h"
#include "erl_bits.h"
#include "erl_bif_unique.h"
#include "erl_map.h"
#include "erl_msacc.h"
Export *erts_await_result;
static Export* flush_monitor_messages_trap = NULL;
static Export* set_cpu_topology_trap = NULL;
static Export* await_proc_exit_trap = NULL;
static Export* await_port_send_result_trap = NULL;
Export* erts_format_cpu_topology_trap = NULL;
static Export dsend_continue_trap_export;
Export *erts_convert_time_unit_trap = NULL;
static Export *await_msacc_mod_trap = NULL;
static erts_smp_atomic32_t msacc;
static Export *await_sched_wall_time_mod_trap;
static erts_smp_atomic32_t sched_wall_time;
#define DECL_AM(S) Eterm AM_ ## S = am_atom_put(#S, sizeof(#S) - 1)
/*
* The BIF's now follow, see the Erlang Manual for a description of what
* each individual BIF does.
*/
BIF_RETTYPE spawn_3(BIF_ALIST_3)
{
ErlSpawnOpts so;
Eterm pid;
so.flags = erts_default_spo_flags;
pid = erl_create_process(BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3, &so);
if (is_non_value(pid)) {
BIF_ERROR(BIF_P, so.error_code);
} else {
if (ERTS_USE_MODIFIED_TIMING()) {
BIF_TRAP2(erts_delay_trap, BIF_P, pid, ERTS_MODIFIED_TIMING_DELAY);
}
BIF_RET(pid);
}
}
/**********************************************************************/
/* Utility to add a new link between processes p and another internal
* process (rpid). Process p must be the currently executing process.
*/
static int insert_internal_link(Process* p, Eterm rpid)
{
Process *rp;
ErtsProcLocks rp_locks = ERTS_PROC_LOCK_LINK;
ASSERT(is_internal_pid(rpid));
if (IS_TRACED(p)
&& (ERTS_TRACE_FLAGS(p) & (F_TRACE_SOL|F_TRACE_SOL1))) {
rp_locks = ERTS_PROC_LOCKS_ALL;
}
erts_smp_proc_lock(p, ERTS_PROC_LOCK_LINK);
/* get a pointer to the process struct of the linked process */
rp = erts_pid2proc_opt(p, ERTS_PROC_LOCK_MAIN|ERTS_PROC_LOCK_LINK,
rpid, rp_locks,
ERTS_P2P_FLG_ALLOW_OTHER_X);
if (!rp) {
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_LINK);
return 0;
}
if (p != rp) {
erts_add_link(&ERTS_P_LINKS(p), LINK_PID, rp->common.id);
erts_add_link(&ERTS_P_LINKS(rp), LINK_PID, p->common.id);
ASSERT(IS_TRACER_VALID(ERTS_TRACER(p)));
if (IS_TRACED(p)) {
if (ERTS_TRACE_FLAGS(p) & (F_TRACE_SOL|F_TRACE_SOL1)) {
ERTS_TRACE_FLAGS(rp) |= (ERTS_TRACE_FLAGS(p) & TRACEE_FLAGS);
erts_tracer_replace(&rp->common, ERTS_TRACER(p));
if (ERTS_TRACE_FLAGS(p) & F_TRACE_SOL1) { /* maybe override */
ERTS_TRACE_FLAGS(rp) &= ~(F_TRACE_SOL1 | F_TRACE_SOL);
ERTS_TRACE_FLAGS(p) &= ~(F_TRACE_SOL1 | F_TRACE_SOL);
}
}
}
}
if (IS_TRACED_FL(rp, F_TRACE_PROCS))
trace_proc(p, p == rp ? rp_locks : ERTS_PROC_LOCK_MAIN|ERTS_PROC_LOCK_LINK,
rp, am_getting_linked, p->common.id);
if (p == rp)
erts_smp_proc_unlock(p, rp_locks & ~ERTS_PROC_LOCK_MAIN);
else {
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_LINK);
erts_smp_proc_unlock(rp, rp_locks);
}
return 1;
}
/* create a link to the process */
BIF_RETTYPE link_1(BIF_ALIST_1)
{
DistEntry *dep;
if (IS_TRACED_FL(BIF_P, F_TRACE_PROCS)) {
trace_proc(BIF_P, ERTS_PROC_LOCK_MAIN, BIF_P, am_link, BIF_ARG_1);
}
/* check that the pid or port which is our argument is OK */
if (is_internal_pid(BIF_ARG_1)) {
if (insert_internal_link(BIF_P, BIF_ARG_1)) {
BIF_RET(am_true);
}
else {
goto res_no_proc;
}
}
if (is_internal_port(BIF_ARG_1)) {
int send_link_signal = 0;
Port *prt = erts_port_lookup(BIF_ARG_1,
(erts_port_synchronous_ops
? ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP
: ERTS_PORT_SFLGS_INVALID_LOOKUP));
if (!prt) {
goto res_no_proc;
}
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_LINK);
if (erts_add_link(&ERTS_P_LINKS(BIF_P), LINK_PID, BIF_ARG_1) >= 0)
send_link_signal = 1;
/* else: already linked */
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK);
if (send_link_signal) {
Eterm ref;
Eterm *refp = erts_port_synchronous_ops ? &ref : NULL;
switch (erts_port_link(BIF_P, prt, BIF_P->common.id, refp)) {
case ERTS_PORT_OP_DROPPED:
case ERTS_PORT_OP_BADARG:
goto res_no_proc;
case ERTS_PORT_OP_SCHEDULED:
if (refp) {
ASSERT(is_internal_ordinary_ref(ref));
BIF_TRAP3(await_port_send_result_trap, BIF_P, ref, am_true, am_true);
}
default:
break;
}
}
BIF_RET(am_true);
}
else if (is_external_port(BIF_ARG_1)
&& external_port_dist_entry(BIF_ARG_1) == erts_this_dist_entry) {
goto res_no_proc;
}
if (is_external_pid(BIF_ARG_1)) {
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_LINK);
/* We may earn time by checking first that we're not linked already */
if (erts_lookup_link(ERTS_P_LINKS(BIF_P), BIF_ARG_1) != NULL) {
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK);
BIF_RET(am_true);
}
else {
ErtsLink *lnk;
int code;
ErtsDSigData dsd;
dep = external_pid_dist_entry(BIF_ARG_1);
if (dep == erts_this_dist_entry) {
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK);
goto res_no_proc;
}
code = erts_dsig_prepare(&dsd, dep, BIF_P, ERTS_DSP_RLOCK, 0);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
/* Let the dlink trap handle it */
case ERTS_DSIG_PREP_NOT_CONNECTED:
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK);
BIF_TRAP1(dlink_trap, BIF_P, BIF_ARG_1);
case ERTS_DSIG_PREP_CONNECTED:
/* We are connected. Setup link and send link signal */
erts_smp_de_links_lock(dep);
erts_add_link(&ERTS_P_LINKS(BIF_P), LINK_PID, BIF_ARG_1);
lnk = erts_add_or_lookup_link(&(dep->nlinks),
LINK_PID,
BIF_P->common.id);
ASSERT(lnk != NULL);
erts_add_link(&ERTS_LINK_ROOT(lnk), LINK_PID, BIF_ARG_1);
erts_smp_de_links_unlock(dep);
erts_smp_de_runlock(dep);
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK);
code = erts_dsig_send_link(&dsd, BIF_P->common.id, BIF_ARG_1);
if (code == ERTS_DSIG_SEND_YIELD)
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
BIF_RET(am_true);
default:
ASSERT(! "Invalid dsig prepare result");
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
}
}
}
BIF_ERROR(BIF_P, BADARG);
res_no_proc: {
erts_aint32_t state = erts_smp_atomic32_read_nob(&BIF_P->state);
if (state & ERTS_PSFLG_TRAP_EXIT) {
ErtsProcLocks locks = ERTS_PROC_LOCK_MAIN;
erts_deliver_exit_message(BIF_ARG_1, BIF_P, &locks, am_noproc, NIL);
erts_smp_proc_unlock(BIF_P, ~ERTS_PROC_LOCK_MAIN & locks);
BIF_RET(am_true);
}
else
BIF_ERROR(BIF_P, EXC_NOPROC);
}
}
/* This function is allowed to return range of values handled by demonitor/1-2
* Namely: atoms true, false, yield, internal_error, badarg or THE_NON_VALUE
*/
static Eterm
remote_demonitor(Process *c_p, DistEntry *dep, Eterm ref, Eterm to)
{
ErtsDSigData dsd;
ErtsMonitor *dmon;
ErtsMonitor *mon;
int code;
Eterm res = am_false;
ERTS_SMP_LC_ASSERT((ERTS_PROC_LOCK_MAIN|ERTS_PROC_LOCK_LINK)
== erts_proc_lc_my_proc_locks(c_p));
code = erts_dsig_prepare(&dsd, dep, c_p, ERTS_DSP_RLOCK, 0);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
case ERTS_DSIG_PREP_NOT_CONNECTED:
/*
* In the smp case this is possible if the node goes
* down just before the call to demonitor.
*/
if (dep) {
erts_smp_de_links_lock(dep);
dmon = erts_remove_monitor(&dep->monitors, ref);
erts_smp_de_links_unlock(dep);
if (dmon)
erts_destroy_monitor(dmon);
}
mon = erts_remove_monitor(&ERTS_P_MONITORS(c_p), ref);
erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_LINK);
res = am_true;
break;
case ERTS_DSIG_PREP_CONNECTED:
erts_smp_de_links_lock(dep);
mon = erts_remove_monitor(&ERTS_P_MONITORS(c_p), ref);
dmon = erts_remove_monitor(&dep->monitors, ref);
erts_smp_de_links_unlock(dep);
erts_smp_de_runlock(dep);
erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_LINK);
if (!dmon) {
/*
* This is possible when smp support is enabled.
* 'DOWN' message just arrived.
*/
res = am_true;
}
else {
/*
* Soft (no force) send, use ->data in dist slot
* monitor list since in case of monitor name
* the atom is stored there. Yield if necessary.
*/
code = erts_dsig_send_demonitor(&dsd,
c_p->common.id,
(mon->name != NIL
? mon->name
: mon->u.pid),
ref,
0);
res = (code == ERTS_DSIG_SEND_YIELD ? am_yield : am_true);
erts_destroy_monitor(dmon);
}
break;
default:
ASSERT(! "Invalid dsig prepare result");
return am_internal_error;
}
/*
* We aren't allowed to destroy 'mon' until now, since 'to'
* may refer into 'mon' (external pid).
*/
ASSERT(mon); /* Since link lock wasn't released between
lookup and remove */
erts_destroy_monitor(mon);
ERTS_SMP_LC_ASSERT(ERTS_PROC_LOCK_MAIN == erts_proc_lc_my_proc_locks(c_p));
return res;
}
static ERTS_INLINE void
demonitor_local_process(Process *c_p, Eterm ref, Eterm to, Eterm *res)
{
Process *rp = erts_pid2proc_opt(c_p,
ERTS_PROC_LOCK_MAIN|ERTS_PROC_LOCK_LINK,
to,
ERTS_PROC_LOCK_LINK,
ERTS_P2P_FLG_ALLOW_OTHER_X);
ErtsMonitor *mon = erts_remove_monitor(&ERTS_P_MONITORS(c_p), ref);
if (!mon)
*res = am_false;
else
{
*res = am_true;
erts_destroy_monitor(mon);
}
if (rp) {
ErtsMonitor *rmon;
rmon = erts_remove_monitor(&ERTS_P_MONITORS(rp), ref);
if (rp != c_p)
erts_smp_proc_unlock(rp, ERTS_PROC_LOCK_LINK);
if (rmon != NULL)
erts_destroy_monitor(rmon);
}
else {
ERTS_SMP_ASSERT_IS_NOT_EXITING(c_p);
}
}
static ERTS_INLINE BIF_RETTYPE
demonitor_local_port(Process *origin, Eterm ref, Eterm target)
{
BIF_RETTYPE res = am_false;
Port *port = erts_port_lookup_raw(target);
if (!port) {
BIF_ERROR(origin, BADARG);
}
erts_smp_proc_unlock(origin, ERTS_PROC_LOCK_LINK);
if (port) {
Eterm trap_ref;
switch (erts_port_demonitor(origin, ERTS_PORT_DEMONITOR_NORMAL,
port, ref, &trap_ref)) {
case ERTS_PORT_OP_DROPPED:
case ERTS_PORT_OP_BADARG:
break;
case ERTS_PORT_OP_SCHEDULED:
BIF_TRAP3(await_port_send_result_trap, origin, trap_ref,
am_busy_port, am_true);
/* the busy_port atom will never be returned, because it cannot be
* returned from erts_port_(de)monitor, but just in case if in future
* internal API changes - you may see this atom */
default:
break;
}
}
else {
ERTS_SMP_ASSERT_IS_NOT_EXITING(origin);
}
BIF_RET(res);
}
/* Can return atom true, false, yield, internal_error, badarg or
* THE_NON_VALUE if error occurred or trap has been set up
*/
static
BIF_RETTYPE demonitor(Process *c_p, Eterm ref, Eterm *multip)
{
ErtsMonitor *mon = NULL; /* The monitor entry to delete */
Eterm to = NIL; /* Monitor link traget */
DistEntry *dep = NULL; /* Target's distribution entry */
int deref_de = 0;
BIF_RETTYPE res = am_false;
int unlock_link = 1;
erts_smp_proc_lock(c_p, ERTS_PROC_LOCK_LINK);
if (is_not_internal_ref(ref)) {
res = am_badarg;
goto done; /* Cannot be this monitor's ref */
}
mon = erts_lookup_monitor(ERTS_P_MONITORS(c_p), ref);
if (!mon) {
goto done;
}
switch (mon->type) {
case MON_TIME_OFFSET:
*multip = am_true;
erts_demonitor_time_offset(ref);
res = am_true;
break;
case MON_ORIGIN:
to = mon->u.pid;
*multip = am_false;
if (is_atom(to)) {
/* Monitoring a name at node to */
ASSERT(is_node_name_atom(to));
dep = erts_sysname_to_connected_dist_entry(to);
ASSERT(dep != erts_this_dist_entry);
if (dep)
deref_de = 1;
} else if (is_port(to)) {
if (port_dist_entry(to) != erts_this_dist_entry) {
goto badarg;
}
res = demonitor_local_port(c_p, ref, to);
unlock_link = 0;
goto done;
} else {
ASSERT(is_pid(to));
dep = pid_dist_entry(to);
}
if (dep != erts_this_dist_entry) {
res = remote_demonitor(c_p, dep, ref, to);
/* remote_demonitor() unlocks link lock on c_p */
unlock_link = 0;
}
else { /* Local monitor */
if (deref_de) {
deref_de = 0;
erts_deref_dist_entry(dep);
}
dep = NULL;
demonitor_local_process(c_p, ref, to, &res);
}
break;
default /* case */ :
badarg:
res = am_badarg; /* will be converted to error by caller */
*multip = am_false;
break;
}
done:
if (unlock_link)
erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_LINK);
if (deref_de) {
ASSERT(dep);
erts_deref_dist_entry(dep);
}
ERTS_SMP_LC_ASSERT(ERTS_PROC_LOCK_MAIN == erts_proc_lc_my_proc_locks(c_p));
BIF_RET(res);
}
BIF_RETTYPE demonitor_1(BIF_ALIST_1)
{
Eterm multi;
switch (demonitor(BIF_P, BIF_ARG_1, &multi)) {
case am_false:
case am_true: BIF_RET(am_true);
case THE_NON_VALUE: BIF_RET(THE_NON_VALUE);
case am_yield: ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
case am_badarg: BIF_ERROR(BIF_P, BADARG);
case am_internal_error:
default:
ASSERT(! "demonitor(): internal error");
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
}
}
BIF_RETTYPE demonitor_2(BIF_ALIST_2)
{
BIF_RETTYPE res = am_true;
Eterm multi = am_false;
int info = 0;
int flush = 0;
Eterm list = BIF_ARG_2;
while (is_list(list)) {
Eterm* consp = list_val(list);
switch (CAR(consp)) {
case am_flush:
flush = 1;
break;
case am_info:
info = 1;
break;
default:
goto badarg;
}
list = CDR(consp);
}
if (is_not_nil(list))
goto badarg;
switch (demonitor(BIF_P, BIF_ARG_1, &multi)) {
case THE_NON_VALUE:
/* If other error occurred or trap has been set up - pass through */
BIF_RET(THE_NON_VALUE);
case am_false:
if (info)
res = am_false;
if (flush) {
flush_messages:
BIF_TRAP3(flush_monitor_messages_trap, BIF_P,
BIF_ARG_1, multi, res);
}
case am_true:
if (multi == am_true && flush)
goto flush_messages;
BIF_RET(res);
case am_yield:
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
case am_badarg:
badarg:
BIF_ERROR(BIF_P, BADARG);
case am_internal_error:
default:
ASSERT(! "demonitor(): internal error");
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
}
}
/* Type must be atomic object! */
void
erts_queue_monitor_message(Process *p,
ErtsProcLocks *p_locksp,
Eterm ref,
Eterm type,
Eterm item,
Eterm reason)
{
Eterm tup;
Eterm* hp;
Eterm reason_copy, ref_copy, item_copy;
Uint reason_size, ref_size, item_size, heap_size;
ErlOffHeap *ohp;
ErtsMessage *msgp;
reason_size = IS_CONST(reason) ? 0 : size_object(reason);
item_size = IS_CONST(item) ? 0 : size_object(item);
ref_size = size_object(ref);
heap_size = 6+reason_size+ref_size+item_size;
msgp = erts_alloc_message_heap(p, p_locksp, heap_size,
&hp, &ohp);
reason_copy = (IS_CONST(reason)
? reason
: copy_struct(reason, reason_size, &hp, ohp));
item_copy = (IS_CONST(item)
? item
: copy_struct(item, item_size, &hp, ohp));
ref_copy = copy_struct(ref, ref_size, &hp, ohp);
tup = TUPLE5(hp, am_DOWN, ref_copy, type, item_copy, reason_copy);
erts_queue_message(p, *p_locksp, msgp, tup, am_system);
}
static Eterm
local_pid_monitor(Process *p, Eterm target, Eterm mon_ref, int boolean)
{
Eterm ret = mon_ref;
Process *rp;
ErtsProcLocks p_locks = ERTS_PROC_LOCK_MAIN|ERTS_PROC_LOCK_LINK;
if (target == p->common.id) {
return ret;
}
erts_smp_proc_lock(p, ERTS_PROC_LOCK_LINK);
rp = erts_pid2proc_opt(p, p_locks,
target, ERTS_PROC_LOCK_LINK,
ERTS_P2P_FLG_ALLOW_OTHER_X);
if (!rp) {
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_LINK);
p_locks &= ~ERTS_PROC_LOCK_LINK;
if (boolean)
ret = am_false;
else
erts_queue_monitor_message(p, &p_locks,
mon_ref, am_process, target, am_noproc);
}
else {
ASSERT(rp != p);
if (boolean)
ret = am_true;
erts_add_monitor(&ERTS_P_MONITORS(p), MON_ORIGIN, mon_ref, target, NIL);
erts_add_monitor(&ERTS_P_MONITORS(rp), MON_TARGET, mon_ref, p->common.id, NIL);
erts_smp_proc_unlock(rp, ERTS_PROC_LOCK_LINK);
}
erts_smp_proc_unlock(p, p_locks & ~ERTS_PROC_LOCK_MAIN);
return ret;
}
static BIF_RETTYPE
local_port_monitor(Process *origin, Eterm target)
{
BIF_RETTYPE ref = erts_make_ref(origin);
Port *port = erts_sig_lookup_port(origin, target);
ErtsProcLocks p_locks = ERTS_PROC_LOCK_MAIN;
if (!port) {
res_no_proc:
/* Send the DOWN message immediately. Ref is made on the fly because
* caller has never seen it yet. */
erts_queue_monitor_message(origin, &p_locks, ref,
am_port, target, am_noproc);
}
else {
switch (erts_port_monitor(origin, port, target, &ref)) {
case ERTS_PORT_OP_DROPPED:
case ERTS_PORT_OP_BADARG:
goto res_no_proc;
case ERTS_PORT_OP_SCHEDULED:
BIF_TRAP3(await_port_send_result_trap, origin, ref,
am_busy_port, ref);
/* the busy_port atom will never be returned, because it cannot be
* returned from erts_port_monitor, but just in case if in future
* internal API changes - you may see this atom */
default:
break;
}
}
erts_smp_proc_unlock(origin, p_locks & ~ERTS_PROC_LOCK_MAIN);
BIF_RET(ref);
}
/* Type = process | port :: atom(), 1st argument passed to erlang:monitor/2
*/
static BIF_RETTYPE
local_name_monitor(Process *self, Eterm type, Eterm target_name)
{
BIF_RETTYPE ret = erts_make_ref(self);
ErtsProcLocks p_locks = ERTS_PROC_LOCK_MAIN | ERTS_PROC_LOCK_LINK;
Process *proc = NULL;
Port *port = NULL;
erts_smp_proc_lock(self, ERTS_PROC_LOCK_LINK);
erts_whereis_name(self, p_locks, target_name,
&proc, ERTS_PROC_LOCK_LINK,
ERTS_P2P_FLG_ALLOW_OTHER_X,
&port, 0);
/* If the name is not registered,
* or if we asked for proc and got a port,
* or if we asked for port and got a proc,
* we just send the 'DOWN' message.
*/
if ((!proc && !port) ||
(type == am_process && port) ||
(type == am_port && proc)) {
DeclareTmpHeap(lhp,3,self);
Eterm item;
UseTmpHeap(3,self);
erts_smp_proc_unlock(self, ERTS_PROC_LOCK_LINK);
p_locks &= ~ERTS_PROC_LOCK_LINK;
item = TUPLE2(lhp, target_name, erts_this_dist_entry->sysname);
erts_queue_monitor_message(self, &p_locks,
ret,
type, /* = process|port :: atom() */
item, am_noproc);
UnUseTmpHeap(3,self);
}
else if (port) {
erts_smp_proc_unlock(self, p_locks & ~ERTS_PROC_LOCK_MAIN);
p_locks &= ~ERTS_PROC_LOCK_MAIN;
switch (erts_port_monitor(self, port, target_name, &ret)) {
case ERTS_PORT_OP_DONE:
return ret;
case ERTS_PORT_OP_SCHEDULED: { /* Scheduled a signal */
ASSERT(is_internal_ordinary_ref(ret));
BIF_TRAP3(await_port_send_result_trap, self,
ret, am_true, ret);
/* bif_trap returns */
} break;
default:
goto badarg;
}
}
else if (proc != self) {
erts_add_monitor(&ERTS_P_MONITORS(self), MON_ORIGIN, ret,
proc->common.id, target_name);
erts_add_monitor(&ERTS_P_MONITORS(proc), MON_TARGET, ret,
self->common.id, target_name);
erts_smp_proc_unlock(proc, ERTS_PROC_LOCK_LINK);
}
if (p_locks) {
erts_smp_proc_unlock(self, p_locks & ~ERTS_PROC_LOCK_MAIN);
}
BIF_RET(ret);
badarg:
if (p_locks) {
erts_smp_proc_unlock(self, p_locks & ~ERTS_PROC_LOCK_MAIN);
}
BIF_ERROR(self, BADARG);
}
static BIF_RETTYPE
remote_monitor(Process *p, Eterm bifarg1, Eterm bifarg2,
DistEntry *dep, Eterm target, int byname)
{
ErtsDSigData dsd;
BIF_RETTYPE ret;
int code;
erts_smp_proc_lock(p, ERTS_PROC_LOCK_LINK);
code = erts_dsig_prepare(&dsd, dep, p, ERTS_DSP_RLOCK, 0);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
/* Let the dmonitor_p trap handle it */
case ERTS_DSIG_PREP_NOT_CONNECTED:
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_LINK);
ERTS_BIF_PREP_TRAP2(ret, dmonitor_p_trap, p, bifarg1, bifarg2);
break;
case ERTS_DSIG_PREP_CONNECTED:
if (!(dep->flags & DFLAG_DIST_MONITOR)
|| (byname && !(dep->flags & DFLAG_DIST_MONITOR_NAME))) {
erts_smp_de_runlock(dep);
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_LINK);
ERTS_BIF_PREP_ERROR(ret, p, BADARG);
}
else {
Eterm p_trgt, p_name, d_name, mon_ref;
mon_ref = erts_make_ref(p);
if (byname) {
p_trgt = dep->sysname;
p_name = target;
d_name = target;
}
else {
p_trgt = target;
p_name = NIL;
d_name = NIL;
}
erts_smp_de_links_lock(dep);
erts_add_monitor(&ERTS_P_MONITORS(p), MON_ORIGIN, mon_ref, p_trgt,
p_name);
erts_add_monitor(&(dep->monitors), MON_TARGET, mon_ref, p->common.id,
d_name);
erts_smp_de_links_unlock(dep);
erts_smp_de_runlock(dep);
erts_smp_proc_unlock(p, ERTS_PROC_LOCK_LINK);
code = erts_dsig_send_monitor(&dsd, p->common.id, target, mon_ref);
if (code == ERTS_DSIG_SEND_YIELD)
ERTS_BIF_PREP_YIELD_RETURN(ret, p, mon_ref);
else
ERTS_BIF_PREP_RET(ret, mon_ref);
}
break;
default:
ASSERT(! "Invalid dsig prepare result");
ERTS_BIF_PREP_ERROR(ret, p, EXC_INTERNAL_ERROR);
break;
}
BIF_RET(ret);
}
BIF_RETTYPE monitor_2(BIF_ALIST_2)
{
Eterm target = BIF_ARG_2;
BIF_RETTYPE ret;
DistEntry *dep = NULL;
int deref_de = 0;
/* Only process monitors are implemented */
switch (BIF_ARG_1) {
case am_time_offset: {
Eterm ref;
if (BIF_ARG_2 != am_clock_service) {
goto badarg;
}
ref = erts_make_ref(BIF_P);
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_LINK);
erts_add_monitor(&ERTS_P_MONITORS(BIF_P), MON_TIME_OFFSET,
ref, am_clock_service, NIL);
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK);
erts_monitor_time_offset(BIF_P->common.id, ref);
BIF_RET(ref);
}
case am_process:
case am_port:
break;
default:
goto badarg;
}
if (is_internal_pid(target) && BIF_ARG_1 == am_process) {
local_pid:
ret = local_pid_monitor(BIF_P, target, erts_make_ref(BIF_P), 0);
} else if (is_external_pid(target) && BIF_ARG_1 == am_process) {
dep = external_pid_dist_entry(target);
if (dep == erts_this_dist_entry)
goto local_pid;
ret = remote_monitor(BIF_P, BIF_ARG_1, BIF_ARG_2, dep, target, 0);
} else if (is_internal_port(target) && BIF_ARG_1 == am_port) {
local_port:
ret = local_port_monitor(BIF_P, target);
} else if (is_external_port(target) && BIF_ARG_1 == am_port) {
dep = external_port_dist_entry(target);
if (dep == erts_this_dist_entry) {
goto local_port;
}
goto badarg; /* No want remote port */
} else if (is_atom(target)) {
ret = local_name_monitor(BIF_P, BIF_ARG_1, target);
} else if (is_tuple(target)) {
Eterm *tp = tuple_val(target);
Eterm remote_node;
Eterm name;
if (arityval(*tp) != 2) {
goto badarg;
}
remote_node = tp[2];
name = tp[1];
if (!is_atom(remote_node) || !is_atom(name)) {
goto badarg;
}
if (!erts_is_alive && remote_node != am_Noname) {
goto badarg; /* Remote monitor from (this) undistributed node */
}
dep = erts_sysname_to_connected_dist_entry(remote_node);
if (dep == erts_this_dist_entry) {
deref_de = 1;
ret = local_name_monitor(BIF_P, BIF_ARG_1, name);
} else {
if (dep)
deref_de = 1;
ret = remote_monitor(BIF_P, BIF_ARG_1, BIF_ARG_2, dep, name, 1);
}
} else {
badarg:
ERTS_BIF_PREP_ERROR(ret, BIF_P, BADARG);
}
if (deref_de) {
deref_de = 0;
erts_deref_dist_entry(dep);
}
return ret;
}
/**********************************************************************/
/* this is a combination of the spawn and link BIFs */
BIF_RETTYPE spawn_link_3(BIF_ALIST_3)
{
ErlSpawnOpts so;
Eterm pid;
so.flags = erts_default_spo_flags|SPO_LINK;
pid = erl_create_process(BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3, &so);
if (is_non_value(pid)) {
BIF_ERROR(BIF_P, so.error_code);
} else {
if (ERTS_USE_MODIFIED_TIMING()) {
BIF_TRAP2(erts_delay_trap, BIF_P, pid, ERTS_MODIFIED_TIMING_DELAY);
}
BIF_RET(pid);
}
}
/**********************************************************************/
BIF_RETTYPE spawn_opt_1(BIF_ALIST_1)
{
ErlSpawnOpts so;
Eterm pid;
Eterm* tp;
Eterm ap;
Eterm arg;
Eterm res;
/*
* Check that the first argument is a tuple of four elements.
*/
if (is_not_tuple(BIF_ARG_1)) {
error:
BIF_ERROR(BIF_P, BADARG);
}
tp = tuple_val(BIF_ARG_1);
if (*tp != make_arityval(4))
goto error;
/*
* Store default values for options.
*/
so.flags = erts_default_spo_flags|SPO_USE_ARGS;
so.min_heap_size = H_MIN_SIZE;
so.min_vheap_size = BIN_VH_MIN_SIZE;
so.max_heap_size = H_MAX_SIZE;
so.max_heap_flags = H_MAX_FLAGS;
so.priority = PRIORITY_NORMAL;
so.max_gen_gcs = (Uint16) erts_smp_atomic32_read_nob(&erts_max_gen_gcs);
so.scheduler = 0;
/*
* Walk through the option list.
*/
ap = tp[4];
while (is_list(ap)) {
arg = CAR(list_val(ap));
if (arg == am_link) {
so.flags |= SPO_LINK;
} else if (arg == am_monitor) {
so.flags |= SPO_MONITOR;
} else if (is_tuple(arg)) {
Eterm* tp2 = tuple_val(arg);
Eterm val;
if (*tp2 != make_arityval(2))
goto error;
arg = tp2[1];
val = tp2[2];
if (arg == am_priority) {
if (val == am_max)
so.priority = PRIORITY_MAX;
else if (val == am_high)
so.priority = PRIORITY_HIGH;
else if (val == am_normal)
so.priority = PRIORITY_NORMAL;
else if (val == am_low)
so.priority = PRIORITY_LOW;
else
goto error;
} else if (arg == am_message_queue_data) {
switch (val) {
case am_on_heap:
so.flags &= ~SPO_OFF_HEAP_MSGQ;
so.flags |= SPO_ON_HEAP_MSGQ;
break;
case am_off_heap:
so.flags &= ~SPO_ON_HEAP_MSGQ;
so.flags |= SPO_OFF_HEAP_MSGQ;
break;
default:
goto error;
}
} else if (arg == am_min_heap_size && is_small(val)) {
Sint min_heap_size = signed_val(val);
if (min_heap_size < 0) {
goto error;
} else if (min_heap_size < H_MIN_SIZE) {
so.min_heap_size = H_MIN_SIZE;
} else {
so.min_heap_size = erts_next_heap_size(min_heap_size, 0);
}
} else if (arg == am_max_heap_size) {
if (!erts_max_heap_size(val, &so.max_heap_size, &so.max_heap_flags))
goto error;
} else if (arg == am_min_bin_vheap_size && is_small(val)) {
Sint min_vheap_size = signed_val(val);
if (min_vheap_size < 0) {
goto error;
} else if (min_vheap_size < BIN_VH_MIN_SIZE) {
so.min_vheap_size = BIN_VH_MIN_SIZE;
} else {
so.min_vheap_size = erts_next_heap_size(min_vheap_size, 0);
}
} else if (arg == am_fullsweep_after && is_small(val)) {
Sint max_gen_gcs = signed_val(val);
if (max_gen_gcs < 0) {
goto error;
} else {
so.max_gen_gcs = max_gen_gcs;
}
} else if (arg == am_scheduler && is_small(val)) {
Sint scheduler = signed_val(val);
if (scheduler < 0 || erts_no_schedulers < scheduler)
goto error;
so.scheduler = (int) scheduler;
} else {
goto error;
}
} else {
goto error;
}
ap = CDR(list_val(ap));
}
if (is_not_nil(ap)) {
goto error;
}
if (so.max_heap_size != 0 && so.max_heap_size < so.min_heap_size) {
goto error;
}
/*
* Spawn the process.
*/
pid = erl_create_process(BIF_P, tp[1], tp[2], tp[3], &so);
if (is_non_value(pid)) {
BIF_ERROR(BIF_P, so.error_code);
} else if (so.flags & SPO_MONITOR) {
Eterm* hp = HAlloc(BIF_P, 3);
res = TUPLE2(hp, pid, so.mref);
} else {
res = pid;
}
if (ERTS_USE_MODIFIED_TIMING()) {
BIF_TRAP2(erts_delay_trap, BIF_P, res, ERTS_MODIFIED_TIMING_DELAY);
}
else {
BIF_RET(res);
}
}
/**********************************************************************/
/* remove a link from a process */
BIF_RETTYPE unlink_1(BIF_ALIST_1)
{
Process *rp;
DistEntry *dep;
ErtsLink *l = NULL, *rl = NULL;
ErtsProcLocks cp_locks = ERTS_PROC_LOCK_MAIN;
/*
* SMP specific note concerning incoming exit signals:
* We have to have at least the status lock during removal of
* the link half on current process, and check for and handle
* a present pending exit while the status lock is held. This
* in order to ensure that we wont be exited by a link after
* it has been removed.
*
* (We also have to have the link lock, of course, in order to
* be allowed to remove the link...)
*/
if (IS_TRACED_FL(BIF_P, F_TRACE_PROCS)) {
trace_proc(BIF_P, cp_locks, BIF_P, am_unlink, BIF_ARG_1);
}
if (is_internal_port(BIF_ARG_1)) {
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_LINK|ERTS_PROC_LOCK_STATUS);
if (ERTS_PROC_PENDING_EXIT(BIF_P))
goto handle_pending_exit;
l = erts_remove_link(&ERTS_P_LINKS(BIF_P), BIF_ARG_1);
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK|ERTS_PROC_LOCK_STATUS);
if (l) {
Port *prt;
erts_destroy_link(l);
/* Send unlink signal */
prt = erts_port_lookup(BIF_ARG_1, ERTS_PORT_SFLGS_DEAD);
if (prt) {
ErtsPortOpResult res;
Eterm ref;
Eterm *refp = erts_port_synchronous_ops ? &ref : NULL;
#ifdef DEBUG
ref = NIL;
#endif
res = erts_port_unlink(BIF_P, prt, BIF_P->common.id, refp);
if (refp && res == ERTS_PORT_OP_SCHEDULED) {
ASSERT(is_internal_ordinary_ref(ref));
BIF_TRAP3(await_port_send_result_trap, BIF_P, ref, am_true, am_true);
}
}
}
BIF_RET(am_true);
}
else if (is_external_port(BIF_ARG_1)
&& external_port_dist_entry(BIF_ARG_1) == erts_this_dist_entry) {
BIF_RET(am_true);
}
if (is_not_pid(BIF_ARG_1))
BIF_ERROR(BIF_P, BADARG);
if (is_external_pid(BIF_ARG_1)) {
ErtsDistLinkData dld;
int code;
ErtsDSigData dsd;
/* Blind removal, we might have trapped or anything, this leaves
us in a state where monitors might be inconsistent, but the dist
code should take care of it. */
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_LINK|ERTS_PROC_LOCK_STATUS);
if (ERTS_PROC_PENDING_EXIT(BIF_P))
goto handle_pending_exit;
l = erts_remove_link(&ERTS_P_LINKS(BIF_P), BIF_ARG_1);
erts_smp_proc_unlock(BIF_P,
ERTS_PROC_LOCK_LINK|ERTS_PROC_LOCK_STATUS);
if (l)
erts_destroy_link(l);
dep = external_pid_dist_entry(BIF_ARG_1);
if (dep == erts_this_dist_entry) {
BIF_RET(am_true);
}
code = erts_dsig_prepare(&dsd, dep, BIF_P, ERTS_DSP_NO_LOCK, 0);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
case ERTS_DSIG_PREP_NOT_CONNECTED:
#if 1
BIF_RET(am_true);
#else
/*
* This is how we used to do it, but the link is obviously not
* active, so I see no point in setting up a connection.
* /Rickard
*/
BIF_TRAP1(dunlink_trap, BIF_P, BIF_ARG_1);
#endif
case ERTS_DSIG_PREP_CONNECTED:
erts_remove_dist_link(&dld, BIF_P->common.id, BIF_ARG_1, dep);
code = erts_dsig_send_unlink(&dsd, BIF_P->common.id, BIF_ARG_1);
erts_destroy_dist_link(&dld);
if (code == ERTS_DSIG_SEND_YIELD)
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
BIF_RET(am_true);
default:
ASSERT(! "Invalid dsig prepare result");
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
}
}
/* Internal pid... */
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_LINK|ERTS_PROC_LOCK_STATUS);
cp_locks |= ERTS_PROC_LOCK_LINK|ERTS_PROC_LOCK_STATUS;
/* get process struct */
rp = erts_pid2proc_opt(BIF_P, cp_locks,
BIF_ARG_1, ERTS_PROC_LOCK_LINK,
ERTS_P2P_FLG_ALLOW_OTHER_X);
if (ERTS_PROC_PENDING_EXIT(BIF_P)) {
if (rp && rp != BIF_P)
erts_smp_proc_unlock(rp, ERTS_PROC_LOCK_LINK);
goto handle_pending_exit;
}
/* unlink and ignore errors */
l = erts_remove_link(&ERTS_P_LINKS(BIF_P), BIF_ARG_1);
if (l != NULL)
erts_destroy_link(l);
if (!rp) {
ERTS_SMP_ASSERT_IS_NOT_EXITING(BIF_P);
}
else {
rl = erts_remove_link(&ERTS_P_LINKS(rp), BIF_P->common.id);
if (rl != NULL)
erts_destroy_link(rl);
if (IS_TRACED_FL(rp, F_TRACE_PROCS) && rl != NULL) {
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_STATUS);
cp_locks &= ~ERTS_PROC_LOCK_STATUS;
trace_proc(BIF_P, (ERTS_PROC_LOCK_MAIN | ERTS_PROC_LOCK_LINK),
rp, am_getting_unlinked, BIF_P->common.id);
}
if (rp != BIF_P)
erts_smp_proc_unlock(rp, ERTS_PROC_LOCK_LINK);
}
erts_smp_proc_unlock(BIF_P, cp_locks & ~ERTS_PROC_LOCK_MAIN);
BIF_RET(am_true);
handle_pending_exit:
erts_handle_pending_exit(BIF_P, (ERTS_PROC_LOCK_MAIN
| ERTS_PROC_LOCK_LINK
| ERTS_PROC_LOCK_STATUS));
ASSERT(ERTS_PROC_IS_EXITING(BIF_P));
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_LINK|ERTS_PROC_LOCK_STATUS);
ERTS_BIF_EXITED(BIF_P);
}
BIF_RETTYPE hibernate_3(BIF_ALIST_3)
{
/*
* hibernate/3 is usually translated to an instruction; therefore
* this function is only called from HiPE or when the call could not
* be translated.
*/
Eterm reg[3];
if (erts_hibernate(BIF_P, BIF_ARG_1, BIF_ARG_2, BIF_ARG_3, reg)) {
/*
* If hibernate succeeded, TRAP. The process will be wait in a
* hibernated state if its state is inactive (!ERTS_PSFLG_ACTIVE);
* otherwise, continue executing (if any message was in the queue).
*/
BIF_TRAP_CODE_PTR_(BIF_P, BIF_P->i);
}
return THE_NON_VALUE;
}
/**********************************************************************/
BIF_RETTYPE get_stacktrace_0(BIF_ALIST_0)
{
Eterm t = build_stacktrace(BIF_P, BIF_P->ftrace);
BIF_RET(t);
}
/**********************************************************************/
/*
* This is like exit/1, except that errors are logged if they terminate
* the process, and the final error value will be {Term,StackTrace}.
*/
BIF_RETTYPE error_1(BIF_ALIST_1)
{
BIF_P->fvalue = BIF_ARG_1;
BIF_ERROR(BIF_P, EXC_ERROR);
}
/**********************************************************************/
/*
* This is like error/1, except that the given 'args' will be included
* in the stacktrace.
*/
BIF_RETTYPE error_2(BIF_ALIST_2)
{
Eterm* hp = HAlloc(BIF_P, 3);
BIF_P->fvalue = TUPLE2(hp, BIF_ARG_1, BIF_ARG_2);
BIF_ERROR(BIF_P, EXC_ERROR_2);
}
/**********************************************************************/
/*
* This is like exactly like error/1. The only difference is
* that Dialyzer thinks that it it will return an arbitrary term.
* It is useful in stub functions for NIFs.
*/
BIF_RETTYPE nif_error_1(BIF_ALIST_1)
{
BIF_P->fvalue = BIF_ARG_1;
BIF_ERROR(BIF_P, EXC_ERROR);
}
/**********************************************************************/
/*
* This is like exactly like error/2. The only difference is
* that Dialyzer thinks that it it will return an arbitrary term.
* It is useful in stub functions for NIFs.
*/
BIF_RETTYPE nif_error_2(BIF_ALIST_2)
{
Eterm* hp = HAlloc(BIF_P, 3);
BIF_P->fvalue = TUPLE2(hp, BIF_ARG_1, BIF_ARG_2);
BIF_ERROR(BIF_P, EXC_ERROR_2);
}
/**********************************************************************/
/* this is like throw/1 except that we set freason to EXC_EXIT */
BIF_RETTYPE exit_1(BIF_ALIST_1)
{
BIF_P->fvalue = BIF_ARG_1; /* exit value */
BIF_ERROR(BIF_P, EXC_EXIT);
}
/**********************************************************************/
/* raise an exception of given class, value and stacktrace.
*
* If there is an error in the argument format,
* return the atom 'badarg' instead.
*/
BIF_RETTYPE raise_3(BIF_ALIST_3)
{
Process *c_p = BIF_P;
Eterm class = BIF_ARG_1;
Eterm value = BIF_ARG_2;
Eterm stacktrace = BIF_ARG_3;
Eterm reason;
Eterm l, *hp, *hp_end, *tp;
int depth, cnt;
size_t sz;
int must_copy = 0;
struct StackTrace *s;
if (class == am_error) {
c_p->fvalue = value;
reason = EXC_ERROR;
} else if (class == am_exit) {
c_p->fvalue = value;
reason = EXC_EXIT;
} else if (class == am_throw) {
c_p->fvalue = value;
reason = EXC_THROWN;
} else goto error;
reason &= ~EXF_SAVETRACE;
/* Check syntax of stacktrace, and count depth.
* Accept anything that can be returned from erlang:get_stacktrace/0,
* as well as a 2-tuple with a fun as first element that the
* error_handler may need to give us. Also allow old-style
* MFA three-tuples.
*/
for (l = stacktrace, depth = 0;
is_list(l);
l = CDR(list_val(l)), depth++) {
Eterm t = CAR(list_val(l));
Eterm location = NIL;
if (is_not_tuple(t)) goto error;
tp = tuple_val(t);
switch (arityval(tp[0])) {
case 2:
/* {Fun,Args} */
if (is_fun(tp[1])) {
must_copy = 1;
} else {
goto error;
}
break;
case 3:
/*
* One of:
* {Fun,Args,Location}
* {M,F,A}
*/
if (is_fun(tp[1])) {
location = tp[3];
} else if (is_atom(tp[1]) && is_atom(tp[2])) {
must_copy = 1;
} else {
goto error;
}
break;
case 4:
if (!(is_atom(tp[1]) && is_atom(tp[2]))) {
goto error;
}
location = tp[4];
break;
default:
goto error;
}
if (is_not_list(location) && is_not_nil(location)) {
goto error;
}
}
if (is_not_nil(l)) goto error;
/* Create stacktrace and store */
if (erts_backtrace_depth < depth) {
depth = erts_backtrace_depth;
must_copy = 1;
}
if (must_copy) {
cnt = depth;
c_p->ftrace = NIL;
} else {
/* No need to copy the stacktrace */
cnt = 0;
c_p->ftrace = stacktrace;
}
tp = &c_p->ftrace;
sz = (offsetof(struct StackTrace, trace) + sizeof(Eterm) - 1)
/ sizeof(Eterm);
hp = HAlloc(c_p, sz + (2+6)*(cnt + 1));
hp_end = hp + sz + (2+6)*(cnt + 1);
s = (struct StackTrace *) hp;
s->header = make_neg_bignum_header(sz - 1);
s->freason = reason;
s->pc = NULL;
s->current = NULL;
s->depth = 0;
hp += sz;
if (must_copy) {
int cnt;
/* Copy list up to depth */
for (cnt = 0, l = stacktrace;
cnt < depth;
cnt++, l = CDR(list_val(l))) {
Eterm t;
Eterm *tpp;
int arity;
ASSERT(*tp == NIL);
t = CAR(list_val(l));
tpp = tuple_val(t);
arity = arityval(tpp[0]);
if (arity == 2) {
t = TUPLE3(hp, tpp[1], tpp[2], NIL);
hp += 4;
} else if (arity == 3 && is_atom(tpp[1])) {
t = TUPLE4(hp, tpp[1], tpp[2], tpp[3], NIL);
hp += 5;
}
*tp = CONS(hp, t, *tp);
tp = &CDR(list_val(*tp));
hp += 2;
}
}
c_p->ftrace = CONS(hp, c_p->ftrace, make_big((Eterm *) s));
hp += 2;
ASSERT(hp <= hp_end);
HRelease(c_p, hp_end, hp);
BIF_ERROR(c_p, reason);
error:
return am_badarg;
}
/**********************************************************************/
/* send an exit message to another process (if trapping exits) or
exit the other process */
BIF_RETTYPE exit_2(BIF_ALIST_2)
{
Process *rp;
/*
* If the first argument is not a pid, or a local port it is an error.
*/
if (is_internal_port(BIF_ARG_1)) {
Eterm ref, *refp;
Uint32 invalid_flags;
Port *prt;
if (erts_port_synchronous_ops) {
refp = &ref;
invalid_flags = ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP;
}
else {
refp = NULL;
invalid_flags = ERTS_PORT_SFLGS_INVALID_LOOKUP;
}
prt = erts_port_lookup(BIF_ARG_1, invalid_flags);
if (prt) {
ErtsPortOpResult res;
#ifdef DEBUG
ref = NIL;
#endif
res = erts_port_exit(BIF_P, 0, prt, BIF_P->common.id, BIF_ARG_2, refp);
ERTS_BIF_CHK_EXITED(BIF_P);
if (refp && res == ERTS_PORT_OP_SCHEDULED) {
ASSERT(is_internal_ordinary_ref(ref));
BIF_TRAP3(await_port_send_result_trap, BIF_P, ref, am_true, am_true);
}
}
BIF_RET(am_true);
}
else if(is_external_port(BIF_ARG_1)
&& external_port_dist_entry(BIF_ARG_1) == erts_this_dist_entry)
BIF_RET(am_true);
/*
* If it is a remote pid, send a signal to the remote node.
*/
if (is_external_pid(BIF_ARG_1)) {
int code;
ErtsDSigData dsd;
DistEntry *dep;
dep = external_pid_dist_entry(BIF_ARG_1);
if(dep == erts_this_dist_entry)
BIF_RET(am_true);
code = erts_dsig_prepare(&dsd, dep, BIF_P, ERTS_DSP_NO_LOCK, 0);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
case ERTS_DSIG_PREP_NOT_CONNECTED:
BIF_TRAP2(dexit_trap, BIF_P, BIF_ARG_1, BIF_ARG_2);
case ERTS_DSIG_PREP_CONNECTED:
code = erts_dsig_send_exit2(&dsd, BIF_P->common.id, BIF_ARG_1, BIF_ARG_2);
if (code == ERTS_DSIG_SEND_YIELD)
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
BIF_RET(am_true);
default:
ASSERT(! "Invalid dsig prepare result");
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
}
}
else if (is_not_internal_pid(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
else {
/*
* The pid is internal. Verify that it refers to an existing process.
*/
ErtsProcLocks rp_locks;
if (BIF_ARG_1 == BIF_P->common.id) {
rp_locks = ERTS_PROC_LOCKS_ALL;
rp = BIF_P;
erts_smp_proc_lock(rp, ERTS_PROC_LOCKS_ALL_MINOR);
}
else {
rp_locks = ERTS_PROC_LOCKS_XSIG_SEND;
rp = erts_pid2proc(BIF_P, ERTS_PROC_LOCK_MAIN,
BIF_ARG_1, rp_locks);
if (!rp) {
BIF_RET(am_true);
}
}
/*
* Send an exit signal.
*/
erts_send_exit_signal(BIF_P,
BIF_P->common.id,
rp,
&rp_locks,
BIF_ARG_2,
NIL,
NULL,
BIF_P == rp ? ERTS_XSIG_FLG_NO_IGN_NORMAL : 0);
if (rp == BIF_P)
rp_locks &= ~ERTS_PROC_LOCK_MAIN;
if (rp_locks)
erts_smp_proc_unlock(rp, rp_locks);
/*
* We may have exited ourselves and may have to take action.
*/
ERTS_BIF_CHK_EXITED(BIF_P);
BIF_RET(am_true);
}
}
/**********************************************************************/
/* this sets some process info- trapping exits or the error handler */
/* Handle flags common to both process_flag_2 and process_flag_3. */
static BIF_RETTYPE process_flag_aux(Process *BIF_P,
Process *rp,
Eterm flag,
Eterm val)
{
Eterm old_value = NIL; /* shut up warning about use before set */
Sint i;
if (flag == am_save_calls) {
struct saved_calls *scb;
if (!is_small(val))
goto error;
i = signed_val(val);
if (i < 0 || i > 10000)
goto error;
if (i == 0)
scb = NULL;
else {
Uint sz = sizeof(*scb) + (i-1) * sizeof(scb->ct[0]);
scb = erts_alloc(ERTS_ALC_T_CALLS_BUF, sz);
scb->len = i;
scb->cur = 0;
scb->n = 0;
}
#ifdef HIPE
if (rp->flags & F_HIPE_MODE) {
ASSERT(!ERTS_PROC_GET_SAVED_CALLS_BUF(rp));
scb = ERTS_PROC_SET_SUSPENDED_SAVED_CALLS_BUF(rp, scb);
}
else
#endif
{
#ifdef HIPE
ASSERT(!ERTS_PROC_GET_SUSPENDED_SAVED_CALLS_BUF(rp));
#endif
scb = ERTS_PROC_SET_SAVED_CALLS_BUF(rp, scb);
if (rp == BIF_P && ((scb && i == 0) || (!scb && i != 0))) {
/* Adjust fcalls to match save calls setting... */
if (i == 0)
BIF_P->fcalls += CONTEXT_REDS; /* disabled it */
else
BIF_P->fcalls -= CONTEXT_REDS; /* enabled it */
/*
* Make sure we reschedule immediately so the
* change take effect at once.
*/
ERTS_VBUMP_ALL_REDS(BIF_P);
}
}
if (!scb)
old_value = make_small(0);
else {
old_value = make_small(scb->len);
erts_free(ERTS_ALC_T_CALLS_BUF, (void *) scb);
}
BIF_RET(old_value);
}
error:
BIF_ERROR(BIF_P, BADARG);
}
BIF_RETTYPE process_flag_2(BIF_ALIST_2)
{
Eterm old_value;
if (BIF_ARG_1 == am_error_handler) {
if (is_not_atom(BIF_ARG_2)) {
goto error;
}
old_value = erts_proc_set_error_handler(BIF_P, BIF_ARG_2);
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_priority) {
old_value = erts_set_process_priority(BIF_P, BIF_ARG_2);
if (old_value == THE_NON_VALUE)
goto error;
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_trap_exit) {
erts_aint32_t state;
Uint trap_exit;
if (BIF_ARG_2 == am_true) {
trap_exit = 1;
} else if (BIF_ARG_2 == am_false) {
trap_exit = 0;
} else {
goto error;
}
/*
* NOTE: It is important that we check for pending exit signals
* and handle them before returning if trap_exit is set to
* true. For more info, see implementation of
* erts_send_exit_signal().
*/
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCKS_XSIG_SEND);
if (trap_exit)
state = erts_smp_atomic32_read_bor_mb(&BIF_P->state,
ERTS_PSFLG_TRAP_EXIT);
else
state = erts_smp_atomic32_read_band_mb(&BIF_P->state,
~ERTS_PSFLG_TRAP_EXIT);
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCKS_XSIG_SEND);
if (state & ERTS_PSFLG_PENDING_EXIT) {
erts_handle_pending_exit(BIF_P, ERTS_PROC_LOCK_MAIN);
ERTS_BIF_EXITED(BIF_P);
}
old_value = (state & ERTS_PSFLG_TRAP_EXIT) ? am_true : am_false;
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_scheduler) {
ErtsRunQueue *old, *new, *curr;
Sint sched;
erts_aint32_t state;
if (!is_small(BIF_ARG_2))
goto error;
sched = signed_val(BIF_ARG_2);
if (sched < 0 || erts_no_schedulers < sched)
goto error;
if (sched == 0) {
new = NULL;
state = erts_smp_atomic32_read_band_mb(&BIF_P->state,
~ERTS_PSFLG_BOUND);
}
else {
new = erts_schedid2runq(sched);
erts_atomic_set_nob(&BIF_P->run_queue, (erts_aint_t) new);
state = erts_smp_atomic32_read_bor_mb(&BIF_P->state,
ERTS_PSFLG_BOUND);
}
curr = erts_proc_sched_data(BIF_P)->run_queue;
old = (ERTS_PSFLG_BOUND & state) ? curr : NULL;
ASSERT(!old || old == curr);
old_value = old ? make_small(old->ix+1) : make_small(0);
if (new && new != curr)
ERTS_BIF_YIELD_RETURN_X(BIF_P, old_value, am_scheduler);
else
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_min_heap_size) {
Sint i;
if (!is_small(BIF_ARG_2)) {
goto error;
}
i = signed_val(BIF_ARG_2);
if (i < 0) {
goto error;
}
old_value = make_small(BIF_P->min_heap_size);
if (i < H_MIN_SIZE) {
BIF_P->min_heap_size = H_MIN_SIZE;
} else {
BIF_P->min_heap_size = erts_next_heap_size(i, 0);
}
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_min_bin_vheap_size) {
Sint i;
if (!is_small(BIF_ARG_2)) {
goto error;
}
i = signed_val(BIF_ARG_2);
if (i < 0) {
goto error;
}
old_value = make_small(BIF_P->min_vheap_size);
if (i < BIN_VH_MIN_SIZE) {
BIF_P->min_vheap_size = BIN_VH_MIN_SIZE;
} else {
BIF_P->min_vheap_size = erts_next_heap_size(i, 0);
}
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_max_heap_size) {
Eterm *hp;
Uint sz = 0, max_heap_size, max_heap_flags;
if (!erts_max_heap_size(BIF_ARG_2, &max_heap_size, &max_heap_flags))
goto error;
if ((max_heap_size < MIN_HEAP_SIZE(BIF_P) && max_heap_size != 0))
goto error;
erts_max_heap_size_map(MAX_HEAP_SIZE_GET(BIF_P), MAX_HEAP_SIZE_FLAGS_GET(BIF_P), NULL, &sz);
hp = HAlloc(BIF_P, sz);
old_value = erts_max_heap_size_map(MAX_HEAP_SIZE_GET(BIF_P), MAX_HEAP_SIZE_FLAGS_GET(BIF_P), &hp, NULL);
MAX_HEAP_SIZE_SET(BIF_P, max_heap_size);
MAX_HEAP_SIZE_FLAGS_SET(BIF_P, max_heap_flags);
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_message_queue_data) {
old_value = erts_change_message_queue_management(BIF_P, BIF_ARG_2);
if (is_non_value(old_value))
goto error;
BIF_RET(old_value);
}
else if (BIF_ARG_1 == am_sensitive) {
Uint is_sensitive;
if (BIF_ARG_2 == am_true) {
is_sensitive = 1;
} else if (BIF_ARG_2 == am_false) {
is_sensitive = 0;
} else {
goto error;
}
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCKS_ALL_MINOR);
old_value = (ERTS_TRACE_FLAGS(BIF_P) & F_SENSITIVE
? am_true
: am_false);
if (is_sensitive) {
ERTS_TRACE_FLAGS(BIF_P) |= F_SENSITIVE;
} else {
ERTS_TRACE_FLAGS(BIF_P) &= ~F_SENSITIVE;
}
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCKS_ALL_MINOR);
/* make sure to bump all reds so that we get
rescheduled immediately so setting takes effect */
BIF_RET2(old_value, CONTEXT_REDS);
}
else if (BIF_ARG_1 == am_monitor_nodes) {
/*
* This argument is intentionally *not* documented. It is intended
* to be used by net_kernel:monitor_nodes/1.
*/
old_value = erts_monitor_nodes(BIF_P, BIF_ARG_2, NIL);
if (old_value == THE_NON_VALUE)
goto error;
BIF_RET(old_value);
}
else if (is_tuple(BIF_ARG_1)) {
/*
* This argument is intentionally *not* documented. It is intended
* to be used by net_kernel:monitor_nodes/2.
*/
Eterm *tp = tuple_val(BIF_ARG_1);
if (arityval(tp[0]) == 2) {
if (tp[1] == am_monitor_nodes) {
old_value = erts_monitor_nodes(BIF_P, BIF_ARG_2, tp[2]);
if (old_value == THE_NON_VALUE)
goto error;
BIF_RET(old_value);
}
}
/* Fall through and try process_flag_aux() ... */
}
BIF_RET(process_flag_aux(BIF_P, BIF_P, BIF_ARG_1, BIF_ARG_2));
error:
BIF_ERROR(BIF_P, BADARG);
}
BIF_RETTYPE process_flag_3(BIF_ALIST_3)
{
Process *rp;
Eterm res;
rp = erts_pid2proc_not_running(BIF_P, ERTS_PROC_LOCK_MAIN,
BIF_ARG_1, ERTS_PROC_LOCK_MAIN);
if (rp == ERTS_PROC_LOCK_BUSY)
ERTS_BIF_YIELD3(bif_export[BIF_process_flag_3], BIF_P,
BIF_ARG_1, BIF_ARG_2, BIF_ARG_3);
if (!rp)
BIF_ERROR(BIF_P, BADARG);
res = process_flag_aux(BIF_P, rp, BIF_ARG_2, BIF_ARG_3);
if (rp != BIF_P)
erts_smp_proc_unlock(rp, ERTS_PROC_LOCK_MAIN);
return res;
}
/**********************************************************************/
/* register(atom, Process|Port) registers a global process or port
(for this node) */
BIF_RETTYPE register_2(BIF_ALIST_2) /* (Atom, Pid|Port) */
{
if (erts_register_name(BIF_P, BIF_ARG_1, BIF_ARG_2))
BIF_RET(am_true);
else {
BIF_ERROR(BIF_P, BADARG);
}
}
/**********************************************************************/
/* removes the registration of a process or port */
BIF_RETTYPE unregister_1(BIF_ALIST_1)
{
int res;
if (is_not_atom(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
res = erts_unregister_name(BIF_P, ERTS_PROC_LOCK_MAIN, NULL, BIF_ARG_1);
if (res == 0) {
BIF_ERROR(BIF_P, BADARG);
}
BIF_RET(am_true);
}
/**********************************************************************/
/* find out the pid of a registered process */
/* this is a rather unsafe BIF as it allows users to do nasty things. */
BIF_RETTYPE whereis_1(BIF_ALIST_1)
{
Eterm res;
if (is_not_atom(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
res = erts_whereis_name_to_id(BIF_P, BIF_ARG_1);
BIF_RET(res);
}
/**********************************************************************/
/*
* erlang:'!'/2
*/
HIPE_WRAPPER_BIF_DISABLE_GC(ebif_bang, 2)
BIF_RETTYPE
ebif_bang_2(BIF_ALIST_2)
{
return erl_send(BIF_P, BIF_ARG_1, BIF_ARG_2);
}
/*
* Send a message to Process, Port or Registered Process.
* Returns non-negative reduction bump or negative result code.
*/
#define SEND_TRAP (-1)
#define SEND_YIELD (-2)
#define SEND_YIELD_RETURN (-3)
#define SEND_BADARG (-4)
#define SEND_USER_ERROR (-5)
#define SEND_INTERNAL_ERROR (-6)
#define SEND_AWAIT_RESULT (-7)
#define SEND_YIELD_CONTINUE (-8)
static Sint remote_send(Process *p, DistEntry *dep,
Eterm to, Eterm full_to, Eterm msg,
ErtsSendContext* ctx)
{
Sint res;
int code;
ASSERT(is_atom(to) || is_external_pid(to));
code = erts_dsig_prepare(&ctx->dsd, dep, p, ERTS_DSP_NO_LOCK, !ctx->suspend);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
case ERTS_DSIG_PREP_NOT_CONNECTED:
res = SEND_TRAP;
break;
case ERTS_DSIG_PREP_WOULD_SUSPEND:
ASSERT(!ctx->suspend);
res = SEND_YIELD;
break;
case ERTS_DSIG_PREP_CONNECTED: {
if (is_atom(to))
code = erts_dsig_send_reg_msg(to, msg, ctx);
else
code = erts_dsig_send_msg(to, msg, ctx);
/*
* Note that reductions have been bumped on calling
* process by erts_dsig_send_reg_msg() or
* erts_dsig_send_msg().
*/
if (code == ERTS_DSIG_SEND_YIELD)
res = SEND_YIELD_RETURN;
else if (code == ERTS_DSIG_SEND_CONTINUE)
res = SEND_YIELD_CONTINUE;
else
res = 0;
break;
}
default:
ASSERT(! "Invalid dsig prepare result");
res = SEND_INTERNAL_ERROR;
}
if (res >= 0) {
if (IS_TRACED_FL(p, F_TRACE_SEND))
trace_send(p, full_to, msg);
if (ERTS_PROC_GET_SAVED_CALLS_BUF(p))
save_calls(p, &exp_send);
}
return res;
}
static Sint
do_send(Process *p, Eterm to, Eterm msg, Eterm *refp, ErtsSendContext *ctx)
{
Eterm portid;
Port *pt;
Process* rp;
DistEntry *dep;
Eterm* tp;
if (is_internal_pid(to)) {
if (IS_TRACED_FL(p, F_TRACE_SEND))
trace_send(p, to, msg);
if (ERTS_PROC_GET_SAVED_CALLS_BUF(p))
save_calls(p, &exp_send);
rp = erts_proc_lookup_raw(to);
if (!rp)
return 0;
} else if (is_external_pid(to)) {
dep = external_pid_dist_entry(to);
if(dep == erts_this_dist_entry) {
erts_dsprintf_buf_t *dsbufp = erts_create_logger_dsbuf();
erts_dsprintf(dsbufp,
"Discarding message %T from %T to %T in an old "
"incarnation (%d) of this node (%d)\n",
msg,
p->common.id,
to,
external_pid_creation(to),
erts_this_node->creation);
erts_send_error_to_logger(p->group_leader, dsbufp);
return 0;
}
return remote_send(p, dep, to, to, msg, ctx);
} else if (is_atom(to)) {
Eterm id = erts_whereis_name_to_id(p, to);
rp = erts_proc_lookup_raw(id);
if (rp) {
if (IS_TRACED_FL(p, F_TRACE_SEND))
trace_send(p, to, msg);
if (ERTS_PROC_GET_SAVED_CALLS_BUF(p))
save_calls(p, &exp_send);
goto send_message;
}
pt = erts_port_lookup(id,
(erts_port_synchronous_ops
? ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP
: ERTS_PORT_SFLGS_INVALID_LOOKUP));
if (pt) {
portid = id;
goto port_common;
}
if (IS_TRACED_FL(p, F_TRACE_SEND))
trace_send(p, to, msg);
if (ERTS_PROC_GET_SAVED_CALLS_BUF(p))
save_calls(p, &exp_send);
return SEND_BADARG;
} else if (is_external_port(to)
&& (external_port_dist_entry(to)
== erts_this_dist_entry)) {
erts_dsprintf_buf_t *dsbufp = erts_create_logger_dsbuf();
erts_dsprintf(dsbufp,
"Discarding message %T from %T to %T in an old "
"incarnation (%d) of this node (%d)\n",
msg,
p->common.id,
to,
external_port_creation(to),
erts_this_node->creation);
erts_send_error_to_logger(p->group_leader, dsbufp);
return 0;
} else if (is_internal_port(to)) {
int ret_val;
portid = to;
pt = erts_port_lookup(portid,
(erts_port_synchronous_ops
? ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP
: ERTS_PORT_SFLGS_INVALID_LOOKUP));
port_common:
ret_val = 0;
if (pt) {
int ps_flags = ctx->suspend ? 0 : ERTS_PORT_SIG_FLG_NOSUSPEND;
*refp = NIL;
if (IS_TRACED_FL(p, F_TRACE_SEND)) /* trace once only !! */
trace_send(p, portid, msg);
if (have_seqtrace(SEQ_TRACE_TOKEN(p))) {
seq_trace_update_send(p);
seq_trace_output(SEQ_TRACE_TOKEN(p), msg,
SEQ_TRACE_SEND, portid, p);
}
switch (erts_port_command(p, ps_flags, pt, msg, refp)) {
case ERTS_PORT_OP_CALLER_EXIT:
/* We are exiting... */
return SEND_USER_ERROR;
case ERTS_PORT_OP_BUSY:
/* Nothing has been sent */
if (ctx->suspend)
erts_suspend(p, ERTS_PROC_LOCK_MAIN, pt);
return SEND_YIELD;
case ERTS_PORT_OP_BUSY_SCHEDULED:
/* Message was sent */
if (ctx->suspend) {
erts_suspend(p, ERTS_PROC_LOCK_MAIN, pt);
ret_val = SEND_YIELD_RETURN;
break;
}
/* Fall through */
case ERTS_PORT_OP_SCHEDULED:
if (is_not_nil(*refp)) {
ASSERT(is_internal_ordinary_ref(*refp));
ret_val = SEND_AWAIT_RESULT;
}
break;
case ERTS_PORT_OP_DROPPED:
case ERTS_PORT_OP_BADARG:
case ERTS_PORT_OP_DONE:
break;
default:
ERTS_INTERNAL_ERROR("Unexpected erts_port_command() result");
break;
}
}
if (ERTS_PROC_GET_SAVED_CALLS_BUF(p))
save_calls(p, &exp_send);
if (ERTS_PROC_IS_EXITING(p)) {
KILL_CATCHES(p); /* Must exit */
return SEND_USER_ERROR;
}
return ret_val;
} else if (is_tuple(to)) { /* Remote send */
int ret;
tp = tuple_val(to);
if (*tp != make_arityval(2))
return SEND_BADARG;
if (is_not_atom(tp[1]) || is_not_atom(tp[2]))
return SEND_BADARG;
/* sysname_to_connected_dist_entry will return NULL if there
is no dist_entry or the dist_entry has no port,
but remote_send() will handle that. */
dep = erts_sysname_to_connected_dist_entry(tp[2]);
if (dep == erts_this_dist_entry) {
Eterm id;
erts_deref_dist_entry(dep);
if (IS_TRACED_FL(p, F_TRACE_SEND))
trace_send(p, to, msg);
if (ERTS_PROC_GET_SAVED_CALLS_BUF(p))
save_calls(p, &exp_send);
id = erts_whereis_name_to_id(p, tp[1]);
rp = erts_proc_lookup_raw(id);
if (rp)
goto send_message;
pt = erts_port_lookup(id,
(erts_port_synchronous_ops
? ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP
: ERTS_PORT_SFLGS_INVALID_LOOKUP));
if (pt) {
portid = id;
goto port_common;
}
return 0;
}
ret = remote_send(p, dep, tp[1], to, msg, ctx);
if (ret != SEND_YIELD_CONTINUE) {
if (dep) {
erts_deref_dist_entry(dep);
}
} else {
ctx->dep_to_deref = dep;
}
return ret;
} else {
if (IS_TRACED_FL(p, F_TRACE_SEND))
trace_send(p, to, msg);
if (ERTS_PROC_GET_SAVED_CALLS_BUF(p))
save_calls(p, &exp_send);
return SEND_BADARG;
}
send_message: {
ErtsProcLocks rp_locks = 0;
Sint res;
if (p == rp)
rp_locks |= ERTS_PROC_LOCK_MAIN;
/* send to local process */
res = erts_send_message(p, rp, &rp_locks, msg, 0);
if (erts_use_sender_punish)
res *= 4;
else
res = 0;
erts_smp_proc_unlock(rp,
p == rp
? (rp_locks & ~ERTS_PROC_LOCK_MAIN)
: rp_locks);
return res;
}
}
HIPE_WRAPPER_BIF_DISABLE_GC(send, 3)
BIF_RETTYPE send_3(BIF_ALIST_3)
{
BIF_RETTYPE retval;
Eterm ref;
Process *p = BIF_P;
Eterm to = BIF_ARG_1;
Eterm msg = BIF_ARG_2;
Eterm opts = BIF_ARG_3;
int connect = !0;
Eterm l = opts;
Sint result;
DeclareTypedTmpHeap(ErtsSendContext, ctx, BIF_P);
ERTS_MSACC_PUSH_STATE_M_X();
UseTmpHeap(sizeof(ErtsSendContext)/sizeof(Eterm), BIF_P);
ctx->suspend = !0;
ctx->dep_to_deref = NULL;
ctx->return_term = am_ok;
ctx->dss.reds = (Sint) (ERTS_BIF_REDS_LEFT(p) * TERM_TO_BINARY_LOOP_FACTOR);
ctx->dss.phase = ERTS_DSIG_SEND_PHASE_INIT;
while (is_list(l)) {
if (CAR(list_val(l)) == am_noconnect) {
connect = 0;
} else if (CAR(list_val(l)) == am_nosuspend) {
ctx->suspend = 0;
} else {
ERTS_BIF_PREP_ERROR(retval, p, BADARG);
goto done;
}
l = CDR(list_val(l));
}
if(!is_nil(l)) {
ERTS_BIF_PREP_ERROR(retval, p, BADARG);
goto done;
}
#ifdef DEBUG
ref = NIL;
#endif
ERTS_MSACC_SET_STATE_CACHED_M_X(ERTS_MSACC_STATE_SEND);
result = do_send(p, to, msg, &ref, ctx);
ERTS_MSACC_POP_STATE_M_X();
if (result > 0) {
ERTS_VBUMP_REDS(p, result);
if (ERTS_IS_PROC_OUT_OF_REDS(p))
goto yield_return;
ERTS_BIF_PREP_RET(retval, am_ok);
goto done;
}
switch (result) {
case 0:
/* May need to yield even though we do not bump reds here... */
if (ERTS_IS_PROC_OUT_OF_REDS(p))
goto yield_return;
ERTS_BIF_PREP_RET(retval, am_ok);
break;
case SEND_TRAP:
if (connect) {
ERTS_BIF_PREP_TRAP3(retval, dsend3_trap, p, to, msg, opts);
} else {
ERTS_BIF_PREP_RET(retval, am_noconnect);
}
break;
case SEND_YIELD:
if (ctx->suspend) {
ERTS_BIF_PREP_YIELD3(retval,
bif_export[BIF_send_3], p, to, msg, opts);
} else {
ERTS_BIF_PREP_RET(retval, am_nosuspend);
}
break;
case SEND_YIELD_RETURN:
if (!ctx->suspend) {
ERTS_BIF_PREP_RET(retval, am_nosuspend);
break;
}
yield_return:
ERTS_BIF_PREP_YIELD_RETURN(retval, p, am_ok);
break;
case SEND_AWAIT_RESULT:
ASSERT(is_internal_ordinary_ref(ref));
ERTS_BIF_PREP_TRAP3(retval, await_port_send_result_trap, p, ref, am_nosuspend, am_ok);
break;
case SEND_BADARG:
ERTS_BIF_PREP_ERROR(retval, p, BADARG);
break;
case SEND_USER_ERROR:
ERTS_BIF_PREP_ERROR(retval, p, EXC_ERROR);
break;
case SEND_INTERNAL_ERROR:
ERTS_BIF_PREP_ERROR(retval, p, EXC_INTERNAL_ERROR);
break;
case SEND_YIELD_CONTINUE:
BUMP_ALL_REDS(p);
erts_set_gc_state(p, 0);
ERTS_BIF_PREP_TRAP1(retval, &dsend_continue_trap_export, p,
erts_dsend_export_trap_context(p, ctx));
break;
default:
erts_exit(ERTS_ABORT_EXIT, "send_3 invalid result %d\n", (int)result);
break;
}
done:
UnUseTmpHeap(sizeof(ErtsSendContext)/sizeof(Eterm), BIF_P);
return retval;
}
HIPE_WRAPPER_BIF_DISABLE_GC(send, 2)
BIF_RETTYPE send_2(BIF_ALIST_2)
{
return erl_send(BIF_P, BIF_ARG_1, BIF_ARG_2);
}
static BIF_RETTYPE dsend_continue_trap_1(BIF_ALIST_1)
{
Binary* bin = erts_magic_ref2bin(BIF_ARG_1);
ErtsSendContext* ctx = (ErtsSendContext*) ERTS_MAGIC_BIN_DATA(bin);
Sint initial_reds = (Sint) (ERTS_BIF_REDS_LEFT(BIF_P) * TERM_TO_BINARY_LOOP_FACTOR);
int result;
ASSERT(ERTS_MAGIC_BIN_DESTRUCTOR(bin) == erts_dsend_context_dtor);
ctx->dss.reds = initial_reds;
result = erts_dsig_send(&ctx->dsd, &ctx->dss);
switch (result) {
case ERTS_DSIG_SEND_OK:
erts_set_gc_state(BIF_P, 1);
BIF_RET(ctx->return_term);
break;
case ERTS_DSIG_SEND_YIELD: /*SEND_YIELD_RETURN*/
erts_set_gc_state(BIF_P, 1);
if (!ctx->suspend)
BIF_RET(am_nosuspend);
ERTS_BIF_YIELD_RETURN(BIF_P, ctx->return_term);
case ERTS_DSIG_SEND_CONTINUE: { /*SEND_YIELD_CONTINUE*/
BUMP_ALL_REDS(BIF_P);
BIF_TRAP1(&dsend_continue_trap_export, BIF_P, BIF_ARG_1);
}
default:
erts_exit(ERTS_ABORT_EXIT, "dsend_continue_trap invalid result %d\n", (int)result);
break;
}
ASSERT(! "Can not arrive here");
BIF_ERROR(BIF_P, BADARG);
}
Eterm erl_send(Process *p, Eterm to, Eterm msg)
{
Eterm retval;
Eterm ref;
Sint result;
DeclareTypedTmpHeap(ErtsSendContext, ctx, p);
ERTS_MSACC_PUSH_AND_SET_STATE_M_X(ERTS_MSACC_STATE_SEND);
UseTmpHeap(sizeof(ErtsSendContext)/sizeof(Eterm), p);
#ifdef DEBUG
ref = NIL;
#endif
ctx->suspend = !0;
ctx->dep_to_deref = NULL;
ctx->return_term = msg;
ctx->dss.reds = (Sint) (ERTS_BIF_REDS_LEFT(p) * TERM_TO_BINARY_LOOP_FACTOR);
ctx->dss.phase = ERTS_DSIG_SEND_PHASE_INIT;
result = do_send(p, to, msg, &ref, ctx);
ERTS_MSACC_POP_STATE_M_X();
if (result > 0) {
ERTS_VBUMP_REDS(p, result);
if (ERTS_IS_PROC_OUT_OF_REDS(p))
goto yield_return;
ERTS_BIF_PREP_RET(retval, msg);
goto done;
}
switch (result) {
case 0:
/* May need to yield even though we do not bump reds here... */
if (ERTS_IS_PROC_OUT_OF_REDS(p))
goto yield_return;
ERTS_BIF_PREP_RET(retval, msg);
break;
case SEND_TRAP:
ERTS_BIF_PREP_TRAP2(retval, dsend2_trap, p, to, msg);
break;
case SEND_YIELD:
ERTS_BIF_PREP_YIELD2(retval, bif_export[BIF_send_2], p, to, msg);
break;
case SEND_YIELD_RETURN:
yield_return:
ERTS_BIF_PREP_YIELD_RETURN(retval, p, msg);
break;
case SEND_AWAIT_RESULT:
ASSERT(is_internal_ordinary_ref(ref));
ERTS_BIF_PREP_TRAP3(retval,
await_port_send_result_trap, p, ref, msg, msg);
break;
case SEND_BADARG:
ERTS_BIF_PREP_ERROR(retval, p, BADARG);
break;
case SEND_USER_ERROR:
ERTS_BIF_PREP_ERROR(retval, p, EXC_ERROR);
break;
case SEND_INTERNAL_ERROR:
ERTS_BIF_PREP_ERROR(retval, p, EXC_INTERNAL_ERROR);
break;
case SEND_YIELD_CONTINUE:
BUMP_ALL_REDS(p);
erts_set_gc_state(p, 0);
ERTS_BIF_PREP_TRAP1(retval, &dsend_continue_trap_export, p,
erts_dsend_export_trap_context(p, ctx));
break;
default:
erts_exit(ERTS_ABORT_EXIT, "invalid send result %d\n", (int)result);
break;
}
done:
UnUseTmpHeap(sizeof(ErtsSendContext)/sizeof(Eterm), p);
return retval;
}
/**********************************************************************/
/*
* apply/3 is implemented as an instruction and as erlang code in the
* erlang module.
*
* There is only one reason that apply/3 is included in the BIF table:
* The error handling code in the beam emulator passes the pointer to
* this function to the error handling code if the apply instruction
* fails. The error handling use the function pointer to lookup
* erlang:apply/3 in the BIF table.
*
* This function will never be called. (It could be if init did something
* like this: apply(erlang, apply, [M, F, A]). Not recommended.)
*/
BIF_RETTYPE apply_3(BIF_ALIST_3)
{
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
/* integer to float */
/**********************************************************************/
/* returns the head of a list - this function is unecessary
and is only here to keep Robert happy (Even more, since it's OP as well) */
BIF_RETTYPE hd_1(BIF_ALIST_1)
{
if (is_not_list(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
BIF_RET(CAR(list_val(BIF_ARG_1)));
}
/**********************************************************************/
/* returns the tails of a list - same comment as above */
BIF_RETTYPE tl_1(BIF_ALIST_1)
{
if (is_not_list(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
BIF_RET(CDR(list_val(BIF_ARG_1)));
}
/**********************************************************************/
/* return the size of an I/O list */
static Eterm
accumulate(Eterm acc, Uint size)
{
if (is_non_value(acc)) {
/*
* There is no pre-existing accumulator. Allocate a
* bignum buffer with one extra word to be used if
* the bignum grows in the future.
*/
Eterm* hp = (Eterm *) erts_alloc(ERTS_ALC_T_TEMP_TERM,
(BIG_UINT_HEAP_SIZE+1) *
sizeof(Eterm));
return uint_to_big(size, hp);
} else {
Eterm* big;
int need_heap;
/*
* Add 'size' to 'acc' in place. There is always one
* extra word allocated in case the bignum grows by one word.
*/
big = big_val(acc);
need_heap = BIG_NEED_SIZE(BIG_SIZE(big));
acc = big_plus_small(acc, size, big);
if (BIG_NEED_SIZE(big_size(acc)) > need_heap) {
/*
* The extra word has been consumed. Grow the
* allocation by one word.
*/
big = (Eterm *) erts_realloc(ERTS_ALC_T_TEMP_TERM,
big_val(acc),
(need_heap+1) * sizeof(Eterm));
acc = make_big(big);
}
return acc;
}
}
static Eterm
consolidate(Process* p, Eterm acc, Uint size)
{
Eterm* hp;
if (is_non_value(acc)) {
return erts_make_integer(size, p);
} else {
Eterm* big;
Uint sz;
Eterm res;
acc = accumulate(acc, size);
big = big_val(acc);
sz = BIG_NEED_SIZE(BIG_SIZE(big));
hp = HAlloc(p, sz);
res = make_big(hp);
while (sz--) {
*hp++ = *big++;
}
erts_free(ERTS_ALC_T_TEMP_TERM, (void *) big_val(acc));
return res;
}
}
BIF_RETTYPE iolist_size_1(BIF_ALIST_1)
{
Eterm obj, hd;
Eterm* objp;
Uint size = 0;
Uint cur_size;
Uint new_size;
Eterm acc = THE_NON_VALUE;
DECLARE_ESTACK(s);
obj = BIF_ARG_1;
goto L_again;
while (!ESTACK_ISEMPTY(s)) {
obj = ESTACK_POP(s);
L_again:
if (is_list(obj)) {
L_iter_list:
objp = list_val(obj);
hd = CAR(objp);
obj = CDR(objp);
/* Head */
if (is_byte(hd)) {
size++;
if (size == 0) {
acc = accumulate(acc, (Uint) -1);
size = 1;
}
} else if (is_binary(hd) && binary_bitsize(hd) == 0) {
cur_size = binary_size(hd);
if ((new_size = size + cur_size) >= size) {
size = new_size;
} else {
acc = accumulate(acc, size);
size = cur_size;
}
} else if (is_list(hd)) {
ESTACK_PUSH(s, obj);
obj = hd;
goto L_iter_list;
} else if (is_not_nil(hd)) {
goto L_type_error;
}
/* Tail */
if (is_list(obj)) {
goto L_iter_list;
} else if (is_binary(obj) && binary_bitsize(obj) == 0) {
cur_size = binary_size(obj);
if ((new_size = size + cur_size) >= size) {
size = new_size;
} else {
acc = accumulate(acc, size);
size = cur_size;
}
} else if (is_not_nil(obj)) {
goto L_type_error;
}
} else if (is_binary(obj) && binary_bitsize(obj) == 0) {
cur_size = binary_size(obj);
if ((new_size = size + cur_size) >= size) {
size = new_size;
} else {
acc = accumulate(acc, size);
size = cur_size;
}
} else if (is_not_nil(obj)) {
goto L_type_error;
}
}
DESTROY_ESTACK(s);
BIF_RET(consolidate(BIF_P, acc, size));
L_type_error:
DESTROY_ESTACK(s);
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
/* return the N'th element of a tuple */
BIF_RETTYPE element_2(BIF_ALIST_2)
{
if (is_not_small(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
if (is_tuple(BIF_ARG_2)) {
Eterm* tuple_ptr = tuple_val(BIF_ARG_2);
Sint ix = signed_val(BIF_ARG_1);
if ((ix >= 1) && (ix <= arityval(*tuple_ptr)))
BIF_RET(tuple_ptr[ix]);
}
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
/* return the arity of a tuple */
BIF_RETTYPE tuple_size_1(BIF_ALIST_1)
{
if (is_tuple(BIF_ARG_1)) {
return make_small(arityval(*tuple_val(BIF_ARG_1)));
}
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
/* set the n'th element in a tuple */
BIF_RETTYPE setelement_3(BIF_ALIST_3)
{
Eterm* ptr;
Eterm* hp;
Eterm* resp;
Uint ix;
Uint size;
if (is_not_small(BIF_ARG_1) || is_not_tuple(BIF_ARG_2)) {
error:
BIF_ERROR(BIF_P, BADARG);
}
ptr = tuple_val(BIF_ARG_2);
ix = signed_val(BIF_ARG_1);
size = arityval(*ptr) + 1; /* include arity */
if ((ix < 1) || (ix >= size)) {
goto error;
}
hp = HAlloc(BIF_P, size);
/* copy the tuple */
resp = hp;
sys_memcpy(hp, ptr, sizeof(Eterm)*size);
resp[ix] = BIF_ARG_3;
BIF_RET(make_tuple(resp));
}
/**********************************************************************/
BIF_RETTYPE make_tuple_2(BIF_ALIST_2)
{
Sint n;
Eterm* hp;
Eterm res;
if (is_not_small(BIF_ARG_1) || (n = signed_val(BIF_ARG_1)) < 0 || n > ERTS_MAX_TUPLE_SIZE) {
BIF_ERROR(BIF_P, BADARG);
}
hp = HAlloc(BIF_P, n+1);
res = make_tuple(hp);
*hp++ = make_arityval(n);
while (n--) {
*hp++ = BIF_ARG_2;
}
BIF_RET(res);
}
BIF_RETTYPE make_tuple_3(BIF_ALIST_3)
{
Sint n;
Uint limit;
Eterm* hp;
Eterm res;
Eterm list = BIF_ARG_3;
Eterm* tup;
if (is_not_small(BIF_ARG_1) || (n = signed_val(BIF_ARG_1)) < 0 || n > ERTS_MAX_TUPLE_SIZE) {
error:
BIF_ERROR(BIF_P, BADARG);
}
limit = (Uint) n;
hp = HAlloc(BIF_P, n+1);
res = make_tuple(hp);
*hp++ = make_arityval(n);
tup = hp;
while (n--) {
*hp++ = BIF_ARG_2;
}
while(is_list(list)) {
Eterm* cons;
Eterm hd;
Eterm* tp;
Eterm index;
Uint index_val;
cons = list_val(list);
hd = CAR(cons);
list = CDR(cons);
if (is_not_tuple_arity(hd, 2)) {
goto error;
}
tp = tuple_val(hd);
if (is_not_small(index = tp[1])) {
goto error;
}
if ((index_val = unsigned_val(index) - 1) < limit) {
tup[index_val] = tp[2];
} else {
goto error;
}
}
if (is_not_nil(list)) {
goto error;
}
BIF_RET(res);
}
/**********************************************************************/
BIF_RETTYPE append_element_2(BIF_ALIST_2)
{
Eterm* ptr;
Eterm* hp;
Uint arity;
Eterm res;
if (is_not_tuple(BIF_ARG_1)) {
error:
BIF_ERROR(BIF_P, BADARG);
}
ptr = tuple_val(BIF_ARG_1);
arity = arityval(*ptr);
if (arity + 1 > ERTS_MAX_TUPLE_SIZE)
goto error;
hp = HAlloc(BIF_P, arity + 2);
res = make_tuple(hp);
*hp = make_arityval(arity+1);
while (arity--) {
*++hp = *++ptr;
}
*++hp = BIF_ARG_2;
BIF_RET(res);
}
BIF_RETTYPE insert_element_3(BIF_ALIST_3)
{
Eterm* ptr;
Eterm* hp;
Uint arity;
Eterm res;
Sint ix, c1, c2;
if (is_not_tuple(BIF_ARG_2) || is_not_small(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
ptr = tuple_val(BIF_ARG_2);
arity = arityval(*ptr);
ix = signed_val(BIF_ARG_1);
if ((ix < 1) || (ix > (arity + 1))) {
BIF_ERROR(BIF_P, BADARG);
}
hp = HAlloc(BIF_P, arity + 1 + 1);
res = make_tuple(hp);
*hp = make_arityval(arity + 1);
c1 = ix - 1;
c2 = arity - ix + 1;
while (c1--) { *++hp = *++ptr; }
*++hp = BIF_ARG_3;
while (c2--) { *++hp = *++ptr; }
BIF_RET(res);
}
BIF_RETTYPE delete_element_2(BIF_ALIST_3)
{
Eterm* ptr;
Eterm* hp;
Uint arity;
Eterm res;
Sint ix, c1, c2;
if (is_not_tuple(BIF_ARG_2) || is_not_small(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
ptr = tuple_val(BIF_ARG_2);
arity = arityval(*ptr);
ix = signed_val(BIF_ARG_1);
if ((ix < 1) || (ix > arity) || (arity == 0)) {
BIF_ERROR(BIF_P, BADARG);
}
hp = HAlloc(BIF_P, arity + 1 - 1);
res = make_tuple(hp);
*hp = make_arityval(arity - 1);
c1 = ix - 1;
c2 = arity - ix;
while (c1--) { *++hp = *++ptr; }
++ptr;
while (c2--) { *++hp = *++ptr; }
BIF_RET(res);
}
/**********************************************************************/
/* convert an atom to a list of ascii integer */
BIF_RETTYPE atom_to_list_1(BIF_ALIST_1)
{
Atom* ap;
Uint num_chars, num_built, num_eaten;
byte* err_pos;
Eterm res;
#ifdef DEBUG
int ares;
#endif
if (is_not_atom(BIF_ARG_1))
BIF_ERROR(BIF_P, BADARG);
/* read data from atom table */
ap = atom_tab(atom_val(BIF_ARG_1));
if (ap->len == 0)
BIF_RET(NIL); /* the empty atom */
#ifdef DEBUG
ares =
#endif
erts_analyze_utf8(ap->name, ap->len, &err_pos, &num_chars, NULL);
ASSERT(ares == ERTS_UTF8_OK);
res = erts_utf8_to_list(BIF_P, num_chars, ap->name, ap->len, ap->len,
&num_built, &num_eaten, NIL);
ASSERT(num_built == num_chars);
ASSERT(num_eaten == ap->len);
BIF_RET(res);
}
/**********************************************************************/
/* convert a list of ascii integers to an atom */
BIF_RETTYPE list_to_atom_1(BIF_ALIST_1)
{
Eterm res;
byte *buf = (byte *) erts_alloc(ERTS_ALC_T_TMP, MAX_ATOM_SZ_LIMIT);
Sint i = erts_unicode_list_to_buf(BIF_ARG_1, buf, MAX_ATOM_CHARACTERS);
if (i < 0) {
erts_free(ERTS_ALC_T_TMP, (void *) buf);
i = erts_list_length(BIF_ARG_1);
if (i > MAX_ATOM_CHARACTERS) {
BIF_ERROR(BIF_P, SYSTEM_LIMIT);
}
BIF_ERROR(BIF_P, BADARG);
}
res = erts_atom_put(buf, i, ERTS_ATOM_ENC_UTF8, 1);
ASSERT(is_atom(res));
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_RET(res);
}
/* conditionally convert a list of ascii integers to an atom */
BIF_RETTYPE list_to_existing_atom_1(BIF_ALIST_1)
{
byte *buf = (byte *) erts_alloc(ERTS_ALC_T_TMP, MAX_ATOM_SZ_LIMIT);
Sint i = erts_unicode_list_to_buf(BIF_ARG_1, buf, MAX_ATOM_CHARACTERS);
if (i < 0) {
error:
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_ERROR(BIF_P, BADARG);
} else {
Eterm a;
if (erts_atom_get((char *) buf, i, &a, ERTS_ATOM_ENC_UTF8)) {
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_RET(a);
} else {
goto error;
}
}
}
/**********************************************************************/
/* convert an integer to a list of ascii integers */
BIF_RETTYPE integer_to_list_1(BIF_ALIST_1)
{
Eterm* hp;
Uint need;
if (is_not_integer(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
if (is_small(BIF_ARG_1)) {
char *c;
int n;
struct Sint_buf ibuf;
c = Sint_to_buf(signed_val(BIF_ARG_1), &ibuf);
n = sys_strlen(c);
need = 2*n;
hp = HAlloc(BIF_P, need);
BIF_RET(buf_to_intlist(&hp, c, n, NIL));
}
else {
int n = big_decimal_estimate(BIF_ARG_1);
Eterm res;
Eterm* hp_end;
need = 2*n;
hp = HAlloc(BIF_P, need);
hp_end = hp + need;
res = erts_big_to_list(BIF_ARG_1, &hp);
HRelease(BIF_P,hp_end,hp);
BIF_RET(res);
}
}
/**********************************************************************/
/*
* Converts a list of ascii base10 digits to an integer fully or partially.
* Returns result and the remaining tail.
* On error returns: {error,not_a_list}, or {error, no_integer}
*/
BIF_RETTYPE string_list_to_integer_1(BIF_ALIST_1)
{
Eterm res;
Eterm tail;
Eterm *hp;
/* must be a list */
switch (erts_list_to_integer(BIF_P, BIF_ARG_1, 10, &res, &tail)) {
/* HAlloc after erts_list_to_integer as it might HAlloc itself (bignum) */
case LTI_BAD_STRUCTURE:
hp = HAlloc(BIF_P,3);
BIF_RET(TUPLE2(hp, am_error, am_not_a_list));
case LTI_NO_INTEGER:
hp = HAlloc(BIF_P,3);
BIF_RET(TUPLE2(hp, am_error, am_no_integer));
default:
hp = HAlloc(BIF_P,3);
BIF_RET(TUPLE2(hp, res, tail));
}
}
BIF_RETTYPE list_to_integer_1(BIF_ALIST_1)
{
/* Using erts_list_to_integer is about twice as fast as using
erts_chars_to_integer because we do not have to copy the
entire list */
Eterm res;
Eterm dummy;
/* must be a list */
if (erts_list_to_integer(BIF_P, BIF_ARG_1, 10,
&res, &dummy) != LTI_ALL_INTEGER) {
BIF_ERROR(BIF_P,BADARG);
}
BIF_RET(res);
}
BIF_RETTYPE list_to_integer_2(BIF_ALIST_2)
{
/* Bif implementation is about 50% faster than pure erlang,
and since we have erts_chars_to_integer now it is simpler
as well. This could be optmized further if we did not have to
copy the list to buf. */
Sint i;
Eterm res, dummy;
int base;
i = erts_list_length(BIF_ARG_1);
if (i < 0)
BIF_ERROR(BIF_P, BADARG);
base = signed_val(BIF_ARG_2);
if (base < 2 || base > 36)
BIF_ERROR(BIF_P, BADARG);
if (erts_list_to_integer(BIF_P, BIF_ARG_1, base,
&res, &dummy) != LTI_ALL_INTEGER) {
BIF_ERROR(BIF_P,BADARG);
}
BIF_RET(res);
}
/**********************************************************************/
static int do_float_to_charbuf(Process *p, Eterm efloat, Eterm list,
char *fbuf, int sizeof_fbuf) {
const static int arity_two = make_arityval(2);
int decimals = SYS_DEFAULT_FLOAT_DECIMALS;
int compact = 0;
enum fmt_type_ {
FMT_LEGACY,
FMT_FIXED,
FMT_SCIENTIFIC
} fmt_type = FMT_LEGACY;
Eterm arg;
FloatDef f;
/* check the arguments */
if (is_not_float(efloat))
goto badarg;
for(; is_list(list); list = CDR(list_val(list))) {
arg = CAR(list_val(list));
if (arg == am_compact) {
compact = 1;
continue;
} else if (is_tuple(arg)) {
Eterm* tp = tuple_val(arg);
if (*tp == arity_two && is_small(tp[2])) {
decimals = signed_val(tp[2]);
switch (tp[1]) {
case am_decimals:
fmt_type = FMT_FIXED;
continue;
case am_scientific:
fmt_type = FMT_SCIENTIFIC;
continue;
}
}
}
goto badarg;
}
if (is_not_nil(list)) {
goto badarg;
}
GET_DOUBLE(efloat, f);
if (fmt_type == FMT_FIXED) {
return sys_double_to_chars_fast(f.fd, fbuf, sizeof_fbuf,
decimals, compact);
} else {
return sys_double_to_chars_ext(f.fd, fbuf, sizeof_fbuf, decimals);
}
badarg:
return -1;
}
/* convert a float to a list of ascii characters */
static BIF_RETTYPE do_float_to_list(Process *BIF_P, Eterm arg, Eterm opts) {
int used;
Eterm* hp;
char fbuf[256];
if ((used = do_float_to_charbuf(BIF_P,arg,opts,fbuf,sizeof(fbuf))) <= 0) {
BIF_ERROR(BIF_P, BADARG);
}
hp = HAlloc(BIF_P, (Uint)used*2);
BIF_RET(buf_to_intlist(&hp, fbuf, (Uint)used, NIL));
}
BIF_RETTYPE float_to_list_1(BIF_ALIST_1)
{
return do_float_to_list(BIF_P,BIF_ARG_1,NIL);
}
BIF_RETTYPE float_to_list_2(BIF_ALIST_2)
{
return do_float_to_list(BIF_P,BIF_ARG_1,BIF_ARG_2);
}
/* convert a float to a binary of ascii characters */
static BIF_RETTYPE do_float_to_binary(Process *BIF_P, Eterm arg, Eterm opts) {
int used;
char fbuf[256];
if ((used = do_float_to_charbuf(BIF_P,arg,opts,fbuf,sizeof(fbuf))) <= 0) {
BIF_ERROR(BIF_P, BADARG);
}
BIF_RET(new_binary(BIF_P, (byte*)fbuf, (Uint)used));
}
BIF_RETTYPE float_to_binary_1(BIF_ALIST_1)
{
return do_float_to_binary(BIF_P,BIF_ARG_1,NIL);
}
BIF_RETTYPE float_to_binary_2(BIF_ALIST_2)
{
return do_float_to_binary(BIF_P,BIF_ARG_1,BIF_ARG_2);
}
/**********************************************************************/
/* convert a list of ascii integer values e's +'s and -'s to a float */
#define SIGN 0
#define INT 1
#define FRAC 2
#define EXP_SIGN 3
#define EXP0 4
#define EXP1 5
#define END 6
#define IS_DOT(x) (unsigned_val((x)) == '.' || unsigned_val((x)) == ',')
#define IS_E(x) (unsigned_val((x)) == 'e' || unsigned_val((x)) == 'E')
#define IS_DIGIT(x) (unsigned_val((x)) >= '0' && unsigned_val((x)) <= '9')
#define SAVE_E(xi,xim,xl,xlm) ((xim)=(xi), (xlm)=(xl))
#define LOAD_E(xi,xim,xl,xlm) ((xi)=(xim), (xl)=(xlm))
#define STRING_TO_FLOAT_BUF_INC_SZ (128)
BIF_RETTYPE string_list_to_float_1(BIF_ALIST_1)
{
Eterm orig = BIF_ARG_1;
Eterm list = orig;
Eterm list_mem = list;
int i = 0;
int i_mem = 0;
Eterm* hp;
Eterm error_res = NIL;
int part = SIGN; /* expect a + or - (or a digit) first */
FloatDef f;
Eterm tup;
byte *buf = NULL;
Uint bufsz = STRING_TO_FLOAT_BUF_INC_SZ;
/* check it's a valid list to start with */
if (is_nil(list)) {
error_res = am_no_float;
error:
if (buf)
erts_free(ERTS_ALC_T_TMP, (void *) buf);
hp = HAlloc(BIF_P, 3);
BIF_RET(TUPLE2(hp, am_error, error_res));
}
if (is_not_list(list)) {
error_res = am_not_a_list;
goto error;
}
buf = (byte *) erts_alloc(ERTS_ALC_T_TMP, bufsz);
/*
The float might start with a SIGN (+ | -). It must contain an integer
part, INT, followed by a delimiter (. | ,) and a fractional, FRAC,
part. The float might also contain an exponent. If e or E indicates
this we will look for a possible EXP_SIGN (+ | -) followed by the
exponential number, EXP. (EXP0 is the first digit and EXP1 the rest).
When we encounter an expected e or E, we can't tell if it's part of
the float or the rest of the string. We save the current position
with SAVE_E. If we later find out it was not part of the float, we
restore the position (end of the float) with LOAD_E.
*/
while(1) {
if (is_not_small(CAR(list_val(list))))
goto back_to_e;
if (CAR(list_val(list)) == make_small('-')) {
switch (part) {
case SIGN: /* expect integer part next */
part = INT;
break;
case EXP_SIGN: /* expect first digit in exp */
part = EXP0;
break;
case EXP0: /* example: "2.3e--" */
LOAD_E(i, i_mem, list, list_mem);
default: /* unexpected - done */
part = END;
}
} else if (CAR(list_val(list)) == make_small('+')) {
switch (part) {
case SIGN: /* expect integer part next */
part = INT;
goto skip;
case EXP_SIGN: /* expect first digit in exp */
part = EXP0;
break;
case EXP0: /* example: "2.3e++" */
LOAD_E(i, i_mem, list, list_mem);
default: /* unexpected - done */
part = END;
}
} else if (IS_DOT(CAR(list_val(list)))) { /* . or , */
switch (part) {
case INT: /* expect fractional part next */
part = FRAC;
break;
case EXP_SIGN: /* example: "2.3e." */
LOAD_E(i, i_mem, list, list_mem);
case EXP0: /* example: "2.3e+." */
LOAD_E(i, i_mem, list, list_mem);
default: /* unexpected - done */
part = END;
}
} else if (IS_E(CAR(list_val(list)))) { /* e or E */
switch (part) {
case FRAC: /* expect a + or - (or a digit) next */
/*
remember the position of e in case we find out later
that it was not part of the float, e.g. "2.3eh?"
*/
SAVE_E(i, i_mem, list, list_mem);
part = EXP_SIGN;
break;
case EXP0: /* example: "2.3e+e" */
case EXP_SIGN: /* example: "2.3ee" */
LOAD_E(i, i_mem, list, list_mem);
case INT: /* would like this to be ok, example "2e2",
but it's not compatible with list_to_float */
default: /* unexpected - done */
part = END;
}
} else if (IS_DIGIT(CAR(list_val(list)))) { /* digit */
switch (part) {
case SIGN: /* got initial digit in integer part */
part = INT; /* expect more digits to follow */
break;
case EXP_SIGN: /* expect exponential part */
case EXP0: /* expect rest of exponential */
part = EXP1;
break;
}
} else /* character not part of float - done */
goto back_to_e;
if (part == END) {
if (i < 3) { /* we require a fractional part */
error_res = am_no_float;
goto error;
}
break;
}
buf[i++] = unsigned_val(CAR(list_val(list)));
if (i == bufsz - 1)
buf = (byte *) erts_realloc(ERTS_ALC_T_TMP,
(void *) buf,
bufsz += STRING_TO_FLOAT_BUF_INC_SZ);
skip:
list = CDR(list_val(list)); /* next element */
if (is_nil(list))
goto back_to_e;
if (is_not_list(list)) {
back_to_e:
if (part == EXP_SIGN || part == EXP0) {
LOAD_E(i, i_mem, list, list_mem);
}
break;
}
}
if (i == 0) { /* no float first in list */
error_res = am_no_float;
goto error;
}
buf[i] = '\0'; /* null terminal */
ASSERT(bufsz >= i + 1);
if (sys_chars_to_double((char*) buf, &f.fd) != 0) {
error_res = am_no_float;
goto error;
}
hp = HAlloc(BIF_P, FLOAT_SIZE_OBJECT + 3);
tup = TUPLE2(hp+FLOAT_SIZE_OBJECT, make_float(hp), list);
PUT_DOUBLE(f, hp);
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_RET(tup);
}
static BIF_RETTYPE do_charbuf_to_float(Process *BIF_P,char *buf) {
FloatDef f;
Eterm res;
Eterm* hp;
if (sys_chars_to_double(buf, &f.fd) != 0)
BIF_ERROR(BIF_P, BADARG);
hp = HAlloc(BIF_P, FLOAT_SIZE_OBJECT);
res = make_float(hp);
PUT_DOUBLE(f, hp);
BIF_RET(res);
}
BIF_RETTYPE list_to_float_1(BIF_ALIST_1)
{
Sint i;
Eterm res;
char *buf = NULL;
i = erts_list_length(BIF_ARG_1);
if (i < 0)
BIF_ERROR(BIF_P, BADARG);
buf = (char *) erts_alloc(ERTS_ALC_T_TMP, i + 1);
if (intlist_to_buf(BIF_ARG_1, buf, i) < 0)
goto list_to_float_1_error;
buf[i] = '\0'; /* null terminal */
if ((res = do_charbuf_to_float(BIF_P,buf)) == THE_NON_VALUE)
goto list_to_float_1_error;
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_RET(res);
list_to_float_1_error:
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_ERROR(BIF_P, BADARG);
}
BIF_RETTYPE binary_to_float_1(BIF_ALIST_1)
{
Eterm res;
Eterm binary = BIF_ARG_1;
Sint size;
byte* bytes, *buf;
Eterm* real_bin;
Uint offs = 0;
Uint bit_offs = 0;
if (is_not_binary(binary) || (size = binary_size(binary)) == 0)
BIF_ERROR(BIF_P, BADARG);
/*
* Unfortunately we have to copy the binary because we have to insert
* the '\0' at the end of the binary for strtod to work
* (there is no nstrtod :( )
*/
buf = erts_alloc(ERTS_ALC_T_TMP, size + 1);
real_bin = binary_val(binary);
if (*real_bin == HEADER_SUB_BIN) {
ErlSubBin* sb = (ErlSubBin *) real_bin;
if (sb->bitsize) {
goto binary_to_float_1_error;
}
offs = sb->offs;
bit_offs = sb->bitoffs;
real_bin = binary_val(sb->orig);
}
if (*real_bin == HEADER_PROC_BIN) {
bytes = ((ProcBin *) real_bin)->bytes + offs;
} else {
bytes = (byte *)(&(((ErlHeapBin *) real_bin)->data)) + offs;
}
if (bit_offs)
erts_copy_bits(bytes, bit_offs, 1, buf, 0, 1, size*8);
else
memcpy(buf, bytes, size);
buf[size] = '\0';
if ((res = do_charbuf_to_float(BIF_P,(char*)buf)) == THE_NON_VALUE)
goto binary_to_float_1_error;
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_RET(res);
binary_to_float_1_error:
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
/* convert a tuple to a list */
BIF_RETTYPE tuple_to_list_1(BIF_ALIST_1)
{
Uint n;
Eterm *tupleptr;
Eterm list = NIL;
Eterm* hp;
if (is_not_tuple(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
tupleptr = tuple_val(BIF_ARG_1);
n = arityval(*tupleptr);
hp = HAlloc(BIF_P, 2 * n);
tupleptr++;
while(n--) {
list = CONS(hp, tupleptr[n], list);
hp += 2;
}
BIF_RET(list);
}
/**********************************************************************/
/* convert a list to a tuple */
BIF_RETTYPE list_to_tuple_1(BIF_ALIST_1)
{
Eterm list = BIF_ARG_1;
Eterm* cons;
Eterm res;
Eterm* hp;
Sint len;
if ((len = erts_list_length(list)) < 0 || len > ERTS_MAX_TUPLE_SIZE) {
BIF_ERROR(BIF_P, BADARG);
}
hp = HAlloc(BIF_P, len+1);
res = make_tuple(hp);
*hp++ = make_arityval(len);
while(is_list(list)) {
cons = list_val(list);
*hp++ = CAR(cons);
list = CDR(cons);
}
BIF_RET(res);
}
/**********************************************************************/
/* return the pid of our own process, in most cases this has been replaced by
a machine instruction */
BIF_RETTYPE self_0(BIF_ALIST_0)
{
BIF_RET(BIF_P->common.id);
}
/**********************************************************************/
/* return the time of day */
BIF_RETTYPE time_0(BIF_ALIST_0)
{
int hour, minute, second;
Eterm* hp;
get_time(&hour, &minute, &second);
hp = HAlloc(BIF_P, 4); /* {hour, minute, second} + arity */
BIF_RET(TUPLE3(hp, make_small(hour), make_small(minute),
make_small(second)));
}
/**********************************************************************/
/* return the date */
BIF_RETTYPE date_0(BIF_ALIST_0)
{
int year, month, day;
Eterm* hp;
get_date(&year, &month, &day);
hp = HAlloc(BIF_P, 4); /* {year, month, day} + arity */
BIF_RET(TUPLE3(hp, make_small(year), make_small(month), make_small(day)));
}
/**********************************************************************/
/* return the universal time */
BIF_RETTYPE universaltime_0(BIF_ALIST_0)
{
int year, month, day;
int hour, minute, second;
Eterm res1, res2;
Eterm* hp;
/* read the clock */
get_universaltime(&year, &month, &day, &hour, &minute, &second);
hp = HAlloc(BIF_P, 4+4+3);
/* and return the tuple */
res1 = TUPLE3(hp,make_small(year),make_small(month),make_small(day));
hp += 4;
res2 = TUPLE3(hp,make_small(hour),make_small(minute),make_small(second));
hp += 4;
BIF_RET(TUPLE2(hp, res1, res2));
}
/**********************************************************************/
/* return the universal time */
BIF_RETTYPE localtime_0(BIF_ALIST_0)
{
int year, month, day;
int hour, minute, second;
Eterm res1, res2;
Eterm* hp;
/* read the clock */
get_localtime(&year, &month, &day, &hour, &minute, &second);
hp = HAlloc(BIF_P, 4+4+3);
/* and return the tuple */
res1 = TUPLE3(hp,make_small(year),make_small(month),make_small(day));
hp += 4;
res2 = TUPLE3(hp,make_small(hour),make_small(minute),make_small(second));
hp += 4;
BIF_RET(TUPLE2(hp, res1, res2));
}
/**********************************************************************/
/* type check and extract components from a tuple on form: {{Y,M,D},{H,M,S}} */
static int
time_to_parts(Eterm date, Sint* year, Sint* month, Sint* day,
Sint* hour, Sint* minute, Sint* second)
{
Eterm* t1;
Eterm* t2;
if (is_not_tuple(date)) {
return 0;
}
t1 = tuple_val(date);
if (arityval(t1[0]) !=2 ||
is_not_tuple(t1[1]) || is_not_tuple(t1[2]))
return 0;
t2 = tuple_val(t1[1]);
t1 = tuple_val(t1[2]);
if (arityval(t2[0]) != 3 ||
is_not_small(t2[1]) || is_not_small(t2[2]) || is_not_small(t2[3]))
return 0;
*year = signed_val(t2[1]);
*month = signed_val(t2[2]);
*day = signed_val(t2[3]);
if (arityval(t1[0]) != 3 ||
is_not_small(t1[1]) || is_not_small(t1[2]) || is_not_small(t1[3]))
return 0;
*hour = signed_val(t1[1]);
*minute = signed_val(t1[2]);
*second = signed_val(t1[3]);
return 1;
}
/* return the universal time */
BIF_RETTYPE
localtime_to_universaltime_2(BIF_ALIST_2)
{
Process *p = BIF_P;
Eterm localtime = BIF_ARG_1;
Eterm dst = BIF_ARG_2;
Sint year, month, day;
Sint hour, minute, second;
int isdst;
Eterm res1, res2;
Eterm* hp;
if (dst == am_true) isdst = 1;
else if (dst == am_false) isdst = 0;
else if (dst == am_undefined) isdst = -1;
else goto error;
if (!time_to_parts(localtime, &year, &month, &day,
&hour, &minute, &second)) goto error;
if (!local_to_univ(&year, &month, &day,
&hour, &minute, &second, isdst)) goto error;
hp = HAlloc(p, 4+4+3);
res1 = TUPLE3(hp,make_small(year),make_small(month),
make_small(day));
hp += 4;
res2 = TUPLE3(hp,make_small(hour),make_small(minute),
make_small(second));
hp += 4;
BIF_RET(TUPLE2(hp, res1, res2));
error:
BIF_ERROR(p, BADARG);
}
/**********************************************************************/
/* return the universal time */
BIF_RETTYPE universaltime_to_localtime_1(BIF_ALIST_1)
{
Sint year, month, day;
Sint hour, minute, second;
Eterm res1, res2;
Eterm* hp;
if (!time_to_parts(BIF_ARG_1, &year, &month, &day,
&hour, &minute, &second))
BIF_ERROR(BIF_P, BADARG);
if (!univ_to_local(&year, &month, &day,
&hour, &minute, &second))
BIF_ERROR(BIF_P, BADARG);
hp = HAlloc(BIF_P, 4+4+3);
res1 = TUPLE3(hp,make_small(year),make_small(month),
make_small(day));
hp += 4;
res2 = TUPLE3(hp,make_small(hour),make_small(minute),
make_small(second));
hp += 4;
BIF_RET(TUPLE2(hp, res1, res2));
}
/* convert calendar:universaltime_to_seconds/1 */
BIF_RETTYPE universaltime_to_posixtime_1(BIF_ALIST_1)
{
Sint year, month, day;
Sint hour, minute, second;
Sint64 seconds = 0;
Eterm *hp;
Uint hsz = 0;
if (!time_to_parts(BIF_ARG_1, &year, &month, &day,
&hour, &minute, &second))
BIF_ERROR(BIF_P, BADARG);
if (!univ_to_seconds(year, month, day, hour, minute, second, &seconds)) {
BIF_ERROR(BIF_P, BADARG);
}
erts_bld_sint64(NULL, &hsz, seconds);
hp = HAlloc(BIF_P, hsz);
BIF_RET(erts_bld_sint64(&hp, NULL, seconds));
}
/* convert calendar:seconds_to_universaltime/1 */
BIF_RETTYPE posixtime_to_universaltime_1(BIF_ALIST_1)
{
Sint year, month, day;
Sint hour, minute, second;
Eterm res1, res2;
Eterm* hp;
Sint64 time = 0;
if (!term_to_Sint64(BIF_ARG_1, &time)) {
BIF_ERROR(BIF_P, BADARG);
}
if (!seconds_to_univ(time, &year, &month, &day,
&hour, &minute, &second)) {
BIF_ERROR(BIF_P, BADARG);
}
hp = HAlloc(BIF_P, 4+4+3);
res1 = TUPLE3(hp,make_small(year),make_small(month),
make_small(day));
hp += 4;
res2 = TUPLE3(hp,make_small(hour),make_small(minute),
make_small(second));
hp += 4;
BIF_RET(TUPLE2(hp, res1, res2));
}
/**********************************************************************/
/* return a timestamp */
BIF_RETTYPE now_0(BIF_ALIST_0)
{
Uint megasec, sec, microsec;
Eterm* hp;
get_now(&megasec, &sec, µsec);
hp = HAlloc(BIF_P, 4);
BIF_RET(TUPLE3(hp, make_small(megasec), make_small(sec),
make_small(microsec)));
}
/**********************************************************************/
/*
* Pass atom 'minor' for relaxed generational GC run. This is only
* recommendation, major run may still be chosen by VM.
* Pass atom 'major' for default behaviour - major GC run (fullsweep)
*/
BIF_RETTYPE
erts_internal_garbage_collect_1(BIF_ALIST_1)
{
switch (BIF_ARG_1) {
case am_minor: break;
case am_major: FLAGS(BIF_P) |= F_NEED_FULLSWEEP; break;
default: BIF_ERROR(BIF_P, BADARG);
}
erts_garbage_collect(BIF_P, 0, NULL, 0);
return am_true;
}
/**********************************************************************/
/*
* The erlang:processes/0 BIF.
*/
BIF_RETTYPE processes_0(BIF_ALIST_0)
{
return erts_ptab_list(BIF_P, &erts_proc);
}
/**********************************************************************/
/*
* The erlang:ports/0 BIF.
*/
BIF_RETTYPE ports_0(BIF_ALIST_0)
{
return erts_ptab_list(BIF_P, &erts_port);
}
/**********************************************************************/
BIF_RETTYPE throw_1(BIF_ALIST_1)
{
BIF_P->fvalue = BIF_ARG_1;
BIF_ERROR(BIF_P, EXC_THROWN);
}
/**********************************************************************/
/*
* Non-standard, undocumented, dirty BIF, meant for debugging.
*
*/
BIF_RETTYPE display_1(BIF_ALIST_1)
{
erts_printf("%.*T\n", INT_MAX, BIF_ARG_1);
BIF_RET(am_true);
}
/*
* erts_debug:display/1 is for debugging erlang:display/1
*/
BIF_RETTYPE erts_debug_display_1(BIF_ALIST_1)
{
int pres;
Eterm res;
Eterm *hp;
erts_dsprintf_buf_t *dsbufp = erts_create_tmp_dsbuf(64);
pres = erts_dsprintf(dsbufp, "%.*T\n", INT_MAX, BIF_ARG_1);
if (pres < 0)
erts_exit(ERTS_ERROR_EXIT, "Failed to convert term to string: %d (%s)\n",
-pres, erl_errno_id(-pres));
hp = HAlloc(BIF_P, 2*dsbufp->str_len); /* we need length * 2 heap words */
res = buf_to_intlist(&hp, dsbufp->str, dsbufp->str_len, NIL);
erts_printf("%s", dsbufp->str);
erts_destroy_tmp_dsbuf(dsbufp);
BIF_RET(res);
}
BIF_RETTYPE display_string_1(BIF_ALIST_1)
{
Process* p = BIF_P;
Eterm string = BIF_ARG_1;
Sint len = is_string(string);
char *str;
if (len <= 0) {
BIF_ERROR(p, BADARG);
}
str = (char *) erts_alloc(ERTS_ALC_T_TMP, sizeof(char)*(len + 1));
if (intlist_to_buf(string, str, len) != len)
erts_exit(ERTS_ERROR_EXIT, "%s:%d: Internal error\n", __FILE__, __LINE__);
str[len] = '\0';
erts_fprintf(stderr, "%s", str);
erts_free(ERTS_ALC_T_TMP, (void *) str);
BIF_RET(am_true);
}
BIF_RETTYPE display_nl_0(BIF_ALIST_0)
{
erts_fprintf(stderr, "\n");
BIF_RET(am_true);
}
/**********************************************************************/
#define HALT_MSG_SIZE 200
static char halt_msg[HALT_MSG_SIZE+1];
/* stop the system with exit code and flags */
BIF_RETTYPE halt_2(BIF_ALIST_2)
{
Uint code;
Eterm optlist = BIF_ARG_2;
int flush = 1;
for (optlist = BIF_ARG_2;
is_list(optlist);
optlist = CDR(list_val(optlist))) {
Eterm *tp, opt = CAR(list_val(optlist));
if (is_not_tuple(opt))
goto error;
tp = tuple_val(opt);
if (tp[0] != make_arityval(2))
goto error;
if (tp[1] == am_flush) {
if (tp[2] == am_true)
flush = 1;
else if (tp[2] == am_false)
flush = 0;
else
goto error;
}
else
goto error;
}
if (is_not_nil(optlist))
goto error;
if (term_to_Uint_mask(BIF_ARG_1, &code)) {
int pos_int_code = (int) (code & INT_MAX);
VERBOSE(DEBUG_SYSTEM,
("System halted by BIF halt(%T, %T)\n", BIF_ARG_1, BIF_ARG_2));
if (flush) {
erts_halt(pos_int_code);
ERTS_BIF_YIELD2(bif_export[BIF_halt_2], BIF_P, am_undefined, am_undefined);
}
else {
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_exit(pos_int_code, "");
}
}
else if (ERTS_IS_ATOM_STR("abort", BIF_ARG_1)) {
VERBOSE(DEBUG_SYSTEM,
("System halted by BIF halt(%T, %T)\n", BIF_ARG_1, BIF_ARG_2));
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_exit(ERTS_ABORT_EXIT, "");
}
else if (is_string(BIF_ARG_1) || BIF_ARG_1 == NIL) {
Sint i;
if ((i = intlist_to_buf(BIF_ARG_1, halt_msg, HALT_MSG_SIZE)) == -1) {
goto error;
}
if (i == -2) /* truncated string */
i = HALT_MSG_SIZE;
ASSERT(i >= 0 && i <= HALT_MSG_SIZE);
halt_msg[i] = '\0';
VERBOSE(DEBUG_SYSTEM,
("System halted by BIF halt(%T, %T)\n", BIF_ARG_1, BIF_ARG_2));
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_exit(ERTS_DUMP_EXIT, "%s\n", halt_msg);
}
else
goto error;
return NIL; /* Pedantic (lint does not know about erts_exit) */
error:
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
BIF_RETTYPE function_exported_3(BIF_ALIST_3)
{
int arity;
if (is_not_atom(BIF_ARG_1) ||
is_not_atom(BIF_ARG_2) ||
is_not_small(BIF_ARG_3)) {
BIF_ERROR(BIF_P, BADARG);
}
arity = signed_val(BIF_ARG_3);
if (erts_find_function(BIF_ARG_1, BIF_ARG_2, arity,
erts_active_code_ix()) != NULL ||
erts_is_builtin(BIF_ARG_1, BIF_ARG_2, arity)) {
BIF_RET(am_true);
}
BIF_RET(am_false);
}
/**********************************************************************/
BIF_RETTYPE is_builtin_3(BIF_ALIST_3)
{
Process* p = BIF_P;
Eterm Mod = BIF_ARG_1;
Eterm Name = BIF_ARG_2;
Eterm Arity = BIF_ARG_3;
if (is_not_atom(Mod) || is_not_atom(Name) || is_not_small(Arity)) {
BIF_ERROR(p, BADARG);
}
BIF_RET(erts_is_builtin(Mod, Name, signed_val(Arity)) ?
am_true : am_false);
}
/**********************************************************************/
/* NOTE: Cannot be used in all *_to_list() bifs. erts_dsprintf() prints
* some terms on other formats than what is desired as results
* from *_to_list() bifs.
*/
static Eterm
term2list_dsprintf(Process *p, Eterm term)
{
int pres;
Eterm res;
Eterm *hp;
erts_dsprintf_buf_t *dsbufp = erts_create_tmp_dsbuf(64);
pres = erts_dsprintf(dsbufp, "%T", term);
if (pres < 0)
erts_exit(ERTS_ERROR_EXIT, "Failed to convert term to list: %d (%s)\n",
-pres, erl_errno_id(-pres));
hp = HAlloc(p, 2*dsbufp->str_len); /* we need length * 2 heap words */
res = buf_to_intlist(&hp, dsbufp->str, dsbufp->str_len, NIL);
erts_destroy_tmp_dsbuf(dsbufp);
return res;
}
BIF_RETTYPE ref_to_list_1(BIF_ALIST_1)
{
if (is_not_ref(BIF_ARG_1))
BIF_ERROR(BIF_P, BADARG);
erts_magic_ref_save_bin(BIF_ARG_1);
BIF_RET(term2list_dsprintf(BIF_P, BIF_ARG_1));
}
BIF_RETTYPE make_fun_3(BIF_ALIST_3)
{
Eterm* hp;
Sint arity;
if (is_not_atom(BIF_ARG_1) || is_not_atom(BIF_ARG_2) || is_not_small(BIF_ARG_3)) {
error:
BIF_ERROR(BIF_P, BADARG);
}
arity = signed_val(BIF_ARG_3);
if (arity < 0) {
goto error;
}
hp = HAlloc(BIF_P, 2);
hp[0] = HEADER_EXPORT;
hp[1] = (Eterm) erts_export_get_or_make_stub(BIF_ARG_1, BIF_ARG_2, (Uint) arity);
BIF_RET(make_export(hp));
}
BIF_RETTYPE fun_to_list_1(BIF_ALIST_1)
{
Process* p = BIF_P;
Eterm fun = BIF_ARG_1;
if (is_not_any_fun(fun))
BIF_ERROR(p, BADARG);
BIF_RET(term2list_dsprintf(p, fun));
}
/**********************************************************************/
/* convert a pid to an erlang list (for the linked cons cells) of the form
<node.number.serial> to a PID
*/
BIF_RETTYPE pid_to_list_1(BIF_ALIST_1)
{
if (is_not_pid(BIF_ARG_1))
BIF_ERROR(BIF_P, BADARG);
BIF_RET(term2list_dsprintf(BIF_P, BIF_ARG_1));
}
BIF_RETTYPE port_to_list_1(BIF_ALIST_1)
{
if (is_not_port(BIF_ARG_1))
BIF_ERROR(BIF_P, BADARG);
BIF_RET(term2list_dsprintf(BIF_P, BIF_ARG_1));
}
/**********************************************************************/
/* convert a list of ascii characeters of the form
<node.number.serial> to a PID
*/
BIF_RETTYPE list_to_pid_1(BIF_ALIST_1)
{
Uint a = 0, b = 0, c = 0;
char* cp;
Sint i;
DistEntry *dep = NULL;
char *buf = (char *) erts_alloc(ERTS_ALC_T_TMP, 65);
/*
* Max 'Uint64' has 20 decimal digits. If X, Y, Z in <X.Y.Z>
* are 'Uint64's. Max chars are 1 + 20 + 1 + 20 + 1 + 20 + 1 = 64,
* i.e, if the input list is longer than 64 it does not represent
* a pid.
*/
/* walk down the list and create a C string */
if ((i = intlist_to_buf(BIF_ARG_1, buf, 64)) < 0)
goto bad;
buf[i] = '\0'; /* null terminal */
cp = buf;
if (*cp++ != '<') goto bad;
if (*cp < '0' || *cp > '9') goto bad;
while(*cp >= '0' && *cp <= '9') { a = 10*a + (*cp - '0'); cp++; }
if (*cp++ != '.') goto bad;
if (*cp < '0' || *cp > '9') goto bad;
while(*cp >= '0' && *cp <= '9') { b = 10*b + (*cp - '0'); cp++; }
if (*cp++ != '.') goto bad;
if (*cp < '0' || *cp > '9') goto bad;
while(*cp >= '0' && *cp <= '9') { c = 10*c + (*cp - '0'); cp++; }
if (*cp++ != '>') goto bad;
if (*cp != '\0') goto bad;
erts_free(ERTS_ALC_T_TMP, (void *) buf);
buf = NULL;
/* <a.b.c> a = node, b = process number, c = serial */
dep = erts_channel_no_to_dist_entry(a);
if (!dep)
goto bad;
if (c > ERTS_MAX_PID_SERIAL || b > ERTS_MAX_PID_NUMBER)
goto bad;
if(dep == erts_this_dist_entry) {
erts_deref_dist_entry(dep);
BIF_RET(make_internal_pid(make_pid_data(c, b)));
}
else {
ExternalThing *etp;
ErlNode *enp;
if (is_nil(dep->cid))
goto bad;
enp = erts_find_or_insert_node(dep->sysname, dep->creation);
ASSERT(enp != erts_this_node);
etp = (ExternalThing *) HAlloc(BIF_P, EXTERNAL_THING_HEAD_SIZE + 1);
etp->header = make_external_pid_header(1);
etp->next = MSO(BIF_P).first;
etp->node = enp;
etp->data.ui[0] = make_pid_data(c, b);
MSO(BIF_P).first = (struct erl_off_heap_header*) etp;
erts_deref_dist_entry(dep);
BIF_RET(make_external_pid(etp));
}
bad:
if (dep)
erts_deref_dist_entry(dep);
if (buf)
erts_free(ERTS_ALC_T_TMP, (void *) buf);
BIF_ERROR(BIF_P, BADARG);
}
BIF_RETTYPE list_to_port_1(BIF_ALIST_1)
{
/*
* A valid port identifier is on the format
* "#Port<N.P>" where N is node and P is
* the port id. Both N and P are of type Uint32.
*/
Uint32 n, p;
char* cp;
int i;
DistEntry *dep = NULL;
char buf[6 /* #Port< */
+ (2)*(10 + 1) /* N.P> */
+ 1 /* \0 */];
/* walk down the list and create a C string */
if ((i = intlist_to_buf(BIF_ARG_1, buf, sizeof(buf)-1)) < 0)
goto bad;
buf[i] = '\0'; /* null terminal */
cp = &buf[0];
if (strncmp("#Port<", cp, 6) != 0)
goto bad;
cp += 6; /* strlen("#Port<") */
if (sscanf(cp, "%u.%u>", &n, &p) < 2)
goto bad;
if (p > ERTS_MAX_PORT_NUMBER)
goto bad;
dep = erts_channel_no_to_dist_entry(n);
if (!dep)
goto bad;
if(dep == erts_this_dist_entry) {
erts_deref_dist_entry(dep);
BIF_RET(make_internal_port(p));
}
else {
ExternalThing *etp;
ErlNode *enp;
if (is_nil(dep->cid))
goto bad;
enp = erts_find_or_insert_node(dep->sysname, dep->creation);
ASSERT(enp != erts_this_node);
etp = (ExternalThing *) HAlloc(BIF_P, EXTERNAL_THING_HEAD_SIZE + 1);
etp->header = make_external_port_header(1);
etp->next = MSO(BIF_P).first;
etp->node = enp;
etp->data.ui[0] = p;
MSO(BIF_P).first = (struct erl_off_heap_header*) etp;
erts_deref_dist_entry(dep);
BIF_RET(make_external_port(etp));
}
bad:
if (dep)
erts_deref_dist_entry(dep);
BIF_ERROR(BIF_P, BADARG);
}
BIF_RETTYPE list_to_ref_1(BIF_ALIST_1)
{
/*
* A valid reference is on the format
* "#Ref<N.X.Y.Z>" where N, X, Y, and Z are
* 32-bit integers (i.e., max 10 characters).
*/
Eterm *hp;
Eterm res;
Uint32 refn[ERTS_MAX_REF_NUMBERS];
int n = 0;
Uint ints[1 + ERTS_MAX_REF_NUMBERS] = {0};
char* cp;
Sint i;
DistEntry *dep = NULL;
char buf[5 /* #Ref< */
+ (1 + ERTS_MAX_REF_NUMBERS)*(10 + 1) /* N.X.Y.Z> */
+ 1 /* \0 */];
/* walk down the list and create a C string */
if ((i = intlist_to_buf(BIF_ARG_1, buf, sizeof(buf)-1)) < 0)
goto bad;
buf[i] = '\0'; /* null terminal */
cp = &buf[0];
if (*cp++ != '#') goto bad;
if (*cp++ != 'R') goto bad;
if (*cp++ != 'e') goto bad;
if (*cp++ != 'f') goto bad;
if (*cp++ != '<') goto bad;
for (i = 0; i < sizeof(ints)/sizeof(Uint); i++) {
if (*cp < '0' || *cp > '9') goto bad;
while (*cp >= '0' && *cp <= '9') {
ints[i] = 10*ints[i] + (*cp - '0');
cp++;
}
n++;
if (ints[i] > ~((Uint32) 0)) goto bad;
if (*cp == '>') break;
if (*cp++ != '.') goto bad;
}
if (*cp++ != '>') goto bad;
if (*cp != '\0') goto bad;
if (n < 2) goto bad;
for (n = 0; i > 0; i--)
refn[n++] = (Uint32) ints[i];
ASSERT(n <= ERTS_MAX_REF_NUMBERS);
dep = erts_channel_no_to_dist_entry(ints[0]);
if (!dep)
goto bad;
if(dep == erts_this_dist_entry) {
ErtsMagicBinary *mb;
Uint32 sid;
if (refn[0] > MAX_REFERENCE) goto bad;
if (n != ERTS_REF_NUMBERS) goto bad;
sid = erts_get_ref_numbers_thr_id(refn);
if (sid > erts_no_schedulers) goto bad;
mb = erts_magic_ref_lookup_bin(refn);
if (mb) {
hp = HAlloc(BIF_P, ERTS_MAGIC_REF_THING_SIZE);
res = erts_mk_magic_ref(&hp, &BIF_P->off_heap,
(Binary *) mb);
}
else {
hp = HAlloc(BIF_P, ERTS_REF_THING_SIZE);
write_ref_thing(hp, refn[0], refn[1], refn[2]);
res = make_internal_ref(hp);
}
}
else {
ExternalThing *etp;
ErlNode *enp;
Uint hsz;
int j;
if (is_nil(dep->cid))
goto bad;
enp = erts_find_or_insert_node(dep->sysname, dep->creation);
ASSERT(enp != erts_this_node);
hsz = EXTERNAL_THING_HEAD_SIZE;
#if defined(ARCH_64)
hsz += n/2 + 1;
#else
hsz += n;
#endif
etp = (ExternalThing *) HAlloc(BIF_P, hsz);
etp->header = make_external_ref_header(n/2);
etp->next = BIF_P->off_heap.first;
etp->node = enp;
i = 0;
#if defined(ARCH_64)
etp->data.ui32[i] = n;
#endif
for (j = 0; j < n; j++) {
etp->data.ui32[i] = refn[j];
i++;
}
BIF_P->off_heap.first = (struct erl_off_heap_header*) etp;
res = make_external_ref(etp);
}
erts_deref_dist_entry(dep);
BIF_RET(res);
bad:
if (dep)
erts_deref_dist_entry(dep);
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
BIF_RETTYPE group_leader_0(BIF_ALIST_0)
{
BIF_RET(BIF_P->group_leader);
}
/**********************************************************************/
/* arg1 == leader, arg2 == new member */
BIF_RETTYPE group_leader_2(BIF_ALIST_2)
{
Process* new_member;
if (is_not_pid(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
if (is_external_pid(BIF_ARG_2)) {
DistEntry *dep;
int code;
ErtsDSigData dsd;
dep = external_pid_dist_entry(BIF_ARG_2);
if(dep == erts_this_dist_entry)
BIF_ERROR(BIF_P, BADARG);
code = erts_dsig_prepare(&dsd, dep, BIF_P, ERTS_DSP_NO_LOCK, 0);
switch (code) {
case ERTS_DSIG_PREP_NOT_ALIVE:
BIF_RET(am_true);
case ERTS_DSIG_PREP_NOT_CONNECTED:
BIF_TRAP2(dgroup_leader_trap, BIF_P, BIF_ARG_1, BIF_ARG_2);
case ERTS_DSIG_PREP_CONNECTED:
code = erts_dsig_send_group_leader(&dsd, BIF_ARG_1, BIF_ARG_2);
if (code == ERTS_DSIG_SEND_YIELD)
ERTS_BIF_YIELD_RETURN(BIF_P, am_true);
BIF_RET(am_true);
default:
ASSERT(! "Invalid dsig prepare result");
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
}
}
else if (is_internal_pid(BIF_ARG_2)) {
int await_x;
ErtsProcLocks locks = ERTS_PROC_LOCK_MAIN|ERTS_PROC_LOCK_STATUS;
new_member = erts_pid2proc_nropt(BIF_P, ERTS_PROC_LOCK_MAIN,
BIF_ARG_2, locks);
if (!new_member)
BIF_ERROR(BIF_P, BADARG);
if (new_member == ERTS_PROC_LOCK_BUSY)
ERTS_BIF_YIELD2(bif_export[BIF_group_leader_2], BIF_P,
BIF_ARG_1, BIF_ARG_2);
await_x = (new_member != BIF_P
&& ERTS_PROC_PENDING_EXIT(new_member));
if (!await_x) {
if (is_immed(BIF_ARG_1))
new_member->group_leader = BIF_ARG_1;
else {
locks &= ~ERTS_PROC_LOCK_STATUS;
erts_smp_proc_unlock(new_member, ERTS_PROC_LOCK_STATUS);
if (new_member == BIF_P
|| !(erts_smp_atomic32_read_nob(&new_member->state)
& ERTS_PSFLG_DIRTY_RUNNING)) {
new_member->group_leader = STORE_NC_IN_PROC(new_member,
BIF_ARG_1);
}
else {
ErlHeapFragment *bp;
Eterm *hp;
/*
* Other process executing on a dirty scheduler,
* so we are not allowed to write to its heap.
* Store in heap fragment.
*/
bp = new_message_buffer(NC_HEAP_SIZE(BIF_ARG_1));
hp = bp->mem;
new_member->group_leader = STORE_NC(&hp,
&new_member->off_heap,
BIF_ARG_1);
bp->next = new_member->mbuf;
new_member->mbuf = bp;
new_member->mbuf_sz += bp->used_size;
}
}
}
if (new_member == BIF_P)
locks &= ~ERTS_PROC_LOCK_MAIN;
if (locks)
erts_smp_proc_unlock(new_member, locks);
if (await_x) {
/* Wait for new_member to terminate; then badarg */
Eterm args[2] = {BIF_ARG_1, BIF_ARG_2};
ERTS_BIF_AWAIT_X_APPLY_TRAP(BIF_P,
BIF_ARG_2,
am_erlang,
am_group_leader,
args,
2);
}
BIF_RET(am_true);
}
else {
BIF_ERROR(BIF_P, BADARG);
}
}
BIF_RETTYPE system_flag_2(BIF_ALIST_2)
{
Sint n;
if (BIF_ARG_1 == am_multi_scheduling) {
if (BIF_ARG_2 == am_block || BIF_ARG_2 == am_unblock
|| BIF_ARG_2 == am_block_normal || BIF_ARG_2 == am_unblock_normal) {
int block = (BIF_ARG_2 == am_block
|| BIF_ARG_2 == am_block_normal);
int normal = (BIF_ARG_2 == am_block_normal
|| BIF_ARG_2 == am_unblock_normal);
switch (erts_block_multi_scheduling(BIF_P,
ERTS_PROC_LOCK_MAIN,
block,
normal,
0)) {
case ERTS_SCHDLR_SSPND_DONE_MSCHED_BLOCKED:
BIF_RET(am_blocked);
case ERTS_SCHDLR_SSPND_DONE_NMSCHED_BLOCKED:
BIF_RET(am_blocked_normal);
case ERTS_SCHDLR_SSPND_YIELD_DONE_MSCHED_BLOCKED:
ERTS_BIF_YIELD_RETURN_X(BIF_P, am_blocked,
am_multi_scheduling);
case ERTS_SCHDLR_SSPND_YIELD_DONE_NMSCHED_BLOCKED:
ERTS_BIF_YIELD_RETURN_X(BIF_P, am_blocked_normal,
am_multi_scheduling);
case ERTS_SCHDLR_SSPND_DONE:
BIF_RET(am_enabled);
case ERTS_SCHDLR_SSPND_YIELD_RESTART:
ERTS_VBUMP_ALL_REDS(BIF_P);
BIF_TRAP2(bif_export[BIF_system_flag_2],
BIF_P, BIF_ARG_1, BIF_ARG_2);
case ERTS_SCHDLR_SSPND_YIELD_DONE:
ERTS_BIF_YIELD_RETURN_X(BIF_P, am_enabled,
am_multi_scheduling);
case ERTS_SCHDLR_SSPND_EINVAL:
goto error;
default:
ASSERT(0);
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
break;
}
}
} else if (BIF_ARG_1 == am_schedulers_online) {
Sint old_no;
if (!is_small(BIF_ARG_2))
goto error;
switch (erts_set_schedulers_online(BIF_P,
ERTS_PROC_LOCK_MAIN,
signed_val(BIF_ARG_2),
&old_no, 0)) {
case ERTS_SCHDLR_SSPND_DONE:
BIF_RET(make_small(old_no));
case ERTS_SCHDLR_SSPND_YIELD_RESTART:
ERTS_VBUMP_ALL_REDS(BIF_P);
BIF_TRAP2(bif_export[BIF_system_flag_2],
BIF_P, BIF_ARG_1, BIF_ARG_2);
case ERTS_SCHDLR_SSPND_YIELD_DONE:
ERTS_BIF_YIELD_RETURN_X(BIF_P, make_small(old_no),
am_schedulers_online);
case ERTS_SCHDLR_SSPND_EINVAL:
goto error;
default:
ASSERT(0);
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
break;
}
} else if (BIF_ARG_1 == am_fullsweep_after) {
Uint16 nval;
Uint oval;
if (!is_small(BIF_ARG_2) || (n = signed_val(BIF_ARG_2)) < 0) {
goto error;
}
nval = (n > (Sint) ((Uint16) -1)) ? ((Uint16) -1) : ((Uint16) n);
oval = (Uint) erts_smp_atomic32_xchg_nob(&erts_max_gen_gcs,
(erts_aint32_t) nval);
BIF_RET(make_small(oval));
} else if (BIF_ARG_1 == am_min_heap_size) {
int oval = H_MIN_SIZE;
if (!is_small(BIF_ARG_2) || (n = signed_val(BIF_ARG_2)) < 0) {
goto error;
}
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_smp_thr_progress_block();
H_MIN_SIZE = erts_next_heap_size(n, 0);
erts_smp_thr_progress_unblock();
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_MAIN);
BIF_RET(make_small(oval));
} else if (BIF_ARG_1 == am_min_bin_vheap_size) {
int oval = BIN_VH_MIN_SIZE;
if (!is_small(BIF_ARG_2) || (n = signed_val(BIF_ARG_2)) < 0) {
goto error;
}
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_smp_thr_progress_block();
BIN_VH_MIN_SIZE = erts_next_heap_size(n, 0);
erts_smp_thr_progress_unblock();
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_MAIN);
BIF_RET(make_small(oval));
} else if (BIF_ARG_1 == am_max_heap_size) {
Eterm *hp, old_value;
Uint sz = 0, max_heap_size, max_heap_flags;
if (!erts_max_heap_size(BIF_ARG_2, &max_heap_size, &max_heap_flags))
goto error;
if (max_heap_size < H_MIN_SIZE && max_heap_size != 0)
goto error;
erts_max_heap_size_map(H_MAX_SIZE, H_MAX_FLAGS, NULL, &sz);
hp = HAlloc(BIF_P, sz);
old_value = erts_max_heap_size_map(H_MAX_SIZE, H_MAX_FLAGS, &hp, NULL);
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_smp_thr_progress_block();
H_MAX_SIZE = max_heap_size;
H_MAX_FLAGS = max_heap_flags;
erts_smp_thr_progress_unblock();
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_MAIN);
BIF_RET(old_value);
} else if (BIF_ARG_1 == am_display_items) {
int oval = display_items;
if (!is_small(BIF_ARG_2) || (n = signed_val(BIF_ARG_2)) < 0) {
goto error;
}
display_items = n < 32 ? 32 : n;
BIF_RET(make_small(oval));
} else if (BIF_ARG_1 == am_debug_flags) {
BIF_RET(am_true);
} else if (BIF_ARG_1 == am_backtrace_depth) {
int oval = erts_backtrace_depth;
if (!is_small(BIF_ARG_2) || (n = signed_val(BIF_ARG_2)) < 0) {
goto error;
}
if (n > MAX_BACKTRACE_SIZE) n = MAX_BACKTRACE_SIZE;
erts_backtrace_depth = n;
BIF_RET(make_small(oval));
} else if (BIF_ARG_1 == am_trace_control_word) {
BIF_RET(db_set_trace_control_word(BIF_P, BIF_ARG_2));
} else if (BIF_ARG_1 == am_sequential_tracer) {
ErtsTracer new_seq_tracer, old_seq_tracer;
Eterm ret;
if (BIF_ARG_2 == am_false)
new_seq_tracer = erts_tracer_nil;
else
new_seq_tracer = erts_term_to_tracer(THE_NON_VALUE, BIF_ARG_2);
if (new_seq_tracer == THE_NON_VALUE)
goto error;
old_seq_tracer = erts_set_system_seq_tracer(BIF_P,
ERTS_PROC_LOCK_MAIN,
new_seq_tracer);
ERTS_TRACER_CLEAR(&new_seq_tracer);
if (old_seq_tracer == THE_NON_VALUE)
goto error;
if (ERTS_TRACER_IS_NIL(old_seq_tracer))
BIF_RET(am_false);
ret = erts_tracer_to_term(BIF_P, old_seq_tracer);
ERTS_TRACER_CLEAR(&old_seq_tracer);
BIF_RET(ret);
} else if (BIF_ARG_1 == make_small(1)) {
int i, max;
ErtsMessage* mp;
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_smp_thr_progress_block();
max = erts_ptab_max(&erts_proc);
for (i = 0; i < max; i++) {
Process *p = erts_pix2proc(i);
if (p) {
#ifdef USE_VM_PROBES
p->seq_trace_token = (p->dt_utag != NIL) ? am_have_dt_utag : NIL;
#else
p->seq_trace_token = NIL;
#endif
p->seq_trace_clock = 0;
p->seq_trace_lastcnt = 0;
ERTS_SMP_MSGQ_MV_INQ2PRIVQ(p);
mp = p->msg.first;
while(mp != NULL) {
#ifdef USE_VM_PROBES
ERL_MESSAGE_TOKEN(mp) = (ERL_MESSAGE_DT_UTAG(mp) != NIL) ? am_have_dt_utag : NIL;
#else
ERL_MESSAGE_TOKEN(mp) = NIL;
#endif
mp = mp->next;
}
}
}
erts_smp_thr_progress_unblock();
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_MAIN);
BIF_RET(am_true);
} else if (BIF_ARG_1 == am_scheduler_wall_time) {
if (BIF_ARG_2 == am_true || BIF_ARG_2 == am_false) {
erts_aint32_t new = BIF_ARG_2 == am_true ? 1 : 0;
erts_aint32_t old = erts_smp_atomic32_xchg_nob(&sched_wall_time,
new);
Eterm ref = erts_sched_wall_time_request(BIF_P, 1, new, 0, 0);
ASSERT(is_value(ref));
BIF_TRAP2(await_sched_wall_time_mod_trap,
BIF_P,
ref,
old ? am_true : am_false);
}
#if defined(ERTS_SMP) && defined(ERTS_DIRTY_SCHEDULERS)
} else if (BIF_ARG_1 == am_dirty_cpu_schedulers_online) {
Sint old_no;
if (!is_small(BIF_ARG_2))
goto error;
switch (erts_set_schedulers_online(BIF_P,
ERTS_PROC_LOCK_MAIN,
signed_val(BIF_ARG_2),
&old_no,
1)) {
case ERTS_SCHDLR_SSPND_DONE:
BIF_RET(make_small(old_no));
case ERTS_SCHDLR_SSPND_YIELD_RESTART:
ERTS_VBUMP_ALL_REDS(BIF_P);
BIF_TRAP2(bif_export[BIF_system_flag_2],
BIF_P, BIF_ARG_1, BIF_ARG_2);
case ERTS_SCHDLR_SSPND_YIELD_DONE:
ERTS_BIF_YIELD_RETURN_X(BIF_P, make_small(old_no),
am_dirty_cpu_schedulers_online);
case ERTS_SCHDLR_SSPND_EINVAL:
goto error;
default:
ASSERT(0);
BIF_ERROR(BIF_P, EXC_INTERNAL_ERROR);
break;
}
#endif
} else if (BIF_ARG_1 == am_time_offset
&& ERTS_IS_ATOM_STR("finalize", BIF_ARG_2)) {
ErtsTimeOffsetState res;
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
res = erts_finalize_time_offset();
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_MAIN);
switch (res) {
case ERTS_TIME_OFFSET_PRELIMINARY: {
DECL_AM(preliminary);
BIF_RET(AM_preliminary);
}
case ERTS_TIME_OFFSET_FINAL: {
DECL_AM(final);
BIF_RET(AM_final);
}
case ERTS_TIME_OFFSET_VOLATILE: {
DECL_AM(volatile);
BIF_RET(AM_volatile);
}
default:
ERTS_INTERNAL_ERROR("Unknown state");
}
#ifdef ERTS_ENABLE_MSACC
} else if (BIF_ARG_1 == am_microstate_accounting) {
Eterm threads;
if (BIF_ARG_2 == am_true || BIF_ARG_2 == am_false) {
erts_aint32_t new = BIF_ARG_2 == am_true ? ERTS_MSACC_ENABLE : ERTS_MSACC_DISABLE;
erts_aint32_t old = erts_smp_atomic32_xchg_nob(&msacc, new);
Eterm ref = erts_msacc_request(BIF_P, new, &threads);
if (is_non_value(ref))
BIF_RET(old ? am_true : am_false);
BIF_TRAP3(await_msacc_mod_trap,
BIF_P,
ref,
old ? am_true : am_false,
threads);
} else if (BIF_ARG_2 == am_reset) {
Eterm ref = erts_msacc_request(BIF_P, ERTS_MSACC_RESET, &threads);
erts_aint32_t old = erts_smp_atomic32_read_nob(&msacc);
ASSERT(is_value(ref));
BIF_TRAP3(await_msacc_mod_trap,
BIF_P,
ref,
old ? am_true : am_false,
threads);
}
#endif
} else if (ERTS_IS_ATOM_STR("scheduling_statistics", BIF_ARG_1)) {
int what;
if (ERTS_IS_ATOM_STR("disable", BIF_ARG_2))
what = ERTS_SCHED_STAT_MODIFY_DISABLE;
else if (ERTS_IS_ATOM_STR("enable", BIF_ARG_2))
what = ERTS_SCHED_STAT_MODIFY_ENABLE;
else if (ERTS_IS_ATOM_STR("clear", BIF_ARG_2))
what = ERTS_SCHED_STAT_MODIFY_CLEAR;
else
goto error;
erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN);
erts_sched_stat_modify(what);
erts_smp_proc_lock(BIF_P, ERTS_PROC_LOCK_MAIN);
BIF_RET(am_true);
} else if (ERTS_IS_ATOM_STR("internal_cpu_topology", BIF_ARG_1)) {
Eterm res = erts_set_cpu_topology(BIF_P, BIF_ARG_2);
if (is_value(res))
BIF_RET(res);
} else if (ERTS_IS_ATOM_STR("cpu_topology", BIF_ARG_1)) {
erts_send_warning_to_logger_str(
BIF_P->group_leader,
"A call to erlang:system_flag(cpu_topology, _) was made.\n"
"The cpu_topology argument is deprecated and scheduled\n"
"for removal in Erlang/OTP 18. For more information\n"
"see the erlang:system_flag/2 documentation.\n");
BIF_TRAP1(set_cpu_topology_trap, BIF_P, BIF_ARG_2);
} else if (ERTS_IS_ATOM_STR("scheduler_bind_type", BIF_ARG_1)) {
erts_send_warning_to_logger_str(
BIF_P->group_leader,
"A call to erlang:system_flag(scheduler_bind_type, _) was\n"
"made. The scheduler_bind_type argument is deprecated and\n"
"scheduled for removal in Erlang/OTP 18. For more\n"
"information see the erlang:system_flag/2 documentation.\n");
return erts_bind_schedulers(BIF_P, BIF_ARG_2);
}
error:
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
BIF_RETTYPE phash_2(BIF_ALIST_2)
{
Uint32 hash;
Uint32 final_hash;
Uint32 range;
/* Check for special case 2^32 */
if (term_equals_2pow32(BIF_ARG_2)) {
range = 0;
} else {
Uint u;
if (!term_to_Uint(BIF_ARG_2, &u) || ((u >> 16) >> 16) != 0 || !u) {
BIF_ERROR(BIF_P, BADARG);
}
range = (Uint32) u;
}
hash = make_hash(BIF_ARG_1);
if (range) {
final_hash = 1 + (hash % range); /* [1..range] */
} else if ((final_hash = hash + 1) == 0) {
/*
* XXX In this case, there will still be a ArithAlloc() in erts_mixed_plus().
*/
BIF_RET(erts_mixed_plus(BIF_P,
erts_make_integer(hash, BIF_P),
make_small(1)));
}
BIF_RET(erts_make_integer(final_hash, BIF_P));
}
BIF_RETTYPE phash2_1(BIF_ALIST_1)
{
Uint32 hash;
hash = make_hash2(BIF_ARG_1);
BIF_RET(make_small(hash & ((1L << 27) - 1)));
}
BIF_RETTYPE phash2_2(BIF_ALIST_2)
{
Uint32 hash;
Uint32 final_hash;
Uint32 range;
/* Check for special case 2^32 */
if (term_equals_2pow32(BIF_ARG_2)) {
range = 0;
} else {
Uint u;
if (!term_to_Uint(BIF_ARG_2, &u) || ((u >> 16) >> 16) != 0 || !u) {
BIF_ERROR(BIF_P, BADARG);
}
range = (Uint32) u;
}
hash = make_hash2(BIF_ARG_1);
if (range) {
final_hash = hash % range; /* [0..range-1] */
} else {
final_hash = hash;
}
/*
* Return either a small or a big. Use the heap for bigs if there is room.
*/
#if defined(ARCH_64)
BIF_RET(make_small(final_hash));
#else
if (IS_USMALL(0, final_hash)) {
BIF_RET(make_small(final_hash));
} else {
Eterm* hp = HAlloc(BIF_P, BIG_UINT_HEAP_SIZE);
BIF_RET(uint_to_big(final_hash, hp));
}
#endif
}
BIF_RETTYPE bump_reductions_1(BIF_ALIST_1)
{
Sint reds;
if (is_not_small(BIF_ARG_1) || ((reds = signed_val(BIF_ARG_1)) < 0)) {
BIF_ERROR(BIF_P, BADARG);
}
if (reds > CONTEXT_REDS) {
reds = CONTEXT_REDS;
}
BIF_RET2(am_true, reds);
}
BIF_RETTYPE erts_internal_cmp_term_2(BIF_ALIST_2) {
Sint res = CMP_TERM(BIF_ARG_1,BIF_ARG_2);
/* ensure -1, 0, 1 result */
if (res < 0) {
BIF_RET(make_small(-1));
} else if (res > 0) {
BIF_RET(make_small(1));
}
BIF_RET(make_small(0));
}
/*
* Processes doing yield on return in a bif ends up in bif_return_trap().
*/
static BIF_RETTYPE bif_return_trap(BIF_ALIST_2)
{
Eterm res = BIF_ARG_1;
switch (BIF_ARG_2) {
case am_multi_scheduling: {
int msb = erts_is_multi_scheduling_blocked();
if (msb > 0)
res = am_blocked;
else if (msb < 0)
res = am_blocked_normal;
else
ERTS_INTERNAL_ERROR("Unexpected multi scheduling block state");
break;
}
default:
break;
}
BIF_RET(res);
}
/*
* NOTE: The erts_bif_prep_await_proc_exit_*() functions are
* tightly coupled with the implementation of erlang:await_proc_exit/3.
* The erts_bif_prep_await_proc_exit_*() functions can safely call
* skip_current_msgq() since they know that erlang:await_proc_exit/3
* unconditionally will do a monitor and then unconditionally will
* wait for the corresponding 'DOWN' message in a receive, and no other
* receive is done before this receive. This optimization removes an
* unnecessary scan of the currently existing message queue (which
* can be large). If the erlang:await_proc_exit/3 implementation
* is changed so that the above isn't true, nasty bugs in later
* receives, etc, may appear.
*/
static ERTS_INLINE int
skip_current_msgq(Process *c_p)
{
int res;
#if defined(ERTS_ENABLE_LOCK_CHECK) && defined(ERTS_SMP)
erts_proc_lc_chk_only_proc_main(c_p);
#endif
erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
if (ERTS_PROC_PENDING_EXIT(c_p)) {
KILL_CATCHES(c_p);
c_p->freason = EXC_EXIT;
res = 0;
}
else {
ERTS_SMP_MSGQ_MV_INQ2PRIVQ(c_p);
c_p->msg.save = c_p->msg.last;
res = 1;
}
erts_smp_proc_unlock(c_p, ERTS_PROC_LOCKS_MSG_RECEIVE);
return res;
}
void
erts_bif_prep_await_proc_exit_data_trap(Process *c_p, Eterm pid, Eterm ret)
{
if (skip_current_msgq(c_p)) {
ERTS_BIF_PREP_TRAP3_NO_RET(await_proc_exit_trap, c_p, pid, am_data, ret);
}
}
void
erts_bif_prep_await_proc_exit_reason_trap(Process *c_p, Eterm pid)
{
if (skip_current_msgq(c_p)) {
ERTS_BIF_PREP_TRAP3_NO_RET(await_proc_exit_trap, c_p,
pid, am_reason, am_undefined);
}
}
void
erts_bif_prep_await_proc_exit_apply_trap(Process *c_p,
Eterm pid,
Eterm module,
Eterm function,
Eterm args[],
int nargs)
{
ASSERT(is_atom(module) && is_atom(function));
if (skip_current_msgq(c_p)) {
Eterm term;
Eterm *hp;
int i;
hp = HAlloc(c_p, 4+2*nargs);
term = NIL;
for (i = nargs-1; i >= 0; i--) {
term = CONS(hp, args[i], term);
hp += 2;
}
term = TUPLE3(hp, module, function, term);
ERTS_BIF_PREP_TRAP3_NO_RET(await_proc_exit_trap, c_p, pid, am_apply, term);
}
}
Export bif_return_trap_export;
void erts_init_trap_export(Export* ep, Eterm m, Eterm f, Uint a,
Eterm (*bif)(BIF_ALIST))
{
int i;
sys_memset((void *) ep, 0, sizeof(Export));
for (i=0; i<ERTS_NUM_CODE_IX; i++) {
ep->addressv[i] = ep->beam;
}
ep->info.mfa.module = m;
ep->info.mfa.function = f;
ep->info.mfa.arity = a;
ep->beam[0] = (BeamInstr) em_apply_bif;
ep->beam[1] = (BeamInstr) bif;
}
void erts_init_bif(void)
{
/*
* bif_return_trap/2 is a hidden BIF that bifs that need to
* yield the calling process traps to.
*/
erts_init_trap_export(&bif_return_trap_export,
am_erlang, am_bif_return_trap, 2,
&bif_return_trap);
erts_await_result = erts_export_put(am_erts_internal,
am_await_result,
1);
erts_init_trap_export(&dsend_continue_trap_export,
am_erts_internal, am_dsend_continue_trap, 1,
dsend_continue_trap_1);
flush_monitor_messages_trap = erts_export_put(am_erts_internal,
am_flush_monitor_messages,
3);
erts_convert_time_unit_trap = erts_export_put(am_erlang,
am_convert_time_unit,
3);
set_cpu_topology_trap = erts_export_put(am_erlang,
am_set_cpu_topology,
1);
erts_format_cpu_topology_trap = erts_export_put(am_erlang,
am_format_cpu_topology,
1);
await_proc_exit_trap = erts_export_put(am_erlang,am_await_proc_exit,3);
await_port_send_result_trap
= erts_export_put(am_erts_internal, am_await_port_send_result, 3);
await_sched_wall_time_mod_trap
= erts_export_put(am_erlang, am_await_sched_wall_time_modifications, 2);
await_msacc_mod_trap
= erts_export_put(am_erts_internal, am_await_microstate_accounting_modifications, 3);
erts_smp_atomic32_init_nob(&sched_wall_time, 0);
erts_smp_atomic32_init_nob(&msacc, ERTS_MSACC_IS_ENABLED());
}
/*
* Scheduling of BIFs via NifExport...
*/
#define ERTS_WANT_NFUNC_SCHED_INTERNALS__
#include "erl_nfunc_sched.h"
#define ERTS_SCHED_BIF_TRAP_MARKER ((void *) (UWord) 1)
static ERTS_INLINE void
schedule(Process *c_p, Process *dirty_shadow_proc,
ErtsCodeMFA *mfa, BeamInstr *pc,
ErtsBifFunc dfunc, void *ifunc,
Eterm module, Eterm function,
int argc, Eterm *argv)
{
ERTS_SMP_LC_ASSERT(ERTS_PROC_LOCK_MAIN & erts_proc_lc_my_proc_locks(c_p));
(void) erts_nif_export_schedule(c_p, dirty_shadow_proc,
mfa, pc, (BeamInstr) em_apply_bif,
dfunc, ifunc,
module, function,
argc, argv);
}
#ifdef ERTS_DIRTY_SCHEDULERS
static BIF_RETTYPE dirty_bif_result(BIF_ALIST_1)
{
NifExport *nep = (NifExport *) ERTS_PROC_GET_NIF_TRAP_EXPORT(BIF_P);
erts_nif_export_restore(BIF_P, nep, BIF_ARG_1);
BIF_RET(BIF_ARG_1);
}
static BIF_RETTYPE dirty_bif_trap(BIF_ALIST)
{
NifExport *nep = (NifExport *) ERTS_PROC_GET_NIF_TRAP_EXPORT(BIF_P);
/*
* Arity and argument registers already set
* correct by call to dirty_bif_trap()...
*/
ASSERT(BIF_P->arity == nep->exp.info.mfa.arity);
erts_nif_export_restore(BIF_P, nep, THE_NON_VALUE);
BIF_P->i = (BeamInstr *) nep->func;
BIF_P->freason = TRAP;
return THE_NON_VALUE;
}
static BIF_RETTYPE dirty_bif_exception(BIF_ALIST_2)
{
Eterm freason;
ASSERT(is_small(BIF_ARG_1));
freason = signed_val(BIF_ARG_1);
/* Restore orig info for error and clear nif export in handle_error() */
freason |= EXF_RESTORE_NIF;
BIF_P->fvalue = BIF_ARG_2;
BIF_ERROR(BIF_P, freason);
}
#endif /* ERTS_DIRTY_SCHEDULERS */
extern BeamInstr* em_call_bif_e;
static BIF_RETTYPE call_bif(Process *c_p, Eterm *reg, BeamInstr *I);
BIF_RETTYPE
erts_schedule_bif(Process *proc,
Eterm *argv,
BeamInstr *i,
ErtsBifFunc bif,
ErtsSchedType sched_type,
Eterm mod,
Eterm func,
int argc)
{
Process *c_p, *dirty_shadow_proc;
ErtsCodeMFA *mfa;
#ifdef ERTS_DIRTY_SCHEDULERS
if (proc->static_flags & ERTS_STC_FLG_SHADOW_PROC) {
dirty_shadow_proc = proc;
c_p = proc->next;
ASSERT(c_p->common.id == dirty_shadow_proc->common.id);
erts_smp_proc_lock(c_p, ERTS_PROC_LOCK_MAIN);
}
else
#endif
{
dirty_shadow_proc = NULL;
c_p = proc;
}
if (!ERTS_PROC_IS_EXITING(c_p)) {
Export *exp;
BifFunction dbif, ibif;
BeamInstr *pc;
/*
* dbif - direct bif
* ibif - indirect bif
*/
#ifdef ERTS_DIRTY_SCHEDULERS
erts_aint32_t set, mask;
mask = (ERTS_PSFLG_DIRTY_CPU_PROC
| ERTS_PSFLG_DIRTY_IO_PROC);
switch (sched_type) {
case ERTS_SCHED_DIRTY_CPU:
set = ERTS_PSFLG_DIRTY_CPU_PROC;
dbif = bif;
ibif = NULL;
break;
case ERTS_SCHED_DIRTY_IO:
set = ERTS_PSFLG_DIRTY_IO_PROC;
dbif = bif;
ibif = NULL;
break;
case ERTS_SCHED_NORMAL:
default:
set = 0;
dbif = call_bif;
ibif = bif;
break;
}
(void) erts_smp_atomic32_read_bset_nob(&c_p->state, mask, set);
#else
dbif = call_bif;
ibif = bif;
#endif
if (i == NULL) {
ERTS_INTERNAL_ERROR("Missing instruction pointer");
}
#ifdef HIPE
else if (proc->flags & F_HIPE_MODE) {
/* Pointer to bif export in i */
exp = (Export *) i;
pc = c_p->cp;
mfa = &exp->info.mfa;
}
#endif
else if (em_call_bif_e == (BeamInstr *) *i) {
/* Pointer to bif export in i+1 */
exp = (Export *) i[1];
pc = i;
mfa = &exp->info.mfa;
}
else if (em_apply_bif == (BeamInstr *) *i) {
/* Pointer to bif in i+1, and mfa in i-3 */
pc = c_p->cp;
mfa = erts_code_to_codemfa(i);
}
else {
ERTS_INTERNAL_ERROR("erts_schedule_bif() called "
"from unexpected instruction");
}
ASSERT(bif);
if (argc < 0) { /* reschedule original call */
mod = mfa->module;
func = mfa->function;
argc = (int) mfa->arity;
}
schedule(c_p, dirty_shadow_proc, mfa, pc, dbif, ibif,
mod, func, argc, argv);
}
if (dirty_shadow_proc)
erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_MAIN);
return THE_NON_VALUE;
}
static BIF_RETTYPE
call_bif(Process *c_p, Eterm *reg, BeamInstr *I)
{
NifExport *nep = ERTS_I_BEAM_OP_TO_NIF_EXPORT(I);
ErtsBifFunc bif = (ErtsBifFunc) nep->func;
BIF_RETTYPE ret;
ASSERT(!ERTS_SCHEDULER_IS_DIRTY(erts_get_scheduler_data()));
nep->func = ERTS_SCHED_BIF_TRAP_MARKER;
ASSERT(bif);
ret = (*bif)(c_p, reg, I);
if (is_value(ret))
erts_nif_export_restore(c_p, nep, ret);
else if (c_p->freason != TRAP)
c_p->freason |= EXF_RESTORE_NIF; /* restore in handle_error() */
else if (nep->func == ERTS_SCHED_BIF_TRAP_MARKER) {
/* BIF did an ordinary trap... */
erts_nif_export_restore(c_p, nep, ret);
}
/* else:
* BIF rescheduled itself using erts_schedule_bif().
*/
return ret;
}
#ifdef ERTS_DIRTY_SCHEDULERS
int
erts_call_dirty_bif(ErtsSchedulerData *esdp, Process *c_p, BeamInstr *I, Eterm *reg)
{
BIF_RETTYPE result;
int exiting;
Process *dirty_shadow_proc;
ErtsBifFunc bf;
NifExport *nep;
#ifdef DEBUG
Eterm *c_p_htop;
erts_aint32_t state;
ASSERT(!c_p->scheduler_data);
state = erts_smp_atomic32_read_nob(&c_p->state);
ASSERT((state & ERTS_PSFLG_DIRTY_RUNNING)
&& !(state & (ERTS_PSFLG_RUNNING|ERTS_PSFLG_RUNNING_SYS)));
ASSERT(esdp);
#endif
nep = ERTS_I_BEAM_OP_TO_NIF_EXPORT(I);
ASSERT(nep == ERTS_PROC_GET_NIF_TRAP_EXPORT(c_p));
nep->func = ERTS_SCHED_BIF_TRAP_MARKER;
bf = (ErtsBifFunc) I[1];
erts_smp_atomic32_read_band_mb(&c_p->state, ~(ERTS_PSFLG_DIRTY_CPU_PROC
| ERTS_PSFLG_DIRTY_IO_PROC));
dirty_shadow_proc = erts_make_dirty_shadow_proc(esdp, c_p);
dirty_shadow_proc->freason = c_p->freason;
dirty_shadow_proc->fvalue = c_p->fvalue;
dirty_shadow_proc->ftrace = c_p->ftrace;
dirty_shadow_proc->cp = c_p->cp;
dirty_shadow_proc->i = c_p->i;
#ifdef DEBUG
c_p_htop = c_p->htop;
#endif
erts_smp_proc_unlock(c_p, ERTS_PROC_LOCK_MAIN);
result = (*bf)(dirty_shadow_proc, reg, I);
erts_smp_proc_lock(c_p, ERTS_PROC_LOCK_MAIN);
ASSERT(c_p_htop == c_p->htop);
ASSERT(dirty_shadow_proc->static_flags & ERTS_STC_FLG_SHADOW_PROC);
ASSERT(dirty_shadow_proc->next == c_p);
exiting = ERTS_PROC_IS_EXITING(c_p);
if (!exiting) {
if (is_value(result))
schedule(c_p, dirty_shadow_proc, NULL, NULL, dirty_bif_result,
NULL, am_erts_internal, am_dirty_bif_result, 1, &result);
else if (dirty_shadow_proc->freason != TRAP) {
Eterm argv[2];
ASSERT(dirty_shadow_proc->freason <= MAX_SMALL);
argv[0] = make_small(dirty_shadow_proc->freason);
argv[1] = dirty_shadow_proc->fvalue;
schedule(c_p, dirty_shadow_proc, NULL, NULL,
dirty_bif_exception, NULL, am_erts_internal,
am_dirty_bif_exception, 2, argv);
}
else if (nep->func == ERTS_SCHED_BIF_TRAP_MARKER) {
/* Dirty BIF did an ordinary trap... */
ASSERT(!(erts_smp_atomic32_read_nob(&c_p->state)
& (ERTS_PSFLG_DIRTY_CPU_PROC|ERTS_PSFLG_DIRTY_IO_PROC)));
schedule(c_p, dirty_shadow_proc, NULL, NULL,
dirty_bif_trap, (void *) dirty_shadow_proc->i,
am_erts_internal, am_dirty_bif_trap,
dirty_shadow_proc->arity, reg);
}
/* else:
* BIF rescheduled itself using erts_schedule_bif().
*/
c_p->freason = dirty_shadow_proc->freason;
c_p->fvalue = dirty_shadow_proc->fvalue;
c_p->ftrace = dirty_shadow_proc->ftrace;
c_p->cp = dirty_shadow_proc->cp;
c_p->i = dirty_shadow_proc->i;
c_p->arity = dirty_shadow_proc->arity;
}
erts_flush_dirty_shadow_proc(dirty_shadow_proc);
return exiting;
}
#endif /* ERTS_DIRTY_SCHEDULERS */
#ifdef HARDDEBUG
/*
You'll need this line in bif.tab to be able to use this debug bif
bif erlang:send_to_logger/2
*/
BIF_RETTYPE send_to_logger_2(BIF_ALIST_2)
{
byte *buf;
ErlDrvSizeT len;
if (!is_atom(BIF_ARG_1) || !(is_list(BIF_ARG_2) ||
is_nil(BIF_ARG_1))) {
BIF_ERROR(BIF_P,BADARG);
}
if (erts_iolist_size(BIF_ARG_2, &len) != 0)
BIF_ERROR(BIF_P,BADARG);
else if (len == 0)
buf = "";
else {
#ifdef DEBUG
ErlDrvSizeT len2;
#endif
buf = (byte *) erts_alloc(ERTS_ALC_T_TMP, len+1);
#ifdef DEBUG
len2 =
#else
(void)
#endif
erts_iolist_to_buf(BIF_ARG_2, buf, len);
ASSERT(len2 == len);
buf[len] = '\0';
switch (BIF_ARG_1) {
case am_info:
erts_send_info_to_logger(BIF_P->group_leader, buf, len);
break;
case am_warning:
erts_send_warning_to_logger(BIF_P->group_leader, buf, len);
break;
case am_error:
erts_send_error_to_logger(BIF_P->group_leader, buf, len);
break;
default:
{
BIF_ERROR(BIF_P,BADARG);
}
}
erts_free(ERTS_ALC_T_TMP, (void *) buf);
}
BIF_RET(am_true);
}
#endif /* HARDDEBUG */
BIF_RETTYPE get_module_info_1(BIF_ALIST_1)
{
Eterm ret = erts_module_info_0(BIF_P, BIF_ARG_1);
if (is_non_value(ret)) {
BIF_ERROR(BIF_P, BADARG);
}
BIF_RET(ret);
}
BIF_RETTYPE get_module_info_2(BIF_ALIST_2)
{
Eterm ret = erts_module_info_1(BIF_P, BIF_ARG_1, BIF_ARG_2);
if (is_non_value(ret)) {
BIF_ERROR(BIF_P, BADARG);
}
BIF_RET(ret);
}
BIF_RETTYPE dt_put_tag_1(BIF_ALIST_1)
{
#ifdef USE_VM_PROBES
Eterm otag;
if (BIF_ARG_1 == am_undefined) {
otag = (DT_UTAG(BIF_P) == NIL) ? am_undefined : DT_UTAG(BIF_P);
DT_UTAG(BIF_P) = NIL;
DT_UTAG_FLAGS(BIF_P) = 0;
if (SEQ_TRACE_TOKEN(BIF_P) == am_have_dt_utag) {
SEQ_TRACE_TOKEN(BIF_P) = NIL;
}
BIF_RET(otag);
}
if (!is_binary(BIF_ARG_1)) {
BIF_ERROR(BIF_P,BADARG);
}
otag = (DT_UTAG(BIF_P) == NIL) ? am_undefined : DT_UTAG(BIF_P);
DT_UTAG(BIF_P) = BIF_ARG_1;
DT_UTAG_FLAGS(BIF_P) |= DT_UTAG_PERMANENT;
if (SEQ_TRACE_TOKEN(BIF_P) == NIL) {
SEQ_TRACE_TOKEN(BIF_P) = am_have_dt_utag;
}
BIF_RET(otag);
#else
BIF_RET(am_undefined);
#endif
}
BIF_RETTYPE dt_get_tag_0(BIF_ALIST_0)
{
#ifdef USE_VM_PROBES
BIF_RET((DT_UTAG(BIF_P) == NIL || !(DT_UTAG_FLAGS(BIF_P) & DT_UTAG_PERMANENT)) ? am_undefined : DT_UTAG(BIF_P));
#else
BIF_RET(am_undefined);
#endif
}
BIF_RETTYPE dt_get_tag_data_0(BIF_ALIST_0)
{
#ifdef USE_VM_PROBES
BIF_RET((DT_UTAG(BIF_P) == NIL) ? am_undefined : DT_UTAG(BIF_P));
#else
BIF_RET(am_undefined);
#endif
}
BIF_RETTYPE dt_prepend_vm_tag_data_1(BIF_ALIST_1)
{
#ifdef USE_VM_PROBES
Eterm b;
Eterm *hp;
if (is_binary((DT_UTAG(BIF_P)))) {
Uint sz = binary_size(DT_UTAG(BIF_P));
int i;
unsigned char *p,*q;
byte *temp_alloc = NULL;
b = new_binary(BIF_P,NULL,sz+1);
q = binary_bytes(b);
p = erts_get_aligned_binary_bytes(DT_UTAG(BIF_P),&temp_alloc);
for(i=0;i<sz;++i) {
q[i] = p[i];
}
erts_free_aligned_binary_bytes(temp_alloc);
q[sz] = '\0';
} else {
b = new_binary(BIF_P,(byte *)"\0",1);
}
hp = HAlloc(BIF_P,2);
BIF_RET(CONS(hp,b,BIF_ARG_1));
#else
BIF_RET(BIF_ARG_1);
#endif
}
BIF_RETTYPE dt_append_vm_tag_data_1(BIF_ALIST_1)
{
#ifdef USE_VM_PROBES
Eterm b;
Eterm *hp;
if (is_binary((DT_UTAG(BIF_P)))) {
Uint sz = binary_size(DT_UTAG(BIF_P));
int i;
unsigned char *p,*q;
byte *temp_alloc = NULL;
b = new_binary(BIF_P,NULL,sz+1);
q = binary_bytes(b);
p = erts_get_aligned_binary_bytes(DT_UTAG(BIF_P),&temp_alloc);
for(i=0;i<sz;++i) {
q[i] = p[i];
}
erts_free_aligned_binary_bytes(temp_alloc);
q[sz] = '\0';
} else {
b = new_binary(BIF_P,(byte *)"\0",1);
}
hp = HAlloc(BIF_P,2);
BIF_RET(CONS(hp,BIF_ARG_1,b));
#else
BIF_RET(BIF_ARG_1);
#endif
}
BIF_RETTYPE dt_spread_tag_1(BIF_ALIST_1)
{
#ifdef USE_VM_PROBES
Eterm ret;
Eterm *hp;
#endif
if (BIF_ARG_1 != am_true && BIF_ARG_1 != am_false) {
BIF_ERROR(BIF_P,BADARG);
}
#ifdef USE_VM_PROBES
hp = HAlloc(BIF_P,3);
ret = TUPLE2(hp,make_small(DT_UTAG_FLAGS(BIF_P)),DT_UTAG(BIF_P));
if (DT_UTAG(BIF_P) != NIL) {
if (BIF_ARG_1 == am_true) {
DT_UTAG_FLAGS(BIF_P) |= DT_UTAG_SPREADING;
#ifdef DTRACE_TAG_HARDDEBUG
erts_fprintf(stderr,
"Dtrace -> (%T) start spreading tag %T\r\n",
BIF_P->common.id,DT_UTAG(BIF_P));
#endif
} else {
DT_UTAG_FLAGS(BIF_P) &= ~DT_UTAG_SPREADING;
#ifdef DTRACE_TAG_HARDDEBUG
erts_fprintf(stderr,
"Dtrace -> (%T) stop spreading tag %T\r\n",
BIF_P->common.id,DT_UTAG(BIF_P));
#endif
}
}
BIF_RET(ret);
#else
BIF_RET(am_true);
#endif
}
BIF_RETTYPE dt_restore_tag_1(BIF_ALIST_1)
{
#ifdef USE_VM_PROBES
Eterm *tpl;
Uint x;
if (is_not_tuple(BIF_ARG_1)) {
BIF_ERROR(BIF_P,BADARG);
}
tpl = tuple_val(BIF_ARG_1);
if(arityval(*tpl) != 2 || is_not_small(tpl[1]) || (is_not_binary(tpl[2]) && tpl[2] != NIL)) {
BIF_ERROR(BIF_P,BADARG);
}
if (tpl[2] == NIL) {
if (DT_UTAG(BIF_P) != NIL) {
#ifdef DTRACE_TAG_HARDDEBUG
erts_fprintf(stderr,
"Dtrace -> (%T) restore Killing tag!\r\n",
BIF_P->common.id);
#endif
}
DT_UTAG(BIF_P) = NIL;
if (SEQ_TRACE_TOKEN(BIF_P) == am_have_dt_utag) {
SEQ_TRACE_TOKEN(BIF_P) = NIL;
}
DT_UTAG_FLAGS(BIF_P) = 0;
} else {
x = unsigned_val(tpl[1]) & (DT_UTAG_SPREADING | DT_UTAG_PERMANENT);
#ifdef DTRACE_TAG_HARDDEBUG
if (!(x & DT_UTAG_SPREADING) && (DT_UTAG_FLAGS(BIF_P) &
DT_UTAG_SPREADING)) {
erts_fprintf(stderr,
"Dtrace -> (%T) restore stop spreading "
"tag %T\r\n",
BIF_P->common.id, tpl[2]);
} else if ((x & DT_UTAG_SPREADING) &&
!(DT_UTAG_FLAGS(BIF_P) & DT_UTAG_SPREADING)) {
erts_fprintf(stderr,
"Dtrace -> (%T) restore start spreading "
"tag %T\r\n",BIF_P->common.id,tpl[2]);
}
#endif
DT_UTAG_FLAGS(BIF_P) = x;
DT_UTAG(BIF_P) = tpl[2];
if (SEQ_TRACE_TOKEN(BIF_P) == NIL) {
SEQ_TRACE_TOKEN(BIF_P) = am_have_dt_utag;
}
}
#else
if (BIF_ARG_1 != am_true) {
BIF_ERROR(BIF_P,BADARG);
}
#endif
BIF_RET(am_true);
}
|