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