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