3 * Copyright (C) 2003,2004 Develer S.r.l. (http://www.develer.com/)
4 * Copyright (C) 2001 Bernardo Innocenti <bernie@develer.com>
5 * This file is part of DevLib - See devlib/README for information.
9 * \author Bernardo Innocenti <bernie@develer.com>
11 * \brief General pourpose double-linked lists
16 * Revision 1.4 2004/07/18 22:12:53 bernie
17 * Fix warnings with GCC 3.3.2.
19 * Revision 1.3 2004/07/18 22:01:43 bernie
20 * REMHEAD(), REMTAIL(): Move to list.h as inline functions.
22 * Revision 1.2 2004/06/03 11:27:09 bernie
23 * Add dual-license information.
25 * Revision 1.1 2004/05/23 15:43:16 bernie
26 * Import mware modules.
46 /*! Template for a list of \a T structures */
47 #define DECLARE_LIST(T) \
48 struct { T *head; T *null; T *tail; }
50 /*! Template for a node in a list of \a T structures */
51 #define DECLARE_NODE(T) \
52 struct { T *succ; T *pred; }
54 /*! Template for a node in a list of \a T structures */
55 #define DECLARE_NODE_ANON(T) \
59 * Iterate over all nodes in a list. This statement defines a for cicle
60 * accepting the following parameters:
61 * \param n node pointer to be used in each iteration
62 * \param l pointer to list
64 #define FOREACHNODE(n,l) \
66 (n) = (typeof(n))((l)->head); \
67 ((Node *)(n))->succ; \
68 (n) = (typeof(n))(((Node *)(n))->succ) \
71 /*! Initialize a list */
74 (l)->head = (Node *)(&(l)->null); \
76 (l)->tail = (Node *)(&(l)->head); \
79 /*! Add node to list head */
80 #define ADDHEAD(l,n) \
82 (n)->succ = (l)->head; \
83 (n)->pred = (Node *)&(l)->head; \
84 (n)->succ->pred = (n); \
88 /*! Add node to list tail */
89 #define ADDTAIL(l,n) \
91 (n)->succ = (Node *)(&(l)->null); \
92 (n)->pred = (l)->tail; \
93 (n)->pred->succ = (n); \
98 * Insert node \a n before node \a ln
99 * Note: you can't pass in a list header as \a ln, but
100 * it is safe to pass list-\>head of an empty list.
102 #define INSERTBEFORE(n,ln) \
105 (n)->pred = (ln)->pred; \
106 (ln)->pred->succ = (n); \
110 /*! Remove \a n from whatever list it is in */
113 (n)->pred->succ = (n)->succ; \
114 (n)->succ->pred = (n)->pred; \
117 /*! Tell whether a list is empty */
118 #define ISLISTEMPTY(l) ( (l)->head == (Node *)(&(l)->null) )
121 * \name Unlink a node from the head of the list \a l.
122 * \return Pointer to node, or NULL if the list was empty.
124 INLINE Node *REMHEAD(List *l);
125 INLINE Node *REMHEAD(List *l)
134 n->succ->pred = (Node *)l;
139 * \name Unlink a node from the tail of the list \a l.
140 * \return Pointer to node, or NULL if the list was empty.
142 INLINE Node *REMTAIL(List *l);
143 INLINE Node *REMTAIL(List *l)
152 n->pred->succ = (Node *)&l->null;
156 #endif /* MWARE_LIST_H */