3 * Copyright (C) 2001 Bernardo Innocenti <bernie@develer.com>
4 * Copyright (C) 2003 Develer S.r.l. (http://www.develer.com/)
9 * \author Bernardo Innocenti <bernie@develer.com>
11 * \brief General pourpose double-linked lists
16 * Revision 1.1 2004/05/23 15:43:16 bernie
17 * Import mware modules.
37 /*! Template for a list of \a T structures */
38 #define DECLARE_LIST(T) \
39 struct { T *head; T *null; T *tail; }
41 /*! Template for a node in a list of \a T structures */
42 #define DECLARE_NODE(T) \
43 struct { T *succ; T *pred; }
45 /*! Template for a node in a list of \a T structures */
46 #define DECLARE_NODE_ANON(T) \
50 * Iterate over all nodes in a list. This statement defines a for cicle
51 * accepting the following parameters:
52 * \param n node pointer to be used in each iteration
53 * \param l pointer to list
55 #define FOREACHNODE(n,l) \
57 (n) = (typeof(n))((l)->head); \
58 ((Node *)(n))->succ; \
59 (n) = (typeof(n))(((Node *)(n))->succ) \
62 /*! Initialize a list */
65 (l)->head = (Node *)(&(l)->null); \
67 (l)->tail = (Node *)(&(l)->head); \
70 /*! Add node to list head */
71 #define ADDHEAD(l,n) \
73 (n)->succ = (l)->head; \
74 (n)->pred = (Node *)&(l)->head; \
75 (n)->succ->pred = (n); \
79 /*! Add node to list tail */
80 #define ADDTAIL(l,n) \
82 (n)->succ = (Node *)(&(l)->null); \
83 (n)->pred = (l)->tail; \
84 (n)->pred->succ = (n); \
89 * Insert node \a n before node \a ln
90 * Note: you can't pass in a list header as \a ln, but
91 * it is safe to pass list-\>head of an empty list.
93 #define INSERTBEFORE(n,ln) \
96 (n)->pred = (ln)->pred; \
97 (ln)->pred->succ = (n); \
101 /*! Remove \a n from whatever list it is in */
104 (n)->pred->succ = (n)->succ; \
105 (n)->succ->pred = (n)->pred; \
108 /*! Tell whether a list is empty */
109 #define ISLISTEMPTY(l) ( (l)->head == (Node *)(&(l)->null) )
111 /*! \name Extract an element from the head/tail of the list. If the list empty, return NULL. */
113 #define REMHEAD(l) _list_rem_head(l)
114 #define REMTAIL(l) _list_rem_tail(l)
117 /* Prototypes for out-of-line functions */
118 Node *_list_rem_head(List *l);
119 Node *_list_rem_tail(List *l);
122 #endif /* MWARE_LIST_H */