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