For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / lib / list.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33 #include <config.h>
34 #include "list.h"
35 #include <assert.h>
36
37 /* Initializes 'list' as an empty list. */
38 void
39 list_init(struct list *list)
40 {
41     list->next = list->prev = list;
42 }
43
44 /* Inserts 'elem' just before 'before'. */
45 void
46 list_insert(struct list *before, struct list *elem)
47 {
48   elem->prev = before->prev;
49   elem->next = before;
50   before->prev->next = elem;
51   before->prev = elem;
52 }
53
54 /* Removes elements 'first' though 'last' (exclusive) from their current list,
55    then inserts them just before 'before'. */
56 void
57 list_splice(struct list *before, struct list *first, struct list *last)
58 {
59   if (first == last)
60     return;
61   last = last->prev;
62
63   /* Cleanly remove 'first'...'last' from its current list. */
64   first->prev->next = last->next;
65   last->next->prev = first->prev;
66
67   /* Splice 'first'...'last' into new list. */
68   first->prev = before->prev;
69   last->next = before;
70   before->prev->next = first;
71   before->prev = last;
72 }
73
74 /* Inserts 'elem' at the beginning of 'list', so that it becomes the front in
75    'list'. */
76 void
77 list_push_front(struct list *list, struct list *elem)
78 {
79   list_insert(list->next, elem);
80 }
81
82 /* Inserts 'elem' at the end of 'list', so that it becomes the back in
83  * 'list'. */
84 void
85 list_push_back(struct list *list, struct list *elem)
86 {
87   list_insert(list, elem);
88 }
89
90 /* Puts 'elem' in the position currently occupied by 'position'.
91  * Afterward, 'position' is not part of a list. */
92 void
93 list_replace(struct list *element, const struct list *position)
94 {
95     element->next = position->next;
96     element->next->prev = element;
97     element->prev = position->prev;
98     element->prev->next = element;
99 }
100
101 /* Removes 'elem' from its list and returns the element that followed it.
102    Undefined behavior if 'elem' is not in a list. */
103 struct list *
104 list_remove(struct list *elem)
105 {
106   elem->prev->next = elem->next;
107   elem->next->prev = elem->prev;
108   return elem->next;
109 }
110
111 /* Removes the front element from 'list' and returns it.  Undefined behavior if
112    'list' is empty before removal. */
113 struct list *
114 list_pop_front(struct list *list)
115 {
116   struct list *front = list->next;
117   list_remove(front);
118   return front;
119 }
120
121 /* Removes the back element from 'list' and returns it.
122    Undefined behavior if 'list' is empty before removal. */
123 struct list *
124 list_pop_back(struct list *list)
125 {
126   struct list *back = list->prev;
127   list_remove(back);
128   return back;
129 }
130
131 /* Returns the front element in 'list'.
132    Undefined behavior if 'list' is empty. */
133 struct list *
134 list_front(struct list *list)
135 {
136   assert(!list_is_empty(list));
137   return list->next;
138 }
139
140 /* Returns the back element in 'list'.
141    Undefined behavior if 'list' is empty. */
142 struct list *
143 list_back(struct list *list)
144 {
145   assert(!list_is_empty(list));
146   return list->prev;
147 }
148
149 /* Returns the number of elements in 'list'.
150    Runs in O(n) in the number of elements. */
151 size_t
152 list_size(const struct list *list)
153 {
154   const struct list *e;
155   size_t cnt = 0;
156
157   for (e = list->next; e != list; e = e->next)
158     cnt++;
159   return cnt;
160 }
161
162 /* Returns true if 'list' is empty, false otherwise. */
163 bool
164 list_is_empty(const struct list *list)
165 {
166   return list->next == list;
167 }