Import mware modules.
[bertos.git] / mware / list.h
1 /*!
2  * \file
3  * Copyright (C) 2001 Bernardo Innocenti <bernie@develer.com>
4  * Copyright (C) 2003 Develer S.r.l. (http://www.develer.com/)
5  * All Rights Reserved.
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.1  2004/05/23 15:43:16  bernie
17  * Import mware modules.
18  *
19  */
20 #ifndef MWARE_LIST_H
21 #define MWARE_LIST_H
22
23 typedef struct _Node
24 {
25         struct _Node *succ;
26         struct _Node *pred;
27 } Node;
28
29 typedef struct _List
30 {
31         Node *head;
32         Node *null;
33         Node *tail;
34 } List;
35
36
37 /*! Template for a list of \a T  structures */
38 #define DECLARE_LIST(T) \
39         struct { T *head; T *null; T *tail; }
40
41 /*! Template for a node in a list of \a T structures */
42 #define DECLARE_NODE(T) \
43         struct { T *succ; T *pred; }
44
45 /*! Template for a node in a list of \a T structures */
46 #define DECLARE_NODE_ANON(T) \
47         T *succ; T *pred;
48
49 /*!
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
54  */
55 #define FOREACHNODE(n,l) \
56         for( \
57                 (n) = (typeof(n))((l)->head); \
58                 ((Node *)(n))->succ; \
59                 (n) = (typeof(n))(((Node *)(n))->succ) \
60         )
61
62 /*! Initialize a list */
63 #define INITLIST(l) \
64         do { \
65                 (l)->head = (Node *)(&(l)->null); \
66                 (l)->null = NULL; \
67                 (l)->tail = (Node *)(&(l)->head); \
68         } while (0)
69
70 /*! Add node to list head */
71 #define ADDHEAD(l,n) \
72         do { \
73                 (n)->succ = (l)->head; \
74                 (n)->pred = (Node *)&(l)->head; \
75                 (n)->succ->pred = (n); \
76                 (l)->head = (n); \
77         } while (0)
78
79 /*! Add node to list tail */
80 #define ADDTAIL(l,n) \
81         do { \
82                 (n)->succ = (Node *)(&(l)->null); \
83                 (n)->pred = (l)->tail; \
84                 (n)->pred->succ = (n); \
85                 (l)->tail = (n); \
86         } while (0)
87
88 /*!
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.
92  */
93 #define INSERTBEFORE(n,ln) \
94         do { \
95                 (n)->succ = (ln); \
96                 (n)->pred = (ln)->pred; \
97                 (ln)->pred->succ = (n); \
98                 (ln)->pred = (n); \
99         } while (0)
100
101 /*! Remove \a n from whatever list it is in */
102 #define REMOVE(n) \
103         do { \
104                 (n)->pred->succ = (n)->succ; \
105                 (n)->succ->pred = (n)->pred; \
106         } while (0)
107
108 /*! Tell whether a list is empty */
109 #define ISLISTEMPTY(l)  ( (l)->head == (Node *)(&(l)->null) )
110
111 /*! \name Extract an element from the head/tail of the list. If the list empty, return NULL. */
112 /*\{*/
113 #define REMHEAD(l) _list_rem_head(l)
114 #define REMTAIL(l) _list_rem_tail(l)
115 /*\}*/
116
117 /* Prototypes for out-of-line functions */
118 Node *_list_rem_head(List *l);
119 Node *_list_rem_tail(List *l);
120
121
122 #endif /* MWARE_LIST_H */