Convert to new Doxygen style.
[bertos.git] / mware / list.h
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2001 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief General pourpose double-linked lists
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.20  2006/07/19 12:56:28  bernie
19  *#* Convert to new Doxygen style.
20  *#*
21  *#* Revision 1.19  2006/03/20 17:50:29  bernie
22  *#* Fix typo.
23  *#*
24  *#* Revision 1.18  2006/02/27 22:40:21  bernie
25  *#* Add support for poor pre-C99 compilers.
26  *#*
27  *#* Revision 1.17  2006/02/24 01:18:34  bernie
28  *#* LIST_ENQUEUE(): New macro; Remove obsolete names.
29  *#*
30  *#* Revision 1.16  2006/01/23 23:10:38  bernie
31  *#* REVERSE_FOREACH_NODE(): New macro; FOREACHNODE(): Rename to FOREACH_NODE.
32  *#*
33  *#* Revision 1.15  2005/11/04 16:20:02  bernie
34  *#* Fix reference to README.devlib in header.
35  *#*
36  *#* Revision 1.14  2005/07/19 07:25:18  bernie
37  *#* Refactor to remove type aliasing problems.
38  *#*
39  *#* Revision 1.13  2005/04/11 19:10:28  bernie
40  *#* Include top-level headers from cfg/ subdir.
41  *#*
42  *#* Revision 1.12  2005/01/22 04:21:32  bernie
43  *#* Add integrity checks.
44  *#*
45  *#* Revision 1.11  2004/12/31 16:44:11  bernie
46  *#* list_remHead(), list_remTail(): Name like normal functions.
47  *#*
48  *#* Revision 1.10  2004/11/28 23:21:05  bernie
49  *#* Remove obsolete INITLIST macro.
50  *#*
51  *#* Revision 1.9  2004/10/21 09:37:55  bernie
52  *#* Revamp documentation.
53  *#*
54  *#* Revision 1.8  2004/10/19 08:46:34  bernie
55  *#* Fix header.
56  *#*
57  *#* Revision 1.7  2004/08/25 14:12:09  rasky
58  *#* Aggiornato il comment block dei log RCS
59  *#*
60  *#* Revision 1.6  2004/07/30 14:34:10  rasky
61  *#* Vari fix per documentazione e commenti
62  *#* Aggiunte PP_CATn e STATIC_ASSERT
63  *#*
64  *#* Revision 1.5  2004/07/20 23:45:01  bernie
65  *#* Finally remove redundant protos.
66  *#*
67  *#* Revision 1.4  2004/07/18 22:12:53  bernie
68  *#* Fix warnings with GCC 3.3.2.
69  *#*
70  *#* Revision 1.3  2004/07/18 22:01:43  bernie
71  *#* REMHEAD(), REMTAIL(): Move to list.h as inline functions.
72  *#*
73  *#* Revision 1.2  2004/06/03 11:27:09  bernie
74  *#* Add dual-license information.
75  *#*
76  *#* Revision 1.1  2004/05/23 15:43:16  bernie
77  *#* Import mware modules.
78  *#*
79  *#*/
80 #ifndef MWARE_LIST_H
81 #define MWARE_LIST_H
82
83 #include <cfg/compiler.h> /* INLINE */
84 #include <cfg/debug.h> /* ASSERT() */
85
86 /**
87  * This structure represents a node for bidirectional lists.
88  *
89  * Data is usually appended to nodes by making them the first
90  * field of another struture, as a poor-man's form of inheritance.
91  */
92 typedef struct _Node
93 {
94         struct _Node *succ;
95         struct _Node *pred;
96 } Node;
97
98 /**
99  * Head of a doubly-linked list of \c Node structs.
100  *
101  * Lists must be initialized with LIST_INIT() prior to use.
102  *
103  * Nodes can be added and removed from either end of the list
104  * with O(1) performance.  Iterating over these lists can be
105  * tricky: use the FOREACH_NODE() macro instead.
106  */
107 typedef struct _List
108 {
109         Node head;
110         Node tail;
111 } List;
112
113 /**
114  * Extended node for priority queues.
115  */
116 typedef struct _PriNode
117 {
118         Node link;
119         int  pri;
120 } PriNode;
121
122
123 /**
124  * Template for a naked node in a list of \a T structures.
125  *
126  * To be used as data member in other structures:
127  *
128  * \code
129  *    struct Foo
130  *    {
131  *        DECLARE_NODE_ANON(struct Foo)
132  *        int a;
133  *        float b;
134  *    }
135  *
136  *    DECLARE_LIST_TYPE(Foo);
137  *
138  *    void foo(void)
139  *    {
140  *        static LIST_TYPE(Foo) foo_list;
141  *        static Foo foo1, foo2;
142  *        Foo *fp;
143  *
144  *        LIST_INIT(&foo_list);
145  *        ADDHEAD(&foo_list, &foo1);
146  *        INSERT_BEFORE(&foo_list, &foo2);
147  *        FOREACH_NODE(fp, &foo_list)
148  *              fp->a = 10;
149  *    }
150  *
151  * \endcode
152  */
153 #define DECLARE_NODE_ANON(T) \
154         T *succ; T *pred;
155
156 /** Declare a typesafe node for structures of type \a T. */
157 #define DECLARE_NODE_TYPE(T) \
158         typedef struct T##Node { T *succ; T *pred; } T##Node
159
160 /** Template for a list of \a T structures. */
161 #define DECLARE_LIST_TYPE(T) \
162         DECLARE_NODE_TYPE(T); \
163         typedef struct T##List { \
164                  T##Node head; \
165                  T##Node tail; \
166         } T##List
167
168 #define NODE_TYPE(T) T##Node
169 #define LIST_TYPE(T) T##List
170
171 /**
172  * Get a pointer to the first node in a list.
173  *
174  * If \a l is empty, result points to l->tail.
175  */
176 #define LIST_HEAD(l) ((l)->head.succ)
177
178 /**
179  * Get a pointer to the last node in a list.
180  *
181  * If \a l is empty, result points to l->head.
182  */
183 #define LIST_TAIL(l) ((l)->tail.pred)
184
185 // TODO: move in compiler.h
186 #if COMPILER_TYPEOF
187         #define TYPEOF_OR_VOIDPTR(type) typeof(type)
188 #else
189         #define TYPEOF_OR_VOIDPTR(type) void *
190 #endif
191
192 /**
193  * Iterate over all nodes in a list.
194  *
195  * This macro generates a "for" statement using the following parameters:
196  * \param n   Node pointer to be used in each iteration.
197  * \param l   Pointer to list.
198  */
199 #define FOREACH_NODE(n, l) \
200         for( \
201                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
202                 ((Node *)(n))->succ; \
203                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
204         )
205
206 /**
207  * Iterate backwards over all nodes in a list.
208  *
209  * This macro generates a "for" statement using the following parameters:
210  * \param n   Node pointer to be used in each iteration.
211  * \param l   Pointer to list.
212  */
213 #define REVERSE_FOREACH_NODE(n, l) \
214         for( \
215                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
216                 ((Node *)(n))->pred; \
217                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
218         )
219
220 /** Initialize a list. */
221 #define LIST_INIT(l) \
222         do { \
223                 (l)->head.succ = (TYPEOF_OR_VOIDPTR((l)->head.succ)) &(l)->tail; \
224                 (l)->head.pred = NULL; \
225                 (l)->tail.succ = NULL; \
226                 (l)->tail.pred = (TYPEOF_OR_VOIDPTR((l)->tail.pred)) &(l)->head; \
227         } while (0)
228
229 #ifdef _DEBUG
230         /** Make sure that a list is valid (it was initialized and is not corrupted). */
231         #define LIST_ASSERT_VALID(l) \
232                 do { \
233                         Node *n, *pred; \
234                         ASSERT((l)->head.succ != NULL); \
235                         ASSERT((l)->head.pred == NULL); \
236                         ASSERT((l)->tail.succ == NULL); \
237                         ASSERT((l)->tail.pred != NULL); \
238                         pred = &(l)->head; \
239                         FOREACH_NODE(n, l) \
240                         { \
241                                 ASSERT(n->pred == pred); \
242                                 pred = n; \
243                         } \
244                         ASSERT(n == &(l)->tail); \
245                 } while (0)
246
247         #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
248 #else
249         #define LIST_ASSERT_VALID(l) do {} while (0)
250         #define INVALIDATE_NODE(n) do {} while (0)
251 #endif
252
253 /** Add node to list head. */
254 #define ADDHEAD(l,n) \
255         do { \
256                 ASSERT(l); \
257                 ASSERT(n); \
258                 (n)->succ = (l)->head.succ; \
259                 (n)->pred = (l)->head.succ->pred; \
260                 (n)->succ->pred = (n); \
261                 (n)->pred->succ = (n); \
262         } while (0)
263
264 /** Add node to list tail. */
265 #define ADDTAIL(l,n) \
266         do { \
267                 ASSERT(l); \
268                 ASSERT(n); \
269                 (n)->succ = &(l)->tail; \
270                 (n)->pred = (l)->tail.pred; \
271                 (n)->pred->succ = (n); \
272                 (l)->tail.pred = (n); \
273         } while (0)
274
275 /**
276  * Insert node \a n before node \a ln.
277  *
278  * \note You can't pass in a list header as \a ln, but
279  *       it is safe to pass list-\>head of an empty list.
280  */
281 #define INSERT_BEFORE(n,ln) \
282         do { \
283                 (n)->succ = (ln); \
284                 (n)->pred = (ln)->pred; \
285                 (ln)->pred->succ = (n); \
286                 (ln)->pred = (n); \
287         } while (0)
288
289 /**
290  * Remove \a n from whatever list it is in.
291  *
292  * \note Removing a node that has not previously been
293  *       inserted into a list invokes undefined behavior.
294  */
295 #define REMOVE(n) \
296         do { \
297                 (n)->pred->succ = (n)->succ; \
298                 (n)->succ->pred = (n)->pred; \
299                 INVALIDATE_NODE(n); \
300         } while (0)
301
302 /** Tell whether a list is empty. */
303 #define LIST_EMPTY(l)  ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
304
305 /**
306  * Insert a priority node in a priority queue.
307  *
308  * The new node is inserted immediately before the
309  * first node with lower priority or appended to
310  * the tail if no such node exists.
311  */
312 #define LIST_ENQUEUE(list, node) \
313         do { \
314                 PriNode *ln; \
315                 FOREACH_NODE(ln, (list)) \
316                         if (ln->pri < (node)->pri) \
317                                 break; \
318                 INSERT_BEFORE(&(node)->link, &ln->link); \
319         } while (0)
320
321
322 /**
323  * Unlink a node from the head of the list \a l.
324  *
325  * \return Pointer to node, or NULL if the list was empty.
326  */
327 INLINE Node *list_remHead(List *l)
328 {
329         Node *n;
330
331         if (LIST_EMPTY(l))
332                 return (Node *)0;
333
334         n = l->head.succ; /* Get first node. */
335         l->head.succ = n->succ; /* Link list head to second node. */
336         n->succ->pred = &l->head; /* Link second node to list head. */
337
338         INVALIDATE_NODE(n);
339         return n;
340 }
341
342 /**
343  * Unlink a node from the tail of the list \a l.
344  *
345  * \return Pointer to node, or NULL if the list was empty.
346  */
347 INLINE Node *list_remTail(List *l)
348 {
349         Node *n;
350
351         if (LIST_EMPTY(l))
352                 return (Node *)0;
353
354         n = l->tail.pred; /* Get last node. */
355         l->tail.pred = n->pred; /* Link list tail to second last node. */
356         n->pred->succ = &l->tail; /* Link second last node to list tail. */
357
358         INVALIDATE_NODE(n);
359         return n;
360 }
361
362 #endif /* MWARE_LIST_H */