Refactor to use new protocol module and sipo.
[bertos.git] / bertos / struct / list.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2001, 2008 Bernie Innocenti <bernie@codewiz.org>
31  * -->
32  *
33  * \defgroup list General purpose lists
34  * \ingroup struct
35  * \{
36  *
37  * \brief General pourpose double-linked lists
38  *
39  * Lists contain nodes. You can put any custom struct into any list as long
40  * as it has a Node struct inside it. If you make the Node struct the first
41  * member of your data type, you can simply cast it to (Node *) when passing
42  * it to list functions.
43  *
44  * Lists must be initialized before use with LIST_INIT(). You can then add
45  * objects using ADDHEAD() and ADDTAIL() macros, and remove them with
46  * list_remHead() and list_remTail().
47  *
48  * You can create lists with priorities by using PriNode instead of Node as
49  * the base member struct.
50  * Use LIST_ENQUEUE() and LIST_ENQUEUE_HEAD() to insert a priority node into
51  * a list.
52  *
53  * To iterate over a list, use the macros FOREACH_NODE() and REVERSE_FOREACH_NODE()
54  * in this way:
55  * \code
56  * struct Foo
57  * {
58  *     Node n;
59  *     int a;
60  * }
61  *
62  * int main()
63  * {
64  *        List foo_list;
65  *        static Foo foo1, foo2;
66  *        Foo *fp;
67  *
68  *        LIST_INIT(&foo_list);
69  *        ADDHEAD(&foo_list, (Node *)&foo1);
70  *        INSERT_BEFORE(&foo_list, (Node *)&foo2);
71  *        FOREACH_NODE(fp, &foo_list)
72  *            fp->a = 10;
73  * }
74  * \endcode
75  *
76  * \author Bernie Innocenti <bernie@codewiz.org>
77  */
78
79 #ifndef STRUCT_LIST_H
80 #define STRUCT_LIST_H
81
82 #include <cfg/compiler.h> /* INLINE */
83 #include <cfg/debug.h> /* ASSERT_VALID_PTR() */
84
85 /**
86  * This structure represents a node for bidirectional lists.
87  *
88  * Data is usually appended to nodes by making them the first
89  * field of another struture, as a poor-man's form of inheritance.
90  */
91 typedef struct _Node
92 {
93         struct _Node *succ;
94         struct _Node *pred;
95 } Node;
96
97 /**
98  * Head of a doubly-linked list of \c Node structs.
99  *
100  * Lists must be initialized with LIST_INIT() prior to use.
101  *
102  * Nodes can be added and removed from either end of the list
103  * with O(1) performance.  Iterating over these lists can be
104  * tricky: use the FOREACH_NODE() macro instead.
105  */
106 typedef struct _List
107 {
108         Node head;
109         Node tail;
110 } List;
111
112 /**
113  * Extended node for priority queues.
114  */
115 typedef struct _PriNode
116 {
117         Node link;
118         int  pri;
119 } PriNode;
120
121
122 /**
123  * Template for a naked node in a list of \a T structures.
124  *
125  * To be used as data member in other structures:
126  *
127  * \code
128  *    struct Foo
129  *    {
130  *        DECLARE_NODE_ANON(struct Foo)
131  *        int a;
132  *        float b;
133  *    }
134  *
135  *    DECLARE_LIST_TYPE(Foo);
136  *
137  *    void foo(void)
138  *    {
139  *        static LIST_TYPE(Foo) foo_list;
140  *        static Foo foo1, foo2;
141  *        Foo *fp;
142  *
143  *        LIST_INIT(&foo_list);
144  *        ADDHEAD(&foo_list, &foo1);
145  *        INSERT_BEFORE(&foo_list, &foo2);
146  *        FOREACH_NODE(fp, &foo_list)
147  *            fp->a = 10;
148  *    }
149  *
150  * \endcode
151  */
152 #define DECLARE_NODE_ANON(T) \
153         T *succ; T *pred;
154
155 /** Declare a typesafe node for structures of type \a T. */
156 #define DECLARE_NODE_TYPE(T) \
157         typedef struct T##Node { T *succ; T *pred; } T##Node
158
159 /** Template for a list of \a T structures. */
160 #define DECLARE_LIST_TYPE(T) \
161         DECLARE_NODE_TYPE(T); \
162         typedef struct T##List { \
163                  T##Node head; \
164                  T##Node tail; \
165         } T##List
166
167 #define NODE_TYPE(T) T##Node
168 #define LIST_TYPE(T) T##List
169
170 /**
171  * Get a pointer to the first node in a list.
172  *
173  * If \a l is empty, result points to l->tail.
174  */
175 #define LIST_HEAD(l) ((l)->head.succ)
176
177 /**
178  * Get a pointer to the last node in a list.
179  *
180  * If \a l is empty, result points to l->head.
181  */
182 #define LIST_TAIL(l) ((l)->tail.pred)
183
184 // TODO: move in compiler.h
185 #if COMPILER_TYPEOF
186         #define TYPEOF_OR_VOIDPTR(type) typeof(type)
187 #else
188         #define TYPEOF_OR_VOIDPTR(type) void *
189 #endif
190
191 /**
192  * Iterate over all nodes in a list.
193  *
194  * This macro generates a "for" statement using the following parameters:
195  * \param n   Node pointer to be used in each iteration.
196  * \param l   Pointer to list.
197  */
198 #define FOREACH_NODE(n, l) \
199         for( \
200                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
201                 ((Node *)(n))->succ; \
202                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
203         )
204
205 /**
206  * Iterate backwards over all nodes in a list.
207  *
208  * This macro generates a "for" statement using the following parameters:
209  * \param n   Node pointer to be used in each iteration.
210  * \param l   Pointer to list.
211  */
212 #define REVERSE_FOREACH_NODE(n, l) \
213         for( \
214                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
215                 ((Node *)(n))->pred; \
216                 (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
217         )
218
219 /**
220  * Iterate on the list safely against node removal.
221  *
222  * This macro generates a "for" statement using the following parameters:
223  * \param n   Node pointer to be used in each iteration.
224  * \param p   Temporal storage for the iterator.
225  * \param l   Pointer to list.
226  */
227 #define FOREACH_NODE_SAFE(n, p, l) \
228         for( \
229                 (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l), (p) = ((Node *)(n))->succ; \
230                 ((Node *)(n))->succ; \
231                 (n) = (p), (p) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
232         )
233
234 /** Initialize a list. */
235 #define LIST_INIT(l) \
236         do { \
237                 (l)->head.succ = (TYPEOF_OR_VOIDPTR((l)->head.succ)) &(l)->tail; \
238                 (l)->head.pred = NULL; \
239                 (l)->tail.succ = NULL; \
240                 (l)->tail.pred = (TYPEOF_OR_VOIDPTR((l)->tail.pred)) &(l)->head; \
241         } while (0)
242
243 #ifdef _DEBUG
244         /** Make sure that a list is valid (it was initialized and is not corrupted). */
245         #define LIST_ASSERT_VALID(l) \
246                 do { \
247                         Node *n, *pred; \
248                         ASSERT((l)->head.succ != NULL); \
249                         ASSERT((l)->head.pred == NULL); \
250                         ASSERT((l)->tail.succ == NULL); \
251                         ASSERT((l)->tail.pred != NULL); \
252                         pred = &(l)->head; \
253                         FOREACH_NODE(n, l) \
254                         { \
255                                 ASSERT(n->pred == pred); \
256                                 pred = n; \
257                         } \
258                         ASSERT(n == &(l)->tail); \
259                 } while (0)
260
261         /// Checks that a node isn't part of a given list
262         #define LIST_ASSERT_NOT_CONTAINS(list,node) \
263                 do { \
264                         Node *ln; \
265                         ASSERT_VALID_PTR(list); \
266                         ASSERT_VALID_PTR(node); \
267                         FOREACH_NODE(ln, list) \
268                                 ASSERT(ln != (Node *)(node)); \
269                 } while (0)
270
271         #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL)
272 #else
273         #define LIST_ASSERT_VALID(l) do {} while (0)
274         #define LIST_ASSERT_NOT_CONTAINS(list,node) do {} while (0)
275         #define INVALIDATE_NODE(n) do {} while (0)
276 #endif
277
278 /** Tell whether a list is empty. */
279 #define LIST_EMPTY(l)  ( (void *)((l)->head.succ) == (void *)(&(l)->tail) )
280
281 /** Add node to list head. */
282 #define ADDHEAD(l,n) \
283         do { \
284                 LIST_ASSERT_NOT_CONTAINS((l),(n)); \
285                 (n)->succ = (l)->head.succ; \
286                 (n)->pred = (l)->head.succ->pred; \
287                 (n)->succ->pred = (n); \
288                 (n)->pred->succ = (n); \
289         } while (0)
290
291 /** Add node to list tail. */
292 #define ADDTAIL(l,n) \
293         do { \
294                 LIST_ASSERT_NOT_CONTAINS((l),(n)); \
295                 (n)->succ = &(l)->tail; \
296                 (n)->pred = (l)->tail.pred; \
297                 (n)->pred->succ = (n); \
298                 (l)->tail.pred = (n); \
299         } while (0)
300
301 /**
302  * Insert node \a n before node \a ln.
303  *
304  * \note You can't pass in a list header as \a ln, but
305  *       it is safe to pass list-\>head of an empty list.
306  */
307 #define INSERT_BEFORE(n,ln) \
308         do { \
309                 ASSERT_VALID_PTR(n); \
310                 ASSERT_VALID_PTR(ln); \
311                 (n)->succ = (ln); \
312                 (n)->pred = (ln)->pred; \
313                 (ln)->pred->succ = (n); \
314                 (ln)->pred = (n); \
315         } while (0)
316
317 /**
318  * Remove \a n from whatever list it is in.
319  *
320  * \note Removing a node that has not previously been
321  *       inserted into a list invokes undefined behavior.
322  */
323 #define REMOVE(n) \
324         do { \
325                 ASSERT_VALID_PTR(n); \
326                 (n)->pred->succ = (n)->succ; \
327                 (n)->succ->pred = (n)->pred; \
328                 INVALIDATE_NODE(n); \
329         } while (0)
330
331 /**
332  * Insert a priority node in a priority queue.
333  *
334  * The new node is inserted immediately before the first node with the same
335  * priority or appended to the tail if no such node exists.
336  */
337 #define LIST_ENQUEUE_HEAD(list, node) \
338         do { \
339                 PriNode *ln; \
340                 LIST_ASSERT_NOT_CONTAINS((list),(node)); \
341                 FOREACH_NODE(ln, (list)) \
342                         if (ln->pri <= (node)->pri) \
343                                 break; \
344                 INSERT_BEFORE(&(node)->link, &ln->link); \
345         } while (0)
346
347 /**
348  * Insert a priority node in a priority queue.
349  *
350  * The new node is inserted immediately before the first node with lower
351  * priority or appended to the tail if no such node exists.
352  */
353 #define LIST_ENQUEUE(list, node) \
354         do { \
355                 PriNode *ln; \
356                 LIST_ASSERT_NOT_CONTAINS((list),(node)); \
357                 FOREACH_NODE(ln, (list)) \
358                         if (ln->pri < (node)->pri) \
359                                 break; \
360                 INSERT_BEFORE(&(node)->link, &ln->link); \
361         } while (0)
362
363
364 /**
365  * Unlink a node from the head of the list \a l.
366  *
367  * \return Pointer to node, or NULL if the list was empty.
368  */
369 INLINE Node *list_remHead(List *l)
370 {
371         Node *n;
372
373         ASSERT_VALID_PTR(l);
374
375         if (LIST_EMPTY(l))
376                 return (Node *)0;
377
378         n = l->head.succ; /* Get first node. */
379         l->head.succ = n->succ; /* Link list head to second node. */
380         n->succ->pred = &l->head; /* Link second node to list head. */
381
382         INVALIDATE_NODE(n);
383         return n;
384 }
385
386 /**
387  * Unlink a node from the tail of the list \a l.
388  *
389  * \return Pointer to node, or NULL if the list was empty.
390  */
391 INLINE Node *list_remTail(List *l)
392 {
393         Node *n;
394
395         ASSERT_VALID_PTR(l);
396
397         if (LIST_EMPTY(l))
398                 return NULL;
399
400         n = l->tail.pred; /* Get last node. */
401         l->tail.pred = n->pred; /* Link list tail to second last node. */
402         n->pred->succ = &l->tail; /* Link second last node to list tail. */
403
404         INVALIDATE_NODE(n);
405         return n;
406 }
407
408 /** \} */ //defgroup list
409
410 #endif /* STRUCT_LIST_H */