summaryrefslogtreecommitdiff
path: root/software/apilib/llman/octapi_llman.c
blob: 59dcc89392018cda79c82b1ac0f1753149981b15 (plain)
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
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\

File:  octapi_llman.c

    Copyright (c) 2001-2011 Octasic Inc.
    
Description: 

	Library used to manage allocation tables and linked lists.  The library is
	made such that only a block of contiguous memory is needed for the
	management of the linked list/allocation table.

This file is part of the Octasic OCT6100 GPL API . The OCT6100 GPL API  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.

The OCT6100 GPL API 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 the OCT6100 GPL API; if not, write to the Free Software 
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

$Octasic_Release: OCT612xAPI-01.04.05 $

$Octasic_Revision: 22 $

\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#include "octapi_llman_private.h"
#include "apilib/octapi_llman.h"
#include "apilib/octapi_largmath.h"


/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctapiLlmAllocGetSize.
|
|	Description:	This function determines the amount of memory needed to
|					manage the allocation of a fixed amount of resources.
|					The memory is measured in bytes.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	number_of_items		UINT32			The number of resources to be allocated.
|	*l_size	UINT32		UINT32			The amount of memory needed, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctapiLlmAllocGetSize(UINT32 number_of_items,UINT32 * l_size)
{
	if (number_of_items == 0) return(GENERIC_BAD_PARAM);

	*l_size = (sizeof(LLM_ALLOC)) + (number_of_items * sizeof(UINT32));

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctapiLlmAllocInit.
|
|	Description:	This function intializes the LLM_ALLOC structure.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	**l					void			The memory used by the LLM_ALLOC structure.
|	number_of_items		UINT32			The number of resources to be allocated.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctapiLlmAllocInit(void ** l,UINT32 number_of_items)
{
	LLM_ALLOC* ls;
	UINT32 i;

	/* Check the number of items required.*/
	if (number_of_items == 0) return(GENERIC_BAD_PARAM);

	/* If no memory has been allocated yet:*/
	if (*l == NULL) return(OCTAPI_LLM_MEMORY_NOT_ALLOCATED);

	/* Build the structure before starting.*/
	ls = (LLM_ALLOC *)(*l);
	ls->linked_list = (UINT32 *)((BYTE *)ls + sizeof(LLM_ALLOC));

	ls->number_of_items = number_of_items;

	/* Linked list links all structures in ascending order.*/
	for(i=0;i<number_of_items;i++)
	{
		ls->linked_list[i] = i+1;
	}

	ls->linked_list[number_of_items - 1] = 0xFFFFFFFF; /* Invalid link.*/

	/* Next avail is 0.*/
	ls->next_avail_num = 0;

	/* Number of allocated items is null.*/
	ls->allocated_items = 0;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctapiLlmAllocInfo.
|
|	Description:	This function returns the number of free and allocated
|					block in the LLMAN list.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_ALLOC structure.
|	*allocated_items	UINT32			Number of allocated items.
|	*available_items	UINT32			Number of available items.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctapiLlmAllocInfo(void * l,UINT32 * allocated_items,UINT32 * available_items)
{
	LLM_ALLOC* ls;

	/* Build the structure before starting.*/
	ls = (LLM_ALLOC *)l;
	ls->linked_list = (UINT32 *)((BYTE *)ls + sizeof(LLM_ALLOC));

	*allocated_items = ls->allocated_items;
	*available_items = ls->number_of_items - ls->allocated_items;
	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctapiLlmAllocInfo.
|
|	Description:	This function allocates the resource indicated by blocknum.
|					If the resource can be allocated then GENERIC_OK is returned.  
|					Else an error.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_ALLOC structure.
|	*block_num			UINT32			The resource to be allocated.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctapiLlmAllocAlloc(void * l,UINT32 * blocknum)
{
	LLM_ALLOC* ls;
	UINT32 allocated_block;
	UINT32* node;

	/* Build the structure before starting.*/
	ls = (LLM_ALLOC *)l;
	ls->linked_list = (UINT32 *)((BYTE *)ls + sizeof(LLM_ALLOC));

	/* Get next available block number.*/
	allocated_block = ls->next_avail_num;

	/* Check if block is invalid.*/
	if (allocated_block == 0xFFFFFFFF)
	{
		/* Make blocknum NULL.*/
		*blocknum = 0xFFFFFFFF;

		return(OCTAPI_LLM_NO_STRUCTURES_LEFT);
	}

	node = &ls->linked_list[allocated_block];

	/* Copy next block number.*/
	ls->next_avail_num = *node;

	/* Tag as used the current block number.*/
	*node = 0xFFFFFFFE;

	/* Return proper block number.*/
	*blocknum = allocated_block;

	/* Update block usage number.*/
	ls->allocated_items++;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctapiLlmAllocDealloc.
|
|	Description:	This function deallocates the resource indicated by blocknum.
|					If the resource is not already allocated an error is returned.
|					Else GENERIC_OK is returned.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_ALLOC structure.
|	block_num			UINT32			The resource to be deallocated.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctapiLlmAllocDealloc(void * l,UINT32 blocknum)
{
	LLM_ALLOC* ls;
	UINT32* node;

	/* Build the structure before starting.*/
	ls = (LLM_ALLOC *)l;
	ls->linked_list = (UINT32 *)((BYTE *)ls + sizeof(LLM_ALLOC));
	
	/* Check for null item pointer.*/
	if (blocknum == 0xFFFFFFFF) return(GENERIC_OK);

	/* Check if blocknum is within specified item range.*/
	if (blocknum >= ls->number_of_items) return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);

	node = &ls->linked_list[blocknum];

	/* Check if block is really used as of now.*/
	if (*node != 0xFFFFFFFE) return(OCTAPI_LLM_MEMORY_NOT_ALLOCATED);

	/* Add link to list.*/
	*node = ls->next_avail_num;

	/* Point to returned block.*/
	ls->next_avail_num = blocknum;

	/* Update block usage number.*/
	ls->allocated_items--;

	return(GENERIC_OK);
}


/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiTllmAllocGetSize.
|
|	Description:	This function determines the amount of memory needed to
|					manage the allocation of a fixed amount of resources.
|					The memory is measured in bytes. 
|
|					This version is a time manage version of llman.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	number_of_items		UINT32			The number of resources to be allocated.
|	*l_size	UINT32		UINT32			The amount of memory needed, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiTllmAllocGetSize(UINT32 number_of_items,UINT32 * l_size)
{
	if (number_of_items == 0) return(GENERIC_BAD_PARAM);

	*l_size = (sizeof(TLLM_ALLOC)) + (number_of_items * sizeof(TLLM_ALLOC_NODE));

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiTllmAllocInit.
|
|	Description:	This function intializes the TLLM_ALLOC structure.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	**l					void			The memory used by the LLM_ALLOC structure.
|	number_of_items		UINT32			The number of resources to be allocated.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiTllmAllocInit(void ** l,UINT32 number_of_items)
{
	TLLM_ALLOC* ls;
	UINT32 i;

	/* Check the number of items required.*/
	if (number_of_items == 0) return(GENERIC_BAD_PARAM);

	/* If no memory has been allocated yet.*/
	if (*l == NULL) return(OCTAPI_LLM_MEMORY_NOT_ALLOCATED);

	/* Build the structure before starting.*/
	ls = (TLLM_ALLOC *)(*l);
	ls->linked_list = (TLLM_ALLOC_NODE *)((BYTE *)ls + sizeof(TLLM_ALLOC));

	ls->number_of_items = number_of_items;

	/* Linked list links all structures in ascending order.*/
	for(i=0;i<number_of_items;i++)
	{
		ls->linked_list[i].value = i+1;
	}

	ls->linked_list[number_of_items - 1].value = 0xFFFFFFFF; /* Invalid link.*/

	/* Next avail is 0.*/
	ls->next_avail_num = 0;

	/* Number of allocated items is null.*/
	ls->allocated_items = 0;

	/* Set the number of timeout entry.*/
	ls->number_of_timeout = 0;

	/* Next timeout is 0.*/
	ls->next_timeout_num = 0xFFFFFFFF;
	ls->last_timeout_num = 0xFFFFFFFF;

	/* Set the known time to 0.*/
	ls->last_known_time[ 0 ] = 0;
	ls->last_known_time[ 1 ] = 0;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiTllmAllocInfo.
|
|	Description:	This function returns the number of free and allocated
|					block in the TLLMAN list.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_ALLOC structure.
|	*allocated_items	UINT32			Number of allocated items.
|	*available_items	UINT32			Number of available items.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiTllmAllocInfo(void * l,UINT32 * allocated_items,UINT32 * available_items)
{
	TLLM_ALLOC* ls;

	/* Build the structure before starting.*/
	ls = (TLLM_ALLOC *)l;
	*allocated_items = ls->allocated_items;
	*available_items = ls->number_of_items - ls->allocated_items;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiTllmAllocAlloc.
|
|	Description:	This function allocates the resource indicated by blocknum.
|					If the resource can be allocated then GENERIC_OK is returned.  
|					Else an error.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_ALLOC structure.
|	*block_num			UINT32			The resource to be allocated.
|	*current_time		UINT32
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiTllmAllocAlloc(void * l,UINT32 * blocknum, UINT32 *current_time)
{
	TLLM_ALLOC* ls;
	UINT32 allocated_block;
	TLLM_ALLOC_NODE* node;

	/* Build the structure before starting.*/
	ls = (TLLM_ALLOC *)l;
	ls->linked_list = (TLLM_ALLOC_NODE *)((BYTE *)ls + sizeof(TLLM_ALLOC));
	
	if ( ls->allocated_items == ls->number_of_items  && 
		 ls->next_timeout_num != 0xFFFFFFFF )
	{
		UINT32 l_ulResult;
		l_ulResult = OctApiTllmCheckTimeoutList( ls, current_time );
		if ( l_ulResult != GENERIC_OK )
			return l_ulResult;
	}
	
	/* Get next available block number.*/
	allocated_block = ls->next_avail_num;

	/* Check if block is invalid.*/
	if (allocated_block == 0xFFFFFFFF)
	{
		/* Make blocknum NULL.*/
		*blocknum = 0xFFFFFFFF;

		return(OCTAPI_LLM_NO_STRUCTURES_LEFT);
	}

	node = &ls->linked_list[allocated_block];

	/* Copy next block number.*/
	ls->next_avail_num = node->value;

	/* Tag as used the current block number.*/
	node->value = 0xFFFFFFFE;

	/* Return proper block number.*/
	*blocknum = allocated_block;

	/* Update block usage number.*/
	ls->allocated_items++;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiTllmAllocDealloc.
|
|	Description:	This function deallocates the resource indicated by blocknum.
|					If the resource is not already allocated an error is returned.
|					Else GENERIC_OK is returned.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_ALLOC structure.
|	block_num			UINT32			The resource to be deallocated.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiTllmAllocDealloc(void * l,UINT32 blocknum, UINT32 timeout_value, UINT32 current_time[2])
{
	TLLM_ALLOC* ls;
	TLLM_ALLOC_NODE* node;
	UINT32	l_ulResult;

	/* Build the structure before starting.*/
	ls = (TLLM_ALLOC *)l;
	ls->linked_list = (TLLM_ALLOC_NODE *)((BYTE *)ls + sizeof(TLLM_ALLOC));
	
	/* Check for null item pointer.*/
	if (blocknum == 0xFFFFFFFF) return(GENERIC_OK);

	/* Check if blocknum is within specified item range.*/
	if (blocknum >= ls->number_of_items) return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);

	if ( ls->next_timeout_num != 0xFFFFFFFF )
	{
		l_ulResult = OctApiTllmCheckTimeoutList( ls, current_time );
		if ( l_ulResult != GENERIC_OK )
			return l_ulResult;
	}

	node = &ls->linked_list[blocknum];

	/* Check if block is really used as of now.*/
	if (node->value != 0xFFFFFFFE) return(OCTAPI_LLM_MEMORY_NOT_ALLOCATED);

	/* Add link to timeout list.*/
	if ( ls->last_timeout_num != 0xFFFFFFFF )
	{
		TLLM_ALLOC_NODE* last_node;

		/* insert the node at the end of the list.*/
		node->value = 0xFFFFFFFF;
		last_node = &ls->linked_list[ ls->last_timeout_num ];
		last_node->value = blocknum;
	}
	else
	{
		/* The node is alone in the list.*/
		node->value = 0xFFFFFFFF;
		ls->next_timeout_num = blocknum;
	}

	ls->last_timeout_num = blocknum;
	ls->number_of_timeout++;

	/* Set the timeout time of the node.*/
	l_ulResult = OctApiLmAdd( current_time, 1, &timeout_value, 0, node->timeout, 1 );
	if (l_ulResult != GENERIC_OK) 
		return(l_ulResult);

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiTllmCheckTimeoutList.
|
|	Description:	This function will verify if the timeout time 
|					of all the node present in the timeout list are bigger
|					then the current time.  If so the node will be returned
|					ot the free node list.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*ls						TLLM_ALLOC		The memory used by the TLLM_ALLOC structure.
|	current_time			UINT32[2]		The current time in msec.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiTllmCheckTimeoutList(TLLM_ALLOC *ls, UINT32 current_time[2])
{
	UINT32	result;
	UINT32	fConditionFlag = TRUE;

	/* Free-up any pending memory before trying the allocation:*/
	if ((ls->last_known_time[0] != current_time[0] ||
		ls->last_known_time[1] != current_time[1]) && 
		(current_time[1] != 0 || current_time[0] != 0))	/* Time has changed.*/
	{
		TLLM_ALLOC_NODE *pcurrent_node;
		UINT32	current_num;
		USHORT	neg;

		/* Remember time for next time!*/
		ls->last_known_time[0] = current_time[0];
		ls->last_known_time[1] = current_time[1];

	
		while ( fConditionFlag == TRUE )
		{
			/* Get a node from the timeout list.*/
			pcurrent_node = &ls->linked_list[ ls->next_timeout_num ];
			current_num = ls->next_timeout_num;
	
			/* Check if first node has timeout.*/
			result = OctApiLmCompare(current_time,1,pcurrent_node->timeout ,1,&neg);
			if (result != GENERIC_OK) return(result);
			
			/* if the timeout tiem was exceeded, set the block as free.*/
			if ( neg == FALSE )
			{
				/* set the next node pointer.*/
				ls->next_timeout_num = pcurrent_node->value;
				ls->number_of_timeout--;

				/* reset the last pointer of the timeout list.*/
				if ( ls->number_of_timeout == 0 )
					ls->last_timeout_num = 0xFFFFFFFF;

				/* return the node the free list.*/
				pcurrent_node->value = ls->next_avail_num;
				ls->next_avail_num = current_num;
				ls->allocated_items--;
			}
			else	/* node not in timeout */
			{
				fConditionFlag = FALSE;
				break;
			}

			if ( ls->next_timeout_num == 0xFFFFFFFF )
			{
				fConditionFlag = FALSE;
				break;	/* end of timeout list.*/
			}
		}
	}

	return(GENERIC_OK);
}
/**************************************** llm_alloc section **********************************************/







/**************************************** llm_list section **********************************************/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListGetSize
|
|	Description:	This function determines the amount of memory needed by
|					the LLM_LIST structure to manage the allocation of
|					number_of_items number of resources.  The memory is
|					measured in bytes.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	number_of_items		UINT32			The number of resources to be allocated
|										amongst all linked-lists.
|	number_of_lists		UINT32			The maximum number of linked-lists that
|										can be allocated.
|	*l_size	UINT32		UINT32			The amount of memory needed, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListGetSize(UINT32 number_of_items,UINT32 number_of_lists,UINT32 user_info_size,UINT32 * l_size)
{
	UINT32 head_alloc_size;
	UINT32 result;
	UINT32 user_info_size_roundup;

	if (number_of_items == 0) return(GENERIC_BAD_PARAM);
	if (number_of_lists == 0) return(GENERIC_BAD_PARAM);
	if (user_info_size == 0) return(GENERIC_BAD_PARAM);

	user_info_size_roundup = ((user_info_size + 3) / 4) * 4;

	result = OctapiLlmAllocGetSize(number_of_lists,&head_alloc_size);
	if(result != GENERIC_OK) return(result);

	*l_size = sizeof(LLM_LIST) + (number_of_lists * sizeof(LLM_LIST_HEAD)) + head_alloc_size + (number_of_items * (sizeof(LLM_LIST_ITEM) + user_info_size_roundup - 4));

	return(GENERIC_OK);
}

LLM_LIST_ITEM * OctApiLlmListGetItemPointer(LLM_LIST * ls, UINT32 item_number)
{
	return (LLM_LIST_ITEM *) (((BYTE *)ls->li) + (ls->item_size * item_number)) ;
}


/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListInit.
|
|	Description:	This function intializes the LLM_TALLOC structure.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	number_of_items		UINT32			The number of resources to be allocated
|										amongst all linked-lists.
|	number_of_lists		UINT32			The maximum number of linked-lists that
|										can be allocated.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListInit(void ** l,UINT32 number_of_items,UINT32 number_of_lists,UINT32 user_info_size)
{
	LLM_LIST* ls;
	LLM_LIST_ITEM* item;
	UINT32 i;
	UINT32 head_alloc_size;
	UINT32 result;
	UINT32 user_info_size_roundup;
	UINT32 total_lists;
	BYTE* lsbyte;


	if (number_of_items == 0) return(GENERIC_BAD_PARAM);
	if (number_of_lists == 0) return(GENERIC_BAD_PARAM);
	if (user_info_size == 0) return(GENERIC_BAD_PARAM);

	user_info_size_roundup = ((user_info_size + 3) / 4) * 4;

	/* Get the size of the Alloc structure used to manage head of list structures.*/
	result = OctapiLlmAllocGetSize(number_of_lists,&head_alloc_size);
	if(result != GENERIC_OK) return(result);

	if (*l == NULL) return(OCTAPI_LLM_MEMORY_NOT_ALLOCATED);

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)(*l);
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	/* Initialize parameters in the structure.*/
	ls->head_alloc_size = head_alloc_size;
	ls->user_info_bytes = user_info_size;
	ls->user_info_size = user_info_size_roundup;
	ls->total_items = number_of_items;
	ls->assigned_items = 0;
	ls->total_lists = number_of_lists;
	ls->assigned_lists = 0;
	ls->next_empty_item = 0;
	ls->item_size = sizeof(LLM_LIST_ITEM) + user_info_size_roundup - 4;

	/* Complete the build!*/
	ls = (LLM_LIST *)(*l);
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	/* Initialize the head of queue Alloc structure.*/
	result = OctapiLlmAllocInit(&(ls->list_head_alloc),number_of_lists);
	if(result != GENERIC_OK) return(result);

	/* Initialize the linked list of the items:*/
	for(i=0; i<number_of_items; i++)
	{
		item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * i);

		if (i == (number_of_items - 1))
			item->forward_link = 0xFFFFFFFF;
		else
			item->forward_link = i + 1;
	}

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListInfo.
|
|	Description:	This function returns the status of the LLM_LIST structure.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	*allocated_lists	UINT32			The number of linked_lists allocated.
|	*free_lists			UINT32			The number of linked_lists still free.
|	*allocated_items	UINT32			The number of items allocated to lists.
|	*free_items			UINT32			The number of items still free.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListInfo(void * l,UINT32 * allocated_lists,UINT32 * allocated_items,
									UINT32 * free_lists,UINT32 * free_items)
{
	LLM_LIST* ls;
	BYTE* lsbyte;
	UINT32 total_lists;

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	*allocated_items = ls->assigned_items;
	*free_items = ls->total_items - ls->assigned_items;

	*allocated_lists = ls->assigned_lists;
	*free_lists = ls->total_lists - ls->assigned_lists;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListCreate.
|
|	Description:	This function creates a linked list.  The target which is
|					allocated the newly created list can request additions
|					or removals from the list later on.  To target identifies
|					its list with the returned list handle.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	*list_handle		UINT32			The handle to the new list, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListCreate(void * l,UINT32 * list_handle)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	UINT32 blocknum;
	UINT32 total_lists;
	UINT32 result;
	BYTE* lsbyte;

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	/* Get a list using the list head alloc structure.*/
	result = OctapiLlmAllocAlloc(ls->list_head_alloc, &blocknum);
	if (result != GENERIC_OK) return(result);

	/* The handle is the block number.*/
	*list_handle = blocknum;

	/* Initialize the list head structure.*/
	lh = &ls->lh[blocknum];
	lh->list_length = 0;
	lh->head_pointer = 0xFFFFFFFF;
	lh->tail_pointer = 0xFFFFFFFF;
	lh->cache_item_number = 0xFFFFFFFF;
	lh->cache_item_pointer = 0xFFFFFFFF;

	ls->assigned_lists++;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListDelete.
|
|	Description:	This function deletes the linked list indicated by the
|					handle list_handle.  Any items which are still allocated
|					to the list are first deallocated.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	*list_handle		UINT32			The handle to the list.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListDelete(void * l,UINT32 list_handle)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	UINT32 total_lists;
	UINT32 result;
	BYTE* lsbyte;

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);
	if (ls->lh[list_handle].list_length == 0xFFFFFFFF) return(OCTAPI_LLM_INVALID_LIST_HANDLE);

	/* Release internal list header handle...*/
	result = OctapiLlmAllocDealloc(ls->list_head_alloc,list_handle);
	if (result != GENERIC_OK) return(result);

	lh = &ls->lh[list_handle];

	/* Deallocate all items in the list!*/
	if (lh->list_length != 0)
	{
		LLM_LIST_ITEM * item;

		item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * lh->tail_pointer);

		/* Release the items using only the links.*/
		item->forward_link = ls->next_empty_item;
		ls->next_empty_item = lh->head_pointer;

		/* Remove items from item counter.*/
		ls->assigned_items -= lh->list_length;
	}

	lh->list_length = 0xFFFFFFFF;
	lh->head_pointer = 0xFFFFFFFF;
	lh->tail_pointer = 0xFFFFFFFF;
	lh->cache_item_number = 0xFFFFFFFF;
	lh->cache_item_pointer = 0xFFFFFFFF;

	ls->assigned_lists--;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListLength.
|
|	Description:	This function returns the number of items allocated to the
|					list indicated by the handle list_handle.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	list_handle			UINT32			The handle to the list.
|	*number_of_items	UINT32			The number of items in the list, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListLength(void * l,UINT32 list_handle, UINT32 * number_of_items_in_list)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	UINT32 total_lists;
	BYTE* lsbyte;

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);
	if (lh->list_length == 0xFFFFFFFF) return(OCTAPI_LLM_INVALID_LIST_HANDLE);

	*number_of_items_in_list = lh->list_length;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListItemData
|
|	Description:	This function returns a pointer to the user data associated
|					with an item.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	list_handle			UINT32			The handle to the list.
|	item_number			UINT32			The number of the list node in question.
|	**item_data_pnt		void			The pointer to the user data, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListItemData(void * l,UINT32 list_handle,UINT32 item_number,void ** item_data_pnt)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	LLM_LIST_ITEM* item;
	UINT32	cur_list_pnt;
	UINT32	cur_list_num;
	UINT32	total_lists;
	UINT32	list_length;
	BYTE*	lsbyte;
	UINT32	fConditionFlag = TRUE;

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	list_length = lh->list_length;
	
	*item_data_pnt = NULL;
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);
	if (list_length == 0xFFFFFFFF) return(OCTAPI_LLM_INVALID_LIST_HANDLE);
	if (list_length <= item_number)	return(OCTAPI_LLM_ELEMENT_NOT_FOUND);

	/* Determine where the search will start.*/
	if (list_length == (item_number + 1))	/* Last item in list:*/
	{
		cur_list_pnt = lh->tail_pointer;
		cur_list_num = item_number;
	}
	else if (lh->cache_item_number <= item_number)	/* Start at cache:*/
	{
		cur_list_pnt = lh->cache_item_pointer;
		cur_list_num = lh->cache_item_number;
	}
	else  /* Start at beginning:*/
	{
		cur_list_pnt = lh->head_pointer;
		cur_list_num = 0;
	}

	/* Start search from cur_list_pnt and cur_list_num.*/
	while ( fConditionFlag == TRUE )
	{
		item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);

		if (cur_list_num == item_number) /* Item number found.*/
		{
			/* Write new cache entry.*/
			lh->cache_item_pointer = cur_list_pnt;
			lh->cache_item_number = cur_list_num;

			/* Get item info.*/
			*item_data_pnt = (void *)item->user_info;

			return(GENERIC_OK);
		}
		else if(item->forward_link == 0xFFFFFFFF) /* End of list found?!?*/
		{
			return(OCTAPI_LLM_INTERNAL_ERROR0);
		}
		else /* Item was not found, but continue searching.*/
		{
			cur_list_pnt = item->forward_link;
		}

		cur_list_num++;
	}

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListInsertItem.
|
|	Description:	This function allocates a node to the linked list specified
|					by the handle list_handle.  The position of the new item
|					can be specified. A position of 0xFFFFFFFF means append to the 
|					list( use the OCTAPI_LLM_LIST_APPEND define for clarity); 
|                   a position of 0 mean insert at the begining of the list.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	*list_handle		UINT32			The handle to the list.
|	**item_data			void			Address of the user data space for this item.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListInsertItem(void * l,UINT32 list_handle,UINT32 item_number,void ** item_data_pnt)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	LLM_LIST_ITEM* free_item;
	UINT32 free_item_pnt;
	UINT32 total_lists;
	BYTE* lsbyte;
	UINT32	fConditionFlag = TRUE;

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	
	*item_data_pnt = NULL;
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);
	if (lh->list_length == 0xFFFFFFFF) return(OCTAPI_LLM_INVALID_LIST_HANDLE);
	if (lh->list_length < item_number && item_number != 0xFFFFFFFF)	
		return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);
	if (ls->next_empty_item == 0xFFFFFFFF) return(OCTAPI_LLM_NO_STRUCTURES_LEFT);

	/* Get a free item from the free item list!*/
	free_item_pnt = ls->next_empty_item;
	free_item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * free_item_pnt);
	ls->next_empty_item = free_item->forward_link;

	if (item_number == 0xFFFFFFFF)
		item_number = lh->list_length;

	if (lh->list_length == 0)	/* First item and only item:*/
	{
		free_item->forward_link = 0xFFFFFFFF;
		lh->tail_pointer = free_item_pnt;
		lh->head_pointer = free_item_pnt;
	}
	else if (item_number == 0)	/* First item and but list not empty:*/
	{
		free_item->forward_link = lh->head_pointer;
		lh->head_pointer = free_item_pnt;
	}
	else if (item_number == lh->list_length)	/* Append:*/
	{
		LLM_LIST_ITEM * last_item;
		last_item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * lh->tail_pointer);

		last_item->forward_link = free_item_pnt;
		free_item->forward_link = 0xFFFFFFFF;
		lh->tail_pointer = free_item_pnt;
	}
	else	/* Insert:*/
	{
		LLM_LIST_ITEM * last_item = NULL;
		LLM_LIST_ITEM * item;
		UINT32 last_list_pnt;
		UINT32 cur_list_pnt;
		UINT32 cur_list_num;

		if (lh->cache_item_number < item_number)	/* Start at cache:*/
		{
			cur_list_pnt = lh->cache_item_pointer;
			cur_list_num = lh->cache_item_number;
		}
		else /* Start at beginning:*/
		{
			cur_list_pnt = lh->head_pointer;
			cur_list_num = 0;
		}

		last_list_pnt = 0xFFFFFFFF;

		/* Start search from cur_list_pnt and cur_list_num.*/
		while ( fConditionFlag == TRUE )
		{
			item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);

			if (cur_list_num == item_number) /* Item number found.*/
			{
				if (last_list_pnt == 0xFFFFFFFF) return(OCTAPI_LLM_INTERNAL_ERROR1);

				free_item->forward_link = cur_list_pnt;
				last_item->forward_link = free_item_pnt;
				
				fConditionFlag = FALSE;
				break;
			}
			else if (item->forward_link == 0xFFFFFFFF) /* End of list found?!?*/
			{
				return(OCTAPI_LLM_INTERNAL_ERROR0);
			}
			else /* Item was not found, but continue searching.*/
			{
				last_item = item;
				last_list_pnt = cur_list_pnt;
				cur_list_pnt = item->forward_link;
			}

			cur_list_num++;
		}
	}

	/* Increase the list length.*/
	lh->list_length++;
	ls->assigned_items++;
	*item_data_pnt = (void *)free_item->user_info;

	/* Write new cache entry.*/
	lh->cache_item_pointer = free_item_pnt;
	lh->cache_item_number = item_number;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListCreateFull.
|
|	Description:	This function allocates the desired number of nodes to
|					the linked list specified by the handle list_handle.
|					The position of the new item can be specified. A
|					position of 0xFFFFFFFF means append to the list (use the
|					OCTAPI_LLM_LIST_APPEND define for clarity); a position
|					of 0 means insert at the begining of the list.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	*list_handle		UINT32			The handle to the list.
|	**item_data			void			Address of the user data space for this item.
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListCreateFull(void* l, UINT32 list_length, UINT32* plist_handle)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	LLM_LIST_ITEM* free_item;
	LLM_LIST_ITEM* last_item = NULL;
	UINT32 free_item_pnt = 0xFFFFFFFF;
	UINT32 total_lists;
	UINT32 list_handle;
	UINT32 list_length_m1;
	UINT32 next_empty_item;
	UINT32 result;
	UINT32 i;
	BYTE* lsbyte;


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Build the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Make sure another list can be created.*/
	if (ls->assigned_lists == ls->total_lists)
		return(OCTAPI_LLM_ELEMENT_ALREADY_ASSIGNED);

	/* Make sure there are enough free nodes to fill the new list.*/
	if (list_length > (ls->total_items - ls->assigned_items))
		return(OCTAPI_LLM_ELEMENT_ALREADY_ASSIGNED);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Create list (i.e. get a list using the list head alloc structure.*/
	result = OctapiLlmAllocAlloc(ls->list_head_alloc, &list_handle);
	if (result != GENERIC_OK) return(result);

	/* Initialize the list head structure.*/
	lh = &ls->lh[list_handle];
	lh->list_length = 0;
	lh->head_pointer = 0xFFFFFFFF;
	lh->tail_pointer = 0xFFFFFFFF;
	lh->cache_item_number = 0xFFFFFFFF;
	lh->cache_item_pointer = 0xFFFFFFFF;

	ls->assigned_lists++;
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Add the number of requested nodes to the list.*/
	lh = &ls->lh[list_handle];
	list_length_m1 = list_length - 1;
	next_empty_item = ls->next_empty_item;

	for (i=0; i<list_length; i++)
	{
		/* Get a free item from the free item list!*/
		free_item_pnt = next_empty_item;
		free_item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * free_item_pnt);
		next_empty_item = free_item->forward_link;

		/* Branch according to whether the node is the first in list, last, or in
			the middle.*/
		if (i == 0)	
		{
			/* First item.*/
			free_item->forward_link = 0xFFFFFFFF;
			lh->head_pointer = free_item_pnt;
			lh->tail_pointer = free_item_pnt;
		}
		else if (i == list_length_m1)	
		{
			/* Last item.*/
			last_item->forward_link = free_item_pnt;
			free_item->forward_link = 0xFFFFFFFF;
			lh->tail_pointer = free_item_pnt;
		}
		else
		{
			/* Node somewhere in the middle.*/
			last_item->forward_link = free_item_pnt;
		}

		/* Store pointer to free item as pointer to last item (for next iteration).*/
		last_item = free_item;
	}

	/* Store new value of next_empty_item.*/
	ls->next_empty_item = next_empty_item;

	/* Write new cache entry.*/
	lh->cache_item_pointer = free_item_pnt;
	lh->cache_item_number = list_length_m1;

	/* Set the list length.*/
	lh->list_length = list_length;
	ls->assigned_items += list_length;

	/* Return pointer to new list.*/
	*plist_handle = list_handle;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListAppendItems.
|
|	Description:	This function allocates the desired number of nodes to
|					the linked list specified by the handle list_handle.
|					The position of the new item can be specified. A
|					position of 0xFFFFFFFF means append to the list (use the
|					OCTAPI_LLM_LIST_APPEND define for clarity); a position
|					of 0 means insert at the begining of the list.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	*list_handle		UINT32			The handle to the list.
|	**item_data			void			Address of the user data space for this item.
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListAppendItems(void* l, UINT32 list_handle, UINT32 num_items)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	LLM_LIST_ITEM* item_list;
	LLM_LIST_ITEM* curr_item = NULL;
	LLM_LIST_ITEM* free_item;
	UINT32 curr_item_pnt = 0xFFFFFFFF;
	UINT32 total_lists;
	UINT32 next_empty_item;
	UINT32 item_size;
	UINT32 i;
	BYTE* lsbyte;


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Build the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Make sure list handle is valid.*/
	if (list_handle >= ls->total_lists)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);

	/* Make sure there is at least one item.*/
	if (num_items == 0)
		return(OCTAPI_LLM_INVALID_PARAMETER);

	/* Make sure there are enough free nodes.*/
	if (num_items > (ls->total_items - ls->assigned_items))
		return(OCTAPI_LLM_NO_STRUCTURES_LEFT);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Get pointer to list structure.*/
	lh = &ls->lh[list_handle];
	if (lh->list_length == 0xFFFFFFFF)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Add the number of requested nodes to the list.*/
	item_list = ls->li;
	item_size = ls->item_size;
	next_empty_item = ls->next_empty_item;

	for (i=0; i<num_items; i++)
	{
		if (i == 0)
		{
			if (lh->head_pointer == 0xFFFFFFFF)
			{
				/* Current and next items are one and the same!*/
				curr_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * next_empty_item);

				/* Set new head and tail pointers.*/
				lh->head_pointer = next_empty_item;
				lh->tail_pointer = next_empty_item;

				/* Update current item pnt.*/
				curr_item_pnt = next_empty_item;

				/* Update next item.*/
				next_empty_item = curr_item->forward_link;

				/* Set first item to be only item in list.*/
				curr_item->forward_link = 0xFFFFFFFF;
			}
			else
			{
				/* Get a free item from the free item list!*/
				curr_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * lh->tail_pointer);
				free_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * next_empty_item);

				/* Have current item point to next empty item.*/
				curr_item->forward_link = next_empty_item;

				/* Update current item pnt.*/
				curr_item_pnt = next_empty_item;

				/* Update next_empty_item.*/
				next_empty_item = free_item->forward_link;

				/* Update pointers to current item and free item.*/
				curr_item = free_item;
			}
		}
		else
		{
			/* Update pointers to current item and free item.*/
			free_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * next_empty_item);

			/* Have current item point to next empty item.*/
			curr_item->forward_link = next_empty_item;

			/* Update current item pnt.*/
			curr_item_pnt = next_empty_item;

			/* Update next_empty_item.*/
			next_empty_item = free_item->forward_link;

			/* Update pointers to current item and free item.*/
			curr_item = free_item;
		}
	}

	/* Terminate list.*/
	if ( curr_item != NULL )
		curr_item->forward_link = 0xFFFFFFFF;

	/* Update llman structure variables.*/
	ls->next_empty_item = next_empty_item;
	ls->assigned_items += num_items;

	/* Update list variables.*/
	lh->list_length += num_items;
	lh->cache_item_pointer = curr_item_pnt;
	lh->cache_item_number = lh->list_length - 1;
	lh->tail_pointer = curr_item_pnt;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListAppendAndSetItems.
|
|	Description:
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListAppendAndSetItems(void* l, UINT32 list_handle, UINT32 num_items, void* data_list)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	LLM_LIST_ITEM* item_list;
	LLM_LIST_ITEM* curr_item = NULL;
	LLM_LIST_ITEM* free_item;
	UINT32 curr_item_pnt = 0xFFFFFFFF;
	UINT32 total_lists;
	UINT32 next_empty_item;
	UINT32 user_info_bytes;
	UINT32 item_size;
	UINT32 i;
	BYTE* lsbyte;
	void* data_item;


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Build the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Make sure list handle is valid.*/
	if (list_handle >= ls->total_lists)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);

	/* Make sure there is at least one item.*/
	if (num_items == 0)
		return(OCTAPI_LLM_INVALID_PARAMETER);

	/* Make sure there are enough free nodes.*/
	if (num_items > (ls->total_items - ls->assigned_items))
		return(OCTAPI_LLM_NO_STRUCTURES_LEFT);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Get pointer to list structure.*/
	lh = &ls->lh[list_handle];
	if (lh->list_length == 0xFFFFFFFF)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Add the number of requested nodes to the list.*/
	item_list = ls->li;
	user_info_bytes = ls->user_info_bytes;
	item_size = ls->item_size;
	next_empty_item = ls->next_empty_item;
	data_item = data_list;

	for (i=0; i<num_items; i++)
	{
		if (i == 0)
		{
			if (lh->head_pointer == 0xFFFFFFFF)
			{
				/* Current and next items are one and the same!*/
				curr_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * next_empty_item);

				/* Set new head and tail pointers.*/
				lh->head_pointer = next_empty_item;
				lh->tail_pointer = next_empty_item;

				/* Update current item pnt.*/
				curr_item_pnt = next_empty_item;

				/* Update next item.*/
				next_empty_item = curr_item->forward_link;

				/* Set first item to be only item in list.*/
				curr_item->forward_link = 0xFFFFFFFF;
			}
			else
			{
				/* Get a free item from the free item list!*/
				curr_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * lh->tail_pointer);
				free_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * next_empty_item);

				/* Have current item point to next empty item.*/
				curr_item->forward_link = next_empty_item;

				/* Update current item pnt.*/
				curr_item_pnt = next_empty_item;

				/* Update next_empty_item.*/
				next_empty_item = free_item->forward_link;

				/* Update pointers to current item and free item.*/
				curr_item = free_item;
			}
		}
		else
		{
			/* Update pointers to current item and free item.*/
			free_item = (LLM_LIST_ITEM *)((BYTE *)item_list + item_size * next_empty_item);

			/* Have current item point to next empty item.*/
			curr_item->forward_link = next_empty_item;

			/* Update current item pnt.*/
			curr_item_pnt = next_empty_item;

			/* Update next_empty_item.*/
			next_empty_item = free_item->forward_link;

			/* Update pointers to current item and free item.*/
			curr_item = free_item;
		}

		/* Copy data to new item.*/
		OctApiLlmMemCpy(curr_item->user_info, data_item, user_info_bytes);

		/* Update data_item pointer for next iteration (item).*/
		data_item = (void *)((BYTE *)data_item + user_info_bytes);
	}


	/* Terminate list.*/
	if ( curr_item != NULL )
		curr_item->forward_link = 0xFFFFFFFF;

	/* Update llman structure variables.*/
	ls->next_empty_item = next_empty_item;
	ls->assigned_items += num_items;

	/* Update list variables.*/
	lh->list_length += num_items;
	lh->cache_item_pointer = curr_item_pnt;
	lh->cache_item_number = lh->list_length - 1;
	lh->tail_pointer = curr_item_pnt;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListSetItems.
|
|	Description:	This function takes a start entry (0 to length - 1),
|					a pointer to a list of data (each item of list is the
|					size of one data unit, specified at init), and the
|					length of the data list.  From this, the data will be
|					copied from the data list to the linked list, from
|					entry start_entry to (start_entry + data_length - 1).
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListSetItems(void* l, UINT32 list_handle, UINT32 start_item, UINT32 data_length, void* pdata_list)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	LLM_LIST_ITEM* item = NULL;
	UINT32 total_lists;
	UINT32 item_pnt = 0xFFFFFFFF;
	UINT32 i, j;
	BYTE* lsbyte;
	void* pdata_item = NULL;


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Build the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Make sure list handle is valid.*/
	if (list_handle >= ls->total_lists)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);
	lh = &ls->lh[list_handle];
	if (lh->list_length == 0xFFFFFFFF)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);

	/* Make sure the start_entry is within limits.*/
	if (start_item >= lh->list_length)
		return(OCTAPI_LLM_INVALID_PARAMETER);

	/* Make sure the end_entry is within limits.*/
	lh = &ls->lh[list_handle];
	if ((start_item + data_length) > lh->list_length)
		return(OCTAPI_LLM_INVALID_PARAMETER);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Set the data of each node.*/
	for (i=0; i<data_length; i++)
	{
		/* Obtain pointer to current item.*/
		if (i == 0)	
		{
			/* Check if location of start item is already cached.  If not, must search
				for it manually.*/
			if (start_item == (lh->cache_item_number + 1))
			{
				item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * lh->cache_item_pointer);
				item_pnt = item->forward_link;
				item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * item_pnt);
			}
			else
			{
				item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * lh->head_pointer);
				item_pnt = lh->head_pointer;
				for (j=0; j<start_item; j++)
				{
					item_pnt = item->forward_link;
					item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * item_pnt);
				}
			}

			pdata_item = (void *)((BYTE *)pdata_list + (i * ls->user_info_bytes));
		}
		else
		{
			item_pnt = item->forward_link;
			item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * item_pnt);

			pdata_item = (void *)((BYTE *)pdata_item + ls->user_info_bytes);
		}

		/* Set the value of the item's user data.*/
		OctApiLlmMemCpy(item->user_info, pdata_item, ls->user_info_bytes);
	}

	/* Write new cache entry.*/
	lh->cache_item_pointer = item_pnt;
	lh->cache_item_number = start_item + data_length - 1;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListCopyData.
|
|	Description:	This function takes a start entry (0 to length - 1),
|					a pointer to a list of data (each item of list is the
|					size of one data unit, specified at init), and the
|					length of the data list.  From this, the data will be
|					copied from the linked list to the data list, from
|					entry start_entry of the linked list to
|					(start_entry + data_length - 1).
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListCopyData(void* l, UINT32 list_handle, UINT32 start_item, UINT32 data_length, void* pdata_list)
{
	LLM_LIST* ls;
	LLM_LIST_HEAD* lh;
	LLM_LIST_ITEM* item = NULL;
	UINT32 item_pnt = 0xFFFFFFFF;
	UINT32 total_lists;
	UINT32 i, j;
	BYTE* lsbyte;
	void* pdata_item = NULL;


	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Build the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Make sure list handle is valid.*/
	if (list_handle >= ls->total_lists)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);
	lh = &ls->lh[list_handle];
	if (lh->list_length == 0xFFFFFFFF)
		return(OCTAPI_LLM_INVALID_LIST_HANDLE);

	/* Make sure the start_entry is within limits.*/
	if (start_item >= lh->list_length)
		return(OCTAPI_LLM_INVALID_PARAMETER);

	/* Make sure the end_entry is within limits.*/
	lh = &ls->lh[list_handle];
	if ((start_item + data_length) > lh->list_length)
		return(OCTAPI_LLM_INVALID_PARAMETER);
	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/



	/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
	/* Set the data of each node.*/
	for (i=0; i<data_length; i++)
	{
		/* Obtain pointer to current item.*/
		if (i == 0)	
		{
			/* Check if location of start item is already cached.  If not, must search
				for it manually.*/
			if (start_item == (lh->cache_item_number + 1))
			{
				item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * lh->cache_item_pointer);
				item_pnt = item->forward_link;
				item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * item_pnt);
			}
			else
			{
				item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * lh->head_pointer);
				for (j=0; j<start_item; j++)
				{
					item_pnt = item->forward_link;
					item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * item_pnt);
				}
			}

			pdata_item = (void *)((BYTE *)pdata_list + (i * ls->user_info_bytes));
		}
		else
		{
			item_pnt = item->forward_link;
			item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * item_pnt);

			pdata_item = (void *)((BYTE *)pdata_item + ls->user_info_bytes);
		}

		/* Set the value of the item's user data.*/
		OctApiLlmMemCpy(pdata_item, item->user_info, ls->user_info_bytes);
	}

	/* Write new cache entry.*/
	lh->cache_item_pointer = item_pnt;
	lh->cache_item_number = start_item + data_length - 1;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListRemoveItem.
|
|	Description:	This function deallocates a node of the linked list specified
|					by the handle list_handle.  
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	list_handle			UINT32			The handle to the list.
|	item_number			UINT32			The number of the item to be removed.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlmListRemoveItem(void * l,UINT32 list_handle,UINT32 item_number)
{
	LLM_LIST* ls;
	LLM_LIST_ITEM* freed_item = NULL;
	LLM_LIST_HEAD* lh;
	UINT32 freed_item_pnt = 0xFFFFFFFF;
	UINT32 total_lists;
	BYTE* lsbyte;
	UINT32	fConditionFlag = TRUE;

	/* Built the structure based on the base address:*/
	ls = (LLM_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM_LIST_HEAD *)(lsbyte + sizeof(LLM_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)));
	ls->li = (LLM_LIST_ITEM *)(lsbyte + sizeof(LLM_LIST) + (total_lists * sizeof(LLM_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);
	if (lh->list_length == 0xFFFFFFFF) return(OCTAPI_LLM_INVALID_LIST_HANDLE);
	if (lh->list_length <= item_number)	return(OCTAPI_LLM_BLOCKNUM_OUT_OF_RANGE);

	if (item_number == 0 && lh->list_length == 1)/* First item and only item:*/
	{
		freed_item_pnt = lh->head_pointer;
		freed_item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * freed_item_pnt);

		lh->head_pointer = 0xFFFFFFFF;
		lh->tail_pointer = 0xFFFFFFFF;

		lh->cache_item_number = 0xFFFFFFFF;
		lh->cache_item_pointer = 0xFFFFFFFF;
	}
	else if (item_number == 0)	/* First item and but list not empty:*/
	{
		freed_item_pnt = ls->lh[list_handle].head_pointer;
		freed_item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * freed_item_pnt);

		lh->head_pointer = freed_item->forward_link;

		lh->cache_item_number = 0;
		lh->cache_item_pointer = freed_item->forward_link;
	}
	else	/* Discard non-first item! (Caution: this could be the last item!)*/
	{
		LLM_LIST_ITEM * last_item = NULL;
		LLM_LIST_ITEM * item;
		UINT32 last_list_pnt;
		UINT32 cur_list_pnt;
		UINT32 cur_list_num;

		if (lh->cache_item_number < item_number)	/* Start at cache:*/
		{
			cur_list_pnt = lh->cache_item_pointer;
			cur_list_num = lh->cache_item_number;
		}
		else /* Start at beginning:*/
		{
			cur_list_pnt = lh->head_pointer;
			cur_list_num = 0;
		}

		last_list_pnt = 0xFFFFFFFF;

		/* Start search from cur_list_pnt and cur_list_num.*/
		while( fConditionFlag == TRUE )
		{
			item = (LLM_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);

			if (cur_list_num == item_number) /* Item number found.*/
			{
 				if (last_list_pnt == 0xFFFFFFFF) return(OCTAPI_LLM_INTERNAL_ERROR1);

				if ((item_number + 1) == lh->list_length)
				{
					lh->tail_pointer = last_list_pnt;
					last_item->forward_link = 0xFFFFFFFF;
				}
				else
				{
					last_item->forward_link = item->forward_link;
				}
				freed_item_pnt = cur_list_pnt;
				freed_item = item;

				/* Reset cache entry.*/
				lh->cache_item_pointer = last_list_pnt;
				lh->cache_item_number = cur_list_num - 1;

				fConditionFlag = FALSE;
				break;
			}
			else if (item->forward_link == 0xFFFFFFFF) /* End of list found?!?*/
			{
				return(OCTAPI_LLM_INTERNAL_ERROR0);
			}
			else /* Item was not found, but continue searching.*/
			{
				last_item = item;
				last_list_pnt = cur_list_pnt;
				cur_list_pnt = item->forward_link;
			}

			cur_list_num++;
		}
	}

	/* Decrease the list length.*/
	lh->list_length--;
	ls->assigned_items--;

	/* Return free block to free block list:*/
	freed_item->forward_link = ls->next_empty_item;
	ls->next_empty_item = freed_item_pnt;

	return(GENERIC_OK);
}

/**************************************** llm2 function section *****************************************/


/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlm2ListGetSize
|
|	Description:	This function determines the amount of memory needed by
|					the LLM2_LIST structure to manage the allocation of
|					number_of_items number of resources.  The memory is
|					measured in bytes.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	number_of_items		UINT32			The number of resources to be allocated
|										amongst all linked-lists.
|	number_of_lists		UINT32			The maximum number of linked-lists that
|										can be allocated.
|	*l_size	UINT32		UINT32			The amount of memory needed, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListGetSize(UINT32 number_of_items,UINT32 number_of_lists,UINT32 user_info_size,UINT32 * l_size)
{
	UINT32 head_alloc_size;
	UINT32 result;
	UINT32 user_info_size_roundup;

	if (number_of_items == 0) return(GENERIC_BAD_PARAM);
	if (number_of_lists == 0) return(GENERIC_BAD_PARAM);
	if (user_info_size == 0) return(GENERIC_BAD_PARAM);

	user_info_size_roundup = ((user_info_size + 3) / 4) * 4;

	result = OctapiLlmAllocGetSize(number_of_lists,&head_alloc_size);
	if(result != GENERIC_OK) return(result);

	*l_size = sizeof(LLM2_LIST) + (number_of_lists * sizeof(LLM2_LIST_HEAD)) + head_alloc_size + (number_of_items * (sizeof(LLM2_LIST_ITEM) + user_info_size_roundup - 4));

	return(GENERIC_OK);
}

LLM2_LIST_ITEM * OctApiLlm2ListGetItemPointer(LLM2_LIST * ls, UINT32 item_number)
{
	return (LLM2_LIST_ITEM *) (((BYTE *)ls->li) + (ls->item_size * item_number)) ;
}


/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlm2ListInit.
|
|	Description:	This function intializes the LLM2_TALLOC structure.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM2_LIST structure.
|	number_of_items		UINT32			The number of resources to be allocated
|										amongst all linked-lists.
|	number_of_lists		UINT32			The maximum number of linked-lists that
|										can be allocated.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListInit(void ** l,UINT32 number_of_items,UINT32 number_of_lists,UINT32 user_info_size)
{
	LLM2_LIST* ls;
	LLM2_LIST_ITEM* item;
	UINT32 i;
	UINT32 head_alloc_size;
	UINT32 result;
	UINT32 user_info_size_roundup;
	UINT32 total_lists;
	BYTE* lsbyte;


	if (number_of_items == 0) return(GENERIC_BAD_PARAM);
	if (number_of_lists == 0) return(GENERIC_BAD_PARAM);
	if (user_info_size == 0) return(GENERIC_BAD_PARAM);

	user_info_size_roundup = ((user_info_size + 3) / 4) * 4;

	/* Get the size of the Alloc structure used to manage head of list structures.*/
	result = OctapiLlmAllocGetSize(number_of_lists,&head_alloc_size);
	if(result != GENERIC_OK) return(result);

	if (*l == NULL) return(OCTAPI_LLM_MEMORY_NOT_ALLOCATED);

	/* Built the structure based on the base address:*/
	ls = (LLM2_LIST *)(*l);
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	/* Initialize parameters in the structure.*/
	ls->head_alloc_size = head_alloc_size;
	ls->user_info_bytes = user_info_size;
	ls->user_info_size = user_info_size_roundup;
	ls->total_items = number_of_items;
	ls->assigned_items = 0;
	ls->total_lists = number_of_lists;
	ls->assigned_lists = 0;
	ls->next_empty_item = 0;
	ls->item_size = sizeof(LLM2_LIST_ITEM) + user_info_size_roundup - 4;

	/* Complete the build!*/
	ls = (LLM2_LIST *)(*l);
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	/* Initialize the head of queue Alloc structure.*/
	result = OctapiLlmAllocInit(&(ls->list_head_alloc),number_of_lists);
	if(result != GENERIC_OK) return(result);

	/* Initialize the linked list of the items:*/
	for(i=0; i<number_of_items; i++)
	{
		item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * i);

		if (i == (number_of_items - 1))
			item->forward_link = 0xFFFFFFFF;
		else
			item->forward_link = i + 1;
	}

	return(GENERIC_OK);
}


/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlm2ListCreate.
|
|	Description:	This function creates a linked list.  The target which is
|					allocated the newly created list can request additions
|					or removals from the list later on.  To target identifies
|					its list with the returned list handle.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM_LIST structure.
|	*list_handle		UINT32			The handle to the new list, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListCreate(void * l,UINT32 * list_handle)
{
	LLM2_LIST* ls;
	LLM2_LIST_HEAD* lh;
	UINT32 blocknum;
	UINT32 total_lists;
	UINT32 result;
	BYTE* lsbyte;

	/* Built the structure based on the base address:*/
	ls = (LLM2_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	/* Get a list using the list head alloc structure.*/
	result = OctapiLlmAllocAlloc(ls->list_head_alloc, &blocknum);
	if (result != GENERIC_OK) return(result);

	/* The handle is the block number.*/
	*list_handle = blocknum;

	/* Initialize the list head structure.*/
	lh = &ls->lh[blocknum];
	lh->list_length = 0;
	lh->head_pointer = 0xFFFFFFFF;
	lh->tail_pointer = 0xFFFFFFFF;

	ls->assigned_lists++;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListDelete.
|
|	Description:	This function deletes the linked list indicated by the
|					handle list_handle.  Any items which are still allocated
|					to the list are first deallocated.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM2_LIST structure.
|	*list_handle		UINT32			The handle to the list.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListDelete(void * l,UINT32 list_handle)
{
	LLM2_LIST* ls;
	LLM2_LIST_HEAD* lh;
	UINT32 total_lists;
	UINT32 result;
	BYTE* lsbyte;

	/* Built the structure based on the base address:*/
	ls = (LLM2_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM2_BLOCKNUM_OUT_OF_RANGE);
	if (ls->lh[list_handle].list_length == 0xFFFFFFFF) return(OCTAPI_LLM2_INVALID_LIST_HANDLE);

	/* Release internal list header handle...*/
	result = OctapiLlmAllocDealloc(ls->list_head_alloc,list_handle);
	if (result != GENERIC_OK) return(result);

	lh = &ls->lh[list_handle];

	/* Deallocate all items in the list!*/
	if (lh->list_length != 0)
	{
		LLM2_LIST_ITEM * item;

		item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * lh->tail_pointer);

		/* Release the items using only the links.*/
		item->forward_link = ls->next_empty_item;
		ls->next_empty_item = lh->head_pointer;

		/* Remove items from item counter.*/
		ls->assigned_items -= lh->list_length;
	}

	lh->list_length = 0xFFFFFFFF;
	lh->head_pointer = 0xFFFFFFFF;
	lh->tail_pointer = 0xFFFFFFFF;

	ls->assigned_lists--;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmListLength.
|
|	Description:	This function returns the number of items allocated to the
|					list indicated by the handle list_handle.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM2_LIST structure.
|	list_handle			UINT32			The handle to the list.
|	*number_of_items	UINT32			The number of items in the list, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListLength(void * l,UINT32 list_handle, UINT32 * number_of_items_in_list)
{
	LLM2_LIST* ls;
	LLM2_LIST_HEAD* lh;
	UINT32 total_lists;
	BYTE* lsbyte;

	/* Built the structure based on the base address:*/
	ls = (LLM2_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM2_BLOCKNUM_OUT_OF_RANGE);
	if (lh->list_length == 0xFFFFFFFF) return(OCTAPI_LLM2_INVALID_LIST_HANDLE);

	*number_of_items_in_list = lh->list_length;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlm2ListItemData
|
|	Description:	This function returns a pointer to the user data associated
|					with an item.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM2_LIST structure.
|	list_handle			UINT32			The handle to the list.
|	item_number			UINT32			The number of the list node in question.
|	**item_data_pnt		void			The pointer to the user data, returned.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListItemData(void * l,UINT32 list_handle,UINT32 item_key,void ** item_data_pnt, PUINT32 item_number_pnt)
{
	LLM2_LIST* ls;
	LLM2_LIST_HEAD* lh;
	LLM2_LIST_ITEM* item;
	UINT32 cur_list_pnt;
	UINT32 cur_list_key = 0xFFFFFFFF;
	UINT32 total_lists;
	UINT32 list_length;
	BYTE* lsbyte;
	UINT32	fConditionFlag = TRUE;

	/* Built the structure based on the base address:*/
	ls = (LLM2_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	list_length = lh->list_length;
	
	*item_data_pnt = NULL;
	*item_number_pnt = 0;
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM2_BLOCKNUM_OUT_OF_RANGE);
	if (list_length == 0xFFFFFFFF) return(OCTAPI_LLM2_INVALID_LIST_HANDLE);

	/* Determine where the search will start.*/
	/* Start at beginning:*/
	cur_list_pnt = lh->head_pointer;
	item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);
	cur_list_key = item->key;
	
	/* Start search from cur_list_pnt and cur_list_num.*/
	while ( fConditionFlag == TRUE )
	{
		if (cur_list_key == item_key) /* Item key found.*/
		{
			/* Get item info.*/
			*item_data_pnt = (void *)item->user_info;

			return(GENERIC_OK);
		}
		else if(item->forward_link == 0xFFFFFFFF) /* End of list found?!?*/
		{
			return(OCTAPI_LLM2_INTERNAL_ERROR0);
		}
		else /* Item was not found, but continue searching.*/
		{
			cur_list_pnt = item->forward_link;
		}

		item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);
		cur_list_key = item->key;
		(*item_number_pnt)++;
	}

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlm2ListInsertItem.
|
|	Description:	This function allocates a node to the linked list specified
|					by the handle list_handle.  The position of the new item
|					will be defined based on the key value.  All entry are inserted
|					in the list in incremental Key value.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM2_LIST structure.
|	*list_handle		UINT32			The handle to the list.
|	**item_data			void			Address of the user data space for this item.
|	**prev_item_data	void			Address of the user data space for the previous item.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListInsertItem(void * l,UINT32 list_handle,UINT32 item_key,void ** item_data_pnt, void ** prev_item_data_pnt, void ** prev_prev_item_data_pnt, PUINT32 insert_status_pnt )
{
	LLM2_LIST* ls;
	LLM2_LIST_HEAD* lh;
	LLM2_LIST_ITEM* free_item;
	UINT32 free_item_pnt;
	UINT32 total_lists;
	BYTE* lsbyte;
	UINT32	ulPassCount = 0;
	UINT32	fConditionFlag = TRUE;

	/* Set the status of the insertion.*/
	*insert_status_pnt = OCTAPI_LLM2_INSERT_ERROR;

	/* Built the structure based on the base address:*/
	ls = (LLM2_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	
	*item_data_pnt = NULL;
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM2_BLOCKNUM_OUT_OF_RANGE);
	if (lh->list_length == 0xFFFFFFFF) return(OCTAPI_LLM2_INVALID_LIST_HANDLE);
	if (ls->next_empty_item == 0xFFFFFFFF) return(OCTAPI_LLM2_NO_STRUCTURES_LEFT);

	/* Get a free item from the free item list!*/
	free_item_pnt = ls->next_empty_item;
	free_item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * free_item_pnt);
	free_item->key = item_key;
	ls->next_empty_item = free_item->forward_link;

	if (lh->list_length == 0)	/* First item and only item:*/
	{
		free_item->forward_link = 0xFFFFFFFF;
		lh->tail_pointer = free_item_pnt;
		lh->head_pointer = free_item_pnt;
		*insert_status_pnt = OCTAPI_LLM2_INSERT_FIRST_NODE;

		/* There is no previous node information to return.*/
		*prev_item_data_pnt = NULL;
		*prev_prev_item_data_pnt = NULL;
	}
	else	/* Insert:*/
	{
		LLM2_LIST_ITEM * last_last_item = NULL;
		LLM2_LIST_ITEM * last_item = NULL;
		LLM2_LIST_ITEM * item;
		UINT32 last_list_pnt;
		UINT32 cur_list_pnt;
		UINT32 cur_list_key = 0xFFFFFFFF;

		/* Start at beginning:*/
		cur_list_pnt = lh->head_pointer;
		item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);
		cur_list_key = item->key;
		
		last_list_pnt = 0xFFFFFFFF;

		/* Start search from cur_list_pnt and cur_list_num.*/
		while ( fConditionFlag == TRUE )
		{
			/* Increment the pass count to determine if the addition will happen next to last.*/
			ulPassCount++;
	
			if (cur_list_key >= item_key) /* Item new node between the last and the curent. */
			{
				if (last_list_pnt == 0xFFFFFFFF) /* Must insert at the head of the list.*/
				{
					free_item->forward_link = cur_list_pnt;
					lh->head_pointer = free_item_pnt;
				}
				else									/* Standard insertion.*/
				{
					free_item->forward_link = cur_list_pnt;
					last_item->forward_link = free_item_pnt;
				}
			
				/* Check if the entry was made before the last one.*/
				if ( ulPassCount == lh->list_length )
					*insert_status_pnt = OCTAPI_LLM2_INSERT_BEFORE_LAST_NODE;
				else
					*insert_status_pnt = OCTAPI_LLM2_INSERT_LIST_NODE;

				fConditionFlag = FALSE;
				break;
			}
			else if (item->forward_link == 0xFFFFFFFF) /* End of list found, must insert at the end.*/
			{
				free_item->forward_link = 0xFFFFFFFF;
				item->forward_link = free_item_pnt;
				lh->tail_pointer = free_item_pnt;

				*insert_status_pnt = OCTAPI_LLM2_INSERT_LAST_NODE;

				fConditionFlag = FALSE;
				break;
			}
			else /* Item was not found, but continue searching.*/
			{
				last_last_item = last_item;
				last_item = item;
				last_list_pnt = cur_list_pnt;
				cur_list_pnt = item->forward_link;
			}

			item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);
			cur_list_key = item->key;

		}

		/* Return the previous node if possible.*/
		if ( *insert_status_pnt == OCTAPI_LLM2_INSERT_LIST_NODE ||
		     *insert_status_pnt == OCTAPI_LLM2_INSERT_BEFORE_LAST_NODE )
		{
			if ( last_item != NULL )
				*prev_item_data_pnt = (void *)last_item->user_info;

			if ( last_last_item != NULL )
				*prev_prev_item_data_pnt = (void *)last_last_item->user_info;
			else
				*prev_prev_item_data_pnt = NULL;
		}
		else
		{
			*prev_item_data_pnt = (void *)item->user_info;

			if ( ( last_last_item != NULL ) && ( last_item != NULL ) )
				*prev_prev_item_data_pnt = (void *)last_item->user_info;
			else
				*prev_prev_item_data_pnt = NULL;
		}
	}

	/* Increase the list length.*/
	lh->list_length++;
	ls->assigned_items++;
	*item_data_pnt = (void *)free_item->user_info;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlm2ListRemoveItem.
|
|	Description:	This function deallocates a node of the linked list specified
|					by the handle list_handle.  
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*l					void			The memory used by the LLM2_LIST structure.
|	list_handle			UINT32			The handle to the list.
|	item_key			UINT32			The key of the item to be removed.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
UINT32 OctApiLlm2ListRemoveItem(void * l,UINT32 list_handle,UINT32 item_key, PUINT32 prev_item_key_pnt, PUINT32 prev_prev_item_key_pnt, PUINT32 remove_status_pnt )
{
	LLM2_LIST* ls;
	LLM2_LIST_ITEM* freed_item = NULL;
	LLM2_LIST_HEAD* lh;
	UINT32 freed_item_pnt = 0xFFFFFFFF;
	UINT32 total_lists;
	BYTE* lsbyte;
	UINT32	fConditionFlag = TRUE;
	UINT32	ulPassCount = 0;

	/* Built the structure based on the base address:*/
	ls = (LLM2_LIST *)l;
	lsbyte = (BYTE *)ls;
	total_lists = ls->total_lists;

	/* Set the status of the removal to error as a default value.*/
	*remove_status_pnt = OCTAPI_LLM2_REMOVE_ERROR;
	
	ls->lh = (LLM2_LIST_HEAD *)(lsbyte + sizeof(LLM2_LIST));
	ls->list_head_alloc = (void *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)));
	ls->li = (LLM2_LIST_ITEM *)(lsbyte + sizeof(LLM2_LIST) + (total_lists * sizeof(LLM2_LIST_HEAD)) + ls->head_alloc_size);

	lh = &ls->lh[list_handle];
	
	if (list_handle >= ls->total_lists) return(OCTAPI_LLM2_BLOCKNUM_OUT_OF_RANGE);
	if (lh->list_length == 0xFFFFFFFF) return(OCTAPI_LLM2_INVALID_LIST_HANDLE);

	if (lh->list_length == 1)/* First item and only item if he matches.*/
	{
		freed_item_pnt = lh->head_pointer;
		freed_item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * freed_item_pnt);

		if ( freed_item->key == item_key )
		{
			lh->head_pointer = 0xFFFFFFFF;
			lh->tail_pointer = 0xFFFFFFFF;
		}
		else
			return(OCTAPI_LLM2_INTERNAL_ERROR1);
		
		/* Indicate that there was no node prior to the one removed.*/
		*prev_item_key_pnt = 0xFFFFFFFF;
		*prev_prev_item_key_pnt = 0xFFFFFFFF;
		*remove_status_pnt = OCTAPI_LLM2_REMOVE_FIRST_NODE;
	}
	else	/* Discard non-first item! (Caution: this could be the last item!)*/
	{
		LLM2_LIST_ITEM * last_last_item = NULL;
		LLM2_LIST_ITEM * last_item = NULL;
		LLM2_LIST_ITEM * item;
		UINT32 last_list_pnt;
		UINT32 cur_list_pnt;
		UINT32 cur_list_key;

		/* Start at beginning:*/
		cur_list_pnt = lh->head_pointer;
		item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);
		cur_list_key = item->key;
		
		last_list_pnt = 0xFFFFFFFF;

		/* Start search from cur_list_pnt and cur_list_num.*/
		while( fConditionFlag == TRUE )
		{
			ulPassCount++;
			if (cur_list_key == item_key) /* Item number found.*/
			{
 				if (last_list_pnt == 0xFFFFFFFF)	/* First item in the list.*/
				{
					lh->head_pointer = item->forward_link;
					*remove_status_pnt = OCTAPI_LLM2_REMOVE_FIRST_NODE;
				}
				else if ( item->forward_link == 0xFFFFFFFF)	/* Last item of the list.*/
				{
					last_item->forward_link = 0xFFFFFFFF;
					lh->tail_pointer = last_list_pnt;
					*remove_status_pnt = OCTAPI_LLM2_REMOVE_LAST_NODE;
				}
				else
				{
					last_item->forward_link = item->forward_link;

					if ( ulPassCount == ( lh->list_length - 1 ) )
						*remove_status_pnt = OCTAPI_LLM2_REMOVE_BEFORE_LAST_NODE;
					else
						*remove_status_pnt = OCTAPI_LLM2_REMOVE_LIST_NODE;
				}
					
				freed_item_pnt = cur_list_pnt;
				freed_item = item;

				fConditionFlag = FALSE;
				break;
			}
			else if (item->forward_link == 0xFFFFFFFF) /* End of list found?!?*/
			{
				return(OCTAPI_LLM2_INTERNAL_ERROR0);
			}
			else /* Item was not found, but continue searching.*/
			{
				last_last_item = last_item;
				last_item = item;
				last_list_pnt = cur_list_pnt;
				cur_list_pnt = item->forward_link;
			}

			item = (LLM2_LIST_ITEM *)((BYTE *)ls->li + ls->item_size * cur_list_pnt);
			cur_list_key = item->key;
		}

		/* Return the key of the node before the node removed if possible.*/
		if ( last_list_pnt == 0xFFFFFFFF )
			*prev_item_key_pnt = 0xFFFFFFFF;
		else if ( last_item != NULL )
			*prev_item_key_pnt = last_item->key;

		/* Return the key of the node before before the node removed if possible.*/
		if ( last_last_item == NULL )
			*prev_prev_item_key_pnt = 0xFFFFFFFF;
		else
			*prev_prev_item_key_pnt = last_last_item->key;

	}

	/* Decrease the list length.*/
	lh->list_length--;
	ls->assigned_items--;

	/* Return free block to free block list:*/
	freed_item->forward_link = ls->next_empty_item;
	ls->next_empty_item = freed_item_pnt;

	return(GENERIC_OK);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\
|	API UTILITIES
|
|	Function:		OctApiLlmMemCpy.
|
|	Description:	This function copies data from a source to a destination.
|
|  -----------------------------------------------------------------------  
|  |   Variable        |     Type     |          Description                
|  -----------------------------------------------------------------------  
|	*f_pvDestination	VOID			The destination where to copy the data.
|	*f_pvSource			VOID			The source where to copy the data from.
|	f_ulSize			UINT32			The number of bytes to copy.
|
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
VOID * OctApiLlmMemCpy( VOID *f_pvDestination, const VOID * f_pvSource, UINT32 f_ulSize )
{
    CHAR * pbyDst;
    const CHAR * pbySrc;
    UINT32 * f_pulAlignedDst;
    const UINT32 * f_pulAlignedSrc;
    
    pbyDst = (CHAR *)f_pvDestination;
    pbySrc = (const CHAR *)f_pvSource;

    /* 
     * If the size is small, or either SRC or DST is unaligned,
     * then punt into the byte copy loop.  This should be rare.
     */
    if ( ( f_ulSize < sizeof(UINT32) ) 
		|| ( ( (unsigned long)( pbySrc ) & ( sizeof(UINT32) - 1 ) ) | ( (unsigned long)( pbyDst ) & ( sizeof(UINT32) - 1 ) ) ) )
    {
        while ( f_ulSize-- )
            *pbyDst++ = *pbySrc++;
        return f_pvDestination;
    }
    
    f_pulAlignedDst = (UINT32 *)pbyDst;
    f_pulAlignedSrc = (const UINT32 *)pbySrc;
    
    /* Copy 4X long words at a time if possible. */
    while ( f_ulSize >= 4 * sizeof(UINT32) )
    {
        *f_pulAlignedDst++ = *f_pulAlignedSrc++;
        *f_pulAlignedDst++ = *f_pulAlignedSrc++;
        *f_pulAlignedDst++ = *f_pulAlignedSrc++;
        *f_pulAlignedDst++ = *f_pulAlignedSrc++;
        f_ulSize -= 4 * sizeof(UINT32);
    } 
    
    /* Copy one long word at a time if possible. */
    while ( f_ulSize >= sizeof(UINT32) )
    {
        *f_pulAlignedDst++ = *f_pulAlignedSrc++;
        f_ulSize -= sizeof(UINT32);
    }
    
    /* Pick up any residual with a byte copier. */
    pbyDst = (CHAR *)f_pulAlignedDst;
    pbySrc = (const CHAR *)f_pulAlignedSrc;
    while ( f_ulSize-- )
        *pbyDst++ = *pbySrc++;
    
    return f_pvDestination;
}

/**************************************** llm_list section **********************************************/