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