Merge branch "preempt" in "trunk".
[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  * \version $Id$
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38
39 #ifndef STRUCT_LIST_H
40 #define STRUCT_LIST_H
41
42 #include <cfg/compiler.h> /* INLINE */
43 #include <cfg/debug.h> /* ASSERT_VALID_PTR() */
44
45 /**
46  * This structure represents a node for bidirectional lists.
47  *
48  * Data is usually appended to nodes by making them the first
49  * field of another struture, as a poor-man's form of inheritance.
50  */
51 typedef struct _Node
52 {
53         struct _Node *succ;
54         struct _Node *pred;
55 } Node;
56
57 /**
58  * Head of a doubly-linked list of \c Node structs.
59  *
60  * Lists must be initialized with LIST_INIT() prior to use.
61  *
62  * Nodes can be added and removed from either end of the list
63  * with O(1) performance.  Iterating over these lists can be
64  * tricky: use the FOREACH_NODE() macro instead.
65  */
66 typedef struct _List
67 {
68         Node head;
69         Node tail;
70 } List;
71
72 /**
73  * Extended node for priority queues.
74  */
75 typedef struct _PriNode
76 {
77         Node link;
78         int  pri;
79 } PriNode;
80
81
82 /**
83  * Template for a naked node in a list of \a T structures.
84  *
85  * To be used as data member in other structures:
86  *
87  * \code
88  *    struct Foo
89  *    {
90  *        DECLARE_NODE_ANON(struct Foo)
91  *        int a;
92  *        float b;
93  *    }
94  *
95  *    DECLARE_LIST_TYPE(Foo);
96  *
97  *    void foo(void)
98  *    {
99  *        static LIST_TYPE(Foo) foo_list;
100  *        static Foo foo1, foo2;
101  *        Foo *fp;
102  *
103  *        LIST_INIT(&foo_list);
104  *        ADDHEAD(&foo_list, &foo1);
105  *        INSERT_BEFORE(&foo_list, &foo2);
106  *        FOREACH_NODE(fp, &foo_list)
107  *            fp->a = 10;
108  *    }
109  *
110  * \endcode
111  */
112 #define DECLARE_NODE_ANON(T) \
113         T *succ; T *pred;
114
115 /** Declare a typesafe node for structures of type \a T. */
116 #define DECLARE_NODE_TYPE(T) \
117         typedef struct T##Node { T *succ; T *pred; } T##Node
118
119 /** Template for a list of \a T structures. */
120 #define DECLARE_LIST_TYPE(T) \
121         DECLARE_NODE_TYPE(T); \
122         typedef struct T##List { \
123                  T##Node head; \
124                  T##Node tail; \
125         } T##List
126
127 #define NODE_TYPE(T) T##Node
128 #define LIST_TYPE(T) T##List
129
130 /**
131  * Get a pointer to the first node in a list.
132  *
133  * If \a l is empty, result points to l->tail.
134  */
135 #define LIST_HEAD(l) ((l)->head.succ)
136
137 /**
138  * Get a pointer to the last node in a list.
139  *
140  * If \a l is empty, result points to l->head.
141  */
142 #define LIST_TAIL(l) ((l)->tail.pred)
143
144 // TODO: move in compiler.h
145 #if COMPILER_TYPEOF
146         #define TYPEOF_OR_VOIDPTR(type) typeof(type)
147 #else
148         #define TYPEOF_OR_VOIDPTR(type) void *
149 #endif
150
151 /**
152  * Iterate over all nodes in a list.
153  *
154  * This macro generates a "for" statement using the following parameters:
155  * \param n   Node pointer to be used in each iteration.
156  * \param l   Pointer to list.
157  */
158 #define FOREACH_NODE(n, l) \
159         for( \
160                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
161                 ((Node *)(n))->succ; \
162                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
163         )
164
165 /**
166  * Iterate backwards over all nodes in a list.
167  *
168  * This macro generates a "for" statement using the following parameters:
169  * \param n   Node pointer to be used in each iteration.
170  * \param l   Pointer to list.
171  */
172 #define REVERSE_FOREACH_NODE(n, l) \
173         for( \
174                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
175                 ((Node *)(n))->pred; \
176                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
177         )
178
179 /** Initialize a list. */
180 #define LIST_INIT(l) \
181         do { \
182                 (l)->head.succ = (TYPEOF_OR_VOIDPTR((l)->head.succ)) &(l)->tail; \
183                 (l)->head.pred = NULL; \
184                 (l)->tail.succ = NULL; \
185                 (l)->tail.pred = (TYPEOF_OR_VOIDPTR((l)->tail.pred)) &(l)->head; \
186         } while (0)
187
188 #ifdef _DEBUG
189         /** Make sure that a list is valid (it was initialized and is not corrupted). */
190         #define LIST_ASSERT_VALID(l) \
191                 do { \
192                         Node *n, *pred; \
193                         ASSERT((l)->head.succ != NULL); \
194                         ASSERT((l)->head.pred == NULL); \
195                         ASSERT((l)->tail.succ == NULL); \
196                         ASSERT((l)->tail.pred != NULL); \
197                         pred = &(l)->head; \
198                         FOREACH_NODE(n, l) \
199                         { \
200                                 ASSERT(n->pred == pred); \
201                                 pred = n; \
202                         } \
203                         ASSERT(n == &(l)->tail); \
204                 } while (0)
205
206         /// Checks that a node isn't part of a given list
207         #define LIST_ASSERT_NOT_CONTAINS(list,node) \
208                 do { \
209                         Node *ln; \
210                         ASSERT_VALID_PTR(list); \
211                         ASSERT_VALID_PTR(node); \
212                         FOREACH_NODE(ln, list) \
213                                 ASSERT(ln != (Node *)(node)); \
214                 } while (0)
215
216         #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
217 #else
218         #define LIST_ASSERT_VALID(l) do {} while (0)
219         #define LIST_ASSERT_NOT_CONTAINS(list,node) do {} while (0)
220         #define INVALIDATE_NODE(n) do {} while (0)
221 #endif
222
223 /** Tell whether a list is empty. */
224 #define LIST_EMPTY(l)  ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
225
226 /** Add node to list head. */
227 #define ADDHEAD(l,n) \
228         do { \
229                 LIST_ASSERT_NOT_CONTAINS((l),(n)); \
230                 (n)->succ = (l)->head.succ; \
231                 (n)->pred = (l)->head.succ->pred; \
232                 (n)->succ->pred = (n); \
233                 (n)->pred->succ = (n); \
234         } while (0)
235
236 /** Add node to list tail. */
237 #define ADDTAIL(l,n) \
238         do { \
239                 LIST_ASSERT_NOT_CONTAINS((l),(n)); \
240                 (n)->succ = &(l)->tail; \
241                 (n)->pred = (l)->tail.pred; \
242                 (n)->pred->succ = (n); \
243                 (l)->tail.pred = (n); \
244         } while (0)
245
246 /**
247  * Insert node \a n before node \a ln.
248  *
249  * \note You can't pass in a list header as \a ln, but
250  *       it is safe to pass list-\>head of an empty list.
251  */
252 #define INSERT_BEFORE(n,ln) \
253         do { \
254                 ASSERT_VALID_PTR(n); \
255                 ASSERT_VALID_PTR(ln); \
256                 (n)->succ = (ln); \
257                 (n)->pred = (ln)->pred; \
258                 (ln)->pred->succ = (n); \
259                 (ln)->pred = (n); \
260         } while (0)
261
262 /**
263  * Remove \a n from whatever list it is in.
264  *
265  * \note Removing a node that has not previously been
266  *       inserted into a list invokes undefined behavior.
267  */
268 #define REMOVE(n) \
269         do { \
270                 ASSERT_VALID_PTR(n); \
271                 (n)->pred->succ = (n)->succ; \
272                 (n)->succ->pred = (n)->pred; \
273                 INVALIDATE_NODE(n); \
274         } while (0)
275
276 /**
277  * Insert a priority node in a priority queue.
278  *
279  * The new node is inserted immediately before the first node with the same
280  * priority or appended to the tail if no such node exists.
281  */
282 #define LIST_ENQUEUE_HEAD(list, node) \
283         do { \
284                 PriNode *ln; \
285                 LIST_ASSERT_NOT_CONTAINS((list),(node)); \
286                 FOREACH_NODE(ln, (list)) \
287                         if (ln->pri <= (node)->pri) \
288                                 break; \
289                 INSERT_BEFORE(&(node)->link, &ln->link); \
290         } while (0)
291
292 /**
293  * Insert a priority node in a priority queue.
294  *
295  * The new node is inserted immediately before the first node with lower
296  * priority or appended to the tail if no such node exists.
297  */
298 #define LIST_ENQUEUE(list, node) \
299         do { \
300                 PriNode *ln; \
301                 LIST_ASSERT_NOT_CONTAINS((list),(node)); \
302                 FOREACH_NODE(ln, (list)) \
303                         if (ln->pri < (node)->pri) \
304                                 break; \
305                 INSERT_BEFORE(&(node)->link, &ln->link); \
306         } while (0)
307
308
309 /**
310  * Unlink a node from the head of the list \a l.
311  *
312  * \return Pointer to node, or NULL if the list was empty.
313  */
314 INLINE Node *list_remHead(List *l)
315 {
316         Node *n;
317
318         ASSERT_VALID_PTR(l);
319
320         if (LIST_EMPTY(l))
321                 return (Node *)0;
322
323         n = l->head.succ; /* Get first node. */
324         l->head.succ = n->succ; /* Link list head to second node. */
325         n->succ->pred = &l->head; /* Link second node to list head. */
326
327         INVALIDATE_NODE(n);
328         return n;
329 }
330
331 /**
332  * Unlink a node from the tail of the list \a l.
333  *
334  * \return Pointer to node, or NULL if the list was empty.
335  */
336 INLINE Node *list_remTail(List *l)
337 {
338         Node *n;
339
340         ASSERT_VALID_PTR(l);
341
342         if (LIST_EMPTY(l))
343                 return NULL;
344
345         n = l->tail.pred; /* Get last node. */
346         l->tail.pred = n->pred; /* Link list tail to second last node. */
347         n->pred->succ = &l->tail; /* Link second last node to list tail. */
348
349         INVALIDATE_NODE(n);
350         return n;
351 }
352
353 #endif /* STRUCT_LIST_H */