2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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 /* Initializes 'list' as an empty list. */
21 list_init(struct list *list)
23 list->next = list->prev = list;
26 /* Initializes 'list' with pointers that will (probably) cause segfaults if
27 * dereferenced and, better yet, show up clearly in a debugger. */
29 list_poison(struct list *list)
31 memset(list, 0xcc, sizeof *list);
34 /* Inserts 'elem' just before 'before'. */
36 list_insert(struct list *before, struct list *elem)
38 elem->prev = before->prev;
40 before->prev->next = elem;
44 /* Removes elements 'first' though 'last' (exclusive) from their current list,
45 then inserts them just before 'before'. */
47 list_splice(struct list *before, struct list *first, struct list *last)
53 /* Cleanly remove 'first'...'last' from its current list. */
54 first->prev->next = last->next;
55 last->next->prev = first->prev;
57 /* Splice 'first'...'last' into new list. */
58 first->prev = before->prev;
60 before->prev->next = first;
64 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
67 list_push_front(struct list *list, struct list *elem)
69 list_insert(list->next, elem);
72 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
75 list_push_back(struct list *list, struct list *elem)
77 list_insert(list, elem);
80 /* Puts 'elem' in the position currently occupied by 'position'.
81 * Afterward, 'position' is not part of a list. */
83 list_replace(struct list *element, const struct list *position)
85 element->next = position->next;
86 element->next->prev = element;
87 element->prev = position->prev;
88 element->prev->next = element;
91 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
92 * around in memory (e.g. as a consequence of realloc()).
94 * This always works if 'list' is a member of a list, or if 'list' is the head
95 * of a non-empty list. It fails badly, however, if 'list' is the head of an
96 * empty list; just use list_init() in that case. */
98 list_moved(struct list *list)
100 list->prev->next = list->next->prev = list;
103 /* Removes 'elem' from its list and returns the element that followed it.
104 Undefined behavior if 'elem' is not in a list. */
106 list_remove(struct list *elem)
108 elem->prev->next = elem->next;
109 elem->next->prev = elem->prev;
113 /* Removes the front element from 'list' and returns it. Undefined behavior if
114 'list' is empty before removal. */
116 list_pop_front(struct list *list)
118 struct list *front = list->next;
123 /* Removes the back element from 'list' and returns it.
124 Undefined behavior if 'list' is empty before removal. */
126 list_pop_back(struct list *list)
128 struct list *back = list->prev;
133 /* Returns the front element in 'list_'.
134 Undefined behavior if 'list_' is empty. */
136 list_front(const struct list *list_)
138 struct list *list = CONST_CAST(struct list *, list_);
140 ovs_assert(!list_is_empty(list));
144 /* Returns the back element in 'list_'.
145 Undefined behavior if 'list_' is empty. */
147 list_back(const struct list *list_)
149 struct list *list = CONST_CAST(struct list *, list_);
151 ovs_assert(!list_is_empty(list));
155 /* Returns the number of elements in 'list'.
156 Runs in O(n) in the number of elements. */
158 list_size(const struct list *list)
160 const struct list *e;
163 for (e = list->next; e != list; e = e->next)
168 /* Returns true if 'list' is empty, false otherwise. */
170 list_is_empty(const struct list *list)
172 return list->next == list;
175 /* Returns true if 'list' has exactly 1 element, false otherwise. */
177 list_is_singleton(const struct list *list)
179 return list_is_short(list) && !list_is_empty(list);
182 /* Returns true if 'list' has 0 or 1 elements, false otherwise. */
184 list_is_short(const struct list *list)
186 return list->next == list->prev;