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
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
|
diff --git a/include/printf.h b/include/printf.h
index 340b6cb..2dea58f 100644
--- a/include/printf.h
+++ b/include/printf.h
@@ -75,6 +75,7 @@ struct printf_info
unsigned int is_short:1; /* h flag. */
unsigned int is_long:1; /* l flag. */
unsigned int is_long_double:1;/* L flag. */
+ unsigned int __padding:20;/* non-gnu -- total of 32 bits on 32bit arch */
#elif __BYTE_ORDER == __BIG_ENDIAN
diff --git a/include/pthread.h b/include/pthread.h
index 8c01172..cee112b 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -644,7 +644,8 @@ extern void _pthread_cleanup_pop (struct
/* Install a cleanup handler as pthread_cleanup_push does, but also
saves the current cancellation type and set it to deferred cancellation. */
-#ifdef __USE_GNU
+/* #ifdef __USE_GNU */
+#if defined(__USE_GNU) || defined(_LIBC)
# define pthread_cleanup_push_defer_np(routine,arg) \
{ struct _pthread_cleanup_buffer _buffer; \
_pthread_cleanup_push_defer (&_buffer, (routine), (arg));
diff --git a/libc/inet/getnetent.c b/libc/inet/getnetent.c
index 181c5ad..659bf5d 100644
--- a/libc/inet/getnetent.c
+++ b/libc/inet/getnetent.c
@@ -22,18 +22,9 @@
#include <netdb.h>
#include <arpa/inet.h>
+#include <bits/uClibc_mutex.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
-
-
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
#define MAXALIASES 35
static const char NETDB[] = _PATH_NETWORKS;
@@ -46,25 +37,25 @@ int _net_stayopen;
void setnetent(int f)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (netf == NULL)
- netf = fopen(NETDB, "r" );
+ netf = fopen(NETDB, "r" );
else
- rewind(netf);
+ rewind(netf);
_net_stayopen |= f;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return;
}
void endnetent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (netf) {
- fclose(netf);
- netf = NULL;
+ fclose(netf);
+ netf = NULL;
}
_net_stayopen = 0;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
static char * any(register char *cp, char *match)
@@ -72,10 +63,10 @@ static char * any(register char *cp, cha
register char *mp, c;
while ((c = *cp)) {
- for (mp = match; *mp; mp++)
- if (*mp == c)
- return (cp);
- cp++;
+ for (mp = match; *mp; mp++)
+ if (*mp == c)
+ return (cp);
+ cp++;
}
return ((char *)0);
}
@@ -84,59 +75,62 @@ struct netent * getnetent(void)
{
char *p;
register char *cp, **q;
+ struct netent *rv = NULL;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
- UNLOCK;
- return (NULL);
+ goto DONE;
}
-again:
+ again:
if (!line) {
- line = malloc(BUFSIZ + 1);
- if (!line)
- abort();
+ line = malloc(BUFSIZ + 1);
+ if (!line)
+ abort();
}
p = fgets(line, BUFSIZ, netf);
if (p == NULL) {
- UNLOCK;
- return (NULL);
+ goto DONE;
}
if (*p == '#')
- goto again;
+ goto again;
cp = any(p, "#\n");
if (cp == NULL)
- goto again;
+ goto again;
*cp = '\0';
net.n_name = p;
cp = any(p, " \t");
if (cp == NULL)
- goto again;
+ goto again;
*cp++ = '\0';
while (*cp == ' ' || *cp == '\t')
- cp++;
+ cp++;
p = any(cp, " \t");
if (p != NULL)
- *p++ = '\0';
+ *p++ = '\0';
net.n_net = inet_network(cp);
net.n_addrtype = AF_INET;
q = net.n_aliases = net_aliases;
if (p != NULL)
- cp = p;
+ cp = p;
while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- if (q < &net_aliases[MAXALIASES - 1])
- *q++ = cp;
- cp = any(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
+ if (*cp == ' ' || *cp == '\t') {
+ cp++;
+ continue;
+ }
+ if (q < &net_aliases[MAXALIASES - 1])
+ *q++ = cp;
+ cp = any(cp, " \t");
+ if (cp != NULL)
+ *cp++ = '\0';
}
*q = NULL;
- UNLOCK;
- return (&net);
+
+ rv = &net;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
+ return rv;
}
diff --git a/libc/inet/getproto.c b/libc/inet/getproto.c
index c9f35f1..3665d89 100644
--- a/libc/inet/getproto.c
+++ b/libc/inet/getproto.c
@@ -62,17 +62,9 @@
#include <string.h>
#include <errno.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
-
+#include <bits/uClibc_mutex.h>
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
#define MAXALIASES 35
#define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
@@ -85,109 +77,114 @@ static int proto_stayopen;
static void __initbuf(void)
{
if (!static_aliases) {
- static_aliases = malloc(SBUFSIZE);
- if (!static_aliases)
- abort();
+ static_aliases = malloc(SBUFSIZE);
+ if (!static_aliases)
+ abort();
}
}
void setprotoent(int f)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (protof == NULL)
- protof = fopen(_PATH_PROTOCOLS, "r" );
+ protof = fopen(_PATH_PROTOCOLS, "r" );
else
- rewind(protof);
+ rewind(protof);
proto_stayopen |= f;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
void endprotoent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (protof) {
- fclose(protof);
- protof = NULL;
+ fclose(protof);
+ protof = NULL;
}
proto_stayopen = 0;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
int getprotoent_r(struct protoent *result_buf,
- char *buf, size_t buflen,
- struct protoent **result)
+ char *buf, size_t buflen,
+ struct protoent **result)
{
char *p;
register char *cp, **q;
char **proto_aliases;
char *line;
+ int rv;
*result = NULL;
if (buflen < sizeof(*proto_aliases)*MAXALIASES) {
- errno=ERANGE;
- return errno;
+ errno=ERANGE;
+ return errno;
}
- LOCK;
+
+ __UCLIBC_MUTEX_LOCK(mylock);
proto_aliases=(char **)buf;
buf+=sizeof(*proto_aliases)*MAXALIASES;
buflen-=sizeof(*proto_aliases)*MAXALIASES;
if (buflen < BUFSIZ+1) {
- UNLOCK;
- errno=ERANGE;
- return errno;
+ errno=rv=ERANGE;
+ goto DONE;
}
line=buf;
buf+=BUFSIZ+1;
buflen-=BUFSIZ+1;
if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) {
- UNLOCK;
- return errno;
+ rv=errno;
+ goto DONE;
}
-again:
+ again:
if ((p = fgets(line, BUFSIZ, protof)) == NULL) {
- UNLOCK;
- return TRY_AGAIN;
+ rv=TRY_AGAIN;
+ goto DONE;
}
if (*p == '#')
- goto again;
+ goto again;
cp = strpbrk(p, "#\n");
if (cp == NULL)
- goto again;
+ goto again;
*cp = '\0';
result_buf->p_name = p;
cp = strpbrk(p, " \t");
if (cp == NULL)
- goto again;
+ goto again;
*cp++ = '\0';
while (*cp == ' ' || *cp == '\t')
- cp++;
+ cp++;
p = strpbrk(cp, " \t");
if (p != NULL)
- *p++ = '\0';
+ *p++ = '\0';
result_buf->p_proto = atoi(cp);
q = result_buf->p_aliases = proto_aliases;
if (p != NULL) {
- cp = p;
- while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- if (q < &proto_aliases[MAXALIASES - 1])
- *q++ = cp;
- cp = strpbrk(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
- }
+ cp = p;
+ while (cp && *cp) {
+ if (*cp == ' ' || *cp == '\t') {
+ cp++;
+ continue;
+ }
+ if (q < &proto_aliases[MAXALIASES - 1])
+ *q++ = cp;
+ cp = strpbrk(cp, " \t");
+ if (cp != NULL)
+ *cp++ = '\0';
+ }
}
*q = NULL;
*result=result_buf;
- UNLOCK;
- return 0;
+
+ rv = 0;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
+ return rv;
}
struct protoent * getprotoent(void)
@@ -201,26 +198,26 @@ struct protoent * getprotoent(void)
int getprotobyname_r(const char *name,
- struct protoent *result_buf,
- char *buf, size_t buflen,
- struct protoent **result)
+ struct protoent *result_buf,
+ char *buf, size_t buflen,
+ struct protoent **result)
{
register char **cp;
int ret;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
setprotoent(proto_stayopen);
while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) {
- if (strcmp(result_buf->p_name, name) == 0)
- break;
- for (cp = result_buf->p_aliases; *cp != 0; cp++)
- if (strcmp(*cp, name) == 0)
- goto found;
+ if (strcmp(result_buf->p_name, name) == 0)
+ break;
+ for (cp = result_buf->p_aliases; *cp != 0; cp++)
+ if (strcmp(*cp, name) == 0)
+ goto found;
}
-found:
+ found:
if (!proto_stayopen)
- endprotoent();
- UNLOCK;
+ endprotoent();
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return *result?0:ret;
}
@@ -236,20 +233,20 @@ struct protoent * getprotobyname(const c
int getprotobynumber_r (int proto_num,
- struct protoent *result_buf,
- char *buf, size_t buflen,
- struct protoent **result)
+ struct protoent *result_buf,
+ char *buf, size_t buflen,
+ struct protoent **result)
{
int ret;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
setprotoent(proto_stayopen);
while (!(ret=getprotoent_r(result_buf, buf, buflen, result)))
- if (result_buf->p_proto == proto_num)
- break;
+ if (result_buf->p_proto == proto_num)
+ break;
if (!proto_stayopen)
- endprotoent();
- UNLOCK;
+ endprotoent();
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return *result?0:ret;
}
diff --git a/libc/inet/getservice.c b/libc/inet/getservice.c
index cbe5c50..b666057 100644
--- a/libc/inet/getservice.c
+++ b/libc/inet/getservice.c
@@ -65,20 +65,9 @@
#include <arpa/inet.h>
#include <errno.h>
+#include <bits/uClibc_mutex.h>
-
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
-
-
-
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
#define MAXALIASES 35
#define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
@@ -91,32 +80,32 @@ static int serv_stayopen;
static void __initbuf(void)
{
if (!servbuf) {
- servbuf = malloc(SBUFSIZE);
- if (!servbuf)
- abort();
+ servbuf = malloc(SBUFSIZE);
+ if (!servbuf)
+ abort();
}
}
void setservent(int f)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (servf == NULL)
- servf = fopen(_PATH_SERVICES, "r" );
+ servf = fopen(_PATH_SERVICES, "r" );
else
- rewind(servf);
+ rewind(servf);
serv_stayopen |= f;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
void endservent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (servf) {
- fclose(servf);
- servf = NULL;
+ fclose(servf);
+ servf = NULL;
}
serv_stayopen = 0;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
struct servent * getservent(void)
@@ -149,127 +138,129 @@ struct servent * getservbyport(int port,
}
int getservent_r(struct servent * result_buf,
- char * buf, size_t buflen,
- struct servent ** result)
+ char * buf, size_t buflen,
+ struct servent ** result)
{
char *p;
register char *cp, **q;
char **serv_aliases;
char *line;
+ int rv;
*result=NULL;
if (buflen < sizeof(*serv_aliases)*MAXALIASES) {
- errno=ERANGE;
- return errno;
+ errno=ERANGE;
+ return errno;
}
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
serv_aliases=(char **)buf;
buf+=sizeof(*serv_aliases)*MAXALIASES;
buflen-=sizeof(*serv_aliases)*MAXALIASES;
if (buflen < BUFSIZ+1) {
- UNLOCK;
- errno=ERANGE;
- return errno;
+ errno=rv=ERANGE;
+ goto DONE;
}
line=buf;
buf+=BUFSIZ+1;
buflen-=BUFSIZ+1;
if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) {
- UNLOCK;
- errno=EIO;
- return errno;
+ errno=rv=EIO;
+ goto DONE;
}
-again:
+ again:
if ((p = fgets(line, BUFSIZ, servf)) == NULL) {
- UNLOCK;
- errno=EIO;
- return errno;
+ errno=rv=EIO;
+ goto DONE;
}
if (*p == '#')
- goto again;
+ goto again;
cp = strpbrk(p, "#\n");
if (cp == NULL)
- goto again;
+ goto again;
*cp = '\0';
result_buf->s_name = p;
p = strpbrk(p, " \t");
if (p == NULL)
- goto again;
+ goto again;
*p++ = '\0';
while (*p == ' ' || *p == '\t')
- p++;
+ p++;
cp = strpbrk(p, ",/");
if (cp == NULL)
- goto again;
+ goto again;
*cp++ = '\0';
result_buf->s_port = htons((u_short)atoi(p));
result_buf->s_proto = cp;
q = result_buf->s_aliases = serv_aliases;
cp = strpbrk(cp, " \t");
if (cp != NULL)
- *cp++ = '\0';
+ *cp++ = '\0';
while (cp && *cp) {
- if (*cp == ' ' || *cp == '\t') {
- cp++;
- continue;
- }
- if (q < &serv_aliases[MAXALIASES - 1])
- *q++ = cp;
- cp = strpbrk(cp, " \t");
- if (cp != NULL)
- *cp++ = '\0';
+ if (*cp == ' ' || *cp == '\t') {
+ cp++;
+ continue;
+ }
+ if (q < &serv_aliases[MAXALIASES - 1])
+ *q++ = cp;
+ cp = strpbrk(cp, " \t");
+ if (cp != NULL)
+ *cp++ = '\0';
}
*q = NULL;
*result=result_buf;
- UNLOCK;
- return 0;
+
+ rv = 0;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
+ return rv;
}
int getservbyname_r(const char *name, const char *proto,
- struct servent * result_buf, char * buf, size_t buflen,
- struct servent ** result)
+ struct servent * result_buf, char * buf, size_t buflen,
+ struct servent ** result)
{
register char **cp;
int ret;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
setservent(serv_stayopen);
while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
- if (strcmp(name, result_buf->s_name) == 0)
- goto gotname;
- for (cp = result_buf->s_aliases; *cp; cp++)
- if (strcmp(name, *cp) == 0)
- goto gotname;
- continue;
-gotname:
- if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
- break;
+ if (strcmp(name, result_buf->s_name) == 0)
+ goto gotname;
+ for (cp = result_buf->s_aliases; *cp; cp++)
+ if (strcmp(name, *cp) == 0)
+ goto gotname;
+ continue;
+ gotname:
+ if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
+ break;
}
if (!serv_stayopen)
- endservent();
- UNLOCK;
+ endservent();
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return *result?0:ret;
}
int getservbyport_r(int port, const char *proto,
- struct servent * result_buf, char * buf,
- size_t buflen, struct servent ** result)
+ struct servent * result_buf, char * buf,
+ size_t buflen, struct servent ** result)
{
int ret;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
setservent(serv_stayopen);
while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
- if (result_buf->s_port != port)
- continue;
- if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
- break;
+ if (result_buf->s_port != port)
+ continue;
+ if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
+ break;
}
if (!serv_stayopen)
- endservent();
- UNLOCK;
+ endservent();
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return *result?0:ret;
}
diff --git a/libc/inet/resolv.c b/libc/inet/resolv.c
index 27b60ef..0f583ab 100644
--- a/libc/inet/resolv.c
+++ b/libc/inet/resolv.c
@@ -7,7 +7,7 @@
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
-*/
+ */
/*
* Portions Copyright (c) 1985, 1993
@@ -153,6 +153,11 @@
#include <sys/utsname.h>
#include <sys/un.h>
+#include <bits/uClibc_mutex.h>
+
+__UCLIBC_MUTEX_EXTERN(__resolv_lock);
+
+
#define MAX_RECURSE 5
#define REPLY_TIMEOUT 10
#define MAX_RETRIES 3
@@ -180,18 +185,6 @@ extern char * __nameserver[MAX_SERVERS];
extern int __searchdomains;
extern char * __searchdomain[MAX_SEARCH];
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-extern pthread_mutex_t __resolv_lock;
-# define BIGLOCK __pthread_mutex_lock(&__resolv_lock)
-# define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock);
-#else
-# define BIGLOCK
-# define BIGUNLOCK
-#endif
-
-
-
/* Structs */
struct resolv_header {
int id;
@@ -229,49 +222,49 @@ enum etc_hosts_action {
/* function prototypes */
extern int __get_hosts_byname_r(const char * name, int type,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop);
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop);
extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop);
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop);
extern void __open_etc_hosts(FILE **fp);
extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
- enum etc_hosts_action action,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop);
+ enum etc_hosts_action action,
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop);
extern int __dns_lookup(const char * name, int type, int nscount,
- char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
+ char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
extern int __decode_dotted(const unsigned char * message, int offset,
- char * dest, int maxlen);
+ char * dest, int maxlen);
extern int __length_dotted(const unsigned char * message, int offset);
extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
extern int __decode_header(unsigned char * data, struct resolv_header * h);
extern int __encode_question(struct resolv_question * q,
- unsigned char * dest, int maxlen);
+ unsigned char * dest, int maxlen);
extern int __decode_question(unsigned char * message, int offset,
- struct resolv_question * q);
+ struct resolv_question * q);
extern int __encode_answer(struct resolv_answer * a,
- unsigned char * dest, int maxlen);
+ unsigned char * dest, int maxlen);
extern int __decode_answer(unsigned char * message, int offset,
- struct resolv_answer * a);
+ struct resolv_answer * a);
extern int __length_question(unsigned char * message, int offset);
extern int __open_nameservers(void);
extern void __close_nameservers(void);
extern int __dn_expand(const u_char *, const u_char *, const u_char *,
- char *, int);
+ char *, int);
extern int __ns_name_uncompress(const u_char *, const u_char *,
- const u_char *, char *, size_t);
+ const u_char *, char *, size_t);
extern int __ns_name_ntop(const u_char *, char *, size_t);
extern int __ns_name_unpack(const u_char *, const u_char *, const u_char *,
- u_char *, size_t);
+ u_char *, size_t);
#ifdef L_encodeh
@@ -361,7 +354,7 @@ int __encode_dotted(const char *dotted,
This routine understands compressed data. */
int __decode_dotted(const unsigned char *data, int offset,
- char *dest, int maxlen)
+ char *dest, int maxlen)
{
int l;
int measure = 1;
@@ -435,7 +428,7 @@ int __length_dotted(const unsigned char
#ifdef L_encodeq
int __encode_question(struct resolv_question *q,
- unsigned char *dest, int maxlen)
+ unsigned char *dest, int maxlen)
{
int i;
@@ -460,7 +453,7 @@ int __encode_question(struct resolv_ques
#ifdef L_decodeq
int __decode_question(unsigned char *message, int offset,
- struct resolv_question *q)
+ struct resolv_question *q)
{
char temp[256];
int i;
@@ -525,7 +518,7 @@ int __encode_answer(struct resolv_answer
#ifdef L_decodea
int __decode_answer(unsigned char *message, int offset,
- struct resolv_answer *a)
+ struct resolv_answer *a)
{
char temp[256];
int i;
@@ -557,11 +550,11 @@ int __decode_answer(unsigned char *messa
#ifdef L_encodep
int __encode_packet(struct resolv_header *h,
- struct resolv_question **q,
- struct resolv_answer **an,
- struct resolv_answer **ns,
- struct resolv_answer **ar,
- unsigned char *dest, int maxlen)
+ struct resolv_question **q,
+ struct resolv_answer **an,
+ struct resolv_answer **ns,
+ struct resolv_answer **ar,
+ unsigned char *dest, int maxlen)
{
int i, total = 0;
int j;
@@ -621,7 +614,7 @@ int __decode_packet(unsigned char *data,
#ifdef L_formquery
int __form_query(int id, const char *name, int type, unsigned char *packet,
- int maxlen)
+ int maxlen)
{
struct resolv_header h;
struct resolv_question q;
@@ -649,14 +642,7 @@ int __form_query(int id, const char *nam
#ifdef L_dnslookup
-#ifdef __UCLIBC_HAS_THREADS__
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
/* Just for the record, having to lock __dns_lookup() just for these two globals
* is pretty lame. I think these two variables can probably be de-global-ized,
@@ -665,7 +651,7 @@ static pthread_mutex_t mylock = PTHREAD_
static int ns=0, id=1;
int __dns_lookup(const char *name, int type, int nscount, char **nsip,
- unsigned char **outpacket, struct resolv_answer *a)
+ unsigned char **outpacket, struct resolv_answer *a)
{
int i, j, len, fd, pos, rc;
struct timeval tv;
@@ -693,10 +679,10 @@ int __dns_lookup(const char *name, int t
DPRINTF("Looking up type %d answer for '%s'\n", type, name);
/* Mess with globals while under lock */
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
local_ns = ns % nscount;
local_id = id;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
while (retries < MAX_RETRIES) {
if (fd != -1)
@@ -722,13 +708,13 @@ int __dns_lookup(const char *name, int t
strncpy(lookup,name,MAXDNAME);
if (variant >= 0) {
- BIGLOCK;
- if (variant < __searchdomains) {
- strncat(lookup,".", MAXDNAME);
- strncat(lookup,__searchdomain[variant], MAXDNAME);
- }
- BIGUNLOCK;
- }
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
+ if (variant < __searchdomains) {
+ strncat(lookup,".", MAXDNAME);
+ strncat(lookup,__searchdomain[variant], MAXDNAME);
+ }
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
+ }
DPRINTF("lookup name: %s\n", lookup);
q.dotted = (char *)lookup;
q.qtype = type;
@@ -750,7 +736,7 @@ int __dns_lookup(const char *name, int t
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#endif
if (fd < 0) {
- retries++;
+ retries++;
continue;
}
@@ -772,11 +758,11 @@ int __dns_lookup(const char *name, int t
#endif
if (rc < 0) {
if (errno == ENETUNREACH) {
- /* routing error, presume not transient */
- goto tryall;
+ /* routing error, presume not transient */
+ goto tryall;
} else
- /* retry */
- retries++;
+ /* retry */
+ retries++;
continue;
}
@@ -838,55 +824,55 @@ int __dns_lookup(const char *name, int t
first_answer = 1;
for (j=0;j<h.ancount;j++,pos += i)
- {
- i = __decode_answer(packet, pos, &ma);
+ {
+ i = __decode_answer(packet, pos, &ma);
- if (i<0) {
- DPRINTF("failed decode %d\n", i);
- goto again;
- }
+ if (i<0) {
+ DPRINTF("failed decode %d\n", i);
+ goto again;
+ }
- if ( first_answer )
- {
- ma.buf = a->buf;
- ma.buflen = a->buflen;
- ma.add_count = a->add_count;
- memcpy(a, &ma, sizeof(ma));
- if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
- {
- break;
- }
- if (a->atype != type)
- {
- free(a->dotted);
- continue;
- }
- a->add_count = h.ancount - j - 1;
- if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
- {
- break;
- }
- a->add_count = 0;
- first_answer = 0;
- }
- else
- {
- free(ma.dotted);
- if (ma.atype != type)
- {
- continue;
- }
- if (a->rdlength != ma.rdlength)
- {
- free(a->dotted);
- DPRINTF("Answer address len(%u) differs from original(%u)\n",
- ma.rdlength, a->rdlength);
- goto again;
+ if ( first_answer )
+ {
+ ma.buf = a->buf;
+ ma.buflen = a->buflen;
+ ma.add_count = a->add_count;
+ memcpy(a, &ma, sizeof(ma));
+ if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
+ {
+ break;
+ }
+ if (a->atype != type)
+ {
+ free(a->dotted);
+ continue;
+ }
+ a->add_count = h.ancount - j - 1;
+ if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
+ {
+ break;
+ }
+ a->add_count = 0;
+ first_answer = 0;
+ }
+ else
+ {
+ free(ma.dotted);
+ if (ma.atype != type)
+ {
+ continue;
+ }
+ if (a->rdlength != ma.rdlength)
+ {
+ free(a->dotted);
+ DPRINTF("Answer address len(%u) differs from original(%u)\n",
+ ma.rdlength, a->rdlength);
+ goto again;
+ }
+ memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
+ ++a->add_count;
+ }
}
- memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
- ++a->add_count;
- }
- }
DPRINTF("Answer name = |%s|\n", a->dotted);
DPRINTF("Answer type = |%d|\n", a->atype);
@@ -900,48 +886,48 @@ int __dns_lookup(const char *name, int t
free(lookup);
/* Mess with globals while under lock */
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
ns = local_ns;
id = local_id;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return (len); /* success! */
- tryall:
+ tryall:
/* if there are other nameservers, give them a go,
otherwise return with error */
{
variant = -1;
- local_ns = (local_ns + 1) % nscount;
- if (local_ns == 0)
- retries++;
+ local_ns = (local_ns + 1) % nscount;
+ if (local_ns == 0)
+ retries++;
- continue;
+ continue;
}
- again:
+ again:
/* if there are searchdomains, try them or fallback as passed */
{
int sdomains;
- BIGLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
sdomains=__searchdomains;
- BIGUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
if (variant < sdomains - 1) {
- /* next search */
- variant++;
+ /* next search */
+ variant++;
} else {
- /* next server, first search */
- local_ns = (local_ns + 1) % nscount;
- if (local_ns == 0)
- retries++;
+ /* next server, first search */
+ local_ns = (local_ns + 1) % nscount;
+ if (local_ns == 0)
+ retries++;
- variant = -1;
+ variant = -1;
}
}
}
-fail:
+ fail:
if (fd != -1)
close(fd);
if (lookup)
@@ -951,10 +937,10 @@ fail:
h_errno = NETDB_INTERNAL;
/* Mess with globals while under lock */
if (local_ns != -1) {
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
ns = local_ns;
id = local_id;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
return -1;
}
@@ -966,9 +952,8 @@ int __nameservers;
char * __nameserver[MAX_SERVERS];
int __searchdomains;
char * __searchdomain[MAX_SEARCH];
-#ifdef __UCLIBC_HAS_THREADS__
-pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
-#endif
+
+__UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_MUTEX_INITIALIZER);
/*
* we currently read formats not quite the same as that on normal
@@ -982,60 +967,63 @@ int __open_nameservers()
#define RESOLV_ARGS 5
char szBuffer[128], *p, *argv[RESOLV_ARGS];
int argc;
+ int rv = 0;
- BIGLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
if (__nameservers > 0) {
- BIGUNLOCK;
- return 0;
+ goto DONE;
}
if ((fp = fopen("/etc/resolv.conf", "r")) ||
- (fp = fopen("/etc/config/resolv.conf", "r")))
- {
-
- while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
+ (fp = fopen("/etc/config/resolv.conf", "r")))
+ {
- for (p = szBuffer; *p && isspace(*p); p++)
- /* skip white space */;
- if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
- continue;
- argc = 0;
- while (*p && argc < RESOLV_ARGS) {
- argv[argc++] = p;
- while (*p && !isspace(*p) && *p != '\n')
- p++;
- while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
- *p++ = '\0';
- }
+ while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
- if (strcmp(argv[0], "nameserver") == 0) {
- for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
- __nameserver[__nameservers++] = strdup(argv[i]);
- DPRINTF("adding nameserver %s\n", argv[i]);
+ for (p = szBuffer; *p && isspace(*p); p++)
+ /* skip white space */;
+ if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
+ continue;
+ argc = 0;
+ while (*p && argc < RESOLV_ARGS) {
+ argv[argc++] = p;
+ while (*p && !isspace(*p) && *p != '\n')
+ p++;
+ while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
+ *p++ = '\0';
}
- }
- /* domain and search are mutually exclusive, the last one wins */
- if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
- while (__searchdomains > 0) {
- free(__searchdomain[--__searchdomains]);
- __searchdomain[__searchdomains] = NULL;
+ if (strcmp(argv[0], "nameserver") == 0) {
+ for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
+ __nameserver[__nameservers++] = strdup(argv[i]);
+ DPRINTF("adding nameserver %s\n", argv[i]);
+ }
}
- for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
- __searchdomain[__searchdomains++] = strdup(argv[i]);
- DPRINTF("adding search %s\n", argv[i]);
+
+ /* domain and search are mutually exclusive, the last one wins */
+ if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
+ while (__searchdomains > 0) {
+ free(__searchdomain[--__searchdomains]);
+ __searchdomain[__searchdomains] = NULL;
+ }
+ for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
+ __searchdomain[__searchdomains++] = strdup(argv[i]);
+ DPRINTF("adding search %s\n", argv[i]);
+ }
}
}
+ fclose(fp);
+ DPRINTF("nameservers = %d\n", __nameservers);
+ goto DONE;
}
- fclose(fp);
- DPRINTF("nameservers = %d\n", __nameservers);
- BIGUNLOCK;
- return 0;
- }
DPRINTF("failed to open %s\n", "resolv.conf");
h_errno = NO_RECOVERY;
- BIGUNLOCK;
- return -1;
+
+ rv = -1;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
+ return rv;
}
#endif
@@ -1044,7 +1032,7 @@ int __open_nameservers()
void __close_nameservers(void)
{
- BIGLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
while (__nameservers > 0) {
free(__nameserver[--__nameservers]);
__nameserver[__nameservers] = NULL;
@@ -1053,7 +1041,7 @@ void __close_nameservers(void)
free(__searchdomain[--__searchdomains]);
__searchdomain[__searchdomains] = NULL;
}
- BIGUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
}
#endif
@@ -1063,8 +1051,8 @@ struct hostent *gethostbyname(const char
{
static struct hostent h;
static char buf[sizeof(struct in_addr) +
- sizeof(struct in_addr *)*2 +
- sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
+ sizeof(struct in_addr *)*2 +
+ sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
struct hostent *hp;
gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
@@ -1082,8 +1070,8 @@ struct hostent *gethostbyname2(const cha
#else /* __UCLIBC_HAS_IPV6__ */
static struct hostent h;
static char buf[sizeof(struct in6_addr) +
- sizeof(struct in6_addr *)*2 +
- sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
+ sizeof(struct in6_addr *)*2 +
+ sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
struct hostent *hp;
gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
@@ -1119,7 +1107,7 @@ int res_init(void)
/** rp->rhook = NULL; **/
/** rp->_u._ext.nsinit = 0; **/
- BIGLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
if(__searchdomains) {
int i;
for(i=0; i<__searchdomains; i++) {
@@ -1139,7 +1127,7 @@ int res_init(void)
}
}
rp->nscount = __nameservers;
- BIGUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
return(0);
}
@@ -1175,10 +1163,10 @@ int res_query(const char *dname, int cla
memset((char *) &a, '\0', sizeof(a));
- BIGLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
__nameserversXX=__nameservers;
__nameserverXX=__nameserver;
- BIGUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
if (i < 0) {
@@ -1207,10 +1195,10 @@ int res_query(const char *dname, int cla
* is detected. Error code, if any, is left in h_errno.
*/
int res_search(name, class, type, answer, anslen)
- const char *name; /* domain name */
- int class, type; /* class and type of query */
- u_char *answer; /* buffer to put answer */
- int anslen; /* size of answer */
+ const char *name; /* domain name */
+ int class, type; /* class and type of query */
+ u_char *answer; /* buffer to put answer */
+ int anslen; /* size of answer */
{
const char *cp, * const *domain;
HEADER *hp = (HEADER *)(void *)answer;
@@ -1256,11 +1244,11 @@ int res_search(name, class, type, answer
int done = 0;
for (domain = (const char * const *)_res.dnsrch;
- *domain && !done;
- domain++) {
+ *domain && !done;
+ domain++) {
ret = res_querydomain(name, *domain, class, type,
- answer, anslen);
+ answer, anslen);
if (ret > 0)
return (ret);
@@ -1283,22 +1271,22 @@ int res_search(name, class, type, answer
}
switch (h_errno) {
- case NO_DATA:
- got_nodata++;
- /* FALLTHROUGH */
- case HOST_NOT_FOUND:
- /* keep trying */
- break;
- case TRY_AGAIN:
- if (hp->rcode == SERVFAIL) {
- /* try next search element, if any */
- got_servfail++;
+ case NO_DATA:
+ got_nodata++;
+ /* FALLTHROUGH */
+ case HOST_NOT_FOUND:
+ /* keep trying */
break;
- }
- /* FALLTHROUGH */
- default:
- /* anything else implies that we're done */
- done++;
+ case TRY_AGAIN:
+ if (hp->rcode == SERVFAIL) {
+ /* try next search element, if any */
+ got_servfail++;
+ break;
+ }
+ /* FALLTHROUGH */
+ default:
+ /* anything else implies that we're done */
+ done++;
}
/*
* if we got here for some reason other than DNSRCH,
@@ -1342,10 +1330,10 @@ int res_search(name, class, type, answer
* removing a trailing dot from name if domain is NULL.
*/
int res_querydomain(name, domain, class, type, answer, anslen)
- const char *name, *domain;
- int class, type; /* class and type of query */
- u_char *answer; /* buffer to put answer */
- int anslen; /* size of answer */
+ const char *name, *domain;
+ int class, type; /* class and type of query */
+ u_char *answer; /* buffer to put answer */
+ int anslen; /* size of answer */
{
char nbuf[MAXDNAME];
const char *longname = nbuf;
@@ -1359,7 +1347,7 @@ int res_querydomain(name, domain, class,
#ifdef DEBUG
if (_res.options & RES_DEBUG)
printf(";; res_querydomain(%s, %s, %d, %d)\n",
- name, domain?domain:"<Nil>", class, type);
+ name, domain?domain:"<Nil>", class, type);
#endif
if (domain == NULL) {
/*
@@ -1400,11 +1388,11 @@ struct hostent *gethostbyaddr (const voi
static struct hostent h;
static char buf[
#ifndef __UCLIBC_HAS_IPV6__
- sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
+ sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
#else
- sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
+ sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
#endif /* __UCLIBC_HAS_IPV6__ */
- sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
+ sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
struct hostent *hp;
gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
@@ -1425,11 +1413,11 @@ void __open_etc_hosts(FILE **fp)
}
int __read_etc_hosts_r(FILE * fp, const char * name, int type,
- enum etc_hosts_action action,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop)
+ enum etc_hosts_action action,
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop)
{
struct in_addr *in=NULL;
struct in_addr **addr_list=NULL;
@@ -1576,56 +1564,49 @@ int __read_etc_hosts_r(FILE * fp, const
#ifdef L_gethostent
-#ifdef __UCLIBC_HAS_THREADS__
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
static int __stay_open;
static FILE * __gethostent_fp;
void endhostent (void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
__stay_open = 0;
if (__gethostent_fp) {
- fclose(__gethostent_fp);
+ fclose(__gethostent_fp);
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
void sethostent (int stay_open)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
__stay_open = stay_open;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
- struct hostent **result, int *h_errnop)
+ struct hostent **result, int *h_errnop)
{
- int ret;
+ int ret = 0;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (__gethostent_fp == NULL) {
- __open_etc_hosts(&__gethostent_fp);
- if (__gethostent_fp == NULL) {
- UNLOCK;
- *result=NULL;
- return 0;
- }
+ __open_etc_hosts(&__gethostent_fp);
+ if (__gethostent_fp == NULL) {
+ *result=NULL;
+ goto DONE;
+ }
}
ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
- result_buf, buf, buflen, result, h_errnop);
+ result_buf, buf, buflen, result, h_errnop);
if (__stay_open==0) {
- fclose(__gethostent_fp);
+ fclose(__gethostent_fp);
}
- UNLOCK;
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return(ret);
}
@@ -1634,17 +1615,17 @@ struct hostent *gethostent (void)
static struct hostent h;
static char buf[
#ifndef __UCLIBC_HAS_IPV6__
- sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
+ sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
#else
- sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
+ sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
#endif /* __UCLIBC_HAS_IPV6__ */
- sizeof(char *)*(ALIAS_DIM) +
- 80/*namebuffer*/ + 2/* margin */];
+ sizeof(char *)*(ALIAS_DIM) +
+ 80/*namebuffer*/ + 2/* margin */];
struct hostent *host;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return(host);
}
#endif
@@ -1652,23 +1633,23 @@ struct hostent *gethostent (void)
#ifdef L_get_hosts_byname_r
int __get_hosts_byname_r(const char * name, int type,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop)
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop)
{
return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
- result_buf, buf, buflen, result, h_errnop));
+ result_buf, buf, buflen, result, h_errnop));
}
#endif
#ifdef L_get_hosts_byaddr_r
int __get_hosts_byaddr_r(const char * addr, int len, int type,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop)
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop)
{
#ifndef __UCLIBC_HAS_IPV6__
char ipaddr[INET_ADDRSTRLEN];
@@ -1677,24 +1658,24 @@ int __get_hosts_byaddr_r(const char * ad
#endif /* __UCLIBC_HAS_IPV6__ */
switch (type) {
- case AF_INET:
- if (len != sizeof(struct in_addr))
- return 0;
- break;
+ case AF_INET:
+ if (len != sizeof(struct in_addr))
+ return 0;
+ break;
#ifdef __UCLIBC_HAS_IPV6__
- case AF_INET6:
- if (len != sizeof(struct in6_addr))
- return 0;
- break;
+ case AF_INET6:
+ if (len != sizeof(struct in6_addr))
+ return 0;
+ break;
#endif /* __UCLIBC_HAS_IPV6__ */
- default:
- return 0;
+ default:
+ return 0;
}
inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
- result_buf, buf, buflen, result, h_errnop));
+ result_buf, buf, buflen, result, h_errnop));
}
#endif
@@ -1705,8 +1686,8 @@ int __get_hosts_byaddr_r(const char * ad
#endif /* min */
int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
- socklen_t hostlen, char *serv, socklen_t servlen,
- unsigned int flags)
+ socklen_t hostlen, char *serv, socklen_t servlen,
+ unsigned int flags)
{
int serrno = errno;
int ok = 0;
@@ -1720,167 +1701,167 @@ int getnameinfo (const struct sockaddr *
return EAI_FAMILY;
switch (sa->sa_family) {
- case AF_LOCAL:
- break;
- case AF_INET:
- if (addrlen < sizeof (struct sockaddr_in))
- return EAI_FAMILY;
- break;
+ case AF_LOCAL:
+ break;
+ case AF_INET:
+ if (addrlen < sizeof (struct sockaddr_in))
+ return EAI_FAMILY;
+ break;
#ifdef __UCLIBC_HAS_IPV6__
- case AF_INET6:
- if (addrlen < sizeof (struct sockaddr_in6))
- return EAI_FAMILY;
- break;
+ case AF_INET6:
+ if (addrlen < sizeof (struct sockaddr_in6))
+ return EAI_FAMILY;
+ break;
#endif /* __UCLIBC_HAS_IPV6__ */
- default:
- return EAI_FAMILY;
+ default:
+ return EAI_FAMILY;
}
if (host != NULL && hostlen > 0)
switch (sa->sa_family) {
- case AF_INET:
+ case AF_INET:
#ifdef __UCLIBC_HAS_IPV6__
- case AF_INET6:
+ case AF_INET6:
#endif /* __UCLIBC_HAS_IPV6__ */
- if (!(flags & NI_NUMERICHOST)) {
+ if (!(flags & NI_NUMERICHOST)) {
#ifdef __UCLIBC_HAS_IPV6__
- if (sa->sa_family == AF_INET6)
- h = gethostbyaddr ((const void *)
- &(((const struct sockaddr_in6 *) sa)->sin6_addr),
- sizeof(struct in6_addr), AF_INET6);
- else
-#endif /* __UCLIBC_HAS_IPV6__ */
- h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
- sizeof(struct in_addr), AF_INET);
-
- if (h) {
- char *c;
- if ((flags & NI_NOFQDN)
- && (getdomainname (domain, sizeof(domain)) == 0)
- && (c = strstr (h->h_name, domain))
- && (c != h->h_name) && (*(--c) == '.')) {
- strncpy (host, h->h_name,
- min(hostlen, (size_t) (c - h->h_name)));
- host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
- ok = 1;
- } else {
- strncpy (host, h->h_name, hostlen);
- ok = 1;
+ if (sa->sa_family == AF_INET6)
+ h = gethostbyaddr ((const void *)
+ &(((const struct sockaddr_in6 *) sa)->sin6_addr),
+ sizeof(struct in6_addr), AF_INET6);
+ else
+#endif /* __UCLIBC_HAS_IPV6__ */
+ h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
+ sizeof(struct in_addr), AF_INET);
+
+ if (h) {
+ char *c;
+ if ((flags & NI_NOFQDN)
+ && (getdomainname (domain, sizeof(domain)) == 0)
+ && (c = strstr (h->h_name, domain))
+ && (c != h->h_name) && (*(--c) == '.')) {
+ strncpy (host, h->h_name,
+ min(hostlen, (size_t) (c - h->h_name)));
+ host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
+ ok = 1;
+ } else {
+ strncpy (host, h->h_name, hostlen);
+ ok = 1;
+ }
}
- }
- }
+ }
- if (!ok) {
- if (flags & NI_NAMEREQD) {
- errno = serrno;
- return EAI_NONAME;
- } else {
- const char *c;
+ if (!ok) {
+ if (flags & NI_NAMEREQD) {
+ errno = serrno;
+ return EAI_NONAME;
+ } else {
+ const char *c;
#ifdef __UCLIBC_HAS_IPV6__
- if (sa->sa_family == AF_INET6) {
- const struct sockaddr_in6 *sin6p;
+ if (sa->sa_family == AF_INET6) {
+ const struct sockaddr_in6 *sin6p;
- sin6p = (const struct sockaddr_in6 *) sa;
+ sin6p = (const struct sockaddr_in6 *) sa;
- c = inet_ntop (AF_INET6,
- (const void *) &sin6p->sin6_addr, host, hostlen);
+ c = inet_ntop (AF_INET6,
+ (const void *) &sin6p->sin6_addr, host, hostlen);
#if 0
- /* Does scope id need to be supported? */
- uint32_t scopeid;
- scopeid = sin6p->sin6_scope_id;
- if (scopeid != 0) {
- /* Buffer is >= IFNAMSIZ+1. */
- char scopebuf[IFNAMSIZ + 1];
- char *scopeptr;
- int ni_numericscope = 0;
- size_t real_hostlen = __strnlen (host, hostlen);
- size_t scopelen = 0;
-
- scopebuf[0] = SCOPE_DELIMITER;
- scopebuf[1] = '\0';
- scopeptr = &scopebuf[1];
-
- if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
- || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
- if (if_indextoname (scopeid, scopeptr) == NULL)
+ /* Does scope id need to be supported? */
+ uint32_t scopeid;
+ scopeid = sin6p->sin6_scope_id;
+ if (scopeid != 0) {
+ /* Buffer is >= IFNAMSIZ+1. */
+ char scopebuf[IFNAMSIZ + 1];
+ char *scopeptr;
+ int ni_numericscope = 0;
+ size_t real_hostlen = __strnlen (host, hostlen);
+ size_t scopelen = 0;
+
+ scopebuf[0] = SCOPE_DELIMITER;
+ scopebuf[1] = '\0';
+ scopeptr = &scopebuf[1];
+
+ if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
+ || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
+ if (if_indextoname (scopeid, scopeptr) == NULL)
+ ++ni_numericscope;
+ else
+ scopelen = strlen (scopebuf);
+ } else {
++ni_numericscope;
- else
- scopelen = strlen (scopebuf);
- } else {
- ++ni_numericscope;
- }
+ }
- if (ni_numericscope)
- scopelen = 1 + snprintf (scopeptr,
- (scopebuf
- + sizeof scopebuf
- - scopeptr),
- "%u", scopeid);
-
- if (real_hostlen + scopelen + 1 > hostlen)
- return EAI_SYSTEM;
- memcpy (host + real_hostlen, scopebuf, scopelen + 1);
- }
+ if (ni_numericscope)
+ scopelen = 1 + snprintf (scopeptr,
+ (scopebuf
+ + sizeof scopebuf
+ - scopeptr),
+ "%u", scopeid);
+
+ if (real_hostlen + scopelen + 1 > hostlen)
+ return EAI_SYSTEM;
+ memcpy (host + real_hostlen, scopebuf, scopelen + 1);
+ }
#endif
- } else
+ } else
#endif /* __UCLIBC_HAS_IPV6__ */
- c = inet_ntop (AF_INET, (const void *)
- &(((const struct sockaddr_in *) sa)->sin_addr),
- host, hostlen);
-
- if (c == NULL) {
- errno = serrno;
- return EAI_SYSTEM;
+ c = inet_ntop (AF_INET, (const void *)
+ &(((const struct sockaddr_in *) sa)->sin_addr),
+ host, hostlen);
+
+ if (c == NULL) {
+ errno = serrno;
+ return EAI_SYSTEM;
+ }
}
+ ok = 1;
}
- ok = 1;
- }
- break;
-
- case AF_LOCAL:
- if (!(flags & NI_NUMERICHOST)) {
- struct utsname utsname;
+ break;
- if (!uname (&utsname)) {
- strncpy (host, utsname.nodename, hostlen);
- break;
+ case AF_LOCAL:
+ if (!(flags & NI_NUMERICHOST)) {
+ struct utsname utsname;
+
+ if (!uname (&utsname)) {
+ strncpy (host, utsname.nodename, hostlen);
+ break;
+ };
};
- };
- if (flags & NI_NAMEREQD) {
- errno = serrno;
- return EAI_NONAME;
- }
+ if (flags & NI_NAMEREQD) {
+ errno = serrno;
+ return EAI_NONAME;
+ }
- strncpy (host, "localhost", hostlen);
- break;
+ strncpy (host, "localhost", hostlen);
+ break;
- default:
- return EAI_FAMILY;
- }
+ default:
+ return EAI_FAMILY;
+ }
if (serv && (servlen > 0)) {
switch (sa->sa_family) {
- case AF_INET:
+ case AF_INET:
#ifdef __UCLIBC_HAS_IPV6__
- case AF_INET6:
+ case AF_INET6:
#endif /* __UCLIBC_HAS_IPV6__ */
- if (!(flags & NI_NUMERICSERV)) {
- struct servent *s;
- s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
- ((flags & NI_DGRAM) ? "udp" : "tcp"));
- if (s) {
- strncpy (serv, s->s_name, servlen);
- break;
+ if (!(flags & NI_NUMERICSERV)) {
+ struct servent *s;
+ s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
+ ((flags & NI_DGRAM) ? "udp" : "tcp"));
+ if (s) {
+ strncpy (serv, s->s_name, servlen);
+ break;
+ }
}
- }
- snprintf (serv, servlen, "%d",
- ntohs (((const struct sockaddr_in *) sa)->sin_port));
- break;
+ snprintf (serv, servlen, "%d",
+ ntohs (((const struct sockaddr_in *) sa)->sin_port));
+ break;
- case AF_LOCAL:
- strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
- break;
+ case AF_LOCAL:
+ strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
+ break;
}
}
if (host && (hostlen > 0))
@@ -1896,10 +1877,10 @@ int getnameinfo (const struct sockaddr *
#ifdef L_gethostbyname_r
int gethostbyname_r(const char * name,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop)
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop)
{
struct in_addr *in;
struct in_addr **addr_list;
@@ -1921,7 +1902,7 @@ int gethostbyname_r(const char * name,
__set_errno(0); /* to check for missing /etc/hosts. */
if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
- buf, buflen, result, h_errnop))==0)
+ buf, buflen, result, h_errnop))==0)
return i;
switch (*h_errnop) {
case HOST_NOT_FOUND:
@@ -1983,60 +1964,60 @@ int gethostbyname_r(const char * name,
for (;;) {
- BIGLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
__nameserversXX=__nameservers;
__nameserverXX=__nameserver;
- BIGUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
a.buf = buf;
a.buflen = buflen;
a.add_count = 0;
i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
if (i < 0) {
- *h_errnop = HOST_NOT_FOUND;
- DPRINTF("__dns_lookup\n");
- return TRY_AGAIN;
+ *h_errnop = HOST_NOT_FOUND;
+ DPRINTF("__dns_lookup\n");
+ return TRY_AGAIN;
}
if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen)
- {
- free(a.dotted);
- free(packet);
- *h_errnop = NETDB_INTERNAL;
- DPRINTF("buffer too small for all addresses\n");
- return ERANGE;
- }
+ {
+ free(a.dotted);
+ free(packet);
+ *h_errnop = NETDB_INTERNAL;
+ DPRINTF("buffer too small for all addresses\n");
+ return ERANGE;
+ }
else if(a.add_count > 0)
- {
- memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
- addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
- addr_list[0] = in;
- for (i = a.add_count-1; i>=0; --i)
- addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
- addr_list[a.add_count + 1] = 0;
- buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
- buf = (char*)&addr_list[a.add_count + 2];
- }
+ {
+ memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
+ addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
+ addr_list[0] = in;
+ for (i = a.add_count-1; i>=0; --i)
+ addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
+ addr_list[a.add_count + 1] = 0;
+ buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
+ buf = (char*)&addr_list[a.add_count + 2];
+ }
strncpy(buf, a.dotted, buflen);
free(a.dotted);
if (a.atype == T_A) { /* ADDRESS */
- memcpy(in, a.rdata, sizeof(*in));
- result_buf->h_name = buf;
- result_buf->h_addrtype = AF_INET;
- result_buf->h_length = sizeof(*in);
- result_buf->h_addr_list = (char **) addr_list;
+ memcpy(in, a.rdata, sizeof(*in));
+ result_buf->h_name = buf;
+ result_buf->h_addrtype = AF_INET;
+ result_buf->h_length = sizeof(*in);
+ result_buf->h_addr_list = (char **) addr_list;
#ifdef __UCLIBC_MJN3_ONLY__
#warning TODO -- generate the full list
#endif
- result_buf->h_aliases = alias; /* TODO: generate the full list */
- free(packet);
- break;
+ result_buf->h_aliases = alias; /* TODO: generate the full list */
+ free(packet);
+ break;
} else {
- free(packet);
- *h_errnop=HOST_NOT_FOUND;
- return TRY_AGAIN;
+ free(packet);
+ *h_errnop=HOST_NOT_FOUND;
+ return TRY_AGAIN;
}
}
@@ -2049,14 +2030,14 @@ int gethostbyname_r(const char * name,
#ifdef L_gethostbyname2_r
int gethostbyname2_r(const char *name, int family,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop)
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop)
{
#ifndef __UCLIBC_HAS_IPV6__
return family == (AF_INET)? gethostbyname_r(name, result_buf,
- buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
+ buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
#else /* __UCLIBC_HAS_IPV6__ */
struct in6_addr *in;
struct in6_addr **addr_list;
@@ -2084,7 +2065,7 @@ int gethostbyname2_r(const char *name, i
__set_errno(0); /* to check for missing /etc/hosts. */
if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
- buf, buflen, result, h_errnop))==0)
+ buf, buflen, result, h_errnop))==0)
return i;
switch (*h_errnop) {
case HOST_NOT_FOUND:
@@ -2137,10 +2118,10 @@ int gethostbyname2_r(const char *name, i
memset((char *) &a, '\0', sizeof(a));
for (;;) {
- BIGLOCK;
- __nameserversXX=__nameservers;
- __nameserverXX=__nameserver;
- BIGUNLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
+ __nameserversXX=__nameservers;
+ __nameserverXX=__nameserver;
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
@@ -2190,10 +2171,10 @@ int gethostbyname2_r(const char *name, i
#ifdef L_gethostbyaddr_r
int gethostbyaddr_r (const void *addr, socklen_t len, int type,
- struct hostent * result_buf,
- char * buf, size_t buflen,
- struct hostent ** result,
- int * h_errnop)
+ struct hostent * result_buf,
+ char * buf, size_t buflen,
+ struct hostent ** result,
+ int * h_errnop)
{
struct in_addr *in;
@@ -2234,7 +2215,7 @@ int gethostbyaddr_r (const void *addr, s
/* do /etc/hosts first */
if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
- buf, buflen, result, h_errnop))==0)
+ buf, buflen, result, h_errnop))==0)
return i;
switch (*h_errnop) {
case HOST_NOT_FOUND:
@@ -2294,7 +2275,7 @@ int gethostbyaddr_r (const void *addr, s
addr_list[0] = in;
sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
- tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
+ tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
#ifdef __UCLIBC_HAS_IPV6__
} else {
memcpy(in6->s6_addr, addr, len);
@@ -2304,7 +2285,7 @@ int gethostbyaddr_r (const void *addr, s
for (i = len - 1; i >= 0; i--) {
qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
- (in6->s6_addr[i] >> 4) & 0xf);
+ (in6->s6_addr[i] >> 4) & 0xf);
}
strcpy(qp, "ip6.int");
#endif /* __UCLIBC_HAS_IPV6__ */
@@ -2314,10 +2295,10 @@ int gethostbyaddr_r (const void *addr, s
for (;;) {
- BIGLOCK;
- __nameserversXX=__nameservers;
- __nameserverXX=__nameserver;
- BIGUNLOCK;
+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
+ __nameserversXX=__nameservers;
+ __nameserverXX=__nameserver;
+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
if (i < 0) {
@@ -2381,7 +2362,7 @@ int gethostbyaddr_r (const void *addr, s
* Return size of compressed name or -1 if there was an error.
*/
int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
- char *dst, int dstsiz)
+ char *dst, int dstsiz)
{
int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
@@ -2401,7 +2382,7 @@ int __dn_expand(const u_char *msg, const
*/
static int printable(int ch)
{
- return (ch > 0x20 && ch < 0x7f);
+ return (ch > 0x20 && ch < 0x7f);
}
/*
@@ -2413,18 +2394,18 @@ static int printable(int ch)
*/
static int special(int ch)
{
- switch (ch) {
+ switch (ch) {
case 0x22: /* '"' */
case 0x2E: /* '.' */
case 0x3B: /* ';' */
case 0x5C: /* '\\' */
- /* Special modifiers in zone files. */
+ /* Special modifiers in zone files. */
case 0x40: /* '@' */
case 0x24: /* '$' */
- return (1);
+ return (1);
default:
- return (0);
- }
+ return (0);
+ }
}
/*
@@ -2436,7 +2417,7 @@ static int special(int ch)
* Root domain returns as "." not "".
*/
int __ns_name_uncompress(const u_char *msg, const u_char *eom,
- const u_char *src, char *dst, size_t dstsiz)
+ const u_char *src, char *dst, size_t dstsiz)
{
u_char tmp[NS_MAXCDNAME];
int n;
@@ -2525,7 +2506,7 @@ int __ns_name_ntop(const u_char *src, ch
return (-1);
}
*dn++ = '\0';
- return (dn - dst);
+ return (dn - dst);
}
/*
@@ -2535,7 +2516,7 @@ int __ns_name_ntop(const u_char *src, ch
* -1 if it fails, or consumed octets if it succeeds.
*/
int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
- u_char *dst, size_t dstsiz)
+ u_char *dst, size_t dstsiz)
{
const u_char *srcp, *dstlim;
u_char *dstp;
@@ -2554,46 +2535,46 @@ int __ns_name_unpack(const u_char *msg,
while ((n = *srcp++) != 0) {
/* Check for indirection. */
switch (n & NS_CMPRSFLGS) {
- case 0:
- /* Limit checks. */
- if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- checked += n + 1;
- *dstp++ = n;
- memcpy(dstp, srcp, n);
- dstp += n;
- srcp += n;
- break;
+ case 0:
+ /* Limit checks. */
+ if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
+ __set_errno (EMSGSIZE);
+ return (-1);
+ }
+ checked += n + 1;
+ *dstp++ = n;
+ memcpy(dstp, srcp, n);
+ dstp += n;
+ srcp += n;
+ break;
- case NS_CMPRSFLGS:
- if (srcp >= eom) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- if (len < 0)
- len = srcp - src + 1;
- srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
- if (srcp < msg || srcp >= eom) { /* Out of range. */
- __set_errno (EMSGSIZE);
- return (-1);
- }
- checked += 2;
- /*
- * Check for loops in the compressed name;
- * if we've looked at the whole message,
- * there must be a loop.
- */
- if (checked >= eom - msg) {
- __set_errno (EMSGSIZE);
- return (-1);
- }
- break;
+ case NS_CMPRSFLGS:
+ if (srcp >= eom) {
+ __set_errno (EMSGSIZE);
+ return (-1);
+ }
+ if (len < 0)
+ len = srcp - src + 1;
+ srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
+ if (srcp < msg || srcp >= eom) { /* Out of range. */
+ __set_errno (EMSGSIZE);
+ return (-1);
+ }
+ checked += 2;
+ /*
+ * Check for loops in the compressed name;
+ * if we've looked at the whole message,
+ * there must be a loop.
+ */
+ if (checked >= eom - msg) {
+ __set_errno (EMSGSIZE);
+ return (-1);
+ }
+ break;
- default:
- __set_errno (EMSGSIZE);
- return (-1); /* flag error */
+ default:
+ __set_errno (EMSGSIZE);
+ return (-1); /* flag error */
}
}
*dstp = '\0';
diff --git a/libc/inet/rpc/create_xid.c b/libc/inet/rpc/create_xid.c
index cbb961e..c86cbb4 100644
--- a/libc/inet/rpc/create_xid.c
+++ b/libc/inet/rpc/create_xid.c
@@ -27,15 +27,7 @@
/* The RPC code is not threadsafe, but new code should be threadsafe. */
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t createxid_lock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&createxid_lock)
-# define UNLOCK __pthread_mutex_unlock(&createxid_lock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
static int is_initialized;
static struct drand48_data __rpc_lrand48_data;
@@ -43,22 +35,22 @@ static struct drand48_data __rpc_lrand48
unsigned long
_create_xid (void)
{
- unsigned long res;
+ unsigned long res;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
- if (!is_initialized)
- {
- struct timeval now;
+ if (!is_initialized)
+ {
+ struct timeval now;
- gettimeofday (&now, (struct timezone *) 0);
- srand48_r (now.tv_sec ^ now.tv_usec, &__rpc_lrand48_data);
- is_initialized = 1;
- }
+ gettimeofday (&now, (struct timezone *) 0);
+ srand48_r (now.tv_sec ^ now.tv_usec, &__rpc_lrand48_data);
+ is_initialized = 1;
+ }
- lrand48_r (&__rpc_lrand48_data, &res);
+ lrand48_r (&__rpc_lrand48_data, &res);
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
- return res;
+ return res;
}
diff --git a/libc/misc/dirent/closedir.c b/libc/misc/dirent/closedir.c
index 068e2d3..56adb23 100644
--- a/libc/misc/dirent/closedir.c
+++ b/libc/misc/dirent/closedir.c
@@ -4,7 +4,6 @@
#include <unistd.h>
#include "dirstream.h"
-
int closedir(DIR * dir)
{
int fd;
@@ -19,14 +18,10 @@ int closedir(DIR * dir)
__set_errno(EBADF);
return -1;
}
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_lock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
fd = dir->dd_fd;
dir->dd_fd = -1;
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_unlock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
free(dir->dd_buf);
free(dir);
return close(fd);
diff --git a/libc/misc/dirent/dirstream.h b/libc/misc/dirent/dirstream.h
index 2dd0264..bd721c5 100644
--- a/libc/misc/dirent/dirstream.h
+++ b/libc/misc/dirent/dirstream.h
@@ -26,9 +26,8 @@ Cambridge, MA 02139, USA. */
#include <features.h>
#include <sys/types.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-#endif
+
+#include <bits/uClibc_mutex.h>
/* For now, syscall readdir () only supports one entry at a time. It
* will be changed in the future.
@@ -63,11 +62,7 @@ struct __dirstream {
size_t dd_max;
/* lock */
-#ifdef __UCLIBC_HAS_THREADS__
- pthread_mutex_t dd_lock;
-#else
- void *dd_lock;
-#endif
+ __UCLIBC_MUTEX(dd_lock);
}; /* stream data from opendir() */
diff --git a/libc/misc/dirent/readdir.c b/libc/misc/dirent/readdir.c
index 1f196e1..c55317a 100644
--- a/libc/misc/dirent/readdir.c
+++ b/libc/misc/dirent/readdir.c
@@ -5,7 +5,6 @@
#include <dirent.h>
#include "dirstream.h"
-
struct dirent *readdir(DIR * dir)
{
ssize_t bytes;
@@ -16,9 +15,7 @@ struct dirent *readdir(DIR * dir)
return NULL;
}
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_lock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
do {
if (dir->dd_size <= dir->dd_nextloc) {
@@ -44,8 +41,6 @@ struct dirent *readdir(DIR * dir)
} while (de->d_ino == 0);
all_done:
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_unlock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
return de;
}
diff --git a/libc/misc/dirent/readdir64.c b/libc/misc/dirent/readdir64.c
index f798c6f..6da3b0d 100644
--- a/libc/misc/dirent/readdir64.c
+++ b/libc/misc/dirent/readdir64.c
@@ -20,7 +20,6 @@
#include <dirent.h>
#include "dirstream.h"
-
struct dirent64 *readdir64(DIR * dir)
{
ssize_t bytes;
@@ -31,9 +30,7 @@ struct dirent64 *readdir64(DIR * dir)
return NULL;
}
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_lock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
do {
if (dir->dd_size <= dir->dd_nextloc) {
@@ -59,9 +56,7 @@ struct dirent64 *readdir64(DIR * dir)
} while (de->d_ino == 0);
all_done:
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_unlock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
return de;
}
diff --git a/libc/misc/dirent/readdir64_r.c b/libc/misc/dirent/readdir64_r.c
index da3564e..cc96eff 100644
--- a/libc/misc/dirent/readdir64_r.c
+++ b/libc/misc/dirent/readdir64_r.c
@@ -19,7 +19,6 @@
#include <dirent.h>
#include "dirstream.h"
-
int readdir64_r(DIR *dir, struct dirent64 *entry, struct dirent64 **result)
{
int ret;
@@ -32,21 +31,19 @@ int readdir64_r(DIR *dir, struct dirent6
}
de = NULL;
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_lock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
do {
if (dir->dd_size <= dir->dd_nextloc) {
- /* read dir->dd_max bytes of directory entries. */
- bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
- if (bytes <= 0) {
- *result = NULL;
- ret = errno;
- goto all_done;
- }
- dir->dd_size = bytes;
- dir->dd_nextloc = 0;
+ /* read dir->dd_max bytes of directory entries. */
+ bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
+ if (bytes <= 0) {
+ *result = NULL;
+ ret = errno;
+ goto all_done;
+ }
+ dir->dd_size = bytes;
+ dir->dd_nextloc = 0;
}
de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
@@ -66,12 +63,10 @@ int readdir64_r(DIR *dir, struct dirent6
}
ret = 0;
-all_done:
+ all_done:
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_unlock(&(dir->dd_lock));
-#endif
- return((de != NULL)? 0 : ret);
+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
+ return((de != NULL)? 0 : ret);
}
#endif /* __UCLIBC_HAS_LFS__ */
diff --git a/libc/misc/dirent/readdir_r.c b/libc/misc/dirent/readdir_r.c
index 245dcbd..aeccdd8 100644
--- a/libc/misc/dirent/readdir_r.c
+++ b/libc/misc/dirent/readdir_r.c
@@ -5,7 +5,6 @@
#include <dirent.h>
#include "dirstream.h"
-
int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
{
int ret;
@@ -18,21 +17,19 @@ int readdir_r(DIR *dir, struct dirent *e
}
de = NULL;
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_lock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
do {
if (dir->dd_size <= dir->dd_nextloc) {
- /* read dir->dd_max bytes of directory entries. */
- bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
- if (bytes <= 0) {
- *result = NULL;
- ret = errno;
- goto all_done;
- }
- dir->dd_size = bytes;
- dir->dd_nextloc = 0;
+ /* read dir->dd_max bytes of directory entries. */
+ bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
+ if (bytes <= 0) {
+ *result = NULL;
+ ret = errno;
+ goto all_done;
+ }
+ dir->dd_size = bytes;
+ dir->dd_nextloc = 0;
}
de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
@@ -52,10 +49,8 @@ int readdir_r(DIR *dir, struct dirent *e
}
ret = 0;
-all_done:
+ all_done:
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_unlock(&(dir->dd_lock));
-#endif
- return((de != NULL)? 0 : ret);
+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
+ return((de != NULL)? 0 : ret);
}
diff --git a/libc/misc/dirent/rewinddir.c b/libc/misc/dirent/rewinddir.c
index 60ef71d..fe8fc2a 100644
--- a/libc/misc/dirent/rewinddir.c
+++ b/libc/misc/dirent/rewinddir.c
@@ -3,7 +3,6 @@
#include <unistd.h>
#include "dirstream.h"
-
/* rewinddir() just does an lseek(fd,0,0) - see close for comments */
void rewinddir(DIR * dir)
{
@@ -11,12 +10,8 @@ void rewinddir(DIR * dir)
__set_errno(EBADF);
return;
}
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_lock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
lseek(dir->dd_fd, 0, SEEK_SET);
dir->dd_nextoff = dir->dd_nextloc = dir->dd_size = 0;
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_unlock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
}
diff --git a/libc/misc/dirent/seekdir.c b/libc/misc/dirent/seekdir.c
index 139f1e1..6d6f5f0 100644
--- a/libc/misc/dirent/seekdir.c
+++ b/libc/misc/dirent/seekdir.c
@@ -3,19 +3,14 @@
#include <unistd.h>
#include "dirstream.h"
-
void seekdir(DIR * dir, long int offset)
{
if (!dir) {
__set_errno(EBADF);
return;
}
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_lock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
dir->dd_nextoff = lseek(dir->dd_fd, offset, SEEK_SET);
dir->dd_size = dir->dd_nextloc = 0;
-#ifdef __UCLIBC_HAS_THREADS__
- __pthread_mutex_unlock(&(dir->dd_lock));
-#endif
+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
}
diff --git a/libc/misc/mntent/mntent.c b/libc/misc/mntent/mntent.c
index d98a687..af6d848 100644
--- a/libc/misc/mntent/mntent.c
+++ b/libc/misc/mntent/mntent.c
@@ -3,15 +3,9 @@
#include <string.h>
#include <mntent.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+#include <bits/uClibc_mutex.h>
+
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
/* Reentrant version of getmntent. */
struct mntent *getmntent_r (FILE *filep,
@@ -67,7 +61,7 @@ struct mntent *getmntent(FILE * filep)
struct mntent *tmp;
static char *buff = NULL;
static struct mntent mnt;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (!buff) {
buff = malloc(BUFSIZ);
@@ -76,7 +70,7 @@ struct mntent *getmntent(FILE * filep)
}
tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return(tmp);
}
diff --git a/libc/misc/pthread/weaks.c b/libc/misc/pthread/weaks.c
index 89c2611..c27bd10 100644
--- a/libc/misc/pthread/weaks.c
+++ b/libc/misc/pthread/weaks.c
@@ -21,6 +21,7 @@
#include <limits.h>
#include <stdlib.h>
+static void __pthread_return_void __P ((void));
static int __pthread_return_0 __P ((void));
static int __pthread_return_1 __P ((void));
@@ -104,8 +105,17 @@ weak_alias (__pthread_return_0, __pthrea
weak_alias (__pthread_return_0, __pthread_mutex_trylock)
weak_alias (__pthread_return_0, __pthread_mutex_unlock)
+weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
+weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
+
/**********************************************************************/
+static void
+__pthread_return_void (void)
+{
+ return;
+}
+
static int
__pthread_return_0 (void)
{
diff --git a/libc/misc/syslog/syslog.c b/libc/misc/syslog/syslog.c
index 2b478e1..9e9ddbf 100644
--- a/libc/misc/syslog/syslog.c
+++ b/libc/misc/syslog/syslog.c
@@ -80,17 +80,9 @@
#include <ctype.h>
#include <signal.h>
+#include <bits/uClibc_mutex.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
-
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
static int LogFile = -1; /* fd for log */
static int connected; /* have done connect */
@@ -110,26 +102,26 @@ int setlogmask(int pmask);
static void
closelog_intern(int to_default)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (LogFile != -1) {
(void) close(LogFile);
}
LogFile = -1;
connected = 0;
if (to_default)
- {
- LogStat = 0;
- LogTag = "syslog";
- LogFacility = LOG_USER;
- LogMask = 0xff;
- }
- UNLOCK;
+ {
+ LogStat = 0;
+ LogTag = "syslog";
+ LogFacility = LOG_USER;
+ LogMask = 0xff;
+ }
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
static void
sigpipe_handler (int sig)
{
- closelog_intern (0);
+ closelog_intern (0);
}
/*
@@ -165,7 +157,7 @@ vsyslog( int pri, const char *fmt, va_li
saved_errno = errno;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
/* See if we should just throw out this message. */
if (!(LogMask & LOG_MASK(LOG_PRI(pri))) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
@@ -208,7 +200,7 @@ vsyslog( int pri, const char *fmt, va_li
if (p >= end || p < head_end) { /* Returned -1 in case of error... */
static const char truncate_msg[12] = "[truncated] ";
memmove(head_end + sizeof(truncate_msg), head_end,
- end - head_end - sizeof(truncate_msg));
+ end - head_end - sizeof(truncate_msg));
memcpy(head_end, truncate_msg, sizeof(truncate_msg));
if (p < head_end) {
while (p < end && *p) {
@@ -261,11 +253,11 @@ vsyslog( int pri, const char *fmt, va_li
(void)close(fd);
}
-getout:
- UNLOCK;
+ getout:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
if (sigpipe == 0)
sigaction (SIGPIPE, &oldaction,
- (struct sigaction *) NULL);
+ (struct sigaction *) NULL);
}
/*
@@ -276,48 +268,48 @@ openlog( const char *ident, int logstat,
{
int logType = SOCK_DGRAM;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (ident != NULL)
- LogTag = ident;
+ LogTag = ident;
LogStat = logstat;
if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
- LogFacility = logfac;
+ LogFacility = logfac;
if (LogFile == -1) {
- SyslogAddr.sa_family = AF_UNIX;
- (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
- sizeof(SyslogAddr.sa_data));
-retry:
- if (LogStat & LOG_NDELAY) {
- if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
- UNLOCK;
- return;
- }
- /* fcntl(LogFile, F_SETFD, 1); */
- }
+ SyslogAddr.sa_family = AF_UNIX;
+ (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
+ sizeof(SyslogAddr.sa_data));
+ retry:
+ if (LogStat & LOG_NDELAY) {
+ if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
+ goto DONE;
+ }
+ /* fcntl(LogFile, F_SETFD, 1); */
+ }
}
if (LogFile != -1 && !connected) {
- if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
- sizeof(SyslogAddr.sa_data) + strlen(SyslogAddr.sa_data)) != -1)
- {
- connected = 1;
- } else if (logType == SOCK_DGRAM) {
- logType = SOCK_STREAM;
- if (LogFile != -1) {
- close(LogFile);
- LogFile = -1;
- }
- goto retry;
- } else {
- if (LogFile != -1) {
- close(LogFile);
- LogFile = -1;
- }
- }
+ if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
+ sizeof(SyslogAddr.sa_data) + strlen(SyslogAddr.sa_data)) != -1)
+ {
+ connected = 1;
+ } else if (logType == SOCK_DGRAM) {
+ logType = SOCK_STREAM;
+ if (LogFile != -1) {
+ close(LogFile);
+ LogFile = -1;
+ }
+ goto retry;
+ } else {
+ if (LogFile != -1) {
+ close(LogFile);
+ LogFile = -1;
+ }
+ }
}
- UNLOCK;
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
/*
@@ -335,10 +327,10 @@ int setlogmask(int pmask)
int omask;
omask = LogMask;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (pmask != 0)
- LogMask = pmask;
- UNLOCK;
+ LogMask = pmask;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return (omask);
}
diff --git a/libc/misc/time/time.c b/libc/misc/time/time.c
index f43bb8a..6165a52 100644
--- a/libc/misc/time/time.c
+++ b/libc/misc/time/time.c
@@ -143,6 +143,8 @@
#include <locale.h>
#include <bits/uClibc_uintmaxtostr.h>
+#include <bits/uClibc_mutex.h>
+
#ifdef __UCLIBC_HAS_XLOCALE__
#include <xlocale.h>
#endif
@@ -191,21 +193,7 @@ typedef struct {
char tzname[TZNAME_MAX+1];
} rule_struct;
-#ifdef __UCLIBC_HAS_THREADS__
-
-#include <pthread.h>
-
-extern pthread_mutex_t _time_tzlock;
-
-#define TZLOCK __pthread_mutex_lock(&_time_tzlock)
-#define TZUNLOCK __pthread_mutex_unlock(&_time_tzlock)
-
-#else
-
-#define TZLOCK ((void) 0)
-#define TZUNLOCK ((void) 0)
-
-#endif
+__UCLIBC_MUTEX_EXTERN(_time_tzlock);
extern rule_struct _time_tzinfo[2];
@@ -542,13 +530,13 @@ struct tm *localtime(const time_t *timer
struct tm *localtime_r(register const time_t *__restrict timer,
register struct tm *__restrict result)
{
- TZLOCK;
+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
tzset();
__time_localtime_tzi(timer, result, _time_tzinfo);
- TZUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
return result;
}
@@ -1037,7 +1025,7 @@ size_t __XL(strftime)(char *__restrict s
goto LOOP;
}
- o = spec + 26; /* set to "????" */
+ o = ((const char *) spec) + 26; /* set to "????" */
if ((code & MASK_SPEC) == CALC_SPEC) {
if (*p == 's') {
@@ -1073,17 +1061,15 @@ size_t __XL(strftime)(char *__restrict s
#ifdef __UCLIBC_HAS_TM_EXTENSIONS__
-#define RSP_TZUNLOCK ((void) 0)
#define RSP_TZNAME timeptr->tm_zone
#define RSP_GMT_OFFSET (-timeptr->tm_gmtoff)
#else
-#define RSP_TZUNLOCK TZUNLOCK
#define RSP_TZNAME rsp->tzname
#define RSP_GMT_OFFSET rsp->gmt_offset
- TZLOCK;
+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
rsp = _time_tzinfo;
if (timeptr->tm_isdst > 0) {
@@ -1114,15 +1100,17 @@ size_t __XL(strftime)(char *__restrict s
}
#endif
o_count = SIZE_MAX;
- RSP_TZUNLOCK;
+/* RSP_TZUNLOCK; */
+#ifdef __UCLIBC_HAS_TM_EXTENSIONS__
goto OUTPUT;
+#endif
} else { /* z */
*s = '+';
if ((tzo = -RSP_GMT_OFFSET) < 0) {
tzo = -tzo;
*s = '-';
}
- RSP_TZUNLOCK;
+/* RSP_TZUNLOCK; */
++s;
--count;
@@ -1131,7 +1119,13 @@ size_t __XL(strftime)(char *__restrict s
i = 16 + 6; /* 0-fill, width = 4 */
}
-
+#ifdef __UCLIBC_HAS_TM_EXTENSIONS__
+#else
+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
+ if (*p == 'Z') {
+ goto OUTPUT;
+ }
+#endif
} else {
/* TODO: don't need year for U, W */
for (i=0 ; i < 3 ; i++) {
@@ -1664,9 +1658,7 @@ int daylight = 0;
long timezone = 0;
char *tzname[2] = { (char *) UTC, (char *) (UTC-1) };
-#ifdef __UCLIBC_HAS_THREADS__
-pthread_mutex_t _time_tzlock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-#endif
+__UCLIBC_MUTEX_INIT(_time_tzlock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
rule_struct _time_tzinfo[2];
@@ -1796,7 +1788,7 @@ void tzset(void)
static char oldval[TZ_BUFLEN]; /* BSS-zero'd. */
#endif /* __UCLIBC_HAS_TZ_CACHING__ */
- TZLOCK;
+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
e = getenv(TZ); /* TZ env var always takes precedence. */
@@ -1962,10 +1954,10 @@ void tzset(void)
daylight = !!_time_tzinfo[1].tzname[0];
timezone = _time_tzinfo[0].gmt_offset;
-#if defined(__UCLIBC_HAS_TZ_FILE__)
+#if defined(__UCLIBC_HAS_TZ_FILE__) || defined(__UCLIBC_HAS_TZ_CACHING__)
FAST_DONE:
#endif
- TZUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
}
#endif
@@ -2167,13 +2159,13 @@ time_t _time_mktime(struct tm *timeptr,
{
time_t t;
- TZLOCK;
+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
tzset();
t = _time_mktime_tzi(timeptr, store_on_success, _time_tzinfo);
- TZUNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
return t;
}
diff --git a/libc/misc/ttyent/getttyent.c b/libc/misc/ttyent/getttyent.c
index 6e2fbd2..c85c73a 100644
--- a/libc/misc/ttyent/getttyent.c
+++ b/libc/misc/ttyent/getttyent.c
@@ -35,9 +35,6 @@
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-#endif
static char zapchar;
static FILE *tf;
@@ -50,8 +47,8 @@ struct ttyent * getttynam(const char *tt
setttyent();
while ((t = getttyent()))
- if (!strcmp(tty, t->ty_name))
- break;
+ if (!strcmp(tty, t->ty_name))
+ break;
endttyent();
return (t);
}
@@ -67,27 +64,27 @@ static char * skip(register char *p)
register int c, q;
for (q = 0, t = p; (c = *p) != '\0'; p++) {
- if (c == '"') {
- q ^= QUOTED; /* obscure, but nice */
- continue;
- }
- if (q == QUOTED && *p == '\\' && *(p+1) == '"')
- p++;
- *t++ = *p;
- if (q == QUOTED)
- continue;
- if (c == '#') {
- zapchar = c;
- *p = 0;
- break;
- }
- if (c == '\t' || c == ' ' || c == '\n') {
- zapchar = c;
- *p++ = 0;
- while ((c = *p) == '\t' || c == ' ' || c == '\n')
- p++;
- break;
- }
+ if (c == '"') {
+ q ^= QUOTED; /* obscure, but nice */
+ continue;
+ }
+ if (q == QUOTED && *p == '\\' && *(p+1) == '"')
+ p++;
+ *t++ = *p;
+ if (q == QUOTED)
+ continue;
+ if (c == '#') {
+ zapchar = c;
+ *p = 0;
+ break;
+ }
+ if (c == '\t' || c == ' ' || c == '\n') {
+ zapchar = c;
+ *p++ = 0;
+ while ((c = *p) == '\t' || c == ' ' || c == '\n')
+ p++;
+ break;
+ }
}
*--t = '\0';
return (p);
@@ -104,46 +101,46 @@ struct ttyent * getttyent(void)
register int c;
register char *p;
static char *line = NULL;
+ struct ttyent *retval = NULL;
if (!tf && !setttyent())
- return (NULL);
+ return (NULL);
if (!line) {
- line = malloc(BUFSIZ);
+ line = malloc(BUFSIZ);
if (!line)
abort();
}
- __STDIO_ALWAYS_THREADLOCK(tf);
+ __STDIO_ALWAYS_THREADLOCK(tf);
for (;;) {
- if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
- __STDIO_ALWAYS_THREADUNLOCK(tf);
- return (NULL);
- }
- /* skip lines that are too big */
- if (!index(p, '\n')) {
- while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
- ;
- continue;
- }
- while (isspace(*p))
- ++p;
- if (*p && *p != '#')
- break;
+ if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
+ goto DONE;
+ }
+ /* skip lines that are too big */
+ if (!index(p, '\n')) {
+ while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
+ ;
+ continue;
+ }
+ while (isspace(*p))
+ ++p;
+ if (*p && *p != '#')
+ break;
}
zapchar = 0;
tty.ty_name = p;
p = skip(p);
if (!*(tty.ty_getty = p))
- tty.ty_getty = tty.ty_type = NULL;
+ tty.ty_getty = tty.ty_type = NULL;
else {
- p = skip(p);
- if (!*(tty.ty_type = p))
- tty.ty_type = NULL;
- else
- p = skip(p);
+ p = skip(p);
+ if (!*(tty.ty_type = p))
+ tty.ty_type = NULL;
+ else
+ p = skip(p);
}
tty.ty_status = 0;
tty.ty_window = NULL;
@@ -151,43 +148,45 @@ struct ttyent * getttyent(void)
#define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
#define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
for (; *p; p = skip(p)) {
- if (scmp(_TTYS_OFF))
- tty.ty_status &= ~TTY_ON;
- else if (scmp(_TTYS_ON))
- tty.ty_status |= TTY_ON;
- else if (scmp(_TTYS_SECURE))
- tty.ty_status |= TTY_SECURE;
- else if (vcmp(_TTYS_WINDOW))
- tty.ty_window = value(p);
- else
- break;
+ if (scmp(_TTYS_OFF))
+ tty.ty_status &= ~TTY_ON;
+ else if (scmp(_TTYS_ON))
+ tty.ty_status |= TTY_ON;
+ else if (scmp(_TTYS_SECURE))
+ tty.ty_status |= TTY_SECURE;
+ else if (vcmp(_TTYS_WINDOW))
+ tty.ty_window = value(p);
+ else
+ break;
}
- /* We can release the lock only here since `zapchar' is global. */
- __STDIO_ALWAYS_THREADUNLOCK(tf);
if (zapchar == '#' || *p == '#')
- while ((c = *++p) == ' ' || c == '\t')
- ;
+ while ((c = *++p) == ' ' || c == '\t')
+ ;
tty.ty_comment = p;
if (*p == 0)
- tty.ty_comment = 0;
+ tty.ty_comment = 0;
if ((p = index(p, '\n')))
- *p = '\0';
- return (&tty);
+ *p = '\0';
+ retval = &tty;
+
+ DONE:
+ __STDIO_ALWAYS_THREADUNLOCK(tf);
+ return retval;
}
int setttyent(void)
{
if (tf) {
- rewind(tf);
- return (1);
+ rewind(tf);
+ return (1);
} else if ((tf = fopen(_PATH_TTYS, "r"))) {
- /* We do the locking ourselves. */
+ /* We do the locking ourselves. */
#ifdef __UCLIBC_HAS_THREADS__
- __fsetlocking (tf, FSETLOCKING_BYCALLER);
+ __fsetlocking (tf, FSETLOCKING_BYCALLER);
#endif
- return (1);
+ return (1);
}
return (0);
}
@@ -197,9 +196,9 @@ int endttyent(void)
int rval;
if (tf) {
- rval = !(fclose(tf) == EOF);
- tf = NULL;
- return (rval);
+ rval = !(fclose(tf) == EOF);
+ tf = NULL;
+ return (rval);
}
return (1);
}
diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c
index c1d8d6f..0fc6df4 100644
--- a/libc/misc/utmp/utent.c
+++ b/libc/misc/utmp/utent.c
@@ -20,19 +20,9 @@
#include <string.h>
#include <utmp.h>
+#include <bits/uClibc_mutex.h>
-
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t utmplock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&utmplock)
-# define UNLOCK __pthread_mutex_unlock(&utmplock)
-#else
-# define LOCK
-# define UNLOCK
-#endif
-
-
+__UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
/* Some global crap */
static int static_fd = -1;
@@ -46,19 +36,19 @@ static struct utmp *__getutent(int utmp_
{
if (utmp_fd == -1) {
- setutent();
+ setutent();
}
if (utmp_fd == -1) {
- return NULL;
+ return NULL;
}
- LOCK;
+ __UCLIBC_MUTEX_LOCK(utmplock);
if (read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) != sizeof(struct utmp))
- {
- return NULL;
- }
+ {
+ return NULL;
+ }
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(utmplock);
return &static_utmp;
}
@@ -66,39 +56,39 @@ void setutent(void)
{
int ret;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(utmplock);
if (static_fd == -1) {
- if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
- if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
- goto bummer;
- }
- }
- /* Make sure the file will be closed on exec() */
- ret = fcntl(static_fd, F_GETFD, 0);
- if (ret >= 0) {
- ret = fcntl(static_fd, F_GETFD, 0);
- }
- if (ret < 0) {
-bummer:
- UNLOCK;
- static_fd = -1;
- close(static_fd);
- return;
- }
+ if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
+ if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
+ goto bummer;
+ }
+ }
+ /* Make sure the file will be closed on exec() */
+ ret = fcntl(static_fd, F_GETFD, 0);
+ if (ret >= 0) {
+ ret = fcntl(static_fd, F_GETFD, 0);
+ }
+ if (ret < 0) {
+ bummer:
+ close(static_fd);
+ static_fd = -1;
+ goto DONE;
+ }
}
lseek(static_fd, 0, SEEK_SET);
- UNLOCK;
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(utmplock);
return;
}
void endutent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(utmplock);
if (static_fd != -1) {
- close(static_fd);
+ close(static_fd);
}
static_fd = -1;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(utmplock);
}
/* Locking is done in __getutent */
@@ -113,22 +103,22 @@ struct utmp *getutid (const struct utmp
struct utmp *lutmp;
while ((lutmp = __getutent(static_fd)) != NULL) {
- if ( (utmp_entry->ut_type == RUN_LVL ||
- utmp_entry->ut_type == BOOT_TIME ||
- utmp_entry->ut_type == NEW_TIME ||
- utmp_entry->ut_type == OLD_TIME) &&
- lutmp->ut_type == utmp_entry->ut_type)
- {
- return lutmp;
- }
- if ( (utmp_entry->ut_type == INIT_PROCESS ||
- utmp_entry->ut_type == DEAD_PROCESS ||
- utmp_entry->ut_type == LOGIN_PROCESS ||
- utmp_entry->ut_type == USER_PROCESS) &&
- !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
- {
- return lutmp;
- }
+ if ( (utmp_entry->ut_type == RUN_LVL ||
+ utmp_entry->ut_type == BOOT_TIME ||
+ utmp_entry->ut_type == NEW_TIME ||
+ utmp_entry->ut_type == OLD_TIME) &&
+ lutmp->ut_type == utmp_entry->ut_type)
+ {
+ return lutmp;
+ }
+ if ( (utmp_entry->ut_type == INIT_PROCESS ||
+ utmp_entry->ut_type == DEAD_PROCESS ||
+ utmp_entry->ut_type == LOGIN_PROCESS ||
+ utmp_entry->ut_type == USER_PROCESS) &&
+ !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
+ {
+ return lutmp;
+ }
}
return NULL;
@@ -140,11 +130,11 @@ struct utmp *getutline(const struct utmp
struct utmp *lutmp;
while ((lutmp = __getutent(static_fd)) != NULL) {
- if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
- !strcmp(lutmp->ut_line, utmp_entry->ut_line))
- {
- return lutmp;
- }
+ if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
+ !strcmp(lutmp->ut_line, utmp_entry->ut_line))
+ {
+ return lutmp;
+ }
}
return NULL;
@@ -152,42 +142,42 @@ struct utmp *getutline(const struct utmp
struct utmp *pututline (const struct utmp *utmp_entry)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(utmplock);
/* Ignore the return value. That way, if they've already positioned
the file pointer where they want it, everything will work out. */
lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
if (getutid(utmp_entry) != NULL) {
- lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
- if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
- return NULL;
+ lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
+ if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
+ return NULL;
} else {
- lseek(static_fd, (off_t) 0, SEEK_END);
- if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
- return NULL;
+ lseek(static_fd, (off_t) 0, SEEK_END);
+ if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
+ return NULL;
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(utmplock);
return (struct utmp *)utmp_entry;
}
int utmpname (const char *new_ut_name)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(utmplock);
if (new_ut_name != NULL) {
- if (static_ut_name != default_file_name)
- free((char *)static_ut_name);
- static_ut_name = strdup(new_ut_name);
- if (static_ut_name == NULL) {
- /* We should probably whine about out-of-memory
- * errors here... Instead just reset to the default */
- static_ut_name = default_file_name;
- }
+ if (static_ut_name != default_file_name)
+ free((char *)static_ut_name);
+ static_ut_name = strdup(new_ut_name);
+ if (static_ut_name == NULL) {
+ /* We should probably whine about out-of-memory
+ * errors here... Instead just reset to the default */
+ static_ut_name = default_file_name;
+ }
}
if (static_fd != -1)
- close(static_fd);
- UNLOCK;
+ close(static_fd);
+ __UCLIBC_MUTEX_UNLOCK(utmplock);
return 0;
}
diff --git a/libc/misc/wchar/wstdio.c b/libc/misc/wchar/wstdio.c
index b49494f..408c57a 100644
--- a/libc/misc/wchar/wstdio.c
+++ b/libc/misc/wchar/wstdio.c
@@ -82,9 +82,6 @@ strong_alias(NAME,NAME##_unlocked) \
void NAME PARAMS
#endif
-#define __STDIO_THREADLOCK_OPENLIST
-#define __STDIO_THREADUNLOCK_OPENLIST
-
#else /* __UCLIBC_HAS_THREADS__ */
#include <pthread.h>
@@ -112,15 +109,6 @@ void NAME PARAMS \
} \
void NAME##_unlocked PARAMS
-#define __STDIO_THREADLOCK_OPENLIST \
- __pthread_mutex_lock(&_stdio_openlist_lock)
-
-#define __STDIO_THREADUNLOCK_OPENLIST \
- __pthread_mutex_unlock(&_stdio_openlist_lock)
-
-#define __STDIO_THREADTRYLOCK_OPENLIST \
- __pthread_mutex_trylock(&_stdio_openlist_lock)
-
#endif /* __UCLIBC_HAS_THREADS__ */
#ifndef __STDIO_BUFFERS
diff --git a/libc/pwd_grp/lckpwdf.c b/libc/pwd_grp/lckpwdf.c
index 6b9c251..063fed4 100644
--- a/libc/pwd_grp/lckpwdf.c
+++ b/libc/pwd_grp/lckpwdf.c
@@ -27,15 +27,9 @@
#include <sys/file.h>
#include <paths.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+#include <bits/uClibc_mutex.h>
+
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
/* How long to wait for getting the lock before returning with an
error. */
@@ -57,18 +51,18 @@ int lckpwdf (void)
struct sigaction new_act; /* New signal action. */
struct flock fl; /* Information struct for locking. */
int result;
+ int rv = -1;
if (lock_fd != -1)
/* Still locked by own process. */
return -1;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
lock_fd = open (_PATH_PASSWD, O_WRONLY);
if (lock_fd == -1) {
/* Cannot create lock file. */
- UNLOCK;
- return -1;
+ goto DONE;
}
/* Make sure file gets correctly closed when process finished. */
@@ -77,16 +71,14 @@ int lckpwdf (void)
/* Cannot get file flags. */
close(lock_fd);
lock_fd = -1;
- UNLOCK;
- return -1;
+ goto DONE;
}
flags |= FD_CLOEXEC; /* Close on exit. */
if (fcntl (lock_fd, F_SETFD, flags) < 0) {
/* Cannot set new flags. */
close(lock_fd);
lock_fd = -1;
- UNLOCK;
- return -1;
+ goto DONE;
}
/* Now we have to get exclusive write access. Since multiple
@@ -107,8 +99,7 @@ int lckpwdf (void)
/* Cannot install signal handler. */
close(lock_fd);
lock_fd = -1;
- UNLOCK;
- return -1;
+ goto DONE;
}
/* Now make sure the alarm signal is not blocked. */
@@ -118,8 +109,7 @@ int lckpwdf (void)
sigaction (SIGALRM, &saved_act, NULL);
close(lock_fd);
lock_fd = -1;
- UNLOCK;
- return -1;
+ goto DONE;
}
/* Start timer. If we cannot get the lock in the specified time we
@@ -146,12 +136,14 @@ int lckpwdf (void)
if (result < 0) {
close(lock_fd);
lock_fd = -1;
- UNLOCK;
- return -1;
+ goto DONE;
}
- UNLOCK;
- return 0;
+ rv = 0;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
+ return rv;
}
@@ -164,11 +156,11 @@ int ulckpwdf (void)
result = -1;
}
else {
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
result = close (lock_fd);
/* Mark descriptor as unused. */
lock_fd = -1;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
return result;
diff --git a/libc/pwd_grp/pwd_grp.c b/libc/pwd_grp/pwd_grp.c
index 91c0d83..a302c7c 100644
--- a/libc/pwd_grp/pwd_grp.c
+++ b/libc/pwd_grp/pwd_grp.c
@@ -42,9 +42,8 @@
#include <pwd.h>
#include <grp.h>
#include <shadow.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-#endif
+
+#include <bits/uClibc_mutex.h>
/**********************************************************************/
/* Sizes for staticly allocated buffers. */
@@ -445,34 +444,27 @@ int getpw(uid_t uid, char *buf)
/**********************************************************************/
#ifdef L_getpwent_r
-#ifdef __UCLIBC_HAS_THREADS__
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK ((void) 0)
-# define UNLOCK ((void) 0)
-#endif
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
static FILE *pwf /*= NULL*/;
void setpwent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (pwf) {
rewind(pwf);
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
void endpwent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (pwf) {
fclose(pwf);
pwf = NULL;
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
@@ -482,7 +474,7 @@ int getpwent_r(struct passwd *__restrict
{
int rv;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
*result = NULL; /* In case of error... */
@@ -500,7 +492,7 @@ int getpwent_r(struct passwd *__restrict
}
ERR:
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return rv;
}
@@ -509,34 +501,27 @@ int getpwent_r(struct passwd *__restrict
/**********************************************************************/
#ifdef L_getgrent_r
-#ifdef __UCLIBC_HAS_THREADS__
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK ((void) 0)
-# define UNLOCK ((void) 0)
-#endif
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
static FILE *grf /*= NULL*/;
void setgrent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (grf) {
rewind(grf);
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
void endgrent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (grf) {
fclose(grf);
grf = NULL;
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
int getgrent_r(struct group *__restrict resultbuf,
@@ -545,7 +530,7 @@ int getgrent_r(struct group *__restrict
{
int rv;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
*result = NULL; /* In case of error... */
@@ -563,7 +548,7 @@ int getgrent_r(struct group *__restrict
}
ERR:
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return rv;
}
@@ -572,34 +557,27 @@ int getgrent_r(struct group *__restrict
/**********************************************************************/
#ifdef L_getspent_r
-#ifdef __UCLIBC_HAS_THREADS__
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK ((void) 0)
-# define UNLOCK ((void) 0)
-#endif
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
static FILE *spf /*= NULL*/;
void setspent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (spf) {
rewind(spf);
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
void endspent(void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (spf) {
fclose(spf);
spf = NULL;
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
int getspent_r(struct spwd *resultbuf, char *buffer,
@@ -607,7 +585,7 @@ int getspent_r(struct spwd *resultbuf, c
{
int rv;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
*result = NULL; /* In case of error... */
@@ -625,7 +603,7 @@ int getspent_r(struct spwd *resultbuf, c
}
ERR:
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return rv;
}
diff --git a/libc/stdio/_READ.c b/libc/stdio/_READ.c
index 7d3c38c..fe1bc91 100644
--- a/libc/stdio/_READ.c
+++ b/libc/stdio/_READ.c
@@ -41,7 +41,7 @@ size_t __stdio_READ(register FILE *strea
#warning EINTR?
#endif
/* RETRY: */
- if ((rv = __READ(stream, buf, bufsize)) <= 0) {
+ if ((rv = __READ(stream, (char *) buf, bufsize)) <= 0) {
if (rv == 0) {
__STDIO_STREAM_SET_EOF(stream);
} else {
diff --git a/libc/stdio/_WRITE.c b/libc/stdio/_WRITE.c
index d300d39..4131eb7 100644
--- a/libc/stdio/_WRITE.c
+++ b/libc/stdio/_WRITE.c
@@ -47,7 +47,7 @@ size_t __stdio_WRITE(register FILE *stre
return bufsize;
}
stodo = (todo <= SSIZE_MAX) ? todo : SSIZE_MAX;
- if ((rv = __WRITE(stream, buf, stodo)) >= 0) {
+ if ((rv = __WRITE(stream, (char *) buf, stodo)) >= 0) {
#ifdef __UCLIBC_MJN3_ONLY__
#warning TODO: Make custom stream write return check optional.
#endif
diff --git a/libc/stdio/_fopen.c b/libc/stdio/_fopen.c
index f7f5bb6..4984f11 100644
--- a/libc/stdio/_fopen.c
+++ b/libc/stdio/_fopen.c
@@ -194,10 +194,23 @@ FILE *_stdio_fopen(intptr_t fname_or_mod
#endif
#ifdef __STDIO_HAS_OPENLIST
- __STDIO_THREADLOCK_OPENLIST;
- stream->__nextopen = _stdio_openlist; /* New files are inserted at */
- _stdio_openlist = stream; /* the head of the list. */
- __STDIO_THREADUNLOCK_OPENLIST;
+#if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
+ if (!(stream->__modeflags & __FLAG_FREEFILE))
+ {
+ /* An freopen call so the file was never removed from the list. */
+ }
+ else
+#endif
+ {
+ /* We have to lock the del mutex in case another thread wants to fclose()
+ * the last file. */
+ __STDIO_THREADLOCK_OPENLIST_DEL;
+ __STDIO_THREADLOCK_OPENLIST_ADD;
+ stream->__nextopen = _stdio_openlist; /* New files are inserted at */
+ _stdio_openlist = stream; /* the head of the list. */
+ __STDIO_THREADUNLOCK_OPENLIST_ADD;
+ __STDIO_THREADUNLOCK_OPENLIST_DEL;
+ }
#endif
__STDIO_STREAM_VALIDATE(stream);
diff --git a/libc/stdio/_stdio.c b/libc/stdio/_stdio.c
index 4aae3c4..9cfe02c 100644
--- a/libc/stdio/_stdio.c
+++ b/libc/stdio/_stdio.c
@@ -151,8 +151,12 @@ FILE *__stdout = _stdio_streams + 1; /*
FILE *_stdio_openlist = _stdio_streams;
# ifdef __UCLIBC_HAS_THREADS__
-pthread_mutex_t _stdio_openlist_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-int _stdio_openlist_delflag = 0;
+__UCLIBC_MUTEX_INIT(_stdio_openlist_add_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
+#ifdef __STDIO_BUFFERS
+__UCLIBC_MUTEX_INIT(_stdio_openlist_del_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
+volatile int _stdio_openlist_use_count = 0;
+int _stdio_openlist_del_count = 0;
+#endif
# endif
#endif
@@ -162,10 +166,10 @@ int _stdio_openlist_delflag = 0;
/* 2 if threading not initialized and 0 otherwise; */
int _stdio_user_locking = 2;
-void __stdio_init_mutex(pthread_mutex_t *m)
+void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m)
{
- static const pthread_mutex_t __stdio_mutex_initializer
- = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+ const __UCLIBC_MUTEX_STATIC(__stdio_mutex_initializer,
+ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
}
@@ -184,7 +188,11 @@ void _stdio_term(void)
* locked, then I suppose there is a chance that a pointer in the
* chain might be corrupt due to a partial store.
*/
- __stdio_init_mutex(&_stdio_openlist_lock);
+ __stdio_init_mutex(&_stdio_openlist_add_lock);
+#warning check
+#ifdef __STDIO_BUFFERS
+ __stdio_init_mutex(&_stdio_openlist_del_lock);
+#endif
/* Next we need to worry about the streams themselves. If a stream
* is currently locked, then it may be in an invalid state. So we
@@ -192,7 +200,7 @@ void _stdio_term(void)
* Then we reinitialize the locks.
*/
for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
- if (__STDIO_ALWAYS_THREADTRYLOCK(ptr)) {
+ if (__STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(ptr)) {
/* The stream is already locked, so we don't want to touch it.
* However, if we have custom streams, we can't just close it
* or leave it locked since a custom stream may be stacked
@@ -258,10 +266,6 @@ void _stdio_init(void)
#error Assumption violated about __MASK_READING and __FLAG_UNGOT
#endif
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-#endif
-
#ifndef NDEBUG
void _stdio_validate_FILE(const FILE *stream)
diff --git a/libc/stdio/_stdio.h b/libc/stdio/_stdio.h
index e3c2c58..decf57d 100644
--- a/libc/stdio/_stdio.h
+++ b/libc/stdio/_stdio.h
@@ -22,23 +22,57 @@
#include <wchar.h>
#endif
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
+#include <bits/uClibc_mutex.h>
-#define __STDIO_THREADLOCK_OPENLIST \
- __pthread_mutex_lock(&_stdio_openlist_lock)
+#define __STDIO_THREADLOCK_OPENLIST_ADD \
+ __UCLIBC_MUTEX_LOCK(_stdio_openlist_add_lock)
-#define __STDIO_THREADUNLOCK_OPENLIST \
- __pthread_mutex_unlock(&_stdio_openlist_lock)
+#define __STDIO_THREADUNLOCK_OPENLIST_ADD \
+ __UCLIBC_MUTEX_UNLOCK(_stdio_openlist_add_lock)
-#define __STDIO_THREADTRYLOCK_OPENLIST \
- __pthread_mutex_trylock(&_stdio_openlist_lock)
+#ifdef __STDIO_BUFFERS
-#else
+#define __STDIO_THREADLOCK_OPENLIST_DEL \
+ __UCLIBC_MUTEX_LOCK(_stdio_openlist_del_lock)
+
+#define __STDIO_THREADUNLOCK_OPENLIST_DEL \
+ __UCLIBC_MUTEX_UNLOCK(_stdio_openlist_del_lock)
-#define __STDIO_THREADLOCK_OPENLIST ((void)0)
-#define __STDIO_THREADUNLOCK_OPENLIST ((void)0)
+#define __STDIO_OPENLIST_INC_USE \
+do { \
+ __STDIO_THREADLOCK_OPENLIST_DEL; \
+ ++_stdio_openlist_use_count; \
+ __STDIO_THREADUNLOCK_OPENLIST_DEL; \
+} while (0)
+
+extern void _stdio_openlist_dec_use(void);
+
+#define __STDIO_OPENLIST_DEC_USE \
+ _stdio_openlist_dec_use()
+
+#define __STDIO_OPENLIST_INC_DEL_CNT \
+do { \
+ __STDIO_THREADLOCK_OPENLIST_DEL; \
+ ++_stdio_openlist_del_count; \
+ __STDIO_THREADUNLOCK_OPENLIST_DEL; \
+} while (0)
+
+#define __STDIO_OPENLIST_DEC_DEL_CNT \
+do { \
+ __STDIO_THREADLOCK_OPENLIST_DEL; \
+ --_stdio_openlist_del_count; \
+ __STDIO_THREADUNLOCK_OPENLIST_DEL; \
+} while (0)
+
+#endif /* __STDIO_BUFFERS */
+#ifndef __STDIO_THREADLOCK_OPENLIST_DEL
+#define __STDIO_THREADLOCK_OPENLIST_DEL ((void)0)
+#define __STDIO_THREADUNLOCK_OPENLIST_DEL ((void)0)
+#define __STDIO_OPENLIST_INC_USE ((void)0)
+#define __STDIO_OPENLIST_DEC_USE ((void)0)
+#define __STDIO_OPENLIST_INC_DEL_CNT ((void)0)
+#define __STDIO_OPENLIST_DEC_DEL_CNT ((void)0)
#endif
#define __UNDEFINED_OR_NONPORTABLE ((void)0)
diff --git a/libc/stdio/fclose.c b/libc/stdio/fclose.c
index 4df2e42..dfababc 100644
--- a/libc/stdio/fclose.c
+++ b/libc/stdio/fclose.c
@@ -12,30 +12,34 @@ int fclose(register FILE *stream)
int rv = 0;
__STDIO_AUTO_THREADLOCK_VAR;
- /* First, remove the file from the open file list. */
-#ifdef __STDIO_HAS_OPENLIST
- {
- register FILE *ptr;
-
- __STDIO_THREADLOCK_OPENLIST;
- if ((ptr = _stdio_openlist) == stream) {
- _stdio_openlist = stream->__nextopen;
- } else {
- while (ptr) {
- if (ptr->__nextopen == stream) {
- ptr->__nextopen = stream->__nextopen;
- break;
- }
- ptr = ptr->__nextopen;
- }
- }
- __STDIO_THREADUNLOCK_OPENLIST;
-
- if (!ptr) { /* Did not find stream in the open file list! */
- return EOF;
- }
- }
-#endif
+#warning dead code... but may want to simply check and not remove
+/* #ifdef __STDIO_HAS_OPENLIST */
+/* #if !defined(__UCLIBC_HAS_THREADS__) || !defined(__STDIO_BUFFERS) */
+/* /\* First, remove the file from the open file list. *\/ */
+/* { */
+/* register FILE *ptr; */
+
+/* __STDIO_THREADLOCK_OPENLIST; */
+/* if ((ptr = _stdio_openlist) == stream) { */
+/* #warning does a mod!!! */
+/* _stdio_openlist = stream->__nextopen; */
+/* } else { */
+/* while (ptr) { */
+/* if (ptr->__nextopen == stream) { */
+/* ptr->__nextopen = stream->__nextopen; */
+/* break; */
+/* } */
+/* ptr = ptr->__nextopen; */
+/* } */
+/* } */
+/* __STDIO_THREADUNLOCK_OPENLIST; */
+
+/* if (!ptr) { /\* Did not find stream in the open file list! *\/ */
+/* return EOF; */
+/* } */
+/* } */
+/* #endif */
+/* #endif */
__STDIO_AUTO_THREADLOCK(stream);
@@ -80,7 +84,15 @@ int fclose(register FILE *stream)
__STDIO_AUTO_THREADUNLOCK(stream);
__STDIO_STREAM_FREE_BUFFER(stream);
+#warning... inefficient - locks and unlocks twice and walks whole list
+#if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
+ /* inefficient - locks/unlocks twice and walks whole list */
+ __STDIO_OPENLIST_INC_USE;
+ __STDIO_OPENLIST_INC_DEL_CNT;
+ __STDIO_OPENLIST_DEC_USE; /* This with free the file if necessary. */
+#else
__STDIO_STREAM_FREE_FILE(stream);
+#endif
return rv;
}
diff --git a/libc/stdio/fcloseall.c b/libc/stdio/fcloseall.c
index dbb6000..f62281a 100644
--- a/libc/stdio/fcloseall.c
+++ b/libc/stdio/fcloseall.c
@@ -19,14 +19,34 @@ int fcloseall (void)
#ifdef __STDIO_HAS_OPENLIST
int retval = 0;
+ FILE *f;
- __STDIO_THREADLOCK_OPENLIST;
- while (_stdio_openlist) {
- if (fclose(_stdio_openlist)) {
+#warning remove dead code
+/* __STDIO_THREADLOCK_OPENLIST; */
+/* while (_stdio_openlist) { */
+/* if (fclose(_stdio_openlist)) { */
+/* retval = EOF; */
+/* } */
+/* } */
+/* __STDIO_THREADUNLOCK_OPENLIST; */
+
+ __STDIO_OPENLIST_INC_USE;
+
+#warning should probably have a get_head() operation
+ __STDIO_THREADLOCK_OPENLIST_ADD;
+ f = _stdio_openlist;
+ __STDIO_THREADUNLOCK_OPENLIST_ADD;
+
+ while (f) {
+#warning should probably have a get_next() operation
+ FILE *n = f->__nextopen;
+ if (fclose(f)) {
retval = EOF;
}
+ f = n;
}
- __STDIO_THREADUNLOCK_OPENLIST;
+
+ __STDIO_OPENLIST_DEC_USE;
return retval;
diff --git a/libc/stdio/fflush.c b/libc/stdio/fflush.c
index 6baa0ec..66b65cd 100644
--- a/libc/stdio/fflush.c
+++ b/libc/stdio/fflush.c
@@ -20,23 +20,50 @@ weak_alias(__fflush_unlocked,fflush_unlo
weak_alias(__fflush_unlocked,fflush);
#endif
-#ifdef __UCLIBC_HAS_THREADS__
/* Even if the stream is set to user-locking, we still need to lock
* when all (lbf) writing streams are flushed. */
-#define MY_STDIO_THREADLOCK(STREAM) \
- if (_stdio_user_locking != 2) { \
- __STDIO_ALWAYS_THREADLOCK(STREAM); \
- }
-#define MY_STDIO_THREADUNLOCK(STREAM) \
- if (_stdio_user_locking != 2) { \
- __STDIO_ALWAYS_THREADUNLOCK(STREAM); \
- }
-#else
-#define MY_STDIO_THREADLOCK(STREAM) ((void)0)
-#define MY_STDIO_THREADUNLOCK(STREAM) ((void)0)
-#endif
+#define __MY_STDIO_THREADLOCK(__stream) \
+ __UCLIBC_MUTEX_CONDITIONAL_LOCK((__stream)->__lock, \
+ (_stdio_user_locking != 2))
+
+#define __MY_STDIO_THREADUNLOCK(__stream) \
+ __UCLIBC_MUTEX_CONDITIONAL_UNLOCK((__stream)->__lock, \
+ (_stdio_user_locking != 2))
+#if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
+void _stdio_openlist_dec_use(void)
+{
+ __STDIO_THREADLOCK_OPENLIST_DEL;
+ if ((_stdio_openlist_use_count == 1) && (_stdio_openlist_del_count > 0)) {
+ FILE *p = NULL;
+ FILE *n;
+ FILE *stream;
+
+ __STDIO_THREADLOCK_OPENLIST_ADD;
+ for (stream = _stdio_openlist; stream; stream = n) {
+#warning walk the list and clear out all fclosed()d files
+ n = stream->__nextopen;
+#warning fix for nonatomic
+ if ((stream->__modeflags & (__FLAG_READONLY|__FLAG_WRITEONLY))
+ == (__FLAG_READONLY|__FLAG_WRITEONLY)
+ ) { /* The file was closed so remove from the list. */
+ if (!p) {
+ _stdio_openlist = n;
+ } else {
+ p->__nextopen = n;
+ }
+ __STDIO_STREAM_FREE_FILE(stream);
+ } else {
+ p = stream;
+ }
+ }
+ __STDIO_THREADUNLOCK_OPENLIST_DEL;
+ }
+ --_stdio_openlist_use_count;
+ __STDIO_THREADUNLOCK_OPENLIST_DEL;
+}
+#endif
int __fflush_unlocked(register FILE *stream)
{
@@ -60,23 +87,39 @@ int __fflush_unlocked(register FILE *str
}
if (!stream) { /* Flush all (lbf) writing streams. */
- __STDIO_THREADLOCK_OPENLIST;
- for (stream = _stdio_openlist; stream ; stream = stream->__nextopen) {
- MY_STDIO_THREADLOCK(stream);
- if (!(((stream->__modeflags | bufmask)
- ^ (__FLAG_WRITING|__FLAG_LBF)
- ) & (__FLAG_WRITING|__MASK_BUFMODE))
- ) {
- if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
- __STDIO_STREAM_DISABLE_PUTC(stream);
- __STDIO_STREAM_CLEAR_WRITING(stream);
- } else {
- retval = EOF;
+
+ __STDIO_OPENLIST_INC_USE;
+
+ __STDIO_THREADLOCK_OPENLIST_ADD;
+ stream = _stdio_openlist;
+ __STDIO_THREADUNLOCK_OPENLIST_ADD;
+
+ while(stream) {
+ /* We only care about currently writing streams and do not want to
+ * block trying to obtain mutexes on non-writing streams. */
+#warning fix for nonatomic
+#warning unnecessary check if no threads
+ if (__STDIO_STREAM_IS_WRITING(stream)) { /* ONLY IF ATOMIC!!! */
+ __MY_STDIO_THREADLOCK(stream);
+ /* Need to check again once we have the lock. */
+ if (!(((stream->__modeflags | bufmask)
+ ^ (__FLAG_WRITING|__FLAG_LBF)
+ ) & (__FLAG_WRITING|__MASK_BUFMODE))
+ ) {
+ if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
+ __STDIO_STREAM_DISABLE_PUTC(stream);
+ __STDIO_STREAM_CLEAR_WRITING(stream);
+ } else {
+ retval = EOF;
+ }
}
+ __MY_STDIO_THREADUNLOCK(stream);
}
- MY_STDIO_THREADUNLOCK(stream);
+ stream = stream->__nextopen;
}
- __STDIO_THREADUNLOCK_OPENLIST;
+
+ __STDIO_OPENLIST_DEC_USE;
+
} else if (__STDIO_STREAM_IS_WRITING(stream)) {
if (!__STDIO_COMMIT_WRITE_BUFFER(stream)) {
__STDIO_STREAM_DISABLE_PUTC(stream);
diff --git a/libc/stdio/flockfile.c b/libc/stdio/flockfile.c
index 0dcc7c2..3fad711 100644
--- a/libc/stdio/flockfile.c
+++ b/libc/stdio/flockfile.c
@@ -11,6 +11,6 @@ void flockfile(FILE *stream)
{
__STDIO_STREAM_VALIDATE(stream);
- __STDIO_ALWAYS_THREADLOCK(stream);
+ __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(stream);
}
diff --git a/libc/stdio/freopen.c b/libc/stdio/freopen.c
index 0eccaac..36b8488 100644
--- a/libc/stdio/freopen.c
+++ b/libc/stdio/freopen.c
@@ -42,6 +42,8 @@ FILE *freopen(const char * __restrict fi
__STDIO_STREAM_VALIDATE(stream);
+ __STDIO_OPENLIST_INC_USE; /* Do not remove the file from the list. */
+
/* First, flush and close, but don't deallocate, the stream. */
/* This also removes the stream for the open file list. */
dynmode = (stream->__modeflags & (__FLAG_FREEBUF|__FLAG_FREEFILE));
@@ -57,9 +59,16 @@ FILE *freopen(const char * __restrict fi
fp = _stdio_fopen(((intptr_t) filename), mode, stream, FILEDES_ARG);
+#warning if fp is NULL, then we do not free file (but beware stdin,stdout,stderr)
+ if (fp) {
+ __STDIO_OPENLIST_DEC_DEL_CNT;
+ }
+
/* Reset the allocation flags. */
stream->__modeflags |= dynmode;
+ __STDIO_OPENLIST_DEC_USE;
+
__STDIO_AUTO_THREADUNLOCK(stream);
return fp;
diff --git a/libc/stdio/ftello.c b/libc/stdio/ftello.c
index 7092f34..69385ce 100644
--- a/libc/stdio/ftello.c
+++ b/libc/stdio/ftello.c
@@ -48,7 +48,10 @@ OFFSET_TYPE FTELL(register FILE *stream)
__STDIO_STREAM_VALIDATE(stream);
- if ((__SEEK(stream, &pos, SEEK_CUR) < 0)
+ if ((__SEEK(stream, &pos,
+ ((__STDIO_STREAM_IS_WRITING(stream)
+ && (stream->__modeflags & __FLAG_APPEND))
+ ? SEEK_END : SEEK_CUR)) < 0)
|| (__stdio_adjust_position(stream, &pos) < 0)) {
pos = -1;
}
diff --git a/libc/stdio/ftrylockfile.c b/libc/stdio/ftrylockfile.c
index d85b8ff..0d2e156 100644
--- a/libc/stdio/ftrylockfile.c
+++ b/libc/stdio/ftrylockfile.c
@@ -15,5 +15,5 @@ int ftrylockfile(FILE *stream)
{
__STDIO_STREAM_VALIDATE(stream);
- return __STDIO_ALWAYS_THREADTRYLOCK(stream);
+ return __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(stream);
}
diff --git a/libc/stdio/funlockfile.c b/libc/stdio/funlockfile.c
index 048c093..2ddf097 100644
--- a/libc/stdio/funlockfile.c
+++ b/libc/stdio/funlockfile.c
@@ -11,5 +11,5 @@ void funlockfile(FILE *stream)
{
__STDIO_STREAM_VALIDATE(stream);
- __STDIO_ALWAYS_THREADUNLOCK(stream);
+ __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(stream);
}
diff --git a/libc/stdio/popen.c b/libc/stdio/popen.c
index c7887ad..ab8d296 100644
--- a/libc/stdio/popen.c
+++ b/libc/stdio/popen.c
@@ -14,6 +14,7 @@
* Fix failure exit code for failed execve().
*/
+#warning hmm... susv3 says "Pipe streams are byte-oriented."
#include <stdio.h>
#include <stdlib.h>
@@ -21,6 +22,8 @@
#include <unistd.h>
#include <sys/wait.h>
+#include <bits/uClibc_mutex.h>
+
/* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
#include <sys/syscall.h>
#if ! defined __NR_vfork
@@ -29,19 +32,11 @@
# define VFORK_UNLOCK ((void) 0)
#endif
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK ((void) 0)
-# define UNLOCK ((void) 0)
-#endif
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
#ifndef VFORK_LOCK
-# define VFORK_LOCK LOCK
-# define VFORK_UNLOCK UNLOCK
+# define VFORK_LOCK __UCLIBC_MUTEX_LOCK(mylock)
+# define VFORK_UNLOCK __UCLIBC_MUTEX_UNLOCK(mylock)
#endif
struct popen_list_item {
@@ -118,10 +113,10 @@ FILE *popen(const char *command, const c
if (pid > 0) { /* Parent of vfork... */
pi->pid = pid;
pi->f = fp;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
pi->next = popen_list;
popen_list = pi;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return fp;
}
@@ -136,6 +131,8 @@ FILE *popen(const char *command, const c
return NULL;
}
+#warning is pclose correct wrt the new mutex semantics?
+
int pclose(FILE *stream)
{
struct popen_list_item *p;
@@ -144,7 +141,7 @@ int pclose(FILE *stream)
/* First, find the list entry corresponding to stream and remove it
* from the list. Set p to the list item (NULL if not found). */
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if ((p = popen_list) != NULL) {
if (p->f == stream) {
popen_list = p->next;
@@ -163,7 +160,7 @@ int pclose(FILE *stream)
} while (1);
}
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
if (p) {
pid = p->pid; /* Save the pid we need */
diff --git a/libc/stdio/setvbuf.c b/libc/stdio/setvbuf.c
index 3fe62c6..6d53ab1 100644
--- a/libc/stdio/setvbuf.c
+++ b/libc/stdio/setvbuf.c
@@ -75,8 +75,8 @@ int setvbuf(register FILE * __restrict s
}
stream->__modeflags |= alloc_flag;
- stream->__bufstart = buf;
- stream->__bufend = buf + size;
+ stream->__bufstart = (unsigned char *) buf;
+ stream->__bufend = (unsigned char *) buf + size;
__STDIO_STREAM_INIT_BUFREAD_BUFPOS(stream);
__STDIO_STREAM_DISABLE_GETC(stream);
__STDIO_STREAM_DISABLE_PUTC(stream);
diff --git a/libc/stdio/vasprintf.c b/libc/stdio/vasprintf.c
index 688ab7c..6d7664d 100644
--- a/libc/stdio/vasprintf.c
+++ b/libc/stdio/vasprintf.c
@@ -63,6 +63,8 @@ int vasprintf(char **__restrict buf, con
free(*buf);
*buf = NULL;
}
+ } else {
+ rv = -1;
}
}
diff --git a/libc/stdio/vdprintf.c b/libc/stdio/vdprintf.c
index de8362c..7cb707f 100644
--- a/libc/stdio/vdprintf.c
+++ b/libc/stdio/vdprintf.c
@@ -15,8 +15,8 @@ int vdprintf(int filedes, const char * _
#ifdef __STDIO_BUFFERS
char buf[64]; /* TODO: provide _optional_ buffering? */
- f.__bufend = buf + sizeof(buf);
- f.__bufstart = buf;
+ f.__bufend = (unsigned char *) buf + sizeof(buf);
+ f.__bufstart = (unsigned char *) buf;
__STDIO_STREAM_DISABLE_GETC(&f);
__STDIO_STREAM_DISABLE_PUTC(&f);
__STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
diff --git a/libc/stdio/vfprintf.c b/libc/stdio/vfprintf.c
index 10114f0..9214e3b 100644
--- a/libc/stdio/vfprintf.c
+++ b/libc/stdio/vfprintf.c
@@ -569,7 +569,7 @@ int _ppfs_init(register ppfs_t *ppfs, co
ppfs->fmtpos = fmt0; /* rewind */
}
-#ifdef NL_MAX_ARG
+#ifdef NL_ARGMAX
/* If we have positional args, make sure we know all the types. */
{
register int *p = ppfs->argtype;
@@ -581,7 +581,7 @@ int _ppfs_init(register ppfs_t *ppfs, co
++p;
}
}
-#endif /* NL_MAX_ARG */
+#endif /* NL_ARGMAX */
return 0;
}
@@ -1214,7 +1214,7 @@ static size_t _fp_out_narrow(FILE *fp, i
}
len = buflen;
}
- return r + OUTNSTR(fp, (const char *) buf, len);
+ return r + OUTNSTR(fp, (const unsigned char *) buf, len);
}
#endif /* __STDIO_PRINTF_FLOAT */
diff --git a/libc/stdlib/abort.c b/libc/stdlib/abort.c
index 77c2cdc..9f69918 100644
--- a/libc/stdlib/abort.c
+++ b/libc/stdlib/abort.c
@@ -70,16 +70,9 @@ extern void _exit __P((int __status)) __
static int been_there_done_that = 0;
/* Be prepared in case multiple threads try to abort() */
-#ifdef __UCLIBC_HAS_THREADS__
-# include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock)
-#else
-# define LOCK
-# define UNLOCK
-#endif
+#include <bits/uClibc_mutex.h>
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
/* Cause an abnormal program termination with core-dump */
void abort(void)
@@ -87,7 +80,7 @@ void abort(void)
sigset_t sigset;
/* Make sure we acquire the lock before proceeding */
- LOCK;
+ __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
/* Unmask SIGABRT to be sure we can get it */
if (__sigemptyset(&sigset) == 0 && __sigaddset(&sigset, SIGABRT) == 0) {
@@ -110,9 +103,9 @@ void abort(void)
#endif
abort_it:
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(mylock);
raise(SIGABRT);
- LOCK;
+ __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(mylock);
}
/* Still here? Try to remove any signal handlers */
diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c
index 280f42c..b028068 100644
--- a/libc/stdlib/atexit.c
+++ b/libc/stdlib/atexit.c
@@ -40,17 +40,9 @@
#include <stdlib.h>
#include <errno.h>
+#include <bits/uClibc_mutex.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-extern pthread_mutex_t mylock;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
-
+__UCLIBC_MUTEX_EXTERN(__atexit_lock);
typedef void (*aefuncp) (void); /* atexit function pointer */
typedef void (*oefuncp) (int, void *); /* on_exit function pointer */
@@ -90,8 +82,9 @@ extern struct exit_function __exit_funct
int atexit(aefuncp func)
{
struct exit_function *efp;
+ int rv = -1;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(__atexit_lock);
if (func) {
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
/* If we are out of function table slots, make some more */
@@ -99,18 +92,16 @@ int atexit(aefuncp func)
efp=realloc(__exit_function_table,
(__exit_slots+20)*sizeof(struct exit_function));
if (efp==NULL) {
- UNLOCK;
__set_errno(ENOMEM);
- return -1;
+ goto DONE;
}
__exit_function_table = efp;
__exit_slots+=20;
}
#else
if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
- UNLOCK;
__set_errno(ENOMEM);
- return -1;
+ goto DONE;
}
#endif
__exit_cleanup = __exit_handler; /* enable cleanup */
@@ -118,8 +109,12 @@ int atexit(aefuncp func)
efp->type = ef_atexit;
efp->funcs.atexit = func;
}
- UNLOCK;
- return 0;
+
+ rv = 0;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
+ return rv;
}
#endif
@@ -133,8 +128,9 @@ int atexit(aefuncp func)
int on_exit(oefuncp func, void *arg)
{
struct exit_function *efp;
+ int rv = -1;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(__atexit_lock);
if (func) {
#ifdef __UCLIBC_DYNAMIC_ATEXIT__
/* If we are out of function table slots, make some more */
@@ -142,18 +138,16 @@ int on_exit(oefuncp func, void *arg)
efp=realloc(__exit_function_table,
(__exit_slots+20)*sizeof(struct exit_function));
if (efp==NULL) {
- UNLOCK;
__set_errno(ENOMEM);
- return -1;
+ goto DONE;
}
__exit_function_table=efp;
__exit_slots+=20;
}
#else
if (__exit_count >= __UCLIBC_MAX_ATEXIT) {
- UNLOCK;
__set_errno(ENOMEM);
- return -1;
+ goto DONE;
}
#endif
@@ -163,8 +157,12 @@ int on_exit(oefuncp func, void *arg)
efp->funcs.on_exit.func = func;
efp->funcs.on_exit.arg = arg;
}
- UNLOCK;
- return 0;
+
+ rv = 0;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
+ return rv;
}
#endif
@@ -214,9 +212,8 @@ void __exit_handler(int status)
#ifdef L_exit
extern void weak_function _stdio_term(void);
void (*__exit_cleanup) (int) = 0;
-#ifdef __UCLIBC_HAS_THREADS__
-pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-#endif
+
+__UCLIBC_MUTEX_INIT(__atexit_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
#ifdef __UCLIBC_CTOR_DTOR__
extern void (*__app_fini)(void);
@@ -229,11 +226,11 @@ extern void (*__rtld_fini)(void);
void exit(int rv)
{
/* Perform exit-specific cleanup (atexit and on_exit) */
- LOCK;
+ __UCLIBC_MUTEX_LOCK(__atexit_lock);
if (__exit_cleanup) {
__exit_cleanup(rv);
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(__atexit_lock);
#ifdef __UCLIBC_CTOR_DTOR__
if (__app_fini != NULL)
diff --git a/libc/stdlib/malloc-simple/alloc.c b/libc/stdlib/malloc-simple/alloc.c
index ed14c37..519a875 100644
--- a/libc/stdlib/malloc-simple/alloc.c
+++ b/libc/stdlib/malloc-simple/alloc.c
@@ -108,15 +108,14 @@ void free(void *ptr)
#endif
#ifdef L_memalign
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-# define LOCK __pthread_mutex_lock(&__malloc_lock)
-# define UNLOCK __pthread_mutex_unlock(&__malloc_lock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+
+#include <bits/uClibc_mutex.h>
+
+__UCLIBC_MUTEX_EXTERN(__malloc_lock);
+
+#define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
+#define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
+
/* List of blocks allocated with memalign or valloc */
struct alignlist
@@ -135,7 +134,7 @@ int __libc_free_aligned(void *ptr)
if (ptr == NULL)
return 0;
- LOCK;
+ __MALLOC_LOCK;
for (l = _aligned_blocks; l != NULL; l = l->next) {
if (l->aligned == ptr) {
/* Mark the block as free */
@@ -146,7 +145,7 @@ int __libc_free_aligned(void *ptr)
return 1;
}
}
- UNLOCK;
+ __MALLOC_UNLOCK;
return 0;
}
void * memalign (size_t alignment, size_t size)
@@ -159,10 +158,10 @@ void * memalign (size_t alignment, size_
return NULL;
adj = (unsigned long int) ((unsigned long int) ((char *) result -
- (char *) NULL)) % alignment;
+ (char *) NULL)) % alignment;
if (adj != 0) {
struct alignlist *l;
- LOCK;
+ __MALLOC_LOCK;
for (l = _aligned_blocks; l != NULL; l = l->next)
if (l->aligned == NULL)
/* This slot is free. Use it. */
@@ -171,15 +170,16 @@ void * memalign (size_t alignment, size_
l = (struct alignlist *) malloc (sizeof (struct alignlist));
if (l == NULL) {
free(result);
- UNLOCK;
- return NULL;
+ result = NULL;
+ goto DONE;
}
l->next = _aligned_blocks;
_aligned_blocks = l;
}
l->exact = result;
result = l->aligned = (char *) result + alignment - adj;
- UNLOCK;
+ DONE:
+ __MALLOC_UNLOCK;
}
return result;
diff --git a/libc/stdlib/malloc-standard/calloc.c b/libc/stdlib/malloc-standard/calloc.c
index a67dad7..4277954 100644
--- a/libc/stdlib/malloc-standard/calloc.c
+++ b/libc/stdlib/malloc-standard/calloc.c
@@ -8,7 +8,7 @@
VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
Note: There may be an updated version of this malloc obtainable at
- ftp://gee.cs.oswego.edu/pub/misc/malloc.c
+ ftp://gee.cs.oswego.edu/pub/misc/malloc.c
Check before installing!
Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
@@ -31,63 +31,63 @@ void* calloc(size_t n_elements, size_t e
* to fall through and call malloc(0) */
size = n_elements * elem_size;
if (n_elements && elem_size != (size / n_elements)) {
- __set_errno(ENOMEM);
- return NULL;
+ __set_errno(ENOMEM);
+ return NULL;
}
- LOCK;
+ __MALLOC_LOCK;
mem = malloc(size);
if (mem != 0) {
- p = mem2chunk(mem);
+ p = mem2chunk(mem);
- if (!chunk_is_mmapped(p))
- {
- /*
- Unroll clear of <= 36 bytes (72 if 8byte sizes)
- We know that contents have an odd number of
- size_t-sized words; minimally 3.
- */
-
- d = (size_t*)mem;
- clearsize = chunksize(p) - (sizeof(size_t));
- nclears = clearsize / sizeof(size_t);
- assert(nclears >= 3);
-
- if (nclears > 9)
- memset(d, 0, clearsize);
-
- else {
- *(d+0) = 0;
- *(d+1) = 0;
- *(d+2) = 0;
- if (nclears > 4) {
- *(d+3) = 0;
- *(d+4) = 0;
- if (nclears > 6) {
- *(d+5) = 0;
- *(d+6) = 0;
- if (nclears > 8) {
- *(d+7) = 0;
- *(d+8) = 0;
+ if (!chunk_is_mmapped(p))
+ {
+ /*
+ Unroll clear of <= 36 bytes (72 if 8byte sizes)
+ We know that contents have an odd number of
+ size_t-sized words; minimally 3.
+ */
+
+ d = (size_t*)mem;
+ clearsize = chunksize(p) - (sizeof(size_t));
+ nclears = clearsize / sizeof(size_t);
+ assert(nclears >= 3);
+
+ if (nclears > 9)
+ memset(d, 0, clearsize);
+
+ else {
+ *(d+0) = 0;
+ *(d+1) = 0;
+ *(d+2) = 0;
+ if (nclears > 4) {
+ *(d+3) = 0;
+ *(d+4) = 0;
+ if (nclears > 6) {
+ *(d+5) = 0;
+ *(d+6) = 0;
+ if (nclears > 8) {
+ *(d+7) = 0;
+ *(d+8) = 0;
+ }
+ }
+ }
+ }
}
- }
- }
- }
- }
#if 0
- else
- {
- /* Standard unix mmap using /dev/zero clears memory so calloc
- * doesn't need to actually zero anything....
- */
- d = (size_t*)mem;
- /* Note the additional (sizeof(size_t)) */
- clearsize = chunksize(p) - 2*(sizeof(size_t));
- memset(d, 0, clearsize);
- }
+ else
+ {
+ /* Standard unix mmap using /dev/zero clears memory so calloc
+ * doesn't need to actually zero anything....
+ */
+ d = (size_t*)mem;
+ /* Note the additional (sizeof(size_t)) */
+ clearsize = chunksize(p) - 2*(sizeof(size_t));
+ memset(d, 0, clearsize);
+ }
#endif
}
- UNLOCK;
+ __MALLOC_UNLOCK;
return mem;
}
diff --git a/libc/stdlib/malloc-standard/free.c b/libc/stdlib/malloc-standard/free.c
index 94e1d65..4e08ef7 100644
--- a/libc/stdlib/malloc-standard/free.c
+++ b/libc/stdlib/malloc-standard/free.c
@@ -8,7 +8,7 @@
VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
Note: There may be an updated version of this malloc obtainable at
- ftp://gee.cs.oswego.edu/pub/misc/malloc.c
+ ftp://gee.cs.oswego.edu/pub/misc/malloc.c
Check before installing!
Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
@@ -42,71 +42,71 @@ static int __malloc_trim(size_t pad, mst
if (extra > 0) {
- /*
- Only proceed if end of memory is where we last set it.
- This avoids problems if there were foreign sbrk calls.
- */
- current_brk = (char*)(MORECORE(0));
- if (current_brk == (char*)(av->top) + top_size) {
-
- /*
- Attempt to release memory. We ignore MORECORE return value,
- and instead call again to find out where new end of memory is.
- This avoids problems if first call releases less than we asked,
- of if failure somehow altered brk value. (We could still
- encounter problems if it altered brk in some very bad way,
- but the only thing we can do is adjust anyway, which will cause
- some downstream failure.)
- */
-
- MORECORE(-extra);
- new_brk = (char*)(MORECORE(0));
-
- if (new_brk != (char*)MORECORE_FAILURE) {
- released = (long)(current_brk - new_brk);
-
- if (released != 0) {
- /* Success. Adjust top. */
- av->sbrked_mem -= released;
- set_head(av->top, (top_size - released) | PREV_INUSE);
- check_malloc_state();
- return 1;
+ /*
+ Only proceed if end of memory is where we last set it.
+ This avoids problems if there were foreign sbrk calls.
+ */
+ current_brk = (char*)(MORECORE(0));
+ if (current_brk == (char*)(av->top) + top_size) {
+
+ /*
+ Attempt to release memory. We ignore MORECORE return value,
+ and instead call again to find out where new end of memory is.
+ This avoids problems if first call releases less than we asked,
+ of if failure somehow altered brk value. (We could still
+ encounter problems if it altered brk in some very bad way,
+ but the only thing we can do is adjust anyway, which will cause
+ some downstream failure.)
+ */
+
+ MORECORE(-extra);
+ new_brk = (char*)(MORECORE(0));
+
+ if (new_brk != (char*)MORECORE_FAILURE) {
+ released = (long)(current_brk - new_brk);
+
+ if (released != 0) {
+ /* Success. Adjust top. */
+ av->sbrked_mem -= released;
+ set_head(av->top, (top_size - released) | PREV_INUSE);
+ check_malloc_state();
+ return 1;
+ }
+ }
}
- }
- }
}
return 0;
}
/* ------------------------- malloc_trim -------------------------
- malloc_trim(size_t pad);
+ malloc_trim(size_t pad);
- If possible, gives memory back to the system (via negative
- arguments to sbrk) if there is unused memory at the `high' end of
- the malloc pool. You can call this after freeing large blocks of
- memory to potentially reduce the system-level memory requirements
- of a program. However, it cannot guarantee to reduce memory. Under
- some allocation patterns, some large free blocks of memory will be
- locked between two used chunks, so they cannot be given back to
- the system.
-
- The `pad' argument to malloc_trim represents the amount of free
- trailing space to leave untrimmed. If this argument is zero,
- only the minimum amount of memory to maintain internal data
- structures will be left (one page or less). Non-zero arguments
- can be supplied to maintain enough trailing space to service
- future expected allocations without having to re-obtain memory
- from the system.
-
- Malloc_trim returns 1 if it actually released any memory, else 0.
- On systems that do not support "negative sbrks", it will always
- return 0.
+ If possible, gives memory back to the system (via negative
+ arguments to sbrk) if there is unused memory at the `high' end of
+ the malloc pool. You can call this after freeing large blocks of
+ memory to potentially reduce the system-level memory requirements
+ of a program. However, it cannot guarantee to reduce memory. Under
+ some allocation patterns, some large free blocks of memory will be
+ locked between two used chunks, so they cannot be given back to
+ the system.
+
+ The `pad' argument to malloc_trim represents the amount of free
+ trailing space to leave untrimmed. If this argument is zero,
+ only the minimum amount of memory to maintain internal data
+ structures will be left (one page or less). Non-zero arguments
+ can be supplied to maintain enough trailing space to service
+ future expected allocations without having to re-obtain memory
+ from the system.
+
+ Malloc_trim returns 1 if it actually released any memory, else 0.
+ On systems that do not support "negative sbrks", it will always
+ return 0.
*/
int malloc_trim(size_t pad)
{
- mstate av = get_malloc_state();
- __malloc_consolidate(av);
- return __malloc_trim(pad, av);
+ mstate av = get_malloc_state();
+ __malloc_consolidate(av);
+ return __malloc_trim(pad, av);
}
/*
@@ -125,8 +125,8 @@ static void malloc_init_state(mstate av)
/* Establish circular links for normal bins */
for (i = 1; i < NBINS; ++i) {
- bin = bin_at(av,i);
- bin->fd = bin->bk = bin;
+ bin = bin_at(av,i);
+ bin->fd = bin->bk = bin;
}
av->top_pad = DEFAULT_TOP_PAD;
@@ -157,15 +157,15 @@ static void malloc_init_state(mstate av)
/* ------------------------- __malloc_consolidate -------------------------
- __malloc_consolidate is a specialized version of free() that tears
- down chunks held in fastbins. Free itself cannot be used for this
- purpose since, among other things, it might place chunks back onto
- fastbins. So, instead, we need to use a minor variant of the same
- code.
-
- Also, because this routine needs to be called the first time through
- malloc anyway, it turns out to be the perfect place to trigger
- initialization code.
+__malloc_consolidate is a specialized version of free() that tears
+down chunks held in fastbins. Free itself cannot be used for this
+purpose since, among other things, it might place chunks back onto
+fastbins. So, instead, we need to use a minor variant of the same
+code.
+
+Also, because this routine needs to be called the first time through
+malloc anyway, it turns out to be the perfect place to trigger
+initialization code.
*/
void __malloc_consolidate(mstate av)
{
@@ -186,78 +186,78 @@ void __malloc_consolidate(mstate av)
mchunkptr fwd;
/*
- If max_fast is 0, we know that av hasn't
- yet been initialized, in which case do so below
- */
+ If max_fast is 0, we know that av hasn't
+ yet been initialized, in which case do so below
+ */
if (av->max_fast != 0) {
- clear_fastchunks(av);
+ clear_fastchunks(av);
- unsorted_bin = unsorted_chunks(av);
+ unsorted_bin = unsorted_chunks(av);
- /*
- Remove each chunk from fast bin and consolidate it, placing it
- then in unsorted bin. Among other reasons for doing this,
- placing in unsorted bin avoids needing to calculate actual bins
- until malloc is sure that chunks aren't immediately going to be
- reused anyway.
- */
-
- maxfb = &(av->fastbins[fastbin_index(av->max_fast)]);
- fb = &(av->fastbins[0]);
- do {
- if ( (p = *fb) != 0) {
- *fb = 0;
+ /*
+ Remove each chunk from fast bin and consolidate it, placing it
+ then in unsorted bin. Among other reasons for doing this,
+ placing in unsorted bin avoids needing to calculate actual bins
+ until malloc is sure that chunks aren't immediately going to be
+ reused anyway.
+ */
+ maxfb = &(av->fastbins[fastbin_index(av->max_fast)]);
+ fb = &(av->fastbins[0]);
do {
- check_inuse_chunk(p);
- nextp = p->fd;
+ if ( (p = *fb) != 0) {
+ *fb = 0;
- /* Slightly streamlined version of consolidation code in free() */
- size = p->size & ~PREV_INUSE;
- nextchunk = chunk_at_offset(p, size);
- nextsize = chunksize(nextchunk);
+ do {
+ check_inuse_chunk(p);
+ nextp = p->fd;
+
+ /* Slightly streamlined version of consolidation code in free() */
+ size = p->size & ~PREV_INUSE;
+ nextchunk = chunk_at_offset(p, size);
+ nextsize = chunksize(nextchunk);
+
+ if (!prev_inuse(p)) {
+ prevsize = p->prev_size;
+ size += prevsize;
+ p = chunk_at_offset(p, -((long) prevsize));
+ unlink(p, bck, fwd);
+ }
+
+ if (nextchunk != av->top) {
+ nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
+ set_head(nextchunk, nextsize);
+
+ if (!nextinuse) {
+ size += nextsize;
+ unlink(nextchunk, bck, fwd);
+ }
+
+ first_unsorted = unsorted_bin->fd;
+ unsorted_bin->fd = p;
+ first_unsorted->bk = p;
+
+ set_head(p, size | PREV_INUSE);
+ p->bk = unsorted_bin;
+ p->fd = first_unsorted;
+ set_foot(p, size);
+ }
+
+ else {
+ size += nextsize;
+ set_head(p, size | PREV_INUSE);
+ av->top = p;
+ }
- if (!prev_inuse(p)) {
- prevsize = p->prev_size;
- size += prevsize;
- p = chunk_at_offset(p, -((long) prevsize));
- unlink(p, bck, fwd);
- }
+ } while ( (p = nextp) != 0);
- if (nextchunk != av->top) {
- nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
- set_head(nextchunk, nextsize);
-
- if (!nextinuse) {
- size += nextsize;
- unlink(nextchunk, bck, fwd);
}
-
- first_unsorted = unsorted_bin->fd;
- unsorted_bin->fd = p;
- first_unsorted->bk = p;
-
- set_head(p, size | PREV_INUSE);
- p->bk = unsorted_bin;
- p->fd = first_unsorted;
- set_foot(p, size);
- }
-
- else {
- size += nextsize;
- set_head(p, size | PREV_INUSE);
- av->top = p;
- }
-
- } while ( (p = nextp) != 0);
-
- }
- } while (fb++ != maxfb);
+ } while (fb++ != maxfb);
}
else {
- malloc_init_state(av);
- check_malloc_state();
+ malloc_init_state(av);
+ check_malloc_state();
}
}
@@ -279,9 +279,9 @@ void free(void* mem)
/* free(0) has no effect */
if (mem == NULL)
- return;
+ return;
- LOCK;
+ __MALLOC_LOCK;
av = get_malloc_state();
p = mem2chunk(mem);
size = chunksize(p);
@@ -289,9 +289,9 @@ void free(void* mem)
check_inuse_chunk(p);
/*
- If eligible, place chunk on a fastbin so it can be found
- and used quickly in malloc.
- */
+ If eligible, place chunk on a fastbin so it can be found
+ and used quickly in malloc.
+ */
if ((unsigned long)(size) <= (unsigned long)(av->max_fast)
@@ -300,114 +300,114 @@ void free(void* mem)
bordering top into fastbins */
&& (chunk_at_offset(p, size) != av->top)
#endif
- ) {
+ ) {
- set_fastchunks(av);
- fb = &(av->fastbins[fastbin_index(size)]);
- p->fd = *fb;
- *fb = p;
+ set_fastchunks(av);
+ fb = &(av->fastbins[fastbin_index(size)]);
+ p->fd = *fb;
+ *fb = p;
}
/*
- Consolidate other non-mmapped chunks as they arrive.
- */
+ Consolidate other non-mmapped chunks as they arrive.
+ */
else if (!chunk_is_mmapped(p)) {
- set_anychunks(av);
+ set_anychunks(av);
+
+ nextchunk = chunk_at_offset(p, size);
+ nextsize = chunksize(nextchunk);
+
+ /* consolidate backward */
+ if (!prev_inuse(p)) {
+ prevsize = p->prev_size;
+ size += prevsize;
+ p = chunk_at_offset(p, -((long) prevsize));
+ unlink(p, bck, fwd);
+ }
+
+ if (nextchunk != av->top) {
+ /* get and clear inuse bit */
+ nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
+ set_head(nextchunk, nextsize);
+
+ /* consolidate forward */
+ if (!nextinuse) {
+ unlink(nextchunk, bck, fwd);
+ size += nextsize;
+ }
+
+ /*
+ Place the chunk in unsorted chunk list. Chunks are
+ not placed into regular bins until after they have
+ been given one chance to be used in malloc.
+ */
+
+ bck = unsorted_chunks(av);
+ fwd = bck->fd;
+ p->bk = bck;
+ p->fd = fwd;
+ bck->fd = p;
+ fwd->bk = p;
- nextchunk = chunk_at_offset(p, size);
- nextsize = chunksize(nextchunk);
+ set_head(p, size | PREV_INUSE);
+ set_foot(p, size);
+
+ check_free_chunk(p);
+ }
+
+ /*
+ If the chunk borders the current high end of memory,
+ consolidate into top
+ */
- /* consolidate backward */
- if (!prev_inuse(p)) {
- prevsize = p->prev_size;
- size += prevsize;
- p = chunk_at_offset(p, -((long) prevsize));
- unlink(p, bck, fwd);
- }
-
- if (nextchunk != av->top) {
- /* get and clear inuse bit */
- nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
- set_head(nextchunk, nextsize);
-
- /* consolidate forward */
- if (!nextinuse) {
- unlink(nextchunk, bck, fwd);
- size += nextsize;
- }
-
- /*
- Place the chunk in unsorted chunk list. Chunks are
- not placed into regular bins until after they have
- been given one chance to be used in malloc.
- */
-
- bck = unsorted_chunks(av);
- fwd = bck->fd;
- p->bk = bck;
- p->fd = fwd;
- bck->fd = p;
- fwd->bk = p;
-
- set_head(p, size | PREV_INUSE);
- set_foot(p, size);
-
- check_free_chunk(p);
- }
-
- /*
- If the chunk borders the current high end of memory,
- consolidate into top
- */
-
- else {
- size += nextsize;
- set_head(p, size | PREV_INUSE);
- av->top = p;
- check_chunk(p);
- }
-
- /*
- If freeing a large space, consolidate possibly-surrounding
- chunks. Then, if the total unused topmost memory exceeds trim
- threshold, ask malloc_trim to reduce top.
-
- Unless max_fast is 0, we don't know if there are fastbins
- bordering top, so we cannot tell for sure whether threshold
- has been reached unless fastbins are consolidated. But we
- don't want to consolidate on each free. As a compromise,
- consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
- is reached.
- */
-
- if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) {
- if (have_fastchunks(av))
- __malloc_consolidate(av);
-
- if ((unsigned long)(chunksize(av->top)) >=
- (unsigned long)(av->trim_threshold))
- __malloc_trim(av->top_pad, av);
- }
+ else {
+ size += nextsize;
+ set_head(p, size | PREV_INUSE);
+ av->top = p;
+ check_chunk(p);
+ }
+
+ /*
+ If freeing a large space, consolidate possibly-surrounding
+ chunks. Then, if the total unused topmost memory exceeds trim
+ threshold, ask malloc_trim to reduce top.
+
+ Unless max_fast is 0, we don't know if there are fastbins
+ bordering top, so we cannot tell for sure whether threshold
+ has been reached unless fastbins are consolidated. But we
+ don't want to consolidate on each free. As a compromise,
+ consolidation is performed if FASTBIN_CONSOLIDATION_THRESHOLD
+ is reached.
+ */
+
+ if ((unsigned long)(size) >= FASTBIN_CONSOLIDATION_THRESHOLD) {
+ if (have_fastchunks(av))
+ __malloc_consolidate(av);
+
+ if ((unsigned long)(chunksize(av->top)) >=
+ (unsigned long)(av->trim_threshold))
+ __malloc_trim(av->top_pad, av);
+ }
}
/*
- If the chunk was allocated via mmap, release via munmap()
- Note that if HAVE_MMAP is false but chunk_is_mmapped is
- true, then user must have overwritten memory. There's nothing
- we can do to catch this error unless DEBUG is set, in which case
- check_inuse_chunk (above) will have triggered error.
- */
+ If the chunk was allocated via mmap, release via munmap()
+ Note that if HAVE_MMAP is false but chunk_is_mmapped is
+ true, then user must have overwritten memory. There's nothing
+ we can do to catch this error unless DEBUG is set, in which case
+ check_inuse_chunk (above) will have triggered error.
+ */
else {
- int ret;
- size_t offset = p->prev_size;
- av->n_mmaps--;
- av->mmapped_mem -= (size + offset);
- ret = munmap((char*)p - offset, size + offset);
- /* munmap returns non-zero on failure */
- assert(ret == 0);
+ int ret;
+ size_t offset = p->prev_size;
+ av->n_mmaps--;
+ av->mmapped_mem -= (size + offset);
+ ret = munmap((char*)p - offset, size + offset);
+ /* munmap returns non-zero on failure */
+ assert(ret == 0);
}
- UNLOCK;
+ __MALLOC_UNLOCK;
}
diff --git a/libc/stdlib/malloc-standard/mallinfo.c b/libc/stdlib/malloc-standard/mallinfo.c
index 51ac423..1e0875c 100644
--- a/libc/stdlib/malloc-standard/mallinfo.c
+++ b/libc/stdlib/malloc-standard/mallinfo.c
@@ -8,7 +8,7 @@
VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
Note: There may be an updated version of this malloc obtainable at
- ftp://gee.cs.oswego.edu/pub/misc/malloc.c
+ ftp://gee.cs.oswego.edu/pub/misc/malloc.c
Check before installing!
Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
@@ -30,11 +30,11 @@ struct mallinfo mallinfo(void)
int nblocks;
int nfastblocks;
- LOCK;
+ __MALLOC_LOCK;
av = get_malloc_state();
/* Ensure initialization */
if (av->top == 0) {
- __malloc_consolidate(av);
+ __malloc_consolidate(av);
}
check_malloc_state();
@@ -48,21 +48,21 @@ struct mallinfo mallinfo(void)
fastavail = 0;
for (i = 0; i < NFASTBINS; ++i) {
- for (p = av->fastbins[i]; p != 0; p = p->fd) {
- ++nfastblocks;
- fastavail += chunksize(p);
- }
+ for (p = av->fastbins[i]; p != 0; p = p->fd) {
+ ++nfastblocks;
+ fastavail += chunksize(p);
+ }
}
avail += fastavail;
/* traverse regular bins */
for (i = 1; i < NBINS; ++i) {
- b = bin_at(av, i);
- for (p = last(b); p != b; p = p->bk) {
- ++nblocks;
- avail += chunksize(p);
- }
+ b = bin_at(av, i);
+ for (p = last(b); p != b; p = p->bk) {
+ ++nblocks;
+ avail += chunksize(p);
+ }
}
mi.smblks = nfastblocks;
@@ -75,7 +75,7 @@ struct mallinfo mallinfo(void)
mi.fsmblks = fastavail;
mi.keepcost = chunksize(av->top);
mi.usmblks = av->max_total_mem;
- UNLOCK;
+ __MALLOC_UNLOCK;
return mi;
}
@@ -84,23 +84,40 @@ void malloc_stats(FILE *file)
struct mallinfo mi;
if (file==NULL) {
- file = stderr;
+ file = stderr;
}
mi = mallinfo();
- fprintf(file, "total bytes allocated = %10u\n", (unsigned int)(mi.arena + mi.hblkhd));
- fprintf(file, "total bytes in use bytes = %10u\n", (unsigned int)(mi.uordblks + mi.hblkhd));
- fprintf(file, "total non-mmapped bytes allocated = %10d\n", mi.arena);
- fprintf(file, "number of mmapped regions = %10d\n", mi.hblks);
- fprintf(file, "total allocated mmap space = %10d\n", mi.hblkhd);
- fprintf(file, "total allocated sbrk space = %10d\n", mi.uordblks);
+ fprintf(file,
+ "total bytes allocated = %10u\n"
+ "total bytes in use bytes = %10u\n"
+ "total non-mmapped bytes allocated = %10d\n"
+ "number of mmapped regions = %10d\n"
+ "total allocated mmap space = %10d\n"
+ "total allocated sbrk space = %10d\n"
#if 0
- fprintf(file, "number of free chunks = %10d\n", mi.ordblks);
- fprintf(file, "number of fastbin blocks = %10d\n", mi.smblks);
- fprintf(file, "space in freed fastbin blocks = %10d\n", mi.fsmblks);
+ "number of free chunks = %10d\n"
+ "number of fastbin blocks = %10d\n"
+ "space in freed fastbin blocks = %10d\n"
#endif
- fprintf(file, "maximum total allocated space = %10d\n", mi.usmblks);
- fprintf(file, "total free space = %10d\n", mi.fordblks);
- fprintf(file, "memory releasable via malloc_trim = %10d\n", mi.keepcost);
+ "maximum total allocated space = %10d\n"
+ "total free space = %10d\n"
+ "memory releasable via malloc_trim = %10d\n",
+
+ (unsigned int)(mi.arena + mi.hblkhd),
+ (unsigned int)(mi.uordblks + mi.hblkhd),
+ mi.arena,
+ mi.hblks,
+ mi.hblkhd,
+ mi.uordblks,
+#if 0
+ mi.ordblks,
+ mi.smblks,
+ mi.fsmblks,
+#endif
+ mi.usmblks,
+ mi.fordblks,
+ mi.keepcost
+ );
}
diff --git a/libc/stdlib/malloc-standard/malloc.c b/libc/stdlib/malloc-standard/malloc.c
index 7025e83..60494a0 100644
--- a/libc/stdlib/malloc-standard/malloc.c
+++ b/libc/stdlib/malloc-standard/malloc.c
@@ -8,7 +8,7 @@
VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
Note: There may be an updated version of this malloc obtainable at
- ftp://gee.cs.oswego.edu/pub/misc/malloc.c
+ ftp://gee.cs.oswego.edu/pub/misc/malloc.c
Check before installing!
Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
@@ -17,17 +17,14 @@
#define _GNU_SOURCE
#include "malloc.h"
-
-#ifdef __UCLIBC_HAS_THREADS__
-pthread_mutex_t __malloc_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-#endif
+__UCLIBC_MUTEX_INIT(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
/*
- There is exactly one instance of this struct in this malloc.
- If you are adapting this malloc in a way that does NOT use a static
- malloc_state, you MUST explicitly zero-fill it before using. This
- malloc relies on the property that malloc_state is initialized to
- all zeroes (as is true of C statics).
+ There is exactly one instance of this struct in this malloc.
+ If you are adapting this malloc in a way that does NOT use a static
+ malloc_state, you MUST explicitly zero-fill it before using. This
+ malloc relies on the property that malloc_state is initialized to
+ all zeroes (as is true of C statics).
*/
struct malloc_state __malloc_state; /* never directly referenced */
@@ -77,30 +74,30 @@ void __do_check_chunk(mchunkptr p)
if (!chunk_is_mmapped(p)) {
- /* Has legal address ... */
- if (p != av->top) {
- if (contiguous(av)) {
- assert(((char*)p) >= min_address);
- assert(((char*)p + sz) <= ((char*)(av->top)));
- }
- }
- else {
- /* top size is always at least MINSIZE */
- assert((unsigned long)(sz) >= MINSIZE);
- /* top predecessor always marked inuse */
- assert(prev_inuse(p));
- }
+ /* Has legal address ... */
+ if (p != av->top) {
+ if (contiguous(av)) {
+ assert(((char*)p) >= min_address);
+ assert(((char*)p + sz) <= ((char*)(av->top)));
+ }
+ }
+ else {
+ /* top size is always at least MINSIZE */
+ assert((unsigned long)(sz) >= MINSIZE);
+ /* top predecessor always marked inuse */
+ assert(prev_inuse(p));
+ }
}
else {
- /* address is outside main heap */
- if (contiguous(av) && av->top != initial_top(av)) {
- assert(((char*)p) < min_address || ((char*)p) > max_address);
- }
- /* chunk is page-aligned */
- assert(((p->prev_size + sz) & (av->pagesize-1)) == 0);
- /* mem is aligned */
- assert(aligned_OK(chunk2mem(p)));
+ /* address is outside main heap */
+ if (contiguous(av) && av->top != initial_top(av)) {
+ assert(((char*)p) < min_address || ((char*)p) > max_address);
+ }
+ /* chunk is page-aligned */
+ assert(((p->prev_size + sz) & (av->pagesize-1)) == 0);
+ /* mem is aligned */
+ assert(aligned_OK(chunk2mem(p)));
}
}
@@ -121,21 +118,21 @@ void __do_check_free_chunk(mchunkptr p)
/* Unless a special marker, must have OK fields */
if ((unsigned long)(sz) >= MINSIZE)
- {
- assert((sz & MALLOC_ALIGN_MASK) == 0);
- assert(aligned_OK(chunk2mem(p)));
- /* ... matching footer field */
- assert(next->prev_size == sz);
- /* ... and is fully consolidated */
- assert(prev_inuse(p));
- assert (next == av->top || inuse(next));
-
- /* ... and has minimally sane links */
- assert(p->fd->bk == p);
- assert(p->bk->fd == p);
- }
+ {
+ assert((sz & MALLOC_ALIGN_MASK) == 0);
+ assert(aligned_OK(chunk2mem(p)));
+ /* ... matching footer field */
+ assert(next->prev_size == sz);
+ /* ... and is fully consolidated */
+ assert(prev_inuse(p));
+ assert (next == av->top || inuse(next));
+
+ /* ... and has minimally sane links */
+ assert(p->fd->bk == p);
+ assert(p->bk->fd == p);
+ }
else /* markers are always of size (sizeof(size_t)) */
- assert(sz == (sizeof(size_t)));
+ assert(sz == (sizeof(size_t)));
}
/* Properties of inuse chunks */
@@ -146,7 +143,7 @@ void __do_check_inuse_chunk(mchunkptr p)
__do_check_chunk(p);
if (chunk_is_mmapped(p))
- return; /* mmapped chunks have no next/prev */
+ return; /* mmapped chunks have no next/prev */
/* Check whether it claims to be in use ... */
assert(inuse(p));
@@ -156,20 +153,20 @@ void __do_check_inuse_chunk(mchunkptr p)
/* ... and is surrounded by OK chunks.
Since more things can be checked with free chunks than inuse ones,
if an inuse chunk borders them and debug is on, it's worth doing them.
- */
+ */
if (!prev_inuse(p)) {
- /* Note that we cannot even look at prev unless it is not inuse */
- mchunkptr prv = prev_chunk(p);
- assert(next_chunk(prv) == p);
- __do_check_free_chunk(prv);
+ /* Note that we cannot even look at prev unless it is not inuse */
+ mchunkptr prv = prev_chunk(p);
+ assert(next_chunk(prv) == p);
+ __do_check_free_chunk(prv);
}
if (next == av->top) {
- assert(prev_inuse(next));
- assert(chunksize(next) >= MINSIZE);
+ assert(prev_inuse(next));
+ assert(chunksize(next) >= MINSIZE);
}
else if (!inuse(next))
- __do_check_free_chunk(next);
+ __do_check_free_chunk(next);
}
/* Properties of chunks recycled from fastbins */
@@ -198,14 +195,14 @@ void __do_check_malloced_chunk(mchunkptr
__do_check_remalloced_chunk(p, s);
/*
- ... plus, must obey implementation invariant that prev_inuse is
- always true of any allocated chunk; i.e., that each allocated
- chunk borders either a previously allocated and still in-use
- chunk, or the base of its memory arena. This is ensured
- by making all allocations from the the `lowest' part of any found
- chunk. This does not necessarily hold however for chunks
- recycled via fastbins.
- */
+ ... plus, must obey implementation invariant that prev_inuse is
+ always true of any allocated chunk; i.e., that each allocated
+ chunk borders either a previously allocated and still in-use
+ chunk, or the base of its memory arena. This is ensured
+ by making all allocations from the the `lowest' part of any found
+ chunk. This does not necessarily hold however for chunks
+ recycled via fastbins.
+ */
assert(prev_inuse(p));
}
@@ -243,7 +240,7 @@ void __do_check_malloc_state(void)
/* cannot run remaining checks until fully initialized */
if (av->top == 0 || av->top == initial_top(av))
- return;
+ return;
/* pagesize is a power of 2 */
assert((av->pagesize & (av->pagesize-1)) == 0);
@@ -256,64 +253,64 @@ void __do_check_malloc_state(void)
max_fast_bin = fastbin_index(av->max_fast);
for (i = 0; i < NFASTBINS; ++i) {
- p = av->fastbins[i];
+ p = av->fastbins[i];
- /* all bins past max_fast are empty */
- if (i > max_fast_bin)
- assert(p == 0);
-
- while (p != 0) {
- /* each chunk claims to be inuse */
- __do_check_inuse_chunk(p);
- total += chunksize(p);
- /* chunk belongs in this bin */
- assert(fastbin_index(chunksize(p)) == i);
- p = p->fd;
- }
+ /* all bins past max_fast are empty */
+ if (i > max_fast_bin)
+ assert(p == 0);
+
+ while (p != 0) {
+ /* each chunk claims to be inuse */
+ __do_check_inuse_chunk(p);
+ total += chunksize(p);
+ /* chunk belongs in this bin */
+ assert(fastbin_index(chunksize(p)) == i);
+ p = p->fd;
+ }
}
if (total != 0)
- assert(have_fastchunks(av));
+ assert(have_fastchunks(av));
else if (!have_fastchunks(av))
- assert(total == 0);
+ assert(total == 0);
/* check normal bins */
for (i = 1; i < NBINS; ++i) {
- b = bin_at(av,i);
+ b = bin_at(av,i);
- /* binmap is accurate (except for bin 1 == unsorted_chunks) */
- if (i >= 2) {
- binbit = get_binmap(av,i);
- empty = last(b) == b;
- if (!binbit)
- assert(empty);
- else if (!empty)
- assert(binbit);
- }
-
- for (p = last(b); p != b; p = p->bk) {
- /* each chunk claims to be free */
- __do_check_free_chunk(p);
- size = chunksize(p);
- total += size;
- if (i >= 2) {
- /* chunk belongs in bin */
- idx = bin_index(size);
- assert(idx == i);
- /* lists are sorted */
- if ((unsigned long) size >= (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
- assert(p->bk == b ||
- (unsigned long)chunksize(p->bk) >=
- (unsigned long)chunksize(p));
- }
- }
- /* chunk is followed by a legal chain of inuse chunks */
- for (q = next_chunk(p);
- (q != av->top && inuse(q) &&
- (unsigned long)(chunksize(q)) >= MINSIZE);
- q = next_chunk(q))
- __do_check_inuse_chunk(q);
- }
+ /* binmap is accurate (except for bin 1 == unsorted_chunks) */
+ if (i >= 2) {
+ binbit = get_binmap(av,i);
+ empty = last(b) == b;
+ if (!binbit)
+ assert(empty);
+ else if (!empty)
+ assert(binbit);
+ }
+
+ for (p = last(b); p != b; p = p->bk) {
+ /* each chunk claims to be free */
+ __do_check_free_chunk(p);
+ size = chunksize(p);
+ total += size;
+ if (i >= 2) {
+ /* chunk belongs in bin */
+ idx = bin_index(size);
+ assert(idx == i);
+ /* lists are sorted */
+ if ((unsigned long) size >= (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
+ assert(p->bk == b ||
+ (unsigned long)chunksize(p->bk) >=
+ (unsigned long)chunksize(p));
+ }
+ }
+ /* chunk is followed by a legal chain of inuse chunks */
+ for (q = next_chunk(p);
+ (q != av->top && inuse(q) &&
+ (unsigned long)(chunksize(q)) >= MINSIZE);
+ q = next_chunk(q))
+ __do_check_inuse_chunk(q);
+ }
}
/* top chunk is OK */
@@ -326,13 +323,13 @@ void __do_check_malloc_state(void)
assert(av->n_mmaps <= av->max_n_mmaps);
assert((unsigned long)(av->sbrked_mem) <=
- (unsigned long)(av->max_sbrked_mem));
+ (unsigned long)(av->max_sbrked_mem));
assert((unsigned long)(av->mmapped_mem) <=
- (unsigned long)(av->max_mmapped_mem));
+ (unsigned long)(av->max_mmapped_mem));
assert((unsigned long)(av->max_total_mem) >=
- (unsigned long)(av->mmapped_mem) + (unsigned long)(av->sbrked_mem));
+ (unsigned long)(av->mmapped_mem) + (unsigned long)(av->sbrked_mem));
}
#endif
@@ -370,84 +367,84 @@ static void* __malloc_alloc(size_t nb, m
size_t pagemask = av->pagesize - 1;
/*
- If there is space available in fastbins, consolidate and retry
- malloc from scratch rather than getting memory from system. This
- can occur only if nb is in smallbin range so we didn't consolidate
- upon entry to malloc. It is much easier to handle this case here
- than in malloc proper.
- */
+ If there is space available in fastbins, consolidate and retry
+ malloc from scratch rather than getting memory from system. This
+ can occur only if nb is in smallbin range so we didn't consolidate
+ upon entry to malloc. It is much easier to handle this case here
+ than in malloc proper.
+ */
if (have_fastchunks(av)) {
- assert(in_smallbin_range(nb));
- __malloc_consolidate(av);
- return malloc(nb - MALLOC_ALIGN_MASK);
+ assert(in_smallbin_range(nb));
+ __malloc_consolidate(av);
+ return malloc(nb - MALLOC_ALIGN_MASK);
}
/*
- If have mmap, and the request size meets the mmap threshold, and
- the system supports mmap, and there are few enough currently
- allocated mmapped regions, try to directly map this request
- rather than expanding top.
- */
+ If have mmap, and the request size meets the mmap threshold, and
+ the system supports mmap, and there are few enough currently
+ allocated mmapped regions, try to directly map this request
+ rather than expanding top.
+ */
if ((unsigned long)(nb) >= (unsigned long)(av->mmap_threshold) &&
(av->n_mmaps < av->n_mmaps_max)) {
- char* mm; /* return value from mmap call*/
-
- /*
- Round up size to nearest page. For mmapped chunks, the overhead
- is one (sizeof(size_t)) unit larger than for normal chunks, because there
- is no following chunk whose prev_size field could be used.
- */
- size = (nb + (sizeof(size_t)) + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
-
- /* Don't try if size wraps around 0 */
- if ((unsigned long)(size) > (unsigned long)(nb)) {
-
- mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
-
- if (mm != (char*)(MORECORE_FAILURE)) {
+ char* mm; /* return value from mmap call*/
/*
- The offset to the start of the mmapped region is stored
- in the prev_size field of the chunk. This allows us to adjust
- returned start address to meet alignment requirements here
- and in memalign(), and still be able to compute proper
- address argument for later munmap in free() and realloc().
- */
-
- front_misalign = (size_t)chunk2mem(mm) & MALLOC_ALIGN_MASK;
- if (front_misalign > 0) {
- correction = MALLOC_ALIGNMENT - front_misalign;
- p = (mchunkptr)(mm + correction);
- p->prev_size = correction;
- set_head(p, (size - correction) |IS_MMAPPED);
- }
- else {
- p = (mchunkptr)mm;
- p->prev_size = 0;
- set_head(p, size|IS_MMAPPED);
- }
+ Round up size to nearest page. For mmapped chunks, the overhead
+ is one (sizeof(size_t)) unit larger than for normal chunks, because there
+ is no following chunk whose prev_size field could be used.
+ */
+ size = (nb + (sizeof(size_t)) + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
+
+ /* Don't try if size wraps around 0 */
+ if ((unsigned long)(size) > (unsigned long)(nb)) {
+
+ mm = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
+
+ if (mm != (char*)(MORECORE_FAILURE)) {
+
+ /*
+ The offset to the start of the mmapped region is stored
+ in the prev_size field of the chunk. This allows us to adjust
+ returned start address to meet alignment requirements here
+ and in memalign(), and still be able to compute proper
+ address argument for later munmap in free() and realloc().
+ */
+
+ front_misalign = (size_t)chunk2mem(mm) & MALLOC_ALIGN_MASK;
+ if (front_misalign > 0) {
+ correction = MALLOC_ALIGNMENT - front_misalign;
+ p = (mchunkptr)(mm + correction);
+ p->prev_size = correction;
+ set_head(p, (size - correction) |IS_MMAPPED);
+ }
+ else {
+ p = (mchunkptr)mm;
+ p->prev_size = 0;
+ set_head(p, size|IS_MMAPPED);
+ }
+
+ /* update statistics */
+
+ if (++av->n_mmaps > av->max_n_mmaps)
+ av->max_n_mmaps = av->n_mmaps;
+
+ sum = av->mmapped_mem += size;
+ if (sum > (unsigned long)(av->max_mmapped_mem))
+ av->max_mmapped_mem = sum;
+ sum += av->sbrked_mem;
+ if (sum > (unsigned long)(av->max_total_mem))
+ av->max_total_mem = sum;
- /* update statistics */
+ check_chunk(p);
- if (++av->n_mmaps > av->max_n_mmaps)
- av->max_n_mmaps = av->n_mmaps;
-
- sum = av->mmapped_mem += size;
- if (sum > (unsigned long)(av->max_mmapped_mem))
- av->max_mmapped_mem = sum;
- sum += av->sbrked_mem;
- if (sum > (unsigned long)(av->max_total_mem))
- av->max_total_mem = sum;
-
- check_chunk(p);
-
- return chunk2mem(p);
- }
- }
+ return chunk2mem(p);
+ }
+ }
}
/* Record incoming configuration of top */
@@ -462,8 +459,8 @@ static void* __malloc_alloc(size_t nb, m
* be at least MINSIZE and to have prev_inuse set. */
assert((old_top == initial_top(av) && old_size == 0) ||
- ((unsigned long) (old_size) >= MINSIZE &&
- prev_inuse(old_top)));
+ ((unsigned long) (old_size) >= MINSIZE &&
+ prev_inuse(old_top)));
/* Precondition: not enough current space to satisfy nb request */
assert((unsigned long)(old_size) < (unsigned long)(nb + MINSIZE));
@@ -477,272 +474,272 @@ static void* __malloc_alloc(size_t nb, m
size = nb + av->top_pad + MINSIZE;
/*
- If contiguous, we can subtract out existing space that we hope to
- combine with new space. We add it back later only if
- we don't actually get contiguous space.
- */
+ If contiguous, we can subtract out existing space that we hope to
+ combine with new space. We add it back later only if
+ we don't actually get contiguous space.
+ */
if (contiguous(av))
- size -= old_size;
+ size -= old_size;
/*
- Round to a multiple of page size.
- If MORECORE is not contiguous, this ensures that we only call it
- with whole-page arguments. And if MORECORE is contiguous and
- this is not first time through, this preserves page-alignment of
- previous calls. Otherwise, we correct to page-align below.
- */
+ Round to a multiple of page size.
+ If MORECORE is not contiguous, this ensures that we only call it
+ with whole-page arguments. And if MORECORE is contiguous and
+ this is not first time through, this preserves page-alignment of
+ previous calls. Otherwise, we correct to page-align below.
+ */
size = (size + pagemask) & ~pagemask;
/*
- Don't try to call MORECORE if argument is so big as to appear
- negative. Note that since mmap takes size_t arg, it may succeed
- below even if we cannot call MORECORE.
- */
+ Don't try to call MORECORE if argument is so big as to appear
+ negative. Note that since mmap takes size_t arg, it may succeed
+ below even if we cannot call MORECORE.
+ */
if (size > 0)
- brk = (char*)(MORECORE(size));
+ brk = (char*)(MORECORE(size));
/*
- If have mmap, try using it as a backup when MORECORE fails or
- cannot be used. This is worth doing on systems that have "holes" in
- address space, so sbrk cannot extend to give contiguous space, but
- space is available elsewhere. Note that we ignore mmap max count
- and threshold limits, since the space will not be used as a
- segregated mmap region.
- */
+ If have mmap, try using it as a backup when MORECORE fails or
+ cannot be used. This is worth doing on systems that have "holes" in
+ address space, so sbrk cannot extend to give contiguous space, but
+ space is available elsewhere. Note that we ignore mmap max count
+ and threshold limits, since the space will not be used as a
+ segregated mmap region.
+ */
if (brk == (char*)(MORECORE_FAILURE)) {
- /* Cannot merge with old top, so add its size back in */
- if (contiguous(av))
- size = (size + old_size + pagemask) & ~pagemask;
-
- /* If we are relying on mmap as backup, then use larger units */
- if ((unsigned long)(size) < (unsigned long)(MMAP_AS_MORECORE_SIZE))
- size = MMAP_AS_MORECORE_SIZE;
-
- /* Don't try if size wraps around 0 */
- if ((unsigned long)(size) > (unsigned long)(nb)) {
-
- brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
-
- if (brk != (char*)(MORECORE_FAILURE)) {
-
- /* We do not need, and cannot use, another sbrk call to find end */
- snd_brk = brk + size;
-
- /* Record that we no longer have a contiguous sbrk region.
- After the first time mmap is used as backup, we do not
- ever rely on contiguous space since this could incorrectly
- bridge regions.
- */
- set_noncontiguous(av);
- }
- }
+ /* Cannot merge with old top, so add its size back in */
+ if (contiguous(av))
+ size = (size + old_size + pagemask) & ~pagemask;
+
+ /* If we are relying on mmap as backup, then use larger units */
+ if ((unsigned long)(size) < (unsigned long)(MMAP_AS_MORECORE_SIZE))
+ size = MMAP_AS_MORECORE_SIZE;
+
+ /* Don't try if size wraps around 0 */
+ if ((unsigned long)(size) > (unsigned long)(nb)) {
+
+ brk = (char*)(MMAP(0, size, PROT_READ|PROT_WRITE));
+
+ if (brk != (char*)(MORECORE_FAILURE)) {
+
+ /* We do not need, and cannot use, another sbrk call to find end */
+ snd_brk = brk + size;
+
+ /* Record that we no longer have a contiguous sbrk region.
+ After the first time mmap is used as backup, we do not
+ ever rely on contiguous space since this could incorrectly
+ bridge regions.
+ */
+ set_noncontiguous(av);
+ }
+ }
}
if (brk != (char*)(MORECORE_FAILURE)) {
- av->sbrked_mem += size;
+ av->sbrked_mem += size;
- /*
- If MORECORE extends previous space, we can likewise extend top size.
- */
-
- if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
- set_head(old_top, (size + old_size) | PREV_INUSE);
- }
-
- /*
- Otherwise, make adjustments:
-
- * If the first time through or noncontiguous, we need to call sbrk
- just to find out where the end of memory lies.
-
- * We need to ensure that all returned chunks from malloc will meet
- MALLOC_ALIGNMENT
-
- * If there was an intervening foreign sbrk, we need to adjust sbrk
- request size to account for fact that we will not be able to
- combine new space with existing space in old_top.
-
- * Almost all systems internally allocate whole pages at a time, in
- which case we might as well use the whole last page of request.
- So we allocate enough more memory to hit a page boundary now,
- which in turn causes future contiguous calls to page-align.
- */
-
- else {
- front_misalign = 0;
- end_misalign = 0;
- correction = 0;
- aligned_brk = brk;
-
- /*
- If MORECORE returns an address lower than we have seen before,
- we know it isn't really contiguous. This and some subsequent
- checks help cope with non-conforming MORECORE functions and
- the presence of "foreign" calls to MORECORE from outside of
- malloc or by other threads. We cannot guarantee to detect
- these in all cases, but cope with the ones we do detect.
- */
- if (contiguous(av) && old_size != 0 && brk < old_end) {
- set_noncontiguous(av);
- }
-
- /* handle contiguous cases */
- if (contiguous(av)) {
-
- /* We can tolerate forward non-contiguities here (usually due
- to foreign calls) but treat them as part of our space for
- stats reporting. */
- if (old_size != 0)
- av->sbrked_mem += brk - old_end;
-
- /* Guarantee alignment of first new chunk made from this space */
-
- front_misalign = (size_t)chunk2mem(brk) & MALLOC_ALIGN_MASK;
- if (front_misalign > 0) {
-
- /*
- Skip over some bytes to arrive at an aligned position.
- We don't need to specially mark these wasted front bytes.
- They will never be accessed anyway because
- prev_inuse of av->top (and any chunk created from its start)
- is always true after initialization.
- */
+ /*
+ If MORECORE extends previous space, we can likewise extend top size.
+ */
- correction = MALLOC_ALIGNMENT - front_misalign;
- aligned_brk += correction;
+ if (brk == old_end && snd_brk == (char*)(MORECORE_FAILURE)) {
+ set_head(old_top, (size + old_size) | PREV_INUSE);
}
/*
- If this isn't adjacent to existing space, then we will not
- be able to merge with old_top space, so must add to 2nd request.
- */
-
- correction += old_size;
-
- /* Extend the end address to hit a page boundary */
- end_misalign = (size_t)(brk + size + correction);
- correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
-
- assert(correction >= 0);
- snd_brk = (char*)(MORECORE(correction));
-
- if (snd_brk == (char*)(MORECORE_FAILURE)) {
- /*
- If can't allocate correction, try to at least find out current
- brk. It might be enough to proceed without failing.
- */
- correction = 0;
- snd_brk = (char*)(MORECORE(0));
- }
- else if (snd_brk < brk) {
- /*
- If the second call gives noncontiguous space even though
- it says it won't, the only course of action is to ignore
- results of second call, and conservatively estimate where
- the first call left us. Also set noncontiguous, so this
- won't happen again, leaving at most one hole.
-
- Note that this check is intrinsically incomplete. Because
- MORECORE is allowed to give more space than we ask for,
- there is no reliable way to detect a noncontiguity
- producing a forward gap for the second call.
- */
- snd_brk = brk + size;
- correction = 0;
- set_noncontiguous(av);
- }
-
- }
-
- /* handle non-contiguous cases */
- else {
- /* MORECORE/mmap must correctly align */
- assert(aligned_OK(chunk2mem(brk)));
-
- /* Find out current end of memory */
- if (snd_brk == (char*)(MORECORE_FAILURE)) {
- snd_brk = (char*)(MORECORE(0));
- av->sbrked_mem += snd_brk - brk - size;
- }
- }
-
- /* Adjust top based on results of second sbrk */
- if (snd_brk != (char*)(MORECORE_FAILURE)) {
- av->top = (mchunkptr)aligned_brk;
- set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
- av->sbrked_mem += correction;
+ Otherwise, make adjustments:
- /*
- If not the first time through, we either have a
- gap due to foreign sbrk or a non-contiguous region. Insert a
- double fencepost at old_top to prevent consolidation with space
- we don't own. These fenceposts are artificial chunks that are
- marked as inuse and are in any case too small to use. We need
- two to make sizes and alignments work out.
- */
-
- if (old_size != 0) {
- /* Shrink old_top to insert fenceposts, keeping size a
- multiple of MALLOC_ALIGNMENT. We know there is at least
- enough space in old_top to do this.
- */
- old_size = (old_size - 3*(sizeof(size_t))) & ~MALLOC_ALIGN_MASK;
- set_head(old_top, old_size | PREV_INUSE);
-
- /*
- Note that the following assignments completely overwrite
- old_top when old_size was previously MINSIZE. This is
- intentional. We need the fencepost, even if old_top otherwise gets
- lost.
- */
- chunk_at_offset(old_top, old_size )->size =
- (sizeof(size_t))|PREV_INUSE;
-
- chunk_at_offset(old_top, old_size + (sizeof(size_t)))->size =
- (sizeof(size_t))|PREV_INUSE;
-
- /* If possible, release the rest, suppressing trimming. */
- if (old_size >= MINSIZE) {
- size_t tt = av->trim_threshold;
- av->trim_threshold = (size_t)(-1);
- free(chunk2mem(old_top));
- av->trim_threshold = tt;
- }
- }
- }
- }
-
- /* Update statistics */
- sum = av->sbrked_mem;
- if (sum > (unsigned long)(av->max_sbrked_mem))
- av->max_sbrked_mem = sum;
-
- sum += av->mmapped_mem;
- if (sum > (unsigned long)(av->max_total_mem))
- av->max_total_mem = sum;
-
- check_malloc_state();
-
- /* finally, do the allocation */
-
- p = av->top;
- size = chunksize(p);
-
- /* check that one of the above allocation paths succeeded */
- if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
- remainder_size = size - nb;
- remainder = chunk_at_offset(p, nb);
- av->top = remainder;
- set_head(p, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- check_malloced_chunk(p, nb);
- return chunk2mem(p);
- }
+ * If the first time through or noncontiguous, we need to call sbrk
+ just to find out where the end of memory lies.
+
+ * We need to ensure that all returned chunks from malloc will meet
+ MALLOC_ALIGNMENT
+
+ * If there was an intervening foreign sbrk, we need to adjust sbrk
+ request size to account for fact that we will not be able to
+ combine new space with existing space in old_top.
+
+ * Almost all systems internally allocate whole pages at a time, in
+ which case we might as well use the whole last page of request.
+ So we allocate enough more memory to hit a page boundary now,
+ which in turn causes future contiguous calls to page-align.
+ */
+
+ else {
+ front_misalign = 0;
+ end_misalign = 0;
+ correction = 0;
+ aligned_brk = brk;
+
+ /*
+ If MORECORE returns an address lower than we have seen before,
+ we know it isn't really contiguous. This and some subsequent
+ checks help cope with non-conforming MORECORE functions and
+ the presence of "foreign" calls to MORECORE from outside of
+ malloc or by other threads. We cannot guarantee to detect
+ these in all cases, but cope with the ones we do detect.
+ */
+ if (contiguous(av) && old_size != 0 && brk < old_end) {
+ set_noncontiguous(av);
+ }
+
+ /* handle contiguous cases */
+ if (contiguous(av)) {
+
+ /* We can tolerate forward non-contiguities here (usually due
+ to foreign calls) but treat them as part of our space for
+ stats reporting. */
+ if (old_size != 0)
+ av->sbrked_mem += brk - old_end;
+
+ /* Guarantee alignment of first new chunk made from this space */
+
+ front_misalign = (size_t)chunk2mem(brk) & MALLOC_ALIGN_MASK;
+ if (front_misalign > 0) {
+
+ /*
+ Skip over some bytes to arrive at an aligned position.
+ We don't need to specially mark these wasted front bytes.
+ They will never be accessed anyway because
+ prev_inuse of av->top (and any chunk created from its start)
+ is always true after initialization.
+ */
+
+ correction = MALLOC_ALIGNMENT - front_misalign;
+ aligned_brk += correction;
+ }
+
+ /*
+ If this isn't adjacent to existing space, then we will not
+ be able to merge with old_top space, so must add to 2nd request.
+ */
+
+ correction += old_size;
+
+ /* Extend the end address to hit a page boundary */
+ end_misalign = (size_t)(brk + size + correction);
+ correction += ((end_misalign + pagemask) & ~pagemask) - end_misalign;
+
+ assert(correction >= 0);
+ snd_brk = (char*)(MORECORE(correction));
+
+ if (snd_brk == (char*)(MORECORE_FAILURE)) {
+ /*
+ If can't allocate correction, try to at least find out current
+ brk. It might be enough to proceed without failing.
+ */
+ correction = 0;
+ snd_brk = (char*)(MORECORE(0));
+ }
+ else if (snd_brk < brk) {
+ /*
+ If the second call gives noncontiguous space even though
+ it says it won't, the only course of action is to ignore
+ results of second call, and conservatively estimate where
+ the first call left us. Also set noncontiguous, so this
+ won't happen again, leaving at most one hole.
+
+ Note that this check is intrinsically incomplete. Because
+ MORECORE is allowed to give more space than we ask for,
+ there is no reliable way to detect a noncontiguity
+ producing a forward gap for the second call.
+ */
+ snd_brk = brk + size;
+ correction = 0;
+ set_noncontiguous(av);
+ }
+
+ }
+
+ /* handle non-contiguous cases */
+ else {
+ /* MORECORE/mmap must correctly align */
+ assert(aligned_OK(chunk2mem(brk)));
+
+ /* Find out current end of memory */
+ if (snd_brk == (char*)(MORECORE_FAILURE)) {
+ snd_brk = (char*)(MORECORE(0));
+ av->sbrked_mem += snd_brk - brk - size;
+ }
+ }
+
+ /* Adjust top based on results of second sbrk */
+ if (snd_brk != (char*)(MORECORE_FAILURE)) {
+ av->top = (mchunkptr)aligned_brk;
+ set_head(av->top, (snd_brk - aligned_brk + correction) | PREV_INUSE);
+ av->sbrked_mem += correction;
+
+ /*
+ If not the first time through, we either have a
+ gap due to foreign sbrk or a non-contiguous region. Insert a
+ double fencepost at old_top to prevent consolidation with space
+ we don't own. These fenceposts are artificial chunks that are
+ marked as inuse and are in any case too small to use. We need
+ two to make sizes and alignments work out.
+ */
+
+ if (old_size != 0) {
+ /* Shrink old_top to insert fenceposts, keeping size a
+ multiple of MALLOC_ALIGNMENT. We know there is at least
+ enough space in old_top to do this.
+ */
+ old_size = (old_size - 3*(sizeof(size_t))) & ~MALLOC_ALIGN_MASK;
+ set_head(old_top, old_size | PREV_INUSE);
+
+ /*
+ Note that the following assignments completely overwrite
+ old_top when old_size was previously MINSIZE. This is
+ intentional. We need the fencepost, even if old_top otherwise gets
+ lost.
+ */
+ chunk_at_offset(old_top, old_size )->size =
+ (sizeof(size_t))|PREV_INUSE;
+
+ chunk_at_offset(old_top, old_size + (sizeof(size_t)))->size =
+ (sizeof(size_t))|PREV_INUSE;
+
+ /* If possible, release the rest, suppressing trimming. */
+ if (old_size >= MINSIZE) {
+ size_t tt = av->trim_threshold;
+ av->trim_threshold = (size_t)(-1);
+ free(chunk2mem(old_top));
+ av->trim_threshold = tt;
+ }
+ }
+ }
+ }
+
+ /* Update statistics */
+ sum = av->sbrked_mem;
+ if (sum > (unsigned long)(av->max_sbrked_mem))
+ av->max_sbrked_mem = sum;
+
+ sum += av->mmapped_mem;
+ if (sum > (unsigned long)(av->max_total_mem))
+ av->max_total_mem = sum;
+
+ check_malloc_state();
+
+ /* finally, do the allocation */
+
+ p = av->top;
+ size = chunksize(p);
+
+ /* check that one of the above allocation paths succeeded */
+ if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
+ remainder_size = size - nb;
+ remainder = chunk_at_offset(p, nb);
+ av->top = remainder;
+ set_head(p, nb | PREV_INUSE);
+ set_head(remainder, remainder_size | PREV_INUSE);
+ check_malloced_chunk(p, nb);
+ return chunk2mem(p);
+ }
}
@@ -767,25 +764,25 @@ static int __malloc_largebin_index(unsig
#if defined(__GNUC__) && defined(i386)
__asm__("bsrl %1,%0\n\t"
- : "=r" (m)
- : "g" (x));
+ : "=r" (m)
+ : "g" (x));
#else
{
- /*
- Based on branch-free nlz algorithm in chapter 5 of Henry
- S. Warren Jr's book "Hacker's Delight".
- */
-
- unsigned int n = ((x - 0x100) >> 16) & 8;
- x <<= n;
- m = ((x - 0x1000) >> 16) & 4;
- n += m;
- x <<= m;
- m = ((x - 0x4000) >> 16) & 2;
- n += m;
- x = (x << m) >> 14;
- m = 13 - n + (x & ~(x>>1));
+ /*
+ Based on branch-free nlz algorithm in chapter 5 of Henry
+ S. Warren Jr's book "Hacker's Delight".
+ */
+
+ unsigned int n = ((x - 0x100) >> 16) & 8;
+ x <<= n;
+ m = ((x - 0x1000) >> 16) & 4;
+ n += m;
+ x <<= m;
+ m = ((x - 0x4000) >> 16) & 2;
+ n += m;
+ x = (x << m) >> 14;
+ m = 13 - n + (x & ~(x>>1));
}
#endif
@@ -826,69 +823,70 @@ void* malloc(size_t bytes)
mchunkptr fwd; /* misc temp for linking */
mchunkptr bck; /* misc temp for linking */
void * sysmem;
+ void * retval;
#if !defined(__MALLOC_GLIBC_COMPAT__)
if (!bytes) return NULL;
#endif
- LOCK;
+ __MALLOC_LOCK;
av = get_malloc_state();
/*
- Convert request size to internal form by adding (sizeof(size_t)) bytes
- overhead plus possibly more to obtain necessary alignment and/or
- to obtain a size of at least MINSIZE, the smallest allocatable
- size. Also, checked_request2size traps (returning 0) request sizes
- that are so large that they wrap around zero when padded and
- aligned.
- */
+ Convert request size to internal form by adding (sizeof(size_t)) bytes
+ overhead plus possibly more to obtain necessary alignment and/or
+ to obtain a size of at least MINSIZE, the smallest allocatable
+ size. Also, checked_request2size traps (returning 0) request sizes
+ that are so large that they wrap around zero when padded and
+ aligned.
+ */
checked_request2size(bytes, nb);
/*
- Bypass search if no frees yet
- */
+ Bypass search if no frees yet
+ */
if (!have_anychunks(av)) {
- if (av->max_fast == 0) /* initialization check */
- __malloc_consolidate(av);
- goto use_top;
+ if (av->max_fast == 0) /* initialization check */
+ __malloc_consolidate(av);
+ goto use_top;
}
/*
- If the size qualifies as a fastbin, first check corresponding bin.
- */
+ If the size qualifies as a fastbin, first check corresponding bin.
+ */
if ((unsigned long)(nb) <= (unsigned long)(av->max_fast)) {
- fb = &(av->fastbins[(fastbin_index(nb))]);
- if ( (victim = *fb) != 0) {
- *fb = victim->fd;
- check_remalloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
- }
+ fb = &(av->fastbins[(fastbin_index(nb))]);
+ if ( (victim = *fb) != 0) {
+ *fb = victim->fd;
+ check_remalloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
}
/*
- If a small request, check regular bin. Since these "smallbins"
- hold one size each, no searching within bins is necessary.
- (For a large request, we need to wait until unsorted chunks are
- processed to find best fit. But for small ones, fits are exact
- anyway, so we can check now, which is faster.)
- */
+ If a small request, check regular bin. Since these "smallbins"
+ hold one size each, no searching within bins is necessary.
+ (For a large request, we need to wait until unsorted chunks are
+ processed to find best fit. But for small ones, fits are exact
+ anyway, so we can check now, which is faster.)
+ */
if (in_smallbin_range(nb)) {
- idx = smallbin_index(nb);
- bin = bin_at(av,idx);
+ idx = smallbin_index(nb);
+ bin = bin_at(av,idx);
- if ( (victim = last(bin)) != bin) {
- bck = victim->bk;
- set_inuse_bit_at_offset(victim, nb);
- bin->bk = bck;
- bck->fd = bin;
-
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
- }
+ if ( (victim = last(bin)) != bin) {
+ bck = victim->bk;
+ set_inuse_bit_at_offset(victim, nb);
+ bin->bk = bck;
+ bck->fd = bin;
+
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
}
/* If this is a large request, consolidate fastbins before continuing.
@@ -899,154 +897,154 @@ void* malloc(size_t bytes)
large requests, but less often mixtures, so consolidation is not
invoked all that often in most programs. And the programs that
it is called frequently in otherwise tend to fragment.
- */
+ */
else {
- idx = __malloc_largebin_index(nb);
- if (have_fastchunks(av))
- __malloc_consolidate(av);
+ idx = __malloc_largebin_index(nb);
+ if (have_fastchunks(av))
+ __malloc_consolidate(av);
}
/*
- Process recently freed or remaindered chunks, taking one only if
- it is exact fit, or, if this a small request, the chunk is remainder from
- the most recent non-exact fit. Place other traversed chunks in
- bins. Note that this step is the only place in any routine where
- chunks are placed in bins.
- */
+ Process recently freed or remaindered chunks, taking one only if
+ it is exact fit, or, if this a small request, the chunk is remainder from
+ the most recent non-exact fit. Place other traversed chunks in
+ bins. Note that this step is the only place in any routine where
+ chunks are placed in bins.
+ */
while ( (victim = unsorted_chunks(av)->bk) != unsorted_chunks(av)) {
- bck = victim->bk;
- size = chunksize(victim);
+ bck = victim->bk;
+ size = chunksize(victim);
+
+ /* If a small request, try to use last remainder if it is the
+ only chunk in unsorted bin. This helps promote locality for
+ runs of consecutive small requests. This is the only
+ exception to best-fit, and applies only when there is
+ no exact fit for a small chunk.
+ */
+
+ if (in_smallbin_range(nb) &&
+ bck == unsorted_chunks(av) &&
+ victim == av->last_remainder &&
+ (unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
+
+ /* split and reattach remainder */
+ remainder_size = size - nb;
+ remainder = chunk_at_offset(victim, nb);
+ unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
+ av->last_remainder = remainder;
+ remainder->bk = remainder->fd = unsorted_chunks(av);
+
+ set_head(victim, nb | PREV_INUSE);
+ set_head(remainder, remainder_size | PREV_INUSE);
+ set_foot(remainder, remainder_size);
+
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
+
+ /* remove from unsorted list */
+ unsorted_chunks(av)->bk = bck;
+ bck->fd = unsorted_chunks(av);
+
+ /* Take now instead of binning if exact fit */
+
+ if (size == nb) {
+ set_inuse_bit_at_offset(victim, size);
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
+
+ /* place chunk in bin */
- /* If a small request, try to use last remainder if it is the
- only chunk in unsorted bin. This helps promote locality for
- runs of consecutive small requests. This is the only
- exception to best-fit, and applies only when there is
- no exact fit for a small chunk.
- */
-
- if (in_smallbin_range(nb) &&
- bck == unsorted_chunks(av) &&
- victim == av->last_remainder &&
- (unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
-
- /* split and reattach remainder */
- remainder_size = size - nb;
- remainder = chunk_at_offset(victim, nb);
- unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
- av->last_remainder = remainder;
- remainder->bk = remainder->fd = unsorted_chunks(av);
-
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- set_foot(remainder, remainder_size);
-
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
- }
-
- /* remove from unsorted list */
- unsorted_chunks(av)->bk = bck;
- bck->fd = unsorted_chunks(av);
-
- /* Take now instead of binning if exact fit */
-
- if (size == nb) {
- set_inuse_bit_at_offset(victim, size);
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
- }
-
- /* place chunk in bin */
-
- if (in_smallbin_range(size)) {
- victim_index = smallbin_index(size);
- bck = bin_at(av, victim_index);
- fwd = bck->fd;
- }
- else {
- victim_index = __malloc_largebin_index(size);
- bck = bin_at(av, victim_index);
- fwd = bck->fd;
-
- if (fwd != bck) {
- /* if smaller than smallest, place first */
- if ((unsigned long)(size) < (unsigned long)(bck->bk->size)) {
- fwd = bck;
- bck = bck->bk;
- }
- else if ((unsigned long)(size) >=
- (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
-
- /* maintain large bins in sorted order */
- size |= PREV_INUSE; /* Or with inuse bit to speed comparisons */
- while ((unsigned long)(size) < (unsigned long)(fwd->size))
- fwd = fwd->fd;
- bck = fwd->bk;
- }
- }
- }
-
- mark_bin(av, victim_index);
- victim->bk = bck;
- victim->fd = fwd;
- fwd->bk = victim;
- bck->fd = victim;
+ if (in_smallbin_range(size)) {
+ victim_index = smallbin_index(size);
+ bck = bin_at(av, victim_index);
+ fwd = bck->fd;
+ }
+ else {
+ victim_index = __malloc_largebin_index(size);
+ bck = bin_at(av, victim_index);
+ fwd = bck->fd;
+
+ if (fwd != bck) {
+ /* if smaller than smallest, place first */
+ if ((unsigned long)(size) < (unsigned long)(bck->bk->size)) {
+ fwd = bck;
+ bck = bck->bk;
+ }
+ else if ((unsigned long)(size) >=
+ (unsigned long)(FIRST_SORTED_BIN_SIZE)) {
+
+ /* maintain large bins in sorted order */
+ size |= PREV_INUSE; /* Or with inuse bit to speed comparisons */
+ while ((unsigned long)(size) < (unsigned long)(fwd->size))
+ fwd = fwd->fd;
+ bck = fwd->bk;
+ }
+ }
+ }
+
+ mark_bin(av, victim_index);
+ victim->bk = bck;
+ victim->fd = fwd;
+ fwd->bk = victim;
+ bck->fd = victim;
}
/*
- If a large request, scan through the chunks of current bin to
- find one that fits. (This will be the smallest that fits unless
- FIRST_SORTED_BIN_SIZE has been changed from default.) This is
- the only step where an unbounded number of chunks might be
- scanned without doing anything useful with them. However the
- lists tend to be short.
- */
+ If a large request, scan through the chunks of current bin to
+ find one that fits. (This will be the smallest that fits unless
+ FIRST_SORTED_BIN_SIZE has been changed from default.) This is
+ the only step where an unbounded number of chunks might be
+ scanned without doing anything useful with them. However the
+ lists tend to be short.
+ */
if (!in_smallbin_range(nb)) {
- bin = bin_at(av, idx);
-
- for (victim = last(bin); victim != bin; victim = victim->bk) {
- size = chunksize(victim);
+ bin = bin_at(av, idx);
- if ((unsigned long)(size) >= (unsigned long)(nb)) {
- remainder_size = size - nb;
- unlink(victim, bck, fwd);
+ for (victim = last(bin); victim != bin; victim = victim->bk) {
+ size = chunksize(victim);
- /* Exhaust */
- if (remainder_size < MINSIZE) {
- set_inuse_bit_at_offset(victim, size);
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
- }
- /* Split */
- else {
- remainder = chunk_at_offset(victim, nb);
- unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
- remainder->bk = remainder->fd = unsorted_chunks(av);
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- set_foot(remainder, remainder_size);
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
+ if ((unsigned long)(size) >= (unsigned long)(nb)) {
+ remainder_size = size - nb;
+ unlink(victim, bck, fwd);
+
+ /* Exhaust */
+ if (remainder_size < MINSIZE) {
+ set_inuse_bit_at_offset(victim, size);
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
+ /* Split */
+ else {
+ remainder = chunk_at_offset(victim, nb);
+ unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
+ remainder->bk = remainder->fd = unsorted_chunks(av);
+ set_head(victim, nb | PREV_INUSE);
+ set_head(remainder, remainder_size | PREV_INUSE);
+ set_foot(remainder, remainder_size);
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
+ }
}
- }
- }
}
/*
- Search for a chunk by scanning bins, starting with next largest
- bin. This search is strictly by best-fit; i.e., the smallest
- (with ties going to approximately the least recently used) chunk
- that fits is selected.
+ Search for a chunk by scanning bins, starting with next largest
+ bin. This search is strictly by best-fit; i.e., the smallest
+ (with ties going to approximately the least recently used) chunk
+ that fits is selected.
- The bitmap avoids needing to check that most blocks are nonempty.
- */
+ The bitmap avoids needing to check that most blocks are nonempty.
+ */
++idx;
bin = bin_at(av,idx);
@@ -1056,109 +1054,111 @@ void* malloc(size_t bytes)
for (;;) {
- /* Skip rest of block if there are no more set bits in this block. */
- if (bit > map || bit == 0) {
- do {
- if (++block >= BINMAPSIZE) /* out of bins */
- goto use_top;
- } while ( (map = av->binmap[block]) == 0);
-
- bin = bin_at(av, (block << BINMAPSHIFT));
- bit = 1;
- }
-
- /* Advance to bin with set bit. There must be one. */
- while ((bit & map) == 0) {
- bin = next_bin(bin);
- bit <<= 1;
- assert(bit != 0);
- }
-
- /* Inspect the bin. It is likely to be non-empty */
- victim = last(bin);
-
- /* If a false alarm (empty bin), clear the bit. */
- if (victim == bin) {
- av->binmap[block] = map &= ~bit; /* Write through */
- bin = next_bin(bin);
- bit <<= 1;
- }
-
- else {
- size = chunksize(victim);
-
- /* We know the first chunk in this bin is big enough to use. */
- assert((unsigned long)(size) >= (unsigned long)(nb));
-
- remainder_size = size - nb;
-
- /* unlink */
- bck = victim->bk;
- bin->bk = bck;
- bck->fd = bin;
-
- /* Exhaust */
- if (remainder_size < MINSIZE) {
- set_inuse_bit_at_offset(victim, size);
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
- }
+ /* Skip rest of block if there are no more set bits in this block. */
+ if (bit > map || bit == 0) {
+ do {
+ if (++block >= BINMAPSIZE) /* out of bins */
+ goto use_top;
+ } while ( (map = av->binmap[block]) == 0);
- /* Split */
- else {
- remainder = chunk_at_offset(victim, nb);
+ bin = bin_at(av, (block << BINMAPSHIFT));
+ bit = 1;
+ }
- unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
- remainder->bk = remainder->fd = unsorted_chunks(av);
- /* advertise as last remainder */
- if (in_smallbin_range(nb))
- av->last_remainder = remainder;
+ /* Advance to bin with set bit. There must be one. */
+ while ((bit & map) == 0) {
+ bin = next_bin(bin);
+ bit <<= 1;
+ assert(bit != 0);
+ }
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
- set_foot(remainder, remainder_size);
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
- }
- }
+ /* Inspect the bin. It is likely to be non-empty */
+ victim = last(bin);
+
+ /* If a false alarm (empty bin), clear the bit. */
+ if (victim == bin) {
+ av->binmap[block] = map &= ~bit; /* Write through */
+ bin = next_bin(bin);
+ bit <<= 1;
+ }
+
+ else {
+ size = chunksize(victim);
+
+ /* We know the first chunk in this bin is big enough to use. */
+ assert((unsigned long)(size) >= (unsigned long)(nb));
+
+ remainder_size = size - nb;
+
+ /* unlink */
+ bck = victim->bk;
+ bin->bk = bck;
+ bck->fd = bin;
+
+ /* Exhaust */
+ if (remainder_size < MINSIZE) {
+ set_inuse_bit_at_offset(victim, size);
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
+
+ /* Split */
+ else {
+ remainder = chunk_at_offset(victim, nb);
+
+ unsorted_chunks(av)->bk = unsorted_chunks(av)->fd = remainder;
+ remainder->bk = remainder->fd = unsorted_chunks(av);
+ /* advertise as last remainder */
+ if (in_smallbin_range(nb))
+ av->last_remainder = remainder;
+
+ set_head(victim, nb | PREV_INUSE);
+ set_head(remainder, remainder_size | PREV_INUSE);
+ set_foot(remainder, remainder_size);
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
+ }
+ }
}
-use_top:
+ use_top:
/*
- If large enough, split off the chunk bordering the end of memory
- (held in av->top). Note that this is in accord with the best-fit
- search rule. In effect, av->top is treated as larger (and thus
- less well fitting) than any other available chunk since it can
- be extended to be as large as necessary (up to system
- limitations).
-
- We require that av->top always exists (i.e., has size >=
- MINSIZE) after initialization, so if it would otherwise be
- exhuasted by current request, it is replenished. (The main
- reason for ensuring it exists is that we may need MINSIZE space
- to put in fenceposts in sysmalloc.)
- */
+ If large enough, split off the chunk bordering the end of memory
+ (held in av->top). Note that this is in accord with the best-fit
+ search rule. In effect, av->top is treated as larger (and thus
+ less well fitting) than any other available chunk since it can
+ be extended to be as large as necessary (up to system
+ limitations).
+
+ We require that av->top always exists (i.e., has size >=
+ MINSIZE) after initialization, so if it would otherwise be
+ exhuasted by current request, it is replenished. (The main
+ reason for ensuring it exists is that we may need MINSIZE space
+ to put in fenceposts in sysmalloc.)
+ */
victim = av->top;
size = chunksize(victim);
if ((unsigned long)(size) >= (unsigned long)(nb + MINSIZE)) {
- remainder_size = size - nb;
- remainder = chunk_at_offset(victim, nb);
- av->top = remainder;
- set_head(victim, nb | PREV_INUSE);
- set_head(remainder, remainder_size | PREV_INUSE);
-
- check_malloced_chunk(victim, nb);
- UNLOCK;
- return chunk2mem(victim);
+ remainder_size = size - nb;
+ remainder = chunk_at_offset(victim, nb);
+ av->top = remainder;
+ set_head(victim, nb | PREV_INUSE);
+ set_head(remainder, remainder_size | PREV_INUSE);
+
+ check_malloced_chunk(victim, nb);
+ retval = chunk2mem(victim);
+ goto DONE;
}
/* If no space in top, relay to handle system-dependent cases */
sysmem = __malloc_alloc(nb, av);
- UNLOCK;
- return sysmem;
+ retval = sysmem;
+ DONE:
+ __MALLOC_UNLOCK;
+ return retval;
}
diff --git a/libc/stdlib/malloc-standard/malloc.h b/libc/stdlib/malloc-standard/malloc.h
index fbc1492..14a0dd9 100644
--- a/libc/stdlib/malloc-standard/malloc.h
+++ b/libc/stdlib/malloc-standard/malloc.h
@@ -22,16 +22,12 @@
#include <malloc.h>
#include <stdlib.h>
+#include <bits/uClibc_mutex.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-extern pthread_mutex_t __malloc_lock;
-# define LOCK __pthread_mutex_lock(&__malloc_lock)
-# define UNLOCK __pthread_mutex_unlock(&__malloc_lock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+__UCLIBC_MUTEX_EXTERN(__malloc_lock);
+
+#define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
+#define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
diff --git a/libc/stdlib/malloc-standard/mallopt.c b/libc/stdlib/malloc-standard/mallopt.c
index e287920..41aa614 100644
--- a/libc/stdlib/malloc-standard/mallopt.c
+++ b/libc/stdlib/malloc-standard/mallopt.c
@@ -8,7 +8,7 @@
VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
Note: There may be an updated version of this malloc obtainable at
- ftp://gee.cs.oswego.edu/pub/misc/malloc.c
+ ftp://gee.cs.oswego.edu/pub/misc/malloc.c
Check before installing!
Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
@@ -25,40 +25,40 @@ int mallopt(int param_number, int value)
ret = 0;
- LOCK;
+ __MALLOC_LOCK;
av = get_malloc_state();
/* Ensure initialization/consolidation */
__malloc_consolidate(av);
switch(param_number) {
- case M_MXFAST:
- if (value >= 0 && value <= MAX_FAST_SIZE) {
- set_max_fast(av, value);
- ret = 1;
- }
- break;
-
- case M_TRIM_THRESHOLD:
- av->trim_threshold = value;
- ret = 1;
- break;
-
- case M_TOP_PAD:
- av->top_pad = value;
- ret = 1;
- break;
-
- case M_MMAP_THRESHOLD:
- av->mmap_threshold = value;
- ret = 1;
- break;
-
- case M_MMAP_MAX:
- av->n_mmaps_max = value;
- ret = 1;
- break;
+ case M_MXFAST:
+ if (value >= 0 && value <= MAX_FAST_SIZE) {
+ set_max_fast(av, value);
+ ret = 1;
+ }
+ break;
+
+ case M_TRIM_THRESHOLD:
+ av->trim_threshold = value;
+ ret = 1;
+ break;
+
+ case M_TOP_PAD:
+ av->top_pad = value;
+ ret = 1;
+ break;
+
+ case M_MMAP_THRESHOLD:
+ av->mmap_threshold = value;
+ ret = 1;
+ break;
+
+ case M_MMAP_MAX:
+ av->n_mmaps_max = value;
+ ret = 1;
+ break;
}
- UNLOCK;
+ __MALLOC_UNLOCK;
return ret;
}
diff --git a/libc/stdlib/malloc-standard/memalign.c b/libc/stdlib/malloc-standard/memalign.c
index bd95362..e78d752 100644
--- a/libc/stdlib/malloc-standard/memalign.c
+++ b/libc/stdlib/malloc-standard/memalign.c
@@ -8,7 +8,7 @@
VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
Note: There may be an updated version of this malloc obtainable at
- ftp://gee.cs.oswego.edu/pub/misc/malloc.c
+ ftp://gee.cs.oswego.edu/pub/misc/malloc.c
Check before installing!
Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
@@ -35,6 +35,7 @@ void* memalign(size_t alignment, size_t
mchunkptr remainder; /* spare room at end to split off */
unsigned long remainder_size; /* its size */
size_t size;
+ void *retval;
/* If need less alignment than we give anyway, just relay to malloc */
@@ -46,12 +47,12 @@ void* memalign(size_t alignment, size_t
/* Make sure alignment is power of 2 (in case MINSIZE is not). */
if ((alignment & (alignment - 1)) != 0) {
- size_t a = MALLOC_ALIGNMENT * 2;
- while ((unsigned long)a < (unsigned long)alignment) a <<= 1;
- alignment = a;
+ size_t a = MALLOC_ALIGNMENT * 2;
+ while ((unsigned long)a < (unsigned long)alignment) a <<= 1;
+ alignment = a;
}
- LOCK;
+ __MALLOC_LOCK;
checked_request2size(bytes, nb);
/* Strategy: find a spot within that chunk that meets the alignment
@@ -63,64 +64,67 @@ void* memalign(size_t alignment, size_t
m = (char*)(malloc(nb + alignment + MINSIZE));
if (m == 0) {
- UNLOCK;
- return 0; /* propagate failure */
+ retval = 0; /* propagate failure */
+ goto DONE;
}
p = mem2chunk(m);
if ((((unsigned long)(m)) % alignment) != 0) { /* misaligned */
- /*
- Find an aligned spot inside chunk. Since we need to give back
- leading space in a chunk of at least MINSIZE, if the first
- calculation places us at a spot with less than MINSIZE leader,
- we can move to the next aligned spot -- we've allocated enough
- total room so that this is always possible.
- */
-
- brk = (char*)mem2chunk((unsigned long)(((unsigned long)(m + alignment - 1)) &
- -((signed long) alignment)));
- if ((unsigned long)(brk - (char*)(p)) < MINSIZE)
- brk += alignment;
-
- newp = (mchunkptr)brk;
- leadsize = brk - (char*)(p);
- newsize = chunksize(p) - leadsize;
-
- /* For mmapped chunks, just adjust offset */
- if (chunk_is_mmapped(p)) {
- newp->prev_size = p->prev_size + leadsize;
- set_head(newp, newsize|IS_MMAPPED);
- UNLOCK;
- return chunk2mem(newp);
- }
-
- /* Otherwise, give back leader, use the rest */
- set_head(newp, newsize | PREV_INUSE);
- set_inuse_bit_at_offset(newp, newsize);
- set_head_size(p, leadsize);
- free(chunk2mem(p));
- p = newp;
+ /*
+ Find an aligned spot inside chunk. Since we need to give back
+ leading space in a chunk of at least MINSIZE, if the first
+ calculation places us at a spot with less than MINSIZE leader,
+ we can move to the next aligned spot -- we've allocated enough
+ total room so that this is always possible.
+ */
+
+ brk = (char*)mem2chunk((unsigned long)(((unsigned long)(m + alignment - 1)) &
+ -((signed long) alignment)));
+ if ((unsigned long)(brk - (char*)(p)) < MINSIZE)
+ brk += alignment;
+
+ newp = (mchunkptr)brk;
+ leadsize = brk - (char*)(p);
+ newsize = chunksize(p) - leadsize;
+
+ /* For mmapped chunks, just adjust offset */
+ if (chunk_is_mmapped(p)) {
+ newp->prev_size = p->prev_size + leadsize;
+ set_head(newp, newsize|IS_MMAPPED);
+ retval = chunk2mem(newp);
+ goto DONE;
+ }
+
+ /* Otherwise, give back leader, use the rest */
+ set_head(newp, newsize | PREV_INUSE);
+ set_inuse_bit_at_offset(newp, newsize);
+ set_head_size(p, leadsize);
+ free(chunk2mem(p));
+ p = newp;
- assert (newsize >= nb &&
- (((unsigned long)(chunk2mem(p))) % alignment) == 0);
+ assert (newsize >= nb &&
+ (((unsigned long)(chunk2mem(p))) % alignment) == 0);
}
/* Also give back spare room at the end */
if (!chunk_is_mmapped(p)) {
- size = chunksize(p);
- if ((unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
- remainder_size = size - nb;
- remainder = chunk_at_offset(p, nb);
- set_head(remainder, remainder_size | PREV_INUSE);
- set_head_size(p, nb);
- free(chunk2mem(remainder));
- }
+ size = chunksize(p);
+ if ((unsigned long)(size) > (unsigned long)(nb + MINSIZE)) {
+ remainder_size = size - nb;
+ remainder = chunk_at_offset(p, nb);
+ set_head(remainder, remainder_size | PREV_INUSE);
+ set_head_size(p, nb);
+ free(chunk2mem(remainder));
+ }
}
check_inuse_chunk(p);
- UNLOCK;
- return chunk2mem(p);
+ retval = chunk2mem(p);
+
+ DONE:
+ __MALLOC_UNLOCK;
+ return retval;
}
diff --git a/libc/stdlib/malloc-standard/realloc.c b/libc/stdlib/malloc-standard/realloc.c
index 1950130..9ca4b26 100644
--- a/libc/stdlib/malloc-standard/realloc.c
+++ b/libc/stdlib/malloc-standard/realloc.c
@@ -8,7 +8,7 @@
VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
Note: There may be an updated version of this malloc obtainable at
- ftp://gee.cs.oswego.edu/pub/misc/malloc.c
+ ftp://gee.cs.oswego.edu/pub/misc/malloc.c
Check before installing!
Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
@@ -23,14 +23,14 @@ void* realloc(void* oldmem, size_t bytes
{
mstate av;
- size_t nb; /* padded request size */
+ size_t nb; /* padded request size */
mchunkptr oldp; /* chunk corresponding to oldmem */
- size_t oldsize; /* its size */
+ size_t oldsize; /* its size */
mchunkptr newp; /* chunk to return */
- size_t newsize; /* its size */
- void* newmem; /* corresponding user mem */
+ size_t newsize; /* its size */
+ void* newmem; /* corresponding user mem */
mchunkptr next; /* next contiguous chunk after oldp */
@@ -40,21 +40,23 @@ void* realloc(void* oldmem, size_t bytes
mchunkptr bck; /* misc temp for linking */
mchunkptr fwd; /* misc temp for linking */
- unsigned long copysize; /* bytes to copy */
+ unsigned long copysize; /* bytes to copy */
unsigned int ncopies; /* size_t words to copy */
- size_t* s; /* copy source */
- size_t* d; /* copy destination */
+ size_t* s; /* copy source */
+ size_t* d; /* copy destination */
+
+ void *retval;
/* Check for special cases. */
if (! oldmem)
- return malloc(bytes);
+ return malloc(bytes);
if (! bytes) {
- free (oldmem);
- return malloc(bytes);
+ free (oldmem);
+ return malloc(bytes);
}
- LOCK;
+ __MALLOC_LOCK;
av = get_malloc_state();
checked_request2size(bytes, nb);
@@ -65,173 +67,176 @@ void* realloc(void* oldmem, size_t bytes
if (!chunk_is_mmapped(oldp)) {
- if ((unsigned long)(oldsize) >= (unsigned long)(nb)) {
- /* already big enough; split below */
- newp = oldp;
- newsize = oldsize;
- }
-
- else {
- next = chunk_at_offset(oldp, oldsize);
-
- /* Try to expand forward into top */
- if (next == av->top &&
- (unsigned long)(newsize = oldsize + chunksize(next)) >=
- (unsigned long)(nb + MINSIZE)) {
- set_head_size(oldp, nb);
- av->top = chunk_at_offset(oldp, nb);
- set_head(av->top, (newsize - nb) | PREV_INUSE);
- UNLOCK;
- return chunk2mem(oldp);
- }
-
- /* Try to expand forward into next chunk; split off remainder below */
- else if (next != av->top &&
- !inuse(next) &&
- (unsigned long)(newsize = oldsize + chunksize(next)) >=
- (unsigned long)(nb)) {
- newp = oldp;
- unlink(next, bck, fwd);
- }
-
- /* allocate, copy, free */
- else {
- newmem = malloc(nb - MALLOC_ALIGN_MASK);
- if (newmem == 0) {
- UNLOCK;
- return 0; /* propagate failure */
- }
-
- newp = mem2chunk(newmem);
- newsize = chunksize(newp);
-
- /*
- Avoid copy if newp is next chunk after oldp.
- */
- if (newp == next) {
- newsize += oldsize;
- newp = oldp;
+ if ((unsigned long)(oldsize) >= (unsigned long)(nb)) {
+ /* already big enough; split below */
+ newp = oldp;
+ newsize = oldsize;
}
+
else {
- /*
- Unroll copy of <= 36 bytes (72 if 8byte sizes)
- We know that contents have an odd number of
- size_t-sized words; minimally 3.
- */
-
- copysize = oldsize - (sizeof(size_t));
- s = (size_t*)(oldmem);
- d = (size_t*)(newmem);
- ncopies = copysize / sizeof(size_t);
- assert(ncopies >= 3);
-
- if (ncopies > 9)
- memcpy(d, s, copysize);
-
- else {
- *(d+0) = *(s+0);
- *(d+1) = *(s+1);
- *(d+2) = *(s+2);
- if (ncopies > 4) {
- *(d+3) = *(s+3);
- *(d+4) = *(s+4);
- if (ncopies > 6) {
- *(d+5) = *(s+5);
- *(d+6) = *(s+6);
- if (ncopies > 8) {
- *(d+7) = *(s+7);
- *(d+8) = *(s+8);
+ next = chunk_at_offset(oldp, oldsize);
+
+ /* Try to expand forward into top */
+ if (next == av->top &&
+ (unsigned long)(newsize = oldsize + chunksize(next)) >=
+ (unsigned long)(nb + MINSIZE)) {
+ set_head_size(oldp, nb);
+ av->top = chunk_at_offset(oldp, nb);
+ set_head(av->top, (newsize - nb) | PREV_INUSE);
+ retval = chunk2mem(oldp);
+ goto DONE;
+ }
+
+ /* Try to expand forward into next chunk; split off remainder below */
+ else if (next != av->top &&
+ !inuse(next) &&
+ (unsigned long)(newsize = oldsize + chunksize(next)) >=
+ (unsigned long)(nb)) {
+ newp = oldp;
+ unlink(next, bck, fwd);
+ }
+
+ /* allocate, copy, free */
+ else {
+ newmem = malloc(nb - MALLOC_ALIGN_MASK);
+ if (newmem == 0) {
+ retval = 0; /* propagate failure */
+ goto DONE;
+ }
+
+ newp = mem2chunk(newmem);
+ newsize = chunksize(newp);
+
+ /*
+ Avoid copy if newp is next chunk after oldp.
+ */
+ if (newp == next) {
+ newsize += oldsize;
+ newp = oldp;
+ }
+ else {
+ /*
+ Unroll copy of <= 36 bytes (72 if 8byte sizes)
+ We know that contents have an odd number of
+ size_t-sized words; minimally 3.
+ */
+
+ copysize = oldsize - (sizeof(size_t));
+ s = (size_t*)(oldmem);
+ d = (size_t*)(newmem);
+ ncopies = copysize / sizeof(size_t);
+ assert(ncopies >= 3);
+
+ if (ncopies > 9)
+ memcpy(d, s, copysize);
+
+ else {
+ *(d+0) = *(s+0);
+ *(d+1) = *(s+1);
+ *(d+2) = *(s+2);
+ if (ncopies > 4) {
+ *(d+3) = *(s+3);
+ *(d+4) = *(s+4);
+ if (ncopies > 6) {
+ *(d+5) = *(s+5);
+ *(d+6) = *(s+6);
+ if (ncopies > 8) {
+ *(d+7) = *(s+7);
+ *(d+8) = *(s+8);
+ }
+ }
+ }
+ }
+
+ free(oldmem);
+ check_inuse_chunk(newp);
+ retval = chunk2mem(newp);
+ goto DONE;
}
- }
}
- }
+ }
+
+ /* If possible, free extra space in old or extended chunk */
+
+ assert((unsigned long)(newsize) >= (unsigned long)(nb));
+
+ remainder_size = newsize - nb;
- free(oldmem);
- check_inuse_chunk(newp);
- UNLOCK;
- return chunk2mem(newp);
- }
- }
- }
-
- /* If possible, free extra space in old or extended chunk */
-
- assert((unsigned long)(newsize) >= (unsigned long)(nb));
-
- remainder_size = newsize - nb;
-
- if (remainder_size < MINSIZE) { /* not enough extra to split off */
- set_head_size(newp, newsize);
- set_inuse_bit_at_offset(newp, newsize);
- }
- else { /* split remainder */
- remainder = chunk_at_offset(newp, nb);
- set_head_size(newp, nb);
- set_head(remainder, remainder_size | PREV_INUSE);
- /* Mark remainder as inuse so free() won't complain */
- set_inuse_bit_at_offset(remainder, remainder_size);
- free(chunk2mem(remainder));
- }
-
- check_inuse_chunk(newp);
- UNLOCK;
- return chunk2mem(newp);
+ if (remainder_size < MINSIZE) { /* not enough extra to split off */
+ set_head_size(newp, newsize);
+ set_inuse_bit_at_offset(newp, newsize);
+ }
+ else { /* split remainder */
+ remainder = chunk_at_offset(newp, nb);
+ set_head_size(newp, nb);
+ set_head(remainder, remainder_size | PREV_INUSE);
+ /* Mark remainder as inuse so free() won't complain */
+ set_inuse_bit_at_offset(remainder, remainder_size);
+ free(chunk2mem(remainder));
+ }
+
+ check_inuse_chunk(newp);
+ retval = chunk2mem(newp);
+ goto DONE;
}
/*
- Handle mmap cases
- */
+ Handle mmap cases
+ */
else {
- size_t offset = oldp->prev_size;
- size_t pagemask = av->pagesize - 1;
- char *cp;
- unsigned long sum;
-
- /* Note the extra (sizeof(size_t)) overhead */
- newsize = (nb + offset + (sizeof(size_t)) + pagemask) & ~pagemask;
-
- /* don't need to remap if still within same page */
- if (oldsize == newsize - offset) {
- UNLOCK;
- return oldmem;
- }
-
- cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
-
- if (cp != (char*)MORECORE_FAILURE) {
-
- newp = (mchunkptr)(cp + offset);
- set_head(newp, (newsize - offset)|IS_MMAPPED);
-
- assert(aligned_OK(chunk2mem(newp)));
- assert((newp->prev_size == offset));
-
- /* update statistics */
- sum = av->mmapped_mem += newsize - oldsize;
- if (sum > (unsigned long)(av->max_mmapped_mem))
- av->max_mmapped_mem = sum;
- sum += av->sbrked_mem;
- if (sum > (unsigned long)(av->max_total_mem))
- av->max_total_mem = sum;
-
- UNLOCK;
- return chunk2mem(newp);
- }
-
- /* Note the extra (sizeof(size_t)) overhead. */
- if ((unsigned long)(oldsize) >= (unsigned long)(nb + (sizeof(size_t))))
- newmem = oldmem; /* do nothing */
- else {
- /* Must alloc, copy, free. */
- newmem = malloc(nb - MALLOC_ALIGN_MASK);
- if (newmem != 0) {
- memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
- free(oldmem);
- }
- }
- UNLOCK;
- return newmem;
+ size_t offset = oldp->prev_size;
+ size_t pagemask = av->pagesize - 1;
+ char *cp;
+ unsigned long sum;
+
+ /* Note the extra (sizeof(size_t)) overhead */
+ newsize = (nb + offset + (sizeof(size_t)) + pagemask) & ~pagemask;
+
+ /* don't need to remap if still within same page */
+ if (oldsize == newsize - offset) {
+ retval = oldmem;
+ goto DONE;
+ }
+
+ cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
+
+ if (cp != (char*)MORECORE_FAILURE) {
+
+ newp = (mchunkptr)(cp + offset);
+ set_head(newp, (newsize - offset)|IS_MMAPPED);
+
+ assert(aligned_OK(chunk2mem(newp)));
+ assert((newp->prev_size == offset));
+
+ /* update statistics */
+ sum = av->mmapped_mem += newsize - oldsize;
+ if (sum > (unsigned long)(av->max_mmapped_mem))
+ av->max_mmapped_mem = sum;
+ sum += av->sbrked_mem;
+ if (sum > (unsigned long)(av->max_total_mem))
+ av->max_total_mem = sum;
+
+ retval = chunk2mem(newp);
+ goto DONE;
+ }
+
+ /* Note the extra (sizeof(size_t)) overhead. */
+ if ((unsigned long)(oldsize) >= (unsigned long)(nb + (sizeof(size_t))))
+ newmem = oldmem; /* do nothing */
+ else {
+ /* Must alloc, copy, free. */
+ newmem = malloc(nb - MALLOC_ALIGN_MASK);
+ if (newmem != 0) {
+ memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
+ free(oldmem);
+ }
+ }
+ retval = newmem;
}
+
+ DONE:
+ __MALLOC_UNLOCK;
+ return retval;
}
diff --git a/libc/stdlib/random.c b/libc/stdlib/random.c
index b0a00e1..1bd63bc 100644
--- a/libc/stdlib/random.c
+++ b/libc/stdlib/random.c
@@ -27,16 +27,14 @@
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
+
/* POSIX.1c requires that there is mutual exclusion for the `rand' and
`srand' functions to prevent concurrent calls from modifying common
data. */
-static pthread_mutex_t lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
-#else
-#define __pthread_mutex_lock(x)
-#define __pthread_mutex_unlock(x)
-#endif
+
+#include <bits/uClibc_mutex.h>
+
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
/* An improved random number generation package. In addition to the standard
rand()/srand() like interface, this package also has a special state info
@@ -184,9 +182,9 @@ static struct random_data unsafe_state =
for default usage relies on values produced by this routine. */
void srandom (unsigned int x)
{
- __pthread_mutex_lock(&lock);
+ __UCLIBC_MUTEX_LOCK(mylock);
srandom_r (x, &unsafe_state);
- __pthread_mutex_unlock(&lock);
+ __UCLIBC_MUTEX_UNLOCK(mylock);
}
weak_alias (srandom, srand)
@@ -205,10 +203,10 @@ char * initstate (unsigned int seed, cha
{
int32_t *ostate;
- __pthread_mutex_lock(&lock);
+ __UCLIBC_MUTEX_LOCK(mylock);
ostate = &unsafe_state.state[-1];
initstate_r (seed, arg_state, n, &unsafe_state);
- __pthread_mutex_unlock(&lock);
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return (char *) ostate;
}
@@ -224,11 +222,11 @@ char * setstate (char *arg_state)
{
int32_t *ostate;
- __pthread_mutex_lock(&lock);
+ __UCLIBC_MUTEX_LOCK(mylock);
ostate = &unsafe_state.state[-1];
if (setstate_r (arg_state, &unsafe_state) < 0)
ostate = NULL;
- __pthread_mutex_unlock(&lock);
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return (char *) ostate;
}
@@ -247,9 +245,9 @@ long int random ()
{
int32_t retval;
- __pthread_mutex_lock(&lock);
+ __UCLIBC_MUTEX_LOCK(mylock);
random_r (&unsafe_state, &retval);
- __pthread_mutex_unlock(&lock);
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return retval;
}
diff --git a/libc/stdlib/setenv.c b/libc/stdlib/setenv.c
index d0cfe52..2d899cc 100644
--- a/libc/stdlib/setenv.c
+++ b/libc/stdlib/setenv.c
@@ -17,7 +17,7 @@
02111-1307 USA.
modified for uClibc by Erik Andersen <andersen@codepoet.org>
- */
+*/
#define _GNU_SOURCE
#include <features.h>
@@ -26,16 +26,9 @@
#include <string.h>
#include <unistd.h>
-#ifdef __UCLIBC_HAS_THREADS__
-#include <pthread.h>
-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
-# define LOCK __pthread_mutex_lock(&mylock)
-# define UNLOCK __pthread_mutex_unlock(&mylock);
-#else
-# define LOCK
-# define UNLOCK
-#endif
+#include <bits/uClibc_mutex.h>
+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
/* If this variable is not a null pointer we allocated the current
environment. */
@@ -49,14 +42,15 @@ static char **last_environ;
to reuse values once generated for a `setenv' call since we can never
free the strings. */
int __add_to_environ (const char *name, const char *value,
- const char *combined, int replace)
+ const char *combined, int replace)
{
register char **ep;
register size_t size;
const size_t namelen = strlen (name);
const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
+ int rv = -1;
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
/* We have to get the pointer now that we have the lock and not earlier
since another thread might have created a new environment. */
@@ -64,72 +58,72 @@ int __add_to_environ (const char *name,
size = 0;
if (ep != NULL) {
- for (; *ep != NULL; ++ep) {
- if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
- break;
- else
- ++size;
- }
+ for (; *ep != NULL; ++ep) {
+ if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
+ break;
+ else
+ ++size;
+ }
}
if (ep == NULL || *ep == NULL) {
- char **new_environ;
+ char **new_environ;
- /* We allocated this space; we can extend it. */
- new_environ = (char **) realloc (last_environ,
- (size + 2) * sizeof (char *));
- if (new_environ == NULL) {
- UNLOCK;
- return -1;
- }
-
- /* If the whole entry is given add it. */
- if (combined != NULL) {
- /* We must not add the string to the search tree since it belongs
- to the user. */
- new_environ[size] = (char *) combined;
- } else {
- /* See whether the value is already known. */
- new_environ[size] = (char *) malloc (namelen + 1 + vallen);
- if (new_environ[size] == NULL) {
- __set_errno (ENOMEM);
- UNLOCK;
- return -1;
- }
-
- memcpy (new_environ[size], name, namelen);
- new_environ[size][namelen] = '=';
- memcpy (&new_environ[size][namelen + 1], value, vallen);
- }
-
- if (__environ != last_environ) {
- memcpy ((char *) new_environ, (char *) __environ,
- size * sizeof (char *));
- }
+ /* We allocated this space; we can extend it. */
+ new_environ = (char **) realloc (last_environ,
+ (size + 2) * sizeof (char *));
+ if (new_environ == NULL) {
+ goto DONE;
+ }
+
+ /* If the whole entry is given add it. */
+ if (combined != NULL) {
+ /* We must not add the string to the search tree since it belongs
+ to the user. */
+ new_environ[size] = (char *) combined;
+ } else {
+ /* See whether the value is already known. */
+ new_environ[size] = (char *) malloc (namelen + 1 + vallen);
+ if (new_environ[size] == NULL) {
+ __set_errno (ENOMEM);
+ goto DONE;
+ }
+
+ memcpy (new_environ[size], name, namelen);
+ new_environ[size][namelen] = '=';
+ memcpy (&new_environ[size][namelen + 1], value, vallen);
+ }
+
+ if (__environ != last_environ) {
+ memcpy ((char *) new_environ, (char *) __environ,
+ size * sizeof (char *));
+ }
- new_environ[size + 1] = NULL;
- last_environ = __environ = new_environ;
+ new_environ[size + 1] = NULL;
+ last_environ = __environ = new_environ;
} else if (replace) {
- char *np;
+ char *np;
- /* Use the user string if given. */
- if (combined != NULL) {
- np = (char *) combined;
- } else {
- np = malloc (namelen + 1 + vallen);
- if (np == NULL) {
- UNLOCK;
- return -1;
- }
- memcpy (np, name, namelen);
- np[namelen] = '=';
- memcpy (&np[namelen + 1], value, vallen);
- }
- *ep = np;
- }
-
- UNLOCK;
- return 0;
+ /* Use the user string if given. */
+ if (combined != NULL) {
+ np = (char *) combined;
+ } else {
+ np = malloc (namelen + 1 + vallen);
+ if (np == NULL) {
+ goto DONE;
+ }
+ memcpy (np, name, namelen);
+ np[namelen] = '=';
+ memcpy (&np[namelen + 1], value, vallen);
+ }
+ *ep = np;
+ }
+
+ rv = 0;
+
+ DONE:
+ __UCLIBC_MUTEX_UNLOCK(mylock);
+ return rv;
}
int setenv (const char *name, const char *value, int replace)
@@ -143,26 +137,26 @@ int unsetenv (const char *name)
char **ep;
if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) {
- __set_errno (EINVAL);
- return -1;
+ __set_errno (EINVAL);
+ return -1;
}
len = strlen (name);
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
ep = __environ;
while (*ep != NULL) {
- if (!strncmp (*ep, name, len) && (*ep)[len] == '=') {
- /* Found it. Remove this pointer by moving later ones back. */
- char **dp = ep;
- do {
- dp[0] = dp[1];
- } while (*dp++);
- /* Continue the loop in case NAME appears again. */
- } else {
- ++ep;
- }
+ if (!strncmp (*ep, name, len) && (*ep)[len] == '=') {
+ /* Found it. Remove this pointer by moving later ones back. */
+ char **dp = ep;
+ do {
+ dp[0] = dp[1];
+ } while (*dp++);
+ /* Continue the loop in case NAME appears again. */
+ } else {
+ ++ep;
+ }
}
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return 0;
}
@@ -171,15 +165,15 @@ int unsetenv (const char *name)
for Fortran 77) requires this function. */
int clearenv (void)
{
- LOCK;
+ __UCLIBC_MUTEX_LOCK(mylock);
if (__environ == last_environ && __environ != NULL) {
- /* We allocated this environment so we can free it. */
- free (__environ);
- last_environ = NULL;
+ /* We allocated this environment so we can free it. */
+ free (__environ);
+ last_environ = NULL;
}
/* Clear the environment pointer removes the whole environment. */
__environ = NULL;
- UNLOCK;
+ __UCLIBC_MUTEX_UNLOCK(mylock);
return 0;
}
@@ -190,10 +184,10 @@ int putenv (char *string)
const char *const name_end = strchr (string, '=');
if (name_end != NULL) {
- char *name = strndup(string, name_end - string);
- result = __add_to_environ (name, NULL, string, 1);
- free(name);
- return(result);
+ char *name = strndup(string, name_end - string);
+ result = __add_to_environ (name, NULL, string, 1);
+ free(name);
+ return(result);
}
unsetenv (string);
return 0;
diff --git a/libc/sysdeps/linux/common/bits/uClibc_stdio.h b/libc/sysdeps/linux/common/bits/uClibc_stdio.h
index 40cd5fe..3c6911e 100644
--- a/libc/sysdeps/linux/common/bits/uClibc_stdio.h
+++ b/libc/sysdeps/linux/common/bits/uClibc_stdio.h
@@ -116,9 +116,7 @@
#endif
/**********************************************************************/
-#ifdef __UCLIBC_HAS_THREADS__
-/* Need this for pthread_mutex_t. */
-#include <bits/pthreadtypes.h>
+#include <bits/uClibc_mutex.h>
/* user_locking
* 0 : do auto locking/unlocking
@@ -132,43 +130,37 @@
* This way, we avoid calling the weak lock/unlock functions.
*/
-#define __STDIO_AUTO_THREADLOCK_VAR int __infunc_user_locking
-
-#define __STDIO_AUTO_THREADLOCK(__stream) \
- if ((__infunc_user_locking = (__stream)->__user_locking) == 0) { \
- __pthread_mutex_lock(&(__stream)->__lock); \
- }
-
-#define __STDIO_AUTO_THREADUNLOCK(__stream) \
- if (__infunc_user_locking == 0) { \
- __pthread_mutex_unlock(&(__stream)->__lock); \
- }
+#define __STDIO_AUTO_THREADLOCK_VAR \
+ __UCLIBC_MUTEX_AUTO_LOCK_VAR(__infunc_user_locking)
-#define __STDIO_SET_USER_LOCKING(__stream) ((__stream)->__user_locking = 1)
+#define __STDIO_AUTO_THREADLOCK(__stream) \
+ __UCLIBC_MUTEX_AUTO_LOCK((__stream)->__lock, __infunc_user_locking, \
+ (__stream)->__user_locking)
-#define __STDIO_ALWAYS_THREADLOCK(__stream) \
- __pthread_mutex_lock(&(__stream)->__lock)
+#define __STDIO_AUTO_THREADUNLOCK(__stream) \
+ __UCLIBC_MUTEX_AUTO_UNLOCK((__stream)->__lock, __infunc_user_locking)
-#define __STDIO_ALWAYS_THREADTRYLOCK(__stream) \
- __pthread_mutex_trylock(&(__stream)->__lock)
+#define __STDIO_ALWAYS_THREADLOCK(__stream) \
+ __UCLIBC_MUTEX_LOCK((__stream)->__lock)
-#define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
- __pthread_mutex_unlock(&(__stream)->__lock)
+#define __STDIO_ALWAYS_THREADUNLOCK(__stream) \
+ __UCLIBC_MUTEX_UNLOCK((__stream)->__lock)
-#else /* __UCLIBC_HAS_THREADS__ */
+#define __STDIO_ALWAYS_THREADLOCK_CANCEL_UNSAFE(__stream) \
+ __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE((__stream)->__lock)
-#define __STDIO_AUTO_THREADLOCK_VAR ((void)0)
+#define __STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(__stream) \
+ __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE((__stream)->__lock)
-#define __STDIO_AUTO_THREADLOCK(__stream) ((void)0)
-#define __STDIO_AUTO_THREADUNLOCK(__stream) ((void)0)
+#define __STDIO_ALWAYS_THREADUNLOCK_CANCEL_UNSAFE(__stream) \
+ __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE((__stream)->__lock)
+#ifdef __UCLIBC_HAS_THREADS__
+#define __STDIO_SET_USER_LOCKING(__stream) ((__stream)->__user_locking = 1)
+#else
#define __STDIO_SET_USER_LOCKING(__stream) ((void)0)
+#endif
-#define __STDIO_ALWAYS_THREADLOCK(__stream) ((void)0)
-#define __STDIO_ALWAYS_THREADTRYLOCK(__stream) (0) /* Always succeed. */
-#define __STDIO_ALWAYS_THREADUNLOCK(__stream) ((void)0)
-
-#endif /* __UCLIBC_HAS_THREADS__ */
/**********************************************************************/
#define __STDIO_IOFBF 0 /* Fully buffered. */
@@ -283,7 +275,7 @@ struct __STDIO_FILE_STRUCT {
#endif
#ifdef __UCLIBC_HAS_THREADS__
int __user_locking;
- pthread_mutex_t __lock;
+ __UCLIBC_MUTEX(__lock);
#endif
/* Everything after this is unimplemented... and may be trashed. */
#if __STDIO_BUILTIN_BUF_SIZE > 0
@@ -358,10 +350,14 @@ extern void _stdio_term(void);
extern struct __STDIO_FILE_STRUCT *_stdio_openlist;
#ifdef __UCLIBC_HAS_THREADS__
-extern pthread_mutex_t _stdio_openlist_lock;
-extern int _stdio_openlist_delflag;
+__UCLIBC_MUTEX_EXTERN(_stdio_openlist_add_lock);
+#ifdef __STDIO_BUFFERS
+__UCLIBC_MUTEX_EXTERN(_stdio_openlist_del_lock);
+extern volatile int _stdio_openlist_use_count; /* _stdio_openlist_del_lock */
+extern int _stdio_openlist_del_count; /* _stdio_openlist_del_lock */
+#endif
extern int _stdio_user_locking;
-extern void __stdio_init_mutex(pthread_mutex_t *m);
+extern void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m);
#endif
#endif
diff --git a/libc/sysdeps/linux/common/getdents.c b/libc/sysdeps/linux/common/getdents.c
index ab6a276..23463e5 100644
--- a/libc/sysdeps/linux/common/getdents.c
+++ b/libc/sysdeps/linux/common/getdents.c
@@ -30,8 +30,6 @@
#include <sys/syscall.h>
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-
struct kernel_dirent
{
long d_ino;
diff --git a/libc/sysdeps/linux/common/sigprocmask.c b/libc/sysdeps/linux/common/sigprocmask.c
index 70ff366..565318d 100644
--- a/libc/sysdeps/linux/common/sigprocmask.c
+++ b/libc/sysdeps/linux/common/sigprocmask.c
@@ -23,6 +23,8 @@ int sigprocmask(int how, const sigset_t
if (set &&
#if (SIG_BLOCK == 0) && (SIG_UNBLOCK == 1) && (SIG_SETMASK == 2)
(((unsigned int) how) > 2)
+#elif (SIG_BLOCK == 1) && (SIG_UNBLOCK == 2) && (SIG_SETMASK == 3)
+ (((unsigned int)(how-1)) > 2)
#else
#warning "compile time assumption violated.. slow path..."
((how != SIG_BLOCK) && (how != SIG_UNBLOCK)
@@ -48,6 +50,8 @@ int sigprocmask(int how, const sigset_t
if (set &&
#if (SIG_BLOCK == 0) && (SIG_UNBLOCK == 1) && (SIG_SETMASK == 2)
(((unsigned int) how) > 2)
+#elif (SIG_BLOCK == 1) && (SIG_UNBLOCK == 2) && (SIG_SETMASK == 3)
+ (((unsigned int)(how-1)) > 2)
#else
#warning "compile time assumption violated.. slow path..."
((how != SIG_BLOCK) && (how != SIG_UNBLOCK)
diff --git a/libc/sysdeps/linux/mips/bits/kernel_sigaction.h b/libc/sysdeps/linux/mips/bits/kernel_sigaction.h
index b6f52cc..317e5b3 100644
--- a/libc/sysdeps/linux/mips/bits/kernel_sigaction.h
+++ b/libc/sysdeps/linux/mips/bits/kernel_sigaction.h
@@ -38,3 +38,6 @@ struct kernel_sigaction {
void (*sa_restorer)(void);
int s_resv[1]; /* reserved */
};
+
+extern int __syscall_rt_sigaction (int, const struct kernel_sigaction *__unbounded,
+ struct kernel_sigaction *__unbounded, size_t);
diff --git a/libc/sysdeps/linux/mips/pipe.S b/libc/sysdeps/linux/mips/pipe.S
index c3afae5..cd88074 100644
--- a/libc/sysdeps/linux/mips/pipe.S
+++ b/libc/sysdeps/linux/mips/pipe.S
@@ -7,25 +7,36 @@
#include <asm/unistd.h>
#include <asm/regdef.h>
- .globl pipe
- .ent pipe, 0
+ .globl pipe
+ .ent pipe, 0
pipe:
- addiu sp,sp,-24
- sw a0,16(sp)
- li v0,__NR_pipe
- syscall
- beqz a3, 1f
- la t3, errno
- sw v0, (t3)
- li v0, -1
- b 2f
+ .frame sp, 24, sp
+#ifdef __PIC__
+ .set noreorder
+ .cpload $25
+ .set reorder
+ addiu sp,sp,-24
+ .cprestore 16
+#else
+ addiu sp,sp,-24
+#endif
+ sw a0,16(sp)
+ li v0,__NR_pipe
+ syscall
+ beqz a3, 1f
+#ifdef __PIC__
+ la t0, __syscall_error
+ jr t9
+#else
+ j __syscall_error
+#endif
1:
- lw a0, 16(sp)
- sw v0, 0(a0)
- sw v1, 4(a0)
- li v0, 0
+ lw a0, 16(sp)
+ sw v0, 0(a0)
+ sw v1, 4(a0)
+ li v0, 0
2:
- addiu sp,sp,24
- j ra
- .end pipe
- .size pipe,.-pipe
+ addiu sp,sp,24
+ j ra
+ .end pipe
+ .size pipe,.-pipe
diff --git a/libcrypt/des.c b/libcrypt/des.c
index 3b49a7a..f7a6be1 100644
--- a/libcrypt/des.c
+++ b/libcrypt/des.c
@@ -504,7 +504,7 @@ do_des( u_int32_t l_in, u_int32_t r_in,
kl = kl1;
kr = kr1;
round = 16;
- while (round--) {
+ do {
/*
* Expand R to 48 bits (simulate the E-box).
*/
@@ -540,7 +540,7 @@ do_des( u_int32_t l_in, u_int32_t r_in,
f ^= l;
l = r;
r = f;
- }
+ } while (--round);
r = l;
l = f;
}
diff --git a/libpthread/linuxthreads/ptfork.c b/libpthread/linuxthreads/ptfork.c
index eb544f3..cfec2b7 100644
--- a/libpthread/linuxthreads/ptfork.c
+++ b/libpthread/linuxthreads/ptfork.c
@@ -26,6 +26,15 @@
#include "pthread.h"
#include "internals.h"
+#warning hack alert... should be sufficent for system(), but what about other libc mutexes?
+#include <bits/uClibc_mutex.h>
+
+__UCLIBC_MUTEX_EXTERN(__malloc_lock);
+
+#define __MALLOC_LOCK __UCLIBC_MUTEX_LOCK(__malloc_lock)
+#define __MALLOC_UNLOCK __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
+#warning hack alert block end
+
struct handler_list {
void (*handler)(void);
struct handler_list * next;
@@ -91,9 +100,18 @@ pid_t __fork(void)
parent = pthread_atfork_parent;
pthread_mutex_unlock(&pthread_atfork_lock);
pthread_call_handlers(prepare);
+
+#warning hack alert
+ __MALLOC_LOCK;
+
pid = __libc_fork();
+
+#warning hack alert
+ __MALLOC_UNLOCK;
+
if (pid == 0) {
__pthread_reset_main_thread();
+#warning need to reconsider __fresetlockfiles!
__fresetlockfiles();
pthread_call_handlers(child);
} else {
diff -urN -x .git uClibc-0.9.28/libc/sysdeps/linux/common/bits/uClibc_mutex.h uClibc-mjn3/libc/sysdeps/linux/common/bits/uClibc_mutex.h
--- uClibc-0.9.28/libc/sysdeps/linux/common/bits/uClibc_mutex.h 1969-12-31 17:00:00.000000000 -0700
+++ uClibc-mjn3/libc/sysdeps/linux/common/bits/uClibc_mutex.h 2006-03-08 11:21:58.000000000 -0700
@@ -0,0 +1,87 @@
+/* Copyright (C) 2006 Manuel Novoa III <mjn3@codepoet.org>
+ *
+ * GNU Library General Public License (LGPL) version 2 or later.
+ *
+ * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
+ */
+
+#ifndef _UCLIBC_MUTEX_H
+#define _UCLIBC_MUTEX_H
+
+#include <features.h>
+
+#ifdef __UCLIBC_HAS_THREADS__
+
+#include <pthread.h>
+
+#define __UCLIBC_MUTEX_TYPE pthread_mutex_t
+
+#define __UCLIBC_MUTEX(M) pthread_mutex_t M
+#define __UCLIBC_MUTEX_INIT(M,I) pthread_mutex_t M = I
+#define __UCLIBC_MUTEX_STATIC(M,I) static pthread_mutex_t M = I
+#define __UCLIBC_MUTEX_EXTERN(M) extern pthread_mutex_t M
+
+#define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) \
+ __pthread_mutex_lock(&(M))
+
+#define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) \
+ __pthread_mutex_unlock(&(M))
+
+#define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) \
+ __pthread_mutex_trylock(&(M))
+
+#define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) \
+ do { \
+ struct _pthread_cleanup_buffer __infunc_pthread_cleanup_buffer; \
+ if (C) { \
+ _pthread_cleanup_push_defer(&__infunc_pthread_cleanup_buffer, \
+ __pthread_mutex_unlock, \
+ &(M)); \
+ __pthread_mutex_lock(&(M)); \
+ } \
+ ((void)0)
+
+#define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) \
+ if (C) { \
+ _pthread_cleanup_pop_restore(&__infunc_pthread_cleanup_buffer,1);\
+ } \
+ } while (0)
+
+#define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) int A
+
+#define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) \
+ __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,((A=(V)) == 0))
+
+#define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) \
+ __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,(A == 0))
+
+#define __UCLIBC_MUTEX_LOCK(M) \
+ __UCLIBC_MUTEX_CONDITIONAL_LOCK(M, 1)
+
+#define __UCLIBC_MUTEX_UNLOCK(M) \
+ __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M, 1)
+
+#else
+
+#define __UCLIBC_MUTEX(M) void *__UCLIBC_MUTEX_DUMMY_ ## M
+#define __UCLIBC_MUTEX_INIT(M,I) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
+#define __UCLIBC_MUTEX_STATIC(M) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
+#define __UCLIBC_MUTEX_EXTERN(M) extern void *__UCLIBC_MUTEX_DUMMY_ ## M
+
+#define __UCLIBC_MUTEX_LOCK_CANCEL_UNSAFE(M) ((void)0)
+#define __UCLIBC_MUTEX_UNLOCK_CANCEL_UNSAFE(M) ((void)0)
+#define __UCLIBC_MUTEX_TRYLOCK_CANCEL_UNSAFE(M) (0) /* Always succeed? */
+
+#define __UCLIBC_MUTEX_CONDITIONAL_LOCK(M,C) ((void)0)
+#define __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(M,C) ((void)0)
+
+#define __UCLIBC_MUTEX_AUTO_LOCK_VAR(A) ((void)0)
+#define __UCLIBC_MUTEX_AUTO_LOCK(M,A,V) ((void)0)
+#define __UCLIBC_MUTEX_AUTO_UNLOCK(M,A) ((void)0)
+
+#define __UCLIBC_MUTEX_LOCK(M) ((void)0)
+#define __UCLIBC_MUTEX_UNLOCK(M) ((void)0)
+
+#endif
+
+#endif /* _UCLIBC_MUTEX_H */
diff -urN -x .git uClibc-0.9.28/libc/sysdeps/linux/mips/pipe.c uClibc-mjn3/libc/sysdeps/linux/mips/pipe.c
--- uClibc-0.9.28/libc/sysdeps/linux/mips/pipe.c 2005-08-17 16:49:44.000000000 -0600
+++ uClibc-mjn3/libc/sysdeps/linux/mips/pipe.c 1969-12-31 17:00:00.000000000 -0700
@@ -1,23 +0,0 @@
-/* pipe system call for Linux/MIPS */
-
-/*see uClibc's sh/pipe.c and glibc-2.2.4's mips/pipe.S */
-
-#include <errno.h>
-#include <unistd.h>
-#include <syscall.h>
-
-int pipe(int *fd)
-{
- register long int res __asm__ ("$2"); // v0
- register long int res2 __asm__ ("$3"); // v1
-
- asm ("move\t$4,%2\n\t" // $4 = a0
- "syscall" /* Perform the system call. */
- : "=r" (res)
- : "0" (__NR_pipe), "r" (fd)
- : "$4", "$7");
-
- fd[0] = res;
- fd[1] = res2;
- return(0);
-}
|