fedora-spec: add missing buildrequires
[cascardo/ovs.git] / lib / list.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <config.h>
17 #include "list.h"
18
19 /* Initializes 'list' as an empty list. */
20 void
21 list_init(struct list *list)
22 {
23     list->next = list->prev = list;
24 }
25
26 /* Initializes 'list' with pointers that will (probably) cause segfaults if
27  * dereferenced and, better yet, show up clearly in a debugger. */
28 void
29 list_poison(struct list *list)
30 {
31     memset(list, 0xcc, sizeof *list);
32 }
33
34 /* Inserts 'elem' just before 'before'. */
35 void
36 list_insert(struct list *before, struct list *elem)
37 {
38     elem->prev = before->prev;
39     elem->next = before;
40     before->prev->next = elem;
41     before->prev = elem;
42 }
43
44 /* Removes elements 'first' though 'last' (exclusive) from their current list,
45    then inserts them just before 'before'. */
46 void
47 list_splice(struct list *before, struct list *first, struct list *last)
48 {
49     if (first == last) {
50         return;
51     }
52     last = last->prev;
53
54     /* Cleanly remove 'first'...'last' from its current list. */
55     first->prev->next = last->next;
56     last->next->prev = first->prev;
57
58     /* Splice 'first'...'last' into new list. */
59     first->prev = before->prev;
60     last->next = before;
61     before->prev->next = first;
62     before->prev = last;
63 }
64
65 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
66    'list'. */
67 void
68 list_push_front(struct list *list, struct list *elem)
69 {
70     list_insert(list->next, elem);
71 }
72
73 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
74  * 'list'. */
75 void
76 list_push_back(struct list *list, struct list *elem)
77 {
78     list_insert(list, elem);
79 }
80
81 /* Puts 'elem' in the position currently occupied by 'position'.
82  * Afterward, 'position' is not part of a list. */
83 void
84 list_replace(struct list *element, const struct list *position)
85 {
86     element->next = position->next;
87     element->next->prev = element;
88     element->prev = position->prev;
89     element->prev->next = element;
90 }
91
92 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
93  * around in memory (e.g. as a consequence of realloc()), with original
94  * location 'orig'.
95  *
96  * ('orig' likely points to freed memory, but this function does not
97  * dereference 'orig', it only compares it to 'list'.  In a very pedantic
98  * language lawyer sense, this still yields undefined behavior, but it works
99  * with actual compilers.) */
100 void
101 list_moved(struct list *list, const struct list *orig)
102 {
103     if (list->next == orig) {
104         list_init(list);
105     } else {
106         list->prev->next = list->next->prev = list;
107     }
108 }
109
110 /* Initializes 'dst' with the contents of 'src', compensating for moving it
111  * around in memory.  The effect is that, if 'src' was the head of a list, now
112  * 'dst' is the head of a list containing the same elements. */
113 void
114 list_move(struct list *dst, struct list *src)
115 {
116     *dst = *src;
117     list_moved(dst, src);
118 }
119
120 /* Removes 'elem' from its list and returns the element that followed it.
121    Undefined behavior if 'elem' is not in a list. */
122 struct list *
123 list_remove(struct list *elem)
124 {
125     elem->prev->next = elem->next;
126     elem->next->prev = elem->prev;
127     return elem->next;
128 }
129
130 /* Removes the front element from 'list' and returns it.  Undefined behavior if
131    'list' is empty before removal. */
132 struct list *
133 list_pop_front(struct list *list)
134 {
135     struct list *front = list->next;
136     list_remove(front);
137     return front;
138 }
139
140 /* Removes the back element from 'list' and returns it.
141    Undefined behavior if 'list' is empty before removal. */
142 struct list *
143 list_pop_back(struct list *list)
144 {
145     struct list *back = list->prev;
146     list_remove(back);
147     return back;
148 }
149
150 /* Returns the front element in 'list_'.
151    Undefined behavior if 'list_' is empty. */
152 struct list *
153 list_front(const struct list *list_)
154 {
155     struct list *list = CONST_CAST(struct list *, list_);
156
157     ovs_assert(!list_is_empty(list));
158     return list->next;
159 }
160
161 /* Returns the back element in 'list_'.
162    Undefined behavior if 'list_' is empty. */
163 struct list *
164 list_back(const struct list *list_)
165 {
166     struct list *list = CONST_CAST(struct list *, list_);
167
168     ovs_assert(!list_is_empty(list));
169     return list->prev;
170 }
171
172 /* Returns the number of elements in 'list'.
173    Runs in O(n) in the number of elements. */
174 size_t
175 list_size(const struct list *list)
176 {
177     const struct list *e;
178     size_t cnt = 0;
179
180     for (e = list->next; e != list; e = e->next) {
181         cnt++;
182     }
183     return cnt;
184 }
185
186 /* Returns true if 'list' is empty, false otherwise. */
187 bool
188 list_is_empty(const struct list *list)
189 {
190     return list->next == list;
191 }
192
193 /* Returns true if 'list' has exactly 1 element, false otherwise. */
194 bool
195 list_is_singleton(const struct list *list)
196 {
197     return list_is_short(list) && !list_is_empty(list);
198 }
199
200 /* Returns true if 'list' has 0 or 1 elements, false otherwise. */
201 bool
202 list_is_short(const struct list *list)
203 {
204     return list->next == list->prev;
205 }