]> git.ozlabs.org Git - ccan/blob - ccan/list/list.h
03614873ef6a718aaaadec32a27356f82c6c7912
[ccan] / ccan / list / list.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_LIST_H
3 #define CCAN_LIST_H
4 #include <stdbool.h>
5 #include <assert.h>
6 #include <ccan/container_of/container_of.h>
7 #include <ccan/check_type/check_type.h>
8
9 /**
10  * struct list_node - an entry in a doubly-linked list
11  * @next: next entry (self if empty)
12  * @prev: previous entry (self if empty)
13  *
14  * This is used as an entry in a linked list.
15  * Example:
16  *      struct child {
17  *              const char *name;
18  *              // Linked list of all us children.
19  *              struct list_node list;
20  *      };
21  */
22 struct list_node
23 {
24         struct list_node *next, *prev;
25 };
26
27 /**
28  * struct list_head - the head of a doubly-linked list
29  * @h: the list_head (containing next and prev pointers)
30  *
31  * This is used as the head of a linked list.
32  * Example:
33  *      struct parent {
34  *              const char *name;
35  *              struct list_head children;
36  *              unsigned int num_children;
37  *      };
38  */
39 struct list_head
40 {
41         struct list_node n;
42 };
43
44 /**
45  * list_check - check head of a list for consistency
46  * @h: the list_head
47  * @abortstr: the location to print on aborting, or NULL.
48  *
49  * Because list_nodes have redundant information, consistency checking between
50  * the back and forward links can be done.  This is useful as a debugging check.
51  * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
52  * is inconsistent, and the function will abort.
53  *
54  * Returns the list head if the list is consistent, NULL if not (it
55  * can never return NULL if @abortstr is set).
56  *
57  * See also: list_check_node()
58  *
59  * Example:
60  *      static void dump_parent(struct parent *p)
61  *      {
62  *              struct child *c;
63  *
64  *              printf("%s (%u children):\n", p->name, p->num_children);
65  *              list_check(&p->children, "bad child list");
66  *              list_for_each(&p->children, c, list)
67  *                      printf(" -> %s\n", c->name);
68  *      }
69  */
70 struct list_head *list_check(const struct list_head *h, const char *abortstr);
71
72 /**
73  * list_check_node - check node of a list for consistency
74  * @n: the list_node
75  * @abortstr: the location to print on aborting, or NULL.
76  *
77  * Check consistency of the list node is in (it must be in one).
78  *
79  * See also: list_check()
80  *
81  * Example:
82  *      static void dump_child(const struct child *c)
83  *      {
84  *              list_check_node(&c->list, "bad child list");
85  *              printf("%s\n", c->name);
86  *      }
87  */
88 struct list_node *list_check_node(const struct list_node *n,
89                                   const char *abortstr);
90
91 #ifdef CCAN_LIST_DEBUG
92 #define list_debug(h) list_check((h), __func__)
93 #define list_debug_node(n) list_check_node((n), __func__)
94 #else
95 #define list_debug(h) (h)
96 #define list_debug_node(n) (n)
97 #endif
98
99 /**
100  * LIST_HEAD_INIT - initializer for an empty list_head
101  * @name: the name of the list.
102  *
103  * Explicit initializer for an empty list.
104  *
105  * See also:
106  *      LIST_HEAD, list_head_init()
107  *
108  * Example:
109  *      static struct list_head my_list = LIST_HEAD_INIT(my_list);
110  */
111 #define LIST_HEAD_INIT(name) { { &name.n, &name.n } }
112
113 /**
114  * LIST_HEAD - define and initialize an empty list_head
115  * @name: the name of the list.
116  *
117  * The LIST_HEAD macro defines a list_head and initializes it to an empty
118  * list.  It can be prepended by "static" to define a static list_head.
119  *
120  * See also:
121  *      LIST_HEAD_INIT, list_head_init()
122  *
123  * Example:
124  *      static LIST_HEAD(my_global_list);
125  */
126 #define LIST_HEAD(name) \
127         struct list_head name = LIST_HEAD_INIT(name)
128
129 /**
130  * list_head_init - initialize a list_head
131  * @h: the list_head to set to the empty list
132  *
133  * Example:
134  *      ...
135  *      struct parent *parent = malloc(sizeof(*parent));
136  *
137  *      list_head_init(&parent->children);
138  *      parent->num_children = 0;
139  */
140 static inline void list_head_init(struct list_head *h)
141 {
142         h->n.next = h->n.prev = &h->n;
143 }
144
145 /**
146  * list_add - add an entry at the start of a linked list.
147  * @h: the list_head to add the node to
148  * @n: the list_node to add to the list.
149  *
150  * The list_node does not need to be initialized; it will be overwritten.
151  * Example:
152  *      struct child *child = malloc(sizeof(*child));
153  *
154  *      child->name = "marvin";
155  *      list_add(&parent->children, &child->list);
156  *      parent->num_children++;
157  */
158 static inline void list_add(struct list_head *h, struct list_node *n)
159 {
160         n->next = h->n.next;
161         n->prev = &h->n;
162         h->n.next->prev = n;
163         h->n.next = n;
164         (void)list_debug(h);
165 }
166
167 /**
168  * list_add_tail - add an entry at the end of a linked list.
169  * @h: the list_head to add the node to
170  * @n: the list_node to add to the list.
171  *
172  * The list_node does not need to be initialized; it will be overwritten.
173  * Example:
174  *      list_add_tail(&parent->children, &child->list);
175  *      parent->num_children++;
176  */
177 static inline void list_add_tail(struct list_head *h, struct list_node *n)
178 {
179         n->next = &h->n;
180         n->prev = h->n.prev;
181         h->n.prev->next = n;
182         h->n.prev = n;
183         (void)list_debug(h);
184 }
185
186 /**
187  * list_empty - is a list empty?
188  * @h: the list_head
189  *
190  * If the list is empty, returns true.
191  *
192  * Example:
193  *      assert(list_empty(&parent->children) == (parent->num_children == 0));
194  */
195 static inline bool list_empty(const struct list_head *h)
196 {
197         (void)list_debug(h);
198         return h->n.next == &h->n;
199 }
200
201 /**
202  * list_del - delete an entry from an (unknown) linked list.
203  * @n: the list_node to delete from the list.
204  *
205  * Note that this leaves @n in an undefined state; it can be added to
206  * another list, but not deleted again.
207  *
208  * See also:
209  *      list_del_from()
210  *
211  * Example:
212  *      list_del(&child->list);
213  *      parent->num_children--;
214  */
215 static inline void list_del(struct list_node *n)
216 {
217         (void)list_debug_node(n);
218         n->next->prev = n->prev;
219         n->prev->next = n->next;
220 #ifdef CCAN_LIST_DEBUG
221         /* Catch use-after-del. */
222         n->next = n->prev = NULL;
223 #endif
224 }
225
226 /**
227  * list_del_from - delete an entry from a known linked list.
228  * @h: the list_head the node is in.
229  * @n: the list_node to delete from the list.
230  *
231  * This explicitly indicates which list a node is expected to be in,
232  * which is better documentation and can catch more bugs.
233  *
234  * See also: list_del()
235  *
236  * Example:
237  *      list_del_from(&parent->children, &child->list);
238  *      parent->num_children--;
239  */
240 static inline void list_del_from(struct list_head *h, struct list_node *n)
241 {
242 #ifdef CCAN_LIST_DEBUG
243         {
244                 /* Thorough check: make sure it was in list! */
245                 struct list_node *i;
246                 for (i = h->n.next; i != n; i = i->next)
247                         assert(i != &h->n);
248         }
249 #endif /* CCAN_LIST_DEBUG */
250
251         /* Quick test that catches a surprising number of bugs. */
252         assert(!list_empty(h));
253         list_del(n);
254 }
255
256 /**
257  * list_entry - convert a list_node back into the structure containing it.
258  * @n: the list_node
259  * @type: the type of the entry
260  * @member: the list_node member of the type
261  *
262  * Example:
263  *      // First list entry is children.next; convert back to child.
264  *      child = list_entry(parent->children.n.next, struct child, list);
265  *
266  * See Also:
267  *      list_top(), list_for_each()
268  */
269 #define list_entry(n, type, member) container_of(n, type, member)
270
271 /**
272  * list_top - get the first entry in a list
273  * @h: the list_head
274  * @type: the type of the entry
275  * @member: the list_node member of the type
276  *
277  * If the list is empty, returns NULL.
278  *
279  * Example:
280  *      struct child *first;
281  *      first = list_top(&parent->children, struct child, list);
282  *      if (!first)
283  *              printf("Empty list!\n");
284  */
285 #define list_top(h, type, member)                                       \
286         ((type *)list_top_((h), list_off_(type, member)))
287
288 static inline const void *list_top_(const struct list_head *h, size_t off)
289 {
290         if (list_empty(h))
291                 return NULL;
292         return (const char *)h->n.next - off;
293 }
294
295 /**
296  * list_pop - remove the first entry in a list
297  * @h: the list_head
298  * @type: the type of the entry
299  * @member: the list_node member of the type
300  *
301  * If the list is empty, returns NULL.
302  *
303  * Example:
304  *      struct child *one;
305  *      one = list_pop(&parent->children, struct child, list);
306  *      if (!one)
307  *              printf("Empty list!\n");
308  */
309 #define list_pop(h, type, member)                                       \
310         ((type *)list_pop_((h), list_off_(type, member)))
311
312 static inline const void *list_pop_(const struct list_head *h, size_t off)
313 {
314         struct list_node *n;
315
316         if (list_empty(h))
317                 return NULL;
318         n = h->n.next;
319         list_del(n);
320         return (const char *)n - off;
321 }
322
323 /**
324  * list_tail - get the last entry in a list
325  * @h: the list_head
326  * @type: the type of the entry
327  * @member: the list_node member of the type
328  *
329  * If the list is empty, returns NULL.
330  *
331  * Example:
332  *      struct child *last;
333  *      last = list_tail(&parent->children, struct child, list);
334  *      if (!last)
335  *              printf("Empty list!\n");
336  */
337 #define list_tail(h, type, member) \
338         ((type *)list_tail_((h), list_off_(type, member)))
339
340 static inline const void *list_tail_(const struct list_head *h, size_t off)
341 {
342         if (list_empty(h))
343                 return NULL;
344         return (const char *)h->n.prev - off;
345 }
346
347 /**
348  * list_for_each - iterate through a list.
349  * @h: the list_head (warning: evaluated multiple times!)
350  * @i: the structure containing the list_node
351  * @member: the list_node member of the structure
352  *
353  * This is a convenient wrapper to iterate @i over the entire list.  It's
354  * a for loop, so you can break and continue as normal.
355  *
356  * Example:
357  *      list_for_each(&parent->children, child, list)
358  *              printf("Name: %s\n", child->name);
359  */
360 #define list_for_each(h, i, member)                                     \
361         list_for_each_off(h, i, list_off_var_(i, member))
362
363 /**
364  * list_for_each_rev - iterate through a list backwards.
365  * @h: the list_head
366  * @i: the structure containing the list_node
367  * @member: the list_node member of the structure
368  *
369  * This is a convenient wrapper to iterate @i over the entire list.  It's
370  * a for loop, so you can break and continue as normal.
371  *
372  * Example:
373  *      list_for_each_rev(&parent->children, child, list)
374  *              printf("Name: %s\n", child->name);
375  */
376 #define list_for_each_rev(h, i, member)                                 \
377         for (i = container_of_var(list_debug(h)->n.prev, i, member);    \
378              &i->member != &(h)->n;                                     \
379              i = container_of_var(i->member.prev, i, member))
380
381 /**
382  * list_for_each_safe - iterate through a list, maybe during deletion
383  * @h: the list_head
384  * @i: the structure containing the list_node
385  * @nxt: the structure containing the list_node
386  * @member: the list_node member of the structure
387  *
388  * This is a convenient wrapper to iterate @i over the entire list.  It's
389  * a for loop, so you can break and continue as normal.  The extra variable
390  * @nxt is used to hold the next element, so you can delete @i from the list.
391  *
392  * Example:
393  *      struct child *next;
394  *      list_for_each_safe(&parent->children, child, next, list) {
395  *              list_del(&child->list);
396  *              parent->num_children--;
397  *      }
398  */
399 #define list_for_each_safe(h, i, nxt, member)                           \
400         list_for_each_safe_off(h, i, nxt, list_off_var_(i, member))
401
402 /**
403  * list_next - get the next entry in a list
404  * @h: the list_head
405  * @i: a pointer to an entry in the list.
406  * @member: the list_node member of the structure
407  *
408  * If @i was the last entry in the list, returns NULL.
409  *
410  * Example:
411  *      struct child *second;
412  *      second = list_next(&parent->children, first, list);
413  *      if (!second)
414  *              printf("No second child!\n");
415  */
416 #define list_next(h, i, member)                                         \
417         ((list_typeof(i))list_entry_or_null(list_debug(h),              \
418                                             (i)->member.next,           \
419                                             list_off_var_((i), member)))
420
421 /**
422  * list_prev - get the previous entry in a list
423  * @h: the list_head
424  * @i: a pointer to an entry in the list.
425  * @member: the list_node member of the structure
426  *
427  * If @i was the first entry in the list, returns NULL.
428  *
429  * Example:
430  *      first = list_prev(&parent->children, second, list);
431  *      if (!first)
432  *              printf("Can't go back to first child?!\n");
433  */
434 #define list_prev(h, i, member)                                         \
435         ((list_typeof(i))list_entry_or_null(list_debug(h),              \
436                                             (i)->member.prev,           \
437                                             list_off_var_((i), member)))
438
439 /**
440  * list_append_list - empty one list onto the end of another.
441  * @to: the list to append into
442  * @from: the list to empty.
443  *
444  * This takes the entire contents of @from and moves it to the end of
445  * @to.  After this @from will be empty.
446  *
447  * Example:
448  *      struct list_head adopter;
449  *
450  *      list_append_list(&adopter, &parent->children);
451  *      assert(list_empty(&parent->children));
452  *      parent->num_children = 0;
453  */
454 static inline void list_append_list(struct list_head *to,
455                                     struct list_head *from)
456 {
457         struct list_node *from_tail = list_debug(from)->n.prev;
458         struct list_node *to_tail = list_debug(to)->n.prev;
459
460         /* Sew in head and entire list. */
461         to->n.prev = from_tail;
462         from_tail->next = &to->n;
463         to_tail->next = &from->n;
464         from->n.prev = to_tail;
465
466         /* Now remove head. */
467         list_del(&from->n);
468         list_head_init(from);
469 }
470
471 /**
472  * list_prepend_list - empty one list into the start of another.
473  * @to: the list to prepend into
474  * @from: the list to empty.
475  *
476  * This takes the entire contents of @from and moves it to the start
477  * of @to.  After this @from will be empty.
478  *
479  * Example:
480  *      list_prepend_list(&adopter, &parent->children);
481  *      assert(list_empty(&parent->children));
482  *      parent->num_children = 0;
483  */
484 static inline void list_prepend_list(struct list_head *to,
485                                      struct list_head *from)
486 {
487         struct list_node *from_tail = list_debug(from)->n.prev;
488         struct list_node *to_head = list_debug(to)->n.next;
489
490         /* Sew in head and entire list. */
491         to->n.next = &from->n;
492         from->n.prev = &to->n;
493         to_head->prev = from_tail;
494         from_tail->next = to_head;
495
496         /* Now remove head. */
497         list_del(&from->n);
498         list_head_init(from);
499 }
500
501 /**
502  * list_for_each_off - iterate through a list of memory regions.
503  * @h: the list_head
504  * @i: the pointer to a memory region wich contains list node data.
505  * @off: offset(relative to @i) at which list node data resides.
506  *
507  * This is a low-level wrapper to iterate @i over the entire list, used to
508  * implement all oher, more high-level, for-each constructs. It's a for loop,
509  * so you can break and continue as normal.
510  *
511  * WARNING! Being the low-level macro that it is, this wrapper doesn't know
512  * nor care about the type of @i. The only assumtion made is that @i points
513  * to a chunk of memory that at some @offset, relative to @i, contains a
514  * properly filled `struct node_list' which in turn contains pointers to
515  * memory chunks and it's turtles all the way down. Whith all that in mind
516  * remember that given the wrong pointer/offset couple this macro will
517  * happilly churn all you memory untill SEGFAULT stops it, in other words
518  * caveat emptor.
519  *
520  * It is worth mentioning that one of legitimate use-cases for that wrapper
521  * is operation on opaque types with known offset for `struct list_node'
522  * member(preferably 0), because it allows you not to disclose the type of
523  * @i.
524  *
525  * Example:
526  *      list_for_each_off(&parent->children, child,
527  *                              offsetof(struct child, list))
528  *              printf("Name: %s\n", child->name);
529  */
530 #define list_for_each_off(h, i, off)                                    \
531   for (i = list_node_to_off_(list_debug(h)->n.next, (off));             \
532        list_node_from_off_((void *)i, (off)) != &(h)->n;                \
533        i = list_node_to_off_(list_node_from_off_((void *)i, (off))->next, \
534                              (off)))
535
536 /**
537  * list_for_each_safe_off - iterate through a list of memory regions, maybe
538  * during deletion
539  * @h: the list_head
540  * @i: the pointer to a memory region wich contains list node data.
541  * @nxt: the structure containing the list_node
542  * @off: offset(relative to @i) at which list node data resides.
543  *
544  * For details see `list_for_each_off' and `list_for_each_safe'
545  * descriptions.
546  *
547  * Example:
548  *      list_for_each_safe_off(&parent->children, child,
549  *              next, offsetof(struct child, list))
550  *              printf("Name: %s\n", child->name);
551  */
552 #define list_for_each_safe_off(h, i, nxt, off)                          \
553   for (i = list_node_to_off_(list_debug(h)->n.next, (off)),             \
554          nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
555                                  (off));                                \
556        list_node_from_off_(i, (off)) != &(h)->n;                        \
557        i = nxt,                                                         \
558          nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
559                                  (off)))
560
561
562 /* Other -off variants. */
563 #define list_entry_off(n, type, off)            \
564         ((type *)list_node_from_off_((n), (off)))
565
566 #define list_head_off(h, type, off)             \
567         ((type *)list_head_off((h), (off)))
568
569 #define list_tail_off(h, type, off)             \
570         ((type *)list_tail_((h), (off)))
571
572 #define list_add_off(h, n, off)                 \
573         list_add((h), list_node_from_off_((n), (off)))
574
575 #define list_del_off(n, off)                    \
576         list_del(list_node_from_off_((n), (off)))
577
578 #define list_del_from_off(h, n, off)                    \
579         list_del_from(h, list_node_from_off_((n), (off)))
580
581 /* Offset helper functions so we only single-evaluate. */
582 static inline void *list_node_to_off_(struct list_node *node, size_t off)
583 {
584         return (void *)((char *)node - off);
585 }
586 static inline struct list_node *list_node_from_off_(void *ptr, size_t off)
587 {
588         return (struct list_node *)((char *)ptr + off);
589 }
590
591 /* Get the offset of the member, but make sure it's a list_node. */
592 #define list_off_(type, member)                                 \
593         (container_off(type, member) +                          \
594          check_type(((type *)0)->member, struct list_node))
595
596 #define list_off_var_(var, member)                      \
597         (container_off_var(var, member) +               \
598          check_type(var->member, struct list_node))
599
600 #if HAVE_TYPEOF
601 #define list_typeof(var) typeof(var)
602 #else
603 #define list_typeof(var) void *
604 #endif
605
606 /* Returns member, or NULL if at end of list. */
607 static inline void *list_entry_or_null(struct list_head *h,
608                                        struct list_node *n,
609                                        size_t off)
610 {
611         if (n == &h->n)
612                 return NULL;
613         return (char *)n - off;
614 }
615 #endif /* CCAN_LIST_H */