1 /* Licensed under LGPL - see LICENSE file for details */
4 #include <ccan/list/list.h>
5 #include <ccan/tcon/tcon.h>
8 * TLIST_TYPE - declare a typed list type (struct tlist)
9 * @suffix: the name to use (struct tlist_@suffix)
10 * @type: the type the list will contain (void for any type)
12 * This declares a structure "struct tlist_@suffix" to use for
13 * lists containing this type. The actual list can be accessed using
14 * ".raw" or tlist_raw().
17 * // Defines struct tlist_children
18 * TLIST_TYPE(children, struct child);
21 * struct tlist_children children;
22 * unsigned int num_children;
27 * struct list_node list;
30 #define TLIST_TYPE(suffix, type) \
31 struct tlist_##suffix { \
32 struct list_head raw; \
37 * TLIST_INIT - initalizer for an empty tlist
38 * @name: the name of the list.
40 * Explicit initializer for an empty list.
46 * static struct tlist_children my_list = TLIST_INIT(my_list);
48 #define TLIST_INIT(name) { LIST_HEAD_INIT(name.raw) }
51 * tlist_check - check head of a list for consistency
53 * @abortstr: the location to print on aborting, or NULL.
55 * Because list_nodes have redundant information, consistency checking between
56 * the back and forward links can be done. This is useful as a debugging check.
57 * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
58 * is inconsistent, and the function will abort.
60 * Returns non-NULL if the list is consistent, NULL otherwise (it
61 * can never return NULL if @abortstr is set).
63 * See also: list_check()
66 * static void dump_parent(struct parent *p)
70 * printf("%s (%u children):\n", p->name, p->num_children);
71 * tlist_check(&p->children, "bad child list");
72 * tlist_for_each(&p->children, c, list)
73 * printf(" -> %s\n", c->name);
76 #define tlist_check(h, abortstr) \
77 list_check(&(h)->raw, (abortstr))
80 * tlist_init - initialize a tlist
81 * @h: the tlist to set to the empty list
85 * struct parent *parent = malloc(sizeof(*parent));
87 * tlist_init(&parent->children);
88 * parent->num_children = 0;
90 #define tlist_init(h) list_head_init(&(h)->raw)
93 * tlist_raw - unwrap the typed list and check the type
95 * @expr: the expression to check the type against (not evaluated)
97 * This macro usually causes the compiler to emit a warning if the
98 * variable is of an unexpected type. It is used internally where we
99 * need to access the raw underlying list.
101 #define tlist_raw(h, expr) (&tcon_check((h), canary, (expr))->raw)
104 * tlist_add - add an entry at the start of a linked list.
105 * @h: the tlist to add the node to
106 * @n: the entry to add to the list.
107 * @member: the member of n to add to the list.
109 * The entry's list_node does not need to be initialized; it will be
112 * struct child *child = malloc(sizeof(*child));
114 * child->name = "marvin";
115 * tlist_add(&parent->children, child, list);
116 * parent->num_children++;
118 #define tlist_add(h, n, member) list_add(tlist_raw((h), (n)), &(n)->member)
121 * tlist_add_tail - add an entry at the end of a linked list.
122 * @h: the tlist to add the node to
123 * @n: the entry to add to the list.
124 * @member: the member of n to add to the list.
126 * The list_node does not need to be initialized; it will be overwritten.
128 * tlist_add_tail(&parent->children, child, list);
129 * parent->num_children++;
131 #define tlist_add_tail(h, n, member) \
132 list_add_tail(tlist_raw((h), (n)), &(n)->member)
135 * tlist_del_from - delete an entry from a linked list.
136 * @h: the tlist @n is in
137 * @n: the entry to delete
138 * @member: the member of n to remove from the list.
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.
146 * See also: tlist_del()
149 * tlist_del_from(&parent->children, child, list);
150 * parent->num_children--;
152 #define tlist_del_from(h, n, member) \
153 list_del_from(tlist_raw((h), (n)), &(n)->member)
156 * tlist_del - delete an entry from an unknown linked list.
157 * @n: the entry to delete from the list.
158 * @member: the member of @n which is in the list.
161 * tlist_del(child, list);
162 * parent->num_children--;
164 #define tlist_del(n, member) \
165 list_del(&(n)->member)
168 * tlist_empty - is a list empty?
171 * If the list is empty, returns true.
174 * assert(tlist_empty(&parent->children) == (parent->num_children == 0));
176 #define tlist_empty(h) list_empty(&(h)->raw)
179 * tlist_top - get the first entry in a list
181 * @type: the type of the entry
182 * @member: the list_node member of the type
184 * If the list is empty, returns NULL.
187 * struct child *first;
188 * first = tlist_top(&parent->children, struct child, list);
190 #define tlist_top(h, type, member) \
191 list_top(tlist_raw((h), (type *)NULL), type, member)
194 * tlist_tail - get the last entry in a list
196 * @type: the type of the entry
197 * @member: the list_node member of the type
199 * If the list is empty, returns NULL.
202 * struct child *last;
203 * last = tlist_tail(&parent->children, struct child, list);
205 #define tlist_tail(h, type, member) \
206 list_tail(tlist_raw((h), (type *)NULL), type, member)
209 * tlist_for_each - iterate through a list.
211 * @i: an iterator of suitable type for this list.
212 * @member: the list_node member of @i
214 * This is a convenient wrapper to iterate @i over the entire list. It's
215 * a for loop, so you can break and continue as normal.
218 * tlist_for_each(&parent->children, child, list)
219 * printf("Name: %s\n", child->name);
221 #define tlist_for_each(h, i, member) \
222 list_for_each(tlist_raw((h), (i)), (i), member)
225 * tlist_for_each_safe - iterate through a list, maybe during deletion
227 * @i: an iterator of suitable type for this list.
228 * @nxt: another iterator to store the next entry.
229 * @member: the list_node member of the structure
231 * This is a convenient wrapper to iterate @i over the entire list. It's
232 * a for loop, so you can break and continue as normal. The extra variable
233 * @nxt is used to hold the next element, so you can delete @i from the list.
236 * struct child *next;
237 * tlist_for_each_safe(&parent->children, child, next, list) {
238 * tlist_del(child, list);
239 * parent->num_children--;
242 #define tlist_for_each_safe(h, i, nxt, member) \
243 list_for_each_safe(tlist_raw((h), (i)), (i), (nxt), member)
245 #endif /* CCAN_TLIST_H */