1 /* Licensed under LGPL - see LICENSE file for details */
4 #include <ccan/list/list.h>
5 #include <ccan/tcon/tcon.h>
8 * TLIST2 - declare a typed list type (struct tlist)
9 * @etype: the type the list will contain
10 * @link: the name of the member of @etype that is the link
12 * This declares an anonymous structure to use for lists containing this type.
13 * The actual list can be accessed using tlist2_raw().
16 * #include <ccan/list/list.h>
17 * #include <ccan/tlist2/tlist2.h>
20 * struct list_node list;
24 * TLIST2(struct child, list) children;
25 * unsigned int num_children;
29 #define TLIST2(etype, link) \
30 TCON_WRAP(struct list_head, \
31 TCON_CONTAINER(canary, etype, link))
34 * TLIST2_INIT - initializer for an empty tlist
35 * @name: the name of the list.
37 * Explicit initializer for an empty list.
43 * TLIST2(struct child, list) my_list = TLIST2_INIT(my_list);
45 #define TLIST2_INIT(name) TCON_WRAP_INIT( LIST_HEAD_INIT(*tcon_unwrap(&(name))) )
48 * tlist2_check - check head of a list for consistency
50 * @abortstr: the location to print on aborting, or NULL.
52 * Because list_nodes have redundant information, consistency checking between
53 * the back and forward links can be done. This is useful as a debugging check.
54 * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
55 * is inconsistent, and the function will abort.
57 * Returns non-NULL if the list is consistent, NULL otherwise (it
58 * can never return NULL if @abortstr is set).
60 * See also: list_check()
63 * static void dump_parent(struct parent *p)
67 * printf("%s (%u children):\n", p->name, p->num_children);
68 * tlist2_check(&p->children, "bad child list");
69 * tlist2_for_each(&p->children, c)
70 * printf(" -> %s\n", c->name);
73 #define tlist2_check(h, abortstr) \
74 list_check(tcon_unwrap(h), (abortstr))
77 * tlist2_init - initialize a tlist
78 * @h: the tlist to set to the empty list
82 * struct parent *parent = malloc(sizeof(*parent));
84 * tlist2_init(&parent->children);
85 * parent->num_children = 0;
87 #define tlist2_init(h) list_head_init(tcon_unwrap(h))
90 * tlist2_raw - unwrap the typed list and check the type
92 * @expr: the expression to check the type against (not evaluated)
94 * This macro usually causes the compiler to emit a warning if the
95 * variable is of an unexpected type. It is used internally where we
96 * need to access the raw underlying list.
98 #define tlist2_raw(h, expr) tcon_unwrap(tcon_container_check_ptr(h, canary, expr))
101 * tlist2_unwrap - unwrap the typed list without any checks
104 #define tlist2_unwrap(h) tcon_unwrap(h)
107 * tlist2_add - add an entry at the start of a linked list.
108 * @h: the tlist to add the node to
109 * @n: the entry to add to the list.
111 * The entry's list_node does not need to be initialized; it will be
114 * struct child *child = malloc(sizeof(*child));
116 * child->name = "marvin";
117 * tlist2_add(&parent->children, child);
118 * parent->num_children++;
120 #define tlist2_add(h, n) list_add(tlist2_raw((h), (n)), tcon_member_of(h, canary, n))
123 * tlist2_add_tail - add an entry at the end of a linked list.
124 * @h: the tlist to add the node to
125 * @n: the entry to add to the list.
127 * The list_node does not need to be initialized; it will be overwritten.
129 * tlist2_add_tail(&parent->children, child);
130 * parent->num_children++;
132 #define tlist2_add_tail(h, n) \
133 list_add_tail(tlist2_raw((h), (n)), tcon_member_of((h), canary, (n)))
136 * tlist2_del_from - delete an entry from a linked list.
137 * @h: the tlist @n is in
138 * @n: the entry to delete
140 * This explicitly indicates which list a node is expected to be in,
141 * which is better documentation and can catch more bugs.
143 * Note that this leaves @n->@member in an undefined state; it
144 * can be added to another list, but not deleted again.
147 * tlist2_del_from(&parent->children, child);
148 * parent->num_children--;
150 #define tlist2_del_from(h, n) \
151 list_del_from(tlist2_raw((h), (n)), tcon_member_of((h), canary, (n)))
154 * tlist2_empty - is a list empty?
157 * If the list is empty, returns true.
160 * assert(tlist2_empty(&parent->children) == (parent->num_children == 0));
162 #define tlist2_empty(h) list_empty(tcon_unwrap(h))
165 * tlist2_top - get the first entry in a list
168 * If the list is empty, returns NULL.
171 * struct child *first;
172 * first = tlist2_top(&parent->children);
174 * printf("Empty list!\n");
176 #define tlist2_top(h) tcon_container_of((h), canary, list_top_(tcon_unwrap(h), 0))
179 * tlist2_tail - get the last entry in a list
182 * If the list is empty, returns NULL.
185 * struct child *last;
186 * last = tlist2_tail(&parent->children);
188 * printf("Empty list!\n");
190 #define tlist2_tail(h) tcon_container_of((h), canary, list_tail_(tcon_unwrap(h), 0))
193 * tlist2_for_each - iterate through a list.
195 * @i: an iterator of suitable type for this list.
197 * This is a convenient wrapper to iterate @i over the entire list. It's
198 * a for loop, so you can break and continue as normal.
201 * tlist2_for_each(&parent->children, child)
202 * printf("Name: %s\n", child->name);
204 #define tlist2_for_each(h, i) \
205 list_for_each_off(tlist2_raw((h), (i)), (i), tcon_offset((h), canary))
208 * tlist2_for_each_rev - iterate through a list backwards.
210 * @i: an iterator of suitable type for this list.
212 * This is a convenient wrapper to iterate @i over the entire list. It's
213 * a for loop, so you can break and continue as normal.
216 * tlist2_for_each_rev(&parent->children, child)
217 * printf("Name: %s\n", child->name);
219 #define tlist2_for_each_rev(h, i) \
220 list_for_each_rev_off(tlist2_raw((h), (i)), (i), tcon_offset((h), canary))
223 * tlist2_for_each_safe - iterate through a list, maybe during deletion
225 * @i: an iterator of suitable type for this list.
226 * @nxt: another iterator to store the next entry.
228 * This is a convenient wrapper to iterate @i over the entire list. It's
229 * a for loop, so you can break and continue as normal. The extra variable
230 * @nxt is used to hold the next element, so you can delete @i from the list.
233 * struct child *next;
234 * tlist2_for_each_safe(&parent->children, child, next) {
235 * tlist2_del_from(&parent->children, child);
236 * parent->num_children--;
239 #define tlist2_for_each_safe(h, i, nxt) \
240 list_for_each_safe_off(tlist2_raw((h), (i)), (i), (nxt), tcon_offset((h), canary))
242 #endif /* CCAN_TLIST2_H */