BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file LinkedList0.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Very simple doubly linked list, with only a 'first' pointer an no 'last'
32 * pointer.
33 */
34  
35 #ifndef BADVPN_STRUCTURE_LINKEDLIST0_H
36 #define BADVPN_STRUCTURE_LINKEDLIST0_H
37  
38 #include <stddef.h>
39  
40 #include <misc/debug.h>
41  
42 /**
43 * Linked list node.
44 */
45 typedef struct LinkedList0Node_t
46 {
47 struct LinkedList0Node_t *p;
48 struct LinkedList0Node_t *n;
49 } LinkedList0Node;
50  
51 /**
52 * Simple doubly linked list.
53 */
54 typedef struct
55 {
56 LinkedList0Node *first;
57 } LinkedList0;
58  
59 /**
60 * Initializes the linked list.
61 *
62 * @param list list to initialize
63 */
64 static void LinkedList0_Init (LinkedList0 *list);
65  
66 /**
67 * Determines if the list is empty.
68 *
69 * @param list the list
70 * @return 1 if empty, 0 if not
71 */
72 static int LinkedList0_IsEmpty (LinkedList0 *list);
73  
74 /**
75 * Returns the first node of the list.
76 *
77 * @param list the list
78 * @return first node of the list, or NULL if the list is empty
79 */
80 static LinkedList0Node * LinkedList0_GetFirst (LinkedList0 *list);
81  
82 /**
83 * Inserts a node to the beginning of the list.
84 *
85 * @param list the list
86 * @param node uninitialized node to insert
87 */
88 static void LinkedList0_Prepend (LinkedList0 *list, LinkedList0Node *node);
89  
90 /**
91 * Inserts a node before a given node.
92 *
93 * @param list the list
94 * @param node uninitialized node to insert
95 * @param target node in the list to insert before
96 */
97 static void LinkedList0_InsertBefore (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target);
98  
99 /**
100 * Inserts a node after a given node.
101 *
102 * @param list the list
103 * @param node uninitialized node to insert
104 * @param target node in the list to insert after
105 */
106 static void LinkedList0_InsertAfter (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target);
107  
108 /**
109 * Removes a node from the list.
110 *
111 * @param list the list
112 * @param node node to remove
113 */
114 static void LinkedList0_Remove (LinkedList0 *list, LinkedList0Node *node);
115  
116 /**
117 * Returns the next node of a given node.
118 *
119 * @param node reference node
120 * @return next node, or NULL if none
121 */
122 static LinkedList0Node * LinkedList0Node_Next (LinkedList0Node *node);
123  
124 /**
125 * Returns the previous node of a given node.
126 *
127 * @param node reference node
128 * @return previous node, or NULL if none
129 */
130 static LinkedList0Node * LinkedList0Node_Prev (LinkedList0Node *node);
131  
132 void LinkedList0_Init (LinkedList0 *list)
133 {
134 list->first = NULL;
135 }
136  
137 int LinkedList0_IsEmpty (LinkedList0 *list)
138 {
139 return (!list->first);
140 }
141  
142 LinkedList0Node * LinkedList0_GetFirst (LinkedList0 *list)
143 {
144 return (list->first);
145 }
146  
147 void LinkedList0_Prepend (LinkedList0 *list, LinkedList0Node *node)
148 {
149 node->p = NULL;
150 node->n = list->first;
151 if (list->first) {
152 list->first->p = node;
153 }
154 list->first = node;
155 }
156  
157 void LinkedList0_InsertBefore (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target)
158 {
159 node->p = target->p;
160 node->n = target;
161 if (target->p) {
162 target->p->n = node;
163 } else {
164 list->first = node;
165 }
166 target->p = node;
167 }
168  
169 void LinkedList0_InsertAfter (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target)
170 {
171 node->p = target;
172 node->n = target->n;
173 if (target->n) {
174 target->n->p = node;
175 }
176 target->n = node;
177 }
178  
179 void LinkedList0_Remove (LinkedList0 *list, LinkedList0Node *node)
180 {
181 // remove from list
182 if (node->p) {
183 node->p->n = node->n;
184 } else {
185 list->first = node->n;
186 }
187 if (node->n) {
188 node->n->p = node->p;
189 }
190 }
191  
192 LinkedList0Node * LinkedList0Node_Next (LinkedList0Node *node)
193 {
194 return node->n;
195 }
196  
197 LinkedList0Node * LinkedList0Node_Prev (LinkedList0Node *node)
198 {
199 return node->p;
200 }
201  
202 #endif