Initial OpenECOMP Demo commit
[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(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  *    this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:  This product includes
23  *    software developed by the AT&T.
24  * 4. Neither the name of AT&T nor the names of its contributors may be used to
25  *    endorse or promote products derived from this software without specific
26  *    prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
29  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  *****************************************************************************/
39
40 #include <assert.h>
41 #include <malloc.h>
42
43 #include "double_list.h"
44 #include "evel.h"
45
46 /**************************************************************************//**
47  * List initialization.
48  *
49  * Initialize the list supplied to be empty.
50  *
51  * @param   list    Pointer to the list to be initialized.
52
53  * @returns Nothing
54 ******************************************************************************/
55 void dlist_initialize(DLIST * list)
56 {
57   EVEL_ENTER();
58
59   /***************************************************************************/
60   /* Check assumptions.                                                      */
61   /***************************************************************************/
62   assert(list != NULL);
63
64   /***************************************************************************/
65   /* Initialize the list as empty.                                           */
66   /***************************************************************************/
67   list->head = NULL;
68   list->tail = NULL;
69
70   EVEL_EXIT();
71 }
72
73 void * dlist_pop_last(DLIST * list)
74 {
75   void *item = NULL;
76   DLIST_ITEM *current_tail = NULL;
77   DLIST_ITEM *new_tail = NULL;
78
79   assert(list != NULL);
80
81   current_tail = list->tail;
82   if (current_tail != NULL)
83   {
84     item = current_tail->item;
85     new_tail = current_tail->previous;
86     if (new_tail == NULL)
87     {
88       list->head = NULL;
89       list->tail = NULL;
90     }
91     else
92     {
93       new_tail->next = NULL;
94       list->tail = new_tail;
95     }
96     free(current_tail);
97   }
98
99   return item;
100 }
101
102 void dlist_push_first(DLIST * list, void * item)
103 {
104   DLIST_ITEM * new_element = NULL;
105   DLIST_ITEM * current_head = NULL;
106
107   /***************************************************************************/
108   /* Check assumptions.  Note that we do allow putting NULL pointers into    */
109   /* the list - not sure you'd want to, but let it happen.                   */
110   /***************************************************************************/
111   assert(list != NULL);
112
113   current_head = list->head;
114
115   new_element = malloc(sizeof(DLIST_ITEM));
116   assert(new_element != NULL);
117   new_element->next = current_head;
118   new_element->previous = NULL;
119   new_element->item = item;
120   list->head = new_element;
121
122   if (current_head != NULL)
123   {
124     current_head->previous = new_element;
125   }
126   else
127   {
128     list->tail = new_element;
129   }
130 }
131
132 void dlist_push_last(DLIST * list, void * item)
133 {
134   DLIST_ITEM * new_element = NULL;
135   DLIST_ITEM * current_tail = NULL;
136
137   /***************************************************************************/
138   /* Check assumptions.  Note that we do allow putting NULL pointers into    */
139   /* the list - not sure you'd want to, but let it happen.                   */
140   /***************************************************************************/
141   assert(list != NULL);
142
143   current_tail = list->tail;
144
145   new_element = malloc(sizeof(DLIST_ITEM));
146   assert(new_element != NULL);
147   new_element->next = NULL;
148   new_element->previous = current_tail;
149   new_element->item = item;
150   list->tail = new_element;
151
152   if (current_tail != NULL)
153   {
154     current_tail->next = new_element;
155   }
156   else
157   {
158     list->head = new_element;
159   }
160 }
161
162 DLIST_ITEM * dlist_get_first(DLIST * list)
163 {
164   return list->head;
165 }
166
167 DLIST_ITEM * dlist_get_last(DLIST * list)
168 {
169   return list->tail;
170 }
171
172 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
173 {
174   return item->next;
175 }
176
177 int dlist_is_empty(DLIST * list)
178 {
179   return (list->head == NULL);
180 }
181
182 int dlist_count(DLIST * list)
183 {
184   int count = 0;
185   DLIST_ITEM * item = list->head;
186
187   while (item != NULL)
188   {
189     count++;
190     item = item->next;
191   }
192
193   return count;
194 }