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.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief General pourpose double-linked lists
18 *#* Revision 1.19 2006/03/20 17:50:29 bernie
21 *#* Revision 1.18 2006/02/27 22:40:21 bernie
22 *#* Add support for poor pre-C99 compilers.
24 *#* Revision 1.17 2006/02/24 01:18:34 bernie
25 *#* LIST_ENQUEUE(): New macro; Remove obsolete names.
27 *#* Revision 1.16 2006/01/23 23:10:38 bernie
28 *#* REVERSE_FOREACH_NODE(): New macro; FOREACHNODE(): Rename to FOREACH_NODE.
30 *#* Revision 1.15 2005/11/04 16:20:02 bernie
31 *#* Fix reference to README.devlib in header.
33 *#* Revision 1.14 2005/07/19 07:25:18 bernie
34 *#* Refactor to remove type aliasing problems.
36 *#* Revision 1.13 2005/04/11 19:10:28 bernie
37 *#* Include top-level headers from cfg/ subdir.
39 *#* Revision 1.12 2005/01/22 04:21:32 bernie
40 *#* Add integrity checks.
42 *#* Revision 1.11 2004/12/31 16:44:11 bernie
43 *#* list_remHead(), list_remTail(): Name like normal functions.
45 *#* Revision 1.10 2004/11/28 23:21:05 bernie
46 *#* Remove obsolete INITLIST macro.
48 *#* Revision 1.9 2004/10/21 09:37:55 bernie
49 *#* Revamp documentation.
51 *#* Revision 1.8 2004/10/19 08:46:34 bernie
54 *#* Revision 1.7 2004/08/25 14:12:09 rasky
55 *#* Aggiornato il comment block dei log RCS
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
61 *#* Revision 1.5 2004/07/20 23:45:01 bernie
62 *#* Finally remove redundant protos.
64 *#* Revision 1.4 2004/07/18 22:12:53 bernie
65 *#* Fix warnings with GCC 3.3.2.
67 *#* Revision 1.3 2004/07/18 22:01:43 bernie
68 *#* REMHEAD(), REMTAIL(): Move to list.h as inline functions.
70 *#* Revision 1.2 2004/06/03 11:27:09 bernie
71 *#* Add dual-license information.
73 *#* Revision 1.1 2004/05/23 15:43:16 bernie
74 *#* Import mware modules.
80 #include <cfg/compiler.h> /* INLINE */
81 #include <cfg/debug.h> /* ASSERT() */
84 * This structure represents a node for bidirectional lists.
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.
96 * Head of a doubly-linked list of \c Node structs.
98 * Lists must be initialized with LIST_INIT() prior to use.
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.
111 * Extended node for priority queues.
113 typedef struct _PriNode
121 * Template for a naked node in a list of \a T structures.
123 * To be used as data member in other structures:
128 * DECLARE_NODE_ANON(struct Foo)
133 * DECLARE_LIST_TYPE(Foo);
137 * static LIST_TYPE(Foo) foo_list;
138 * static Foo foo1, foo2;
141 * LIST_INIT(&foo_list);
142 * ADDHEAD(&foo_list, &foo1);
143 * INSERT_BEFORE(&foo_list, &foo2);
144 * FOREACH_NODE(fp, &foo_list)
150 #define DECLARE_NODE_ANON(T) \
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
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 { \
165 #define NODE_TYPE(T) T##Node
166 #define LIST_TYPE(T) T##List
169 * Get a pointer to the first node in a list.
171 * If \a l is empty, result points to l->tail.
173 #define LIST_HEAD(l) ((l)->head.succ)
176 * Get a pointer to the last node in a list.
178 * If \a l is empty, result points to l->head.
180 #define LIST_TAIL(l) ((l)->tail.pred)
182 // TODO: move in compiler.h
184 #define TYPEOF_OR_VOIDPTR(type) typeof(type)
186 #define TYPEOF_OR_VOIDPTR(type) void *
190 * Iterate over all nodes in a list.
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.
196 #define FOREACH_NODE(n, l) \
198 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
199 ((Node *)(n))->succ; \
200 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
204 * Iterate backwards over all nodes in a list.
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.
210 #define REVERSE_FOREACH_NODE(n, l) \
212 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
213 ((Node *)(n))->pred; \
214 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
217 /*! Initialize a list. */
218 #define LIST_INIT(l) \
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; \
227 /*! Make sure that a list is valid (it was initialized and is not corrupted). */
228 #define LIST_ASSERT_VALID(l) \
231 ASSERT((l)->head.succ != NULL); \
232 ASSERT((l)->head.pred == NULL); \
233 ASSERT((l)->tail.succ == NULL); \
234 ASSERT((l)->tail.pred != NULL); \
238 ASSERT(n->pred == pred); \
241 ASSERT(n == &(l)->tail); \
244 #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
246 #define LIST_ASSERT_VALID(l) do {} while (0)
247 #define INVALIDATE_NODE(n) do {} while (0)
250 /*! Add node to list head. */
251 #define ADDHEAD(l,n) \
255 (n)->succ = (l)->head.succ; \
256 (n)->pred = (l)->head.succ->pred; \
257 (n)->succ->pred = (n); \
258 (n)->pred->succ = (n); \
261 /*! Add node to list tail. */
262 #define ADDTAIL(l,n) \
266 (n)->succ = &(l)->tail; \
267 (n)->pred = (l)->tail.pred; \
268 (n)->pred->succ = (n); \
269 (l)->tail.pred = (n); \
273 * Insert node \a n before node \a ln.
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.
278 #define INSERT_BEFORE(n,ln) \
281 (n)->pred = (ln)->pred; \
282 (ln)->pred->succ = (n); \
287 * Remove \a n from whatever list it is in.
289 * \note Removing a node that has not previously been
290 * inserted into a list invokes undefined behavior.
294 (n)->pred->succ = (n)->succ; \
295 (n)->succ->pred = (n)->pred; \
296 INVALIDATE_NODE(n); \
299 /*! Tell whether a list is empty. */
300 #define LIST_EMPTY(l) ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
303 * Insert a priority node in a priority queue.
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.
309 #define LIST_ENQUEUE(list, node) \
312 FOREACH_NODE(ln, (list)) \
313 if (ln->pri < (node)->pri) \
315 INSERT_BEFORE(&(node)->link, &ln->link); \
320 * Unlink a node from the head of the list \a l.
322 * \return Pointer to node, or NULL if the list was empty.
324 INLINE Node *list_remHead(List *l)
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. */
340 * Unlink a node from the tail of the list \a l.
342 * \return Pointer to node, or NULL if the list was empty.
344 INLINE Node *list_remTail(List *l)
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. */
359 #endif /* MWARE_LIST_H */