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.18 2006/02/27 22:40:21 bernie
19 *#* Add support for poor pre-C99 compilers.
21 *#* Revision 1.17 2006/02/24 01:18:34 bernie
22 *#* LIST_ENQUEUE(): New macro; Remove obsolete names.
24 *#* Revision 1.16 2006/01/23 23:10:38 bernie
25 *#* REVERSE_FOREACH_NODE(): New macro; FOREACHNODE(): Rename to FOREACH_NODE.
27 *#* Revision 1.15 2005/11/04 16:20:02 bernie
28 *#* Fix reference to README.devlib in header.
30 *#* Revision 1.14 2005/07/19 07:25:18 bernie
31 *#* Refactor to remove type aliasing problems.
33 *#* Revision 1.13 2005/04/11 19:10:28 bernie
34 *#* Include top-level headers from cfg/ subdir.
36 *#* Revision 1.12 2005/01/22 04:21:32 bernie
37 *#* Add integrity checks.
39 *#* Revision 1.11 2004/12/31 16:44:11 bernie
40 *#* list_remHead(), list_remTail(): Name like normal functions.
42 *#* Revision 1.10 2004/11/28 23:21:05 bernie
43 *#* Remove obsolete INITLIST macro.
45 *#* Revision 1.9 2004/10/21 09:37:55 bernie
46 *#* Revamp documentation.
48 *#* Revision 1.8 2004/10/19 08:46:34 bernie
51 *#* Revision 1.7 2004/08/25 14:12:09 rasky
52 *#* Aggiornato il comment block dei log RCS
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
58 *#* Revision 1.5 2004/07/20 23:45:01 bernie
59 *#* Finally remove redundant protos.
61 *#* Revision 1.4 2004/07/18 22:12:53 bernie
62 *#* Fix warnings with GCC 3.3.2.
64 *#* Revision 1.3 2004/07/18 22:01:43 bernie
65 *#* REMHEAD(), REMTAIL(): Move to list.h as inline functions.
67 *#* Revision 1.2 2004/06/03 11:27:09 bernie
68 *#* Add dual-license information.
70 *#* Revision 1.1 2004/05/23 15:43:16 bernie
71 *#* Import mware modules.
77 #include <cfg/compiler.h> /* INLINE */
78 #include <cfg/debug.h> /* ASSERT() */
81 * This structure represents a node for bidirectional lists.
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.
93 * Head of a doubly-linked list of \c Node structs.
95 * Lists must be initialized with LIST_INIT() prior to use.
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.
108 * Extended node for priority queues.
110 typedef struct _PriNode
118 * Template for a naked node in a list of \a T structures.
120 * To be used as data member in other structures:
125 * DECLARE_NODE_ANON(struct Foo)
130 * DECLARE_LIST_TYPE(Foo);
134 * static LIST_TYPE(Foo) foo_list;
135 * static Foo foo1, foo2;
138 * LIST_INIT(&foo_list);
139 * ADDHEAD(&foo_list, &foo1);
140 * INSERTBEFORE(&foo_list, &foo2);
141 * FOREACH_NODE(fp, &foo_list)
147 #define DECLARE_NODE_ANON(T) \
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
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 { \
162 #define NODE_TYPE(T) T##Node
163 #define LIST_TYPE(T) T##List
166 * Get a pointer to the first node in a list.
168 * If \a l is empty, result points to l->tail.
170 #define LIST_HEAD(l) ((l)->head.succ)
173 * Get a pointer to the last node in a list.
175 * If \a l is empty, result points to l->head.
177 #define LIST_TAIL(l) ((l)->tail.pred)
179 // TODO: move in compiler.h
181 #define TYPEOF_OR_VOIDPTR(type) typeof(type)
183 #define TYPEOF_OR_VOIDPTR(type) void *
187 * Iterate over all nodes in a list.
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.
193 #define FOREACH_NODE(n, l) \
195 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
196 ((Node *)(n))->succ; \
197 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
201 * Iterate backwards over all nodes in a list.
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.
207 #define REVERSE_FOREACH_NODE(n, l) \
209 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
210 ((Node *)(n))->pred; \
211 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
214 /*! Initialize a list. */
215 #define LIST_INIT(l) \
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; \
224 /*! Make sure that a list is valid (it was initialized and is not corrupted). */
225 #define LIST_ASSERT_VALID(l) \
228 ASSERT((l)->head.succ != NULL); \
229 ASSERT((l)->head.pred == NULL); \
230 ASSERT((l)->tail.succ == NULL); \
231 ASSERT((l)->tail.pred != NULL); \
235 ASSERT(n->pred == pred); \
238 ASSERT(n == &(l)->tail); \
241 #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
243 #define LIST_ASSERT_VALID(l) do {} while (0)
244 #define INVALIDATE_NODE(n) do {} while (0)
247 /*! Add node to list head. */
248 #define ADDHEAD(l,n) \
252 (n)->succ = (l)->head.succ; \
253 (n)->pred = (l)->head.succ->pred; \
254 (n)->succ->pred = (n); \
255 (n)->pred->succ = (n); \
258 /*! Add node to list tail. */
259 #define ADDTAIL(l,n) \
263 (n)->succ = &(l)->tail; \
264 (n)->pred = (l)->tail.pred; \
265 (n)->pred->succ = (n); \
266 (l)->tail.pred = (n); \
270 * Insert node \a n before node \a ln.
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.
275 #define INSERT_BEFORE(n,ln) \
278 (n)->pred = (ln)->pred; \
279 (ln)->pred->succ = (n); \
284 * Remove \a n from whatever list it is in.
286 * \note Removing a node that has not previously been
287 * inserted into a list invokes undefined behavior.
291 (n)->pred->succ = (n)->succ; \
292 (n)->succ->pred = (n)->pred; \
293 INVALIDATE_NODE(n); \
296 /*! Tell whether a list is empty. */
297 #define LIST_EMPTY(l) ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
300 * Insert a priority node in a priority queue.
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.
306 #define LIST_ENQUEUE(list, node) \
309 FOREACH_NODE(ln, (list)) \
310 if (ln->pri < (node)->pri) \
312 INSERT_BEFORE(&(node)->link, &ln->link); \
317 * Unlink a node from the head of the list \a l.
319 * \return Pointer to node, or NULL if the list was empty.
321 INLINE Node *list_remHead(List *l)
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. */
337 * Unlink a node from the tail of the list \a l.
339 * \return Pointer to node, or NULL if the list was empty.
341 INLINE Node *list_remTail(List *l)
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. */
356 #endif /* MWARE_LIST_H */