2 * Copyright (c) 2008, 2009 Nicira Networks.
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.
20 /* Initializes 'list' as an empty list. */
22 list_init(struct list *list)
24 list->next = list->prev = list;
27 /* Inserts 'elem' just before 'before'. */
29 list_insert(struct list *before, struct list *elem)
31 elem->prev = before->prev;
33 before->prev->next = elem;
37 /* Removes elements 'first' though 'last' (exclusive) from their current list,
38 then inserts them just before 'before'. */
40 list_splice(struct list *before, struct list *first, struct list *last)
46 /* Cleanly remove 'first'...'last' from its current list. */
47 first->prev->next = last->next;
48 last->next->prev = first->prev;
50 /* Splice 'first'...'last' into new list. */
51 first->prev = before->prev;
53 before->prev->next = first;
57 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
60 list_push_front(struct list *list, struct list *elem)
62 list_insert(list->next, elem);
65 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
68 list_push_back(struct list *list, struct list *elem)
70 list_insert(list, elem);
73 /* Puts 'elem' in the position currently occupied by 'position'.
74 * Afterward, 'position' is not part of a list. */
76 list_replace(struct list *element, const struct list *position)
78 element->next = position->next;
79 element->next->prev = element;
80 element->prev = position->prev;
81 element->prev->next = element;
84 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
85 * around in memory (e.g. as a consequence of realloc()). */
87 list_moved(struct list *list)
89 list->prev->next = list->next->prev = list;
92 /* Removes 'elem' from its list and returns the element that followed it.
93 Undefined behavior if 'elem' is not in a list. */
95 list_remove(struct list *elem)
97 elem->prev->next = elem->next;
98 elem->next->prev = elem->prev;
102 /* Removes the front element from 'list' and returns it. Undefined behavior if
103 'list' is empty before removal. */
105 list_pop_front(struct list *list)
107 struct list *front = list->next;
112 /* Removes the back element from 'list' and returns it.
113 Undefined behavior if 'list' is empty before removal. */
115 list_pop_back(struct list *list)
117 struct list *back = list->prev;
122 /* Returns the front element in 'list'.
123 Undefined behavior if 'list' is empty. */
125 list_front(struct list *list)
127 assert(!list_is_empty(list));
131 /* Returns the back element in 'list'.
132 Undefined behavior if 'list' is empty. */
134 list_back(struct list *list)
136 assert(!list_is_empty(list));
140 /* Returns the number of elements in 'list'.
141 Runs in O(n) in the number of elements. */
143 list_size(const struct list *list)
145 const struct list *e;
148 for (e = list->next; e != list; e = e->next)
153 /* Returns true if 'list' is empty, false otherwise. */
155 list_is_empty(const struct list *list)
157 return list->next == list;