1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /********************************************************************20**
21 Name: common functions - linked list management
25 Desc: common functions for linked lists
29 *********************************************************************21*/
31 /* header include files (.h) */
33 #include "envopt.h" /* environment options */
34 #include "envdep.h" /* environment dependent */
35 #include "envind.h" /* environment independent */
37 #include "gen.h" /* general layer */
38 #include "ssi.h" /* system services */
40 /* header/extern include files (.x) */
42 #include "gen.x" /* general layer */
43 #include "ssi.x" /* system services */
44 #include "cm_llist.x" /* common functions */
47 /* Linked List functions */
54 * Desc: initializes a linked list control pointer.
66 CmLListCp *lCp /* list control point */
70 CmLListCp *lCp; /* list control point */
74 lCp->first = (CmLList *)NULLP;
75 lCp->last = (CmLList *)NULLP;
76 lCp->crnt = (CmLList *)NULLP;
80 } /* end of cmLListInit */
84 /*insert before head*/
87 * Fun: cmLListAdd2Head
89 * Desc: adds node to linked list before head.
101 CmLListCp *lCp, /* list control point */
102 CmLList *node /* node to be added */
105 Void cmLListAdd2Head(lCp, node)
106 CmLListCp *lCp; /* list control point */
107 CmLList *node; /* node to be added */
112 if (lCp == (CmLListCp *)NULLP)
118 node->next = lCp->first;
120 lCp->first = lCp->crnt = node;
128 node->next->prev = node;
130 } /* end of cmLListAdd2Head */
135 * Fun: cmLListAdd2Tail
137 * Desc: adds node to linked list after last.
149 CmLListCp *lCp, /* list control point */
150 CmLList *node /* node to be added */
153 Void cmLListAdd2Tail(lCp, node)
154 CmLListCp *lCp; /* list control point */
155 CmLList *node; /* node to be added */
160 if (lCp == (CmLListCp *)NULLP)
166 node->prev = lCp->last;
168 lCp->last = lCp->crnt = node;
176 node->prev->next = node;
178 } /* end of cmLListAdd2Tail */
183 * Fun: cmLListInsCrnt
185 * Desc: adds node to linked list before crnt.
197 CmLListCp *lCp, /* list control point */
198 CmLList *node /* node to be added */
201 Void cmLListInsCrnt(lCp, node)
202 CmLListCp *lCp; /* list control point */
203 CmLList *node; /* node to be added */
216 lCp->crnt = lCp->first = lCp->last = node;
221 node->next = lCp->crnt;
222 node->prev = lCp->crnt->prev;
224 node->prev->next = node;
225 node->next->prev = node;
227 if (lCp->first == lCp->crnt)
232 } /* end of cmLListInsCrnt */
234 /* cm_llist_c_001.main_7 - Add function */
237 * Fun: cmLListInsAfterCrnt
239 * Desc: adds node to linked list after crnt.
249 Void cmLListInsAfterCrnt
251 CmLListCp *lCp, /* list control point */
252 CmLList *node /* node to be added */
255 Void cmLListInsAfterCrnt(lCp, node)
256 CmLListCp *lCp; /* list control point */
257 CmLList *node; /* node to be added */
270 lCp->crnt = lCp->first = lCp->last = node;
274 node->prev = lCp->crnt;
275 node->next = lCp->crnt->next;
277 node->next->prev = node;
278 node->prev->next = node;
280 if (lCp->last == lCp->crnt)
285 } /* end of cmLListInsAfterCrnt */
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.
305 CmLList *cmLListDelFrm
307 CmLListCp *lCp, /* list control pointer */
308 CmLList *node /* node to be removed */
311 CmLList *cmLListDelFrm(lCp, node)
312 CmLListCp *lCp; /* list control pointer */
313 CmLList *node; /* node to be removed */
318 /* cm_llist_c_001.main_8 : added null check for node */
319 if (lCp == (CmLListCp *)NULLP || lCp->count == 0 || !node)
327 lCp->first = lCp->crnt = lCp->last = (CmLList *)NULLP;
333 lCp->crnt = (CmLList *)NULLP;
334 if (lCp->first == node)
337 node->next->prev = (CmLList *)NULLP;
338 lCp->first = node->next;
339 node->next = node->prev = (CmLList *)NULLP;
343 if (lCp->last == node)
346 node->prev->next = (CmLList *)NULLP;
347 lCp->last = node->prev;
348 node->next = node->prev = (CmLList *)NULLP;
352 node->prev->next = node->next;
353 node->next->prev = node->prev;
354 node->next = node->prev = (CmLList *)NULLP;
356 } /* end of cmLListDelFrm */
361 * Fun: cmLListCatLList
363 * Desc: adds a linked list to the end of the first list. list2 is
364 * added at the end of list1
376 CmLListCp *list1, /*-- list control point --*/
377 CmLListCp *list2 /*-- node to be added --*/
380 Void cmLListCatLList(list1, list2)
381 CmLListCp *list1; /*-- list control point --*/
382 CmLListCp *list2; /*-- node to be added --*/
386 /*-- if the second list is empty nothing to do --*/
387 if(list2->count == 0)
392 /*-- if the first list is empty make first the same as second*/
393 if(list1->count == 0)
395 list1->first = list2->first;
396 list1->last = list2->last;
397 list1->count = list2->count;
398 list1->crnt = list1->first;
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;
412 } /*-- end of cmLListCatLList --*/
414 /**********************************************************************
416 **********************************************************************/