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
|
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
/*
* MT safe
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include "garray.h"
#include "galloca.h"
#include "gbytes.h"
#include "ghash.h"
#include "gslice.h"
#include "gmem.h"
#include "gtestutils.h"
#include "gthread.h"
#include "gmessages.h"
#include "gqsort.h"
#include "grefcount.h"
#include "gutilsprivate.h"
#define MIN_ARRAY_SIZE 16
typedef struct _GRealArray GRealArray;
/**
* GArray: (copy-func g_array_ref) (free-func g_array_unref)
* @data: a pointer to the element data. The data may be moved as
* elements are added to the `GArray`.
* @len: the number of elements in the `GArray` not including the
* possible terminating zero element
*
* Contains the public fields of a `GArray`.
*/
struct _GRealArray
{
guint8 *data;
guint len;
guint elt_capacity;
guint elt_size;
guint zero_terminated : 1;
guint clear : 1;
gatomicrefcount ref_count;
GDestroyNotify clear_func;
};
/**
* g_array_index:
* @a: an array
* @t: the type of the elements
* @i: the index of the element to return
*
* Returns the element of a `GArray` at the given index. The return
* value is cast to the given type. This is the main way to read or write an
* element in a `GArray`.
*
* Writing an element is typically done by reference, as in the following
* example. This example gets a pointer to an element in a `GArray`, and then
* writes to a field in it:
* ```c
* EDayViewEvent *event;
* // This gets a pointer to the 4th element in the array of
* // EDayViewEvent structs.
* event = &g_array_index (events, EDayViewEvent, 3);
* event->start_time = g_get_current_time ();
* ```
*
* This example reads from and writes to an array of integers:
* ```c
* g_autoptr(GArray) int_array = g_array_new (FALSE, FALSE, sizeof (guint));
* for (guint i = 0; i < 10; i++)
* g_array_append_val (int_array, i);
*
* guint *my_int = &g_array_index (int_array, guint, 1);
* g_print ("Int at index 1 is %u; decrementing it\n", *my_int);
* *my_int = *my_int - 1;
* ```
*
* Returns: (transfer none): The element of the `GArray` at the index given by @i
*/
#define g_array_elt_len(array,i) ((gsize)(array)->elt_size * (i))
#define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
#define g_array_elt_zero(array, pos, len) \
(memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
#define g_array_zero_terminate(array) G_STMT_START{ \
if ((array)->zero_terminated) \
g_array_elt_zero ((array), (array)->len, 1); \
}G_STMT_END
static void g_array_maybe_expand (GRealArray *array,
guint len);
/**
* g_array_new:
* @zero_terminated: if true, the array should have an extra element at
* the end which is set to 0
* @clear_: if true, `GArray` elements should be automatically cleared
* to 0 when they are allocated
* @element_size: the size of each element in bytes
*
* Creates a new `GArray` with a reference count of 1.
*
* Returns: (transfer full): The new `GArray`
*/
GArray*
g_array_new (gboolean zero_terminated,
gboolean clear,
guint elt_size)
{
g_return_val_if_fail (elt_size > 0, NULL);
#if (UINT_WIDTH / 8) >= GLIB_SIZEOF_SIZE_T
g_return_val_if_fail (elt_size <= G_MAXSIZE / 2 - 1, NULL);
#endif
return g_array_sized_new (zero_terminated, clear, elt_size, 0);
}
/**
* g_array_new_take: (skip)
* @data: (array length=len) (transfer full) (nullable): an array of
* elements of @element_size
* @len: the number of elements in @data
* @clear: if true, `GArray` elements should be automatically cleared
* to 0 when they are allocated
* @element_size: the size of each element in bytes
*
* Creates a new `GArray` with @data as array data, @len as length and a
* reference count of 1.
*
* This avoids having to copy the data manually, when it can just be
* inherited.
* After this call, @data belongs to the `GArray` and may no longer be
* modified by the caller. The memory of @data has to be dynamically
* allocated and will eventually be freed with [func@GLib.free].
*
* In case the elements need to be cleared when the array is freed, use
* [func@GLib.Array.set_clear_func] to set a [callback@GLib.DestroyNotify]
* function to perform such task.
*
* Do not use it if @len or @element_size are greater than
* [`G_MAXUINT`](types.html#guint). `GArray` stores the length of its data in
* `guint`, which may be shorter than `gsize`.
*
* Returns: (transfer full): The new #GArray
*
* Since: 2.76
*/
GArray *
g_array_new_take (gpointer data,
gsize len,
gboolean clear,
gsize element_size)
{
GRealArray *rarray;
GArray *array;
g_return_val_if_fail (data != NULL || len == 0, NULL);
g_return_val_if_fail (len <= G_MAXUINT, NULL);
g_return_val_if_fail (element_size > 0 && element_size <= G_MAXUINT, NULL);
array = g_array_sized_new (FALSE, clear, element_size, 0);
rarray = (GRealArray *) array;
rarray->data = (guint8 *) g_steal_pointer (&data);
rarray->len = len;
rarray->elt_capacity = len;
return array;
}
/**
* g_array_new_take_zero_terminated: (skip)
* @data: (array zero-terminated=1) (transfer full) (nullable): an array
* of elements of @element_size, `NULL` terminated
* @clear: if true, `GArray` elements should be automatically cleared
* to 0 when they are allocated
* @element_size: the size of each element in bytes
*
* Creates a new `GArray` with @data as array data, computing the length of it
* and setting the reference count to 1.
*
* This avoids having to copy the data manually, when it can just be
* inherited.
* After this call, @data belongs to the `GArray` and may no longer be
* modified by the caller. The memory of @data has to be dynamically
* allocated and will eventually be freed with [func@GLib.free].
*
* The length is calculated by iterating through @data until the first `NULL`
* element is found.
*
* In case the elements need to be cleared when the array is freed, use
* [func@GLib.Array.set_clear_func] to set a [callback@GLib.DestroyNotify]
* function to perform such task.
*
* Do not use it if @data length or @element_size are greater than
* [`G_MAXUINT`](types.html#guint). `GArray` stores the length of its data in
* `guint`, which may be shorter than `gsize`.
*
* Returns: (transfer full): The new `GArray`
*
* Since: 2.76
*/
GArray *
g_array_new_take_zero_terminated (gpointer data,
gboolean clear,
gsize element_size)
{
GRealArray *rarray;
GArray *array;
gsize len = 0;
g_return_val_if_fail (element_size > 0 && element_size <= G_MAXUINT, NULL);
if (data != NULL)
{
guint8 *array_data = data;
for (gsize i = 0; ; ++i)
{
const guint8 *element_start = array_data + (i * element_size);
if (*element_start == 0 &&
memcmp (element_start, element_start + 1, element_size - 1) == 0)
break;
len += 1;
}
}
g_return_val_if_fail (len < G_MAXUINT, NULL);
array = g_array_new_take (data, len, clear, element_size);
rarray = (GRealArray *) array;
rarray->zero_terminated = TRUE;
if (G_LIKELY (rarray->data != NULL))
rarray->elt_capacity = len + 1;
return array;
}
/**
* g_array_steal:
* @array: an array
* @len: (optional) (out): a pointer to retrieve the number of
* elements of the original array
*
* Frees the data in the array and resets the size to zero, while
* the underlying array is preserved for use elsewhere and returned
* to the caller.
*
* Note that if the array was created with the @zero_terminate
* property set to true, this may still return `NULL` if the length
* of the array was zero and data was not yet allocated.
*
* If array elements contain dynamically-allocated memory,
* the array elements should also be freed by the caller.
*
* A short example of use:
* ```c
* ...
* gpointer data;
* gsize data_len;
* data = g_array_steal (some_array, &data_len);
* ...
* ```
*
* Returns: (transfer full): The allocated element data
*
* Since: 2.64
*/
gpointer
g_array_steal (GArray *array,
gsize *len)
{
GRealArray *rarray;
gpointer segment;
g_return_val_if_fail (array != NULL, NULL);
rarray = (GRealArray *) array;
segment = (gpointer) rarray->data;
if (len != NULL)
*len = rarray->len;
rarray->data = NULL;
rarray->len = 0;
rarray->elt_capacity = 0;
return segment;
}
/**
* g_array_sized_new:
* @zero_terminated: if true, the array should have an extra element at
* the end with all bits cleared
* @clear_: if true, all bits in the array should be cleared to 0 on
* allocation
* @element_size: the size of each element in the array
* @reserved_size: the number of elements preallocated
*
* Creates a new `GArray` with @reserved_size elements preallocated and
* a reference count of 1. This avoids frequent reallocation, if you
* are going to add many elements to the array. Note however that the
* size of the array is still 0.
*
* Returns: (transfer full): The new `GArray`
*/
GArray*
g_array_sized_new (gboolean zero_terminated,
gboolean clear,
guint elt_size,
guint reserved_size)
{
GRealArray *array;
g_return_val_if_fail (elt_size > 0, NULL);
#if (UINT_WIDTH / 8) >= GLIB_SIZEOF_SIZE_T
g_return_val_if_fail (elt_size <= G_MAXSIZE / 2 - 1, NULL);
#endif
array = g_slice_new (GRealArray);
array->data = NULL;
array->len = 0;
array->elt_capacity = 0;
array->zero_terminated = (zero_terminated ? 1 : 0);
array->clear = (clear ? 1 : 0);
array->elt_size = elt_size;
array->clear_func = NULL;
g_atomic_ref_count_init (&array->ref_count);
if (array->zero_terminated || reserved_size != 0)
{
g_array_maybe_expand (array, reserved_size);
g_assert (array->data != NULL);
g_array_zero_terminate (array);
}
return (GArray*) array;
}
/**
* g_array_set_clear_func:
* @array: an array
* @clear_func: (nullable): a function to clear an element of @array
*
* Sets a function to clear an element of @array.
*
* The @clear_func will be called when an element in the array
* data segment is removed and when the array is freed and data
* segment is deallocated as well. @clear_func will be passed a
* pointer to the element to clear, rather than the element itself.
*
* Note that in contrast with other uses of [callback@GLib.DestroyNotify]
* functions, @clear_func is expected to clear the contents of
* the array element it is given, but not free the element itself.
*
* ```c
* typedef struct
* {
* gchar *str;
* GObject *obj;
* } ArrayElement;
*
* static void
* array_element_clear (ArrayElement *element)
* {
* g_clear_pointer (&element->str, g_free);
* g_clear_object (&element->obj);
* }
*
* // main code
* GArray *garray = g_array_new (FALSE, FALSE, sizeof (ArrayElement));
* g_array_set_clear_func (garray, (GDestroyNotify) array_element_clear);
* // assign data to the structure
* g_array_free (garray, TRUE);
* ```
*
* Since: 2.32
*/
void
g_array_set_clear_func (GArray *array,
GDestroyNotify clear_func)
{
GRealArray *rarray = (GRealArray *) array;
g_return_if_fail (array != NULL);
rarray->clear_func = clear_func;
}
/**
* g_array_ref:
* @array: an array
*
* Atomically increments the reference count of @array by one.
* This function is thread-safe and may be called from any thread.
*
* Returns: (transfer full): The passed in `GArray`
*
* Since: 2.22
*/
GArray *
g_array_ref (GArray *array)
{
GRealArray *rarray = (GRealArray*) array;
g_return_val_if_fail (array, NULL);
g_atomic_ref_count_inc (&rarray->ref_count);
return array;
}
typedef enum
{
FREE_SEGMENT = 1 << 0,
PRESERVE_WRAPPER = 1 << 1
} ArrayFreeFlags;
static gchar *array_free (GRealArray *, ArrayFreeFlags);
/**
* g_array_unref:
* @array: (transfer full): an array
*
* Atomically decrements the reference count of @array by one. If the
* reference count drops to 0, the effect is the same as calling
* [func@GLib.Array.free] with @free_segment set to true. This function is
* thread-safe and may be called from any thread.
*
* Since: 2.22
*/
void
g_array_unref (GArray *array)
{
GRealArray *rarray = (GRealArray*) array;
g_return_if_fail (array);
if (g_atomic_ref_count_dec (&rarray->ref_count))
array_free (rarray, FREE_SEGMENT);
}
/**
* g_array_get_element_size:
* @array: an array
*
* Gets the size of the elements in @array.
*
* Returns: The size of each element, in bytes
*
* Since: 2.22
*/
guint
g_array_get_element_size (GArray *array)
{
GRealArray *rarray = (GRealArray*) array;
g_return_val_if_fail (array, 0);
return rarray->elt_size;
}
/**
* g_array_free:
* @array: (transfer full): an array
* @free_segment: if true, the actual element data is freed as well
*
* Frees the memory allocated for the `GArray`. If @free_segment is
* true it frees the memory block holding the elements as well. Pass
* false if you want to free the `GArray` wrapper but preserve the
* underlying array for use elsewhere. If the reference count of
* @array is greater than one, the `GArray` wrapper is preserved but
* the size of @array will be set to zero.
*
* If array contents point to dynamically-allocated memory, they should
* be freed separately if @free_segment is true and no @clear_func
* function has been set for @array.
*
* This function is not thread-safe. If using a `GArray` from multiple
* threads, use only the atomic [func@GLib.Array.ref] and
* [func@GLib.Array.unref] functions.
*
* Returns: The allocated element data if @free_segment is false, otherwise
* `NULL`
*/
gchar*
g_array_free (GArray *farray,
gboolean free_segment)
{
GRealArray *array = (GRealArray*) farray;
ArrayFreeFlags flags;
g_return_val_if_fail (array, NULL);
flags = (free_segment ? FREE_SEGMENT : 0);
/* if others are holding a reference, preserve the wrapper but do free/return the data */
if (!g_atomic_ref_count_dec (&array->ref_count))
flags |= PRESERVE_WRAPPER;
return array_free (array, flags);
}
static gchar *
array_free (GRealArray *array,
ArrayFreeFlags flags)
{
gchar *segment;
if (flags & FREE_SEGMENT)
{
if (array->clear_func != NULL)
{
guint i;
for (i = 0; i < array->len; i++)
array->clear_func (g_array_elt_pos (array, i));
}
g_free (array->data);
segment = NULL;
}
else
segment = (gchar*) array->data;
if (flags & PRESERVE_WRAPPER)
{
array->data = NULL;
array->len = 0;
array->elt_capacity = 0;
}
else
{
g_slice_free1 (sizeof (GRealArray), array);
}
return segment;
}
/**
* g_array_append_vals:
* @array: an array
* @data: (nullable): a pointer to the elements to append to the end of the array
* @len: the number of elements to append
*
* Adds @len elements onto the end of the array.
*
* @data may be `NULL` if (and only if) @len is zero. If @len is zero, this
* function is a no-op.
*
* Returns: (transfer none): The `GArray`
*/
/**
* g_array_append_val:
* @a: an array
* @v: the value to append to the #GArray
*
* Adds the value on to the end of the array. The array will grow in
* size automatically if necessary.
*
* `g_array_append_val()` is a macro which uses a reference to the value
* parameter @v. This means that you cannot use it with literal values
* such as `"27"`. You must use variables.
*
* Returns: (transfer none): The `GArray`
*/
GArray*
g_array_append_vals (GArray *farray,
gconstpointer data,
guint len)
{
GRealArray *array = (GRealArray*) farray;
g_return_val_if_fail (array, NULL);
if (len == 0)
return farray;
g_array_maybe_expand (array, len);
memcpy (g_array_elt_pos (array, array->len), data,
g_array_elt_len (array, len));
array->len += len;
g_array_zero_terminate (array);
return farray;
}
/**
* g_array_prepend_vals:
* @array: an array
* @data: (nullable): a pointer to the elements to prepend to the start of the array
* @len: the number of elements to prepend, which may be zero
*
* Adds @len elements onto the start of the array.
*
* @data may be `NULL` if (and only if) @len is zero. If @len is zero, this
* function is a no-op.
*
* This operation is slower than [func@GLib.Array.append_vals] since the
* existing elements in the array have to be moved to make space for
* the new elements.
*
* Returns: (transfer none): The `GArray`
*/
/**
* g_array_prepend_val:
* @a: an array
* @v: the value to prepend to the #GArray
*
* Adds the value on to the start of the array. The array will grow in
* size automatically if necessary.
*
* This operation is slower than [func@GLib.array_append_val] since the
* existing elements in the array have to be moved to make space for
* the new element.
*
* `g_array_prepend_val()` is a macro which uses a reference to the value
* parameter @v. This means that you cannot use it with literal values
* such as `"27"`. You must use variables.
*
* Returns: (transfer none): The `GArray`
*/
GArray*
g_array_prepend_vals (GArray *farray,
gconstpointer data,
guint len)
{
GRealArray *array = (GRealArray*) farray;
g_return_val_if_fail (array, NULL);
if (len == 0)
return farray;
g_array_maybe_expand (array, len);
memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
g_array_elt_len (array, array->len));
memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
array->len += len;
g_array_zero_terminate (array);
return farray;
}
/**
* g_array_insert_vals:
* @array: an array
* @index_: the index to place the elements at
* @data: (nullable): a pointer to the elements to insert
* @len: the number of elements to insert
*
* Inserts @len elements into a `GArray` at the given index.
*
* If @index_ is greater than the array’s current length, the array is expanded.
* The elements between the old end of the array and the newly inserted elements
* will be initialised to zero if the array was configured to clear elements;
* otherwise their values will be undefined.
*
* If @index_ is less than the array’s current length, new entries will be
* inserted into the array, and the existing entries above @index_ will be moved
* upwards.
*
* @data may be `NULL` if (and only if) @len is zero. If @len is zero, this
* function is a no-op.
*
* Returns: The `GArray`
*/
/**
* g_array_insert_val:
* @a: an array
* @i: the index to place the element at
* @v: the value to insert into the array
*
* Inserts an element into an array at the given index.
*
* `g_array_insert_val()` is a macro which uses a reference to the value
* parameter @v. This means that you cannot use it with literal values
* such as `"27"`. You must use variables.
*
* Returns: (transfer none): The `GArray`
*/
GArray*
g_array_insert_vals (GArray *farray,
guint index_,
gconstpointer data,
guint len)
{
GRealArray *array = (GRealArray*) farray;
g_return_val_if_fail (array, NULL);
if (len == 0)
return farray;
/* Is the index off the end of the array, and hence do we need to over-allocate
* and clear some elements? */
if (index_ >= array->len)
{
g_array_maybe_expand (array, index_ - array->len + len);
return g_array_append_vals (g_array_set_size (farray, index_), data, len);
}
g_array_maybe_expand (array, len);
memmove (g_array_elt_pos (array, len + index_),
g_array_elt_pos (array, index_),
g_array_elt_len (array, array->len - index_));
memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
array->len += len;
g_array_zero_terminate (array);
return farray;
}
/**
* g_array_set_size:
* @array: an array
* @length: the new size of the #GArray
*
* Sets the size of the array, expanding it if necessary. If the array
* was created with @clear_ set to true, the new elements are set to 0.
*
* Returns: (transfer none): The `GArray`
*/
GArray*
g_array_set_size (GArray *farray,
guint length)
{
GRealArray *array = (GRealArray*) farray;
g_return_val_if_fail (array, NULL);
if (length > array->len)
{
g_array_maybe_expand (array, length - array->len);
if (array->clear)
g_array_elt_zero (array, array->len, length - array->len);
}
else if (length < array->len)
g_array_remove_range (farray, length, array->len - length);
array->len = length;
if (G_LIKELY (array->data != NULL))
g_array_zero_terminate (array);
return farray;
}
/**
* g_array_remove_index:
* @array: an array
* @index_: the index of the element to remove
*
* Removes the element at the given index from a `GArray`. The following
* elements are moved down one place.
*
* Returns: (transfer none): The `GArray`
*/
GArray*
g_array_remove_index (GArray *farray,
guint index_)
{
GRealArray* array = (GRealArray*) farray;
g_return_val_if_fail (array, NULL);
g_return_val_if_fail (index_ < array->len, NULL);
if (array->clear_func != NULL)
array->clear_func (g_array_elt_pos (array, index_));
if (index_ != array->len - 1)
memmove (g_array_elt_pos (array, index_),
g_array_elt_pos (array, index_ + 1),
g_array_elt_len (array, array->len - index_ - 1));
array->len -= 1;
if (G_UNLIKELY (g_mem_gc_friendly))
g_array_elt_zero (array, array->len, 1);
else
g_array_zero_terminate (array);
return farray;
}
/**
* g_array_remove_index_fast:
* @array: an array
* @index_: the index of the element to remove
*
* Removes the element at the given index from a `GArray`. The last
* element in the array is used to fill in the space, so this function
* does not preserve the order of the `GArray`. But it is faster than
* [func@GLib.Array.remove_index].
*
* Returns: (transfer none): The `GArray`
*/
GArray*
g_array_remove_index_fast (GArray *farray,
guint index_)
{
GRealArray* array = (GRealArray*) farray;
g_return_val_if_fail (array, NULL);
g_return_val_if_fail (index_ < array->len, NULL);
if (array->clear_func != NULL)
array->clear_func (g_array_elt_pos (array, index_));
if (index_ != array->len - 1)
memcpy (g_array_elt_pos (array, index_),
g_array_elt_pos (array, array->len - 1),
g_array_elt_len (array, 1));
array->len -= 1;
if (G_UNLIKELY (g_mem_gc_friendly))
g_array_elt_zero (array, array->len, 1);
else
g_array_zero_terminate (array);
return farray;
}
/**
* g_array_remove_range:
* @array: an array
* @index_: the index of the first element to remove
* @length: the number of elements to remove
*
* Removes the given number of elements starting at the given index
* from a `GArray`. The following elements are moved to close the gap.
*
* Returns: (transfer none): The `GArray`
*
* Since: 2.4
*/
GArray*
g_array_remove_range (GArray *farray,
guint index_,
guint length)
{
GRealArray *array = (GRealArray*) farray;
g_return_val_if_fail (array, NULL);
g_return_val_if_fail (index_ <= array->len, NULL);
g_return_val_if_fail (index_ <= G_MAXUINT - length, NULL);
g_return_val_if_fail (index_ + length <= array->len, NULL);
if (length == 0)
return farray;
if (array->clear_func != NULL)
{
guint i;
for (i = 0; i < length; i++)
array->clear_func (g_array_elt_pos (array, index_ + i));
}
if (index_ + length != array->len)
memmove (g_array_elt_pos (array, index_),
g_array_elt_pos (array, index_ + length),
g_array_elt_len (array, array->len - (index_ + length)));
array->len -= length;
if (G_UNLIKELY (g_mem_gc_friendly))
g_array_elt_zero (array, array->len, length);
else
g_array_zero_terminate (array);
return farray;
}
/**
* g_array_sort:
* @array: an array
* @compare_func: (scope call): a comparison function
*
* Sorts a `GArray` using @compare_func which should be a `qsort()`-style
* comparison function (returns less than zero for first arg is less
* than second arg, zero for equal, greater zero if first arg is
* greater than second arg).
*
* This is guaranteed to be a stable sort since version 2.32.
*/
void
g_array_sort (GArray *farray,
GCompareFunc compare_func)
{
GRealArray *array = (GRealArray*) farray;
g_return_if_fail (array != NULL);
/* Don't use qsort as we want a guaranteed stable sort */
if (array->len > 0)
g_sort_array (array->data,
array->len,
array->elt_size,
(GCompareDataFunc) compare_func,
NULL);
}
/**
* g_array_sort_with_data:
* @array: an array
* @compare_func: (scope call): a comparison function
* @user_data: the data to pass to @compare_func
*
* Like [func@GLib.Array.sort], but the comparison function receives an extra
* user data argument.
*
* This is guaranteed to be a stable sort since version 2.32.
*
* There used to be a comment here about making the sort stable by
* using the addresses of the elements in the comparison function.
* This did not actually work, so any such code should be removed.
*/
void
g_array_sort_with_data (GArray *farray,
GCompareDataFunc compare_func,
gpointer user_data)
{
GRealArray *array = (GRealArray*) farray;
g_return_if_fail (array != NULL);
if (array->len > 0)
g_sort_array (array->data,
array->len,
array->elt_size,
compare_func,
user_data);
}
/**
* g_array_binary_search:
* @array: an array
* @target: a pointer to the item to look up
* @compare_func: (scope call): a comparison function to locate @target
* @out_match_index: (optional) (out): the return location
* for the index of the element, if found
*
* Checks whether @target exists in @array by performing a binary
* search based on the given comparison function @compare_func which
* gets pointers to items as arguments. If the element is found, true
* is returned and the element’s index is returned in @out_match_index
* (if non-`NULL`). Otherwise, false is returned and @out_match_index
* is undefined. This search is using a binary search, so the @array must
* absolutely be sorted to return a correct result (if not, the function may
* produce false-negative).
*
* This example defines a comparison function and searches an element in a
* `GArray`:
* ```c
* static gint
* cmpint (gconstpointer a, gconstpointer b)
* {
* const gint *_a = a;
* const gint *_b = b;
*
* return *_a - *_b;
* }
* ...
* gint i = 424242;
* guint matched_index;
* gboolean result = g_array_binary_search (garray, &i, cmpint, &matched_index);
* ...
* ```
*
* Returns: true if @target is one of the elements of @array; false otherwise
*
* Since: 2.62
*/
gboolean
g_array_binary_search (GArray *array,
gconstpointer target,
GCompareFunc compare_func,
guint *out_match_index)
{
gboolean result = FALSE;
GRealArray *_array = (GRealArray *) array;
guint left, middle = 0, right;
gint val;
g_return_val_if_fail (_array != NULL, FALSE);
g_return_val_if_fail (compare_func != NULL, FALSE);
if (G_LIKELY(_array->len))
{
left = 0;
right = _array->len - 1;
while (left <= right)
{
middle = left + (right - left) / 2;
val = compare_func (g_array_elt_pos (_array, middle), target);
if (val == 0)
{
result = TRUE;
break;
}
else if (val < 0)
left = middle + 1;
else if (/* val > 0 && */ middle > 0)
right = middle - 1;
else
break; /* element not found */
}
}
if (result && out_match_index != NULL)
*out_match_index = middle;
return result;
}
static void
g_array_maybe_expand (GRealArray *array,
guint len)
{
guint max_len, want_len;
/* The maximum array length is derived from following constraints:
* - The number of bytes must fit into a gsize / 2.
* - The number of elements must fit into guint.
* - zero terminated arrays must leave space for the terminating element
*/
max_len = MIN (G_MAXSIZE / 2 / array->elt_size, G_MAXUINT) - array->zero_terminated;
/* Detect potential overflow */
if G_UNLIKELY ((max_len - array->len) < len)
g_error ("adding %u to array would overflow", len);
want_len = array->len + len + array->zero_terminated;
if (want_len > array->elt_capacity)
{
gsize want_alloc = g_nearest_pow (g_array_elt_len (array, want_len));
g_assert (want_alloc >= g_array_elt_len (array, want_len));
want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
array->data = g_realloc (array->data, want_alloc);
if (G_UNLIKELY (g_mem_gc_friendly))
memset (g_array_elt_pos (array, array->elt_capacity), 0,
g_array_elt_len (array, want_len - array->elt_capacity));
array->elt_capacity = MIN (want_alloc / array->elt_size, G_MAXUINT);
}
}
typedef struct _GRealPtrArray GRealPtrArray;
/**
* GPtrArray: (copy-func g_ptr_array_ref) (free-func g_ptr_array_unref)
* @pdata: a pointer to the array of pointers, which may be moved when the
* array grows
* @len: the number of pointers in the array
*
* Contains the public fields of a `GPtrArray`.
*/
struct _GRealPtrArray
{
gpointer *pdata;
guint len;
guint alloc;
gatomicrefcount ref_count;
guint8 null_terminated : 1; /* always either 0 or 1, so it can be added to array lengths */
GDestroyNotify element_free_func;
};
/**
* g_ptr_array_index:
* @array: a pointer array
* @index_: the index of the pointer to return
*
* Returns the pointer at the given index of the pointer array.
*
* This does not perform bounds checking on the given @index_,
* so you are responsible for checking it against the array length.
*
* Returns: (transfer none): The pointer at the given index
*/
static void g_ptr_array_maybe_expand (GRealPtrArray *array,
guint len);
static void
ptr_array_maybe_null_terminate (GRealPtrArray *rarray)
{
if (G_UNLIKELY (rarray->null_terminated))
rarray->pdata[rarray->len] = NULL;
}
static GPtrArray *
ptr_array_new (guint reserved_size,
GDestroyNotify element_free_func,
gboolean null_terminated)
{
GRealPtrArray *array;
array = g_slice_new (GRealPtrArray);
array->pdata = NULL;
array->len = 0;
array->alloc = 0;
array->null_terminated = null_terminated ? 1 : 0;
array->element_free_func = element_free_func;
g_atomic_ref_count_init (&array->ref_count);
if (reserved_size != 0)
{
g_ptr_array_maybe_expand (array, reserved_size);
g_assert (array->pdata != NULL);
if (null_terminated)
{
/* don't use ptr_array_maybe_null_terminate(). It helps the compiler
* to see when @null_terminated is false and thereby inline
* ptr_array_new() and possibly remove the code entirely. */
array->pdata[0] = NULL;
}
}
return (GPtrArray *) array;
}
/**
* g_ptr_array_new:
*
* Creates a new `GPtrArray` with a reference count of 1.
*
* Returns: (transfer full): The new `GPtrArray`
*/
GPtrArray*
g_ptr_array_new (void)
{
return ptr_array_new (0, NULL, FALSE);
}
/**
* g_ptr_array_new_take: (skip)
* @data: (array length=len) (transfer full) (nullable): an array of pointers
* @len: the number of pointers in @data
* @element_free_func: (nullable): a function to free elements on @array
* destruction
*
* Creates a new `GPtrArray` with @data as pointers, @len as length and a
* reference count of 1.
*
* This avoids having to copy such data manually.
* After this call, @data belongs to the `GPtrArray` and may no longer be
* modified by the caller. The memory of @data has to be dynamically
* allocated and will eventually be freed with [func@GLib.free].
*
* It also sets @element_free_func for freeing each element when the array is
* destroyed either via [func@GLib.PtrArray.unref], when
* [func@GLib.PtrArray.free] is called with @free_segment set to true or when
* removing elements.
*
* Do not use it if @len is greater than [`G_MAXUINT`](types.html#guint).
* `GPtrArray` stores the length of its data in `guint`, which may be shorter
* than `gsize`.
*
* Returns: (transfer full): The new `GPtrArray`
*
* Since: 2.76
*/
GPtrArray *
g_ptr_array_new_take (gpointer *data,
gsize len,
GDestroyNotify element_free_func)
{
GPtrArray *array;
GRealPtrArray *rarray;
g_return_val_if_fail (data != NULL || len == 0, NULL);
g_return_val_if_fail (len <= G_MAXUINT, NULL);
array = ptr_array_new (0, element_free_func, FALSE);
rarray = (GRealPtrArray *)array;
rarray->pdata = g_steal_pointer (&data);
rarray->len = len;
rarray->alloc = len;
return array;
}
/**
* g_ptr_array_new_take_null_terminated: (skip)
* @data: (array zero-terminated=1) (transfer full) (nullable): an array
* of pointers, `NULL` terminated
* @element_free_func: (nullable): a function to free elements on @array
* destruction
*
* Creates a new `GPtrArray` with @data as pointers, computing the length of it
* and setting the reference count to 1.
*
* This avoids having to copy such data manually.
* After this call, @data belongs to the `GPtrArray` and may no longer be
* modified by the caller. The memory of @data has to be dynamically
* allocated and will eventually be freed with [func@GLib.free].
*
* The length is calculated by iterating through @data until the first `NULL`
* element is found.
*
* It also sets @element_free_func for freeing each element when the array is
* destroyed either via [func@GLib.PtrArray.unref], when
* [func@GLib.PtrArray.free] is called with @free_segment set to true or when
* removing elements.
*
* Do not use it if the @data length is greater than
* [`G_MAXUINT`](types.html#guint). `GPtrArray` stores the length of its data
* in `guint`, which may be shorter than `gsize`.
*
* Returns: (transfer full): The new `GPtrArray`
*
* Since: 2.76
*/
GPtrArray *
g_ptr_array_new_take_null_terminated (gpointer *data,
GDestroyNotify element_free_func)
{
GRealPtrArray *rarray;
GPtrArray *array;
gsize len = 0;
if (data != NULL)
{
for (gsize i = 0; data[i] != NULL; ++i)
len += 1;
}
g_return_val_if_fail (len < G_MAXUINT, NULL);
array = g_ptr_array_new_take (g_steal_pointer (&data), len, element_free_func);
rarray = (GRealPtrArray *) array;
rarray->null_terminated = TRUE;
if (G_LIKELY (rarray->pdata != NULL))
rarray->alloc = len + 1;
return array;
}
static GPtrArray *
ptr_array_new_from_array (gpointer *data,
gsize len,
GCopyFunc copy_func,
gpointer copy_func_user_data,
GDestroyNotify element_free_func,
gboolean null_terminated)
{
GPtrArray *array;
GRealPtrArray *rarray;
g_assert (data != NULL || len == 0);
g_assert (len <= G_MAXUINT - (null_terminated ? 1 : 0));
array = ptr_array_new (len, element_free_func, null_terminated);
rarray = (GRealPtrArray *)array;
if (copy_func != NULL)
{
for (gsize i = 0; i < len; i++)
rarray->pdata[i] = copy_func (data[i], copy_func_user_data);
}
else if (len != 0)
{
memcpy (rarray->pdata, data, len * sizeof (gpointer));
}
if (null_terminated && rarray->pdata != NULL)
rarray->pdata[len] = NULL;
rarray->len = len;
return array;
}
/**
* g_ptr_array_new_from_array: (skip)
* @data: (array length=len) (transfer none) (nullable): an array of pointers
* @len: the number of pointers in @data
* @copy_func: (nullable): a copy function used to copy every element in the
* array
* @copy_func_user_data: the user data passed to @copy_func
* @element_free_func: (nullable): a function to free elements on @array
* destruction
*
* Creates a new `GPtrArray`, copying @len pointers from @data, and setting
* the array’s reference count to 1.
*
* This avoids having to manually add each element one by one.
*
* If @copy_func is provided, then it is used to copy each element before
* adding them to the new array. If it is `NULL` then the pointers are copied
* directly.
*
* It also sets @element_free_func for freeing each element when the array is
* destroyed either via [func@GLib.PtrArray.unref], when
* [func@GLib.PtrArray.free] is called with @free_segment set to true or when
* removing elements.
*
* Do not use it if @len is greater than [`G_MAXUINT`](types.html#guint).
* `GPtrArray` stores the length of its data in `guint`, which may be shorter
* than `gsize`.
*
* Returns: (transfer full): The new `GPtrArray`
*
* Since: 2.76
*/
GPtrArray *
g_ptr_array_new_from_array (gpointer *data,
gsize len,
GCopyFunc copy_func,
gpointer copy_func_user_data,
GDestroyNotify element_free_func)
{
g_return_val_if_fail (data != NULL || len == 0, NULL);
g_return_val_if_fail (len <= G_MAXUINT, NULL);
return ptr_array_new_from_array (
data, len, copy_func, copy_func_user_data, element_free_func, FALSE);
}
/**
* g_ptr_array_new_from_null_terminated_array: (skip)
* @data: (array zero-terminated=1) (transfer none) (nullable): an array of
* pointers, `NULL` terminated
* @copy_func: (nullable): a copy function used to copy every element in the
* array
* @copy_func_user_data: the user data passed to @copy_func
* @element_free_func: (nullable): a function to free elements on @array
* destruction
*
* Creates a new `GPtrArray` copying the pointers from @data after having
* computed the length of it and with a reference count of 1.
* This avoids having to manually add each element one by one.
* If @copy_func is provided, then it is used to copy the data in the new
* array.
* It also sets @element_free_func for freeing each element when the array is
* destroyed either via [func@GLib.PtrArray.unref], when
* [func@GLib.PtrArray.free] is called with @free_segment set to true or when
* removing elements.
*
* Do not use it if the @data has more than [`G_MAXUINT`](types.html#guint)
* elements. `GPtrArray` stores the length of its data in `guint`, which may be
* shorter than `gsize`.
*
* Returns: (transfer full): The new `GPtrArray`
*
* Since: 2.76
*/
GPtrArray *
g_ptr_array_new_from_null_terminated_array (gpointer *data,
GCopyFunc copy_func,
gpointer copy_func_user_data,
GDestroyNotify element_free_func)
{
gsize len = 0;
if (data != NULL)
{
for (gsize i = 0; data[i] != NULL; ++i)
len += 1;
}
g_assert (data != NULL || len == 0);
g_return_val_if_fail (len < G_MAXUINT, NULL);
return ptr_array_new_from_array (
data, len, copy_func, copy_func_user_data, element_free_func, TRUE);
}
/**
* g_ptr_array_steal:
* @array: a pointer array
* @len: (optional) (out): a pointer to retrieve the number of
* elements of the original array
*
* Frees the data in the array and resets the size to zero, while
* the underlying array is preserved for use elsewhere and returned
* to the caller.
*
* Note that if the array is `NULL` terminated this may still return
* `NULL` if the length of the array was zero and pdata was not yet
* allocated.
*
* Even if set, the [callback@GLib.DestroyNotify] function will never be called
* on the current contents of the array and the caller is
* responsible for freeing the array elements.
*
* An example of use:
* ```c
* g_autoptr(GPtrArray) chunk_buffer = g_ptr_array_new_with_free_func (g_bytes_unref);
*
* // Some part of your application appends a number of chunks to the pointer array.
* g_ptr_array_add (chunk_buffer, g_bytes_new_static ("hello", 5));
* g_ptr_array_add (chunk_buffer, g_bytes_new_static ("world", 5));
*
* …
*
* // Periodically, the chunks need to be sent as an array-and-length to some
* // other part of the program.
* GBytes **chunks;
* gsize n_chunks;
*
* chunks = g_ptr_array_steal (chunk_buffer, &n_chunks);
* for (gsize i = 0; i < n_chunks; i++)
* {
* // Do something with each chunk here, and then free them, since
* // g_ptr_array_steal() transfers ownership of all the elements and the
* // array to the caller.
* …
*
* g_bytes_unref (chunks[i]);
* }
*
* g_free (chunks);
*
* // After calling g_ptr_array_steal(), the pointer array can be reused for the
* // next set of chunks.
* g_assert (chunk_buffer->len == 0);
* ```
*
* Returns: (transfer full) (nullable) (array length=len): The allocated element data.
* This may be `NULL`if the array doesn’t have any elements (i.e. if `*len` is zero).
*
* Since: 2.64
*/
gpointer *
g_ptr_array_steal (GPtrArray *array,
gsize *len)
{
GRealPtrArray *rarray;
gpointer *segment;
g_return_val_if_fail (array != NULL, NULL);
rarray = (GRealPtrArray *) array;
segment = (gpointer *) rarray->pdata;
if (len != NULL)
*len = rarray->len;
rarray->pdata = NULL;
rarray->len = 0;
rarray->alloc = 0;
return segment;
}
/**
* g_ptr_array_copy:
* @array: a pointer array to duplicate
* @func: (scope call) (nullable): a copy function used to copy every element in the array
* @user_data: the user data passed to the copy function @func
*
* Makes a full (deep) copy of a `GPtrArray`.
*
* @func, as a [callback@GLib.CopyFunc], takes two arguments, the data to be
* copied
* and a @user_data pointer. On common processor architectures, it’s safe to
* pass `NULL` as @user_data if the copy function takes only one argument. You
* may get compiler warnings from this though if compiling with GCC’s
* `-Wcast-function-type` warning.
*
* If @func is `NULL`, then only the pointers (and not what they are
* pointing to) are copied to the new `GPtrArray`.
*
* The copy of @array will have the same [callback@GLib.DestroyNotify] for its
* elements as
* @array. The copy will also be `NULL` terminated if (and only if) the source
* array is.
*
* Returns: (transfer full): The deep copy of the initial `GPtrArray`
*
* Since: 2.62
**/
GPtrArray *
g_ptr_array_copy (GPtrArray *array,
GCopyFunc func,
gpointer user_data)
{
GRealPtrArray *rarray = (GRealPtrArray *) array;
GPtrArray *new_array;
g_return_val_if_fail (array != NULL, NULL);
new_array = ptr_array_new (0,
rarray->element_free_func,
rarray->null_terminated);
if (rarray->alloc > 0)
{
g_ptr_array_maybe_expand ((GRealPtrArray *) new_array, array->len);
if (array->len > 0)
{
if (func != NULL)
{
guint i;
for (i = 0; i < array->len; i++)
new_array->pdata[i] = func (array->pdata[i], user_data);
}
else
{
memcpy (new_array->pdata, array->pdata,
array->len * sizeof (*array->pdata));
}
new_array->len = array->len;
}
ptr_array_maybe_null_terminate ((GRealPtrArray *) new_array);
}
return new_array;
}
/**
* g_ptr_array_sized_new:
* @reserved_size: the number of pointers preallocated
*
* Creates a new `GPtrArray` with @reserved_size pointers preallocated
* and a reference count of 1. This avoids frequent reallocation, if
* you are going to add many pointers to the array. Note however that
* the size of the array is still 0.
*
* Returns: (transfer full): The new `GPtrArray`
*/
GPtrArray*
g_ptr_array_sized_new (guint reserved_size)
{
return ptr_array_new (reserved_size, NULL, FALSE);
}
/**
* g_array_copy:
* @array: an array
*
* Creates a shallow copy of a #GArray. If the array elements consist of
* pointers to data, the pointers are copied but the actual data is not.
*
* Returns: (transfer container): The copy of @array
*
* Since: 2.62
**/
GArray *
g_array_copy (GArray *array)
{
GRealArray *rarray = (GRealArray *) array;
GRealArray *new_rarray;
g_return_val_if_fail (rarray != NULL, NULL);
new_rarray =
(GRealArray *) g_array_sized_new (rarray->zero_terminated, rarray->clear,
rarray->elt_size, rarray->len);
new_rarray->len = rarray->len;
if (rarray->len > 0)
memcpy (new_rarray->data, rarray->data, g_array_elt_len (rarray, rarray->len));
g_array_zero_terminate (new_rarray);
return (GArray *) new_rarray;
}
/**
* g_ptr_array_new_with_free_func:
* @element_free_func: (nullable): a function to free elements with
* destroy @array
*
* Creates a new `GPtrArray` with a reference count of 1 and use
* @element_free_func for freeing each element when the array is destroyed
* either via [func@GLib.PtrArray.unref], when [func@GLib.PtrArray.free] is
* called with @free_segment set to true or when removing elements.
*
* Returns: (transfer full): The new `GPtrArray`
*
* Since: 2.22
*/
GPtrArray*
g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
{
return ptr_array_new (0, element_free_func, FALSE);
}
/**
* g_ptr_array_new_full:
* @reserved_size: the number of pointers preallocated
* @element_free_func: (nullable): a function to free elements with
* destroy @array
*
* Creates a new `GPtrArray` with @reserved_size pointers preallocated
* and a reference count of 1. This avoids frequent reallocation, if
* you are going to add many pointers to the array. Note however that
* the size of the array is still 0. It also sets @element_free_func
* for freeing each element when the array is destroyed either via
* [func@GLib.PtrArray.unref], when [func@GLib.PtrArray.free] is called with
* @free_segment set to true or when removing elements.
*
* Returns: (transfer full): The new `GPtrArray`
*
* Since: 2.30
*/
GPtrArray*
g_ptr_array_new_full (guint reserved_size,
GDestroyNotify element_free_func)
{
return ptr_array_new (reserved_size, element_free_func, FALSE);
}
/**
* g_ptr_array_new_null_terminated:
* @reserved_size: the number of pointers preallocated.
* If @null_terminated is `TRUE`, the actually allocated
* buffer size is @reserved_size plus 1, unless @reserved_size
* is zero, in which case no initial buffer gets allocated.
* @element_free_func: (nullable): a function to free elements during
* destruction of @array
* @null_terminated: if true, make the array `NULL` terminated
*
* Like [func@GLib.PtrArray.new_full] but also allows to set the array to
* be `NULL` terminated. A `NULL` terminated pointer array has an
* additional `NULL` pointer after the last element, beyond the
* current length.
*
* `GPtrArray` created by other constructors are not automatically `NULL`
* terminated.
*
* Note that if the @array’s length is zero and currently no
* data array is allocated, then `pdata` will still be `NULL`.
* `GPtrArray` will only `NULL` terminate `pdata`, if an actual
* array is allocated. It does not guarantee that an array
* is always allocated. In other words, if the length is zero,
* then `pdata` may either point to a `NULL` terminated array of length
* zero or be `NULL`.
*
* Returns: (transfer full): The new `GPtrArray`
*
* Since: 2.74
*/
GPtrArray *
g_ptr_array_new_null_terminated (guint reserved_size,
GDestroyNotify element_free_func,
gboolean null_terminated)
{
return ptr_array_new (reserved_size, element_free_func, null_terminated);
}
/**
* g_ptr_array_set_free_func:
* @array: a pointer array
* @element_free_func: (nullable): a function to free elements during
* destruction of @array
*
* Sets a function for freeing each element when @array is destroyed
* either via [func@GLib.PtrArray.unref], when [func@GLib.PtrArray.free] is
* called with @free_segment set to true or when removing elements.
*
* Since: 2.22
*/
void
g_ptr_array_set_free_func (GPtrArray *array,
GDestroyNotify element_free_func)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
g_return_if_fail (array);
rarray->element_free_func = element_free_func;
}
/**
* g_ptr_array_is_null_terminated:
* @array: a pointer array
*
* Checks whether the @array was constructed as `NULL`-terminated.
*
* This will only return true for arrays constructed by passing true to the
* `null_terminated` argument of [func@GLib.PtrArray.new_null_terminated]. It
* will not return true for normal arrays which have had a `NULL` element
* appended to them.
*
* Returns: true if the array is made to be `NULL` terminated; false otherwise
*
* Since: 2.74
*/
gboolean
g_ptr_array_is_null_terminated (GPtrArray *array)
{
g_return_val_if_fail (array, FALSE);
return ((GRealPtrArray *) array)->null_terminated;
}
/**
* g_ptr_array_ref:
* @array: a pointer array
*
* Atomically increments the reference count of @array by one.
* This function is thread-safe and may be called from any thread.
*
* Returns: (transfer full): The passed in `GPtrArray`
*
* Since: 2.22
*/
GPtrArray*
g_ptr_array_ref (GPtrArray *array)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
g_return_val_if_fail (array, NULL);
g_atomic_ref_count_inc (&rarray->ref_count);
return array;
}
static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
/**
* g_ptr_array_unref:
* @array: (transfer full): a pointer array
*
* Atomically decrements the reference count of @array by one. If the
* reference count drops to 0, the effect is the same as calling
* [func@GLib.PtrArray.free] with @free_segment set to true. This function
* is thread-safe and may be called from any thread.
*
* Since: 2.22
*/
void
g_ptr_array_unref (GPtrArray *array)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
g_return_if_fail (array);
if (g_atomic_ref_count_dec (&rarray->ref_count))
ptr_array_free (array, FREE_SEGMENT);
}
/**
* g_ptr_array_free:
* @array: (transfer full): a pointer array
* @free_segment: if true, the actual pointer array is freed as well
*
* Frees the memory allocated for the `GPtrArray`. If @free_segment is true
* it frees the memory block holding the elements as well. Pass false
* if you want to free the `GPtrArray` wrapper but preserve the
* underlying array for use elsewhere. If the reference count of @array
* is greater than one, the `GPtrArray` wrapper is preserved but the
* size of @array will be set to zero.
*
* If array contents point to dynamically-allocated memory, they should
* be freed separately if @free_segment is true and no
* [callback@GLib.DestroyNotify] function has been set for @array.
*
* Note that if the array is `NULL` terminated and @free_segment is false
* then this will always return an allocated `NULL` terminated buffer.
* If `pdata` is previously `NULL`, a new buffer will be allocated.
*
* This function is not thread-safe. If using a `GPtrArray` from multiple
* threads, use only the atomic [func@GLib.PtrArray.ref] and
* [func@GLib.PtrArray.unref] functions.
*
* Returns: (transfer full) (array) (nullable): The allocated pointer array if
* @free_segment is false, otherwise `NULL`.
*/
gpointer*
g_ptr_array_free (GPtrArray *array,
gboolean free_segment)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
ArrayFreeFlags flags;
g_return_val_if_fail (rarray, NULL);
flags = (free_segment ? FREE_SEGMENT : 0);
/* if others are holding a reference, preserve the wrapper but
* do free/return the data
*
* Coverity doesn’t understand this and assumes it’s a leak, so comment this
* out.
*/
#ifndef __COVERITY__
if (!g_atomic_ref_count_dec (&rarray->ref_count))
flags |= PRESERVE_WRAPPER;
#endif
return ptr_array_free (array, flags);
}
static gpointer *
ptr_array_free (GPtrArray *array,
ArrayFreeFlags flags)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
gpointer *segment;
if (flags & FREE_SEGMENT)
{
/* Data here is stolen and freed manually. It is an
* error to attempt to access the array data (including
* mutating the array bounds) during destruction).
*
* https://bugzilla.gnome.org/show_bug.cgi?id=769064
*/
gpointer *stolen_pdata = g_steal_pointer (&rarray->pdata);
if (rarray->element_free_func != NULL)
{
guint i;
for (i = 0; i < rarray->len; ++i)
rarray->element_free_func (stolen_pdata[i]);
}
g_free (stolen_pdata);
segment = NULL;
}
else
{
segment = rarray->pdata;
if (!segment && rarray->null_terminated)
segment = (gpointer *) g_new0 (char *, 1);
}
if (flags & PRESERVE_WRAPPER)
{
rarray->pdata = NULL;
rarray->len = 0;
rarray->alloc = 0;
}
else
{
g_slice_free1 (sizeof (GRealPtrArray), rarray);
}
return segment;
}
static void
g_ptr_array_maybe_expand (GRealPtrArray *array,
guint len)
{
guint max_len, want_len;
/* The maximum array length is derived from following constraints:
* - The number of bytes must fit into a gsize / 2.
* - The number of elements must fit into guint.
* - null terminated arrays must leave space for the terminating element
*/
max_len = MIN (G_MAXSIZE / 2 / sizeof (gpointer), G_MAXUINT) - (array->null_terminated ? 1 : 0);
/* Detect potential overflow */
if G_UNLIKELY ((max_len - array->len) < len)
g_error ("adding %u to array would overflow", len);
want_len = array->len + len + (array->null_terminated ? 1 : 0);
if (want_len > array->alloc)
{
guint old_alloc = array->alloc;
gsize want_alloc = g_nearest_pow (sizeof (gpointer) * want_len);
want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
array->alloc = MIN (want_alloc / sizeof (gpointer), G_MAXUINT);
array->pdata = g_realloc (array->pdata, want_alloc);
if (G_UNLIKELY (g_mem_gc_friendly))
for ( ; old_alloc < array->alloc; old_alloc++)
array->pdata [old_alloc] = NULL;
}
}
/**
* g_ptr_array_set_size:
* @array: a pointer array
* @length: the new length of the pointer array
*
* Sets the size of the array. When making the array larger,
* newly-added elements will be set to `NULL`. When making it smaller,
* if @array has a non-`NULL` [callback@GLib.DestroyNotify] function then it
* will be called for the removed elements.
*/
void
g_ptr_array_set_size (GPtrArray *array,
gint length)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
guint length_unsigned;
g_return_if_fail (rarray);
g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
g_return_if_fail (length >= 0);
length_unsigned = (guint) length;
if (length_unsigned > rarray->len)
{
guint i;
g_ptr_array_maybe_expand (rarray, length_unsigned - rarray->len);
/* This is not
* memset (array->pdata + array->len, 0,
* sizeof (gpointer) * (length_unsigned - array->len));
* to make it really portable. Remember (void*)NULL needn't be
* bitwise zero. It of course is silly not to use memset (..,0,..).
*/
for (i = rarray->len; i < length_unsigned; i++)
rarray->pdata[i] = NULL;
rarray->len = length_unsigned;
ptr_array_maybe_null_terminate (rarray);
}
else if (length_unsigned < rarray->len)
g_ptr_array_remove_range (array, length_unsigned, rarray->len - length_unsigned);
}
static gpointer
ptr_array_remove_index (GPtrArray *array,
guint index_,
gboolean fast,
gboolean free_element)
{
GRealPtrArray *rarray = (GRealPtrArray *) array;
gpointer result;
g_return_val_if_fail (rarray, NULL);
g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
g_return_val_if_fail (index_ < rarray->len, NULL);
result = rarray->pdata[index_];
if (rarray->element_free_func != NULL && free_element)
rarray->element_free_func (rarray->pdata[index_]);
if (index_ != rarray->len - 1 && !fast)
memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
sizeof (gpointer) * (rarray->len - index_ - 1));
else if (index_ != rarray->len - 1)
rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
rarray->len -= 1;
if (rarray->null_terminated || G_UNLIKELY (g_mem_gc_friendly))
rarray->pdata[rarray->len] = NULL;
return result;
}
/**
* g_ptr_array_remove_index:
* @array: a pointer array
* @index_: the index of the pointer to remove
*
* Removes the pointer at the given index from the pointer array.
* The following elements are moved down one place. If @array has
* a non-`NULL` [callback@GLib.DestroyNotify] function it is called for the
* removed
* element. If so, the return value from this function will potentially point
* to freed memory (depending on the [callback@GLib.DestroyNotify]
* implementation).
*
* Returns: (nullable): The pointer which was removed
*/
gpointer
g_ptr_array_remove_index (GPtrArray *array,
guint index_)
{
return ptr_array_remove_index (array, index_, FALSE, TRUE);
}
/**
* g_ptr_array_remove_index_fast:
* @array: a pointer array
* @index_: the index of the pointer to remove
*
* Removes the pointer at the given index from the pointer array.
* The last element in the array is used to fill in the space, so
* this function does not preserve the order of the array. But it
* is faster than [func@GLib.PtrArray.remove_index]. If @array has a non-`NULL`
* [callback@GLib.DestroyNotify] function it is called for the removed element.
* If so, the
* return value from this function will potentially point to freed memory
* (depending on the [callback@GLib.DestroyNotify] implementation).
*
* Returns: (nullable): The pointer which was removed
*/
gpointer
g_ptr_array_remove_index_fast (GPtrArray *array,
guint index_)
{
return ptr_array_remove_index (array, index_, TRUE, TRUE);
}
/**
* g_ptr_array_steal_index:
* @array: a pointer array
* @index_: the index of the pointer to steal
*
* Removes the pointer at the given index from the pointer array.
* The following elements are moved down one place. The
* [callback@GLib.DestroyNotify] for
* @array is *not* called on the removed element; ownership is transferred to
* the caller of this function.
*
* Returns: (transfer full) (nullable): The pointer which was removed
* Since: 2.58
*/
gpointer
g_ptr_array_steal_index (GPtrArray *array,
guint index_)
{
return ptr_array_remove_index (array, index_, FALSE, FALSE);
}
/**
* g_ptr_array_steal_index_fast:
* @array: a pointer array
* @index_: the index of the pointer to steal
*
* Removes the pointer at the given index from the pointer array.
* The last element in the array is used to fill in the space, so
* this function does not preserve the order of the array. But it
* is faster than [func@GLib.PtrArray.steal_index]. The
* [callback@GLib.DestroyNotify] for @array is
* *not* called on the removed element; ownership is transferred to the caller
* of this function.
*
* Returns: (transfer full) (nullable): The pointer which was removed
* Since: 2.58
*/
gpointer
g_ptr_array_steal_index_fast (GPtrArray *array,
guint index_)
{
return ptr_array_remove_index (array, index_, TRUE, FALSE);
}
/**
* g_ptr_array_remove_range:
* @array: a pointer array
* @index_: the index of the first pointer to remove
* @length: the number of pointers to remove
*
* Removes the given number of pointers starting at the given index
* from a `GPtrArray`. The following elements are moved to close the
* gap. If @array has a non-`NULL` [callback@GLib.DestroyNotify] function it is
* called for the removed elements.
*
* Returns: (transfer none): The @array
*
* Since: 2.4
*/
GPtrArray*
g_ptr_array_remove_range (GPtrArray *array,
guint index_,
guint length)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
guint i;
g_return_val_if_fail (rarray != NULL, NULL);
g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
g_return_val_if_fail (index_ <= rarray->len, NULL);
g_return_val_if_fail (index_ <= G_MAXUINT - length, NULL);
g_return_val_if_fail (length == 0 || index_ + length <= rarray->len, NULL);
if (length == 0)
return array;
if (rarray->element_free_func != NULL)
{
for (i = index_; i < index_ + length; i++)
rarray->element_free_func (rarray->pdata[i]);
}
if (index_ + length != rarray->len)
{
memmove (&rarray->pdata[index_],
&rarray->pdata[index_ + length],
(rarray->len - (index_ + length)) * sizeof (gpointer));
}
rarray->len -= length;
if (G_UNLIKELY (g_mem_gc_friendly))
{
for (i = 0; i < length; i++)
rarray->pdata[rarray->len + i] = NULL;
}
else
ptr_array_maybe_null_terminate (rarray);
return array;
}
/**
* g_ptr_array_remove:
* @array: a pointer array
* @data: the pointer to remove
*
* Removes the first occurrence of the given pointer from the pointer
* array. The following elements are moved down one place. If @array
* has a non-`NULL` [callback@GLib.DestroyNotify] function it is called for the
* removed element.
*
* It returns true if the pointer was removed, or false if the
* pointer was not found.
*
* Returns: true if the pointer is found and removed; false otherwise
*/
gboolean
g_ptr_array_remove (GPtrArray *array,
gpointer data)
{
guint i;
g_return_val_if_fail (array, FALSE);
g_return_val_if_fail (array->len == 0 || (array->len != 0 && array->pdata != NULL), FALSE);
for (i = 0; i < array->len; i += 1)
{
if (array->pdata[i] == data)
{
g_ptr_array_remove_index (array, i);
return TRUE;
}
}
return FALSE;
}
/**
* g_ptr_array_remove_fast:
* @array: a pointer array
* @data: the pointer to remove
*
* Removes the first occurrence of the given pointer from the pointer
* array. The last element in the array is used to fill in the space,
* so this function does not preserve the order of the array. But it
* is faster than [func@GLib.PtrArray.remove]. If @array has a non-`NULL`
* [callback@GLib.DestroyNotify] function it is called for the removed element.
*
* It returns true if the pointer was removed, or false if the
* pointer was not found.
*
* Returns: true if the pointer is found and removed; false otherwise
*/
gboolean
g_ptr_array_remove_fast (GPtrArray *array,
gpointer data)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
guint i;
g_return_val_if_fail (rarray, FALSE);
g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), FALSE);
for (i = 0; i < rarray->len; i += 1)
{
if (rarray->pdata[i] == data)
{
g_ptr_array_remove_index_fast (array, i);
return TRUE;
}
}
return FALSE;
}
/**
* g_ptr_array_add:
* @array: a pointer array
* @data: the pointer to add
*
* Adds a pointer to the end of the pointer array. The array will grow
* in size automatically if necessary.
*/
void
g_ptr_array_add (GPtrArray *array,
gpointer data)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
g_return_if_fail (rarray);
g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
g_ptr_array_maybe_expand (rarray, 1u);
rarray->pdata[rarray->len++] = data;
ptr_array_maybe_null_terminate (rarray);
}
/**
* g_ptr_array_extend:
* @array_to_extend: a pointer array
* @array: (transfer none): a pointer array to add to the end of @array_to_extend
* @func: (scope call) (nullable): a copy function used to copy every element in the array
* @user_data: the user data passed to the copy function @func
*
* Adds all pointers of @array to the end of the array @array_to_extend.
* The array will grow in size automatically if needed. @array_to_extend is
* modified in-place.
*
* @func, as a [callback@GLib.CopyFunc], takes two arguments, the data to be
* copied
* and a @user_data pointer. On common processor architectures, it’s safe to
* pass `NULL` as @user_data if the copy function takes only one argument. You
* may get compiler warnings from this though if compiling with GCC’s
* `-Wcast-function-type` warning.
*
* If @func is `NULL`, then only the pointers (and not what they are
* pointing to) are copied to the new `GPtrArray`.
*
* Whether @array_to_extend is `NULL` terminated stays unchanged by this function.
*
* Since: 2.62
**/
void
g_ptr_array_extend (GPtrArray *array_to_extend,
GPtrArray *array,
GCopyFunc func,
gpointer user_data)
{
GRealPtrArray *rarray_to_extend = (GRealPtrArray *) array_to_extend;
g_return_if_fail (array_to_extend != NULL);
g_return_if_fail (array != NULL);
if (array->len == 0u)
return;
g_ptr_array_maybe_expand (rarray_to_extend, array->len);
if (func != NULL)
{
guint i;
for (i = 0; i < array->len; i++)
rarray_to_extend->pdata[i + rarray_to_extend->len] =
func (array->pdata[i], user_data);
}
else if (array->len > 0)
{
memcpy (rarray_to_extend->pdata + rarray_to_extend->len, array->pdata,
array->len * sizeof (*array->pdata));
}
rarray_to_extend->len += array->len;
ptr_array_maybe_null_terminate (rarray_to_extend);
}
/**
* g_ptr_array_extend_and_steal:
* @array_to_extend: (transfer none): a pointer array
* @array: (transfer container): a pointer array to add to the end of
* @array_to_extend
*
* Adds all the pointers in @array to the end of @array_to_extend, transferring
* ownership of each element from @array to @array_to_extend and modifying
* @array_to_extend in-place. @array is then freed.
*
* As with [func@GLib.PtrArray.free], @array will be destroyed if its reference
* count is 1. If its reference count is higher, it will be decremented and the
* length of @array set to zero.
*
* Since: 2.62
**/
void
g_ptr_array_extend_and_steal (GPtrArray *array_to_extend,
GPtrArray *array)
{
gpointer *pdata;
g_return_if_fail (array_to_extend != NULL);
g_return_if_fail (array != NULL);
g_ptr_array_extend (array_to_extend, array, NULL, NULL);
/* Get rid of @array without triggering the GDestroyNotify attached
* to the elements moved from @array to @array_to_extend. */
pdata = g_steal_pointer (&array->pdata);
array->len = 0;
((GRealPtrArray *) array)->alloc = 0;
g_ptr_array_unref (array);
g_free (pdata);
}
/**
* g_ptr_array_insert:
* @array: a pointer array
* @index_: the index to place the new element at, or -1 to append
* @data: the pointer to add
*
* Inserts an element into the pointer array at the given index. The
* array will grow in size automatically if necessary.
*
* Since: 2.40
*/
void
g_ptr_array_insert (GPtrArray *array,
gint index_,
gpointer data)
{
GRealPtrArray *rarray = (GRealPtrArray *)array;
guint real_index;
g_return_if_fail (rarray);
g_return_if_fail (index_ >= -1);
g_return_if_fail (index_ < 0 || (guint) index_ <= rarray->len);
g_ptr_array_maybe_expand (rarray, 1u);
real_index = (index_ >= 0) ? (guint) index_ : rarray->len;
if (real_index < rarray->len)
memmove (&(rarray->pdata[real_index + 1]),
&(rarray->pdata[real_index]),
(rarray->len - real_index) * sizeof (gpointer));
rarray->len++;
rarray->pdata[real_index] = data;
ptr_array_maybe_null_terminate (rarray);
}
/* Please keep this doc-comment in sync with pointer_array_sort_example()
* in glib/tests/array-test.c */
/**
* g_ptr_array_sort:
* @array: a pointer array
* @compare_func: (scope call): a comparison function
*
* Sorts the array, using @compare_func which should be a `qsort()`-style
* comparison function (returns less than zero for first arg is less
* than second arg, zero for equal, greater than zero if first arg is
* greater than second arg).
*
* Note that the comparison function for [func@GLib.PtrArray.sort] doesn’t
* take the pointers from the array as arguments, it takes pointers to
* the pointers in the array.
*
* Use [func@GLib.PtrArray.sort_values] if you want to use normal
* [callback@GLib.CompareFunc] instances, otherwise here is a full example of
* use:
*
* ```c
* typedef struct
* {
* gchar *name;
* gint size;
* } FileListEntry;
*
* static gint
* sort_filelist (gconstpointer a, gconstpointer b)
* {
* const FileListEntry *entry1 = *((FileListEntry **) a);
* const FileListEntry *entry2 = *((FileListEntry **) b);
*
* return g_ascii_strcasecmp (entry1->name, entry2->name);
* }
*
* …
* g_autoptr (GPtrArray) file_list = NULL;
*
* // initialize file_list array and load with many FileListEntry entries
* ...
* // now sort it with
* g_ptr_array_sort (file_list, sort_filelist);
* ```
*
* This is guaranteed to be a stable sort since version 2.32.
*/
void
g_ptr_array_sort (GPtrArray *array,
GCompareFunc compare_func)
{
g_return_if_fail (array != NULL);
/* Don't use qsort as we want a guaranteed stable sort */
if (array->len > 0)
g_sort_array (array->pdata,
array->len,
sizeof (gpointer),
(GCompareDataFunc) compare_func,
NULL);
}
/* Please keep this doc-comment in sync with
* pointer_array_sort_with_data_example() in glib/tests/array-test.c */
/**
* g_ptr_array_sort_with_data:
* @array: a pointer array
* @compare_func: (scope call): a comparison function
* @user_data: the data to pass to @compare_func
*
* Like [func@GLib.PtrArray.sort], but the comparison function has an extra
* user data argument.
*
* Note that the comparison function for [func@GLib.PtrArray.sort_with_data]
* doesn’t take the pointers from the array as arguments, it takes
* pointers to the pointers in the array.
*
* Use [func@GLib.PtrArray.sort_values_with_data] if you want to use normal
* [callback@GLib.CompareDataFunc] instances, otherwise here is a full example
* of use:
*
* ```c
* typedef enum { SORT_NAME, SORT_SIZE } SortMode;
*
* typedef struct
* {
* gchar *name;
* gint size;
* } FileListEntry;
*
* static gint
* sort_filelist (gconstpointer a, gconstpointer b, gpointer user_data)
* {
* gint order;
* const SortMode sort_mode = GPOINTER_TO_INT (user_data);
* const FileListEntry *entry1 = *((FileListEntry **) a);
* const FileListEntry *entry2 = *((FileListEntry **) b);
*
* switch (sort_mode)
* {
* case SORT_NAME:
* order = g_ascii_strcasecmp (entry1->name, entry2->name);
* break;
* case SORT_SIZE:
* order = entry1->size - entry2->size;
* break;
* default:
* order = 0;
* break;
* }
* return order;
* }
*
* ...
* g_autoptr (GPtrArray) file_list = NULL;
* SortMode sort_mode;
*
* // initialize file_list array and load with many FileListEntry entries
* ...
* // now sort it with
* sort_mode = SORT_NAME;
* g_ptr_array_sort_with_data (file_list,
* sort_filelist,
* GINT_TO_POINTER (sort_mode));
* ```
*
* This is guaranteed to be a stable sort since version 2.32.
*/
void
g_ptr_array_sort_with_data (GPtrArray *array,
GCompareDataFunc compare_func,
gpointer user_data)
{
g_return_if_fail (array != NULL);
if (array->len > 0)
g_sort_array (array->pdata,
array->len,
sizeof (gpointer),
compare_func,
user_data);
}
static inline gint
compare_ptr_array_values (gconstpointer a, gconstpointer b, gpointer user_data)
{
gconstpointer aa = *((gconstpointer *) a);
gconstpointer bb = *((gconstpointer *) b);
GCompareFunc compare_func = user_data;
return compare_func (aa, bb);
}
/**
* g_ptr_array_sort_values:
* @array: a pointer array
* @compare_func: (scope call): a comparison function
*
* Sorts the array, using @compare_func which should be a `qsort()`-style
* comparison function (returns less than zero for first arg is less
* than second arg, zero for equal, greater than zero if first arg is
* greater than second arg).
*
* This is guaranteed to be a stable sort.
*
* Since: 2.76
*/
void
g_ptr_array_sort_values (GPtrArray *array,
GCompareFunc compare_func)
{
g_ptr_array_sort_with_data (array, compare_ptr_array_values, compare_func);
}
typedef struct
{
GCompareDataFunc compare_func;
gpointer user_data;
} GPtrArraySortValuesData;
static inline gint
compare_ptr_array_values_with_data (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
gconstpointer aa = *((gconstpointer *) a);
gconstpointer bb = *((gconstpointer *) b);
GPtrArraySortValuesData *data = user_data;
return data->compare_func (aa, bb, data->user_data);
}
/**
* g_ptr_array_sort_values_with_data:
* @array: a pointer array
* @compare_func: (scope call): a comparison function
* @user_data: the data to pass to @compare_func
*
* Like [func@GLib.PtrArray.sort_values], but the comparison function has an
* extra user data argument.
*
* This is guaranteed to be a stable sort.
*
* Since: 2.76
*/
void
g_ptr_array_sort_values_with_data (GPtrArray *array,
GCompareDataFunc compare_func,
gpointer user_data)
{
g_ptr_array_sort_with_data (array, compare_ptr_array_values_with_data,
&(GPtrArraySortValuesData){
.compare_func = compare_func,
.user_data = user_data,
});
}
/**
* g_ptr_array_foreach:
* @array: a pointer array
* @func: (scope call): the function to call for each array element
* @user_data: the user data to pass to the function
*
* Calls a function for each element of a `GPtrArray`. @func must not
* add elements to or remove elements from the array.
*
* Since: 2.4
*/
void
g_ptr_array_foreach (GPtrArray *array,
GFunc func,
gpointer user_data)
{
guint i;
g_return_if_fail (array);
for (i = 0; i < array->len; i++)
(*func) (array->pdata[i], user_data);
}
/**
* g_ptr_array_find: (skip)
* @haystack: the pointer array to be searched
* @needle: the pointer to look for
* @index_: (optional) (out): the return location for the index of
* the element, if found
*
* Checks whether @needle exists in @haystack. If the element is found, true
* is returned and the element’s index is returned in @index_ (if non-`NULL`).
* Otherwise, false is returned and @index_ is undefined. If @needle exists
* multiple times in @haystack, the index of the first instance is returned.
*
* This does pointer comparisons only. If you want to use more complex equality
* checks, such as string comparisons, use
* [func@GLib.PtrArray.find_with_equal_func].
*
* Returns: true if @needle is one of the elements of @haystack; false otherwise
* Since: 2.54
*/
gboolean
g_ptr_array_find (GPtrArray *haystack,
gconstpointer needle,
guint *index_)
{
return g_ptr_array_find_with_equal_func (haystack, needle, NULL, index_);
}
/**
* g_ptr_array_find_with_equal_func: (skip)
* @haystack: the pointer array to be searched
* @needle: the pointer to look for
* @equal_func: (nullable): the function to call for each element, which should
* return true when the desired element is found; or `NULL` to use pointer
* equality
* @index_: (optional) (out): the return location for the index of
* the element, if found
*
* Checks whether @needle exists in @haystack, using the given @equal_func.
* If the element is found, true is returned and the element’s index is
* returned in @index_ (if non-`NULL`). Otherwise, false is returned and @index_
* is undefined. If @needle exists multiple times in @haystack, the index of
* the first instance is returned.
*
* @equal_func is called with the element from the array as its first parameter,
* and @needle as its second parameter. If @equal_func is `NULL`, pointer
* equality is used.
*
* Returns: true if @needle is one of the elements of @haystack; false otherwise
* Since: 2.54
*/
gboolean
g_ptr_array_find_with_equal_func (GPtrArray *haystack,
gconstpointer needle,
GEqualFunc equal_func,
guint *index_)
{
guint i;
g_return_val_if_fail (haystack != NULL, FALSE);
if (equal_func == NULL)
equal_func = g_direct_equal;
for (i = 0; i < haystack->len; i++)
{
if (equal_func (g_ptr_array_index (haystack, i), needle))
{
if (index_ != NULL)
*index_ = i;
return TRUE;
}
}
return FALSE;
}
/**
* GByteArray: (copy-func g_byte_array_ref) (free-func g_byte_array_unref)
* @data: a pointer to the element data. The data may be moved as
* elements are added to the `GByteArray`
* @len: the number of elements in the `GByteArray`
*
* Contains the public fields of a `GByteArray`.
*/
/**
* g_byte_array_new:
*
* Creates a new `GByteArray` with a reference count of 1.
*
* Returns: (transfer full): The new `GByteArray`
*/
GByteArray*
g_byte_array_new (void)
{
return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, 0);
}
/**
* g_byte_array_steal:
* @array: a byte array
* @len: (optional) (out): the pointer to retrieve the number of
* elements of the original array
*
* Frees the data in the array and resets the size to zero, while
* the underlying array is preserved for use elsewhere and returned
* to the caller.
*
* Returns: (transfer full) (array length=len): The allocated element data
*
* Since: 2.64
*/
guint8 *
g_byte_array_steal (GByteArray *array,
gsize *len)
{
return (guint8 *) g_array_steal ((GArray *) array, len);
}
/**
* g_byte_array_new_take:
* @data: (transfer full) (array length=len): the byte data for the array
* @len: the length of @data
*
* Creates a byte array containing the @data.
* After this call, @data belongs to the `GByteArray` and may no longer be
* modified by the caller. The memory of @data has to be dynamically
* allocated and will eventually be freed with [func@GLib.free].
*
* Do not use it if @len is greater than [`G_MAXUINT`](types.html#guint).
* `GByteArray` stores the length of its data in `guint`, which may be shorter
* than `gsize`.
*
* Since: 2.32
*
* Returns: (transfer full): The new `GByteArray`
*/
GByteArray*
g_byte_array_new_take (guint8 *data,
gsize len)
{
GByteArray *array;
GRealArray *real;
g_return_val_if_fail (len <= G_MAXUINT, NULL);
array = g_byte_array_new ();
real = (GRealArray *)array;
g_assert (real->data == NULL);
g_assert (real->len == 0);
real->data = data;
real->len = len;
real->elt_capacity = len;
return array;
}
/**
* g_byte_array_sized_new:
* @reserved_size: the number of bytes preallocated
*
* Creates a new `GByteArray` with @reserved_size bytes preallocated.
* This avoids frequent reallocation, if you are going to add many
* bytes to the array. Note however that the size of the array is still
* 0.
*
* Returns: (transfer full): The new `GByteArray`
*/
GByteArray*
g_byte_array_sized_new (guint reserved_size)
{
return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, reserved_size);
}
/**
* g_byte_array_free:
* @array: (transfer full): a byte array
* @free_segment: if true, the actual byte data is freed as well
*
* Frees the memory allocated by the `GByteArray`. If @free_segment is
* true it frees the actual byte data. If the reference count of
* @array is greater than one, the `GByteArray` wrapper is preserved but
* the size of @array will be set to zero.
*
* Returns: (nullable) (array) (transfer full): The allocated element data if
* @free_segment is false, otherwise `NULL`.
*/
guint8*
g_byte_array_free (GByteArray *array,
gboolean free_segment)
{
return (guint8 *)g_array_free ((GArray *)array, free_segment);
}
/**
* g_byte_array_free_to_bytes:
* @array: (transfer full): a byte array
*
* Transfers the data from the `GByteArray` into a new immutable
* [struct@GLib.Bytes].
*
* The `GByteArray` is freed unless the reference count of @array is greater
* than one, in which the `GByteArray` wrapper is preserved but the size of
* @array will be set to zero.
*
* This is identical to using [ctor@GLib.Bytes.new_take] and
* [func@GLib.ByteArray.free] together.
*
* Since: 2.32
*
* Returns: (transfer full): The new immutable [struct@GLib.Bytes] representing
* same byte data that was in the array
*/
GBytes*
g_byte_array_free_to_bytes (GByteArray *array)
{
gsize length;
g_return_val_if_fail (array != NULL, NULL);
length = array->len;
return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
}
/**
* g_byte_array_ref:
* @array: a byte array
*
* Atomically increments the reference count of @array by one.
* This function is thread-safe and may be called from any thread.
*
* Returns: (transfer full): The passed in `GByteArray`
*
* Since: 2.22
*/
GByteArray*
g_byte_array_ref (GByteArray *array)
{
return (GByteArray *)g_array_ref ((GArray *)array);
}
/**
* g_byte_array_unref:
* @array: (transfer full): a byte array
*
* Atomically decrements the reference count of @array by one. If the
* reference count drops to 0, all memory allocated by the array is
* released. This function is thread-safe and may be called from any
* thread.
*
* Since: 2.22
*/
void
g_byte_array_unref (GByteArray *array)
{
g_array_unref ((GArray *)array);
}
/**
* g_byte_array_append:
* @array: a byte array
* @data: (array length=len): the byte data to be added
* @len: the number of bytes to add
*
* Adds the given bytes to the end of the `GByteArray`.
* The array will grow in size automatically if necessary.
*
* Returns: (transfer none): The `GByteArray`
*/
GByteArray*
g_byte_array_append (GByteArray *array,
const guint8 *data,
guint len)
{
return (GByteArray *) g_array_append_vals ((GArray *) array, (guint8 *) data, len);
}
/**
* g_byte_array_prepend:
* @array: a byte array
* @data: (array length=len): the byte data to be added
* @len: the number of bytes to add
*
* Adds the given data to the start of the `GByteArray`.
* The array will grow in size automatically if necessary.
*
* Returns: (transfer none): The `GByteArray`
*/
GByteArray*
g_byte_array_prepend (GByteArray *array,
const guint8 *data,
guint len)
{
return (GByteArray *) g_array_prepend_vals ((GArray *) array, (guint8 *) data, len);
}
/**
* g_byte_array_set_size:
* @array: a byte array
* @length: the new size of the `GByteArray`
*
* Sets the size of the `GByteArray`, expanding it if necessary.
*
* Returns: (transfer none): The `GByteArray`
*/
GByteArray*
g_byte_array_set_size (GByteArray *array,
guint length)
{
return (GByteArray *) g_array_set_size ((GArray *) array, length);
}
/**
* g_byte_array_remove_index:
* @array: a byte array
* @index_: the index of the byte to remove
*
* Removes the byte at the given index from a `GByteArray`.
* The following bytes are moved down one place.
*
* Returns: (transfer none): The `GByteArray`
**/
GByteArray*
g_byte_array_remove_index (GByteArray *array,
guint index_)
{
return (GByteArray *) g_array_remove_index ((GArray *) array, index_);
}
/**
* g_byte_array_remove_index_fast:
* @array: a byte array
* @index_: the index of the byte to remove
*
* Removes the byte at the given index from a `GByteArray`. The last
* element in the array is used to fill in the space, so this function
* does not preserve the order of the `GByteArray`. But it is faster
* than [func@GLib.ByteArray.remove_index].
*
* Returns: (transfer none): The `GByteArray`
*/
GByteArray*
g_byte_array_remove_index_fast (GByteArray *array,
guint index_)
{
return (GByteArray *) g_array_remove_index_fast ((GArray *) array, index_);
}
/**
* g_byte_array_remove_range:
* @array: a byte array
* @index_: the index of the first byte to remove
* @length: the number of bytes to remove
*
* Removes the given number of bytes starting at the given index from a
* `GByteArray`. The following elements are moved to close the gap.
*
* Returns: (transfer none): The `GByteArray`
*
* Since: 2.4
*/
GByteArray*
g_byte_array_remove_range (GByteArray *array,
guint index_,
guint length)
{
g_return_val_if_fail (array, NULL);
g_return_val_if_fail (index_ <= array->len, NULL);
g_return_val_if_fail (index_ <= G_MAXUINT - length, NULL);
g_return_val_if_fail (index_ + length <= array->len, NULL);
return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
}
/**
* g_byte_array_sort:
* @array: a byte array
* @compare_func: (scope call): the comparison function
*
* Sorts a byte array, using @compare_func which should be a
* `qsort()`-style comparison function (returns less than zero for first
* arg is less than second arg, zero for equal, greater than zero if
* first arg is greater than second arg).
*
* If two array elements compare equal, their order in the sorted array
* is undefined. If you want equal elements to keep their order (i.e.
* you want a stable sort) you can write a comparison function that,
* if two elements would otherwise compare equal, compares them by
* their addresses.
*/
void
g_byte_array_sort (GByteArray *array,
GCompareFunc compare_func)
{
g_array_sort ((GArray *)array, compare_func);
}
/**
* g_byte_array_sort_with_data:
* @array: a byte array
* @compare_func: (scope call): the comparison function
* @user_data: the data to pass to @compare_func
*
* Like [func@GLib.ByteArray.sort], but the comparison function takes an extra
* user data argument.
*/
void
g_byte_array_sort_with_data (GByteArray *array,
GCompareDataFunc compare_func,
gpointer user_data)
{
g_array_sort_with_data ((GArray *)array, compare_func, user_data);
}
|