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