5 #include <ccan/container_of/container_of.h>
8 * struct list_node - an entry in a doubly-linked list
9 * @next: next entry (self if empty)
10 * @prev: previous entry (self if empty)
12 * This is used as an entry in a linked list.
16 * // Linked list of all us children.
17 * struct list_node list;
22 struct list_node *next, *prev;
26 * struct list_head - the head of a doubly-linked list
27 * @h: the list_head (containing next and prev pointers)
29 * This is used as the head of a linked list.
33 * struct list_head children;
34 * unsigned int num_children;
43 * list_check - check head of a list for consistency
45 * @abortstr: the location to print on aborting, or NULL.
47 * Because list_nodes have redundant information, consistency checking between
48 * the back and forward links can be done. This is useful as a debugging check.
49 * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
50 * is inconsistent, and the function will abort.
52 * Returns the list head if the list is consistent, NULL if not (it
53 * can never return NULL if @abortstr is set).
55 * See also: list_check_node()
58 * static void dump_parent(struct parent *p)
62 * printf("%s (%u children):\n", p->name, p->num_children);
63 * list_check(&p->children, "bad child list");
64 * list_for_each(&p->children, c, list)
65 * printf(" -> %s\n", c->name);
68 struct list_head *list_check(const struct list_head *h, const char *abortstr);
71 * list_check_node - check node of a list for consistency
73 * @abortstr: the location to print on aborting, or NULL.
75 * Check consistency of the list node is in (it must be in one).
77 * See also: list_check()
80 * static void dump_child(const struct child *c)
82 * list_check_node(&c->list, "bad child list");
83 * printf("%s\n", c->name);
86 struct list_node *list_check_node(const struct list_node *n,
87 const char *abortstr);
89 #ifdef CCAN_LIST_DEBUG
90 #define list_debug(h) list_check((h), __func__)
91 #define list_debug_node(n) list_check_node((n), __func__)
93 #define list_debug(h) (h)
94 #define list_debug_node(n) (n)
98 * LIST_HEAD - define and initalize an empty list_head
99 * @name: the name of the list.
101 * The LIST_HEAD macro defines a list_head and initializes it to an empty
102 * list. It can be prepended by "static" to define a static list_head.
105 * static LIST_HEAD(my_global_list);
107 #define LIST_HEAD(name) \
108 struct list_head name = { { &name.n, &name.n } }
111 * list_head_init - initialize a list_head
112 * @h: the list_head to set to the empty list
116 * struct parent *parent = malloc(sizeof(*parent));
118 * list_head_init(&parent->children);
119 * parent->num_children = 0;
121 static inline void list_head_init(struct list_head *h)
123 h->n.next = h->n.prev = &h->n;
127 * list_add - add an entry at the start of a linked list.
128 * @h: the list_head to add the node to
129 * @n: the list_node to add to the list.
131 * The list_node does not need to be initialized; it will be overwritten.
133 * struct child *child = malloc(sizeof(*child));
135 * child->name = "marvin";
136 * list_add(&parent->children, &child->list);
137 * parent->num_children++;
139 static inline void list_add(struct list_head *h, struct list_node *n)
149 * list_add_tail - add an entry at the end of a linked list.
150 * @h: the list_head to add the node to
151 * @n: the list_node to add to the list.
153 * The list_node does not need to be initialized; it will be overwritten.
155 * list_add_tail(&parent->children, &child->list);
156 * parent->num_children++;
158 static inline void list_add_tail(struct list_head *h, struct list_node *n)
168 * list_empty - is a list empty?
171 * If the list is empty, returns true.
174 * assert(list_empty(&parent->children) == (parent->num_children == 0));
176 static inline bool list_empty(const struct list_head *h)
179 return h->n.next == &h->n;
183 * list_del - delete an entry from an (unknown) linked list.
184 * @n: the list_node to delete from the list.
186 * Note that this leaves @n in an undefined state; it can be added to
187 * another list, but not deleted again.
193 * list_del(&child->list);
194 * parent->num_children--;
196 static inline void list_del(struct list_node *n)
198 (void)list_debug_node(n);
199 n->next->prev = n->prev;
200 n->prev->next = n->next;
201 #ifdef CCAN_LIST_DEBUG
202 /* Catch use-after-del. */
203 n->next = n->prev = NULL;
208 * list_del_from - delete an entry from a known linked list.
209 * @h: the list_head the node is in.
210 * @n: the list_node to delete from the list.
212 * This explicitly indicates which list a node is expected to be in,
213 * which is better documentation and can catch more bugs.
215 * See also: list_del()
218 * list_del_from(&parent->children, &child->list);
219 * parent->num_children--;
221 static inline void list_del_from(struct list_head *h, struct list_node *n)
223 #ifdef CCAN_LIST_DEBUG
225 /* Thorough check: make sure it was in list! */
227 for (i = h->n.next; i != n; i = i->next)
230 #endif /* CCAN_LIST_DEBUG */
232 /* Quick test that catches a surprising number of bugs. */
233 assert(!list_empty(h));
238 * list_entry - convert a list_node back into the structure containing it.
240 * @type: the type of the entry
241 * @member: the list_node member of the type
244 * // First list entry is children.next; convert back to child.
245 * child = list_entry(parent->children.n.next, struct child, list);
248 * list_top(), list_for_each()
250 #define list_entry(n, type, member) container_of(n, type, member)
253 * list_top - get the first entry in a list
255 * @type: the type of the entry
256 * @member: the list_node member of the type
258 * If the list is empty, returns NULL.
261 * struct child *first;
262 * first = list_top(&parent->children, struct child, list);
264 #define list_top(h, type, member) \
265 (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
268 * list_tail - get the last entry in a list
270 * @type: the type of the entry
271 * @member: the list_node member of the type
273 * If the list is empty, returns NULL.
276 * struct child *last;
277 * last = list_tail(&parent->children, struct child, list);
279 #define list_tail(h, type, member) \
280 (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
283 * list_for_each - iterate through a list.
285 * @i: the structure containing the list_node
286 * @member: the list_node member of the structure
288 * This is a convenient wrapper to iterate @i over the entire list. It's
289 * a for loop, so you can break and continue as normal.
292 * list_for_each(&parent->children, child, list)
293 * printf("Name: %s\n", child->name);
295 #define list_for_each(h, i, member) \
296 for (i = container_of_var(list_debug(h)->n.next, i, member); \
297 &i->member != &(h)->n; \
298 i = container_of_var(i->member.next, i, member))
301 * list_for_each_safe - iterate through a list, maybe during deletion
303 * @i: the structure containing the list_node
304 * @nxt: the structure containing the list_node
305 * @member: the list_node member of the structure
307 * This is a convenient wrapper to iterate @i over the entire list. It's
308 * a for loop, so you can break and continue as normal. The extra variable
309 * @nxt is used to hold the next element, so you can delete @i from the list.
312 * struct child *next;
313 * list_for_each_safe(&parent->children, child, next, list) {
314 * list_del(&child->list);
315 * parent->num_children--;
318 #define list_for_each_safe(h, i, nxt, member) \
319 for (i = container_of_var(list_debug(h)->n.next, i, member), \
320 nxt = container_of_var(i->member.next, i, member); \
321 &i->member != &(h)->n; \
322 i = nxt, nxt = container_of_var(i->member.next, i, member))
323 #endif /* CCAN_LIST_H */