1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
6 #include <ccan/container_of/container_of.h>
9 * struct list_node - an entry in a doubly-linked list
10 * @next: next entry (self if empty)
11 * @prev: previous entry (self if empty)
13 * This is used as an entry in a linked list.
17 * // Linked list of all us children.
18 * struct list_node list;
23 struct list_node *next, *prev;
27 * struct list_head - the head of a doubly-linked list
28 * @h: the list_head (containing next and prev pointers)
30 * This is used as the head of a linked list.
34 * struct list_head children;
35 * unsigned int num_children;
44 * list_check - check head of a list for consistency
46 * @abortstr: the location to print on aborting, or NULL.
48 * Because list_nodes have redundant information, consistency checking between
49 * the back and forward links can be done. This is useful as a debugging check.
50 * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
51 * is inconsistent, and the function will abort.
53 * Returns the list head if the list is consistent, NULL if not (it
54 * can never return NULL if @abortstr is set).
56 * See also: list_check_node()
59 * static void dump_parent(struct parent *p)
63 * printf("%s (%u children):\n", p->name, p->num_children);
64 * list_check(&p->children, "bad child list");
65 * list_for_each(&p->children, c, list)
66 * printf(" -> %s\n", c->name);
69 struct list_head *list_check(const struct list_head *h, const char *abortstr);
72 * list_check_node - check node of a list for consistency
74 * @abortstr: the location to print on aborting, or NULL.
76 * Check consistency of the list node is in (it must be in one).
78 * See also: list_check()
81 * static void dump_child(const struct child *c)
83 * list_check_node(&c->list, "bad child list");
84 * printf("%s\n", c->name);
87 struct list_node *list_check_node(const struct list_node *n,
88 const char *abortstr);
90 #ifdef CCAN_LIST_DEBUG
91 #define list_debug(h) list_check((h), __func__)
92 #define list_debug_node(n) list_check_node((n), __func__)
94 #define list_debug(h) (h)
95 #define list_debug_node(n) (n)
99 * LIST_HEAD_INIT - initializer for an empty list_head
100 * @name: the name of the list.
102 * Explicit initializer for an empty list.
105 * LIST_HEAD, list_head_init()
108 * static struct list_head my_list = LIST_HEAD_INIT(my_list);
110 #define LIST_HEAD_INIT(name) { { &name.n, &name.n } }
113 * LIST_HEAD - define and initialize an empty list_head
114 * @name: the name of the list.
116 * The LIST_HEAD macro defines a list_head and initializes it to an empty
117 * list. It can be prepended by "static" to define a static list_head.
120 * LIST_HEAD_INIT, list_head_init()
123 * static LIST_HEAD(my_global_list);
125 #define LIST_HEAD(name) \
126 struct list_head name = LIST_HEAD_INIT(name)
129 * list_head_init - initialize a list_head
130 * @h: the list_head to set to the empty list
134 * struct parent *parent = malloc(sizeof(*parent));
136 * list_head_init(&parent->children);
137 * parent->num_children = 0;
139 static inline void list_head_init(struct list_head *h)
141 h->n.next = h->n.prev = &h->n;
145 * list_add - add an entry at the start of a linked list.
146 * @h: the list_head to add the node to
147 * @n: the list_node to add to the list.
149 * The list_node does not need to be initialized; it will be overwritten.
151 * struct child *child = malloc(sizeof(*child));
153 * child->name = "marvin";
154 * list_add(&parent->children, &child->list);
155 * parent->num_children++;
157 static inline void list_add(struct list_head *h, struct list_node *n)
167 * list_add_tail - add an entry at the end of a linked list.
168 * @h: the list_head to add the node to
169 * @n: the list_node to add to the list.
171 * The list_node does not need to be initialized; it will be overwritten.
173 * list_add_tail(&parent->children, &child->list);
174 * parent->num_children++;
176 static inline void list_add_tail(struct list_head *h, struct list_node *n)
186 * list_empty - is a list empty?
189 * If the list is empty, returns true.
192 * assert(list_empty(&parent->children) == (parent->num_children == 0));
194 static inline bool list_empty(const struct list_head *h)
197 return h->n.next == &h->n;
201 * list_del - delete an entry from an (unknown) linked list.
202 * @n: the list_node to delete from the list.
204 * Note that this leaves @n in an undefined state; it can be added to
205 * another list, but not deleted again.
211 * list_del(&child->list);
212 * parent->num_children--;
214 static inline void list_del(struct list_node *n)
216 (void)list_debug_node(n);
217 n->next->prev = n->prev;
218 n->prev->next = n->next;
219 #ifdef CCAN_LIST_DEBUG
220 /* Catch use-after-del. */
221 n->next = n->prev = NULL;
226 * list_del_from - delete an entry from a known linked list.
227 * @h: the list_head the node is in.
228 * @n: the list_node to delete from the list.
230 * This explicitly indicates which list a node is expected to be in,
231 * which is better documentation and can catch more bugs.
233 * See also: list_del()
236 * list_del_from(&parent->children, &child->list);
237 * parent->num_children--;
239 static inline void list_del_from(struct list_head *h, struct list_node *n)
241 #ifdef CCAN_LIST_DEBUG
243 /* Thorough check: make sure it was in list! */
245 for (i = h->n.next; i != n; i = i->next)
248 #endif /* CCAN_LIST_DEBUG */
250 /* Quick test that catches a surprising number of bugs. */
251 assert(!list_empty(h));
256 * list_entry - convert a list_node back into the structure containing it.
258 * @type: the type of the entry
259 * @member: the list_node member of the type
262 * // First list entry is children.next; convert back to child.
263 * child = list_entry(parent->children.n.next, struct child, list);
266 * list_top(), list_for_each()
268 #define list_entry(n, type, member) container_of(n, type, member)
271 * list_top - get the first entry in a list
273 * @type: the type of the entry
274 * @member: the list_node member of the type
276 * If the list is empty, returns NULL.
279 * struct child *first;
280 * first = list_top(&parent->children, struct child, list);
282 #define list_top(h, type, member) \
283 ((type *)list_top_((h), container_off((h)->n.next, type, member)))
285 static inline const void *list_top_(const struct list_head *h, size_t off)
289 return (const char *)h->n.next - off;
293 * list_tail - get the last entry in a list
295 * @type: the type of the entry
296 * @member: the list_node member of the type
298 * If the list is empty, returns NULL.
301 * struct child *last;
302 * last = list_tail(&parent->children, struct child, list);
304 #define list_tail(h, type, member) \
305 ((type *)list_tail_((h), container_off((h)->n.next, type, member)))
307 static inline const void *list_tail_(const struct list_head *h, size_t off)
311 return (const char *)h->n.prev - off;
315 * list_for_each - iterate through a list.
316 * @h: the list_head (warning: evaluated multiple times!)
317 * @i: the structure containing the list_node
318 * @member: the list_node member of the structure
320 * This is a convenient wrapper to iterate @i over the entire list. It's
321 * a for loop, so you can break and continue as normal.
324 * list_for_each(&parent->children, child, list)
325 * printf("Name: %s\n", child->name);
327 #define list_for_each(h, i, member) \
328 for (i = container_of_var(list_debug(h)->n.next, i, member); \
329 &i->member != &(h)->n; \
330 i = container_of_var(i->member.next, i, member))
333 * list_for_each_rev - iterate through a list backwards.
335 * @i: the structure containing the list_node
336 * @member: the list_node member of the structure
338 * This is a convenient wrapper to iterate @i over the entire list. It's
339 * a for loop, so you can break and continue as normal.
342 * list_for_each_rev(&parent->children, child, list)
343 * printf("Name: %s\n", child->name);
345 #define list_for_each_rev(h, i, member) \
346 for (i = container_of_var(list_debug(h)->n.prev, i, member); \
347 &i->member != &(h)->n; \
348 i = container_of_var(i->member.prev, i, member))
351 * list_for_each_safe - iterate through a list, maybe during deletion
353 * @i: the structure containing the list_node
354 * @nxt: the structure containing the list_node
355 * @member: the list_node member of the structure
357 * This is a convenient wrapper to iterate @i over the entire list. It's
358 * a for loop, so you can break and continue as normal. The extra variable
359 * @nxt is used to hold the next element, so you can delete @i from the list.
362 * struct child *next;
363 * list_for_each_safe(&parent->children, child, next, list) {
364 * list_del(&child->list);
365 * parent->num_children--;
368 #define list_for_each_safe(h, i, nxt, member) \
369 for (i = container_of_var(list_debug(h)->n.next, i, member), \
370 nxt = container_of_var(i->member.next, i, member); \
371 &i->member != &(h)->n; \
372 i = nxt, nxt = container_of_var(i->member.next, i, member))
373 #endif /* CCAN_LIST_H */