2 * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 /* Doubly linked list. */
24 #include "openvswitch/list.h"
26 /* "struct ovs_list" with pointers that will (probably) cause segfaults if
27 * dereferenced and, better yet, show up clearly in a debugger. */
28 #define OVS_LIST_POISON \
29 (struct ovs_list) { (void *) (uintptr_t) 0xccccccccccccccccULL, \
30 (void *) (uintptr_t) 0xccccccccccccccccULL }
32 static inline void list_init(struct ovs_list *);
33 static inline void list_poison(struct ovs_list *);
36 static inline void list_insert(struct ovs_list *, struct ovs_list *);
37 static inline void list_splice(struct ovs_list *before, struct ovs_list *first,
38 struct ovs_list *last);
39 static inline void list_push_front(struct ovs_list *, struct ovs_list *);
40 static inline void list_push_back(struct ovs_list *, struct ovs_list *);
41 static inline void list_replace(struct ovs_list *, const struct ovs_list *);
42 static inline void list_moved(struct ovs_list *, const struct ovs_list *orig);
43 static inline void list_move(struct ovs_list *dst, struct ovs_list *src);
46 static inline struct ovs_list *list_remove(struct ovs_list *);
47 static inline struct ovs_list *list_pop_front(struct ovs_list *);
48 static inline struct ovs_list *list_pop_back(struct ovs_list *);
51 static inline struct ovs_list *list_front(const struct ovs_list *);
52 static inline struct ovs_list *list_back(const struct ovs_list *);
54 /* List properties. */
55 static inline size_t list_size(const struct ovs_list *);
56 static inline bool list_is_empty(const struct ovs_list *);
57 static inline bool list_is_singleton(const struct ovs_list *);
58 static inline bool list_is_short(const struct ovs_list *);
60 #define LIST_FOR_EACH(ITER, MEMBER, LIST) \
61 for (INIT_CONTAINER(ITER, (LIST)->next, MEMBER); \
62 &(ITER)->MEMBER != (LIST); \
63 ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
64 #define LIST_FOR_EACH_CONTINUE(ITER, MEMBER, LIST) \
65 for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER); \
66 &(ITER)->MEMBER != (LIST); \
67 ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
68 #define LIST_FOR_EACH_REVERSE(ITER, MEMBER, LIST) \
69 for (INIT_CONTAINER(ITER, (LIST)->prev, MEMBER); \
70 &(ITER)->MEMBER != (LIST); \
71 ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
72 #define LIST_FOR_EACH_REVERSE_CONTINUE(ITER, MEMBER, LIST) \
73 for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER); \
74 &(ITER)->MEMBER != (LIST); \
75 ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.prev, MEMBER))
76 #define LIST_FOR_EACH_SAFE(ITER, NEXT, MEMBER, LIST) \
77 for (INIT_CONTAINER(ITER, (LIST)->next, MEMBER); \
78 (&(ITER)->MEMBER != (LIST) \
79 ? INIT_CONTAINER(NEXT, (ITER)->MEMBER.next, MEMBER), 1 \
82 #define LIST_FOR_EACH_POP(ITER, MEMBER, LIST) \
83 while (!list_is_empty(LIST) \
84 && (INIT_CONTAINER(ITER, list_pop_front(LIST), MEMBER), 1))
86 /* Inline implementations. */
88 /* Initializes 'list' as an empty list. */
90 list_init(struct ovs_list *list)
92 list->next = list->prev = list;
95 /* Initializes 'list' with pointers that will (probably) cause segfaults if
96 * dereferenced and, better yet, show up clearly in a debugger. */
98 list_poison(struct ovs_list *list)
100 *list = OVS_LIST_POISON;
103 /* Inserts 'elem' just before 'before'. */
105 list_insert(struct ovs_list *before, struct ovs_list *elem)
107 elem->prev = before->prev;
109 before->prev->next = elem;
113 /* Removes elements 'first' though 'last' (exclusive) from their current list,
114 then inserts them just before 'before'. */
116 list_splice(struct ovs_list *before, struct ovs_list *first, struct ovs_list *last)
123 /* Cleanly remove 'first'...'last' from its current list. */
124 first->prev->next = last->next;
125 last->next->prev = first->prev;
127 /* Splice 'first'...'last' into new list. */
128 first->prev = before->prev;
130 before->prev->next = first;
134 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
137 list_push_front(struct ovs_list *list, struct ovs_list *elem)
139 list_insert(list->next, elem);
142 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
145 list_push_back(struct ovs_list *list, struct ovs_list *elem)
147 list_insert(list, elem);
150 /* Puts 'elem' in the position currently occupied by 'position'.
151 * Afterward, 'position' is not part of a list. */
153 list_replace(struct ovs_list *element, const struct ovs_list *position)
155 element->next = position->next;
156 element->next->prev = element;
157 element->prev = position->prev;
158 element->prev->next = element;
161 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
162 * around in memory (e.g. as a consequence of realloc()), with original
165 * ('orig' likely points to freed memory, but this function does not
166 * dereference 'orig', it only compares it to 'list'. In a very pedantic
167 * language lawyer sense, this still yields undefined behavior, but it works
168 * with actual compilers.) */
170 list_moved(struct ovs_list *list, const struct ovs_list *orig)
172 if (list->next == orig) {
175 list->prev->next = list->next->prev = list;
179 /* Initializes 'dst' with the contents of 'src', compensating for moving it
180 * around in memory. The effect is that, if 'src' was the head of a list, now
181 * 'dst' is the head of a list containing the same elements. */
183 list_move(struct ovs_list *dst, struct ovs_list *src)
186 list_moved(dst, src);
189 /* Removes 'elem' from its list and returns the element that followed it.
190 Undefined behavior if 'elem' is not in a list. */
191 static inline struct ovs_list *
192 list_remove(struct ovs_list *elem)
194 elem->prev->next = elem->next;
195 elem->next->prev = elem->prev;
199 /* Removes the front element from 'list' and returns it. Undefined behavior if
200 'list' is empty before removal. */
201 static inline struct ovs_list *
202 list_pop_front(struct ovs_list *list)
204 struct ovs_list *front = list->next;
210 /* Removes the back element from 'list' and returns it.
211 Undefined behavior if 'list' is empty before removal. */
212 static inline struct ovs_list *
213 list_pop_back(struct ovs_list *list)
215 struct ovs_list *back = list->prev;
221 /* Returns the front element in 'list_'.
222 Undefined behavior if 'list_' is empty. */
223 static inline struct ovs_list *
224 list_front(const struct ovs_list *list_)
226 struct ovs_list *list = CONST_CAST(struct ovs_list *, list_);
228 ovs_assert(!list_is_empty(list));
233 /* Returns the back element in 'list_'.
234 Undefined behavior if 'list_' is empty. */
235 static inline struct ovs_list *
236 list_back(const struct ovs_list *list_)
238 struct ovs_list *list = CONST_CAST(struct ovs_list *, list_);
240 ovs_assert(!list_is_empty(list));
245 /* Returns the number of elements in 'list'.
246 Runs in O(n) in the number of elements. */
248 list_size(const struct ovs_list *list)
250 const struct ovs_list *e;
253 for (e = list->next; e != list; e = e->next) {
259 /* Returns true if 'list' is empty, false otherwise. */
261 list_is_empty(const struct ovs_list *list)
263 return list->next == list;
266 /* Returns true if 'list' has exactly 1 element, false otherwise. */
268 list_is_singleton(const struct ovs_list *list)
270 return list_is_short(list) && !list_is_empty(list);
273 /* Returns true if 'list' has 0 or 1 elements, false otherwise. */
275 list_is_short(const struct ovs_list *list)
277 return list->next == list->prev;