1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871
|
/*
*
* (C) Copyright IBM Corp. 2001, 2003
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Module: dlist.c
*
* Functions: dlist_t CreateList
* int InsertObject
* int ExclusiveInsertObject
* int DeleteAllItems
* int GetObject
* int BlindGetObject
* int GetNextObject
* int ExtractObject
* int BlindExtractObject
* int ReplaceObject
* int GetHandle
* int GetListSize
* boolean ListEmpty
* boolean AtEndOfList
* int DestroyList
* int NextItem
* int PreviousItem
* int GoToStartOfList
* int GoToEndOfList
* int GoToSpecifiedItem
* int SortList
* int ForEachItem
* int PruneList
* int AppendList
* int CopyList
*
* Description: This module implements a simple, generic, doubly linked list.
* Data objects of any type can be placed into a linked list
* created by this module. Furthermore, data objects of different
* types may be placed into the same linked list.
*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dlist.h" /* Import dlist.h so that the compiler can check the */
/* consistency of the declarations in dlist.h against */
/* those in this module. */
/*--------------------------------------------------
* Private Constants
--------------------------------------------------*/
/* Each list contains a Verify field. Before any operations are performed on
the list, this module checks the Verify field to see if the VerifyValue
is in the field. If the VerifyValue is not found in the Verify field,
the operation is aborted. */
#define VerifyValue 39646966L
/*--------------------------------------------------
* Private Type definitions
--------------------------------------------------*/
/* A list has the following structure:
dlist_t An item of type dlist_t is a pointer
| to the following structure:
|
V
----------------------
| |
| ControlNode |
----------------------
/ / \
Pointer to the / / \
LinkNode of / / \ Pointer to the LinkNode of
the first / / \ the last item in the list.
item in the / / Pointer to the \
list. / / LinkNode of the \
/ / Current Item in \ NOTE: All LinkNodes
/ / the list. \ have a pointer
V V V to the ControlNode.
------------- ------------- -------------
NULL <--| LinkNode | <--> | LinkNode |<--> ... <--> | LinkNode |--> NULL
------------- ------------- -------------
| | |
| | |
V V V
Data Data Data
NOTES: The ControlNode does the beekeeping for things which affect the
entire list. It tracks the number of items in the list, where the
first, current, and last items in the list reside. It also holds
the Verify field which is used to see if a pointer passed to us
really does point to a list created by this module.
Each data item placed in the list gets its own LinkNode. The
LinkNode tracks where in memory the data item is, how big the
data item is, what item tag the user gave that data item, and
where the LinkNodes for the previous and next items in the list
are located. By tracking this information here, the user does
not have to worry about placing fields for this information
inside of his data in order for his data items to be compatible
with this module. Thus, the operation of the list module is
decoupled from the data that is placed in the list.
*/
/*--------------------------------------------------
Private global variables.
--------------------------------------------------*/
boolean ErrorsFound = FALSE; /* Used to track whether or not errors have
been found. Can be used with a memory access
breakpoint to stop program execution when
an error is detected so that the type of
error can be seen. */
/*********************************************************************/
/* */
/* Function Name: CreateList */
/* */
/* Descriptive Name: This function allocates and initializes the */
/* data structures associated with a list and */
/* then returns a pointer to these structures. */
/* */
/* Input: None. */
/* */
/* Output: If Success : The function return value will be non-NULL */
/* */
/* If Failure : The function return value will be NULL. */
/* */
/* Error Handling: The function will only fail if it can not */
/* allocate enough memory to create the new list. */
/* */
/* Side Effects: None. */
/* */
/* Notes: None. */
/* */
/*********************************************************************/
dlist_t CreateList( void )
{
/* This function returns a DLIST. A variable of type dlist_t is really a
pointer to a ControlNode which has been typecast to (void *).
We will therefore declare a variable of type (ControlNode *) to
work with while in this function and then, if successful, typecast
this variable to dlist_t for the return value. */
ControlNode * ListData;
/* We must now initialize the data structures for the dlist_t we are
creating. We will start by allocating the memory. */
ListData = (ControlNode *) malloc(sizeof(ControlNode));
if (ListData == NULL)
{
return NULL;
}
/* Now that we have the memory, lets initialize the fields in the master record. */
ListData->ItemCount = 0; /* No items in the list. */
ListData->StartOfList = NULL; /* Since the list is empty, there is no first item */
ListData->EndOfList = NULL; /* Since the list is empty, there is no last item */
ListData->CurrentItem = NULL; /* Since the list is empty, there is no current item */
#ifdef EVMS_DEBUG
ListData->Verify = VerifyValue; /* Initialize the Verify field so that this list will recognized as being valid. */
#endif
/* Set the return value. */
return (dlist_t) ListData;
}
/*********************************************************************/
/* */
/* Function Name: InsertObject */
/* */
/* Descriptive Name: This function inserts an object into a DLIST.*/
/* The object can be inserted before or after */
/* the current item in the list. */
/* */
/* Input: dlist_t ListToAddTo : The list to which the */
/* data object is to be */
/* inserted. */
/* ADDRESS ItemLocation : The address of the data */
/* to append to the list */
/* TAG ItemTag : The item tag to associate with */
/* the item being appended to the */
/* list */
/* ADDRESS TargetHandle : The item in ListToAddTo which */
/* is used to determine where */
/* the item being transferred will */
/* be placed. If this is NULL, */
/* then the current item in */
/* ListToAddTo will be used. */
/* Insertion_Modes Insert_Mode : This indicates where, */
/* relative to the item in */
/* ListToAddTo specified by */
/* Target_Handle, the item being */
/* inserted can be placed. */
/* boolean MakeCurrent : If TRUE, the item being inserted */
/* into ListToAddTo becomes the */
/* current item in ListToAddTo. */
/* ADDRESS * Handle : The address of a variable to hold */
/* the handle for the item that was */
/* inserted into the list. */
/* */
/* Output: If all went well, the return value will be */
/* DLIST_SUCCESS and *Handle will contain the ADDRESS of */
/* the new item. If errors were encountered, the . */
/* return value will be the error code and *Handle will */
/* be NULL. */
/* */
/* Error Handling: This function will fail under the following */
/* conditions: */
/* ListToAddTo does not point to a valid */
/* list */
/* ItemLocation is NULL */
/* The memory required for a LINK NODE can not */
/* be allocated. */
/* TargetHandle is invalid or is for an item */
/* in another list. */
/* If this routine fails, an error code is returned*/
/* and any memory allocated by this function is */
/* freed. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The item to insert is NOT copied to the heap. Instead, */
/* the location of the item is stored in the list. */
/* InsertObject stores the address provided by the user. */
/* */
/* It is assumed that TargetHandle is valid, or is at least*/
/* the address of an accessible block of storage. If */
/* TargetHandle is invalid, or is not the address of an */
/* accessible block of storage, then a trap or exception */
/* may occur. */
/* */
/* It is assumed that if ItemLocation is not NULL, then */
/* it is a valid address that can be dereferenced. If */
/* this assumption is violated, an exception or trap may */
/* occur. */
/* */
/*********************************************************************/
int InsertObject (dlist_t ListToAddTo,
ADDRESS ItemLocation,
TAG ItemTag,
ADDRESS TargetHandle,
Insertion_Modes Insert_Mode,
boolean MakeCurrent,
ADDRESS * Handle)
{
/* Since ListToAddTo is of type DLIST, we can not use it without having
to type cast it each time. To avoid all of the type casting, we
will declare a local variable of type ControlNode * and then
initialize it once using ListToAddTo. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * NewNode; /* Used to create the LinkNode for the new item. */
LinkNode * CurrentNode; /* Used to hold the reference point for the insertion. */
LinkNode * PreviousNode; /* Used to point to the item prior to CurrentNode in the list while
the new item is being inserted. */
LinkNode * NextNode; /* Used to point to the item after CurrentNode in the list while
the new item is being inserted. */
/* Initialize the return handle in case we bail out with an error. */
*Handle = NULL;
/* We will assume that ListToAppendTo points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToAddTo;
/* Has the user specified a specific item in the list as a reference point for this insertion? */
if ( TargetHandle != NULL )
{
/* Since the user has specified a reference point for this insertion, set up to use it. */
CurrentNode = (LinkNode *) TargetHandle;
}
else
{
/* The user did not specify a reference point, so use the current item in the list as the reference point. */
CurrentNode = ListData->CurrentItem;
}
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
/* Since the list is valid, we must now see if the TargetHandle is valid. We
will assume that, if the TargetHandle is not NULL, it points to a LinkNode.
If the ControlNodeLocation field of the LinkNode points to the
ControlNode for the list we are working with, then the LinkNode is in
the list and can therefore be used safely.
At this point, CurrentNode has been set equal to TargetHandle if TargetHandle
is not NULL. If TargetHandle is NULL, then CurrentNode was set to the current
item in the list. */
if ( TargetHandle != NULL )
{
/* Is CurrentNode part of this list? */
if ( CurrentNode->ControlNodeLocation != ListData )
{
/* The handle either did not point to a ControlNode or it pointed to the wrong ControlNode! */
return DLIST_BAD_HANDLE;
}
}
/* We must check the insertion mode. */
if ( Insert_Mode > AppendToList )
{
return DLIST_INVALID_INSERTION_MODE;
}
/* Lets check the item being added to the DLIST. */
if (ItemLocation == NULL)
{
return DLIST_BAD_ITEM_POINTER;
}
#endif
/* Since both the list and item are valid, lets make a LinkNode. */
NewNode = (LinkNode *) malloc( sizeof(LinkNode) );
/* Did we get the memory? */
if (NewNode == NULL)
{
return DLIST_OUT_OF_MEMORY;
}
/* Now that all memory has been allocated, lets finish initializing the LinkNode. */
NewNode->DataLocation = ItemLocation;
NewNode->DataTag = ItemTag;
NewNode->NextLinkNode = NULL;
NewNode->PreviousLinkNode = NULL;
NewNode->ControlNodeLocation = ListData; /* Initialize the link to the control node
of the list containing this link node. */
/* Now we can add the node to the list. */
/* Is the list empty? If so, then the Insertion_Mode does not matter! */
if (ListData->CurrentItem == NULL)
{
/* The List is empty. This will be the first (and only) item in the list.
Also, since this will be the only item in the list, it automatically
becomes the current item. */
ListData->EndOfList = NewNode;
ListData->StartOfList = NewNode;
ListData->CurrentItem = NewNode;
}
else
{
/* The list was not empty. */
/* Now lets insert the item according to the specified Insert_Mode. */
switch ( Insert_Mode )
{
case InsertAtStart: /* Get the first item in the list. */
CurrentNode = ListData->StartOfList;
/* Now insert NewNode before CurrentNode. */
NewNode->NextLinkNode = CurrentNode;
CurrentNode->PreviousLinkNode = NewNode;
/* Now update the ControlNode. */
ListData->StartOfList = NewNode;
break;
case InsertBefore: /* CurrentNode already points to the Item we are to insert NewNode before. */
/* Is CurrentNode the first item in the list? */
if ( ListData->StartOfList != CurrentNode )
{
/* Since CurrentNode is not the first item in the list, we need the node prior to CurrentNode
so we can adjust its link fields. */
PreviousNode = CurrentNode->PreviousLinkNode;
/* Now make PreviousLinkNode point to NewNode and vice versa. */
PreviousNode->NextLinkNode = NewNode;
NewNode->PreviousLinkNode = PreviousNode;
}
else
{
/* Since CurrentNode is the first item in the list, that means that NewNode will be
the first item in the list after it is inserted. Update the ControlNode for this
list to reflect that NewNode will be the first item in the list. */
ListData->StartOfList = NewNode;
}
/* Now make NewNode point to CurrentNode and vice versa. */
NewNode->NextLinkNode = CurrentNode;
CurrentNode->PreviousLinkNode = NewNode;
break;
case InsertAfter: /* CurrentNode already points to the Item we are to insert NewNode after. */
/* Is CurrentNode the last item in the list? */
if ( ListData->EndOfList != CurrentNode )
{
/* Since CurrentNode is not the last item in the list, we need the node after to CurrentNode
so we can adjust its link fields. */
NextNode = CurrentNode->NextLinkNode;
/* Now make NextLinkNode point to NewNode and vice versa. */
NextNode->PreviousLinkNode = NewNode;
NewNode->NextLinkNode = NextNode;
}
else
{
/* Since CurrentNode is the last item in the list, that means that NewNode will be
the last item in the list after it is inserted. Update the ControlNode for this
list to reflect that NewNode will be the last item in the list. */
ListData->EndOfList = NewNode;
}
/* Now make NewNode point to CurrentNode and vice versa. */
CurrentNode->NextLinkNode = NewNode;
NewNode->PreviousLinkNode = CurrentNode;
break;
case AppendToList: /* Get the last item in the list. */
CurrentNode = ListData->EndOfList;
/* Now insert NewNode after CurrentNode. */
CurrentNode->NextLinkNode = NewNode;
NewNode->PreviousLinkNode = CurrentNode;
/* Now update the ControlNode. */
ListData->EndOfList = NewNode;
break;
default :
NewNode->ControlNodeLocation = NULL;
free(NewNode->DataLocation);
free(NewNode);
return DLIST_INVALID_INSERTION_MODE;
}
}
/* Adjust the count of the number of items in the list. */
ListData->ItemCount++;
/* Should the new node become the current item in the list? */
if ( MakeCurrent )
{
/* Adjust the control node so that NewNode becomes the current item in the list. */
ListData->CurrentItem = NewNode;
}
/* All done. Return the handle. Signal successful operation. */
*Handle = NewNode;
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: ExclusiveInsertObject */
/* */
/* Descriptive Name: This function inserts an object into a */
/* dlist_t. The object can be inserted before */
/* or after the current item in the list. If */
/* object is already in the list, it is not */
/* added again. */
/* */
/* Input: dlist_t ListToAddTo : The list to which the */
/* data object is to be */
/* inserted. */
/* ADDRESS ItemLocation : The address of the data */
/* to append to the list */
/* TAG ItemTag : The item tag to associate with */
/* the item being appended to the */
/* list */
/* ADDRESS TargetHandle : The item in ListToAddTo which */
/* is used to determine where */
/* the item being transferred will */
/* be placed. If this is NULL, */
/* then the current item in */
/* ListToAddTo will be used. */
/* Insertion_Modes Insert_Mode : This indicates where, */
/* relative to the item in */
/* ListToAddTo specified by */
/* Target_Handle, the item being */
/* inserted can be placed. */
/* boolean MakeCurrent : If TRUE, the item being inserted */
/* into ListToAddTo becomes the */
/* current item in ListToAddTo. */
/* ADDRESS * Handle : The address of a variable to hold */
/* the handle for the item that was */
/* inserted into the list. */
/* */
/* Output: If all went well, the return value will be */
/* DLIST_SUCCESS and *Handle will contain the ADDRESS of */
/* the new item. If errors were encountered, the . */
/* return value will be the error code and *Handle will */
/* be NULL. */
/* */
/* Error Handling: This function will fail under the following */
/* conditions: */
/* ListToAddTo does not point to a valid */
/* list */
/* ItemLocation is NULL */
/* The memory required for a LINK NODE can not */
/* be allocated. */
/* TargetHandle is invalid or is for an item */
/* in another list. */
/* If this routine fails, an error code is returned*/
/* and any memory allocated by this function is */
/* freed. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The item to insert is NOT copied to the heap. Instead, */
/* the location of the item is stored in the list. */
/* InsertObject stores the address provided by the user. */
/* */
/* It is assumed that TargetHandle is valid, or is at least*/
/* the address of an accessible block of storage. If */
/* TargetHandle is invalid, or is not the address of an */
/* accessible block of storage, then a trap or exception */
/* may occur. */
/* */
/* It is assumed that if ItemLocation is not NULL, then */
/* it is a valid address that can be dereferenced. If */
/* this assumption is violated, an exception or trap may */
/* occur. */
/* */
/*********************************************************************/
int ExclusiveInsertObject (dlist_t ListToAddTo,
ADDRESS ItemLocation,
TAG ItemTag,
ADDRESS TargetHandle,
Insertion_Modes Insert_Mode,
boolean MakeCurrent,
ADDRESS * Handle)
{
/* Since ListToProcess is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToProcess. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item while we access its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
int Error;
/* We will assume that ListToAddTo points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToAddTo;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* If the list has items in it, search for the specified item */
if (ListData->ItemCount != 0) {
/* Get the first link node in the list. */
CurrentLinkNode = ListData->StartOfList;
/* Now loop through the items in the list. */
while ( CurrentLinkNode != NULL ) {
if ( CurrentLinkNode->DataLocation == ItemLocation ) {
/* Item is already in the list. */
return DLIST_SUCCESS;
}
CurrentLinkNode = CurrentLinkNode->NextLinkNode;
}
}
/* The item is not already in the list, so add it */
Error = InsertObject( ListToAddTo,
ItemLocation,
ItemTag,
TargetHandle,
Insert_Mode,
MakeCurrent,
Handle);
/* All items in the list have been processed. */
return Error;
}
/*********************************************************************/
/* */
/* Function Name: DeleteObject */
/* */
/* Descriptive Name: This function removes the specified object */
/* from the list. */
/* */
/* Input: dlist_t ListToDeleteFrom : The list whose current */
/* item is to be deleted. */
/* ADDRESS Object : The address of the object to be removed*/
/* from the list. */
/* */
/* Output: Return DLIST_SUCCESS if successful, else an error code.*/
/* */
/* Error Handling: This function will fail if ListToDeleteFrom is */
/* not a valid list, or if ListToDeleteFrom is */
/* empty, or if Handle is invalid. */
/* If this routine fails, an error code is */
/* returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: This function does not alter which item is the current */
/* item in the list, unless the handle specified belongs */
/* to the current item in the list. */
/* */
/*********************************************************************/
int DeleteObject (dlist_t ListToDeleteFrom,
ADDRESS Object){
/* Since ListToProcess is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToProcess. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item while we access its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
int Error;
ADDRESS TmpObject;
/* We will assume that ListToProcess points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToDeleteFrom;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Assume success. */
Error = DLIST_OBJECT_NOT_FOUND;
/* Check for empty list. */
if (ListData->ItemCount == 0)
{
return DLIST_OBJECT_NOT_FOUND;
}
/* Get the first link node in the list. */
CurrentLinkNode = ListData->StartOfList;
/* Now loop through the items in the list. */
while ( CurrentLinkNode != NULL )
{
if ( CurrentLinkNode->DataLocation == Object )
{
Error = ExtractObject(ListToDeleteFrom, CurrentLinkNode->DataTag, CurrentLinkNode, &TmpObject);
break;
} else{
CurrentLinkNode = CurrentLinkNode->NextLinkNode;
}
}
/* All items in the list have been processed. */
return Error;
}
/*********************************************************************/
/* */
/* Function Name: DeleteAllItems */
/* */
/* Descriptive Name: This function deletes all of the items in the*/
/* specified list and optionally frees the */
/* memory associated with each item deleted. */
/* */
/* Input: dlist_t ListToDeleteFrom : The list whose items */
/* are to be deleted. */
/* boolean FreeMemory : If TRUE, then the memory */
/* associated with each item in the*/
/* list will be freed. If FALSE */
/* then the each item will be */
/* removed from the list but its */
/* memory will not be freed. */
/* */
/* Output: Return DLIST_SUCCESS if successful, else an error code.*/
/* */
/* Error Handling: This function will fail if ListToDeleteFrom is */
/* not a valid list, or if ListToDeleteFrom is */
/* empty. */
/* If this routine fails, an error code is */
/* returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: Items in a list can be accessed in two ways: A copy of */
/* the item can be obtained using GetObject and its related*/
/* calls, or a pointer to the item can be obtained using */
/* GetObject and its related calls. If you have a copy of */
/* the data and wish to remove the item from the list, set */
/* FreeMemory to TRUE. This will remove the item from the */
/* list and deallocate the memory used to hold it. If you */
/* have a pointer to the item in the list (from one of the */
/* GetObject style functions) and wish to remove the item */
/* from the list, set FreeMemory to FALSE. This removes */
/* the item from the list without freeing its memory, so */
/* that the pointer obtained with the GetObject style */
/* functions is still useable. */
/* */
/*********************************************************************/
int DeleteAllItems (dlist_t ListToDeleteFrom,
boolean FreeMemory)
{
/* Since ListToDeleteFrom is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToDeleteFrom. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* This is used to walk through the
linked list of LinkNodes. */
/* We will assume that ListToDeleteFrom points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) (ListToDeleteFrom);
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/*--------------------------------------------------
To empty a DLIST, we must traverse the linked
list of LinkNodes and dispose of each LinkNode,
as well as the data item associated with each
LinkNode.
--------------------------------------------------*/
/* Loop to dispose of the ListNodes. */
while (ListData->ItemCount > 0)
{
CurrentLinkNode = ListData->StartOfList; /* Get the first dlist_t node. */
ListData->StartOfList = CurrentLinkNode->NextLinkNode; /* Remove that dlist_t node from the DLIST. */
ListData->ItemCount--; /* Decrement the number of items in the list or we will never leave the loop! */
if ( (CurrentLinkNode->DataLocation != NULL) && FreeMemory )
{
free(CurrentLinkNode->DataLocation);
}
CurrentLinkNode->ControlNodeLocation = NULL;
free(CurrentLinkNode);
}
/* Since there are no items in the list, set the CurrentItem and EndOfList pointers to NULL. */
ListData->CurrentItem = NULL;
ListData->EndOfList = NULL;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: GetObject */
/* */
/* Descriptive Name: This function returns the address of the data*/
/* associated with the specified item in the */
/* list. */
/* */
/* Input: dlist_t ListToGetItemFrom : The list whose current item */
/* is to have its address */
/* returned to the caller. */
/* TAG ItemTag : What the caller thinks the item tag */
/* of the current item is. */
/* ADDRESS Handle : The handle of the item to get. This */
/* handle must be of an item which resides*/
/* in ListToGetItemFrom, or NULL. If */
/* NULL, then the current item in the list*/
/* boolean MakeCurrent : If TRUE, the item to get will */
/* become the current item in the */
/* list. */
/* ADDRESS * Object : The address of a variable to hold */
/* the ADDRESS of data associated */
/* with the current item. */
/* */
/* Output: If Successful : */
/* Return DLIST_SUCCESS. */
/* *Object will be the address of the data */
/* associated with the current item in the list. */
/* If Failure : */
/* Return an error code. */
/* *Object will be NULL. */
/* */
/* Error Handling: This function will fail under any of the */
/* following conditions: */
/* ListToGetItemFrom is not a valid list */
/* ItemTag does not match the item tag */
/* of the current item in the list */
/* Handle is invalid, or is for an item */
/* which is not in ListToGetItemFrom */
/* If any of these conditions occur, an error code */
/* will be returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The user should not free the memory associated with */
/* the address returned by this function as the object is */
/* still in the list. */
/* */
/* It is assumed that Handle is valid, or is at least the */
/* address of an accessible block of storage. If Handle */
/* is invalid, or is not the address of an accessible block*/
/* of storage, then a trap or exception may occur. */
/* NOTE: For this function, NULL is considered a valid */
/* handle designating the current item in the list. */
/* */
/* It is assumed that Object is a valid address. If not, */
/* an exception or trap may occur. */
/* */
/* This function does not alter which item is the current */
/* item in the list. */
/* */
/*********************************************************************/
int GetObject(dlist_t ListToGetItemFrom,
TAG ItemTag,
ADDRESS Handle,
boolean MakeCurrent,
ADDRESS * Object)
{
/* Since ListToGetItemFrom is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToGetItemFrom. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the item to return. */
/* Initialize the return address in case we bail out with an error. */
*Object = NULL;
/* We will assume that ListToGetItemFrom points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToGetItemFrom;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check to see if the dlist_t is empty. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Were we given a handle? */
if ( Handle != NULL )
{
/* Lets check the handle we were given.*/
CurrentLinkNode = (LinkNode*) Handle;
/* Is the handle valid? */
if ( CurrentLinkNode->ControlNodeLocation != ListData )
{
/* The handle is not valid! Abort! */
return DLIST_BAD_HANDLE;
}
}
else
{
/* Since we were not given a handle, we will use the current item in the list for this operation. */
CurrentLinkNode = ListData->CurrentItem;
}
/* We must check the actual item tag against the item tag expected by the user. A mismatch could lead to errors! */
if (CurrentLinkNode->DataTag != ItemTag)
{
return DLIST_ITEM_TAG_WRONG;
}
/* Does the user want this item made the current item in the list? */
if ( MakeCurrent )
{
/* Make this item the current item in the list. */
ListData->CurrentItem = CurrentLinkNode;
}
/* Since everything checks out, lets signal success and return the address of the data. */
*Object = CurrentLinkNode->DataLocation;
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: BlindGetObject */
/* */
/* Descriptive Name: This function returns the address of the data*/
/* associated with the specified item in the */
/* list. */
/* */
/* Input: dlist_t ListToGetItemFrom : The list whose current */
/* item is to have its address */
/* returned to the caller. */
/* TAG * ItemTag : The tag of the current item */
/* ADDRESS Handle : The handle of the item to get. This */
/* handle must be of an item which resides*/
/* in ListToGetItemFrom, or NULL. If */
/* NULL, then the current item in the list*/
/* boolean MakeCurrent : If TRUE, the item to get will */
/* become the current item in the */
/* list. */
/* ADDRESS * Object : The address of a variable to hold */
/* the ADDRESS of data associated */
/* with the current item. */
/* */
/* Output: If Successful : */
/* Return DLIST_SUCCESS. */
/* *Object will be the address of the data */
/* associated with the current item in the list. */
/* If Failure : */
/* Return an error code. */
/* *Object will be NULL. */
/* */
/* Error Handling: This function will fail under any of the */
/* following conditions: */
/* ListToGetItemFrom is not a valid list */
/* ItemTag does not match the item tag */
/* of the current item in the list */
/* Handle is invalid, or is for an item */
/* which is not in ListToGetItemFrom */
/* If any of these conditions occur, an error code */
/* will be returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The user should not free the memory associated with */
/* the address returned by this function as the object is */
/* still in the list. */
/* */
/* It is assumed that Handle is valid, or is at least the */
/* address of an accessible block of storage. If Handle */
/* is invalid, or is not the address of an accessible block*/
/* of storage, then a trap or exception may occur. */
/* NOTE: For this function, NULL is considered a valid */
/* handle designating the current item in the list. */
/* */
/* It is assumed that Object is a valid address. If not, */
/* an exception or trap may occur. */
/* */
/* This function does not alter which item is the current */
/* item in the list. */
/* */
/*********************************************************************/
int BlindGetObject(dlist_t ListToGetItemFrom,
TAG * ItemTag,
ADDRESS Handle,
boolean MakeCurrent,
ADDRESS * Object){
/* Since ListToGetItemFrom is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToGetItemFrom. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the item to return. */
/* Initialize the return address in case we bail out with an error. */
*Object = NULL;
/* We will assume that ListToGetItemFrom points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToGetItemFrom;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check to see if the dlist_t is empty. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Were we given a handle? */
if ( Handle != NULL )
{
/* Lets check the handle we were given.*/
CurrentLinkNode = (LinkNode*) Handle;
/* Is the handle valid? */
if ( CurrentLinkNode->ControlNodeLocation != ListData )
{
/* The handle is not valid! Abort! */
return DLIST_BAD_HANDLE;
}
}
else
{
/* Since we were not given a handle, we will use the current item in the list for this operation. */
CurrentLinkNode = ListData->CurrentItem;
}
/* Return the tag to the user */
*ItemTag = CurrentLinkNode->DataTag;
/* Does the user want this item made the current item in the list? */
if ( MakeCurrent )
{
/* Make this item the current item in the list. */
ListData->CurrentItem = CurrentLinkNode;
}
/* Since everything checks out, lets signal success and return the address of the data. */
*Object = CurrentLinkNode->DataLocation;
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: GetNextObject */
/* */
/* Descriptive Name: This function advances the current item */
/* pointer and then returns the address of the */
/* data associated with the current item in the */
/* list. */
/* */
/* Input: dlist_t ListToGetItemFrom : The list whose current item */
/* is to be copied and returned*/
/* to the caller. */
/* TAG ItemTag : What the caller thinks the item tag */
/* of the current item is. */
/* ADDRESS * Object : The address of a variable to hold */
/* the ADDRESS of data associated */
/* with the next item. */
/* */
/* Output: If Successful : */
/* Return DLIST_SUCCESS. */
/* *Object will be the address of the data */
/* associated with the current item in the list. */
/* If Failure : */
/* Return an error code. */
/* *Object will be NULL. */
/* The current item pointer will NOT be advanced. */
/* The current item in the list will be the same */
/* as before the call to this function. */
/* */
/* Error Handling: This function will fail under any of the */
/* following conditions: */
/* ListToGetItemFrom is not a valid list */
/* ItemTag does not match the item tag */
/* of the current item in the list */
/* The current item in the list before this */
/* function is called is the last item */
/* item in the list. */
/* If any of these conditions occur, an error code */
/* will be returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The user should not free the memory associated with */
/* the address returned by this function as the object is */
/* still in the list. */
/* */
/* It is assumed that Object is a valid address. If not, */
/* an exception or trap may occur. */
/* */
/*********************************************************************/
int GetNextObject(dlist_t ListToGetItemFrom,
TAG ItemTag,
ADDRESS * Object)
{
/* Since ListToGetItemFrom is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToAdvance. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item while we access its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
LinkNode * OriginalCurrentLinkNode; /* This is used to hold the value
of the Current Item pointer
as it was upon entry to this
function. If there is an
error, the Current Item
pointer will be reset to the
value stored in this variable. */
/* Initialize the return address in case we bail out with an error. */
*Object = NULL;
/* We will assume that ListToGetItemFrom points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToGetItemFrom;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check for empty list. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Check for end of list. */
if (ListData->CurrentItem == ListData->EndOfList)
{
return DLIST_END_OF_LIST;
}
/* Save the current item pointer so that we can restore it should something
go wrong later. */
OriginalCurrentLinkNode = ListData->CurrentItem;
/* Advance the current item pointer. */
CurrentLinkNode = ListData->CurrentItem;
ListData->CurrentItem = CurrentLinkNode->NextLinkNode;
/* Lets get the current node from the DLIST. */
CurrentLinkNode = ListData->CurrentItem;
/* We must check the actual item tag against the item tag expected by the user. A mismatch could lead to errors! */
if (CurrentLinkNode->DataTag != ItemTag)
{
ListData->CurrentItem = OriginalCurrentLinkNode;
return DLIST_ITEM_TAG_WRONG;
}
/* Since everything checks out, lets signal success and return the address of the data. */
*Object = CurrentLinkNode->DataLocation;
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: ExtractObject */
/* */
/* Descriptive Name: This function returns the address of the data*/
/* associated with the specified item in the */
/* list and then removes that item from the list*/
/* */
/* Input: dlist_t ListToGetItemFrom : The list whose current item */
/* is to be copied and returned*/
/* to the caller. */
/* TAG ItemTag : What the caller thinks the item tag */
/* of the current item is. */
/* ADDRESS Handle : The handle of the item to get. This */
/* handle must be of an item which resides*/
/* in ListToGetItemFrom, or NULL. If */
/* NULL, then the current item in the */
/* list will be used. */
/* ADDRESS * Object : The address of a variable to hold */
/* the ADDRESS of data associated */
/* with the current item. */
/* */
/* Output: If Successful : */
/* Return DLIST_SUCCESS. */
/* *Object will be the address of the data */
/* associated with the current item in the list. */
/* If Failure : */
/* Return an error code. */
/* *Object will be NULL. */
/* */
/* Error Handling: This function will fail under any of the */
/* following conditions: */
/* ListToGetItemFrom is not a valid list */
/* ItemTag does not match the item tag */
/* of the current item in the list */
/* Handle is invalid, or is for an item */
/* which is not in ListToGetItemFrom */
/* If any of these conditions occur, an error code */
/* will be returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The user is responsible for the memory associated with */
/* the address returned by this function since this */
/* function removes that object from the list. This means */
/* that, when the user is through with the object, they */
/* should free it. */
/* */
/* It is assumed that Handle is valid, or is at least the */
/* address of an accessible block of storage. If Handle */
/* is invalid, or is not the address of an accessible block*/
/* of storage, then a trap or exception may occur. */
/* NOTE: For this function, NULL is considered a valid */
/* handle which refers to the current item in the */
/* list. */
/* */
/* It is assumed that Object is a valid address. If not, */
/* an exception or trap may occur. */
/* */
/* This function does not alter which item is the current */
/* item in the list, unless the handle specified belongs */
/* to the current item in the list, in which case the */
/* item following the current item becomes the current */
/* item in the list. If there is no item following the */
/* current item in the list, then the item proceeding the */
/* current item will become the current item in the list. */
/* */
/*********************************************************************/
int ExtractObject(dlist_t ListToGetItemFrom,
TAG ItemTag,
ADDRESS Handle,
ADDRESS * Object)
{
/* Since ListToGetItemFrom is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToGetItemFrom. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the current item. */
void * DataLocation; /* Used to store the address that will be returned
as the function value. */
LinkNode * NextLinkNode; /* Used to point to the item immediately following
the one being extracted so that its fields can
be updated. */
LinkNode * PreviousLinkNode; /* Used to point to the item immediately before
the one being deleted so that its fields can
be updated. */
/* Initialize the return address in case we bail out with an error. */
*Object = NULL;
/* We will assume that ListToGetItemFrom points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToGetItemFrom;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check to see if the dlist_t is empty. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Were we given a handle? */
if ( Handle != NULL )
{
/* Lets check the handle we were given.*/
CurrentLinkNode = (LinkNode*) Handle;
/* Is the handle valid? */
if ( CurrentLinkNode->ControlNodeLocation != ListData )
{
/* The handle is not valid! Abort! */
return DLIST_BAD_HANDLE;
}
}
else
{
/* Since we were not given a handle, we will use the current item in the list for this operation. */
CurrentLinkNode = ListData->CurrentItem;
}
/* We must check the actual item tag against the item tag expected by the user. A mismatch could lead to errors! */
if (CurrentLinkNode->DataTag != ItemTag)
{
return DLIST_ITEM_TAG_WRONG;
}
/* Since everything checks out, lets store the address of the data so that we can return it later. */
DataLocation = CurrentLinkNode->DataLocation;
/* Now we must remove the current item from the DLIST. */
/* Find the next and previous link nodes in the source list. */
NextLinkNode = CurrentLinkNode->NextLinkNode;
PreviousLinkNode = CurrentLinkNode->PreviousLinkNode;
/* Take the current node out of the source list. */
if (PreviousLinkNode != NULL)
{
/* The current item was not the first item in the list. */
/* Remove the current item from the list. */
PreviousLinkNode->NextLinkNode = NextLinkNode;
}
if ( NextLinkNode != NULL )
{
/* The current item was not the last item in the list. */
NextLinkNode->PreviousLinkNode = PreviousLinkNode;
}
/* Was the current link node the first item in the list? */
if ( ListData->StartOfList == CurrentLinkNode )
ListData->StartOfList = NextLinkNode;
/* Was the current link node the last item in the list?*/
if ( ListData->EndOfList == CurrentLinkNode )
ListData->EndOfList = PreviousLinkNode;
/* Was the node being extracted the current item in the list? */
if ( ListData->CurrentItem == CurrentLinkNode )
{
/* The current item in the list will be the item which follows the
item we are extracting. If the item being extracted is the last
item in the list, then the current item becomes the item immediately
before the item being extracted. If there are no items before or
after the item being extracted, then the list is empty! */
if ( NextLinkNode != NULL )
ListData->CurrentItem = NextLinkNode;
else
if ( PreviousLinkNode != NULL )
ListData->CurrentItem = PreviousLinkNode;
else
ListData->CurrentItem = NULL;
}
/* Adjust the count of items in the list. */
ListData->ItemCount = ListData->ItemCount - 1;
/* Now we must free the memory associated with the current node. */
CurrentLinkNode->ControlNodeLocation = NULL;
free(CurrentLinkNode);
/* Time to return the address of the data. */
*Object = DataLocation;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: BlindExtractObject */
/* */
/* Descriptive Name: This function returns the address of the data*/
/* associated with the specified item in the */
/* list and then removes that item from the list*/
/* */
/* Input: dlist_t ListToGetItemFrom : The list whose current */
/* item is to be copied and */
/* returned to the caller. */
/* TAG * ItemTag : The tag of the current item */
/* ADDRESS Handle : The handle of the item to get. This */
/* handle must be of an item which resides*/
/* in ListToGetItemFrom, or NULL. If */
/* NULL, then the current item in the */
/* list will be used. */
/* ADDRESS * Object : The address of a variable to hold */
/* the ADDRESS of data associated */
/* with the current item. */
/* */
/* Output: If Successful : */
/* Return DLIST_SUCCESS. */
/* *Object will be the address of the data */
/* associated with the current item in the list. */
/* If Failure : */
/* Return an error code. */
/* *Object will be NULL. */
/* */
/* Error Handling: This function will fail under any of the */
/* following conditions: */
/* ListToGetItemFrom is not a valid list */
/* ItemTag does not match the item tag */
/* of the current item in the list */
/* Handle is invalid, or is for an item */
/* which is not in ListToGetItemFrom */
/* If any of these conditions occur, an error code */
/* will be returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The user is responsible for the memory associated with */
/* the address returned by this function since this */
/* function removes that object from the list. This means */
/* that, when the user is through with the object, they */
/* should free it. */
/* */
/* It is assumed that Handle is valid, or is at least the */
/* address of an accessible block of storage. If Handle */
/* is invalid, or is not the address of an accessible block*/
/* of storage, then a trap or exception may occur. */
/* NOTE: For this function, NULL is considered a valid */
/* handle which refers to the current item in the */
/* list. */
/* */
/* It is assumed that Object is a valid address. If not, */
/* an exception or trap may occur. */
/* */
/* This function does not alter which item is the current */
/* item in the list, unless the handle specified belongs */
/* to the current item in the list, in which case the */
/* item following the current item becomes the current */
/* item in the list. If there is no item following the */
/* current item in the list, then the item preceding the */
/* current item will become the current item in the list. */
/* */
/*********************************************************************/
int BlindExtractObject(dlist_t ListToGetItemFrom,
TAG * ItemTag,
ADDRESS Handle,
ADDRESS * Object){
/* Since ListToGetItemFrom is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToGetItemFrom. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the current item. */
void * DataLocation; /* Used to store the address that will be returned
as the function value. */
LinkNode * NextLinkNode; /* Used to point to the item immediately following
the one being extracted so that its fields can
be updated. */
LinkNode * PreviousLinkNode; /* Used to point to the item immediately before
the one being deleted so that its fields can
be updated. */
/* Initialize the return address in case we bail out with an error. */
*Object = NULL;
/* We will assume that ListToGetItemFrom points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToGetItemFrom;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check to see if the dlist_t is empty. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Were we given a handle? */
if ( Handle != NULL )
{
/* Lets check the handle we were given.*/
CurrentLinkNode = (LinkNode*) Handle;
/* Is the handle valid? */
if ( CurrentLinkNode->ControlNodeLocation != ListData )
{
/* The handle is not valid! Abort! */
return DLIST_BAD_HANDLE;
}
}
else
{
/* Since we were not given a handle, we will use the current item in the list for this operation. */
CurrentLinkNode = ListData->CurrentItem;
}
/* return the Tag to the user. */
*ItemTag = CurrentLinkNode->DataTag;
/* Since everything checks out, lets store the address of the data so that we can return it later. */
DataLocation = CurrentLinkNode->DataLocation;
/* Now we must remove the current item from the DLIST. */
/* Find the next and previous link nodes in the source list. */
NextLinkNode = CurrentLinkNode->NextLinkNode;
PreviousLinkNode = CurrentLinkNode->PreviousLinkNode;
/* Take the current node out of the source list. */
if (PreviousLinkNode != NULL)
{
/* The current item was not the first item in the list. */
/* Remove the current item from the list. */
PreviousLinkNode->NextLinkNode = NextLinkNode;
}
if ( NextLinkNode != NULL )
{
/* The current item was not the last item in the list. */
NextLinkNode->PreviousLinkNode = PreviousLinkNode;
}
/* Was the current link node the first item in the list? */
if ( ListData->StartOfList == CurrentLinkNode )
ListData->StartOfList = NextLinkNode;
/* Was the current link node the last item in the list?*/
if ( ListData->EndOfList == CurrentLinkNode )
ListData->EndOfList = PreviousLinkNode;
/* Was the node being extracted the current item in the list? */
if ( ListData->CurrentItem == CurrentLinkNode )
{
/* The current item in the list will be the item which follows the
item we are extracting. If the item being extracted is the last
item in the list, then the current item becomes the item immediately
before the item being extracted. If there are no items before or
after the item being extracted, then the list is empty! */
if ( NextLinkNode != NULL )
ListData->CurrentItem = NextLinkNode;
else
if ( PreviousLinkNode != NULL )
ListData->CurrentItem = PreviousLinkNode;
else
ListData->CurrentItem = NULL;
}
/* Adjust the count of items in the list. */
ListData->ItemCount = ListData->ItemCount - 1;
/* Now we must free the memory associated with the current node. */
CurrentLinkNode->ControlNodeLocation = NULL;
free(CurrentLinkNode);
/* Time to return the address of the data. */
*Object = DataLocation;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: ReplaceObject */
/* */
/* Descriptive Name: This function replaces the specified object */
/* in the list with the one provided as its */
/* argument. */
/* */
/* Input: dlist_t ListToReplaceItemIn : The list whose current */
/* object is to be replaced */
/* ADDRESS ItemLocation : The address of the replacement */
/* item */
/* TAG ItemTag : The item tag that the user wishes to */
/* associate with the replacement item */
/* ADDRESS Handle : The handle of the item to get. This */
/* handle must be of an item which resides */
/* in ListToGetItemFrom, or NULL. If NULL */
/* then the current item in the list will */
/* be used. */
/* boolean MakeCurrent : If TRUE, the item to get will */
/* become the current item in the */
/* list. */
/* ADDRESS * Object : The address of a variable to hold */
/* the ADDRESS of the object that */
/* was replaced. */
/* */
/* Output: If Successful then return DLIST_SUCCESS and the */
/* *Object will contain the address of the object that */
/* was replaced. */
/* If Unsuccessful, then return an error code and */
/* *Object will be NULL. */
/* */
/* Error Handling: This function will fail under the following */
/* conditions: */
/* ListToReplaceItemIn is empty */
/* ItemLocation is NULL */
/* The memory required can not be allocated. */
/* Handle is invalid, or is for an item */
/* which is not in ListToGetItemFrom */
/* If any of these conditions occurs, an error */
/* code will be returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The user is responsible for the memory associated with */
/* the object returned by this function as that object is */
/* removed from the list. This means that, when the user */
/* is through with the object returned by this function, */
/* they should free it. */
/* */
/* It is assumed that if ItemLocation is not NULL, then */
/* it is a valid address that can be dereferenced. If */
/* these assumptions are violated, an exception or trap */
/* may occur. */
/* */
/* It is assumed that Handle is valid, or is at least the */
/* address of an accessible block of storage. If Handle */
/* is invalid, or is not the address of an accessible block*/
/* of storage, then a trap or exception may occur. */
/* NOTE: For this function, NULL is a valid handle for the */
/* current item in the list. */
/* */
/* It is assumed that Object is a valid address. If not, */
/* an exception or trap may occur. */
/* */
/* This function does not alter which item is the current */
/* item in the list. */
/* */
/*********************************************************************/
int ReplaceObject(dlist_t ListToReplaceItemIn,
ADDRESS ItemLocation,
TAG * ItemTag, /* On input - TAG of new object. On return = TAG of old object. */
ADDRESS Handle,
boolean MakeCurrent,
ADDRESS * Object)
{
/* Since ListToReplaceItemIn is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToReplaceItemIn. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
item while we replace its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
TAG OldItemTag;
ADDRESS OldItemLocation;
/* Initialize the return address in case we bail out with an error. */
*Object = NULL;
/* We will assume that ListToReplaceItemIn points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToReplaceItemIn;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
/* Lets check the replacement data. */
if (ItemLocation == NULL)
{
return DLIST_BAD_ITEM_POINTER;
}
#endif
/* Check to see if the dlist_t is empty. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Were we given a handle? */
if ( Handle != NULL )
{
/* Lets check the handle we were given.*/
CurrentLinkNode = (LinkNode*) Handle;
/* Is the handle valid? */
if ( CurrentLinkNode->ControlNodeLocation != ListData )
{
/* The handle is not valid! Abort! */
return DLIST_BAD_HANDLE;
}
}
else
{
/* Since we were not given a handle, we will use the current item in the list for this operation. */
CurrentLinkNode = ListData->CurrentItem;
}
/* Save the old values of DataTag, and DataLocation for return to
the caller. */
OldItemTag = CurrentLinkNode->DataTag;
OldItemLocation = CurrentLinkNode->DataLocation;
/* Now lets put our replacement into the list. */
CurrentLinkNode->DataTag = *ItemTag;
CurrentLinkNode->DataLocation = ItemLocation;
/* Setup return values for user. */
*ItemTag = OldItemTag;
/* Did the user want this item to become the current item in the list? */
if ( MakeCurrent )
{
/* Make this item the current item in the list. */
ListData->CurrentItem = CurrentLinkNode;
}
/* Signal success. */
*Object = OldItemLocation;
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: GetHandle */
/* */
/* Descriptive Name: This function returns a handle for the */
/* current item in the list. This handle is */
/* then associated with that item regardless of */
/* its position in the list. This handle can be*/
/* used to make its associated item the current */
/* item in the list. */
/* */
/* Input: dlist_t ListToGetHandleFrom : The list from which a */
/* handle is needed. */
/* ADDRESS * Handle : The address of a variable to hold */
/* the handle */
/* */
/* Output: If successful, the function returns DLIST_SUCCESS and */
/* *Handle is set to the handle for the current item */
/* in ListToGetHandleFrom. */
/* If unsuccessful, an error code is returned and *Handle */
/* is set to 0. */
/* */
/* Error Handling: This function will fail if ListToGetHandleFrom */
/* is not a valid list or is an empty list. In */
/* either of these cases, an error code is */
/* returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: The handle returned is a pointer to the LinkNode of the */
/* current item in the list. This allows the item to move */
/* around in the list without losing its associated handle.*/
/* However, if the item is deleted from the list, then the */
/* handle is invalid and its use could result in a trap. */
/* */
/*********************************************************************/
int GetHandle (dlist_t ListToGetHandleFrom,
ADDRESS * Handle)
{
/* Since ListToGetHandleFrom is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToGetHandleFrom. This way we just do the
cast once. */
ControlNode * ListData;
/* Initialize the return variable in case we bail out with an error. */
*Handle = NULL;
/* We will assume that ListToGetHandleFrom points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToGetHandleFrom;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check to see if the dlist_t is empty. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Return the address of the CurrentItem. This will serve as the handle. */
*Handle = ListData->CurrentItem;
/* Indicate success */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: GetListSize */
/* */
/* Descriptive Name: This function returns the number of items in */
/* a list. */
/* */
/* Input: dlist_t ListToGetSizeOf : The list whose size we wish to*/
/* know */
/* uint * Size : The address of a variable to hold */
/* the size of the list. */
/* */
/* Output: If successful, the function returns DLIST_SUCCESS and */
/* *Size contains the a count of the number of items */
/* in the list. */
/* If unsuccessful, an error code is returned and *Size */
/* is set to 0. */
/* */
/* Error Handling: This function will fail if ListToGetSizeOf is */
/* not a valid list. If this happens, then an */
/* error code is returned. . */
/* */
/* Side Effects: None. */
/* */
/* Notes: It is assumed that Size contains a valid address. If */
/* this assumption is violated, an exception or trap */
/* may occur. */
/* */
/*********************************************************************/
int GetListSize(dlist_t ListToGetSizeOf,
uint * Size)
{
/* Since ListToGetSizeOf is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToGetSizeOf. This way we just do the
cast once. */
ControlNode * ListData;
/* Initialize the return variable in case we bail out with an error. */
*Size = 0;
/* We will assume that ListToGetSizeOf points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToGetSizeOf;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Return the size of the list. */
*Size = ListData->ItemCount;
/* Indicate success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: ListEmpty */
/* */
/* Descriptive Name: This function returns TRUE if the */
/* specified list is empty, otherwise it returns*/
/* FALSE. */
/* */
/* Input: dlist_t ListToCheck : The list to check to see if it */
/* is empty */
/* */
/* Output: If successful, the function returns TRUE if the */
/* number of items in the list is 0, otherwise it */
/* returns FALSE. */
/* If unsuccessful, the function returns TRUE. */
/* */
/* Error Handling: This function will return TRUE if ListToCheck */
/* is not a valid list. */
/* */
/* Side Effects: None. */
/* */
/*********************************************************************/
boolean ListEmpty(dlist_t ListToCheck)
{
/* Since ListToCheck is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToCheck. This way we just do the
cast once. */
ControlNode * ListData;
/* We will assume that ListToCheck points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToCheck;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
/* DLIST_NOT_INITIALIZED */
return TRUE;
}
#endif
/* Check to see if the dlist_t is empty. */
if (ListData->ItemCount == 0)
return (TRUE);
else
return (FALSE);
}
/*********************************************************************/
/* */
/* Function Name: AtEndOfList */
/* */
/* Descriptive Name: This function returns TRUE if the */
/* current item in the list is the last item */
/* in the list. Returns FALSE otherwise. */
/* */
/* Input: dlist_t ListToCheck : The list to check. */
/* */
/* Output: If successful, the function returns TRUE if the */
/* current item in the list is the last item in the */
/* list. If it is not the last item in the list, */
/* FALSE is returned. */
/* If unsuccessful, the function returns FALSE. */
/* */
/* Error Handling: This function will return FALSE ListToCheck is */
/* not a valid list. */
/* */
/* Side Effects: None. */
/* */
/*********************************************************************/
boolean AtEndOfList(dlist_t ListToCheck)
{
/* Since ListToCheck is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToCheck. This way we just do the
cast once. */
ControlNode * ListData;
/* We will assume that ListToCheck points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToCheck;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
/* DLIST_NOT_INITIALIZED */
return FALSE;
}
#endif
/* Check to see if the current item in the list is also the last item in the list. */
if (ListData->CurrentItem == ListData->EndOfList)
return (TRUE);
else
return (FALSE);
}
/*********************************************************************/
/* */
/* Function Name: DestroyList */
/* */
/* Descriptive Name: This function releases the memory associated */
/* with the internal data structures of a DLIST.*/
/* Once a dlist_t has been destroyed by this */
/* function, it must be reinitialized before it */
/* can be used again. */
/* */
/* Input: dlist_t ListToDestroy : The list to be eliminated */
/* from memory. */
/* boolean FreeItemMemory : If TRUE, all items in the list */
/* will be freed. If FALSE, all */
/* items in the list are not */
/* freed, only the list structures*/
/* associated with them are. */
/* */
/* Output: If successful, return DLIST_SUCCESS */
/* If unsuccessful, return an error code. */
/* */
/* Error Handling: This function will fail if ListToDestroy is not */
/* a valid list. If this happens, then an error */
/* code is returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: If FreeItemMemory is TRUE, then this function will try */
/* to delete any items which may be in the list. However, */
/* since this function has no way of knowing the internal */
/* structure of an item, items which contain embedded */
/* pointers will not be entirely freed. This can lead to */
/* memory leaks. The programmer should ensure that any */
/* list passed to this function when the FreeItemMemory */
/* parameter is TRUE is empty or does not contain any */
/* items with embedded pointers. */
/* */
/*********************************************************************/
int DestroyList(dlist_t * ListToDestroy,
boolean FreeItemMemory)
{
/* Since ListToDestroy is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToDestroy. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* This is used to walk through the
linked list of LinkNodes. */
/* We will assume that ListToDestroy points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) (*ListToDestroy);
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/*--------------------------------------------------
To dispose of a DLIST, we must traverse the linked
list of LinkNodes and dispose of each LinkNode,
as well as the data item associated with each
LinkNode. Once all of the LinkNodes (and their
data items) have been freed, we can then free the
ControlNode.
--------------------------------------------------*/
/* Loop to dispose of the ListNodes. */
while (ListData->ItemCount > 0)
{
CurrentLinkNode = ListData->StartOfList; /* Get the first dlist_t node. */
ListData->StartOfList = CurrentLinkNode->NextLinkNode; /* Remove that dlist_t node from the DLIST. */
ListData->ItemCount--; /* Decrement the number of items in the list or we will never leave the loop! */
if ( (CurrentLinkNode->DataLocation != NULL) && FreeItemMemory )
{
free(CurrentLinkNode->DataLocation);
}
CurrentLinkNode->ControlNodeLocation = NULL;
free(CurrentLinkNode);
}
#ifdef EVMS_DEBUG
/* Set Verify to 0 so that, if the same block of
memory is reused for another list, the InitializeList
function does not get fooled into thinking that
the block of memory already contains a valid list. */
ListData->Verify = 0;
#endif
/* Now free the memory used to store the master dlist_t node. */
free(*ListToDestroy);
*ListToDestroy = NULL;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: NextItem */
/* */
/* Descriptive Name: This function makes the next item in the list*/
/* the current item in the list (i.e. it */
/* advances the current item pointer). */
/* */
/* Input: dlist_t ListToAdvance : The list whose current item */
/* pointer is to be advanced */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* If unsuccessful, return error code. */
/* */
/* Error Handling: This function will fail under the following */
/* conditions: */
/* ListToAdvance is not a valid list */
/* ListToAdvance is empty */
/* The current item is the last item in the */
/* list */
/* If any of these conditions occurs, then an */
/* error code is returned. */
/* */
/* Side Effects: None. */
/* */
/*********************************************************************/
int NextItem(dlist_t ListToAdvance)
{
/* Since ListToAdvance is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToAdvance. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item while we access its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
/* We will assume that ListToAdvance points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToAdvance;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check for empty list. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Check for end of list. */
if (ListData->CurrentItem == ListData->EndOfList)
{
return DLIST_END_OF_LIST;
}
/* Advance the current item pointer. */
CurrentLinkNode = ListData->CurrentItem;
ListData->CurrentItem = CurrentLinkNode->NextLinkNode;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: PreviousItem */
/* */
/* Descriptive Name: This function makes the previous item in the */
/* list the current item in the list. */
/* */
/* Input: dlist_t ListToChange : The list whose current item */
/* pointer is to be changed */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* If unsuccessful, return an error code. */
/* */
/* Error Handling: This function will fail under the following */
/* conditions: */
/* ListToChange is not a valid list */
/* ListToChange is empty */
/* The current item is the first item in the */
/* list */
/* If any of these conditions occurs, then return */
/* an error code. */
/* */
/* Side Effects: None. */
/* */
/*********************************************************************/
int PreviousItem(dlist_t ListToChange)
{
/* Since ListToAdvance is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToAdvance. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item while we access its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
/* We will assume that ListToChange points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToChange;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Check for empty list. */
if (ListData->ItemCount == 0)
{
return DLIST_EMPTY;
}
/* Check for beginning of list. */
if (ListData->CurrentItem == ListData->StartOfList)
{
return DLIST_ALREADY_AT_START;
}
/* Position the current item pointer. */
CurrentLinkNode = ListData->CurrentItem;
ListData->CurrentItem = CurrentLinkNode->PreviousLinkNode;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: GoToStartOfList */
/* */
/* Descriptive Name: This function makes the first item in the */
/* list the current item in the list. */
/* */
/* Input: dlist_t ListToReset : The list whose current item */
/* is to be set to the first item */
/* in the list */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* If unsuccessful, return an error code */
/* */
/* Error Handling: This function will fail if ListToAdvance is not */
/* a valid list. If this occurs, then an error */
/* code is returned. */
/* */
/* Side Effects: None. */
/* */
/*********************************************************************/
int GoToStartOfList(dlist_t ListToReset)
{
/* Since ListToReset is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToReset. This way we just do the
cast once. */
ControlNode * ListData;
/* We will assume that ListToReset points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToReset;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Set the current item pointer. */
ListData->CurrentItem = ListData->StartOfList;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: GoToEndOfList */
/* */
/* Descriptive Name: This function makes the last item in the */
/* list the current item in the list. */
/* */
/* Input: dlist_t ListToSet : The list whose current item */
/* is to be set to the last item */
/* in the list */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* If unsuccessful, return an error code */
/* */
/* Error Handling: This function will fail if ListToAdvance is not */
/* a valid list. If this occurs, then an error */
/* code is returned. */
/* */
/* Side Effects: None. */
/* */
/*********************************************************************/
int GoToEndOfList(dlist_t ListToSet)
{
/* Since ListToSet is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToSet. This way we just do the
cast once. */
ControlNode * ListData;
/* We will assume that ListToSet points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToSet;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Set the current item pointer. */
ListData->CurrentItem = ListData->EndOfList;
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: GoToSpecifiedItem */
/* */
/* Descriptive Name: This function makes the item associated with */
/* Handle the current item in the list. */
/* */
/* Input: dlist_t ListToReposition: The list whose current item */
/* is to be set to the item */
/* associated with Handle. */
/* ADDRESS Handle : A handle obtained by using the */
/* GetHandle function. This handle */
/* identifies a unique item in the list. */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* If unsuccessful, return an error code */
/* */
/* Error Handling: This function will fail if ListToAdvance is not */
/* a valid list. If this occurs, then an error */
/* code is returned. */
/* */
/* Side Effects: None. */
/* */
/* Notes: It is assumed that Handle is a valid handle and that */
/* the item associated with Handle is still in the list. */
/* If these conditions are not met, an exception or trap */
/* may occur. */
/* */
/*********************************************************************/
int GoToSpecifiedItem(dlist_t ListToReposition,
ADDRESS Handle)
{
/* Since ListToReposition is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToReposition. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentNode = (LinkNode *) Handle; /* Used to minimize type casting
when manipulating and testing
the handle. */
/* We will assume that ListToReposition points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToReposition;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Since the list is valid, we must now see if the Handle is valid. We
will assume that, if the Handle is not NULL, it points to a LinkNode.
If the ControlNodeLocation field of the LinkNode points to the
ControlNode for ListToReposition, then the LinkNode is in ListToReposition
and can therefore become the current item in ListToReposition. */
if ( (CurrentNode != NULL ) &&
(CurrentNode->ControlNodeLocation == ListData) )
{
/* The handle pointed to a valid LinkNode which is in ListToReposition.
Lets make that node the current item in ListToReposition. */
ListData->CurrentItem = CurrentNode;
}
else
{
/* The handle was either NULL or the LinkNode it pointed to was not
in ListToReposition. */
return DLIST_BAD_HANDLE;
}
/* Signal success. */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: SortList */
/* */
/* Descriptive Name: This function sorts the contents of a list. */
/* The sorting algorithm used is a stable sort */
/* whose performance is not dependent upon the */
/* initial order of the items in the list. */
/* */
/* Input: dlist_t ListToSort : The dlist_t that is to be sorted. */
/* */
/* int (*Compare) ( ... ) */
/* */
/* This is a pointer to a function that can compare any */
/* two items in the list. It should return -1 if */
/* Object1 is less than Object2, 0 if Object1 is equal */
/* to Object2, and 1 if Object1 is greater than Object2.*/
/* This function will be called during the sort whenever*/
/* the sorting algorithm needs to compare two objects. */
/* */
/* The Compare function takes the following parameters: */
/* */
/* ADDRESS Object1 : The address of the data for the */
/* first object to be compared. */
/* TAG Object1Tag : The user assigned TAG value for the */
/* first object to be compared. */
/* ADDRESS Object2 : The address of the data for the */
/* second object to be compared. */
/* TAG Object2Tag : The user assigned TAG value for the */
/* second object to be compared. */
/* uint * Error : The address of a variable to */
/* hold the error return value. */
/* */
/* If this function ever sets *Error to a non-zero value*/
/* the sort will terminate and the error code will be */
/* returned to the caller of the SortList function. */
/* */
/* */
/* Output: If successful, this function will return DLIST_SUCCESS */
/* and ListToSort will have been sorted. */
/* If unsuccessful, an error code will be returned. */
/* The order of the items in ListToSort is undefined */
/* and may have changed. */
/* */
/* Error Handling: This function will terminate if *Compare sets */
/* *Error to a non-zero value, or if ListToSort */
/* is invalid. If this function does terminate in */
/* the middle of a sort, the order of the items in */
/* ListToSort may be different than it was before */
/* the function was called. */
/* */
/* Side Effects: None. */
/* */
/* Notes: This function works by breaking the list into sublists */
/* and merging the sublists back into one list. The size */
/* of the sublists starts at 1, and with each pass, the */
/* of the sublists is doubled. The sort ends when the size*/
/* of a sublist is greater than the size of the original */
/* list. */
/* */
/*********************************************************************/
int SortList(dlist_t ListToSort,
int (*Compare) (ADDRESS Object1,
TAG Object1Tag,
ADDRESS Object2,
TAG Object2Tag,
uint * Error))
{
ControlNode * ListData;
LinkNode * NodeToMove;
LinkNode * MergeList1;
uint MergeList1Size;
LinkNode * MergeList2;
uint MergeList2Size;
uint MergeListMaxSize;
uint ListSize;
int CompareResult;
int Error;
/* We will assume that ListToSort points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToSort;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* We will assume success until proven otherwise. */
Error = DLIST_SUCCESS;
/* Is the list big enough to sort? */
if ( ListData->ItemCount > 1)
{
/* The original list will be repeatedly broken into sublists, which are then
merged back into one list. This process is done two sublists at a time.
The two sublists are MergeList1 and MergeList2. Both sublists are the
same size. The only exception occurs when there are not enough items
remaining to create a MergeList2 of the same size as the MergeList1.
The size of MergeList1 and MergeList2 starts out at 1, and will be doubled
with each iteration of the outer "do" loop below. */
MergeListMaxSize = 1;
/* This is the outer "do" loop which controls the size of the sublists being
merged. The sublists are merged two at a time, with MergeList1 and
MergeList2 representing the two sublists being merged. */
do
{
/* The first sublist will always start with the first element of the
list being sorted. */
MergeList1 = ListData->StartOfList;
/* This loop controls the merging of sublists back into one list. */
do
{
/* The maximum number of items in each of the sublists to be merged
is MergeListMaxSize. As items are merged, they are removed from
the sublist they were in and placed in the single list which results
from the merging process. */
MergeList1Size = MergeListMaxSize;
MergeList2Size = MergeListMaxSize;
/* Find the start of the second list for merging. */
ListSize = MergeList1Size;
MergeList2 = MergeList1;
while ( ( MergeList2 != NULL ) && (ListSize > 0) )
{
MergeList2 = MergeList2->NextLinkNode;
ListSize--;
}
/* Now merge the two lists */
while ( (MergeList1 != NULL) && (MergeList2 != NULL) &&
(MergeList1Size > 0) && (MergeList2Size > 0) )
{
/* Compare the first item in MergeList1 with the first item in MergeList2. */
CompareResult = (*Compare)(MergeList1->DataLocation,MergeList1->DataTag,MergeList2->DataLocation,MergeList2->DataTag,&Error);
/* If there was an error during the comparison, bail out! */
if ( Error != DLIST_SUCCESS )
{
return Error;
}
/* See who gets moved. */
if ( CompareResult > 0 )
{
/* Object1 is greater than Object2. */
/* Object2 must be placed before Object 1. */
NodeToMove = MergeList2;
/* Make MergeList2 point to the new start of the second list. */
MergeList2 = MergeList2->NextLinkNode;
/* If NodeToMove was the last item in the list, we must update EndOfList since
NodeToMove will no longer be the last item in the list! */
if ( NodeToMove == ListData->EndOfList )
{
ListData->EndOfList = NodeToMove->PreviousLinkNode;
}
/* Remove NodeToMove from the list. */
if ( NodeToMove->PreviousLinkNode != NULL)
{
NodeToMove->PreviousLinkNode->NextLinkNode = MergeList2;
if (MergeList2 != NULL)
{
MergeList2->PreviousLinkNode = NodeToMove->PreviousLinkNode;
}
}
/* NodeToMove must go in front of the current item in the first list. The
current item in the first list is given by MergeList1. */
if (MergeList1->PreviousLinkNode != NULL)
{
/* Make the item before MergeList1 point to NodeToMove. */
MergeList1->PreviousLinkNode->NextLinkNode = NodeToMove;
}
/* Make NodeToMove->PreviousLinkNode point to the item before MergeList1. */
NodeToMove->PreviousLinkNode = MergeList1->PreviousLinkNode;
/* Make NodeToMove->NextLinkNode point to MergeList1. */
NodeToMove->NextLinkNode = MergeList1;
/* Complete the process by making MergeList1->PreviousLinkNode point to NodeToMove. */
MergeList1->PreviousLinkNode = NodeToMove;
/* If MergeList1 was the first item in the list, we must update StartOfList since
MergeList1 is no longer the first item in the list! */
if ( MergeList1 == ListData->StartOfList )
{
ListData->StartOfList = NodeToMove;
}
MergeList2Size--;
}
else
{
/* Object1 is less than or equal to Object2. */
/* Remove Object1 from the first list. To do this, we just need to
advance the MergeList1 pointer, since it always points to the
first item in the first of the lists which are being merged. */
MergeList1 = MergeList1->NextLinkNode;
MergeList1Size--;
}
}
/* We have left the while loop. All of the items in one of the merge lists
must have been used. We must now setup MergeList1 to point to the first
of the next two lists to be merged. */
if ( (MergeList2Size == 0) || (MergeList2 == NULL) )
{
/* MergeList2 is empty. Either MergeList2 now points to the first
item in the next list to be merged, or MergeList2 is NULL. Thus,
MergeList2 points to what MergeList1 should point to. So make
MergeList1 equal to MergeList2. When we reach the top of the
"do" loop, MergeList2 will be set to point to the proper location. */
MergeList1 = MergeList2;
}
else
{
/* The first of the next two lists to be merged starts after the end of the
list pointed to by MergeList2. Thus, we must start MergeList1 at
MergeList2 and advance it past the remaining items in MergeList2. */
ListSize = MergeList2Size;
MergeList1 = MergeList2;
while ( ( MergeList1 != NULL ) && (ListSize > 0) )
{
MergeList1 = MergeList1->NextLinkNode;
ListSize--;
}
}
} while (MergeList1 != NULL);
MergeListMaxSize = MergeListMaxSize * 2;
} while ( ListData->ItemCount > MergeListMaxSize);
}
return Error;
}
/*********************************************************************/
/* */
/* Function Name: ForEachItem */
/* */
/* Descriptive Name: This function passes a pointer to each item */
/* in a list to a user provided function for */
/* processing by the user provided function. */
/* */
/* Input: dlist_t ListToProcess : The dlist_t whose items are to */
/* be processed by the user */
/* provided function. */
/* */
/* int (*ProcessItem) (...) */
/* */
/* This is a pointer to the user provided function. */
/* This user provided function takes the following */
/* parameters: */
/* */
/* ADDRESS Object : A pointer to an item in */
/* ListToProcess. */
/* TAG Object1Tag : The user assigned TAG value for */
/* the item pointed to by Object. */
/* ADDRESS Parameter : The address of a block of */
/* memory containing any */
/* parameters that the user */
/* wishes to have passed to this*/
/* function. */
/* */
/* ADDRESS Parameters : This field is passed through to */
/* *ProcessItem. This function does */
/* not even look at the contents of */
/* this field. This field is here to */
/* provide the user a way to pass */
/* additional data to *ProcessItem */
/* that *ProcessItem may need to */
/* function correctly. */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* If unsuccessful, return an error code. */
/* */
/* Error Handling: This function aborts immediately when an error */
/* is detected, and any remaining items in the list*/
/* will not be processed. */
/* */
/* Side Effects: None. */
/* */
/* Notes: This function allows the user to access all of the items */
/* in a list and perform an operation on them. The */
/* operation performed must not free any items in the list, */
/* or perform any list operations on the list being */
/* processed. */
/* */
/* As an example of when this would be useful, consider a */
/* a list of graphic objects (rectangles, triangles, circles*/
/* etc.) which comprise a drawing. To draw the picture */
/* that these graphic objects represent, one could build a */
/* loop which gets and draws each item. Another way to */
/* do this would be to build a drawing function which can */
/* draw any of the graphic objects, and then use that */
/* function as the ProcessItem function in a call to */
/* ForEachItem. */
/* */
/* If the ProcessItem function returns an error code */
/* other than DLIST_SUCCESS, then ForEachItem will terminate*/
/* and return an error to whoever called it. The single */
/* exception to this is if ProcessItem returns */
/* DLIST_SEARCH_COMPLETE, in which case ForEachItem */
/* terminates and returns DLIST_SUCCESS. This is */
/* useful for using ForEachItem to search a list and then */
/* terminating the search once the desired item is found. */
/* */
/* A word about the Parameters parameter. This parameter */
/* is passed through to *ProcessItem and is never looked at */
/* by this function. This means that the user can put any */
/* value they desire into Parameters as long as it is the */
/* same size (in bytes) as Parameters. The intended use of */
/* Parameters is to allow the user to pass information to */
/* *ProcessItem that *ProcessItem may need. Either way, */
/* how Parameters is used is literally up to the user. */
/* */
/*********************************************************************/
int ForEachItem(dlist_t ListToProcess,
int (*ProcessItem) (ADDRESS Object,
TAG ObjectTag,
ADDRESS ObjectHandle,
ADDRESS Parameters),
ADDRESS Parameters,
boolean Forward)
{
/* Since ListToProcess is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToProcess. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item while we access its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
int Error;
/* We will assume that ListToProcess points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToProcess;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Assume success. */
Error = DLIST_SUCCESS;
/* Check for empty list. */
if (ListData->ItemCount == 0)
{
return Error;
}
/* Set CurrentLinkNode based upon the direction we are going to traverse the list. */
if ( Forward )
{
/* Get the first link node in the list. */
CurrentLinkNode = ListData->StartOfList;
}
else
{
/* Get the last link node in the list. */
CurrentLinkNode = ListData->EndOfList;
}
/* Now loop through the items in the list. */
while ( CurrentLinkNode != NULL )
{
/* Call the user provided function to process the current item in the list. */
Error = ProcessItem(CurrentLinkNode->DataLocation,CurrentLinkNode->DataTag, CurrentLinkNode, Parameters);
if ( Error != DLIST_SUCCESS )
{
if ( Error == DLIST_SEARCH_COMPLETE )
Error = DLIST_SUCCESS;
return Error;
}
/* Advance to the next item in the list based upon the direction that we are traversing the list in. */
if ( Forward )
{
CurrentLinkNode = CurrentLinkNode->NextLinkNode;
}
else
{
CurrentLinkNode = CurrentLinkNode->PreviousLinkNode;
}
}
/* All items in the list have been processed. */
return Error;
}
/*********************************************************************/
/* */
/* Function Name: PruneList */
/* */
/* Descriptive Name: This function allows the caller to examine */
/* each item in a list and optionally delete */
/* it from the list. */
/* */
/* Input: dlist_t ListToProcess : The dlist_t to be pruned. */
/* */
/* boolean (*KillItem) (...) */
/* */
/* This is a pointer to a user provided function. */
/* This user provided function takes the following */
/* parameters: */
/* */
/* ADDRESS Object : A pointer to an item in */
/* ListToProcess. */
/* TAG Object1Tag : The user assigned TAG value for */
/* the item pointed to by Object. */
/* ADDRESS Parameter : The address of a block of */
/* memory containing any */
/* parameters that the user */
/* wishes to have passed to this*/
/* function. */
/* boolean * FreeMemory : The address of a boolean */
/* variable which this */
/* function will set to */
/* either TRUE or FALSE. */
/* If the function return */
/* value is TRUE, then the */
/* value in *FreeMemory will */
/* be examined. If it is */
/* TRUE, then PruneList will */
/* free the memory associated*/
/* with the item being */
/* deleted. If *FreeMemory */
/* is FALSE, then the item */
/* being removed from the */
/* dlist_t will not be freed,*/
/* and it is up to the user */
/* to ensure that this memory*/
/* is handled properly. */
/* uint * Error : The address of a variable to*/
/* hold the error return value.*/
/* */
/* ADDRESS Parameters : This field is passed through to */
/* *KillItem. This function does */
/* not even look at the contents of */
/* this field. This field is here to */
/* provide the user a way to pass */
/* additional data to *ProcessItem */
/* that *ProcessItem may need to */
/* function correctly. */
/* */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* If unsuccessful, return an error code. */
/* */
/* Error Handling: This function aborts immediately when an error */
/* is detected, and any remaining items in the list*/
/* will not be processed. */
/* */
/* Side Effects: None. */
/* */
/* Notes: This function allows the user to access all of the items */
/* in a list, perform an operation on them, and then */
/* optionally delete ("remove") them from the DLIST. The */
/* operation performed must not free any items in the list, */
/* or perform any list operations on the list being */
/* processed. */
/* */
/* If the KillItem function sets *Error to something other */
/* than DLIST_SUCCESS, then PruneList will terminate and */
/* return an error to whoever called it. The single */
/* exception to this is if KillItem sets *Error to */
/* DLIST_SEARCH_COMPLETE, in which case KillItem */
/* terminates and sets *Error to DLIST_SUCCESS. This is */
/* useful for using KillItem to search a list and then */
/* terminating the search once the desired item is found. */
/* */
/* A word about the Parameters parameter. This parameter */
/* is passed through to *ProcessItem and is never looked at */
/* by this function. This means that the user can put any */
/* value they desire into Parameters as long as it is the */
/* same size (in bytes) as Parameters. The intended use of */
/* Parameters is to allow the user to pass information to */
/* *ProcessItem that *ProcessItem may need. Either way, */
/* how Parameters is used is literally up to the user. */
/* */
/*********************************************************************/
int PruneList(dlist_t ListToProcess,
boolean (*KillItem) (ADDRESS Object,
TAG ObjectTag,
ADDRESS ObjectHandle,
ADDRESS Parameters,
boolean * FreeMemory,
uint * Error),
ADDRESS Parameters)
{
/* Since ListToProcess is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using ListToProcess. This way we just do the
cast once. */
ControlNode * ListData;
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item while we access its data.
This limits the levels of indirection
to one, which should result in faster
execution. */
LinkNode * PreviousLinkNode;/* Used to point to the LinkNode immediately
prior to the CurrentLinkNode. This is
needed if an item is deleted. */
boolean FreeMemory; /* Used as a parameter to KillItem to let the
user indicate whether or not to free the
memory associated with an item that is being
removed from the list. */
int Error;
/* We will assume that ListToProcess points to a valid list. Given this,
we will initialize ListData to point to the ControlNode of this
list. */
ListData = (ControlNode *) ListToProcess;
#ifdef EVMS_DEBUG
/* We must now validate the list before we attempt to use it. We will
do this by checking the Verify field in the ControlNode. */
if ((ListData == NULL) || (ListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Assume success. */
Error = DLIST_SUCCESS;
/* Check for empty list. */
if (ListData->ItemCount == 0)
{
return Error;
}
/* Get the first link node in the list. */
CurrentLinkNode = ListData->StartOfList;
/* Now loop through the items in the list. */
while ( CurrentLinkNode != NULL )
{
/* Call the user provided function to decide whether or not to keep the
current item in the list. */
if ( KillItem(CurrentLinkNode->DataLocation,CurrentLinkNode->DataTag,CurrentLinkNode,Parameters,&FreeMemory,&Error) )
{
if ( ( Error != DLIST_SUCCESS ) && ( Error != DLIST_SEARCH_COMPLETE ) )
{
return Error;
}
/* We are to remove the current item from the list. */
/* Initialize PreviousLinkNode. */
PreviousLinkNode = CurrentLinkNode->PreviousLinkNode;
/* Is the item being removed from the list the first item in the list? */
if ( PreviousLinkNode == NULL )
{
/* We are at the start of the list. Update the StartOfList field. */
ListData->StartOfList = CurrentLinkNode->NextLinkNode;
}
else
{
/* We are somewhere in the middle of the list, or possibly even the
last item in the list. */
PreviousLinkNode->NextLinkNode = CurrentLinkNode->NextLinkNode;
}
/* Is the item we are removing the last item in the list? If so, update the
pointer to the last item in the list. */
if ( CurrentLinkNode == ListData->EndOfList )
{
ListData->EndOfList = PreviousLinkNode;
}
else
{
/* Since CurrentLinkNode is not the last item in the list, we must adjust the
PreviousLinkNode field of the item following CurrentLinkNode. */
CurrentLinkNode->NextLinkNode->PreviousLinkNode = PreviousLinkNode;
}
/* CurrentLinkNode has now been removed from the list. */
/* Is the item we are deleting the current item in the list? */
if ( CurrentLinkNode == ListData->CurrentItem )
{
/* Since the item we are deleting is the current item, we must choose
a new current item. If the item we are deleting is NOT the last
item in the list, then we will choose the item following it as the
new current item. If the item we are deleting is the last item
in the list, then we will choose the item immediately before it to
be the new current item. */
if ( CurrentLinkNode->NextLinkNode != NULL )
{
/* We are removing an item from the beginning or middle of the list. */
ListData->CurrentItem = CurrentLinkNode->NextLinkNode;
}
else
{
/* We are removing the last item in the list. */
ListData->CurrentItem = PreviousLinkNode;
}
}
/* Adjust the count of items in the list. */
ListData->ItemCount = ListData->ItemCount - 1;
/* Now we must free the memory associated with the current node. */
if ( FreeMemory )
{
/* Free the memory associated with the actual item stored in the list. */
free(CurrentLinkNode->DataLocation);
}
/* Free the memory associated with the control structures used to manage items in the list. */
CurrentLinkNode->ControlNodeLocation = NULL;
free(CurrentLinkNode);
/* Resume our traversal of the tree. */
/* Are we at the start of the list? If so, then PreviousLinkNode will be
NULL since there is no previous link node. */
if ( PreviousLinkNode != NULL )
{
/* Since we did not delete the first item in the list, we can resume our
tree traversal with the item following PreviousLinkNode. */
CurrentLinkNode = PreviousLinkNode->NextLinkNode;
}
else
{
/* The item we deleted was the first item in the list. We can resume our
list traversal with the item which is now the first item in the list. */
CurrentLinkNode = ListData->StartOfList;
}
/* Did the user indicate that we are to stop the list traversal? */
if ( Error == DLIST_SEARCH_COMPLETE )
{
/* Convert the error code to success and stop the list traversal. */
Error = DLIST_SUCCESS;
return Error;
}
}
else
{
if ( Error != DLIST_SUCCESS )
{
if ( Error == DLIST_SEARCH_COMPLETE )
Error = DLIST_SUCCESS;
return Error;
}
/* We are keeping the current item in the list. */
/* Advance to the next item in the list. */
CurrentLinkNode = CurrentLinkNode->NextLinkNode;
}
}
/* All items in the list have been processed. */
return Error;
}
/*********************************************************************/
/* */
/* Function Name: AppendList */
/* */
/* Descriptive Name: Removes the items in SourceList and appends */
/* them to TargetList. */
/* */
/* Input: dlist_t TargetList : The dlist_t which is to have the */
/* items from SourceList appended to */
/* it. */
/* dlist_t SourceList : The dlist_t whose items are to be */
/* removed and appended to TargetList.*/
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* SourceList will be empty, and TargetList will contain*/
/* all of its original items and all of the items that */
/* were in SourceList. */
/* If unsuccessful, return an error code. SourceList and */
/* TargetList will be unmodified. */
/* */
/* Error Handling: This function will abort immediately upon */
/* detection of an error. All errors that can be */
/* detected are detected before the contents of */
/* SourceList are appended to TargetList, so if an*/
/* error is detected and the function aborts, */
/* SourceList and TargetList are unaltered. */
/* */
/* Side Effects: None. */
/* */
/* Notes: None. */
/* */
/*********************************************************************/
int AppendList(dlist_t TargetList,
dlist_t SourceList)
{
/* Since TargetList is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using TargetList. This way we just do the
cast once. */
ControlNode * TargetListData;
/* Since SourceList is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using SourceList. This way we just do the
cast once. */
ControlNode * SourceListData;
ControlNode TempListData; /* Used to hold the control node for
list when the control nodes for
TargetList and SourceList are being
swapped. */
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item in TargetList while we
access its data. This limits the
levels of indirection to one, which
should result in faster execution. */
LinkNode * SourceLinkNode; /* Used to point to the LinkNode of
the first item in the SourceList. */
/* We will assume that TargetList and SourceList both point to a valid lists.
Given this, we will initialize TargetListData and SourceListData to point
to the ControlNode of TargetList and SourceList, respectively. */
TargetListData = (ControlNode *) TargetList;
SourceListData = (ControlNode *) SourceList;
#ifdef EVMS_DEBUG
/* We must now validate the lists before we attempt to use them. We will
do this by checking the Verify field in the ControlNode. */
if ((TargetListData == NULL) || (TargetListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
if ((SourceListData == NULL) || (SourceListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Is the source list empty? If so, we have nothing to do! */
if (SourceListData->ItemCount == 0)
{
/* The source list is empty! We are done! */
return DLIST_SUCCESS;
}
/* Is the target list currently empty? */
if (TargetListData->ItemCount == 0)
{
/* Since the target list is empty but the source list is not, we will just swap the
control record from the source list with the control record of the target list. */
TempListData = *TargetListData;
*TargetListData = *SourceListData;
*SourceListData = TempListData;
/* Get the first item in the target list. */
CurrentLinkNode = TargetListData->StartOfList;
/* Adjust the ControlNodeLocation field of this Link Node. */
CurrentLinkNode->ControlNodeLocation = TargetListData;
}
else
{
/* Here's where we do the most work. Both the TargetList and the SourceList contain
items. We must append the items from the SourceList to the TargetList. */
/* Get the last item in the target list. */
CurrentLinkNode = TargetListData->EndOfList;
/* Get the first item in the source list. */
SourceLinkNode = SourceListData->StartOfList;
/* Attach the items from the Source List to the end of those from the Target List. */
CurrentLinkNode->NextLinkNode = SourceLinkNode;
SourceLinkNode->PreviousLinkNode = CurrentLinkNode;
/* Update the Target List Control Information. */
TargetListData->EndOfList = SourceListData->EndOfList;
TargetListData->ItemCount = TargetListData->ItemCount + SourceListData->ItemCount;
/* Update the Source List Control Information. */
SourceListData->StartOfList = NULL;
SourceListData->EndOfList = NULL;
SourceListData->CurrentItem = NULL;
SourceListData->ItemCount = 0;
}
/* Adjust the ControlNodeLocation field of all of the items being moved from the Source List to the Target List. */
while (CurrentLinkNode->NextLinkNode != NULL)
{
CurrentLinkNode = CurrentLinkNode->NextLinkNode;
CurrentLinkNode->ControlNodeLocation = TargetListData;
}
/* All done! */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: CopyList */
/* */
/* Descriptive Name: Copies the items in SourceList to the */
/* TargetList. */
/* */
/* Input: dlist_t TargetList : The dlist_t which is to have the */
/* items from SourceList copied to it.*/
/* dlist_t SourceList : The dlist_t whose items are to be */
/* copied to TargetList. */
/* */
/* Output: If successful, return DLIST_SUCCESS. */
/* SourceList will be unchanged and TargetList will */
/* contain all of its original items and all of the */
/* items that were in SourceList. */
/* If unsuccessful, return an error code. SourceList and */
/* TargetList will be unmodified. */
/* */
/* Error Handling: This function will abort immediately upon */
/* detection of an error. All errors that can be */
/* detected are detected before the contents of */
/* SourceList are appended to TargetList, so if an*/
/* error is detected and the function aborts, */
/* SourceList and TargetList are unaltered. */
/* */
/* Side Effects: None. */
/* */
/* Notes: None. */
/* */
/*********************************************************************/
int CopyList(dlist_t TargetList,
dlist_t SourceList,
Insertion_Modes Insert_Mode)
{
/* Since TargetList is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using TargetList. This way we just do the
cast once. */
ControlNode * TargetListData;
/* Since SourceList is of type DLIST, we can not use it without
having to type cast it each time. To avoid all of the type casting,
we will declare a local variable of type ControlNode * and then
initialize it once using SourceList. This way we just do the
cast once. */
ControlNode * SourceListData;
ControlNode TempListData; /* Used to build a copy of the
SourceList before inserting it into
the TargetList. */
LinkNode * CurrentLinkNode; /* Used to point to the LinkNode of the
current item in TargetList while we
access its data. This limits the
levels of indirection to one, which
should result in faster execution. */
int Error;
ADDRESS Handle; /* Needed to catch returned handle
from call to InsertObject(). */
/* We will assume that TargetList and SourceList both point to a valid lists.
Given this, we will initialize TargetListData and SourceListData to point
to the ControlNode of TargetList and SourceList, respectively. */
TargetListData = (ControlNode *) TargetList;
SourceListData = (ControlNode *) SourceList;
#ifdef EVMS_DEBUG
/* We must now validate the lists before we attempt to use them. We will
do this by checking the Verify field in the ControlNode. */
if ((TargetListData == NULL) || (TargetListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
if ((SourceListData == NULL) || (SourceListData->Verify != VerifyValue))
{
return DLIST_NOT_INITIALIZED;
}
#endif
/* Is the source list empty? If so, we have nothing to do! */
if (SourceListData->ItemCount == 0)
{
/* The source list is empty! We are done! */
return DLIST_SUCCESS;
}
/* Validate the Insert_Mode. */
if ( Insert_Mode > AppendToList )
{
return DLIST_INVALID_INSERTION_MODE;
}
/* Build a copy of the SourceList in a temporary location so that we can
check for errors (such as "out of memory") and bail out without having
modified the TargetList. */
TempListData.ItemCount = 0; /* No items in the list. */
TempListData.StartOfList = NULL; /* Since the list is empty, there is no first item */
TempListData.EndOfList = NULL; /* Since the list is empty, there is no last item */
TempListData.CurrentItem = NULL; /* Since the list is empty, there is no current item */
#ifdef EVMS_DEBUG
TempListData.Verify = VerifyValue; /* Initialize the Verify field so that this list will recognized as being valid. */
#endif
CurrentLinkNode = SourceListData->StartOfList;
Error = DLIST_SUCCESS;
while ( (Error == DLIST_SUCCESS) && (CurrentLinkNode != NULL) ){
Error = InsertObject(&TempListData,
CurrentLinkNode->DataLocation,
CurrentLinkNode->DataTag,
NULL,
AppendToList,
FALSE,
&Handle);
CurrentLinkNode = CurrentLinkNode->NextLinkNode;
}
if (Error != DLIST_SUCCESS) {
/* Something went wrong when trying to make copies of the items in the SourceList.
Delete any items we may have created in the temporary list. */
DeleteAllItems(&TempListData, FALSE);
return Error;
}
/* Set the ControlNodeLocation of all the LinkNodes in the new list to be the TargetListData. */
CurrentLinkNode = TempListData.StartOfList;
while (CurrentLinkNode != NULL) {
CurrentLinkNode->ControlNodeLocation = TargetListData;
CurrentLinkNode = CurrentLinkNode->NextLinkNode;
}
/* Put the copy of the SourceList into the TargetList at the place the caller specified. */
if (TargetListData->CurrentItem == NULL) {
// the Target list is empty. Just slap in the temporary list.
TargetListData->StartOfList = TempListData.StartOfList;
TargetListData->EndOfList = TempListData.EndOfList;
TargetListData->CurrentItem = TempListData.StartOfList;
} else if ((Insert_Mode == InsertAtStart) ||
((Insert_Mode == InsertBefore) &&
(TargetListData->CurrentItem == TargetListData->StartOfList))) {
TempListData.EndOfList->NextLinkNode = TargetListData->StartOfList;
TargetListData->StartOfList->PreviousLinkNode = TempListData.EndOfList;
TargetListData->StartOfList = TempListData.StartOfList;
} else if (Insert_Mode == InsertBefore) {
CurrentLinkNode = TargetListData->CurrentItem;
TempListData.StartOfList->PreviousLinkNode = CurrentLinkNode->PreviousLinkNode;
TempListData.EndOfList->NextLinkNode = CurrentLinkNode;
CurrentLinkNode->PreviousLinkNode->NextLinkNode = TempListData.StartOfList;
CurrentLinkNode->PreviousLinkNode = TempListData.EndOfList;
} else if ((Insert_Mode == AppendToList) ||
((Insert_Mode == InsertAfter) &&
(TargetListData->CurrentItem == TargetListData->EndOfList))) {
TempListData.StartOfList->PreviousLinkNode = TargetListData->EndOfList;
TargetListData->EndOfList->NextLinkNode = TempListData.StartOfList;
TargetListData->EndOfList = TempListData.EndOfList;
} else {
/* Insert_Mode must be InsertAfter and the current item is not the last one. */
CurrentLinkNode = TargetListData->CurrentItem;
TempListData.StartOfList->PreviousLinkNode = CurrentLinkNode;
TempListData.EndOfList->NextLinkNode = CurrentLinkNode->NextLinkNode;
CurrentLinkNode->NextLinkNode->PreviousLinkNode = TempListData.EndOfList;
CurrentLinkNode->NextLinkNode = TempListData.StartOfList;
}
/* Update the count of items in the TargetList. */
TargetListData->ItemCount += TempListData.ItemCount;
/* All done! */
return DLIST_SUCCESS;
}
/*********************************************************************/
/* */
/* Function Name: dlist_strerror */
/* */
/* Descriptive Name: REturns a string that describes the dlist */
/* error code. */
/* */
/* Input: int err_num: dlist error code */
/* */
/* Output: const char * description string */
/* */
/* Error Handling: If the error number is not a vlaid dlist */
/* error code, dlist_strerror() returns NULL */
/* */
/* Side Effects: None. */
/* */
/* Notes: None. */
/* */
/*********************************************************************/
static char * err_msg[] =
{
// DLIST_CORRUPTED 201
"Dlist is corrupted",
// DLIST_BAD 202
"Dlist is bad",
// DLIST_NOT_INITIALIZED 203
"Dlist is not initialized",
// DLIST_EMPTY 204
"Dlist is empty",
// DLIST_ITEM_SIZE_WRONG 205
"Item size is wrong",
// DLIST_BAD_ITEM_POINTER 206
"Bad item pointer",
// DLIST_ITEM_SIZE_ZERO 207
"Item size is zero",
// DLIST_ITEM_TAG_WRONG 208
"Item tag is wrong",
// DLIST_END_OF_LIST 209
"At end of dlist",
// DLIST_ALREADY_AT_START 210
"Already at start of dlist",
// DLIST_BAD_HANDLE 211
"Bad handle"
// DLIST_INVALID_INSERTION_MODE 212
"Insertion mode is not valid",
// DLIST_OBJECT_NOT_FOUND 213
"Object not found",
// DLIST_OBJECT_ALREADY_IN_LIST 214
"Object already in list"
};
const char * dlist_strerror(int err_num)
{
if (IS_DLIST_ERROR(err_num)) {
return err_msg[abs(err_num) - DLIST_ERROR_BASE];
} else {
return NULL;
}
}
|