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