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_INIT - initalizer for an empty list_head
99 * @name: the name of the list.
101 * Explicit initializer for an empty list.
104 * LIST_HEAD, list_head_init()
107 * static struct list_head my_list = LIST_HEAD_INIT(my_list);
109 #define LIST_HEAD_INIT(name) { { &name.n, &name.n } }
112 * LIST_HEAD - define and initalize an empty list_head
113 * @name: the name of the list.
115 * The LIST_HEAD macro defines a list_head and initializes it to an empty
116 * list. It can be prepended by "static" to define a static list_head.
119 * LIST_HEAD_INIT, list_head_init()
122 * static LIST_HEAD(my_global_list);
124 #define LIST_HEAD(name) \
125 struct list_head name = LIST_HEAD_INIT(name)
128 * list_head_init - initialize a list_head
129 * @h: the list_head to set to the empty list
133 * struct parent *parent = malloc(sizeof(*parent));
135 * list_head_init(&parent->children);
136 * parent->num_children = 0;
138 static inline void list_head_init(struct list_head *h)
140 h->n.next = h->n.prev = &h->n;
144 * list_add - add an entry at the start of a linked list.
145 * @h: the list_head to add the node to
146 * @n: the list_node to add to the list.
148 * The list_node does not need to be initialized; it will be overwritten.
150 * struct child *child = malloc(sizeof(*child));
152 * child->name = "marvin";
153 * list_add(&parent->children, &child->list);
154 * parent->num_children++;
156 static inline void list_add(struct list_head *h, struct list_node *n)
166 * list_add_tail - add an entry at the end of a linked list.
167 * @h: the list_head to add the node to
168 * @n: the list_node to add to the list.
170 * The list_node does not need to be initialized; it will be overwritten.
172 * list_add_tail(&parent->children, &child->list);
173 * parent->num_children++;
175 static inline void list_add_tail(struct list_head *h, struct list_node *n)
185 * list_empty - is a list empty?
188 * If the list is empty, returns true.
191 * assert(list_empty(&parent->children) == (parent->num_children == 0));
193 static inline bool list_empty(const struct list_head *h)
196 return h->n.next == &h->n;
200 * list_del - delete an entry from an (unknown) linked list.
201 * @n: the list_node to delete from the list.
203 * Note that this leaves @n in an undefined state; it can be added to
204 * another list, but not deleted again.
210 * list_del(&child->list);
211 * parent->num_children--;
213 static inline void list_del(struct list_node *n)
215 (void)list_debug_node(n);
216 n->next->prev = n->prev;
217 n->prev->next = n->next;
218 #ifdef CCAN_LIST_DEBUG
219 /* Catch use-after-del. */
220 n->next = n->prev = NULL;
225 * list_del_from - delete an entry from a known linked list.
226 * @h: the list_head the node is in.
227 * @n: the list_node to delete from the list.
229 * This explicitly indicates which list a node is expected to be in,
230 * which is better documentation and can catch more bugs.
232 * See also: list_del()
235 * list_del_from(&parent->children, &child->list);
236 * parent->num_children--;
238 static inline void list_del_from(struct list_head *h, struct list_node *n)
240 #ifdef CCAN_LIST_DEBUG
242 /* Thorough check: make sure it was in list! */
244 for (i = h->n.next; i != n; i = i->next)
247 #endif /* CCAN_LIST_DEBUG */
249 /* Quick test that catches a surprising number of bugs. */
250 assert(!list_empty(h));
255 * list_entry - convert a list_node back into the structure containing it.
257 * @type: the type of the entry
258 * @member: the list_node member of the type
261 * // First list entry is children.next; convert back to child.
262 * child = list_entry(parent->children.n.next, struct child, list);
265 * list_top(), list_for_each()
267 #define list_entry(n, type, member) container_of(n, type, member)
270 * list_top - get the first entry in a list
272 * @type: the type of the entry
273 * @member: the list_node member of the type
275 * If the list is empty, returns NULL.
278 * struct child *first;
279 * first = list_top(&parent->children, struct child, list);
281 #define list_top(h, type, member) \
282 (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
285 * list_tail - get the last entry in a list
287 * @type: the type of the entry
288 * @member: the list_node member of the type
290 * If the list is empty, returns NULL.
293 * struct child *last;
294 * last = list_tail(&parent->children, struct child, list);
296 #define list_tail(h, type, member) \
297 (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
300 * list_for_each - iterate through a list.
302 * @i: the structure containing the list_node
303 * @member: the list_node member of the structure
305 * This is a convenient wrapper to iterate @i over the entire list. It's
306 * a for loop, so you can break and continue as normal.
309 * list_for_each(&parent->children, child, list)
310 * printf("Name: %s\n", child->name);
312 #define list_for_each(h, i, member) \
313 for (i = container_of_var(list_debug(h)->n.next, i, member); \
314 &i->member != &(h)->n; \
315 i = container_of_var(i->member.next, i, member))
318 * list_for_each_safe - iterate through a list, maybe during deletion
320 * @i: the structure containing the list_node
321 * @nxt: the structure containing the list_node
322 * @member: the list_node member of the structure
324 * This is a convenient wrapper to iterate @i over the entire list. It's
325 * a for loop, so you can break and continue as normal. The extra variable
326 * @nxt is used to hold the next element, so you can delete @i from the list.
329 * struct child *next;
330 * list_for_each_safe(&parent->children, child, next, list) {
331 * list_del(&child->list);
332 * parent->num_children--;
335 #define list_for_each_safe(h, i, nxt, member) \
336 for (i = container_of_var(list_debug(h)->n.next, i, member), \
337 nxt = container_of_var(i->member.next, i, member); \
338 &i->member != &(h)->n; \
339 i = nxt, nxt = container_of_var(i->member.next, i, member))
340 #endif /* CCAN_LIST_H */