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, p->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 - define and initalize an empty list_head
75 * @name: the name of the list.
77 * The LIST_HEAD macro defines a list_head and initializes it to an empty
78 * list. It can be prepended by "static" to define a static list_head.
81 * static LIST_HEAD(my_global_list);
83 #define LIST_HEAD(name) \
84 struct list_head name = { { &name.n, &name.n } }
87 * list_head_init - initialize a list_head
88 * @h: the list_head to set to the empty list
92 * struct parent *parent = malloc(sizeof(*parent));
94 * list_head_init(&parent->children);
95 * parent->num_children = 0;
97 static inline void list_head_init(struct list_head *h)
99 h->n.next = h->n.prev = &h->n;
103 * list_add - add an entry at the start of a linked list.
104 * @h: the list_head to add the node to
105 * @n: the list_node to add to the list.
107 * The list_node does not need to be initialized; it will be overwritten.
109 * struct child *child;
111 * list_add(&parent->children, &child->list);
112 * parent->num_children++;
114 static inline void list_add(struct list_head *h, struct list_node *n)
124 * list_add_tail - add an entry at the end of a linked list.
125 * @h: the list_head to add the node to
126 * @n: the list_node to add to the list.
128 * The list_node does not need to be initialized; it will be overwritten.
130 * list_add_tail(&parent->children, &child->list);
131 * parent->num_children++;
133 static inline void list_add_tail(struct list_head *h, struct list_node *n)
143 * list_del - delete an entry from a linked list.
144 * @n: the list_node to delete from the list.
147 * list_del(&child->list);
148 * parent->num_children--;
150 static inline void list_del(struct list_node *n)
152 n->next->prev = n->prev;
153 n->prev->next = n->next;
154 (void)debug_list(n->next);
155 #ifdef CCAN_LIST_DEBUG
156 /* Catch use-after-del. */
157 n->next = n->prev = NULL;
162 * list_empty - is a list empty?
165 * If the list is empty, returns true.
168 * assert(list_empty(&parent->children) == (parent->num_children == 0));
170 static inline bool list_empty(const struct list_head *h)
173 return h->n.next == &h->n;
177 * list_entry - convert a list_node back into the structure containing it.
179 * @type: the type of the entry
180 * @member: the list_node member of the type
183 * // First list entry is children.next; convert back to child.
184 * child = list_entry(parent->children.n.next, struct child, list);
187 * list_top(), list_for_each()
189 #define list_entry(n, type, member) container_of(n, type, member)
192 * list_top - get the first entry in a list
194 * @type: the type of the entry
195 * @member: the list_node member of the type
197 * If the list is empty, returns NULL.
200 * struct child *first;
201 * first = list_top(&parent->children, struct child, list);
203 #define list_top(h, type, member) \
204 (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
207 * list_tail - get the last entry in a list
209 * @type: the type of the entry
210 * @member: the list_node member of the type
212 * If the list is empty, returns NULL.
215 * struct child *last;
216 * last = list_tail(&parent->children, struct child, list);
218 #define list_tail(h, type, member) \
219 (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
222 * list_for_each - iterate through a list.
224 * @i: the structure containing the list_node
225 * @member: the list_node member of the structure
227 * This is a convenient wrapper to iterate @i over the entire list. It's
228 * a for loop, so you can break and continue as normal.
231 * list_for_each(&parent->children, child, list)
232 * printf("Name: %s\n", child->name);
234 #define list_for_each(h, i, member) \
235 for (i = container_of_var(debug_list(h)->n.next, i, member); \
236 &i->member != &(h)->n; \
237 i = container_of_var(i->member.next, i, member))
240 * list_for_each_safe - iterate through a list, maybe during deletion
242 * @i: the structure containing the list_node
243 * @nxt: the structure containing the list_node
244 * @member: the list_node member of the structure
246 * This is a convenient wrapper to iterate @i over the entire list. It's
247 * a for loop, so you can break and continue as normal. The extra variable
248 * @nxt is used to hold the next element, so you can delete @i from the list.
251 * struct child *next;
252 * list_for_each_safe(&parent->children, child, next, list) {
253 * list_del(&child->list);
254 * parent->num_children--;
257 #define list_for_each_safe(h, i, nxt, member) \
258 for (i = container_of_var(debug_list(h)->n.next, i, member), \
259 nxt = container_of_var(i->member.next, i, member); \
260 &i->member != &(h)->n; \
261 i = nxt, nxt = container_of_var(i->member.next, i, member))
262 #endif /* CCAN_LIST_H */