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.20 2006/07/19 12:56:28 bernie
19 *#* Convert to new Doxygen style.
21 *#* Revision 1.19 2006/03/20 17:50:29 bernie
24 *#* Revision 1.18 2006/02/27 22:40:21 bernie
25 *#* Add support for poor pre-C99 compilers.
27 *#* Revision 1.17 2006/02/24 01:18:34 bernie
28 *#* LIST_ENQUEUE(): New macro; Remove obsolete names.
30 *#* Revision 1.16 2006/01/23 23:10:38 bernie
31 *#* REVERSE_FOREACH_NODE(): New macro; FOREACHNODE(): Rename to FOREACH_NODE.
33 *#* Revision 1.15 2005/11/04 16:20:02 bernie
34 *#* Fix reference to README.devlib in header.
36 *#* Revision 1.14 2005/07/19 07:25:18 bernie
37 *#* Refactor to remove type aliasing problems.
39 *#* Revision 1.13 2005/04/11 19:10:28 bernie
40 *#* Include top-level headers from cfg/ subdir.
42 *#* Revision 1.12 2005/01/22 04:21:32 bernie
43 *#* Add integrity checks.
45 *#* Revision 1.11 2004/12/31 16:44:11 bernie
46 *#* list_remHead(), list_remTail(): Name like normal functions.
48 *#* Revision 1.10 2004/11/28 23:21:05 bernie
49 *#* Remove obsolete INITLIST macro.
51 *#* Revision 1.9 2004/10/21 09:37:55 bernie
52 *#* Revamp documentation.
54 *#* Revision 1.8 2004/10/19 08:46:34 bernie
57 *#* Revision 1.7 2004/08/25 14:12:09 rasky
58 *#* Aggiornato il comment block dei log RCS
60 *#* Revision 1.6 2004/07/30 14:34:10 rasky
61 *#* Vari fix per documentazione e commenti
62 *#* Aggiunte PP_CATn e STATIC_ASSERT
64 *#* Revision 1.5 2004/07/20 23:45:01 bernie
65 *#* Finally remove redundant protos.
67 *#* Revision 1.4 2004/07/18 22:12:53 bernie
68 *#* Fix warnings with GCC 3.3.2.
70 *#* Revision 1.3 2004/07/18 22:01:43 bernie
71 *#* REMHEAD(), REMTAIL(): Move to list.h as inline functions.
73 *#* Revision 1.2 2004/06/03 11:27:09 bernie
74 *#* Add dual-license information.
76 *#* Revision 1.1 2004/05/23 15:43:16 bernie
77 *#* Import mware modules.
83 #include <cfg/compiler.h> /* INLINE */
84 #include <cfg/debug.h> /* ASSERT() */
87 * This structure represents a node for bidirectional lists.
89 * Data is usually appended to nodes by making them the first
90 * field of another struture, as a poor-man's form of inheritance.
99 * Head of a doubly-linked list of \c Node structs.
101 * Lists must be initialized with LIST_INIT() prior to use.
103 * Nodes can be added and removed from either end of the list
104 * with O(1) performance. Iterating over these lists can be
105 * tricky: use the FOREACH_NODE() macro instead.
114 * Extended node for priority queues.
116 typedef struct _PriNode
124 * Template for a naked node in a list of \a T structures.
126 * To be used as data member in other structures:
131 * DECLARE_NODE_ANON(struct Foo)
136 * DECLARE_LIST_TYPE(Foo);
140 * static LIST_TYPE(Foo) foo_list;
141 * static Foo foo1, foo2;
144 * LIST_INIT(&foo_list);
145 * ADDHEAD(&foo_list, &foo1);
146 * INSERT_BEFORE(&foo_list, &foo2);
147 * FOREACH_NODE(fp, &foo_list)
153 #define DECLARE_NODE_ANON(T) \
156 /** Declare a typesafe node for structures of type \a T. */
157 #define DECLARE_NODE_TYPE(T) \
158 typedef struct T##Node { T *succ; T *pred; } T##Node
160 /** Template for a list of \a T structures. */
161 #define DECLARE_LIST_TYPE(T) \
162 DECLARE_NODE_TYPE(T); \
163 typedef struct T##List { \
168 #define NODE_TYPE(T) T##Node
169 #define LIST_TYPE(T) T##List
172 * Get a pointer to the first node in a list.
174 * If \a l is empty, result points to l->tail.
176 #define LIST_HEAD(l) ((l)->head.succ)
179 * Get a pointer to the last node in a list.
181 * If \a l is empty, result points to l->head.
183 #define LIST_TAIL(l) ((l)->tail.pred)
185 // TODO: move in compiler.h
187 #define TYPEOF_OR_VOIDPTR(type) typeof(type)
189 #define TYPEOF_OR_VOIDPTR(type) void *
193 * Iterate over all nodes in a list.
195 * This macro generates a "for" statement using the following parameters:
196 * \param n Node pointer to be used in each iteration.
197 * \param l Pointer to list.
199 #define FOREACH_NODE(n, l) \
201 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
202 ((Node *)(n))->succ; \
203 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
207 * Iterate backwards over all nodes in a list.
209 * This macro generates a "for" statement using the following parameters:
210 * \param n Node pointer to be used in each iteration.
211 * \param l Pointer to list.
213 #define REVERSE_FOREACH_NODE(n, l) \
215 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
216 ((Node *)(n))->pred; \
217 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
220 /** Initialize a list. */
221 #define LIST_INIT(l) \
223 (l)->head.succ = (TYPEOF_OR_VOIDPTR((l)->head.succ)) &(l)->tail; \
224 (l)->head.pred = NULL; \
225 (l)->tail.succ = NULL; \
226 (l)->tail.pred = (TYPEOF_OR_VOIDPTR((l)->tail.pred)) &(l)->head; \
230 /** Make sure that a list is valid (it was initialized and is not corrupted). */
231 #define LIST_ASSERT_VALID(l) \
234 ASSERT((l)->head.succ != NULL); \
235 ASSERT((l)->head.pred == NULL); \
236 ASSERT((l)->tail.succ == NULL); \
237 ASSERT((l)->tail.pred != NULL); \
241 ASSERT(n->pred == pred); \
244 ASSERT(n == &(l)->tail); \
247 #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
249 #define LIST_ASSERT_VALID(l) do {} while (0)
250 #define INVALIDATE_NODE(n) do {} while (0)
253 /** Add node to list head. */
254 #define ADDHEAD(l,n) \
258 (n)->succ = (l)->head.succ; \
259 (n)->pred = (l)->head.succ->pred; \
260 (n)->succ->pred = (n); \
261 (n)->pred->succ = (n); \
264 /** Add node to list tail. */
265 #define ADDTAIL(l,n) \
269 (n)->succ = &(l)->tail; \
270 (n)->pred = (l)->tail.pred; \
271 (n)->pred->succ = (n); \
272 (l)->tail.pred = (n); \
276 * Insert node \a n before node \a ln.
278 * \note You can't pass in a list header as \a ln, but
279 * it is safe to pass list-\>head of an empty list.
281 #define INSERT_BEFORE(n,ln) \
284 (n)->pred = (ln)->pred; \
285 (ln)->pred->succ = (n); \
290 * Remove \a n from whatever list it is in.
292 * \note Removing a node that has not previously been
293 * inserted into a list invokes undefined behavior.
297 (n)->pred->succ = (n)->succ; \
298 (n)->succ->pred = (n)->pred; \
299 INVALIDATE_NODE(n); \
302 /** Tell whether a list is empty. */
303 #define LIST_EMPTY(l) ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
306 * Insert a priority node in a priority queue.
308 * The new node is inserted immediately before the
309 * first node with lower priority or appended to
310 * the tail if no such node exists.
312 #define LIST_ENQUEUE(list, node) \
315 FOREACH_NODE(ln, (list)) \
316 if (ln->pri < (node)->pri) \
318 INSERT_BEFORE(&(node)->link, &ln->link); \
323 * Unlink a node from the head of the list \a l.
325 * \return Pointer to node, or NULL if the list was empty.
327 INLINE Node *list_remHead(List *l)
334 n = l->head.succ; /* Get first node. */
335 l->head.succ = n->succ; /* Link list head to second node. */
336 n->succ->pred = &l->head; /* Link second node to list head. */
343 * Unlink a node from the tail of the list \a l.
345 * \return Pointer to node, or NULL if the list was empty.
347 INLINE Node *list_remTail(List *l)
354 n = l->tail.pred; /* Get last node. */
355 l->tail.pred = n->pred; /* Link list tail to second last node. */
356 n->pred->succ = &l->tail; /* Link second last node to list tail. */
362 #endif /* MWARE_LIST_H */