]> git.ozlabs.org Git - ccan/blob - ccan/list/list.h
Make diag() go to stdout: everything else does.
[ccan] / ccan / list / list.h
1 #ifndef CCAN_LIST_H
2 #define CCAN_LIST_H
3 #include <stdbool.h>
4 #include <ccan/container_of/container_of.h>
5
6 /**
7  * struct list_node - an entry in a doubly-linked list
8  * @next: next entry (self if empty)
9  * @prev: previous entry (self if empty)
10  *
11  * This is used as an entry in a linked list.
12  * Example:
13  *      struct child {
14  *              const char *name;
15  *              // Linked list of all us children.
16  *              struct list_node list;
17  *      };
18  */
19 struct list_node
20 {
21         struct list_node *next, *prev;
22 };
23
24 /**
25  * struct list_head - the head of a doubly-linked list
26  * @h: the list_head (containing next and prev pointers)
27  *
28  * This is used as the head of a linked list.
29  * Example:
30  *      struct parent {
31  *              const char *name;
32  *              struct list_head children;
33  *              unsigned int num_children;
34  *      };
35  */
36 struct list_head
37 {
38         struct list_node n;
39 };
40
41 /**
42  * list_check - check a list for consistency
43  * @h: the list_head
44  * @abortstr: the location to print on aborting, or NULL.
45  *
46  * Because list_nodes have redundant information, consistency checking between
47  * the back and forward links can be done.  This is useful as a debugging check.
48  * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
49  * is inconsistent, and the function will abort.
50  *
51  * Returns the list head if the list is consistent, NULL if not (it
52  * can never return NULL if @abortstr is set).
53  *
54  * Example:
55  *      static void dump_parent(struct parent *p)
56  *      {
57  *              struct child *c;
58  *
59  *              printf("%s (%u children):\n", p->name, parent->num_children);
60  *              list_check(&p->children, "bad child list");
61  *              list_for_each(&p->children, c, list)
62  *                      printf(" -> %s\n", c->name);
63  *      }
64  */
65 struct list_head *list_check(struct list_head *h, const char *abortstr);
66
67 #ifdef CCAN_LIST_DEBUG
68 #define debug_list(h) list_check((h), __func__)
69 #else
70 #define debug_list(h) (h)
71 #endif
72
73 /**
74  * list_head_init - initialize a list_head
75  * @h: the list_head to set to the empty list
76  *
77  * Example:
78  *      list_head_init(&parent->children);
79  *      parent->num_children = 0;
80  */
81 static inline void list_head_init(struct list_head *h)
82 {
83         h->n.next = h->n.prev = &h->n;
84 }
85
86 /**
87  * LIST_HEAD - define and initalized empty list_head
88  * @name: the name of the list.
89  *
90  * The LIST_HEAD macro defines a list_head and initializes it to an empty
91  * list.  It can be prepended by "static" to define a static list_head.
92  *
93  * Example:
94  *      // Header:
95  *      extern struct list_head my_list;
96  *
97  *      // C file:
98  *      LIST_HEAD(my_list);
99  */
100 #define LIST_HEAD(name) \
101         struct list_head name = { { &name.n, &name.n } }
102
103 /**
104  * list_add - add an entry at the start of a linked list.
105  * @h: the list_head to add the node to
106  * @n: the list_node to add to the list.
107  *
108  * The list_node does not need to be initialized; it will be overwritten.
109  * Example:
110  *      list_add(&parent->children, &child->list);
111  *      parent->num_children++;
112  */
113 static inline void list_add(struct list_head *h, struct list_node *n)
114 {
115         n->next = h->n.next;
116         n->prev = &h->n;
117         h->n.next->prev = n;
118         h->n.next = n;
119         (void)debug_list(h);
120 }
121
122 /**
123  * list_add_tail - add an entry at the end of a linked list.
124  * @h: the list_head to add the node to
125  * @n: the list_node to add to the list.
126  *
127  * The list_node does not need to be initialized; it will be overwritten.
128  * Example:
129  *      list_add_tail(&parent->children, &child->list);
130  *      parent->num_children++;
131  */
132 static inline void list_add_tail(struct list_head *h, struct list_node *n)
133 {
134         n->next = &h->n;
135         n->prev = h->n.prev;
136         h->n.prev->next = n;
137         h->n.prev = n;
138         (void)debug_list(h);
139 }
140
141 /**
142  * list_del - delete an entry from a linked list.
143  * @n: the list_node to delete from the list.
144  *
145  * Example:
146  *      list_del(&child->list);
147  *      parent->num_children--;
148  */
149 static inline void list_del(struct list_node *n)
150 {
151         n->next->prev = n->prev;
152         n->prev->next = n->next;
153         (void)debug_list(n->next);
154 #ifdef CCAN_LIST_DEBUG
155         /* Catch use-after-del. */
156         n->next = n->prev = NULL;
157 #endif
158 }
159
160 /**
161  * list_empty - is a list empty?
162  * @h: the list_head
163  *
164  * If the list is empty, returns true.
165  *
166  * Example:
167  *      assert(list_empty(&parent->children) == (parent->num_children == 0));
168  */
169 static inline bool list_empty(struct list_head *h)
170 {
171         (void)debug_list(h);
172         return h->n.next == &h->n;
173 }
174
175 /**
176  * list_entry - convert a list_node back into the structure containing it.
177  * @n: the list_node
178  * @type: the type of the entry
179  * @member: the list_node member of the type
180  *
181  * Example:
182  *      struct child *c;
183  *      // First list entry is children.next; convert back to child.
184  *      c = list_entry(parent->children.next, struct child, list);
185  */
186 #define list_entry(n, type, member) container_of(n, type, member)
187
188 /**
189  * list_top - get the first entry in a list
190  * @h: the list_head
191  * @type: the type of the entry
192  * @member: the list_node member of the type
193  *
194  * If the list is empty, returns NULL.
195  *
196  * Example:
197  *      struct child *first;
198  *      first = list_top(&parent->children, struct child, list);
199  */
200 #define list_top(h, type, member) \
201         (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
202
203 /**
204  * list_tail - get the last entry in a list
205  * @h: the list_head
206  * @type: the type of the entry
207  * @member: the list_node member of the type
208  *
209  * If the list is empty, returns NULL.
210  *
211  * Example:
212  *      struct child *last;
213  *      last = list_tail(&parent->children, struct child, list);
214  */
215 #define list_tail(h, type, member) \
216         (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
217
218 /**
219  * list_for_each - iterate through a list.
220  * @h: the list_head
221  * @i: the structure containing the list_node
222  * @member: the list_node member of the structure
223  *
224  * This is a convenient wrapper to iterate @i over the entire list.  It's
225  * a for loop, so you can break and continue as normal.
226  *
227  * Example:
228  *      struct child *c;
229  *      list_for_each(&parent->children, c, list)
230  *              printf("Name: %s\n", c->name);
231  */
232 #define list_for_each(h, i, member)                                     \
233         for (i = container_of_var(debug_list(h)->n.next, i, member);    \
234              &i->member != &(h)->n;                                     \
235              i = container_of_var(i->member.next, i, member))
236
237 /**
238  * list_for_each_safe - iterate through a list, maybe during deletion
239  * @h: the list_head
240  * @i: the structure containing the list_node
241  * @nxt: the structure containing the list_node
242  * @member: the list_node member of the structure
243  *
244  * This is a convenient wrapper to iterate @i over the entire list.  It's
245  * a for loop, so you can break and continue as normal.  The extra variable
246  * @nxt is used to hold the next element, so you can delete @i from the list.
247  *
248  * Example:
249  *      struct child *c, *n;
250  *      list_for_each_safe(&parent->children, c, n, list) {
251  *              list_del(&c->list);
252  *              parent->num_children--;
253  *      }
254  */
255 #define list_for_each_safe(h, i, nxt, member)                           \
256         for (i = container_of_var(debug_list(h)->n.next, i, member),    \
257                 nxt = container_of_var(i->member.next, i, member);      \
258              &i->member != &(h)->n;                                     \
259              i = nxt, nxt = container_of_var(i->member.next, i, member))
260 #endif /* CCAN_LIST_H */