Merge "Replaced old SSI function with new macros jira id - ODUHIGH-212"
[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 #ifdef ANSI
64 PUBLIC Void cmLListInit
65 (
66 CmLListCp *lCp                /* list control point */
67 )
68 #else 
69 PUBLIC Void cmLListInit(lCp)
70 CmLListCp *lCp;               /* list control point */
71 #endif
72 {
73    TRC3(cmLListInit);
74    
75    lCp->first = (CmLList *)NULLP;
76    lCp->last  = (CmLList *)NULLP;
77    lCp->crnt  = (CmLList *)NULLP;
78    lCp->count = 0;
79
80    RETVOID;
81 } /* end of cmLListInit */
82
83 \f
84
85 /*insert before head*/
86 /*
87 *
88 *       Fun:   cmLListAdd2Head
89 *
90 *       Desc:  adds node to linked list before head.
91 *
92 *       Ret:   ROK   - ok
93 *
94 *       Notes: None
95 *
96 *       File:  cm_llist.c
97 *
98 */
99 #ifdef ANSI
100 PUBLIC Void cmLListAdd2Head
101 (
102 CmLListCp *lCp,               /* list control point */
103 CmLList   *node               /* node to be added */
104 )
105 #else 
106 PUBLIC Void cmLListAdd2Head(lCp, node)
107 CmLListCp *lCp;               /* list control point */
108 CmLList   *node;              /* node to be added */
109 #endif
110 {
111    TRC3(cmLListAdd2Head);
112
113 #ifdef ERRCHK
114    if (lCp == (CmLListCp *)NULLP)
115       RETVOID;
116 #endif
117  
118    lCp->count++;
119
120    node->next = lCp->first;
121    node->prev = NULLP;
122    lCp->first = lCp->crnt = node;
123    
124    if (!node->next)
125    {
126       lCp->last = node;
127       RETVOID;
128    }
129    
130    node->next->prev = node;
131    RETVOID;
132 } /* end of cmLListAdd2Head */
133
134
135 /*
136 *
137 *       Fun:   cmLListAdd2Tail
138 *
139 *       Desc:  adds node to linked list after last.
140 *
141 *       Ret:   ROK   - ok
142 *
143 *       Notes: None
144 *
145 *       File:  cm_llist.c
146 *
147 */
148 #ifdef ANSI
149 PUBLIC Void cmLListAdd2Tail
150 (
151 CmLListCp *lCp,               /* list control point */
152 CmLList   *node               /* node to be added */
153 )
154 #else 
155 PUBLIC Void cmLListAdd2Tail(lCp, node)
156 CmLListCp *lCp;               /* list control point */
157 CmLList   *node;              /* node to be added */
158 #endif
159 {
160    TRC3(cmLListAdd2Tail);
161
162 #ifdef ERRCHK
163    if (lCp == (CmLListCp *)NULLP)
164       RETVOID;
165 #endif
166  
167    lCp->count++;
168
169    node->prev = lCp->last;
170    node->next = NULLP;
171    lCp->last = lCp->crnt = node;
172    
173    if (!node->prev)
174    {
175       lCp->first = node;
176       RETVOID;
177    }
178    
179    node->prev->next = node;
180    RETVOID;
181 } /* end of cmLListAdd2Tail */
182
183 \f
184 /*
185 *
186 *       Fun:   cmLListInsCrnt
187 *
188 *       Desc:  adds node to linked list before crnt.
189 *
190 *       Ret:   ROK   - ok
191 *
192 *       Notes: None
193 *
194 *       File:  cm_llist.c
195 *
196 */
197 #ifdef ANSI
198 PUBLIC Void cmLListInsCrnt
199 (
200 CmLListCp *lCp,               /* list control point */
201 CmLList   *node               /* node to be added */
202 )
203 #else 
204 PUBLIC Void cmLListInsCrnt(lCp, node)
205 CmLListCp *lCp;               /* list control point */
206 CmLList   *node;              /* node to be added */
207 #endif
208 {
209    TRC3(cmLListInsCrnt);
210
211 #ifdef ERRCHK
212    if (!lCp)
213       RETVOID;
214 #endif
215  
216    lCp->count++;
217
218    if (lCp->count == 1)
219    {
220      lCp->crnt = lCp->first = lCp->last = node;
221      node->next = NULLP;
222      RETVOID;
223    }
224
225    node->next = lCp->crnt;
226    node->prev = lCp->crnt->prev;
227    if (node->prev)
228       node->prev->next = node;
229    node->next->prev = node;
230    
231    if (lCp->first == lCp->crnt)
232       lCp->first = node;
233    lCp->crnt = node;
234    
235    RETVOID;
236 } /* end of cmLListInsCrnt */
237 \f
238 /* cm_llist_c_001.main_7 - Add function */
239 /*
240 *
241 *       Fun:   cmLListInsAfterCrnt
242 *
243 *       Desc:  adds node to linked list after crnt.
244 *
245 *       Ret:   ROK   - ok
246 *
247 *       Notes: None
248 *
249 *       File:  cm_llist.c
250 *
251 */
252 #ifdef ANSI
253 PUBLIC Void cmLListInsAfterCrnt
254 (
255 CmLListCp *lCp,               /* list control point */
256 CmLList   *node               /* node to be added */
257 )
258 #else 
259 PUBLIC Void cmLListInsAfterCrnt(lCp, node)
260 CmLListCp *lCp;               /* list control point */
261 CmLList   *node;              /* node to be added */
262 #endif
263 {
264    TRC3(cmLListInsAfterCrnt);
265
266 #ifdef ERRCHK
267    if (!lCp)
268       RETVOID;
269 #endif
270  
271    lCp->count++;
272
273    if (lCp->count == 1)
274    {
275      lCp->crnt = lCp->first = lCp->last = node;
276      RETVOID;
277    }
278
279    node->prev = lCp->crnt;
280    node->next = lCp->crnt->next;
281    if (node->next)
282       node->next->prev = node;
283    node->prev->next = node;
284   
285    if (lCp->last == lCp->crnt)
286       lCp->last = node;
287    lCp->crnt = node;
288    
289    RETVOID;
290 } /* end of cmLListInsAfterCrnt */
291
292
293 \f
294 /*
295 *
296 *       Fun:   cmLListDelFrm
297 *
298 *       Desc:  remove node pointed to by nodePtr from list and return node.
299 *              nodePtr could be anywhere in the list.
300 *              - resets crnt to NULLP.
301 *
302 *       Ret:   pointer
303 *
304 *       Notes: None
305 *
306 *       File:  cm_llist.c
307 *
308 */
309 #ifdef ANSI
310 PUBLIC CmLList *cmLListDelFrm
311 (
312 CmLListCp *lCp,                /* list control pointer */
313 CmLList *node                  /* node to be removed */
314 )
315 #else 
316 PUBLIC CmLList *cmLListDelFrm(lCp, node)
317 CmLListCp *lCp;               /* list control pointer */
318 CmLList *node;                /* node to be removed */
319 #endif
320 {
321    TRC3(cmLListDelFrm);
322   
323 #ifdef ERRCHK
324    /* cm_llist_c_001.main_8 : added null check for node */
325    if (lCp == (CmLListCp *)NULLP || lCp->count == 0 || !node)
326    {
327       return (NULLP);
328    }
329 #endif
330
331    if (lCp->count == 1)
332    {
333       lCp->first = lCp->crnt = lCp->last = (CmLList *)NULLP;
334       lCp->count = 0;
335       return (node);
336    }
337    
338    lCp->count--;
339    lCp->crnt = (CmLList *)NULLP;
340    if (lCp->first == node)
341    {
342       if (node->next)
343          node->next->prev = (CmLList *)NULLP;
344       lCp->first = node->next;
345       node->next = node->prev = (CmLList *)NULLP;
346       return (node);
347    }
348    
349    if (lCp->last == node)
350    {
351       if (node->prev)
352          node->prev->next = (CmLList *)NULLP;
353       lCp->last = node->prev;
354       node->next = node->prev = (CmLList *)NULLP;
355       return (node);
356    }
357
358    node->prev->next = node->next;
359    node->next->prev = node->prev;
360    node->next = node->prev = (CmLList *)NULLP;
361    return (node);
362 } /* end of cmLListDelFrm */
363
364
365 /*--
366   *
367   *       Fun:   cmLListCatLList
368   *
369   *       Desc:  adds a linked list to the end of the first list. list2 is 
370   *              added at the end of list1
371   *
372   *       Ret:   ROK   - ok
373   *
374   *       Notes: None
375   *
376   *       File:  cm_llist.c
377   *
378   --*/
379 #ifdef ANSI
380 PUBLIC Void cmLListCatLList
381 (
382  CmLListCp *list1,              /*-- list control point --*/
383  CmLListCp *list2               /*-- node to be added --*/
384  )
385 #else 
386 PUBLIC Void cmLListCatLList(list1, list2)
387    CmLListCp *list1;              /*-- list control point --*/
388    CmLListCp *list2;              /*-- node to be added --*/
389 #endif
390 {
391    TRC3(cmLListCatLList);
392
393    /*-- if the second list is empty nothing to do --*/
394    if(list2->count == 0)
395    {
396       RETVOID;
397    }
398
399    /*-- if the first list is empty make first the same as second*/
400    if(list1->count == 0)
401    {
402       list1->first = list2->first;
403       list1->last  = list2->last;
404       list1->count = list2->count;
405       list1->crnt  = list1->first;
406    }
407    else
408    {
409       list2->first->prev = list1->last;
410       list1->last->next = list2->first;
411       /* Set the last to the end of the 2nd list */
412       list1->last = list2->last;
413       list1->count += list2->count;
414    }
415
416    cmLListInit(list2);
417
418    RETVOID;
419 } /*-- end of cmLListCatLList --*/
420
421 /**********************************************************************
422          End of file
423 **********************************************************************/