1 /* Licensed under BSD-MIT - see LICENSE file for details */
4 //#define CCAN_LIST_DEBUG 1
7 #include <ccan/str/str.h>
8 #include <ccan/container_of/container_of.h>
9 #include <ccan/check_type/check_type.h>
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)
16 * This is used as an entry in a linked list.
20 * // Linked list of all us children.
21 * struct list_node list;
26 struct list_node *next, *prev;
30 * struct list_head - the head of a doubly-linked list
31 * @h: the list_head (containing next and prev pointers)
33 * This is used as the head of a linked list.
37 * struct list_head children;
38 * unsigned int num_children;
47 * list_check - check head of a list for consistency
49 * @abortstr: the location to print on aborting, or NULL.
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.
56 * Returns the list head if the list is consistent, NULL if not (it
57 * can never return NULL if @abortstr is set).
59 * See also: list_check_node()
62 * static void dump_parent(struct parent *p)
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);
72 struct list_head *list_check(const struct list_head *h, const char *abortstr);
75 * list_check_node - check node of a list for consistency
77 * @abortstr: the location to print on aborting, or NULL.
79 * Check consistency of the list node is in (it must be in one).
81 * See also: list_check()
84 * static void dump_child(const struct child *c)
86 * list_check_node(&c->list, "bad child list");
87 * printf("%s\n", c->name);
90 struct list_node *list_check_node(const struct list_node *n,
91 const char *abortstr);
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)
98 #define list_debug(h, loc) ((void)loc, h)
99 #define list_debug_node(n, loc) ((void)loc, n)
103 * LIST_HEAD_INIT - initializer for an empty list_head
104 * @name: the name of the list.
106 * Explicit initializer for an empty list.
109 * LIST_HEAD, list_head_init()
112 * static struct list_head my_list = LIST_HEAD_INIT(my_list);
114 #define LIST_HEAD_INIT(name) { { &name.n, &name.n } }
117 * LIST_HEAD - define and initialize an empty list_head
118 * @name: the name of the list.
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.
124 * LIST_HEAD_INIT, list_head_init()
127 * static LIST_HEAD(my_global_list);
129 #define LIST_HEAD(name) \
130 struct list_head name = LIST_HEAD_INIT(name)
133 * list_head_init - initialize a list_head
134 * @h: the list_head to set to the empty list
138 * struct parent *parent = malloc(sizeof(*parent));
140 * list_head_init(&parent->children);
141 * parent->num_children = 0;
143 static inline void list_head_init(struct list_head *h)
145 h->n.next = h->n.prev = &h->n;
149 * list_node_init - initialize a list_node
150 * @n: the list_node to link to itself.
152 * You don't need to use this normally! But it lets you list_del(@n)
155 static inline void list_node_init(struct list_node *n)
157 n->next = n->prev = n;
161 * list_add_after - add an entry after an existing node in a linked list
162 * @h: the list_head to add the node to (for debugging)
163 * @p: the existing list_node to add the node after
164 * @n: the new list_node to add to the list.
166 * The existing list_node must already be a member of the list.
167 * The new list_node does not need to be initialized; it will be overwritten.
170 * struct child c1, c2, c3;
173 * list_add_tail(&h, &c1.list);
174 * list_add_tail(&h, &c3.list);
175 * list_add_after(&h, &c1.list, &c2.list);
177 #define list_add_after(h, p, n) list_add_after_(h, p, n, LIST_LOC)
178 static inline void list_add_after_(struct list_head *h,
181 const char *abortstr)
187 (void)list_debug(h, abortstr);
191 * list_add - add an entry at the start of a linked list.
192 * @h: the list_head to add the node to
193 * @n: the list_node to add to the list.
195 * The list_node does not need to be initialized; it will be overwritten.
197 * struct child *child = malloc(sizeof(*child));
199 * child->name = "marvin";
200 * list_add(&parent->children, &child->list);
201 * parent->num_children++;
203 #define list_add(h, n) list_add_(h, n, LIST_LOC)
204 static inline void list_add_(struct list_head *h,
206 const char *abortstr)
208 list_add_after_(h, &h->n, n, abortstr);
212 * list_add_before - add an entry before an existing node in a linked list
213 * @h: the list_head to add the node to (for debugging)
214 * @p: the existing list_node to add the node before
215 * @n: the new list_node to add to the list.
217 * The existing list_node must already be a member of the list.
218 * The new list_node does not need to be initialized; it will be overwritten.
221 * list_head_init(&h);
222 * list_add_tail(&h, &c1.list);
223 * list_add_tail(&h, &c3.list);
224 * list_add_before(&h, &c3.list, &c2.list);
226 #define list_add_before(h, p, n) list_add_before_(h, p, n, LIST_LOC)
227 static inline void list_add_before_(struct list_head *h,
230 const char *abortstr)
236 (void)list_debug(h, abortstr);
240 * list_add_tail - add an entry at the end of a linked list.
241 * @h: the list_head to add the node to
242 * @n: the list_node to add to the list.
244 * The list_node does not need to be initialized; it will be overwritten.
246 * list_add_tail(&parent->children, &child->list);
247 * parent->num_children++;
249 #define list_add_tail(h, n) list_add_tail_(h, n, LIST_LOC)
250 static inline void list_add_tail_(struct list_head *h,
252 const char *abortstr)
254 list_add_before_(h, &h->n, n, abortstr);
258 * list_empty - is a list empty?
261 * If the list is empty, returns true.
264 * assert(list_empty(&parent->children) == (parent->num_children == 0));
266 #define list_empty(h) list_empty_(h, LIST_LOC)
267 static inline bool list_empty_(const struct list_head *h, const char* abortstr)
269 (void)list_debug(h, abortstr);
270 return h->n.next == &h->n;
274 * list_empty_nodebug - is a list empty (and don't perform debug checks)?
277 * If the list is empty, returns true.
278 * This differs from list_empty() in that if CCAN_LIST_DEBUG is set it
279 * will NOT perform debug checks. Only use this function if you REALLY
280 * know what you're doing.
283 * assert(list_empty_nodebug(&parent->children) == (parent->num_children == 0));
285 #ifndef CCAN_LIST_DEBUG
286 #define list_empty_nodebug(h) list_empty(h)
288 static inline bool list_empty_nodebug(const struct list_head *h)
290 return h->n.next == &h->n;
295 * list_del - delete an entry from an (unknown) linked list.
296 * @n: the list_node to delete from the list.
298 * Note that this leaves @n in an undefined state; it can be added to
299 * another list, but not deleted again.
302 * list_del_from(), list_del_init()
305 * list_del(&child->list);
306 * parent->num_children--;
308 #define list_del(n) list_del_(n, LIST_LOC)
309 static inline void list_del_(struct list_node *n, const char* abortstr)
311 (void)list_debug_node(n, abortstr);
312 n->next->prev = n->prev;
313 n->prev->next = n->next;
314 #ifdef CCAN_LIST_DEBUG
315 /* Catch use-after-del. */
316 n->next = n->prev = NULL;
321 * list_del_init - delete a node, and reset it so it can be deleted again.
322 * @n: the list_node to be deleted.
324 * list_del(@n) or list_del_init() again after this will be safe,
325 * which can be useful in some cases.
328 * list_del_from(), list_del()
331 * list_del_init(&child->list);
332 * parent->num_children--;
334 #define list_del_init(n) list_del_init_(n, LIST_LOC)
335 static inline void list_del_init_(struct list_node *n, const char *abortstr)
337 list_del_(n, abortstr);
342 * list_del_from - delete an entry from a known linked list.
343 * @h: the list_head the node is in.
344 * @n: the list_node to delete from the list.
346 * This explicitly indicates which list a node is expected to be in,
347 * which is better documentation and can catch more bugs.
349 * See also: list_del()
352 * list_del_from(&parent->children, &child->list);
353 * parent->num_children--;
355 static inline void list_del_from(struct list_head *h, struct list_node *n)
357 #ifdef CCAN_LIST_DEBUG
359 /* Thorough check: make sure it was in list! */
361 for (i = h->n.next; i != n; i = i->next)
364 #endif /* CCAN_LIST_DEBUG */
366 /* Quick test that catches a surprising number of bugs. */
367 assert(!list_empty(h));
372 * list_swap - swap out an entry from an (unknown) linked list for a new one.
373 * @o: the list_node to replace from the list.
374 * @n: the list_node to insert in place of the old one.
376 * Note that this leaves @o in an undefined state; it can be added to
377 * another list, but not deleted/swapped again.
383 * struct child x1, x2;
386 * list_add(&xh, &x1.list);
387 * list_swap(&x1.list, &x2.list);
389 #define list_swap(o, n) list_swap_(o, n, LIST_LOC)
390 static inline void list_swap_(struct list_node *o,
392 const char* abortstr)
394 (void)list_debug_node(o, abortstr);
398 #ifdef CCAN_LIST_DEBUG
399 /* Catch use-after-del. */
400 o->next = o->prev = NULL;
405 * list_entry - convert a list_node back into the structure containing it.
407 * @type: the type of the entry
408 * @member: the list_node member of the type
411 * // First list entry is children.next; convert back to child.
412 * child = list_entry(parent->children.n.next, struct child, list);
415 * list_top(), list_for_each()
417 #define list_entry(n, type, member) container_of(n, type, member)
420 * list_top - get the first entry in a list
422 * @type: the type of the entry
423 * @member: the list_node member of the type
425 * If the list is empty, returns NULL.
428 * struct child *first;
429 * first = list_top(&parent->children, struct child, list);
431 * printf("Empty list!\n");
433 #define list_top(h, type, member) \
434 ((type *)list_top_((h), list_off_(type, member)))
436 static inline const void *list_top_(const struct list_head *h, size_t off)
440 return (const char *)h->n.next - off;
444 * list_pop - remove the first entry in a list
446 * @type: the type of the entry
447 * @member: the list_node member of the type
449 * If the list is empty, returns NULL.
453 * one = list_pop(&parent->children, struct child, list);
455 * printf("Empty list!\n");
457 #define list_pop(h, type, member) \
458 ((type *)list_pop_((h), list_off_(type, member)))
460 static inline const void *list_pop_(const struct list_head *h, size_t off)
468 return (const char *)n - off;
472 * list_tail - get the last entry in a list
474 * @type: the type of the entry
475 * @member: the list_node member of the type
477 * If the list is empty, returns NULL.
480 * struct child *last;
481 * last = list_tail(&parent->children, struct child, list);
483 * printf("Empty list!\n");
485 #define list_tail(h, type, member) \
486 ((type *)list_tail_((h), list_off_(type, member)))
488 static inline const void *list_tail_(const struct list_head *h, size_t off)
492 return (const char *)h->n.prev - off;
496 * list_for_each - iterate through a list.
497 * @h: the list_head (warning: evaluated multiple times!)
498 * @i: the structure containing the list_node
499 * @member: the list_node member of the structure
501 * This is a convenient wrapper to iterate @i over the entire list. It's
502 * a for loop, so you can break and continue as normal.
505 * list_for_each(&parent->children, child, list)
506 * printf("Name: %s\n", child->name);
508 #define list_for_each(h, i, member) \
509 list_for_each_off(h, i, list_off_var_(i, member))
512 * list_for_each_rev - iterate through a list backwards.
514 * @i: the structure containing the list_node
515 * @member: the list_node member of the structure
517 * This is a convenient wrapper to iterate @i over the entire list. It's
518 * a for loop, so you can break and continue as normal.
521 * list_for_each_rev(&parent->children, child, list)
522 * printf("Name: %s\n", child->name);
524 #define list_for_each_rev(h, i, member) \
525 list_for_each_rev_off(h, i, list_off_var_(i, member))
528 * list_for_each_rev_safe - iterate through a list backwards,
529 * maybe during deletion
531 * @i: the structure containing the list_node
532 * @nxt: the structure containing the list_node
533 * @member: the list_node member of the structure
535 * This is a convenient wrapper to iterate @i over the entire list backwards.
536 * It's a for loop, so you can break and continue as normal. The extra
537 * variable * @nxt is used to hold the next element, so you can delete @i
541 * struct child *next;
542 * list_for_each_rev_safe(&parent->children, child, next, list) {
543 * printf("Name: %s\n", child->name);
546 #define list_for_each_rev_safe(h, i, nxt, member) \
547 list_for_each_rev_safe_off(h, i, nxt, list_off_var_(i, member))
550 * list_for_each_safe - iterate through a list, maybe during deletion
552 * @i: the structure containing the list_node
553 * @nxt: the structure containing the list_node
554 * @member: the list_node member of the structure
556 * This is a convenient wrapper to iterate @i over the entire list. It's
557 * a for loop, so you can break and continue as normal. The extra variable
558 * @nxt is used to hold the next element, so you can delete @i from the list.
561 * list_for_each_safe(&parent->children, child, next, list) {
562 * list_del(&child->list);
563 * parent->num_children--;
566 #define list_for_each_safe(h, i, nxt, member) \
567 list_for_each_safe_off(h, i, nxt, list_off_var_(i, member))
570 * list_next - get the next entry in a list
572 * @i: a pointer to an entry in the list.
573 * @member: the list_node member of the structure
575 * If @i was the last entry in the list, returns NULL.
578 * struct child *second;
579 * second = list_next(&parent->children, first, list);
581 * printf("No second child!\n");
583 #define list_next(h, i, member) \
584 ((list_typeof(i))list_entry_or_null(list_debug(h, \
585 __FILE__ ":" stringify(__LINE__)), \
587 list_off_var_((i), member)))
590 * list_prev - get the previous entry in a list
592 * @i: a pointer to an entry in the list.
593 * @member: the list_node member of the structure
595 * If @i was the first entry in the list, returns NULL.
598 * first = list_prev(&parent->children, second, list);
600 * printf("Can't go back to first child?!\n");
602 #define list_prev(h, i, member) \
603 ((list_typeof(i))list_entry_or_null(list_debug(h, \
604 __FILE__ ":" stringify(__LINE__)), \
606 list_off_var_((i), member)))
609 * list_append_list - empty one list onto the end of another.
610 * @to: the list to append into
611 * @from: the list to empty.
613 * This takes the entire contents of @from and moves it to the end of
614 * @to. After this @from will be empty.
617 * struct list_head adopter;
619 * list_append_list(&adopter, &parent->children);
620 * assert(list_empty(&parent->children));
621 * parent->num_children = 0;
623 #define list_append_list(t, f) list_append_list_(t, f, \
624 __FILE__ ":" stringify(__LINE__))
625 static inline void list_append_list_(struct list_head *to,
626 struct list_head *from,
627 const char *abortstr)
629 struct list_node *from_tail = list_debug(from, abortstr)->n.prev;
630 struct list_node *to_tail = list_debug(to, abortstr)->n.prev;
632 /* Sew in head and entire list. */
633 to->n.prev = from_tail;
634 from_tail->next = &to->n;
635 to_tail->next = &from->n;
636 from->n.prev = to_tail;
638 /* Now remove head. */
640 list_head_init(from);
644 * list_prepend_list - empty one list into the start of another.
645 * @to: the list to prepend into
646 * @from: the list to empty.
648 * This takes the entire contents of @from and moves it to the start
649 * of @to. After this @from will be empty.
652 * list_prepend_list(&adopter, &parent->children);
653 * assert(list_empty(&parent->children));
654 * parent->num_children = 0;
656 #define list_prepend_list(t, f) list_prepend_list_(t, f, LIST_LOC)
657 static inline void list_prepend_list_(struct list_head *to,
658 struct list_head *from,
659 const char *abortstr)
661 struct list_node *from_tail = list_debug(from, abortstr)->n.prev;
662 struct list_node *to_head = list_debug(to, abortstr)->n.next;
664 /* Sew in head and entire list. */
665 to->n.next = &from->n;
666 from->n.prev = &to->n;
667 to_head->prev = from_tail;
668 from_tail->next = to_head;
670 /* Now remove head. */
672 list_head_init(from);
675 /* internal macros, do not use directly */
676 #define list_for_each_off_dir_(h, i, off, dir) \
677 for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.dir, \
679 list_node_from_off_((void *)i, (off)) != &(h)->n; \
680 i = list_node_to_off_(list_node_from_off_((void *)i, (off))->dir, \
683 #define list_for_each_safe_off_dir_(h, i, nxt, off, dir) \
684 for (i = list_node_to_off_(list_debug(h, LIST_LOC)->n.dir, \
686 nxt = list_node_to_off_(list_node_from_off_(i, (off))->dir, \
688 list_node_from_off_(i, (off)) != &(h)->n; \
690 nxt = list_node_to_off_(list_node_from_off_(i, (off))->dir, \
694 * list_for_each_off - iterate through a list of memory regions.
696 * @i: the pointer to a memory region wich contains list node data.
697 * @off: offset(relative to @i) at which list node data resides.
699 * This is a low-level wrapper to iterate @i over the entire list, used to
700 * implement all oher, more high-level, for-each constructs. It's a for loop,
701 * so you can break and continue as normal.
703 * WARNING! Being the low-level macro that it is, this wrapper doesn't know
704 * nor care about the type of @i. The only assumtion made is that @i points
705 * to a chunk of memory that at some @offset, relative to @i, contains a
706 * properly filled `struct node_list' which in turn contains pointers to
707 * memory chunks and it's turtles all the way down. Whith all that in mind
708 * remember that given the wrong pointer/offset couple this macro will
709 * happilly churn all you memory untill SEGFAULT stops it, in other words
712 * It is worth mentioning that one of legitimate use-cases for that wrapper
713 * is operation on opaque types with known offset for `struct list_node'
714 * member(preferably 0), because it allows you not to disclose the type of
718 * list_for_each_off(&parent->children, child,
719 * offsetof(struct child, list))
720 * printf("Name: %s\n", child->name);
722 #define list_for_each_off(h, i, off) \
723 list_for_each_off_dir_((h),(i),(off),next)
726 * list_for_each_rev_off - iterate through a list of memory regions backwards
728 * @i: the pointer to a memory region wich contains list node data.
729 * @off: offset(relative to @i) at which list node data resides.
731 * See list_for_each_off for details
733 #define list_for_each_rev_off(h, i, off) \
734 list_for_each_off_dir_((h),(i),(off),prev)
737 * list_for_each_safe_off - iterate through a list of memory regions, maybe
740 * @i: the pointer to a memory region wich contains list node data.
741 * @nxt: the structure containing the list_node
742 * @off: offset(relative to @i) at which list node data resides.
744 * For details see `list_for_each_off' and `list_for_each_safe'
748 * list_for_each_safe_off(&parent->children, child,
749 * next, offsetof(struct child, list))
750 * printf("Name: %s\n", child->name);
752 #define list_for_each_safe_off(h, i, nxt, off) \
753 list_for_each_safe_off_dir_((h),(i),(nxt),(off),next)
756 * list_for_each_rev_safe_off - iterate backwards through a list of
757 * memory regions, maybe during deletion
759 * @i: the pointer to a memory region wich contains list node data.
760 * @nxt: the structure containing the list_node
761 * @off: offset(relative to @i) at which list node data resides.
763 * For details see `list_for_each_rev_off' and `list_for_each_rev_safe'
767 * list_for_each_rev_safe_off(&parent->children, child,
768 * next, offsetof(struct child, list))
769 * printf("Name: %s\n", child->name);
771 #define list_for_each_rev_safe_off(h, i, nxt, off) \
772 list_for_each_safe_off_dir_((h),(i),(nxt),(off),prev)
774 /* Other -off variants. */
775 #define list_entry_off(n, type, off) \
776 ((type *)list_node_from_off_((n), (off)))
778 #define list_head_off(h, type, off) \
779 ((type *)list_head_off((h), (off)))
781 #define list_tail_off(h, type, off) \
782 ((type *)list_tail_((h), (off)))
784 #define list_add_off(h, n, off) \
785 list_add((h), list_node_from_off_((n), (off)))
787 #define list_del_off(n, off) \
788 list_del(list_node_from_off_((n), (off)))
790 #define list_del_from_off(h, n, off) \
791 list_del_from(h, list_node_from_off_((n), (off)))
793 /* Offset helper functions so we only single-evaluate. */
794 static inline void *list_node_to_off_(struct list_node *node, size_t off)
796 return (void *)((char *)node - off);
798 static inline struct list_node *list_node_from_off_(void *ptr, size_t off)
800 return (struct list_node *)((char *)ptr + off);
803 /* Get the offset of the member, but make sure it's a list_node. */
804 #define list_off_(type, member) \
805 (container_off(type, member) + \
806 check_type(((type *)0)->member, struct list_node))
808 #define list_off_var_(var, member) \
809 (container_off_var(var, member) + \
810 check_type(var->member, struct list_node))
813 #define list_typeof(var) typeof(var)
815 #define list_typeof(var) void *
818 /* Returns member, or NULL if at end of list. */
819 static inline void *list_entry_or_null(const struct list_head *h,
820 const struct list_node *n,
825 return (char *)n - off;
827 #endif /* CCAN_LIST_H */