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