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