4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2001 Bernardo Innocenti <bernie@develer.com>
36 * \author Bernardo Innocenti <bernie@develer.com>
38 * \brief General pourpose double-linked lists
43 *#* Revision 1.20 2006/07/19 12:56:28 bernie
44 *#* Convert to new Doxygen style.
46 *#* Revision 1.19 2006/03/20 17:50:29 bernie
49 *#* Revision 1.18 2006/02/27 22:40:21 bernie
50 *#* Add support for poor pre-C99 compilers.
52 *#* Revision 1.17 2006/02/24 01:18:34 bernie
53 *#* LIST_ENQUEUE(): New macro; Remove obsolete names.
55 *#* Revision 1.16 2006/01/23 23:10:38 bernie
56 *#* REVERSE_FOREACH_NODE(): New macro; FOREACHNODE(): Rename to FOREACH_NODE.
58 *#* Revision 1.15 2005/11/04 16:20:02 bernie
59 *#* Fix reference to README.devlib in header.
61 *#* Revision 1.14 2005/07/19 07:25:18 bernie
62 *#* Refactor to remove type aliasing problems.
64 *#* Revision 1.13 2005/04/11 19:10:28 bernie
65 *#* Include top-level headers from cfg/ subdir.
67 *#* Revision 1.12 2005/01/22 04:21:32 bernie
68 *#* Add integrity checks.
70 *#* Revision 1.11 2004/12/31 16:44:11 bernie
71 *#* list_remHead(), list_remTail(): Name like normal functions.
73 *#* Revision 1.10 2004/11/28 23:21:05 bernie
74 *#* Remove obsolete INITLIST macro.
76 *#* Revision 1.9 2004/10/21 09:37:55 bernie
77 *#* Revamp documentation.
79 *#* Revision 1.8 2004/10/19 08:46:34 bernie
82 *#* Revision 1.7 2004/08/25 14:12:09 rasky
83 *#* Aggiornato il comment block dei log RCS
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
89 *#* Revision 1.5 2004/07/20 23:45:01 bernie
90 *#* Finally remove redundant protos.
92 *#* Revision 1.4 2004/07/18 22:12:53 bernie
93 *#* Fix warnings with GCC 3.3.2.
95 *#* Revision 1.3 2004/07/18 22:01:43 bernie
96 *#* REMHEAD(), REMTAIL(): Move to list.h as inline functions.
98 *#* Revision 1.2 2004/06/03 11:27:09 bernie
99 *#* Add dual-license information.
101 *#* Revision 1.1 2004/05/23 15:43:16 bernie
102 *#* Import mware modules.
108 #include <cfg/compiler.h> /* INLINE */
109 #include <cfg/debug.h> /* ASSERT() */
112 * This structure represents a node for bidirectional lists.
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.
124 * Head of a doubly-linked list of \c Node structs.
126 * Lists must be initialized with LIST_INIT() prior to use.
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.
139 * Extended node for priority queues.
141 typedef struct _PriNode
149 * Template for a naked node in a list of \a T structures.
151 * To be used as data member in other structures:
156 * DECLARE_NODE_ANON(struct Foo)
161 * DECLARE_LIST_TYPE(Foo);
165 * static LIST_TYPE(Foo) foo_list;
166 * static Foo foo1, foo2;
169 * LIST_INIT(&foo_list);
170 * ADDHEAD(&foo_list, &foo1);
171 * INSERT_BEFORE(&foo_list, &foo2);
172 * FOREACH_NODE(fp, &foo_list)
178 #define DECLARE_NODE_ANON(T) \
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
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 { \
193 #define NODE_TYPE(T) T##Node
194 #define LIST_TYPE(T) T##List
197 * Get a pointer to the first node in a list.
199 * If \a l is empty, result points to l->tail.
201 #define LIST_HEAD(l) ((l)->head.succ)
204 * Get a pointer to the last node in a list.
206 * If \a l is empty, result points to l->head.
208 #define LIST_TAIL(l) ((l)->tail.pred)
210 // TODO: move in compiler.h
212 #define TYPEOF_OR_VOIDPTR(type) typeof(type)
214 #define TYPEOF_OR_VOIDPTR(type) void *
218 * Iterate over all nodes in a list.
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.
224 #define FOREACH_NODE(n, l) \
226 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
227 ((Node *)(n))->succ; \
228 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
232 * Iterate backwards over all nodes in a list.
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.
238 #define REVERSE_FOREACH_NODE(n, l) \
240 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
241 ((Node *)(n))->pred; \
242 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
245 /** Initialize a list. */
246 #define LIST_INIT(l) \
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; \
255 /** Make sure that a list is valid (it was initialized and is not corrupted). */
256 #define LIST_ASSERT_VALID(l) \
259 ASSERT((l)->head.succ != NULL); \
260 ASSERT((l)->head.pred == NULL); \
261 ASSERT((l)->tail.succ == NULL); \
262 ASSERT((l)->tail.pred != NULL); \
266 ASSERT(n->pred == pred); \
269 ASSERT(n == &(l)->tail); \
272 #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
274 #define LIST_ASSERT_VALID(l) do {} while (0)
275 #define INVALIDATE_NODE(n) do {} while (0)
278 /** Add node to list head. */
279 #define ADDHEAD(l,n) \
283 (n)->succ = (l)->head.succ; \
284 (n)->pred = (l)->head.succ->pred; \
285 (n)->succ->pred = (n); \
286 (n)->pred->succ = (n); \
289 /** Add node to list tail. */
290 #define ADDTAIL(l,n) \
294 (n)->succ = &(l)->tail; \
295 (n)->pred = (l)->tail.pred; \
296 (n)->pred->succ = (n); \
297 (l)->tail.pred = (n); \
301 * Insert node \a n before node \a ln.
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.
306 #define INSERT_BEFORE(n,ln) \
309 (n)->pred = (ln)->pred; \
310 (ln)->pred->succ = (n); \
315 * Remove \a n from whatever list it is in.
317 * \note Removing a node that has not previously been
318 * inserted into a list invokes undefined behavior.
322 (n)->pred->succ = (n)->succ; \
323 (n)->succ->pred = (n)->pred; \
324 INVALIDATE_NODE(n); \
327 /** Tell whether a list is empty. */
328 #define LIST_EMPTY(l) ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
331 * Insert a priority node in a priority queue.
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.
337 #define LIST_ENQUEUE(list, node) \
340 FOREACH_NODE(ln, (list)) \
341 if (ln->pri < (node)->pri) \
343 INSERT_BEFORE(&(node)->link, &ln->link); \
348 * Unlink a node from the head of the list \a l.
350 * \return Pointer to node, or NULL if the list was empty.
352 INLINE Node *list_remHead(List *l)
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. */
368 * Unlink a node from the tail of the list \a l.
370 * \return Pointer to node, or NULL if the list was empty.
372 INLINE Node *list_remTail(List *l)
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. */
387 #endif /* MWARE_LIST_H */