Cleanup code and correct License
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / evel_library / double_list.c
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
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  * @file
21  * A simple double-linked list.
22  *
23  * @note  No thread protection so you will need to use appropriate
24  * synchronization if use spans multiple threads.
25  *
26  ****************************************************************************/
27
28 #include <assert.h>
29 #include <malloc.h>
30
31 #include "double_list.h"
32 #include "evel.h"
33
34 /**************************************************************************//**
35  * List initialization.
36  *
37  * Initialize the list supplied to be empty.
38  *
39  * @param   list    Pointer to the list to be initialized.
40
41  * @returns Nothing
42 ******************************************************************************/
43 void dlist_initialize(DLIST * list)
44 {
45   EVEL_ENTER();
46
47   /***************************************************************************/
48   /* Check assumptions.                                                      */
49   /***************************************************************************/
50   assert(list != NULL);
51
52   /***************************************************************************/
53   /* Initialize the list as empty.                                           */
54   /***************************************************************************/
55   list->head = NULL;
56   list->tail = NULL;
57
58   EVEL_EXIT();
59 }
60
61 void * dlist_pop_last(DLIST * list)
62 {
63   void *item = NULL;
64   DLIST_ITEM *current_tail = NULL;
65   DLIST_ITEM *new_tail = NULL;
66
67   assert(list != NULL);
68
69   current_tail = list->tail;
70   if (current_tail != NULL)
71   {
72     item = current_tail->item;
73     new_tail = current_tail->previous;
74     if (new_tail == NULL)
75     {
76       list->head = NULL;
77       list->tail = NULL;
78     }
79     else
80     {
81       new_tail->next = NULL;
82       list->tail = new_tail;
83     }
84     free(current_tail);
85   }
86
87   return item;
88 }
89
90 void dlist_push_first(DLIST * list, void * item)
91 {
92   DLIST_ITEM * new_element = NULL;
93   DLIST_ITEM * current_head = NULL;
94
95   /***************************************************************************/
96   /* Check assumptions.  Note that we do allow putting NULL pointers into    */
97   /* the list - not sure you'd want to, but let it happen.                   */
98   /***************************************************************************/
99   assert(list != NULL);
100
101   current_head = list->head;
102
103   new_element = malloc(sizeof(DLIST_ITEM));
104   assert(new_element != NULL);
105   new_element->next = current_head;
106   new_element->previous = NULL;
107   new_element->item = item;
108   list->head = new_element;
109
110   if (current_head != NULL)
111   {
112     current_head->previous = new_element;
113   }
114   else
115   {
116     list->tail = new_element;
117   }
118 }
119
120 void dlist_push_last(DLIST * list, void * item)
121 {
122   DLIST_ITEM * new_element = NULL;
123   DLIST_ITEM * current_tail = NULL;
124
125   /***************************************************************************/
126   /* Check assumptions.  Note that we do allow putting NULL pointers into    */
127   /* the list - not sure you'd want to, but let it happen.                   */
128   /***************************************************************************/
129   assert(list != NULL);
130
131   current_tail = list->tail;
132
133   new_element = malloc(sizeof(DLIST_ITEM));
134   assert(new_element != NULL);
135   new_element->next = NULL;
136   new_element->previous = current_tail;
137   new_element->item = item;
138   list->tail = new_element;
139
140   if (current_tail != NULL)
141   {
142     current_tail->next = new_element;
143   }
144   else
145   {
146     list->head = new_element;
147   }
148 }
149
150 DLIST_ITEM * dlist_get_first(DLIST * list)
151 {
152   return list->head;
153 }
154
155 DLIST_ITEM * dlist_get_last(DLIST * list)
156 {
157   return list->tail;
158 }
159
160 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
161 {
162   return item->next;
163 }
164
165 int dlist_is_empty(DLIST * list)
166 {
167   return (list->head == NULL);
168 }
169
170 int dlist_count(DLIST * list)
171 {
172   int count = 0;
173   DLIST_ITEM * item = list->head;
174
175   while (item != NULL)
176   {
177     count++;
178     item = item->next;
179   }
180
181   return count;
182 }