]> git.ozlabs.org Git - ccan/blob - ccan/list/list.h
list: list_del_from()
[ccan] / ccan / list / list.h
1 #ifndef CCAN_LIST_H
2 #define CCAN_LIST_H
3 #include <stdbool.h>
4 #include <assert.h>
5 #include <ccan/container_of/container_of.h>
6
7 /**
8  * struct list_node - an entry in a doubly-linked list
9  * @next: next entry (self if empty)
10  * @prev: previous entry (self if empty)
11  *
12  * This is used as an entry in a linked list.
13  * Example:
14  *      struct child {
15  *              const char *name;
16  *              // Linked list of all us children.
17  *              struct list_node list;
18  *      };
19  */
20 struct list_node
21 {
22         struct list_node *next, *prev;
23 };
24
25 /**
26  * struct list_head - the head of a doubly-linked list
27  * @h: the list_head (containing next and prev pointers)
28  *
29  * This is used as the head of a linked list.
30  * Example:
31  *      struct parent {
32  *              const char *name;
33  *              struct list_head children;
34  *              unsigned int num_children;
35  *      };
36  */
37 struct list_head
38 {
39         struct list_node n;
40 };
41
42 /**
43  * list_check - check head of a list for consistency
44  * @h: the list_head
45  * @abortstr: the location to print on aborting, or NULL.
46  *
47  * Because list_nodes have redundant information, consistency checking between
48  * the back and forward links can be done.  This is useful as a debugging check.
49  * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
50  * is inconsistent, and the function will abort.
51  *
52  * Returns the list head if the list is consistent, NULL if not (it
53  * can never return NULL if @abortstr is set).
54  *
55  * See also: list_check_node()
56  *
57  * Example:
58  *      static void dump_parent(struct parent *p)
59  *      {
60  *              struct child *c;
61  *
62  *              printf("%s (%u children):\n", p->name, p->num_children);
63  *              list_check(&p->children, "bad child list");
64  *              list_for_each(&p->children, c, list)
65  *                      printf(" -> %s\n", c->name);
66  *      }
67  */
68 struct list_head *list_check(const struct list_head *h, const char *abortstr);
69
70 /**
71  * list_check_node - check node of a list for consistency
72  * @n: the list_node
73  * @abortstr: the location to print on aborting, or NULL.
74  *
75  * Check consistency of the list node is in (it must be in one).
76  *
77  * See also: list_check()
78  *
79  * Example:
80  *      static void dump_child(const struct child *c)
81  *      {
82  *              list_check_node(&c->list, "bad child list");
83  *              printf("%s\n", c->name);
84  *      }
85  */
86 struct list_node *list_check_node(const struct list_node *n,
87                                   const char *abortstr);
88
89 #ifdef CCAN_LIST_DEBUG
90 #define list_debug(h) list_check((h), __func__)
91 #define list_debug_node(n) list_check_node((n), __func__)
92 #else
93 #define list_debug(h) (h)
94 #define list_debug_node(n) (n)
95 #endif
96
97 /**
98  * LIST_HEAD - define and initalize an empty list_head
99  * @name: the name of the list.
100  *
101  * The LIST_HEAD macro defines a list_head and initializes it to an empty
102  * list.  It can be prepended by "static" to define a static list_head.
103  *
104  * Example:
105  *      static LIST_HEAD(my_global_list);
106  */
107 #define LIST_HEAD(name) \
108         struct list_head name = { { &name.n, &name.n } }
109
110 /**
111  * list_head_init - initialize a list_head
112  * @h: the list_head to set to the empty list
113  *
114  * Example:
115  *      ...
116  *      struct parent *parent = malloc(sizeof(*parent));
117  *
118  *      list_head_init(&parent->children);
119  *      parent->num_children = 0;
120  */
121 static inline void list_head_init(struct list_head *h)
122 {
123         h->n.next = h->n.prev = &h->n;
124 }
125
126 /**
127  * list_add - add an entry at the start of a linked list.
128  * @h: the list_head to add the node to
129  * @n: the list_node to add to the list.
130  *
131  * The list_node does not need to be initialized; it will be overwritten.
132  * Example:
133  *      struct child *child = malloc(sizeof(*child));
134  *
135  *      child->name = "marvin";
136  *      list_add(&parent->children, &child->list);
137  *      parent->num_children++;
138  */
139 static inline void list_add(struct list_head *h, struct list_node *n)
140 {
141         n->next = h->n.next;
142         n->prev = &h->n;
143         h->n.next->prev = n;
144         h->n.next = n;
145         (void)list_debug(h);
146 }
147
148 /**
149  * list_add_tail - add an entry at the end of a linked list.
150  * @h: the list_head to add the node to
151  * @n: the list_node to add to the list.
152  *
153  * The list_node does not need to be initialized; it will be overwritten.
154  * Example:
155  *      list_add_tail(&parent->children, &child->list);
156  *      parent->num_children++;
157  */
158 static inline void list_add_tail(struct list_head *h, struct list_node *n)
159 {
160         n->next = &h->n;
161         n->prev = h->n.prev;
162         h->n.prev->next = n;
163         h->n.prev = n;
164         (void)list_debug(h);
165 }
166
167 /**
168  * list_empty - is a list empty?
169  * @h: the list_head
170  *
171  * If the list is empty, returns true.
172  *
173  * Example:
174  *      assert(list_empty(&parent->children) == (parent->num_children == 0));
175  */
176 static inline bool list_empty(const struct list_head *h)
177 {
178         (void)list_debug(h);
179         return h->n.next == &h->n;
180 }
181
182 /**
183  * list_del - delete an entry from an (unknown) linked list.
184  * @n: the list_node to delete from the list.
185  *
186  * Note that this leaves @n in an undefined state; it can be added to
187  * another list, but not deleted again.
188  *
189  * See also:
190  *      list_del_from()
191  *
192  * Example:
193  *      list_del(&child->list);
194  *      parent->num_children--;
195  */
196 static inline void list_del(struct list_node *n)
197 {
198         (void)list_debug_node(n);
199         n->next->prev = n->prev;
200         n->prev->next = n->next;
201 #ifdef CCAN_LIST_DEBUG
202         /* Catch use-after-del. */
203         n->next = n->prev = NULL;
204 #endif
205 }
206
207 /**
208  * list_del_from - delete an entry from a known linked list.
209  * @h: the list_head the node is in.
210  * @n: the list_node to delete from the list.
211  *
212  * This explicitly indicates which list a node is expected to be in,
213  * which is better documentation and can catch more bugs.
214  *
215  * See also: list_del()
216  *
217  * Example:
218  *      list_del_from(&parent->children, &child->list);
219  *      parent->num_children--;
220  */
221 static inline void list_del_from(struct list_head *h, struct list_node *n)
222 {
223 #ifdef CCAN_LIST_DEBUG
224         {
225                 /* Thorough check: make sure it was in list! */
226                 struct list_node *i;
227                 for (i = h->n.next; i != n; i = i->next)
228                         assert(i != &h->n);
229         }
230 #endif /* CCAN_LIST_DEBUG */
231
232         /* Quick test that catches a surprising number of bugs. */
233         assert(!list_empty(h));
234         list_del(n);
235 }
236
237 /**
238  * list_entry - convert a list_node back into the structure containing it.
239  * @n: the list_node
240  * @type: the type of the entry
241  * @member: the list_node member of the type
242  *
243  * Example:
244  *      // First list entry is children.next; convert back to child.
245  *      child = list_entry(parent->children.n.next, struct child, list);
246  *
247  * See Also:
248  *      list_top(), list_for_each()
249  */
250 #define list_entry(n, type, member) container_of(n, type, member)
251
252 /**
253  * list_top - get the first entry in a list
254  * @h: the list_head
255  * @type: the type of the entry
256  * @member: the list_node member of the type
257  *
258  * If the list is empty, returns NULL.
259  *
260  * Example:
261  *      struct child *first;
262  *      first = list_top(&parent->children, struct child, list);
263  */
264 #define list_top(h, type, member) \
265         (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
266
267 /**
268  * list_tail - get the last entry in a list
269  * @h: the list_head
270  * @type: the type of the entry
271  * @member: the list_node member of the type
272  *
273  * If the list is empty, returns NULL.
274  *
275  * Example:
276  *      struct child *last;
277  *      last = list_tail(&parent->children, struct child, list);
278  */
279 #define list_tail(h, type, member) \
280         (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
281
282 /**
283  * list_for_each - iterate through a list.
284  * @h: the list_head
285  * @i: the structure containing the list_node
286  * @member: the list_node member of the structure
287  *
288  * This is a convenient wrapper to iterate @i over the entire list.  It's
289  * a for loop, so you can break and continue as normal.
290  *
291  * Example:
292  *      list_for_each(&parent->children, child, list)
293  *              printf("Name: %s\n", child->name);
294  */
295 #define list_for_each(h, i, member)                                     \
296         for (i = container_of_var(list_debug(h)->n.next, i, member);    \
297              &i->member != &(h)->n;                                     \
298              i = container_of_var(i->member.next, i, member))
299
300 /**
301  * list_for_each_safe - iterate through a list, maybe during deletion
302  * @h: the list_head
303  * @i: the structure containing the list_node
304  * @nxt: the structure containing the list_node
305  * @member: the list_node member of the structure
306  *
307  * This is a convenient wrapper to iterate @i over the entire list.  It's
308  * a for loop, so you can break and continue as normal.  The extra variable
309  * @nxt is used to hold the next element, so you can delete @i from the list.
310  *
311  * Example:
312  *      struct child *next;
313  *      list_for_each_safe(&parent->children, child, next, list) {
314  *              list_del(&child->list);
315  *              parent->num_children--;
316  *      }
317  */
318 #define list_for_each_safe(h, i, nxt, member)                           \
319         for (i = container_of_var(list_debug(h)->n.next, i, member),    \
320                 nxt = container_of_var(i->member.next, i, member);      \
321              &i->member != &(h)->n;                                     \
322              i = nxt, nxt = container_of_var(i->member.next, i, member))
323 #endif /* CCAN_LIST_H */