3 #include <ccan/list/list.h>
5 #if HAVE_FLEXIBLE_ARRAY_MEMBER
7 * TLIST_TYPE - declare a typed list type (struct tlist)
8 * @suffix: the name to use (struct tlist_@suffix)
9 * @type: the type the list will contain (void for any type)
11 * This declares a structure "struct tlist_@suffix" to use for
12 * lists containing this type. The actual list can be accessed using
13 * ".raw" or tlist_raw().
16 * // Defines struct tlist_children
17 * TLIST_TYPE(children, struct child);
20 * struct tlist_children children;
21 * unsigned int num_children;
26 * struct list_node list;
29 #define TLIST_TYPE(suffix, type) \
30 struct tlist_##suffix { \
31 struct list_head raw; \
32 const type *tcheck[]; \
36 * tlist_raw - access the raw list inside a typed list head.
37 * @h: the head of the typed list (struct tlist_@suffix)
38 * @test_var: a pointer to the expected element type.
40 * This elaborate macro usually causes the compiler to emit a warning
41 * if the variable is of an unexpected type. It is used internally
42 * where we need to access the raw underlying list.
44 #define tlist_raw(h, test_var) \
45 (sizeof((h)->tcheck[0] == (test_var)) ? &(h)->raw : &(h)->raw)
47 #define TLIST_TYPE(suffix, type) \
48 struct tlist_##suffix { \
49 struct list_head raw; \
51 #define tlist_raw(h, test_var) (&(h)->raw)
55 * TLIST_INIT - initalizer for an empty tlist
56 * @name: the name of the list.
58 * Explicit initializer for an empty list.
64 * static struct tlist_children my_list = TLIST_INIT(my_list);
66 #define TLIST_INIT(name) { LIST_HEAD_INIT(name.raw) }
69 * tlist_check - check head of a list for consistency
71 * @abortstr: the location to print on aborting, or NULL.
73 * Because list_nodes have redundant information, consistency checking between
74 * the back and forward links can be done. This is useful as a debugging check.
75 * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
76 * is inconsistent, and the function will abort.
78 * Returns non-NULL if the list is consistent, NULL otherwise (it
79 * can never return NULL if @abortstr is set).
81 * See also: list_check()
84 * static void dump_parent(struct parent *p)
88 * printf("%s (%u children):\n", p->name, p->num_children);
89 * tlist_check(&p->children, "bad child list");
90 * tlist_for_each(&p->children, c, list)
91 * printf(" -> %s\n", c->name);
94 #define tlist_check(h, abortstr) \
95 list_check(&(h)->raw, (abortstr))
98 * tlist_init - initialize a tlist
99 * @h: the tlist to set to the empty list
103 * struct parent *parent = malloc(sizeof(*parent));
105 * tlist_init(&parent->children);
106 * parent->num_children = 0;
108 #define tlist_init(h) list_head_init(&(h)->raw)
111 * tlist_add - add an entry at the start of a linked list.
112 * @h: the tlist to add the node to
113 * @n: the entry to add to the list.
114 * @member: the member of n to add to the list.
116 * The entry's list_node does not need to be initialized; it will be
119 * struct child *child = malloc(sizeof(*child));
121 * child->name = "marvin";
122 * tlist_add(&parent->children, child, list);
123 * parent->num_children++;
125 #define tlist_add(h, n, member) list_add(tlist_raw((h), (n)), &(n)->member)
128 * tlist_add_tail - add an entry at the end of a linked list.
129 * @h: the tlist to add the node to
130 * @n: the entry to add to the list.
131 * @member: the member of n to add to the list.
133 * The list_node does not need to be initialized; it will be overwritten.
135 * tlist_add_tail(&parent->children, child, list);
136 * parent->num_children++;
138 #define tlist_add_tail(h, n, member) \
139 list_add_tail(tlist_raw((h), (n)), &(n)->member)
142 * tlist_del_from - delete an entry from a linked list.
143 * @h: the tlist @n is in
144 * @n: the entry to delete
145 * @member: the member of n to remove from the list.
147 * This explicitly indicates which list a node is expected to be in,
148 * which is better documentation and can catch more bugs.
150 * Note that this leaves @n->@member in an undefined state; it
151 * can be added to another list, but not deleted again.
153 * See also: tlist_del()
156 * tlist_del_from(&parent->children, child, list);
157 * parent->num_children--;
159 #define tlist_del_from(h, n, member) \
160 list_del_from(tlist_raw((h), (n)), &(n)->member)
163 * tlist_del - delete an entry from an unknown linked list.
164 * @n: the entry to delete from the list.
165 * @member: the member of @n which is in the list.
168 * tlist_del(child, list);
169 * parent->num_children--;
171 #define tlist_del(n, member) \
172 list_del(&(n)->member)
175 * tlist_empty - is a list empty?
178 * If the list is empty, returns true.
181 * assert(tlist_empty(&parent->children) == (parent->num_children == 0));
183 #define tlist_empty(h) list_empty(&(h)->raw)
186 * tlist_top - get the first entry in a list
188 * @type: the type of the entry
189 * @member: the list_node member of the type
191 * If the list is empty, returns NULL.
194 * struct child *first;
195 * first = tlist_top(&parent->children, struct child, list);
197 #define tlist_top(h, type, member) \
198 list_top(tlist_raw((h), (type *)NULL), type, member)
201 * tlist_tail - get the last entry in a list
203 * @type: the type of the entry
204 * @member: the list_node member of the type
206 * If the list is empty, returns NULL.
209 * struct child *last;
210 * last = tlist_tail(&parent->children, struct child, list);
212 #define tlist_tail(h, type, member) \
213 list_tail(tlist_raw((h), (type *)NULL), type, member)
216 * tlist_for_each - iterate through a list.
218 * @i: an iterator of suitable type for this list.
219 * @member: the list_node member of @i
221 * This is a convenient wrapper to iterate @i over the entire list. It's
222 * a for loop, so you can break and continue as normal.
225 * tlist_for_each(&parent->children, child, list)
226 * printf("Name: %s\n", child->name);
228 #define tlist_for_each(h, i, member) \
229 list_for_each(tlist_raw((h), (i)), (i), member)
232 * tlist_for_each_safe - iterate through a list, maybe during deletion
234 * @i: an iterator of suitable type for this list.
235 * @nxt: another iterator to store the next entry.
236 * @member: the list_node member of the structure
238 * This is a convenient wrapper to iterate @i over the entire list. It's
239 * a for loop, so you can break and continue as normal. The extra variable
240 * @nxt is used to hold the next element, so you can delete @i from the list.
243 * struct child *next;
244 * tlist_for_each_safe(&parent->children, child, next, list) {
245 * tlist_del(child, list);
246 * parent->num_children--;
249 #define tlist_for_each_safe(h, i, nxt, member) \
250 list_for_each_safe(tlist_raw((h), (i)), (i), (nxt), member)
252 #endif /* CCAN_TLIST_H */