Include top-level headers from cfg/ subdir.
[bertos.git] / mware / list.h
1 /*!
2  * \file
3  * <!--
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 devlib/README for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief General pourpose double-linked lists
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.13  2005/04/11 19:10:28  bernie
19  *#* Include top-level headers from cfg/ subdir.
20  *#*
21  *#* Revision 1.12  2005/01/22 04:21:32  bernie
22  *#* Add integrity checks.
23  *#*
24  *#* Revision 1.11  2004/12/31 16:44:11  bernie
25  *#* list_remHead(), list_remTail(): Name like normal functions.
26  *#*
27  *#* Revision 1.10  2004/11/28 23:21:05  bernie
28  *#* Remove obsolete INITLIST macro.
29  *#*
30  *#* Revision 1.9  2004/10/21 09:37:55  bernie
31  *#* Revamp documentation.
32  *#*
33  *#* Revision 1.8  2004/10/19 08:46:34  bernie
34  *#* Fix header.
35  *#*
36  *#* Revision 1.7  2004/08/25 14:12:09  rasky
37  *#* Aggiornato il comment block dei log RCS
38  *#*
39  *#* Revision 1.6  2004/07/30 14:34:10  rasky
40  *#* Vari fix per documentazione e commenti
41  *#* Aggiunte PP_CATn e STATIC_ASSERT
42  *#*
43  *#* Revision 1.5  2004/07/20 23:45:01  bernie
44  *#* Finally remove redundant protos.
45  *#*
46  *#* Revision 1.4  2004/07/18 22:12:53  bernie
47  *#* Fix warnings with GCC 3.3.2.
48  *#*
49  *#* Revision 1.3  2004/07/18 22:01:43  bernie
50  *#* REMHEAD(), REMTAIL(): Move to list.h as inline functions.
51  *#*
52  *#* Revision 1.2  2004/06/03 11:27:09  bernie
53  *#* Add dual-license information.
54  *#*
55  *#* Revision 1.1  2004/05/23 15:43:16  bernie
56  *#* Import mware modules.
57  *#*
58  *#*/
59 #ifndef MWARE_LIST_H
60 #define MWARE_LIST_H
61
62 #include <cfg/compiler.h> // INLINE
63
64 /*!
65  * This structure represents a node for bidirectional lists.
66  *
67  * Data is usually appended to nodes by making them the first
68  * field of another struture, as a poor-man's form of inheritance.
69  */
70 typedef struct _Node
71 {
72         struct _Node *succ;
73         struct _Node *pred;
74 } Node;
75
76 /*!
77  * Head of a doubly-linked list of \c Node structs.
78  *
79  * Lists must be initialized with LIST_INIT() prior to use.
80  *
81  * Nodes can be added and removed from either end of the list
82  * with O(1) performance.  Iterating over these lists can be
83  * tricky: use the FOREACHNODE() macro instead.
84  */
85 typedef struct _List
86 {
87         Node *head;
88         Node *null;
89         Node *tail;
90 } List;
91
92
93 /*! Template for a list of \a T structures. */
94 #define DECLARE_LIST(T) \
95         struct { T *head; T *null; T *tail; }
96
97 /*! Template for a node in a list of \a T structures. */
98 #define DECLARE_NODE(T) \
99         struct { T *succ; T *pred; }
100
101 /*! Template for a node in a list of \a T structures. */
102 #define DECLARE_NODE_ANON(T) \
103         T *succ; T *pred;
104
105 /*!
106  * Iterate over all nodes in a list.
107  *
108  * This macro generates a "for" statement using the following parameters:
109  * \param n   Node pointer to be used in each iteration.
110  * \param l   Pointer to list.
111  */
112 #define FOREACHNODE(n,l) \
113         for( \
114                 (n) = (typeof(n))((l)->head); \
115                 ((Node *)(n))->succ; \
116                 (n) = (typeof(n))(((Node *)(n))->succ) \
117         )
118
119 /*! Initialize a list. */
120 #define LIST_INIT(l) \
121         do { \
122                 (l)->head = (Node *)(&(l)->null); \
123                 (l)->null = NULL; \
124                 (l)->tail = (Node *)(&(l)->head); \
125         } while (0)
126
127 /* Make sure that a list is valid (it was initialized and is not corrupted) */
128 #ifdef _DEBUG
129         #define LIST_ASSERT_VALID(l) \
130                 do { \
131                         Node *n, *pred; \
132                         ASSERT((l)->head != NULL); \
133                         ASSERT((l)->null == NULL); \
134                         ASSERT((l)->tail != NULL); \
135                         pred = (Node *)(&(l)->head); \
136                         FOREACHNODE(n, l) \
137                         { \
138                                 ASSERT(n->pred == pred); \
139                                 pred = n; \
140                         } \
141                         ASSERT(n == (Node *)(&(l)->null)); \
142                 } while (0)
143 #else
144         #define LIST_ASSERT_VALID(l) do {} while (0)
145 #endif
146
147 /*! Add node to list head. */
148 #define ADDHEAD(l,n) \
149         do { \
150                 (n)->succ = (l)->head; \
151                 (n)->pred = (Node *)&(l)->head; \
152                 (n)->succ->pred = (n); \
153                 (l)->head = (n); \
154         } while (0)
155
156 /*! Add node to list tail. */
157 #define ADDTAIL(l,n) \
158         do { \
159                 (n)->succ = (Node *)(&(l)->null); \
160                 (n)->pred = (l)->tail; \
161                 (n)->pred->succ = (n); \
162                 (l)->tail = (n); \
163         } while (0)
164
165 /*!
166  * Insert node \a n before node \a ln.
167  *
168  * \note You can't pass in a list header as \a ln, but
169  *       it is safe to pass list-\>head of an empty list.
170  */
171 #define INSERTBEFORE(n,ln) \
172         do { \
173                 (n)->succ = (ln); \
174                 (n)->pred = (ln)->pred; \
175                 (ln)->pred->succ = (n); \
176                 (ln)->pred = (n); \
177         } while (0)
178
179 /*!
180  * Remove \a n from whatever list it is in.
181  *
182  * \note Removing a node that has not previously been
183  *       inserted into a list invokes undefined behavior.
184  */
185 #define REMOVE(n) \
186         do { \
187                 (n)->pred->succ = (n)->succ; \
188                 (n)->succ->pred = (n)->pred; \
189         } while (0)
190
191 /*! Tell whether a list is empty. */
192 #define ISLISTEMPTY(l)  ( (l)->head == (Node *)(&(l)->null) )
193
194 /*!
195  * Unlink a node from the head of the list \a l.
196  *
197  * \return Pointer to node, or NULL if the list was empty.
198  */
199 INLINE Node *list_remHead(List *l)
200 {
201         Node *n;
202
203         if (ISLISTEMPTY(l))
204                 return (Node *)0;
205
206         n = l->head;
207         l->head = n->succ;
208         n->succ->pred = (Node *)l;
209         return n;
210 }
211
212 /*!
213  * Unlink a node from the tail of the list \a l.
214  *
215  * \return Pointer to node, or NULL if the list was empty.
216  */
217 INLINE Node *list_remTail(List *l)
218 {
219         Node *n;
220
221         if (ISLISTEMPTY(l))
222                 return (Node *)0;
223
224         n = l->tail;
225         l->tail = n->pred;
226         n->pred->succ = (Node *)&l->null;
227         return n;
228 }
229
230 /* OBSOLETE names */
231 #define REMHEAD list_remHead
232 #define REMTAIL list_remTail
233
234 #endif /* MWARE_LIST_H */