Vari fix per documentazione e commenti
[bertos.git] / mware / list.h
1 /*!
2  * \file
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.
6  *
7  * \version $Id$
8  *
9  * \author Bernardo Innocenti <bernie@develer.com>
10  *
11  * \brief General pourpose double-linked lists
12  */
13
14 /*
15  * $Log$
16  * Revision 1.6  2004/07/30 14:34:10  rasky
17  * Vari fix per documentazione e commenti
18  * Aggiunte PP_CATn e STATIC_ASSERT
19  *
20  * Revision 1.5  2004/07/20 23:45:01  bernie
21  * Finally remove redundant protos.
22  *
23  * Revision 1.4  2004/07/18 22:12:53  bernie
24  * Fix warnings with GCC 3.3.2.
25  *
26  * Revision 1.3  2004/07/18 22:01:43  bernie
27  * REMHEAD(), REMTAIL(): Move to list.h as inline functions.
28  *
29  * Revision 1.2  2004/06/03 11:27:09  bernie
30  * Add dual-license information.
31  *
32  * Revision 1.1  2004/05/23 15:43:16  bernie
33  * Import mware modules.
34  *
35  */
36 #ifndef MWARE_LIST_H
37 #define MWARE_LIST_H
38
39 typedef struct _Node
40 {
41         struct _Node *succ;
42         struct _Node *pred;
43 } Node;
44
45 typedef struct _List
46 {
47         Node *head;
48         Node *null;
49         Node *tail;
50 } List;
51
52
53 /*! Template for a list of \a T  structures */
54 #define DECLARE_LIST(T) \
55         struct { T *head; T *null; T *tail; }
56
57 /*! Template for a node in a list of \a T structures */
58 #define DECLARE_NODE(T) \
59         struct { T *succ; T *pred; }
60
61 /*! Template for a node in a list of \a T structures */
62 #define DECLARE_NODE_ANON(T) \
63         T *succ; T *pred;
64
65 /*!
66  * Iterate over all nodes in a list. This statement defines a for cicle
67  * accepting the following parameters:
68  * \param n   node pointer to be used in each iteration
69  * \param l   pointer to list
70  */
71 #define FOREACHNODE(n,l) \
72         for( \
73                 (n) = (typeof(n))((l)->head); \
74                 ((Node *)(n))->succ; \
75                 (n) = (typeof(n))(((Node *)(n))->succ) \
76         )
77
78 /*! Initialize a list */
79 #define INITLIST(l) \
80         do { \
81                 (l)->head = (Node *)(&(l)->null); \
82                 (l)->null = NULL; \
83                 (l)->tail = (Node *)(&(l)->head); \
84         } while (0)
85
86 /*! Add node to list head */
87 #define ADDHEAD(l,n) \
88         do { \
89                 (n)->succ = (l)->head; \
90                 (n)->pred = (Node *)&(l)->head; \
91                 (n)->succ->pred = (n); \
92                 (l)->head = (n); \
93         } while (0)
94
95 /*! Add node to list tail */
96 #define ADDTAIL(l,n) \
97         do { \
98                 (n)->succ = (Node *)(&(l)->null); \
99                 (n)->pred = (l)->tail; \
100                 (n)->pred->succ = (n); \
101                 (l)->tail = (n); \
102         } while (0)
103
104 /*!
105  * Insert node \a n before node \a ln
106  * Note: you can't pass in a list header as \a ln, but
107  * it is safe to pass list-\>head of an empty list.
108  */
109 #define INSERTBEFORE(n,ln) \
110         do { \
111                 (n)->succ = (ln); \
112                 (n)->pred = (ln)->pred; \
113                 (ln)->pred->succ = (n); \
114                 (ln)->pred = (n); \
115         } while (0)
116
117 /*! Remove \a n from whatever list it is in */
118 #define REMOVE(n) \
119         do { \
120                 (n)->pred->succ = (n)->succ; \
121                 (n)->succ->pred = (n)->pred; \
122         } while (0)
123
124 /*! Tell whether a list is empty */
125 #define ISLISTEMPTY(l)  ( (l)->head == (Node *)(&(l)->null) )
126
127 /*!
128  * Unlink a node from the head of the list \a l.
129  * \return Pointer to node, or NULL if the list was empty.
130  */
131 INLINE Node *REMHEAD(List *l)
132 {
133         Node *n;
134
135         if (ISLISTEMPTY(l))
136                 return (Node *)0;
137
138         n = l->head;
139         l->head = n->succ;
140         n->succ->pred = (Node *)l;
141         return n;
142 }
143
144 /*!
145  * Unlink a node from the tail of the list \a l.
146  * \return Pointer to node, or NULL if the list was empty.
147  */
148 INLINE Node *REMTAIL(List *l)
149 {
150         Node *n;
151
152         if (ISLISTEMPTY(l))
153                 return (Node *)0;
154
155         n = l->tail;
156         l->tail = n->pred;
157         n->pred->succ = (Node *)&l->null;
158         return n;
159 }
160
161 #endif /* MWARE_LIST_H */