]>
git.proxmox.com Git - ovs.git/blob - lib/list.h
2 * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2015, 2016 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.
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) };
36 static inline void list_init(struct ovs_list
*);
37 static inline void list_poison(struct ovs_list
*);
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
);
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
*);
55 static inline struct ovs_list
*list_front(const struct ovs_list
*);
56 static inline struct ovs_list
*list_back(const struct ovs_list
*);
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
*);
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 \
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))
90 /* Inline implementations. */
92 /* Initializes 'list' as an empty list. */
94 list_init(struct ovs_list
*list
)
96 list
->next
= list
->prev
= list
;
99 /* Initializes 'list' with pointers that will (probably) cause segfaults if
100 * dereferenced and, better yet, show up clearly in a debugger. */
102 list_poison(struct ovs_list
*list
)
104 *list
= OVS_LIST_POISON
;
107 /* Inserts 'elem' just before 'before'. */
109 list_insert(struct ovs_list
*before
, struct ovs_list
*elem
)
111 elem
->prev
= before
->prev
;
113 before
->prev
->next
= elem
;
117 /* Removes elements 'first' though 'last' (exclusive) from their current list,
118 then inserts them just before 'before'. */
120 list_splice(struct ovs_list
*before
, struct ovs_list
*first
, struct ovs_list
*last
)
127 /* Cleanly remove 'first'...'last' from its current list. */
128 first
->prev
->next
= last
->next
;
129 last
->next
->prev
= first
->prev
;
131 /* Splice 'first'...'last' into new list. */
132 first
->prev
= before
->prev
;
134 before
->prev
->next
= first
;
138 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
141 list_push_front(struct ovs_list
*list
, struct ovs_list
*elem
)
143 list_insert(list
->next
, elem
);
146 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
149 list_push_back(struct ovs_list
*list
, struct ovs_list
*elem
)
151 list_insert(list
, elem
);
154 /* Puts 'elem' in the position currently occupied by 'position'.
155 * Afterward, 'position' is not part of a list. */
157 list_replace(struct ovs_list
*element
, const struct ovs_list
*position
)
159 element
->next
= position
->next
;
160 element
->next
->prev
= element
;
161 element
->prev
= position
->prev
;
162 element
->prev
->next
= element
;
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
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.) */
174 list_moved(struct ovs_list
*list
, const struct ovs_list
*orig
)
176 if (list
->next
== orig
) {
179 list
->prev
->next
= list
->next
->prev
= list
;
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. */
187 list_move(struct ovs_list
*dst
, struct ovs_list
*src
)
190 list_moved(dst
, src
);
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
)
198 elem
->prev
->next
= elem
->next
;
199 elem
->next
->prev
= elem
->prev
;
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
)
208 struct ovs_list
*front
= list
->next
;
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
)
219 struct ovs_list
*back
= list
->prev
;
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_
)
230 struct ovs_list
*list
= CONST_CAST(struct ovs_list
*, list_
);
232 ovs_assert(!list_is_empty(list
));
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_
)
242 struct ovs_list
*list
= CONST_CAST(struct ovs_list
*, list_
);
244 ovs_assert(!list_is_empty(list
));
249 /* Returns the number of elements in 'list'.
250 Runs in O(n) in the number of elements. */
252 list_size(const struct ovs_list
*list
)
254 const struct ovs_list
*e
;
257 for (e
= list
->next
; e
!= list
; e
= e
->next
) {
263 /* Returns true if 'list' is empty, false otherwise. */
265 list_is_empty(const struct ovs_list
*list
)
267 return list
->next
== list
;
270 /* Returns true if 'list' has exactly 1 element, false otherwise. */
272 list_is_singleton(const struct ovs_list
*list
)
274 return list_is_short(list
) && !list_is_empty(list
);
277 /* Returns true if 'list' has 0 or 1 elements, false otherwise. */
279 list_is_short(const struct ovs_list
*list
)
281 return list
->next
== list
->prev
;