aboutsummaryrefslogblamecommitdiffstats
path: root/erts/emulator/sys/vxworks/sys.c
blob: 97a2ae7f7bf3220a725a7f0f9cddaecdf4ffefea (plain) (tree)
1
2
3
4
5
6
7
8
9
10
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

                   
  
                                                        
  




                                                                      
  



                                                                         
  






































































                                                                                      
                                               
























































                                                                              







                                                      






















































































                                                                                





                                    







                                                                   





























































                                                                             
                                                                                        


















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                              



























































































































































































































































































































































































































































































































































































                                                                                 
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 1997-2011. All Rights Reserved.
 *
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * %CopyrightEnd%
 */
/*
 * system-dependent functions
 * 
 */
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include <vxWorks.h>
#include <version.h>
#include <string.h>
#include <types.h>
#include <sigLib.h>
#include <ioLib.h>
#include <iosLib.h>
#include <envLib.h>
#include <fioLib.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <symLib.h>
#include <sysLib.h>
#include <sysSymTbl.h>
#include <loadLib.h>
#include <taskLib.h>
#include <taskVarLib.h>
#include <taskHookLib.h>
#include <tickLib.h>
#include <time.h>
#include <rngLib.h>
#include <semLib.h>
#include <selectLib.h>
#include <sockLib.h>
#include <a_out.h>
#include <wdLib.h>
#include <timers.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdarg.h>


#ifndef WANT_NONBLOCKING
#define WANT_NONBLOCKING
#endif

#include "sys.h"
#include "erl_alloc.h"

/* don't need global.h, but bif_table.h (included by bif.h) won't compile otherwise */
#include "global.h" 
#include "bif.h"

#include "erl_sys_driver.h"

#include "elib_stat.h"

#include "reclaim_private.h" /* Some more or less private reclaim facilities */

#ifndef RETSIGTYPE
#define RETSIGTYPE void
#endif

EXTERN_FUNCTION(void, erl_start, (int, char**));
EXTERN_FUNCTION(void, erl_exit, (int n, char*, _DOTS_));
EXTERN_FUNCTION(void, erl_error, (char*, va_list));
EXTERN_FUNCTION(int, driver_interrupt, (int, int));
EXTERN_FUNCTION(void, increment_time, (int));
EXTERN_FUNCTION(int, erts_next_time, (_VOID_));
EXTERN_FUNCTION(void, set_reclaim_free_function, (FreeFunction));
EXTERN_FUNCTION(int, erl_mem_info_get, (MEM_PART_STATS *));
EXTERN_FUNCTION(void, erl_crash_dump, (char* file, int line, char* fmt, ...));

#define ISREG(st) (((st).st_mode&S_IFMT) == S_IFREG)

/* these are defined in usrLib.c */
extern int spTaskPriority, spTaskOptions;

/* forward declarations */
static FUNCTION(FUNCPTR, lookup, (char*));
static FUNCTION(int, read_fill, (int, char*, int));
#if (CPU == SPARC)
static FUNCTION(RETSIGTYPE, fpe_sig_handler, (int)); /*where is this fun? */
#elif (CPU == PPC603)
static FUNCTION(void, fix_registers, (void));
#endif
static FUNCTION(void, close_pipes, (int*, int*, int));
static FUNCTION(void, delete_hook, (void));
static FUNCTION(void, initialize_allocation, (void));

FUNCTION(STATUS, uxPipeDrv, (void));
FUNCTION(STATUS, pipe, (int*));
FUNCTION(void, uxPipeShow, (int));

void erl_main(int argc, char **argv);
void argcall(char *args);

/* Malloc-realted functions called from the VxWorks shell */
EXTERN_FUNCTION(int, erl_set_memory_block, 
		(int, int, int, int, int, int, int, int, int, int));
EXTERN_FUNCTION(int, erl_memory_show, 
		(int, int, int, int, int, int, int, int, int, int));

#define DEFAULT_PORT_STACK_SIZE  100000 
static int port_stack_size;

static int erlang_id = 0;	/* Inited at loading, set/reset at each run */

/* interval time reported to emulator */
static int sys_itime;

/* XXX - This is defined in .../config/all/configAll.h (NUM_FILES),
   and not easily accessible at compile or run time - however,
   in VxWorks 5.1 it is stored in the (undocumented?) maxFiles variable;
   probably shouldn't depend on it, but we try to pick it up... */
static int max_files = 50;	/* default configAll.h */

int erts_vxworks_max_files;

/* 
 * used by the break handler (set by signal handler on ctl-c)
 */
volatile int erts_break_requested;

/********************* General functions ****************************/

/*
 * Reset the terminal to the original settings on exit
 * (nothing to do for WxWorks).
 */
void sys_tty_reset(int exit_code)
{
}

Uint
erts_sys_misc_mem_sz(void)
{
    Uint res = erts_check_io_size();
    /* res += FIXME */
    return res;
}

/*
 * XXX This declaration should not be here.
 */
void erl_sys_schedule_loop(void);

#ifdef SOFTDEBUG
static void do_trace(int line, char *file, char *format, ...)
{
    va_list va;
    int tid = taskIdSelf();
    char buff[512];

    va_start(va, format);
    sprintf(buff,"Trace: Task: 0x%08x, %s:%d - ",
	    tid, file, line);
    vsprintf(buff + strlen(buff), format, va);
    va_end(va);
    strcat(buff,"\r\n");
    write(2,buff,strlen(buff));
}

#define TRACE() do_trace(__LINE__, __FILE__,"")
#define TRACEF(Args...) do_trace(__LINE__,__FILE__, ## Args)
#endif

void
erts_sys_pre_init(void)
{
    if (erlang_id != 0) {
	/* NOTE: This particular case must *not* call erl_exit() */
	erts_fprintf(stderr, "Sorry, erlang is already running (as task %d)\n",
		     erlang_id);
	exit(1);
    }

    /* This must be done as early as possible... */
    if(!reclaim_init()) 
	fprintf(stderr, "Warning : reclaim facility should be initiated before "
		"erlang is started!\n");
    erts_vxworks_max_files = max_files = reclaim_max_files();

    /* Floating point exceptions */
#if (CPU == SPARC)
    sys_sigset(SIGFPE, fpe_sig_handler);
#elif (CPU == PPC603)
    fix_registers();
#endif

    /* register the private delete hook in reclaim */
    save_delete_hook((FUNCPTR)delete_hook, (caddr_t)0);
    erlang_id = taskIdSelf();
#ifdef DEBUG
    printf("emulator task id = 0x%x\n", erlang_id);
#endif
}

void erts_sys_alloc_init(void)
{
    initialize_allocation();
}

void
erl_sys_init(void)
{
    setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
    /* XXX Bug in VxWorks stdio loses fputch()'ed output after the
       setvbuf() but before a *printf(), and possibly worse (malloc
       errors, crash?) - so let's give it a *printf().... */
    fprintf(stdout, "%s","");
}

void
erl_sys_args(int* argc, char** argv)
{
    erts_init_check_io();
    max_files = erts_check_io_max_files();
    ASSERT(max_files <= erts_vxworks_max_files);
}

void
erts_sys_schedule_interrupt(int set)
{
    erts_check_io_interrupt(set);
}

/*
 * Called from schedule() when it runs out of runnable processes,
 * or when Erlang code has performed INPUT_REDUCTIONS reduction
 * steps. runnable == 0 iff there are no runnable Erlang processes.
 */
void
erl_sys_schedule(int runnable)
{	
    erts_check_io(!runnable);
}

void erts_do_break_handling(void)
{
    SET_BLOCKING(0);
    /* call the break handling function, reset the flag */
    do_break();
    erts_break_requested = 0;
    SET_NONBLOCKING(0);
}

/* signal handling */
RETSIGTYPE (*sys_sigset(sig, func))()
     int sig;
     RETSIGTYPE (*func)();
{
  struct sigaction act, oact;

  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;
  act.sa_handler = func;
  sigaction(sig, &act, &oact);
  return(oact.sa_handler);
}

void sys_sigblock(int sig)
{
  sigset_t mask;

  sigemptyset(&mask);
  sigaddset(&mask, sig);
  sigprocmask(SIG_BLOCK, &mask, (sigset_t *)NULL);
}

void sys_sigrelease(int sig)
{
  sigset_t mask;

  sigemptyset(&mask);
  sigaddset(&mask, sig);
  sigprocmask(SIG_UNBLOCK, &mask, (sigset_t *)NULL);
}

void
erts_sys_prepare_crash_dump(void)
{
    
}

/* register signal handlers XXX - they don't work, need to find out why... */
/* set up signal handlers for break and quit */
static void request_break(void)
{
  /* just set a flag - checked for and handled 
   * in main thread (not signal handler).
   * see check_io() 
   */
#ifdef DEBUG
  fprintf(stderr,"break!\n");
#endif
  erts_break_requested = 1;
  erts_check_io_async_sig_interrupt(1); /* Make sure we don't sleep in erts_poll_wait */
}

static void do_quit(void)
{
    halt_0(0);
}

void erts_set_ignore_break(void) {
}

void init_break_handler(void)
{
  sys_sigset(SIGINT, request_break);
  sys_sigset(SIGQUIT, do_quit);
}

void erts_replace_intr(void) {
}

int sys_max_files(void) 
{
  return(max_files);
}

/******************* Routines for time measurement *********************/

int sys_init_time(void) 
{
    erts_clock_rate =  sysClkRateGet();
    /*
    ** One could imagine that it would be better returning 
    ** a resolution more near the clock rate, like in:
    ** return 1000 / erts_clock_rate; 
    ** but tests show that such isn't the case (rounding errors?)
    ** Well, we go for the Unix variant of returning 1 
    ** as a constant virtual clock rate.
    */
    return SYS_CLOCK_RESOLUTION;
}

int erts_clock_rate;
static volatile int ticks_inuse;
static volatile unsigned long ticks_collected; /* will wrap */
static WDOG_ID watchdog_id;
static ULONG user_time;
static int this_task_id, sys_itime;
static SysHrTime hrtime_wrap;
static unsigned long last_tick_count;

static void tolerant_time_clockint(int count)
{
  if (watchdog_id != NULL) {
    if (taskIsReady(this_task_id))
      user_time += 1;
    ++count;
    if (!ticks_inuse) {
      ticks_collected += count;
      count = 0;
    }
    wdStart(watchdog_id, 1, (FUNCPTR)tolerant_time_clockint, count);
  }
}

int sys_init_hrtime(void)
{
    this_task_id = taskIdSelf(); /* OK, this only works for one single task
				    in the system... */
    user_time = 0;

    ticks_inuse = 0;
    ticks_collected = 0;
    hrtime_wrap = 0;
    last_tick_count = 0;

    sys_itime = 1000 / erts_clock_rate;
    watchdog_id = wdCreate();
    wdStart(watchdog_id, 1, (FUNCPTR) tolerant_time_clockint, 0);
    return 0;
}

SysHrTime sys_gethrtime(void) 
{
    SysHrTime ticks;

    ++ticks_inuse;
    ticks = (SysHrTime) (ticks_collected & 0x7FFFFFFF);
    ticks_inuse = 0;
    if (ticks < (SysHrTime) last_tick_count) {
	hrtime_wrap += 1UL << 31;
    }
    last_tick_count = ticks;
    return (ticks + hrtime_wrap) * ((SysHrTime) (1000000000UL / 
						 erts_clock_rate));
}
    
void sys_gettimeofday(SysTimeval *tvp)
{
    struct timespec now;
    
    clock_gettime(CLOCK_REALTIME, &now);
    tvp->tv_sec = now.tv_sec;
    tvp->tv_usec = now.tv_nsec / 1000;
}

clock_t sys_times(SysTimes *t)
{
    t->tms_stime = t->tms_cutime = t->tms_cstime = 0;
    ++ticks_inuse;
    t->tms_utime = user_time;
    ticks_inuse = 0;
    return tickGet(); /* The best we can do... */
}

/* This is called when *this task* is deleted */
static void delete_hook(void)
{
  if (watchdog_id != NULL) {
    wdDelete(watchdog_id);
    watchdog_id = NULL;
  }
  erlang_id = 0;
  this_task_id = 0;
}

/************************** OS info *******************************/

/* Used by erlang:info/1. */
/* (This code was formerly in drv.XXX/XXX_os_drv.c) */

#define MAX_VER_STR 9           /* Number of characters to
                                   consider in version string */                              

static FUNCTION(int, get_number, (char** str_ptr));

char os_type[] = "vxworks";

static int
get_number(char **str_ptr)
{
    char* s = *str_ptr;		/* Pointer to beginning of string. */
    char* dot;			/* Pointer to dot in string or NULL. */

    if (!isdigit(*s))
	return 0;
    if ((dot = strchr(s, '.')) == NULL) {
	*str_ptr = s+strlen(s);
	return atoi(s);
    } else {
	*dot = '\0';
	*str_ptr = dot+1;
	return atoi(s);
    }
}

/* namebuf;     Where to return the name. */
/* size;	Size of name buffer. */
void
os_flavor(char *namebuf, unsigned size)
{
    strcpy(namebuf, "-");
}

/* int* pMajor;			Pointer to major version. */
/* int* pMinor;			Pointer to minor version. */
/* int* pBuild;			Pointer to build number. */
void
os_version(int *pMajor, int *pMinor, int *pBuild)
{
    char os_ver[MAX_VER_STR+2];
    char* release;		/* Pointer to the release string:
				 * X.Y or X.Y.Z.
				 */
    strncpy(os_ver, vxWorksVersion, MAX_VER_STR);
    release = os_ver;
    *pMajor = get_number(&release);
    *pMinor = get_number(&release);
    *pBuild = get_number(&release);
}

void init_getenv_state(GETENV_STATE *state)
{
   *state = NULL;
}

char *getenv_string(GETENV_STATE *state0)
{
   return NULL;
}

void fini_getenv_state(GETENV_STATE *state)
{
   *state = NULL;
}

/************************** Port I/O *******************************/


/* I. Common stuff */

#define TMP_BUF_MAX (tmp_buf_size - 1024)
static byte *tmp_buf;
static Uint tmp_buf_size;

/* II. The spawn/fd/vanilla drivers */

/* This data is shared by these drivers - initialized by spawn_init() */
static struct driver_data {
    int port_num, ofd, packet_bytes, report_exit;
    int exitcode, exit_reported; /* For returning of exit codes. */
} *driver_data;			/* indexed by fd */

/* 
 * Locking only for exitcodes and exit_reported, one global sem for all
 * spawn ports as this is rare.
 */
static SEM_ID driver_data_sem = NULL;
/*
 * Also locking when looking up entries in the load table
 */
static SEM_ID entry_data_sem = NULL;

/* We maintain a linked fifo queue of these structs in order */
/* to manage unfinnished reads/and writes on differenet fd's */

typedef struct pend {
    char *cpos;
    int fd;
    int remain;
    struct pend *next;
    char buf[1];   /* this is a trick to be able to malloc one chunk */
} Pend;

static struct fd_data {
    int inport, outport;
    char *buf, *cpos;
    int sz, remain;  /* for input on fd */
    Pend* pending;   /* pending outputs */

} *fd_data;			/* indexed by fd */
    

/* Driver interfaces */
static ErlDrvData spawn_start(ErlDrvPort port_num, char *name, SysDriverOpts* opts);
static ErlDrvData fd_start(ErlDrvPort port_num, char *name, SysDriverOpts* opts);
static ErlDrvData vanilla_start(ErlDrvPort port_num, char *name, SysDriverOpts* opts);
static int spawn_init(void);
static void fd_stop(ErlDrvData);
static void stop(ErlDrvData);
static void ready_input(ErlDrvData fd, ErlDrvEvent ready_fd);
static void ready_output(ErlDrvData fd, ErlDrvEvent ready_fd);
static void output(ErlDrvData fd, char *buf, int len);
static void stop_select(ErlDrvEvent, void*);

struct erl_drv_entry spawn_driver_entry = {
    spawn_init,
    spawn_start,
    stop,
    output,
    ready_input,
    ready_output,
    "spawn",
    NULL, /* finish */
    NULL, /* handle */
    NULL, /* control */
    NULL, /* timeout */
    NULL, /* outputv */
    NULL, /* ready_async */
    NULL, /* flush */
    NULL, /* call */
    NULL, /* event */
    ERL_DRV_EXTENDED_MARKER,
    ERL_DRV_EXTENDED_MAJOR_VERSION,
    ERL_DRV_EXTENDED_MINOR_VERSION,
    0, /* ERL_DRV_FLAGs */
    NULL, /* handle2 */
    NULL, /* process_exit */
    stop_select

};
struct erl_drv_entry fd_driver_entry = {
    NULL,
    fd_start,
    fd_stop,
    output,
    ready_input,
    ready_output,
    "fd",
    NULL, /* finish */
    NULL, /* handle */
    NULL, /* control */
    NULL, /* timeout */
    NULL, /* outputv */
    NULL, /* ready_async */
    NULL, /* flush */
    NULL, /* call */
    NULL, /* event */
    ERL_DRV_EXTENDED_MARKER,
    ERL_DRV_EXTENDED_MAJOR_VERSION,
    ERL_DRV_EXTENDED_MINOR_VERSION,
    0, /* ERL_DRV_FLAGs */
    NULL, /* handle2 */
    NULL, /* process_exit */
    stop_select
};
struct erl_drv_entry vanilla_driver_entry = {
    NULL,
    vanilla_start,
    stop,
    output,
    ready_input,
    ready_output,
    "vanilla",
    NULL, /* finish */
    NULL, /* handle */
    NULL, /* control */
    NULL, /* timeout */
    NULL, /* outputv */
    NULL, /* ready_async */
    NULL, /* flush */
    NULL, /* call */
    NULL, /* event */
    ERL_DRV_EXTENDED_MARKER,
    ERL_DRV_EXTENDED_MAJOR_VERSION,
    ERL_DRV_EXTENDED_MINOR_VERSION,
    0, /* ERL_DRV_FLAGs */
    NULL, /* handle2 */
    NULL, /* process_exit */
    stop_select
};

/*
** Set up enough of the driver_data structure to be able to report exit status.
** Some things may be initiated again, but that is no real problem.
*/
static int pre_set_driver_data(int ifd, int ofd, 
			       int read_write, int report_exit) {
  if (read_write & DO_READ) {
    driver_data[ifd].report_exit = report_exit;
    driver_data[ifd].exitcode = 0;
    driver_data[ifd].exit_reported = 0;
    if (read_write & DO_WRITE) {
      driver_data[ifd].ofd = ofd;
      if (ifd != ofd) {
	driver_data[ofd] = driver_data[ifd];
	driver_data[ofd].report_exit = 0;
      }
    } else {			/* DO_READ only */
      driver_data[ifd].ofd = -1;
    }
    return(ifd);
  } else {			/* DO_WRITE only */
    driver_data[ofd].report_exit = 0;
    driver_data[ofd].exitcode = 0;
    driver_data[ofd].exit_reported = 0;
    driver_data[ofd].ofd = ofd;
    return(ofd);
  }
}
    
/*
** Set up the driver_data structure, it may have been initiated
** partly by the function above, but we dont care. 
*/
static int set_driver_data(int port_num, int ifd, int ofd, 
			   int packet_bytes, int read_write,
			   int report_exit)
{
  if (read_write & DO_READ) {
    driver_data[ifd].packet_bytes = packet_bytes;
    driver_data[ifd].port_num = port_num;
    driver_data[ifd].report_exit = report_exit;
    if (read_write & DO_WRITE) {
      driver_data[ifd].ofd = ofd;
      if (ifd != ofd) {
	driver_data[ofd] = driver_data[ifd];
	driver_data[ofd].report_exit = 0;
      }
    } else {			/* DO_READ only */
      driver_data[ifd].ofd = -1;
    }
    (void) driver_select(port_num, ifd, ERL_DRV_READ|ERL_DRV_USE, 1);
    return(ifd);
  } else {			/* DO_WRITE only */
    driver_data[ofd].packet_bytes = packet_bytes;
    driver_data[ofd].port_num = port_num;
    driver_data[ofd].report_exit = 0;
    driver_data[ofd].ofd = ofd;
    return(ofd);
  }
}

static int need_new_sems = 1;

static int spawn_init(void)
{
  char *stackenv;
  int size;
  driver_data = (struct driver_data *)
      erts_alloc(ERTS_ALC_T_DRV_TAB, max_files * sizeof(struct driver_data));
  if (need_new_sems) {
      driver_data_sem = semMCreate
	  (SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
      entry_data_sem = semMCreate
	  (SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
  }
  if (driver_data_sem == NULL || entry_data_sem == NULL) {
      erl_exit(1,"Could not allocate driver locking semaphore.");
  }
  need_new_sems = 0;

  (void)uxPipeDrv();		/* Install pipe driver */

  if ((stackenv = getenv("ERLPORTSTACKSIZE")) != NULL &&
      (size = atoi(stackenv)) > 0)
    port_stack_size = size;
  else
    port_stack_size = DEFAULT_PORT_STACK_SIZE;
  return 0;
}

/* Argv has to be built vith the save_xxx routines, not with whathever 
   sys_xxx2 has in mind... */
#define argv_alloc save_malloc
#define argv_realloc save_realloc
#define argv_free save_free
/* Build argv, return argc or -1 on failure */
static int build_argv(char *name, char ***argvp)
{
  int argvsize = 10, argc = 0;
  char *args, *arglast = NULL, *argp;
  char **argv;

#ifdef DEBUG
  fdprintf(2, "Building argv, %s =>\n", name);
#endif
  if ((argv = (char **)argv_alloc(argvsize * sizeof(char *))) == NULL)
    return(-1);
  if ((args = argv_alloc(strlen(name) + 1)) == NULL)
    return(-1);
  strcpy(args, name);
  argp = strtok_r(args, " \t", &arglast);
  while (argp != NULL) {
    if (argc + 1 >= argvsize) {
      argvsize += 10;
      argv = (char **)argv_realloc((char *)argv, argvsize*sizeof(char *));
      if (argv == NULL) {
	argv_free(args);
	return(-1);
      }
    }
#ifdef DEBUG
  fdprintf(2, "%s\n", argp);
#endif
    argv[argc++] = argp;
    argp = strtok_r((char *)NULL, " \t", &arglast);
  }
  argv[argc] = NULL;
  *argvp = argv;
  return(argc);
}
#undef argv_alloc
#undef argv_realloc
#undef argv_free


/* Lookup and return global text symbol or NULL on failure
   Symbol name is null-terminated and without the leading '_' */
static FUNCPTR
lookup(char *sym)
{
  char buf[256];
  char *symname = buf;
  int len;
  FUNCPTR entry;
  SYM_TYPE type;

  len = strlen(sym);
  if (len > 254 && (symname = malloc(len+2)) == NULL)
    return(NULL);
#if defined _ARCH_PPC || defined SIMSPARCSOLARIS
  /* GCC for PPC and SIMSPARC doesn't add a leading _ to symbols */
  strcpy(symname, sym);
#else
  sprintf(symname, "_%s", sym);
#endif
  if (symFindByNameAndType(sysSymTbl, symname, (char **)&entry,
			   &type, N_EXT | N_TEXT, N_EXT | N_TEXT) != OK)
    entry = NULL;
  if (symname != buf)
    free(symname);
  return(entry);
}

/* This function is spawned to build argc, argv, lookup the symbol to call,
   connect and set up file descriptors, and make the actual call.
   N.B. 'name' was allocated by the Erlang task (through plain_malloc) and
   is freed by this port program task.
   Note: 'name' may be a path containing '/'. */

static void call_proc(char *name, int ifd, int ofd, int read_write, 
		      int redir_stderr, int driver_index, 
		      int p6, int p7, int p8, int p9)
{
  int argc;
  char **argv, *bname;
  FUNCPTR entry;
  int ret = -1;

  /* Must consume 'name' */
  argc = build_argv(name, &argv);
  plain_free(name);
  /* Find basename of path */
  if ((bname = strrchr(argv[0], '/')) != NULL) {
    bname++;
  } else {
    bname = argv[0];
  }
#ifdef DEBUG
  fdprintf(2, "Port program name: %s\n", bname);
#endif
  semTake(entry_data_sem, WAIT_FOREVER);

  if (argc > 0) {
    if ((entry = lookup(bname)) == NULL) {
      int fd;
      char *fn;
      /* NOTE: We don't check the return value of loadModule,
	 since that was incompatibly changed from 5.0.2b to 5.1,
	 but rather do a repeated lookup().   */
      if ((fd = open(argv[0], O_RDONLY)) > 0) {
	(void) loadModule(fd, GLOBAL_SYMBOLS);
	close(fd);
	entry = lookup(bname);
      }
      if (entry == NULL) {
	/* filename == func failed, try func.o */
	if ((fn = malloc(strlen(argv[0]) + 3)) != NULL) { /* ".o\0" */
	  strcpy(fn, argv[0]);
	  strcat(fn, ".o");
	  if ((fd = open(fn, O_RDONLY)) > 0) {
	    (void) loadModule(fd, GLOBAL_SYMBOLS);
	    close(fd);
	    entry = lookup(bname);
	  }
	  free(fn);
	}
      }
    }
  } else {
    entry = NULL;
  }
  semGive(entry_data_sem);
    
  if (read_write & DO_READ) {	/* emulator read */
    save_fd(ofd);
    ioTaskStdSet(0, 1, ofd);	/* stdout for process */
    if(redir_stderr)
	ioTaskStdSet(0, 2, ofd);/* stderr for process */
  }
  if (read_write & DO_WRITE) { /* emulator write */
    save_fd(ifd);
    ioTaskStdSet(0, 0, ifd);	/* stdin for process */
  }
  if (entry != NULL) {
    ret = (*entry)(argc, argv, (char **)NULL); /* NULL for envp */
  } else {
    fdprintf(2, "Could not exec \"%s\"\n", argv[0]);
    ret = -1;
  }
  if (driver_data[driver_index].report_exit) {
      semTake(driver_data_sem, WAIT_FOREVER);
      driver_data[driver_index].exitcode = ret;
      driver_data[driver_index].exit_reported = 1;
      semGive(driver_data_sem);
  }
  /* We *don't* want to close the pipes here, but let the delete
     hook take care of it - it might want to flush stdout and there'd
     better be an open descriptor to flush to...   */
  exit(ret);
}

static void close_pipes(int ifd[2], int ofd[2], int read_write)
{
  if (read_write & DO_READ) {
    (void) close(ifd[0]);
    (void) close(ifd[1]);
  }
  if (read_write & DO_WRITE) {
    (void) close(ofd[0]);
    (void) close(ofd[1]);
  }
}

static void init_fd_data(int fd, int port_unused_argument)
{
  SET_NONBLOCKING(fd);
  fd_data[fd].pending = NULL;
  fd_data[fd].buf = fd_data[fd].cpos = NULL;
  fd_data[fd].remain = fd_data[fd].sz = 0;
}

static ErlDrvData spawn_start(ErlDrvPort port_num, char *name,SysDriverOpts* opts)
{
    int ifd[2], ofd[2], len, nl, id;
    char taskname[11], *progname, *bname;
    char *space_in_command;
    int packet_bytes = opts->packet_bytes;
    int read_write = opts->read_write;
    int use_stdio = opts->use_stdio;
    int redir_stderr = opts->redir_stderr;
    int driver_index;

    if (!use_stdio){
	return (ErlDrvData) -3;
  }

    /* Create pipes and set the Erlang task as owner of its 
     * read and write ends (through save_fd()).
     */
    switch (read_write) {
    case DO_READ:
	if (pipe(ifd) < 0){
	    return (ErlDrvData) -2;
	}
	if (ifd[0] >= max_files) {
	    close_pipes(ifd, ofd, read_write);
	    errno = ENFILE;
	    return (ErlDrvData) -2;
	}
	save_fd(ifd[0]);
	break;
    case DO_WRITE:
	if (pipe(ofd) < 0) {
	    return (ErlDrvData) -2;
	}
	if (ofd[1] >= max_files) {
	    close_pipes(ifd, ofd, read_write);
	    errno = ENFILE;
	    return (ErlDrvData) -2;
	}
	save_fd(ofd[1]);
	break;
    case DO_READ|DO_WRITE:
	if (pipe(ifd) < 0){
	    return (ErlDrvData) -2;
	}
	if (ifd[0] >= max_files || pipe(ofd) < 0) {
	    close_pipes(ifd, ofd, DO_READ);
	    errno = ENFILE;
	    return (ErlDrvData) -2;
	}
	if (ofd[1] >= max_files) {
	    close_pipes(ifd, ofd, read_write);
	    errno = ENFILE;
	    return (ErlDrvData) -2;
	}
	save_fd(ifd[0]);
	save_fd(ofd[1]);
	break;
    default:
	return (ErlDrvData) -1;
    }

    /* Allocate space for program name to be freed by the
     * spawned task. We use plain_malloc so that the allocated
     * space is not owned by the Erlang task.
     */

    if ((progname = plain_malloc(strlen(name) + 1)) == NULL) {
	close_pipes(ifd, ofd, read_write);
	errno = ENOMEM;
	return (ErlDrvData) -2;
    }
    strcpy(progname, name);

    /* Check if name contains a space 
     *  (e.g "port_test -o/home/gandalf/tornado/wind/target/erlang")
     */
    if ((space_in_command = strrchr(progname, ' ')) != NULL) {
	*space_in_command = '\0';
    }

    /* resulting in "port_test" */
    if ((bname = strrchr(progname, '/')) != NULL)
	bname++;
    else
	bname = progname;

    /* resulting in "port_test" */
    len = strlen(bname);
    nl = len > 10 ? 10 : len;
    strncpy(taskname, bname, nl);
    taskname[nl] = '\0';
    if (space_in_command != NULL) 
	*space_in_command = ' ';
    driver_index = pre_set_driver_data(ifd[0], ofd[1], 
				       read_write, opts->exit_status);

  /* resetting to "port_test -o/home/gandalf/tornado/wind/target/erlang" */
    if ((id = taskSpawn(taskname, spTaskPriority, spTaskOptions,
			port_stack_size, (FUNCPTR)call_proc, (int)progname,
			ofd[0], ifd[1], read_write, redir_stderr, driver_index,
			0,0,0,0))
	== ERROR) {
	close_pipes(ifd, ofd, read_write);
	plain_free(progname);	/* only when spawn fails */
	errno = ENOMEM;
	return (ErlDrvData) -2;
    }    
#ifdef DEBUG
    fdprintf(2, "Spawned %s as %s[0x%x]\n", name, taskname, id);
#endif
    if (read_write & DO_READ) 
	init_fd_data(ifd[0], port_num);
    if (read_write & DO_WRITE) 
	init_fd_data(ofd[1], port_num);
    return (ErlDrvData) (set_driver_data(port_num, ifd[0], ofd[1],
					 packet_bytes,read_write,
					 opts->exit_status));
}

static ErlDrvData fd_start(ErlDrvPort port_num, char *name, SysDriverOpts* opts)
{
    if (((opts->read_write & DO_READ) && opts->ifd >= max_files) ||
	((opts->read_write & DO_WRITE) && opts->ofd >= max_files)) {
	return (ErlDrvData) -1;
    }

    if (opts->read_write & DO_READ) 
	init_fd_data(opts->ifd, port_num);
    if (opts->read_write & DO_WRITE) 
	init_fd_data(opts->ofd, port_num);
    return (ErlDrvData) (set_driver_data(port_num, opts->ifd, opts->ofd,
					 opts->packet_bytes, opts->read_write, 0));
}

static void clear_fd_data(int fd) 
{
    
  if (fd_data[fd].sz > 0) 
    erts_free(ERTS_ALC_T_FD_ENTRY_BUF, (void *) fd_data[fd].buf);
  fd_data[fd].buf = NULL;
  fd_data[fd].sz = 0;
  fd_data[fd].remain = 0;
  fd_data[fd].cpos = NULL;
}

static void nbio_stop_fd(int port_num, int fd)
{
  Pend *p, *p1;
    
  driver_select(port_num, fd, ERL_DRV_READ|ERL_DRV_WRITE, 0);
  clear_fd_data(fd);
  p = fd_data[fd].pending;
  SET_BLOCKING(fd);
  while (p) {
    p1 = p->next;
    free(p);
    p = p1;
  }
  fd_data[fd].pending = NULL;
}

static void fd_stop(ErlDrvData drv_data)
{
    int ofd;
    int fd = (int) drv_data;

    nbio_stop_fd(driver_data[fd].port_num, (int)fd);
    ofd = driver_data[fd].ofd;
    if (ofd != fd && ofd != -1) 
	nbio_stop_fd(driver_data[fd].port_num, (int)ofd); /* XXX fd = ofd? */
}

static ErlDrvData
vanilla_start(ErlDrvPort port_num, char *name, SysDriverOpts* opts)
{
    int flags, fd;
    struct stat statbuf;

    DEBUGF(("vanilla_start, name: %s [r=%1i w=%1i]\n", name, 
	    opts->read_write & DO_READ, 
	    opts->read_write & DO_WRITE));
  
    flags = (opts->read_write == DO_READ ? O_RDONLY :
	     opts->read_write == DO_WRITE ? O_WRONLY|O_CREAT|O_TRUNC :
	     O_RDWR|O_CREAT);
    if ((fd = open(name, flags, 0666)) < 0){
	errno = ENFILE;
	return (ErlDrvData) -2;
    }
    if (fd >= max_files) {
	close(fd);
	errno = ENFILE;
	return (ErlDrvData) -2;
    }
    if (fstat(fd, &statbuf) < 0) {
	close(fd);
	errno = ENFILE;
	return (ErlDrvData) -2;
    }

    /* Return error for reading regular files (doesn't work) */
    if (ISREG(statbuf) && ((opts->read_write) & DO_READ)) {
	close(fd);
	return (ErlDrvData) -3;
    }
    init_fd_data(fd, port_num);
    return (ErlDrvData) (set_driver_data(port_num, fd, fd,
					 opts->packet_bytes, opts->read_write, 0));
}

/* Note that driver_data[fd].ifd == fd if the port was opened for reading, */
/* otherwise (i.e. write only) driver_data[fd].ofd = fd.  */

static void stop(ErlDrvData drv_data)
{
    int port_num, ofd;
    int fd = (int) drv_data;

    port_num = driver_data[fd].port_num;
    nbio_stop_fd(port_num, fd);
    driver_select(port_num, fd, ERL_DRV_USE, 0); /* close(fd) */

    ofd = driver_data[fd].ofd;
    if (ofd != fd && ofd != -1) {
	nbio_stop_fd(port_num, ofd);
	driver_select(port_num, ofd, ERL_DRV_USE, 0); /* close(fd) */
    }
}

static int sched_write(int port_num,int fd, char *buf, int len, int pb)
{
  Pend *p, *p2, *p3;
  int p_bytes = len;

  p = (Pend*) erts_alloc_fnf(ERTS_ALC_T_PEND_DATA, pb + len + sizeof(Pend));
  if (!p) {
    driver_failure(port_num, -1);
    return(-1);
  }

  switch(pb) {
  case 4: put_int32(len, p->buf); break;
  case 2: put_int16(len, p->buf); break;
  case 1: put_int8(len, p->buf); break;
  case 0: break;		/* Handles this case too */
  }
  sys_memcpy(p->buf + pb, buf, len);
  driver_select(port_num, fd, ERL_DRV_WRITE|ERL_DRV_USE, 1);
  p->cpos = p->buf;
  p->fd = fd;
  p->next = NULL;
  p->remain = len + pb;
  p2 = fd_data[fd].pending;
  if (p2 == NULL) 
    fd_data[fd].pending = p;
  else {
    p3 = p2->next;
    while(p3) {
      p_bytes += p2->remain;
      p2 = p2->next;
      p3 = p3->next;
    }
    p2->next = p;
  }
  if (p_bytes > (1 << 13))	/* More than 8 k pending */
    set_busy_port(port_num, 1);
  return(0);
}

/* Fd is the value returned as drv_data by the start func */
static void output(ErlDrvData drv_data, char *buf, int len)
{
    int buf_done, port_num, wval, pb, ofd;
    byte lb[4];
    struct iovec iv[2];
    int fd = (int) drv_data;

    pb = driver_data[fd].packet_bytes;
    port_num = driver_data[fd].port_num;

    if ((ofd = driver_data[fd].ofd) == -1) {
	return;
    }

    if (fd_data[ofd].pending) {
	sched_write(port_num, ofd, buf, len, pb);
	return;
    }
    
    if ((pb == 2 && len > 65535) || (pb == 1 && len > 255)) {
	driver_failure_posix(port_num, EINVAL);
	return;
    }
    if (pb == 0) {
	wval = write(ofd, buf, len);
    } else {
	lb[0] = (len >> 24) & 255;	/* MSB */
	lb[1] = (len >> 16) & 255;
	lb[2] = (len >> 8) & 255;
	lb[3] = len & 255;		/* LSB */
	iv[0].iov_base = (char*) lb + (4 - pb);
	iv[0].iov_len = pb;
	iv[1].iov_base = buf;
	iv[1].iov_len = len;
	wval = writev(ofd, iv, 2);
    }
    if (wval == pb + len ) {
	return;
    }
    if (wval < 0) {
	if ((errno == EINTR) || (errno == ERRNO_BLOCK)) {
	    if (pb) {
		sched_write(port_num, ofd, buf ,len, pb);
	    } else if (pb == 0) {
		sched_write(port_num, ofd, buf ,len, 0);
	    }
	    return;
	}
	driver_failure_posix(driver_data[fd].port_num, EINVAL);
	return;
    }
    if (wval < pb) {
	sched_write(port_num, ofd, (lb +4 -pb)  + wval, pb-wval, 0);
	sched_write(port_num, ofd, buf ,len, 0);
	return;
    }

    /* we now know that  wval < (pb + len) */
    buf_done = wval - pb;
    sched_write(port_num, ofd, buf + buf_done, len - buf_done,0);
}

static void stop_select(ErlDrvEvent fd, void* _)
{
    close((int)fd);
}

static int ensure_header(int fd,char *buf,int packet_size, int sofar)
{
  int res = 0;
  int remaining = packet_size - sofar;

  SET_BLOCKING(fd);
  if (read_fill(fd, buf+sofar, remaining) != remaining)
    return -1;
  switch (packet_size) {
  case 1: res = get_int8(buf); break;
  case 2: res = get_int16(buf); break;
  case 4: res = get_int32(buf); break;
  }
  SET_NONBLOCKING(fd);
  return(res);
}

static int port_inp_failure(int port_num, int ready_fd, int res)
{
    (void) driver_select(port_num, ready_fd, ERL_DRV_READ|ERL_DRV_WRITE, 0); 
    clear_fd_data(ready_fd);
    if (res == 0) {
	if (driver_data[ready_fd].report_exit) {
	    int tmpexit = 0;
	    int reported;
	    /* Lock the driver_data structure */
	    semTake(driver_data_sem, WAIT_FOREVER);
	    if ((reported = driver_data[ready_fd].exit_reported))
		tmpexit = driver_data[ready_fd].exitcode;
	    semGive(driver_data_sem);
	    if (reported) {
		erts_fprintf(stderr,"Exitcode %d reported\r\n", tmpexit);
		driver_report_exit(port_num, tmpexit);
	    }
	}
	driver_failure_eof(port_num);
    } else {
	driver_failure(port_num, res);
    }
    return 0;
}

/* fd is the drv_data that is returned from the */
/* initial start routine                        */
/* ready_fd is the descriptor that is ready to read */

static void ready_input(ErlDrvData drv_data, ErlDrvEvent drv_event)
{
  int port_num, packet_bytes, res;
  Uint h = 0;
  char *buf;
  int fd = (int) drv_data;
  int ready_fd = (int) drv_event;

  port_num = driver_data[fd].port_num;
  packet_bytes = driver_data[fd].packet_bytes;

  if (packet_bytes == 0) {
    if ((res = read(ready_fd, tmp_buf, tmp_buf_size)) > 0) {
      driver_output(port_num, (char*)tmp_buf, res);
      return;
    }
    port_inp_failure(port_num, ready_fd, res);
    return;
  }

  if (fd_data[ready_fd].remain > 0) { /* We try to read the remainder */
    /* space is allocated in buf */
    res = read(ready_fd, fd_data[ready_fd].cpos, 
	       fd_data[ready_fd].remain);
    if (res < 0) {
      if ((errno == EINTR) || (errno == ERRNO_BLOCK)) {
	  ;
      } else {
	  port_inp_failure(port_num, ready_fd, res);
      }
    } else if (res == 0) { 
	port_inp_failure(port_num, ready_fd, res);
    } else if (res == fd_data[ready_fd].remain) { /* we're done  */
	driver_output(port_num, fd_data[ready_fd].buf, 
		      fd_data[ready_fd].sz);
	clear_fd_data(ready_fd);
    } else {			/*  if (res < fd_data[ready_fd].remain) */
	fd_data[ready_fd].cpos += res;
	fd_data[ready_fd].remain -= res;
    }
    return;
  }


  if (fd_data[ready_fd].remain == 0) { /* clean fd */
    /* We make one read attempt and see what happens */
    res = read(ready_fd, tmp_buf, tmp_buf_size);
    if (res < 0) {  
      if ((errno == EINTR) || (errno == ERRNO_BLOCK))
	return;
      port_inp_failure(port_num, ready_fd, res);
      return;
    }
    else if (res == 0) {		/* eof */
      port_inp_failure(port_num, ready_fd, res);
      return;
    }
    else if (res < packet_bytes) { /* Ugly case... get at least */
      if ((h = ensure_header(ready_fd, tmp_buf, packet_bytes, res))==-1) {
	port_inp_failure(port_num, ready_fd, -1);
	return;
      }
      buf = erts_alloc_fnf(ERTS_ALC_T_FD_ENTRY_BUF, h);
      if (!buf) {
	port_inp_failure(port_num, ready_fd, -1);
	return;
      }
      fd_data[ready_fd].buf = buf;
      fd_data[ready_fd].sz = h;
      fd_data[ready_fd].remain = h;
      fd_data[ready_fd].cpos = buf;
      return;
    }
    else  {			/* if (res >= packet_bytes) */
      unsigned char* cpos = tmp_buf;
      int bytes_left = res;
      while (1) {		/* driver_output as many as possible */
	  if (bytes_left == 0) {
	      clear_fd_data(ready_fd);
	      return;
	  }
	  if (bytes_left < packet_bytes) { /* Yet an ugly case */
	      if((h=ensure_header(ready_fd, cpos, 
				  packet_bytes, bytes_left))==-1) {
		  port_inp_failure(port_num, ready_fd, -1);
		  return;
	      }
	      buf = erts_alloc_fnf(ERTS_ALC_T_FD_ENTRY_BUF, h);
	      if (!buf) 
		  port_inp_failure(port_num, ready_fd, -1);
	      fd_data[ready_fd].buf = buf;
	      fd_data[ready_fd].sz = h;
	      fd_data[ready_fd].remain = h;
	      fd_data[ready_fd].cpos = buf;
	      return;
	  }
	  switch (packet_bytes) {
	  case 1: h = get_int8(cpos); cpos += 1; break;
	  case 2: h = get_int16(cpos); cpos += 2; break;
	  case 4: h = get_int32(cpos); cpos += 4; break;
	  }
	  bytes_left -= packet_bytes;
	  /* we've got the header, now check if we've got the data */
	  if (h <= (bytes_left)) {
	      driver_output(port_num, (char*) cpos, h);
	      cpos += h;
	      bytes_left -= h;
	      continue;
	  }
	  else {			/* The last message we got was split */
	      buf = erts_alloc_fnf(ERTS_ALC_T_FD_ENTRY_BUF, h);
	      if (!buf) {
		  port_inp_failure(port_num, ready_fd, -1);
	      }
	      sys_memcpy(buf, cpos, bytes_left);
	      fd_data[ready_fd].buf = buf;
	      fd_data[ready_fd].sz = h;
	      fd_data[ready_fd].remain = h - bytes_left;
	      fd_data[ready_fd].cpos = buf + bytes_left;
	      return;
	  }
      }
      return;
    }
  }
  fprintf(stderr, "remain %d \n", fd_data[ready_fd].remain);
  port_inp_failure(port_num, ready_fd, -1);
}


/* fd is the drv_data that is returned from the */
/* initial start routine                        */
/* ready_fd is the descriptor that is ready to read */

static void ready_output(ErlDrvData drv_data, ErlDrvEvent drv_event)
{
  Pend *p;
  int wval;

  int fd = (int) drv_data;
  int ready_fd = (int) drv_event;

  while(1) {
    if ((p = fd_data[ready_fd].pending) == NULL) {
      driver_select(driver_data[fd].port_num, ready_fd, 
		    ERL_DRV_WRITE, 0);
      return;
    }
    wval = write(p->fd, p->cpos, p->remain);
    if (wval == p->remain) {
      fd_data[ready_fd].pending = p->next;
      erts_free(ERTS_ALC_T_PEND_DATA, p);
      if (fd_data[ready_fd].pending == NULL) {
	driver_select(driver_data[fd].port_num, ready_fd, 
		      ERL_DRV_WRITE, 0);
	set_busy_port(driver_data[fd].port_num, 0);
	return;
      }
      else
	continue;
    }
    else if (wval < 0) {
      if (errno == ERRNO_BLOCK || errno == EINTR)
	return;
      else {
	driver_select(driver_data[fd].port_num, ready_fd, 
		      ERL_DRV_WRITE, 0);
	driver_failure(driver_data[fd].port_num, -1);
	return;
      }
    }
    else if (wval < p->remain) {
      p->cpos += wval;
      p->remain -= wval;
      return;
    }
  }
}

/* Fills in the systems representation of the jam/beam process identifier.
** The Pid is put in STRING representation in the supplied buffer,
** no interpretatione of this should be done by the rest of the
** emulator. The buffer should be at least 21 bytes long.
*/
void sys_get_pid(char *buffer){
    int p = taskIdSelf(); /* Hmm, may be negative??? requires some GB of
			     memory to make the TCB address convert to a
			     negative value. */
    sprintf(buffer,"%d", p);
}

int
erts_sys_putenv(char *buffer, int sep_ix)
{
    return putenv(buffer);
}

int
erts_sys_getenv(char *key, char *value, size_t *size)
{
    char *orig_value;
    int res;
    orig_value = getenv(key);
    if (!orig_value)
	res = -1;
    else {
	size_t len = sys_strlen(orig_value);
	if (len >= *size) {
	    *size = len + 1;
	    res = 1;
	}
	else {
	    *size = len;
	    sys_memcpy((void *) value, (void *) orig_value, len+1);
	    res = 0;
	}
    }
    return res;
}

void
sys_init_io(void)
{
  tmp_buf = (byte *) erts_alloc(ERTS_ALC_T_SYS_TMP_BUF, SYS_TMP_BUF_SIZE);
  tmp_buf_size = SYS_TMP_BUF_SIZE;
  fd_data = (struct fd_data *)
      erts_alloc(ERTS_ALC_T_FD_TAB, max_files * sizeof(struct fd_data));
}


/* Fill buffer, return buffer length, 0 for EOF, < 0 for error. */

static int read_fill(int fd, char *buf, int len)
{
  int i, got = 0;
  do {
    if ((i = read(fd, buf+got, len-got)) <= 0) {
      return i;
    }
    got += i;
  } while (got < len);
  return (len);
}	


/************************** Misc... *******************************/

extern const char pre_loaded_code[];
extern char* const pre_loaded[];


/* Float conversion */

int sys_chars_to_double(char *buf, double *fp)
{
  char *s = buf;

  /* The following check is incorporated from the Vee machine */
    
#define ISDIGIT(d) ((d) >= '0' && (d) <= '9')

  /* Robert says that something like this is what he really wanted:
   *
   * 7 == sscanf(Tbuf, "%[+-]%[0-9].%[0-9]%[eE]%[+-]%[0-9]%s", ....);
   * if (*s2 == 0 || *s3 == 0 || *s4 == 0 || *s6 == 0 || *s7)
   *   break;
   */

  /* Scan string to check syntax. */
  if (*s == '+' || *s == '-')
    s++;
	    
  if (!ISDIGIT(*s))		/* Leading digits. */
    return -1;
  while (ISDIGIT(*s)) s++;
  if (*s++ != '.')		/* Decimal part. */
    return -1;
  if (!ISDIGIT(*s))
    return -1;
  while (ISDIGIT(*s)) s++;
  if (*s == 'e' || *s == 'E') {
    /* There is an exponent. */
    s++;
    if (*s == '+' || *s == '-')
      s++;
    if (!ISDIGIT(*s))
      return -1;
    while (ISDIGIT(*s)) s++;
  }
  if (*s)			/* That should be it */
    return -1;
    
  if (sscanf(buf, "%lf", fp) != 1)
    return -1;
  return 0;
}

/* 
 ** Convert a double to ascii format 0.dddde[+|-]ddd
 ** return number of characters converted
 */

int sys_double_to_chars(double fp, char *buf)
{
  (void) sprintf(buf, "%.20e", fp);
  return strlen(buf);
}


/* Floating point exceptions */

#if (CPU == SPARC)
jmp_buf fpe_jmp;

RETSIGTYPE fpe_sig_handler(int sig)
{
  longjmp(fpe_jmp, 1);
}

#elif (CPU == PPC603)
static void fix_registers(void){
    FP_CONTEXT fpcontext;
    fppSave(&fpcontext);
    fpcontext.fpcsr &= ~(_PPC_FPSCR_INIT);
    fppRestore(&fpcontext);
}
#endif


/* Return a pointer to a vector of names of preloaded modules */

Preload* sys_preloaded(void)
{
    return (Preload *) pre_loaded;
}

/* Return a pointer to preloaded code for module "module" */
unsigned char* sys_preload_begin(Preload *pp)
{
    return pp->code;
}

/* Clean up if allocated */
void sys_preload_end(Preload *pp)
{
    /* Nothing */
}

/* Read a key from console (?) */

int sys_get_key(int fd)
{
  int c;
  unsigned char rbuf[64];

  fflush(stdout);		/* Flush query ??? */

  if ((c = read(fd,rbuf,64)) <= 0)
    return c;
  return rbuf[0];
}


/* A real printf that does the equivalent of fprintf(stdout, ...) */

/* ARGSUSED */
static STATUS
stdio_write(char *buf, int nchars, int fp)
{
  if (fwrite(buf, sizeof(char), nchars, (FILE *)fp) == 0)
    return(ERROR);
  return(OK);
}

int real_printf(const char *fmt, ...)
{
  va_list ap;
  int err;
  
  va_start(ap, fmt);
  err = fioFormatV(fmt, ap, stdio_write, (int)stdout);
  va_end(ap);
  return(err);
}
	
	
/* 
 * Little function to do argc, argv calls from (e.g.) VxWorks shell
 *  The arguments should be in the form of a single ""-enclosed string
 *  NOTE: This isn't really part of the emulator, just included here
 *  so we can use the handy functions and memory reclamation.
 */
void argcall(char *args)
{
  int argc;
  char **argv;
  FUNCPTR entry;

  if (args != NULL) {
    if ((argc = build_argv(args, &argv)) > 0) {
      if ((entry = lookup(argv[0])) != NULL)
	(*entry)(argc, argv, (char **)NULL); /* NULL for envp */
      else
	fprintf(stderr, "Couldn't find %s\n", argv[0]);
    } else
      fprintf(stderr, "Failed to build argv!\n");
  } else
    fprintf(stderr, "No argument list!\n");
}


/* That concludes the Erlang stuff - now we just need to implement an OS...
   - Just kidding, but resource reclamation isn't the strength of VxWorks */
#undef calloc
#undef free
#undef cfree
#undef malloc
#undef realloc
#undef open
#undef creat
#undef socket
#undef accept
#undef close
#undef fopen
#undef fdopen
#undef freopen
#undef fclose

/********************* Using elib_malloc ****************************/
/* This gives us yet another level of malloc wrappers. The purpouse */
/* is to be able to select between different varieties of memory    */
/* allocation without recompiling.                                  */
/* Maybe the performance is somewhat degraded by this, but          */
/* on the other hand, performance may be much better if the most    */
/* suiting malloc is used (not to mention the much lower            */
/* fragmentation).                                                  */
/* /Patrik N                                                        */
/********************************************************************/

/* 
 * I don't want to include the whole elib header, especially
 * as it uses char * for generic pointers. Let's fool ANSI C instead.
 */
extern void *elib_malloc(size_t);
extern void *elib_realloc(void *, size_t);
extern void elib_free(void *);
extern void elib_init(void *, int);
extern void elib_force_init(void *, int);
extern size_t elib_sizeof(void *);

/* Flags */
#define USING_ELIB_MALLOC         1   /* We are using the elib_malloc */
#define WARN_MALLOC_MIX           2   /* Warn if plain malloc or save_malloc
					 is mixed with sys_free2 or 
					 sys_realloc2 */
#define REALLOC_MOVES             4   /* Always move on realloc 
					 (less fragmentation) */
#define USER_POOL                 8   /* The user supplied the memory
					 pool, it was not save_alloced. */
#define RECLAIM_USER_POOL        16   /* Use the reclaim mechanism in the
					 user pool. */
#define NEW_USER_POOL            32   /* The user pool is newly suppllied,
					 any old pool should be discarded */


#define ELIB_LOCK \
if(alloc_flags & USING_ELIB_MALLOC) \
    semTake(elib_malloc_sem, WAIT_FOREVER)

#define ELIB_UNLOCK \
if(alloc_flags & USING_ELIB_MALLOC) \
    semGive(elib_malloc_sem)

#define USER_RECLAIM() ((alloc_flags & USING_ELIB_MALLOC) && \
			(alloc_flags & USER_POOL) && \
			(alloc_flags & RECLAIM_USER_POOL))

/*
 * Global state
 * The use of function pointers for the malloc/realloc/free functions
 * is actually only useful in the malloc case, we must know what kind of
 * realloc/free we are going to use, so we could call elib_xxx directly.
 * However, as the overhead is small and this construction makes it 
 * fairly easy to add another malloc algorithm, the function pointers
 * are used in realloc/free to.
 */
static MallocFunction actual_alloc = &save_malloc;
static ReallocFunction actual_realloc = &save_realloc;
static FreeFunction actual_free = &save_free;
static int alloc_flags = 0;
static int alloc_pool_size = 0;
static void *alloc_pool_ptr = NULL;
static SEM_ID elib_malloc_sem = NULL; 

/*
 * Descide if we should use the save_free instead of elib_free or, 
 * in the case of the free used in a delete hook, if we should
 * use plain free instead of elib_free.
 */
static int use_save_free(void *ptr){
    register int diff = ((char *) ptr) - ((char *) alloc_pool_ptr);
    /* 
     * Hmmm... should it be save_free even if diff is exactly 0? 
     * The answer is Yes if the whole area is save_alloced and No if not,
     * so reclaim_free_hook is NOT run in the case of one save_alloced area. 
     */
    return (!(alloc_flags & USING_ELIB_MALLOC) ||
	    (diff < 0 || diff >= alloc_pool_size));
}

/*
 * A free function used by the task deletion hook for the save_xxx functions.
 * Set with the set_reclaim_free_function function.
 */
static void reclaim_free_hook(void *ptr){
    if(use_save_free(ptr)){
	free(ptr);
    } else {
	ELIB_LOCK;
	(*actual_free)(ptr);
	ELIB_UNLOCK;
    }
}
    

/*
 * Initialize, sets the values of pointers based on
 * either nothing (the default) or what's set previously by the
 * erl_set_memory_block function.
 */
static void initialize_allocation(void){
    set_reclaim_free_function(NULL);
    if(alloc_pool_size == 0){
	actual_alloc = (void *(*)(size_t))&save_malloc;
	actual_realloc = (void *(*)(void *, size_t))&save_realloc;
	actual_free = &save_free;
	alloc_flags &= ~(USING_ELIB_MALLOC | USER_POOL | RECLAIM_USER_POOL);
    } else {
	if(elib_malloc_sem == NULL)
	    elib_malloc_sem = semMCreate
		(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
	if(elib_malloc_sem == NULL)
	    erl_exit(1,"Could not create mutex semaphore for elib_malloc");
	if(!(alloc_flags & USER_POOL)){
	    if((alloc_pool_ptr = save_malloc(alloc_pool_size)) == NULL)
		erl_exit(1,"Erlang set to allocate a %d byte block initially;"
			 " not enough memory available.", alloc_pool_size);
	    elib_force_init(alloc_pool_ptr, alloc_pool_size);
	} else if(alloc_flags & NEW_USER_POOL){
	    elib_force_init(alloc_pool_ptr, alloc_pool_size);
	}
	actual_alloc=&elib_malloc;
	actual_realloc=&elib_realloc;
	actual_free=&elib_free;
	alloc_flags |= USING_ELIB_MALLOC;
	/* We MUST see to that the right free function is used
	   otherwise we'll get a very nasty crash! */
	if(USER_RECLAIM())
	    set_reclaim_free_function(&reclaim_free_hook);
    }
    alloc_flags &= ~(NEW_USER_POOL); /* It's never new after initialization*/
}

/* This does not exist on other platforms, we just use it in sys.c 
   and the BSD resolver */
void *sys_calloc2(Uint nelem, Uint elsize){
    void *ptr = erts_alloc_fnf(ERTS_ALC_T_UNDEF, nelem*elsize);
    if(ptr != NULL)
	memset(ptr,0,nelem*elsize);
    return ptr;
}

/*
 * The malloc wrapper
 */
void *
erts_sys_alloc(ErtsAlcType_t type, void *extra, Uint size)
{
    register void *ret;
    ELIB_LOCK;
    if(USER_RECLAIM())
	ret = save_malloc2((size_t)size,actual_alloc);
    else
	ret = (*actual_alloc)((size_t)size);
    ELIB_UNLOCK;
    return ret;
}

/*
 * The realloc wrapper, may respond to the "realloc-always-moves" flag
 * if the area is initially allocated with elib_malloc.
 */
void *
erts_sys_realloc(ErtsAlcType_t type, void *extra, void *ptr, Uint size)
{
    register void *ret;
    if(use_save_free(ptr)){
	if((alloc_flags & WARN_MALLOC_MIX) && 
	   (alloc_flags & USING_ELIB_MALLOC))
	    erts_fprintf(stderr,"Warning, save_malloced data realloced "
		       "by sys_realloc2\n");
	return save_realloc(ptr, (size_t) size);
    } else {	
	ELIB_LOCK;
	if((alloc_flags & REALLOC_MOVES) && 
	   (alloc_flags & USING_ELIB_MALLOC)){
	    size_t osz = elib_sizeof(ptr);
	    if(USER_RECLAIM())
		ret = save_malloc2((size_t) size, actual_alloc);
	    else
		ret = (*actual_alloc)((size_t) size);
	    if(ret != NULL){
		memcpy(ret,ptr,(((size_t)size) < osz) ? ((size_t)size) : osz);
		if(USER_RECLAIM())
		    save_free2(ptr,actual_free);
		else
		    (*actual_free)(ptr);
	    }
	} else {
	    if(USER_RECLAIM())
		ret = save_realloc2(ptr,(size_t)size,actual_realloc);
	    else
		ret = (*actual_realloc)(ptr,(size_t)size);
	}
	ELIB_UNLOCK;
	return ret;
    }
}

/*
 * Wrapped free().
 */
void
erts_sys_free(ErtsAlcType_t type, void *extra, void *ptr)
{
    if(use_save_free(ptr)){
	/* 
	 * This might happen when linked in drivers use save_malloc etc 
	 * directly. 
	 */
	if((alloc_flags & WARN_MALLOC_MIX) && 
	   (alloc_flags & USING_ELIB_MALLOC))
	    erts_fprintf(stderr,"Warning, save_malloced data freed by "
		       "sys_free2\n");
	save_free(ptr);
    } else {
	ELIB_LOCK;
	if(USER_RECLAIM())
	    save_free2(ptr,actual_free);
	else
	    (*actual_free)(ptr);
	ELIB_UNLOCK;
    }
}

/* 
 * External interface to be called before erlang is started 
 * Parameters:
 * isize: The size of the memory block where erlang should malloc().
 * iptr: (optional) A pointer to a user supplied memory block of 
 *       size isize.
 * warn_save: Instructs sys_free2 and sys_realloc2 to warn if
 *            memory allocation/reallocation/freeing is mixed between
 *            pure malloc/save_malloc/sys_alloc2 routines (only
 *            warns if elib is actually used in the sys_alloc2 routines).
 * realloc_moves: Always allocate a fresh memory block on reallocation 
 *                (less fragmentation).
 * reclaim_in_supplied: Use memory reclaim mechanisms inside the user
 *                      supplied area, this makes one area reusable between
 *                      starts of erlang and might be nice for drivers etc.
 */

int erl_set_memory_block(int isize, int iptr, int warn_save, 
			 int realloc_moves, int reclaim_in_supplied, int p5,
			 int p6, int p7, int p8, int p9){
    if(erlang_id != 0){
	erts_fprintf(stderr,"Error, cannot set erlang memory block while an "
		     "erlang task is running!\n");
	return 1;
    }
    if(isize < 8 * 1024 *1024)
	erts_fprintf(stderr,
		     "Warning, the memory pool of %dMb may be to small to "
		     "run erlang in!\n", isize / (1024 * 1024));
    alloc_pool_size = (size_t) isize;
    alloc_pool_ptr = (void *) iptr;
    alloc_flags = 0;
    /* USING_ELIB_MALLOC gets set by the initialization routine */
    if((void *)iptr != NULL)
	alloc_flags |= (USER_POOL | NEW_USER_POOL);
    if(realloc_moves)
	alloc_flags |= REALLOC_MOVES;
    if(warn_save)
	alloc_flags |= WARN_MALLOC_MIX;
    if((void *)iptr != NULL && reclaim_in_supplied)
	alloc_flags |= RECLAIM_USER_POOL;
    return 0;
}

/* External statistics interface */
int erl_memory_show(int p0, int p1, int p2, int p3, int p4, int p5,
		    int p6, int p7, int p8, int p9){
    struct elib_stat statistics;
    if(!(alloc_flags & USING_ELIB_MALLOC) && erlang_id != 0){
	erts_printf("Using plain save_alloc, use memShow instead.\n");
	return 1;
    }
    if(erlang_id == 0 && !((alloc_flags & USER_POOL) && 
			   !(alloc_flags & NEW_USER_POOL))){
	erts_printf("Sorry, no allocation statistics until erlang "
		   "is started.\n");
	return 1;
    }
    erts_printf("Allocation settings:\n");
    erts_printf("Using elib_malloc with memory pool size of %lu bytes.\n",
	       (unsigned long) alloc_pool_size);
    erts_printf("Realloc-always-moves is %s\n", 
	       (alloc_flags & REALLOC_MOVES) ? "on" : "off");
    erts_printf("Warnings about mixed malloc/free's are %s\n", 
	       (alloc_flags & WARN_MALLOC_MIX) ? "on" : "off");
    if(alloc_flags & USER_POOL){
	erts_printf("The memory block used by elib is user supplied "
		   "at 0x%08x.\n", (unsigned int) alloc_pool_ptr);
	if(alloc_flags & RECLAIM_USER_POOL)
	    erts_printf("Allocated memory within the user supplied pool\n"
		       "  will be automatically reclaimed at task exit.\n");
    } else {
	erts_printf("The memory block used by elib is save_malloc'ed "
		   "at 0x%08x.\n", (unsigned int) alloc_pool_ptr);
    }
    erts_printf("Statistics from elib_malloc:\n");
    ELIB_LOCK;

    elib_stat(&statistics);
    ELIB_UNLOCK;
    erts_printf("Type          Size (bytes) Number of blocks\n");
    erts_printf("============= ============ ================\n");
    erts_printf("Total:        %12lu %16lu\n",
	       (unsigned long) statistics.mem_total*4,
	       (unsigned long) statistics.mem_blocks);
    erts_printf("Allocated:    %12lu %16lu\n",
	       (unsigned long) statistics.mem_alloc*4,
	       (unsigned long) statistics.mem_blocks-statistics.free_blocks);
    erts_printf("Free:         %12lu %16lu\n",
	       (unsigned long) statistics.mem_free*4,
	       (unsigned long) statistics.free_blocks);
    erts_printf("Largest free: %12lu                -\n\n",
	       (unsigned long) statistics.max_free*4);
    return 0;
}


/*
** More programmer friendly (as opposed to user friendly ;-) interface
** to the memory statistics. Resembles the VxWorks memPartInfoGet but
** does not take a partition id as parameter...
*/    
int erl_mem_info_get(MEM_PART_STATS *stats){
    struct elib_stat statistics;
    if(!(alloc_flags & USING_ELIB_MALLOC))
	return -1;
    ELIB_LOCK;
    elib_stat(&statistics);
    ELIB_UNLOCK;
    stats->numBytesFree = statistics.mem_free*4;
    stats->numBlocksFree = statistics.free_blocks;
    stats->maxBlockSizeFree = statistics.max_free*4;
    stats->numBytesAlloc = statistics.mem_alloc*4;
    stats->numBlocksAlloc = statistics.mem_blocks-statistics.free_blocks;
    return 0;
}

/********************* Pipe driver **********************************/
/*
 * Purpose:  Pipe driver with Unix (unnamed) pipe semantics.
 * Author:   Peter Hogfeldt ([email protected]) from an outline
 *           by Per Hedeland ([email protected]).
 *
 * Note: This driver must *not* use the reclaim facilities, hence it 
 * is placed here. (after the #undef's of open,malloc etc)
 *
 * This driver supports select() and non-blocking I/O via 
 * ioctl(fd, FIONBIO, val).
 * 
 * 1997-03-21 Peter Hogfeldt
 *            Added non-blocking I/O.
 *
 */

/* 
 *  SEMAPHORES
 *
 *  Each end of a pipe has two semaphores: semExcl for serialising access to 
 *  the pipe end, and semBlock for blocking I/O.
 *
 *  reader->semBlock         is available (full) if and only if the pipe is
 *                           not empty, or the write end is closed. Otherwise
 *                           it is unavailable (empty). It is initially
 *                           unavailable.
 *
 *  writer->semBlock         is available (full) if and only if the pipe is
 *                           not full, or if the reader end is closed. 
 *                           Otherwise it is unavailable. It is initially
 *                           available.
 */

#define UXPIPE_SIZE 4096

/* Forward declaration */
typedef struct uxPipeDev UXPIPE_DEV;

/*
 * Pipe descriptor (one for each open pipe).
 */
typedef struct {
  int drvNum;
  UXPIPE_DEV *reader, *writer;
  RING_ID ringId;
}     UXPIPE;

/*
 * Device descriptor (one for each of the read and write
		      * ends of an open pipe).
		      */
struct uxPipeDev {
  UXPIPE *pipe;
  int blocking;
  SEL_WAKEUP_LIST wakeupList;
  SEM_ID semExcl;
  SEM_ID semBlock;
};

int uxPipeDrvNum = 0; /* driver number of pipe driver */

#define PIPE_NAME 	"/uxpipe" /* only used internally */
#define PIPE_READ 	"/r"	/* ditto */
#define PIPE_WRITE 	"/w"	/* ditto */

LOCAL char pipeRead[64], pipeWrite[64];
LOCAL DEV_HDR devHdr;
LOCAL UXPIPE *newPipe; /* communicate btwn open()s in pipe() */
LOCAL SEM_ID pipeSem; /* mutual exclusion in pipe() */

/* forward declarations */
LOCAL int uxPipeOpen(DEV_HDR *pDv, char *name, int mode);
LOCAL int uxPipeClose(UXPIPE_DEV *pDev);
LOCAL int uxPipeRead(UXPIPE_DEV *pDev, char *buffer, int maxbytes);
LOCAL int uxPipeWrite(UXPIPE_DEV *pDev, char *buffer, int nbytes);
LOCAL STATUS uxPipeIoctl(FAST UXPIPE_DEV *pDev, FAST int function, int arg);


/***************************************************************************
 *
 * uxPipeDrv - install Unix pipe driver
 *
 * This routine initializes the Unix pipe driver.  It must be called
 * before any other routine in this driver.
 *
 * RETURNS:
 *    OK, or ERROR if I/O system is unable to install driver.
 */

STATUS
uxPipeDrv(void)
{
  if (uxPipeDrvNum > 0)
    return (OK);		/* driver already installed */
  if ((uxPipeDrvNum = iosDrvInstall((FUNCPTR) NULL, (FUNCPTR) NULL,
				    uxPipeOpen, uxPipeClose, uxPipeRead,
				    uxPipeWrite, uxPipeIoctl)) == ERROR)
    return (ERROR);
  if (iosDevAdd(&devHdr, PIPE_NAME, uxPipeDrvNum) == ERROR)
    return (ERROR);
  strcpy(pipeRead, PIPE_NAME);
  strcat(pipeRead, PIPE_READ);
  strcpy(pipeWrite, PIPE_NAME);
  strcat(pipeWrite, PIPE_WRITE);
  if ((pipeSem = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE)) == NULL)
    return (ERROR);
  return (OK);
}

/***************************************************************************
 *
 * uxPipeOpen - open a pipe
 *
 * RETURNS: Pointer to device descriptor, or ERROR if memory cannot be
 * allocated (errno = ENOMEM),  or invalid argument (errno = EINVAL).
 */

/*
 * DEV_HDR *pDv;  pointer to device header (dummy) 
 * char *name;  name of pipe to open ("/r" or "/w") 
 * int   mode;  access mode (O_RDONLY or O_WRONLY)
 */		      
LOCAL int
uxPipeOpen(DEV_HDR *pDv, char *name, int mode)
{
  UXPIPE_DEV *reader, *writer;

  if (mode == O_RDONLY && strcmp(name, PIPE_READ) == 0) {
    /* reader open */
    if ((newPipe = (UXPIPE *) malloc(sizeof(UXPIPE))) != NULL) {
      if ((newPipe->ringId = rngCreate(UXPIPE_SIZE)) != NULL) {
	if ((reader = (UXPIPE_DEV *) malloc(sizeof(UXPIPE_DEV))) != NULL) {
	  if ((reader->semExcl = semBCreate(SEM_Q_FIFO, SEM_FULL)) != NULL) {
	    if ((reader->semBlock = semBCreate(SEM_Q_FIFO, SEM_EMPTY)) != NULL) {
	      reader->pipe = newPipe;
	      reader->blocking = 1;
	      selWakeupListInit(&reader->wakeupList);
	      newPipe->reader = reader;
	      newPipe->writer = NULL;
	      newPipe->drvNum = uxPipeDrvNum;
	      return ((int) reader);
	    }
	    semDelete(reader->semExcl);
	  }
	  free(reader);
	}
	rngDelete(newPipe->ringId);
      }
      free(newPipe);
      newPipe = NULL;
      errno = ENOMEM;
    }
  } else if (mode == O_WRONLY && strcmp(name, PIPE_WRITE) == 0) {
    /* writer open */
    if (newPipe != NULL &&
	(writer = (UXPIPE_DEV *) malloc(sizeof(UXPIPE_DEV))) != NULL) {
      if ((writer->semExcl = semBCreate(SEM_Q_FIFO, SEM_FULL)) != NULL) {
	if ((writer->semBlock = semBCreate(SEM_Q_FIFO, SEM_FULL)) != NULL) {
	  writer->blocking = 1;
	  writer->pipe = newPipe;
	  selWakeupListInit(&writer->wakeupList);
	  newPipe->writer = writer;
	  newPipe = NULL;
	  return ((int) writer);
	}
	semDelete(writer->semExcl);
      }
      free(writer);
    }
    if (newPipe != NULL)
      free(newPipe);
    newPipe = NULL;
    errno = ENOMEM;
  } else {
    errno = EINVAL;
  }
  return (ERROR);
}

/***************************************************************************
 *
 * uxPipeClose - close read or write end of a pipe.
 *
 * RETURNS:
 *    OK, or ERROR if device descriptor does not refer to an open read or
 write end of a pipe (errno = EBADF).
 */

LOCAL int
uxPipeClose(UXPIPE_DEV *pDev)
{
  UXPIPE *pajp = pDev->pipe;

  taskLock();
  if (pDev == pajp->reader) {
    /* Close this end */
    semDelete(pDev->semExcl);
    semDelete(pDev->semBlock);
    free(pDev);
    pajp->reader = NULL;
    /* Inform the other end */
    if (pajp->writer != NULL) {
      selWakeupAll(&pajp->writer->wakeupList, SELWRITE);
      semGive(pajp->writer->semBlock);
    }
  } else if (pDev == pajp->writer) {
    /* Close this end */
    semDelete(pDev->semExcl);
    semDelete(pDev->semBlock);
    free(pDev);
    pajp->writer = NULL;
    /* Inform the other end */
    if (pajp->reader != NULL) {
      selWakeupAll(&pajp->reader->wakeupList, SELREAD);
      semGive(pajp->reader->semBlock);
    }
  } else {
    errno = EBADF;
    taskUnlock();
    return (ERROR);
  }
  if (pajp->reader == NULL && pajp->writer == NULL) {
    rngDelete(pajp->ringId);
    pajp->drvNum = 0;
    free(pajp);
  }
  taskUnlock();
  return (OK);
}
/***************************************************************************
 *
 * uxPipeRead - read from a pipe.
 *
 * Reads at most maxbytes bytes from the pipe. Blocks if blocking mode is
 * set and the pipe is empty.
 *
 * RETURNS:
 *    number of bytes read, 0 on EOF, or ERROR if device descriptor does
 *    not refer to an open read end of a pipe (errno = EBADF), or if 
 *    non-blocking mode is set and the pipe is empty (errno = EWOULDBLOCK).
 */

LOCAL int
uxPipeRead(UXPIPE_DEV *pDev, char *buffer, int maxbytes)
{
  UXPIPE *pajp = pDev->pipe;
  int   nbytes = 0;

  if (pDev != pajp->reader) {
    errno = EBADF;
    return (ERROR);
  }
  if (maxbytes == 0)
    return (0);
  semTake(pDev->semExcl, WAIT_FOREVER);
  /* 
   * Note that semBlock may be full, although there is nothing to read.
   * This happens e.g. after the following sequence of operations: a 
   * reader task blocks, a writer task writes two times (the first 
   * write unblocks the reader task, the second write makes semBlock
   * full).
   */
  while (nbytes == 0) {
    if (pDev->blocking) 
      semTake(pDev->semBlock, WAIT_FOREVER);
    /* 
     * Reading and updating of the write end must not be interleaved
     * with a write from another task - hence we lock this task.
     */
    taskLock();
    nbytes = rngBufGet(pajp->ringId, buffer, maxbytes);
    if (nbytes > 0) {
      /* Give own semaphore if bytes remain or if write end is closed */
      if ((!rngIsEmpty(pajp->ringId) || pajp->writer == NULL) &&
	  pDev->blocking)
	semGive(pDev->semBlock);
      /* Inform write end */
      if (pajp->writer != NULL) {
	if (pajp->writer->blocking) 
	  semGive(pajp->writer->semBlock);
	selWakeupAll(&pajp->writer->wakeupList, SELWRITE);
      }
    } else if (pajp->writer == NULL) {
      nbytes = 0;		/* EOF */
      /* Give semaphore when write end is closed */
      if (pDev->blocking) 
	semGive(pDev->semBlock);
      taskUnlock();
      semGive(pDev->semExcl);
      return (nbytes);
    } else if (!pDev->blocking) {
      taskUnlock();
      semGive(pDev->semExcl);
      errno = EWOULDBLOCK;
      return (ERROR);
    }      
    taskUnlock();
  }
  semGive(pDev->semExcl);
  return (nbytes);
}

/***************************************************************************
 *
 * uxPipeWrite - write to a pipe.
 *
 * Writes nbytes bytes to the pipe. Blocks if blocking mode is set, and if 
 * the pipe is full.
 *
 * RETURNS:
 * number of bytes written, or ERROR if the device descriptor does not
 * refer to an open write end of a pipe (errno = EBADF); or if the read end
 * of the pipe is closed (errno = EPIPE); or if non-blocking mode is set
 * and the pipe is full (errno = EWOULDBLOCK).
 *
 */

LOCAL int
uxPipeWrite(UXPIPE_DEV *pDev, char *buffer, int nbytes)
{

  UXPIPE *pajp = pDev->pipe;
  int   sofar = 0, written;

  if (pDev != pajp->writer) {
    errno = EBADF;
    return (ERROR);
  }
  if (pajp->reader == NULL) {
    errno = EPIPE;
    return (ERROR);
  }
  if (nbytes == 0)
    return (0);
  semTake(pDev->semExcl, WAIT_FOREVER);
  while (sofar < nbytes) {
    if (pDev->blocking)
      semTake(pDev->semBlock, WAIT_FOREVER);
    if (pajp->reader == NULL) {
      errno = EPIPE;
      semGive(pDev->semBlock);
      semGive(pDev->semExcl);
      return (ERROR);
    }
    /* Writing and updating of the read end must not be interleaved
     * with a read from another task - hence we lock this task.
     */
    taskLock();
    written = rngBufPut(pajp->ringId, buffer + sofar, nbytes - sofar);
    sofar += written;
    /* Inform the read end if we really wrote something */
    if (written > 0 && pajp->reader != NULL) {
      selWakeupAll(&pajp->reader->wakeupList, SELREAD);
      if (pajp->reader->blocking)
	semGive(pajp->reader->semBlock);
    }
    taskUnlock();
    if (!pDev->blocking) {
      if (sofar == 0) {
      errno = EWOULDBLOCK;
      sofar = ERROR;
      }
      break;
    }
  }
  /* Give own semaphore if space remains */
  if (!rngIsFull(pajp->ringId) && pDev->blocking)
    semGive(pDev->semBlock);
  semGive(pDev->semExcl);
  return (sofar);
}

/***************************************************************************
 *
 * uxPipeIoctl - do device specific I/O control
 *
 * RETURNS:
 *    OK or ERROR.
 */

LOCAL STATUS
uxPipeIoctl(FAST UXPIPE_DEV *pDev, FAST int function, int arg)
     
{
  UXPIPE *pajp = pDev->pipe;
  int   status = OK;

  switch (function) {
  case FIONBIO:
    pDev->blocking = (*(int *)arg) ? 0 : 1;
    break;
  case FIOSELECT:
    taskLock();
    selNodeAdd(&pDev->wakeupList, (SEL_WAKEUP_NODE *) arg);
    if (selWakeupType((SEL_WAKEUP_NODE *) arg) == SELREAD &&
	pDev == pajp->reader &&
	(!rngIsEmpty(pajp->ringId) || pajp->writer == NULL))
      selWakeup((SEL_WAKEUP_NODE *) arg);
    if (selWakeupType((SEL_WAKEUP_NODE *) arg) == SELWRITE &&
	pDev == pajp->writer &&
	(!rngIsFull(pajp->ringId) || pajp->reader == NULL))
      selWakeup((SEL_WAKEUP_NODE *) arg);
    taskUnlock();
    break;
  case FIOUNSELECT:
    selNodeDelete(&pDev->wakeupList, (SEL_WAKEUP_NODE *) arg);
    break;
  default:
    status = ERROR;
    break;
  }
  return (status);
}

/***************************************************************************
 *
 * pipe - create an intertask channel
 *
 * Creates a pipe. fd[0] (fd[1]) is the read (write) file descriptor.
 *
 * RETURNS:
 *    OK or ERROR, if the pipe could not be created.
 */

STATUS
pipe(int fd[2])
{
  semTake(pipeSem, WAIT_FOREVER);
  if ((fd[0] = open(pipeRead, O_RDONLY, 0)) != ERROR) {
    if ((fd[1] = open(pipeWrite, O_WRONLY, 0)) != ERROR) {
      semGive(pipeSem);
      return (OK);
    }
    (void) close(fd[0]);
  }
  errno &= 0xFFFF;
  if((errno & 0xFFFF) == EINTR) /* Why on earth EINTR??? */
      errno = ENFILE;           /* It means we are out of file descriptors...*/
  semGive(pipeSem);
  return (ERROR);
}

/***************************************************************************
 *
 * uxPipeShow - display pipe information
 *
 * RETURNS:
 *    N/A.
 */

void
uxPipeShow(int fd)
{
  UXPIPE_DEV *pDev;
  UXPIPE *pajp;
  int drvValue;

  if ((drvValue = iosFdValue(fd)) == ERROR) {
    erts_fprintf(stderr, "Error: file descriptor invalid\n");
    return;
  }
  pDev = (UXPIPE_DEV *)drvValue;
  pajp = pDev->pipe;
  if (pajp->drvNum != uxPipeDrvNum) {
    erts_fprintf(stderr, "Error: Not a ux pipe device\n");
    return;
  }
  erts_fprintf(stderr, "Device              : 0x%x\n", (int) pDev);
  erts_fprintf(stderr, "Buffer size         : %d\n", UXPIPE_SIZE);
  erts_fprintf(stderr, "Bytes in buffer     : %d\n\n", rngNBytes(pajp->ringId));
  erts_fprintf(stderr, "READ END\n\n");
  if (pajp->reader != NULL) {
    erts_fprintf(stderr, "Mode                : ");
    erts_fprintf(stderr, "%s\n", 
	       (pajp->reader->blocking) ? "blocking" : "non-blocking");
  }
  erts_fprintf(stderr, "Status              : ");
  if (pajp->reader != NULL) {
    erts_fprintf(stderr, "OPEN\n");
    erts_fprintf(stderr, "Wake-up list        : %d\n\n", 
	       selWakeupListLen(&pajp->reader->wakeupList));
    erts_fprintf(stderr, "Exclusion Semaphore\n");
    semShow(pajp->reader->semExcl, 1);
    erts_fprintf(stderr, "Blocking Semaphore\n");
    semShow(pajp->reader->semBlock, 1);
  } else 
    erts_fprintf(stderr, "CLOSED\n\n");
  erts_fprintf(stderr, "WRITE END\n\n");
  if (pajp->writer != NULL) {
    erts_fprintf(stderr, "Mode                : ");
    erts_fprintf(stderr, "%s\n", 
	       (pajp->writer->blocking) ? "blocking" : "non-blocking");
  }
  erts_fprintf(stderr, "Status              : ");
  if (pajp->writer != NULL) {
    erts_fprintf(stderr, "OPEN\n");
    erts_fprintf(stderr, "Wake-up list        : %d\n\n", 
	       selWakeupListLen(&pajp->writer->wakeupList));
    erts_fprintf(stderr, "Exclusion Semaphore\n");
    semShow(pajp->writer->semExcl, 1);
    erts_fprintf(stderr, "Blocking Semaphore\n");
    semShow(pajp->writer->semBlock, 1);
  } else 
    erts_fprintf(stderr, "CLOSED\n\n");
}

#ifdef DEBUG
void
erl_assert_error(char* expr, char* file, int line)
{   
    fflush(stdout);
    fprintf(stderr, "Assertion failed: %s in %s, line %d\n",
	    expr, file, line);
    fflush(stderr);
    erl_crash_dump(file, line, "Assertion failed: %s\n", expr);
    abort();
}
void
erl_debug(char* fmt, ...)
{
    char sbuf[1024];		/* Temporary buffer. */
    va_list va;
    
    va_start(va, fmt);
    vsprintf(sbuf, fmt, va);
    va_end(va);
    fprintf(stderr, "%s\n", sbuf);
}
#endif