4 #include <ccan/container_of/container_of.h>
7 * struct list_node - an entry in a doubly-linked list
8 * @next: next entry (self if empty)
9 * @prev: previous entry (self if empty)
11 * This is used as an entry in a linked list.
15 * // Linked list of all us children.
16 * struct list_node list;
21 struct list_node *next, *prev;
25 * struct list_head - the head of a doubly-linked list
26 * @h: the list_head (containing next and prev pointers)
28 * This is used as the head of a linked list.
32 * struct list_head children;
33 * unsigned int num_children;
42 * list_check - check a list for consistency
44 * @abortstr: the location to print on aborting, or NULL.
46 * Because list_nodes have redundant information, consistency checking between
47 * the back and forward links can be done. This is useful as a debugging check.
48 * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
49 * is inconsistent, and the function will abort.
51 * Returns the list head if the list is consistent, NULL if not (it
52 * can never return NULL if @abortstr is set).
55 * static void dump_parent(struct parent *p)
59 * printf("%s (%u children):\n", p->name, parent->num_children);
60 * list_check(&p->children, "bad child list");
61 * list_for_each(&p->children, c, list)
62 * printf(" -> %s\n", c->name);
65 struct list_head *list_check(const struct list_head *h, const char *abortstr);
67 #ifdef CCAN_LIST_DEBUG
68 #define debug_list(h) list_check((h), __func__)
70 #define debug_list(h) (h)
74 * list_head_init - initialize a list_head
75 * @h: the list_head to set to the empty list
78 * list_head_init(&parent->children);
79 * parent->num_children = 0;
81 static inline void list_head_init(struct list_head *h)
83 h->n.next = h->n.prev = &h->n;
87 * LIST_HEAD - define and initalized empty list_head
88 * @name: the name of the list.
90 * The LIST_HEAD macro defines a list_head and initializes it to an empty
91 * list. It can be prepended by "static" to define a static list_head.
95 * extern struct list_head my_list;
100 #define LIST_HEAD(name) \
101 struct list_head name = { { &name.n, &name.n } }
104 * list_add - add an entry at the start of a linked list.
105 * @h: the list_head to add the node to
106 * @n: the list_node to add to the list.
108 * The list_node does not need to be initialized; it will be overwritten.
110 * list_add(&parent->children, &child->list);
111 * parent->num_children++;
113 static inline void list_add(struct list_head *h, struct list_node *n)
123 * list_add_tail - add an entry at the end of a linked list.
124 * @h: the list_head to add the node to
125 * @n: the list_node to add to the list.
127 * The list_node does not need to be initialized; it will be overwritten.
129 * list_add_tail(&parent->children, &child->list);
130 * parent->num_children++;
132 static inline void list_add_tail(struct list_head *h, struct list_node *n)
142 * list_del - delete an entry from a linked list.
143 * @n: the list_node to delete from the list.
146 * list_del(&child->list);
147 * parent->num_children--;
149 static inline void list_del(struct list_node *n)
151 n->next->prev = n->prev;
152 n->prev->next = n->next;
153 (void)debug_list(n->next);
154 #ifdef CCAN_LIST_DEBUG
155 /* Catch use-after-del. */
156 n->next = n->prev = NULL;
161 * list_empty - is a list empty?
164 * If the list is empty, returns true.
167 * assert(list_empty(&parent->children) == (parent->num_children == 0));
169 static inline bool list_empty(const struct list_head *h)
172 return h->n.next == &h->n;
176 * list_entry - convert a list_node back into the structure containing it.
178 * @type: the type of the entry
179 * @member: the list_node member of the type
183 * // First list entry is children.next; convert back to child.
184 * c = list_entry(parent->children.next, struct child, list);
186 #define list_entry(n, type, member) container_of(n, type, member)
189 * list_top - get the first entry in a list
191 * @type: the type of the entry
192 * @member: the list_node member of the type
194 * If the list is empty, returns NULL.
197 * struct child *first;
198 * first = list_top(&parent->children, struct child, list);
200 #define list_top(h, type, member) \
201 (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
204 * list_tail - get the last entry in a list
206 * @type: the type of the entry
207 * @member: the list_node member of the type
209 * If the list is empty, returns NULL.
212 * struct child *last;
213 * last = list_tail(&parent->children, struct child, list);
215 #define list_tail(h, type, member) \
216 (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
219 * list_for_each - iterate through a list.
221 * @i: the structure containing the list_node
222 * @member: the list_node member of the structure
224 * This is a convenient wrapper to iterate @i over the entire list. It's
225 * a for loop, so you can break and continue as normal.
229 * list_for_each(&parent->children, c, list)
230 * printf("Name: %s\n", c->name);
232 #define list_for_each(h, i, member) \
233 for (i = container_of_var(debug_list(h)->n.next, i, member); \
234 &i->member != &(h)->n; \
235 i = container_of_var(i->member.next, i, member))
238 * list_for_each_safe - iterate through a list, maybe during deletion
240 * @i: the structure containing the list_node
241 * @nxt: the structure containing the list_node
242 * @member: the list_node member of the structure
244 * This is a convenient wrapper to iterate @i over the entire list. It's
245 * a for loop, so you can break and continue as normal. The extra variable
246 * @nxt is used to hold the next element, so you can delete @i from the list.
249 * struct child *c, *n;
250 * list_for_each_safe(&parent->children, c, n, list) {
251 * list_del(&c->list);
252 * parent->num_children--;
255 #define list_for_each_safe(h, i, nxt, member) \
256 for (i = container_of_var(debug_list(h)->n.next, i, member), \
257 nxt = container_of_var(i->member.next, i, member); \
258 &i->member != &(h)->n; \
259 i = nxt, nxt = container_of_var(i->member.next, i, member))
260 #endif /* CCAN_LIST_H */