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