Remove \version svn tag.
[bertos.git] / bertos / struct / list.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \brief General pourpose double-linked lists
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  */
37
38 #ifndef STRUCT_LIST_H
39 #define STRUCT_LIST_H
40
41 #include <cfg/compiler.h> /* INLINE */
42 #include <cfg/debug.h> /* ASSERT_VALID_PTR() */
43
44 /**
45  * This structure represents a node for bidirectional lists.
46  *
47  * Data is usually appended to nodes by making them the first
48  * field of another struture, as a poor-man's form of inheritance.
49  */
50 typedef struct _Node
51 {
52         struct _Node *succ;
53         struct _Node *pred;
54 } Node;
55
56 /**
57  * Head of a doubly-linked list of \c Node structs.
58  *
59  * Lists must be initialized with LIST_INIT() prior to use.
60  *
61  * Nodes can be added and removed from either end of the list
62  * with O(1) performance.  Iterating over these lists can be
63  * tricky: use the FOREACH_NODE() macro instead.
64  */
65 typedef struct _List
66 {
67         Node head;
68         Node tail;
69 } List;
70
71 /**
72  * Extended node for priority queues.
73  */
74 typedef struct _PriNode
75 {
76         Node link;
77         int  pri;
78 } PriNode;
79
80
81 /**
82  * Template for a naked node in a list of \a T structures.
83  *
84  * To be used as data member in other structures:
85  *
86  * \code
87  *    struct Foo
88  *    {
89  *        DECLARE_NODE_ANON(struct Foo)
90  *        int a;
91  *        float b;
92  *    }
93  *
94  *    DECLARE_LIST_TYPE(Foo);
95  *
96  *    void foo(void)
97  *    {
98  *        static LIST_TYPE(Foo) foo_list;
99  *        static Foo foo1, foo2;
100  *        Foo *fp;
101  *
102  *        LIST_INIT(&foo_list);
103  *        ADDHEAD(&foo_list, &foo1);
104  *        INSERT_BEFORE(&foo_list, &foo2);
105  *        FOREACH_NODE(fp, &foo_list)
106  *            fp->a = 10;
107  *    }
108  *
109  * \endcode
110  */
111 #define DECLARE_NODE_ANON(T) \
112         T *succ; T *pred;
113
114 /** Declare a typesafe node for structures of type \a T. */
115 #define DECLARE_NODE_TYPE(T) \
116         typedef struct T##Node { T *succ; T *pred; } T##Node
117
118 /** Template for a list of \a T structures. */
119 #define DECLARE_LIST_TYPE(T) \
120         DECLARE_NODE_TYPE(T); \
121         typedef struct T##List { \
122                  T##Node head; \
123                  T##Node tail; \
124         } T##List
125
126 #define NODE_TYPE(T) T##Node
127 #define LIST_TYPE(T) T##List
128
129 /**
130  * Get a pointer to the first node in a list.
131  *
132  * If \a l is empty, result points to l->tail.
133  */
134 #define LIST_HEAD(l) ((l)->head.succ)
135
136 /**
137  * Get a pointer to the last node in a list.
138  *
139  * If \a l is empty, result points to l->head.
140  */
141 #define LIST_TAIL(l) ((l)->tail.pred)
142
143 // TODO: move in compiler.h
144 #if COMPILER_TYPEOF
145         #define TYPEOF_OR_VOIDPTR(type) typeof(type)
146 #else
147         #define TYPEOF_OR_VOIDPTR(type) void *
148 #endif
149
150 /**
151  * Iterate over all nodes in a list.
152  *
153  * This macro generates a "for" statement using the following parameters:
154  * \param n   Node pointer to be used in each iteration.
155  * \param l   Pointer to list.
156  */
157 #define FOREACH_NODE(n, l) \
158         for( \
159                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
160                 ((Node *)(n))->succ; \
161                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
162         )
163
164 /**
165  * Iterate backwards over all nodes in a list.
166  *
167  * This macro generates a "for" statement using the following parameters:
168  * \param n   Node pointer to be used in each iteration.
169  * \param l   Pointer to list.
170  */
171 #define REVERSE_FOREACH_NODE(n, l) \
172         for( \
173                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
174                 ((Node *)(n))->pred; \
175                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
176         )
177
178 /** Initialize a list. */
179 #define LIST_INIT(l) \
180         do { \
181                 (l)->head.succ = (TYPEOF_OR_VOIDPTR((l)->head.succ)) &(l)->tail; \
182                 (l)->head.pred = NULL; \
183                 (l)->tail.succ = NULL; \
184                 (l)->tail.pred = (TYPEOF_OR_VOIDPTR((l)->tail.pred)) &(l)->head; \
185         } while (0)
186
187 #ifdef _DEBUG
188         /** Make sure that a list is valid (it was initialized and is not corrupted). */
189         #define LIST_ASSERT_VALID(l) \
190                 do { \
191                         Node *n, *pred; \
192                         ASSERT((l)->head.succ != NULL); \
193                         ASSERT((l)->head.pred == NULL); \
194                         ASSERT((l)->tail.succ == NULL); \
195                         ASSERT((l)->tail.pred != NULL); \
196                         pred = &(l)->head; \
197                         FOREACH_NODE(n, l) \
198                         { \
199                                 ASSERT(n->pred == pred); \
200                                 pred = n; \
201                         } \
202                         ASSERT(n == &(l)->tail); \
203                 } while (0)
204
205         /// Checks that a node isn't part of a given list
206         #define LIST_ASSERT_NOT_CONTAINS(list,node) \
207                 do { \
208                         Node *ln; \
209                         ASSERT_VALID_PTR(list); \
210                         ASSERT_VALID_PTR(node); \
211                         FOREACH_NODE(ln, list) \
212                                 ASSERT(ln != (Node *)(node)); \
213                 } while (0)
214
215         #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
216 #else
217         #define LIST_ASSERT_VALID(l) do {} while (0)
218         #define LIST_ASSERT_NOT_CONTAINS(list,node) do {} while (0)
219         #define INVALIDATE_NODE(n) do {} while (0)
220 #endif
221
222 /** Tell whether a list is empty. */
223 #define LIST_EMPTY(l)  ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
224
225 /** Add node to list head. */
226 #define ADDHEAD(l,n) \
227         do { \
228                 LIST_ASSERT_NOT_CONTAINS((l),(n)); \
229                 (n)->succ = (l)->head.succ; \
230                 (n)->pred = (l)->head.succ->pred; \
231                 (n)->succ->pred = (n); \
232                 (n)->pred->succ = (n); \
233         } while (0)
234
235 /** Add node to list tail. */
236 #define ADDTAIL(l,n) \
237         do { \
238                 LIST_ASSERT_NOT_CONTAINS((l),(n)); \
239                 (n)->succ = &(l)->tail; \
240                 (n)->pred = (l)->tail.pred; \
241                 (n)->pred->succ = (n); \
242                 (l)->tail.pred = (n); \
243         } while (0)
244
245 /**
246  * Insert node \a n before node \a ln.
247  *
248  * \note You can't pass in a list header as \a ln, but
249  *       it is safe to pass list-\>head of an empty list.
250  */
251 #define INSERT_BEFORE(n,ln) \
252         do { \
253                 ASSERT_VALID_PTR(n); \
254                 ASSERT_VALID_PTR(ln); \
255                 (n)->succ = (ln); \
256                 (n)->pred = (ln)->pred; \
257                 (ln)->pred->succ = (n); \
258                 (ln)->pred = (n); \
259         } while (0)
260
261 /**
262  * Remove \a n from whatever list it is in.
263  *
264  * \note Removing a node that has not previously been
265  *       inserted into a list invokes undefined behavior.
266  */
267 #define REMOVE(n) \
268         do { \
269                 ASSERT_VALID_PTR(n); \
270                 (n)->pred->succ = (n)->succ; \
271                 (n)->succ->pred = (n)->pred; \
272                 INVALIDATE_NODE(n); \
273         } while (0)
274
275 /**
276  * Insert a priority node in a priority queue.
277  *
278  * The new node is inserted immediately before the first node with the same
279  * priority or appended to the tail if no such node exists.
280  */
281 #define LIST_ENQUEUE_HEAD(list, node) \
282         do { \
283                 PriNode *ln; \
284                 LIST_ASSERT_NOT_CONTAINS((list),(node)); \
285                 FOREACH_NODE(ln, (list)) \
286                         if (ln->pri <= (node)->pri) \
287                                 break; \
288                 INSERT_BEFORE(&(node)->link, &ln->link); \
289         } while (0)
290
291 /**
292  * Insert a priority node in a priority queue.
293  *
294  * The new node is inserted immediately before the first node with lower
295  * priority or appended to the tail if no such node exists.
296  */
297 #define LIST_ENQUEUE(list, node) \
298         do { \
299                 PriNode *ln; \
300                 LIST_ASSERT_NOT_CONTAINS((list),(node)); \
301                 FOREACH_NODE(ln, (list)) \
302                         if (ln->pri < (node)->pri) \
303                                 break; \
304                 INSERT_BEFORE(&(node)->link, &ln->link); \
305         } while (0)
306
307
308 /**
309  * Unlink a node from the head of the list \a l.
310  *
311  * \return Pointer to node, or NULL if the list was empty.
312  */
313 INLINE Node *list_remHead(List *l)
314 {
315         Node *n;
316
317         ASSERT_VALID_PTR(l);
318
319         if (LIST_EMPTY(l))
320                 return (Node *)0;
321
322         n = l->head.succ; /* Get first node. */
323         l->head.succ = n->succ; /* Link list head to second node. */
324         n->succ->pred = &l->head; /* Link second node to list head. */
325
326         INVALIDATE_NODE(n);
327         return n;
328 }
329
330 /**
331  * Unlink a node from the tail of the list \a l.
332  *
333  * \return Pointer to node, or NULL if the list was empty.
334  */
335 INLINE Node *list_remTail(List *l)
336 {
337         Node *n;
338
339         ASSERT_VALID_PTR(l);
340
341         if (LIST_EMPTY(l))
342                 return NULL;
343
344         n = l->tail.pred; /* Get last node. */
345         l->tail.pred = n->pred; /* Link list tail to second last node. */
346         n->pred->succ = &l->tail; /* Link second last node to list tail. */
347
348         INVALIDATE_NODE(n);
349         return n;
350 }
351
352 #endif /* STRUCT_LIST_H */