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