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