5c2cca49cc89a15e238a0d277817bff4d9b31abf
[cascardo/ovs.git] / include / openvswitch / list.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2015, 2016 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 #ifndef OPENVSWITCH_LIST_H
17 #define OPENVSWITCH_LIST_H 1
18
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stddef.h>
22 #include <openvswitch/types.h>
23 #include <openvswitch/util.h>
24
25 /* Doubly linked list head or element. */
26 struct ovs_list {
27     struct ovs_list *prev;     /* Previous list element. */
28     struct ovs_list *next;     /* Next list element. */
29 };
30
31 #define OVS_LIST_INITIALIZER(LIST) { LIST, LIST }
32
33 /* "struct ovs_list" with pointers that will (probably) cause segfaults if
34  * dereferenced and, better yet, show up clearly in a debugger.
35
36  * MSVC2015 doesn't support designated initializers when compiling C++,
37  * and doesn't support ternary operators with non-designated initializers.
38  * So we use these static definitions rather than using initializer macros. */
39 static const struct ovs_list OVS_LIST_POISON =
40     { (struct ovs_list *) (UINTPTR_MAX / 0xf * 0xc),
41       (struct ovs_list *) (UINTPTR_MAX / 0xf * 0xc) };
42
43 static inline void ovs_list_init(struct ovs_list *);
44 static inline void ovs_list_poison(struct ovs_list *);
45
46 /* List insertion. */
47 static inline void ovs_list_insert(struct ovs_list *, struct ovs_list *);
48 static inline void ovs_list_splice(struct ovs_list *before, struct ovs_list *first,
49                                struct ovs_list *last);
50 static inline void ovs_list_push_front(struct ovs_list *, struct ovs_list *);
51 static inline void ovs_list_push_back(struct ovs_list *, struct ovs_list *);
52 static inline void ovs_list_replace(struct ovs_list *, const struct ovs_list *);
53 static inline void ovs_list_moved(struct ovs_list *, const struct ovs_list *orig);
54 static inline void ovs_list_move(struct ovs_list *dst, struct ovs_list *src);
55
56 /* List removal. */
57 static inline struct ovs_list *ovs_list_remove(struct ovs_list *);
58 static inline struct ovs_list *ovs_list_pop_front(struct ovs_list *);
59 static inline struct ovs_list *ovs_list_pop_back(struct ovs_list *);
60
61 /* List elements. */
62 static inline struct ovs_list *ovs_list_front(const struct ovs_list *);
63 static inline struct ovs_list *ovs_list_back(const struct ovs_list *);
64
65 /* List properties. */
66 static inline size_t ovs_list_size(const struct ovs_list *);
67 static inline bool ovs_list_is_empty(const struct ovs_list *);
68 static inline bool ovs_list_is_singleton(const struct ovs_list *);
69 static inline bool ovs_list_is_short(const struct ovs_list *);
70
71 #define LIST_FOR_EACH(ITER, MEMBER, LIST)                               \
72     for (INIT_CONTAINER(ITER, (LIST)->next, MEMBER);                    \
73          &(ITER)->MEMBER != (LIST);                                     \
74          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
75 #define LIST_FOR_EACH_CONTINUE(ITER, MEMBER, LIST)                      \
76     for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER);             \
77          &(ITER)->MEMBER != (LIST);                                     \
78          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
79 #define LIST_FOR_EACH_REVERSE(ITER, MEMBER, LIST)                       \
80     for (INIT_CONTAINER(ITER, (LIST)->prev, MEMBER);                    \
81          &(ITER)->MEMBER != (LIST);                                     \
82          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
83 #define LIST_FOR_EACH_REVERSE_CONTINUE(ITER, MEMBER, LIST)              \
84     for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER);           \
85          &(ITER)->MEMBER != (LIST);                                     \
86          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
87 #define LIST_FOR_EACH_SAFE(ITER, NEXT, MEMBER, LIST)               \
88     for (INIT_CONTAINER(ITER, (LIST)->next, MEMBER);               \
89          (&(ITER)->MEMBER != (LIST)                                \
90           ? INIT_CONTAINER(NEXT, (ITER)->MEMBER.next, MEMBER), 1   \
91           : 0);                                                    \
92          (ITER) = (NEXT))
93 #define LIST_FOR_EACH_POP(ITER, MEMBER, LIST)                      \
94     while (!ovs_list_is_empty(LIST)                                    \
95            && (INIT_CONTAINER(ITER, ovs_list_pop_front(LIST), MEMBER), 1))
96 \f
97 /* Inline implementations. */
98
99 /* Initializes 'list' as an empty list. */
100 static inline void
101 ovs_list_init(struct ovs_list *list)
102 {
103     list->next = list->prev = list;
104 }
105
106 /* Initializes 'list' with pointers that will (probably) cause segfaults if
107  * dereferenced and, better yet, show up clearly in a debugger. */
108 static inline void
109 ovs_list_poison(struct ovs_list *list)
110 {
111     *list = OVS_LIST_POISON;
112 }
113
114 /* Inserts 'elem' just before 'before'. */
115 static inline void
116 ovs_list_insert(struct ovs_list *before, struct ovs_list *elem)
117 {
118     elem->prev = before->prev;
119     elem->next = before;
120     before->prev->next = elem;
121     before->prev = elem;
122 }
123
124 /* Removes elements 'first' though 'last' (exclusive) from their current list,
125    then inserts them just before 'before'. */
126 static inline void
127 ovs_list_splice(struct ovs_list *before, struct ovs_list *first, struct ovs_list *last)
128 {
129     if (first == last) {
130         return;
131     }
132     last = last->prev;
133
134     /* Cleanly remove 'first'...'last' from its current list. */
135     first->prev->next = last->next;
136     last->next->prev = first->prev;
137
138     /* Splice 'first'...'last' into new list. */
139     first->prev = before->prev;
140     last->next = before;
141     before->prev->next = first;
142     before->prev = last;
143 }
144
145 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
146    'list'. */
147 static inline void
148 ovs_list_push_front(struct ovs_list *list, struct ovs_list *elem)
149 {
150     ovs_list_insert(list->next, elem);
151 }
152
153 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
154  * 'list'. */
155 static inline void
156 ovs_list_push_back(struct ovs_list *list, struct ovs_list *elem)
157 {
158     ovs_list_insert(list, elem);
159 }
160
161 /* Puts 'elem' in the position currently occupied by 'position'.
162  * Afterward, 'position' is not part of a list. */
163 static inline void
164 ovs_list_replace(struct ovs_list *element, const struct ovs_list *position)
165 {
166     element->next = position->next;
167     element->next->prev = element;
168     element->prev = position->prev;
169     element->prev->next = element;
170 }
171
172 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
173  * around in memory (e.g. as a consequence of realloc()), with original
174  * location 'orig'.
175  *
176  * ('orig' likely points to freed memory, but this function does not
177  * dereference 'orig', it only compares it to 'list'.  In a very pedantic
178  * language lawyer sense, this still yields undefined behavior, but it works
179  * with actual compilers.) */
180 static inline void
181 ovs_list_moved(struct ovs_list *list, const struct ovs_list *orig)
182 {
183     if (list->next == orig) {
184         ovs_list_init(list);
185     } else {
186         list->prev->next = list->next->prev = list;
187     }
188 }
189
190 /* Initializes 'dst' with the contents of 'src', compensating for moving it
191  * around in memory.  The effect is that, if 'src' was the head of a list, now
192  * 'dst' is the head of a list containing the same elements. */
193 static inline void
194 ovs_list_move(struct ovs_list *dst, struct ovs_list *src)
195 {
196     *dst = *src;
197     ovs_list_moved(dst, src);
198 }
199
200 /* Removes 'elem' from its list and returns the element that followed it.
201    Undefined behavior if 'elem' is not in a list. */
202 static inline struct ovs_list *
203 ovs_list_remove(struct ovs_list *elem)
204 {
205     elem->prev->next = elem->next;
206     elem->next->prev = elem->prev;
207     return elem->next;
208 }
209
210 /* Removes the front element from 'list' and returns it.  Undefined behavior if
211    'list' is empty before removal. */
212 static inline struct ovs_list *
213 ovs_list_pop_front(struct ovs_list *list)
214 {
215     struct ovs_list *front = list->next;
216
217     ovs_list_remove(front);
218     return front;
219 }
220
221 /* Removes the back element from 'list' and returns it.
222    Undefined behavior if 'list' is empty before removal. */
223 static inline struct ovs_list *
224 ovs_list_pop_back(struct ovs_list *list)
225 {
226     struct ovs_list *back = list->prev;
227
228     ovs_list_remove(back);
229     return back;
230 }
231
232 /* Returns the front element in 'list_'.
233    Undefined behavior if 'list_' is empty. */
234 static inline struct ovs_list *
235 ovs_list_front(const struct ovs_list *list_)
236 {
237     struct ovs_list *list = CONST_CAST(struct ovs_list *, list_);
238
239     ovs_assert(!ovs_list_is_empty(list));
240
241     return list->next;
242 }
243
244 /* Returns the back element in 'list_'.
245    Undefined behavior if 'list_' is empty. */
246 static inline struct ovs_list *
247 ovs_list_back(const struct ovs_list *list_)
248 {
249     struct ovs_list *list = CONST_CAST(struct ovs_list *, list_);
250
251     ovs_assert(!ovs_list_is_empty(list));
252
253     return list->prev;
254 }
255
256 /* Returns the number of elements in 'list'.
257    Runs in O(n) in the number of elements. */
258 static inline size_t
259 ovs_list_size(const struct ovs_list *list)
260 {
261     const struct ovs_list *e;
262     size_t cnt = 0;
263
264     for (e = list->next; e != list; e = e->next) {
265         cnt++;
266     }
267     return cnt;
268 }
269
270 /* Returns true if 'list' is empty, false otherwise. */
271 static inline bool
272 ovs_list_is_empty(const struct ovs_list *list)
273 {
274     return list->next == list;
275 }
276
277 /* Returns true if 'list' has exactly 1 element, false otherwise. */
278 static inline bool
279 ovs_list_is_singleton(const struct ovs_list *list)
280 {
281     return ovs_list_is_short(list) && !ovs_list_is_empty(list);
282 }
283
284 /* Returns true if 'list' has 0 or 1 elements, false otherwise. */
285 static inline bool
286 ovs_list_is_short(const struct ovs_list *list)
287 {
288     return list->next == list->prev;
289 }
290
291 #endif /* list.h */