[Epic-ID: ODUHIGH-402][Task-ID: ODUHIGH-418] Harq feature changes
[o-du/l2.git] / src / cm / cm_llist.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 /********************************************************************20**
20   
21      Name:     common functions - linked list management
22   
23      Type:     C source file
24   
25      Desc:     common functions for linked lists
26   
27      File:     cm_llist.c
28   
29 *********************************************************************21*/
30 \f  
31 /* header include files (.h) */
32
33 #include "envopt.h"        /* environment options */  
34 #include "envdep.h"        /* environment dependent */
35 #include "envind.h"        /* environment independent */
36
37 #include "gen.h"           /* general layer */
38 #include "ssi.h"           /* system services */
39
40 /* header/extern include files (.x) */
41   
42 #include "gen.x"           /* general layer */
43 #include "ssi.x"           /* system services */
44 #include "cm_llist.x"      /* common functions */
45
46 \f
47 /* Linked List functions */
48
49 \f
50 /*
51 *
52 *       Fun:   cmLListInit
53 *
54 *       Desc:  initializes a linked list control pointer.
55 *
56 *       Ret:   ROK   - ok
57 *
58 *       Notes: None
59 *
60 *       File:  cm_llist.c
61 *
62 */
63 Void cmLListInit
64 (
65 CmLListCp *lCp                /* list control point */
66 )
67 {
68    
69    lCp->first = (CmLList *)NULLP;
70    lCp->last  = (CmLList *)NULLP;
71    lCp->crnt  = (CmLList *)NULLP;
72    lCp->count = 0;
73
74    return;
75 } /* end of cmLListInit */
76
77 \f
78
79 /*insert before head*/
80 /*
81 *
82 *       Fun:   cmLListAdd2Head
83 *
84 *       Desc:  adds node to linked list before head.
85 *
86 *       Ret:   ROK   - ok
87 *
88 *       Notes: None
89 *
90 *       File:  cm_llist.c
91 *
92 */
93 Void cmLListAdd2Head
94 (
95 CmLListCp *lCp,               /* list control point */
96 CmLList   *node               /* node to be added */
97 )
98 {
99
100 #ifdef ERRCHK
101    if (lCp == (CmLListCp *)NULLP)
102       return;
103 #endif
104  
105    lCp->count++;
106
107    node->next = lCp->first;
108    node->prev = NULLP;
109    lCp->first = lCp->crnt = node;
110    
111    if (!node->next)
112    {
113       lCp->last = node;
114       return;
115    }
116    
117    node->next->prev = node;
118    return;
119 } /* end of cmLListAdd2Head */
120
121
122 /*
123 *
124 *       Fun:   cmLListAdd2Tail
125 *
126 *       Desc:  adds node to linked list after last.
127 *
128 *       Ret:   ROK   - ok
129 *
130 *       Notes: None
131 *
132 *       File:  cm_llist.c
133 *
134 */
135 Void cmLListAdd2Tail
136 (
137 CmLListCp *lCp,               /* list control point */
138 CmLList   *node               /* node to be added */
139 )
140 {
141
142 #ifdef ERRCHK
143    if (lCp == (CmLListCp *)NULLP)
144       return;
145 #endif
146  
147    lCp->count++;
148
149    node->prev = lCp->last;
150    node->next = NULLP;
151    lCp->last = lCp->crnt = node;
152    
153    if (!node->prev)
154    {
155       lCp->first = node;
156       return;
157    }
158    
159    node->prev->next = node;
160    return;
161 } /* end of cmLListAdd2Tail */
162
163 \f
164 /*
165 *
166 *       Fun:   cmLListInsCrnt
167 *
168 *       Desc:  adds node to linked list before crnt.
169 *
170 *       Ret:   ROK   - ok
171 *
172 *       Notes: None
173 *
174 *       File:  cm_llist.c
175 *
176 */
177 Void cmLListInsCrnt
178 (
179 CmLListCp *lCp,               /* list control point */
180 CmLList   *node               /* node to be added */
181 )
182 {
183
184 #ifdef ERRCHK
185    if (!lCp)
186       return;
187 #endif
188  
189    lCp->count++;
190
191    if (lCp->count == 1)
192    {
193      lCp->crnt = lCp->first = lCp->last = node;
194      node->next = NULLP;
195      return;
196    }
197
198    node->next = lCp->crnt;
199    node->prev = lCp->crnt->prev;
200    if (node->prev)
201       node->prev->next = node;
202    node->next->prev = node;
203    
204    if (lCp->first == lCp->crnt)
205       lCp->first = node;
206    lCp->crnt = node;
207    
208    return;
209 } /* end of cmLListInsCrnt */
210 \f
211 /* cm_llist_c_001.main_7 - Add function */
212 /*
213 *
214 *       Fun:   cmLListInsAfterCrnt
215 *
216 *       Desc:  adds node to linked list after crnt.
217 *
218 *       Ret:   ROK   - ok
219 *
220 *       Notes: None
221 *
222 *       File:  cm_llist.c
223 *
224 */
225 Void cmLListInsAfterCrnt
226 (
227 CmLListCp *lCp,               /* list control point */
228 CmLList   *node               /* node to be added */
229 )
230 {
231
232 #ifdef ERRCHK
233    if (!lCp)
234       return;
235 #endif
236  
237    lCp->count++;
238
239    if (lCp->count == 1)
240    {
241      lCp->crnt = lCp->first = lCp->last = node;
242      return;
243    }
244
245    node->prev = lCp->crnt;
246    node->next = lCp->crnt->next;
247    if (node->next)
248       node->next->prev = node;
249    node->prev->next = node;
250   
251    if (lCp->last == lCp->crnt)
252       lCp->last = node;
253    lCp->crnt = node;
254    
255    return;
256 } /* end of cmLListInsAfterCrnt */
257
258
259 \f
260 /*
261 *
262 *       Fun:   cmLListDelFrm
263 *
264 *       Desc:  remove node pointed to by nodePtr from list and return node.
265 *              nodePtr could be anywhere in the list.
266 *              - resets crnt to NULLP.
267 *
268 *       Ret:   pointer
269 *
270 *       Notes: None
271 *
272 *       File:  cm_llist.c
273 *
274 */
275 CmLList *cmLListDelFrm
276 (
277 CmLListCp *lCp,                /* list control pointer */
278 CmLList *node                  /* node to be removed */
279 )
280 {
281   
282 #ifdef ERRCHK
283    /* cm_llist_c_001.main_8 : added null check for node */
284    if (lCp == (CmLListCp *)NULLP || lCp->count == 0 || !node)
285    {
286       return (NULLP);
287    }
288 #endif
289
290    if (lCp->count == 1)
291    {
292       lCp->first = lCp->crnt = lCp->last = (CmLList *)NULLP;
293       lCp->count = 0;
294       return (node);
295    }
296    
297    lCp->count--;
298    lCp->crnt = (CmLList *)NULLP;
299    if (lCp->first == node)
300    {
301       if (node->next)
302          node->next->prev = (CmLList *)NULLP;
303       lCp->first = node->next;
304       node->next = node->prev = (CmLList *)NULLP;
305       return (node);
306    }
307    
308    if (lCp->last == node)
309    {
310       if (node->prev)
311          node->prev->next = (CmLList *)NULLP;
312       lCp->last = node->prev;
313       node->next = node->prev = (CmLList *)NULLP;
314       return (node);
315    }
316
317    node->prev->next = node->next;
318    node->next->prev = node->prev;
319    node->next = node->prev = (CmLList *)NULLP;
320    return (node);
321 } /* end of cmLListDelFrm */
322
323
324 /*--
325   *
326   *       Fun:   cmLListCatLList
327   *
328   *       Desc:  adds a linked list to the end of the first list. list2 is 
329   *              added at the end of list1
330   *
331   *       Ret:   ROK   - ok
332   *
333   *       Notes: None
334   *
335   *       File:  cm_llist.c
336   *
337   --*/
338 Void cmLListCatLList
339 (
340 CmLListCp *list1,              /*-- list control point --*/
341 CmLListCp *list2               /*-- node to be added --*/
342 )
343 {
344
345    /*-- if the second list is empty nothing to do --*/
346    if(list2->count == 0)
347    {
348       return;
349    }
350
351    /*-- if the first list is empty make first the same as second*/
352    if(list1->count == 0)
353    {
354       list1->first = list2->first;
355       list1->last  = list2->last;
356       list1->count = list2->count;
357       list1->crnt  = list1->first;
358    }
359    else
360    {
361       list2->first->prev = list1->last;
362       list1->last->next = list2->first;
363       /* Set the last to the end of the 2nd list */
364       list1->last = list2->last;
365       list1->count += list2->count;
366    }
367
368    cmLListInit(list2);
369
370    return;
371 } /*-- end of cmLListCatLList --*/
372
373
374 /*--
375   *
376   *       Fun:   cmLListDeleteLList
377   *
378   *       Desc:  delete a linked list
379   *
380   *       Ret:   None
381   *
382   *       Notes: None
383   *
384   *       File:  cm_llist.c
385   *
386   --*/
387 Void cmLListDeleteLList
388 (
389 CmLListCp *list              /*-- list control point --*/
390 )
391 {
392    while(list->count)
393    {
394       cmLListDelFrm(list, list->first);
395    }
396    return;
397 }
398
399 /**********************************************************************
400          End of file
401 **********************************************************************/