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