1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_LIST_H
3 #define CCAN_LIST_H
4 #include <stdbool.h>
5 #include <assert.h>
6 #include <ccan/container_of/container_of.h>
7 #include <ccan/check_type/check_type.h>
8 
9 /**
10  * struct list_node - an entry in a doubly-linked list
11  * @next: next entry (self if empty)
12  * @prev: previous entry (self if empty)
13  *
14  * This is used as an entry in a linked list.
15  * Example:
16  *	struct child {
17  *		const char *name;
18  *		// Linked list of all us children.
19  *		struct list_node list;
20  *	};
21  */
22 struct list_node
23 {
24 	struct list_node *next, *prev;
25 };
26 
27 /**
28  * struct list_head - the head of a doubly-linked list
29  * @h: the list_head (containing next and prev pointers)
30  *
31  * This is used as the head of a linked list.
32  * Example:
33  *	struct parent {
34  *		const char *name;
35  *		struct list_head children;
36  *		unsigned int num_children;
37  *	};
38  */
39 struct list_head
40 {
41 	struct list_node n;
42 };
43 
44 /**
45  * list_check - check head of a list for consistency
46  * @h: the list_head
47  * @abortstr: the location to print on aborting, or NULL.
48  *
49  * Because list_nodes have redundant information, consistency checking between
50  * the back and forward links can be done.  This is useful as a debugging check.
51  * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
52  * is inconsistent, and the function will abort.
53  *
54  * Returns the list head if the list is consistent, NULL if not (it
55  * can never return NULL if @abortstr is set).
56  *
57  * See also: list_check_node()
58  *
59  * Example:
60  *	static void dump_parent(struct parent *p)
61  *	{
62  *		struct child *c;
63  *
64  *		printf("%s (%u children):\n", p->name, p->num_children);
65  *		list_check(&p->children, "bad child list");
66  *		list_for_each(&p->children, c, list)
67  *			printf(" -> %s\n", c->name);
68  *	}
69  */
70 struct list_head *list_check(const struct list_head *h, const char *abortstr);
71 
72 /**
73  * list_check_node - check node of a list for consistency
74  * @n: the list_node
75  * @abortstr: the location to print on aborting, or NULL.
76  *
77  * Check consistency of the list node is in (it must be in one).
78  *
79  * See also: list_check()
80  *
81  * Example:
82  *	static void dump_child(const struct child *c)
83  *	{
84  *		list_check_node(&c->list, "bad child list");
85  *		printf("%s\n", c->name);
86  *	}
87  */
88 struct list_node *list_check_node(const struct list_node *n,
89 				  const char *abortstr);
90 
91 #ifdef CCAN_LIST_DEBUG
92 #define list_debug(h) list_check((h), __func__)
93 #define list_debug_node(n) list_check_node((n), __func__)
94 #else
95 #define list_debug(h) (h)
96 #define list_debug_node(n) (n)
97 #endif
98 
99 /**
100  * LIST_HEAD_INIT - initializer for an empty list_head
101  * @name: the name of the list.
102  *
103  * Explicit initializer for an empty list.
104  *
105  * See also:
106  *	LIST_HEAD, list_head_init()
107  *
108  * Example:
109  *	static struct list_head my_list = LIST_HEAD_INIT(my_list);
110  */
111 #define LIST_HEAD_INIT(name) { { &name.n, &name.n } }
112 
113 /**
114  * LIST_HEAD - define and initialize an empty list_head
115  * @name: the name of the list.
116  *
117  * The LIST_HEAD macro defines a list_head and initializes it to an empty
118  * list.  It can be prepended by "static" to define a static list_head.
119  *
120  * See also:
121  *	LIST_HEAD_INIT, list_head_init()
122  *
123  * Example:
124  *	static LIST_HEAD(my_global_list);
125  */
126 #define LIST_HEAD(name) \
127 	struct list_head name = LIST_HEAD_INIT(name)
128 
129 /**
130  * list_head_init - initialize a list_head
131  * @h: the list_head to set to the empty list
132  *
133  * Example:
134  *	...
135  *	struct parent *parent = malloc(sizeof(*parent));
136  *
137  *	list_head_init(&parent->children);
138  *	parent->num_children = 0;
139  */
list_head_init(struct list_head * h)140 static inline void list_head_init(struct list_head *h)
141 {
142 	h->n.next = h->n.prev = &h->n;
143 }
144 
145 /**
146  * list_add - add an entry at the start of a linked list.
147  * @h: the list_head to add the node to
148  * @n: the list_node to add to the list.
149  *
150  * The list_node does not need to be initialized; it will be overwritten.
151  * Example:
152  *	struct child *child = malloc(sizeof(*child));
153  *
154  *	child->name = "marvin";
155  *	list_add(&parent->children, &child->list);
156  *	parent->num_children++;
157  */
list_add(struct list_head * h,struct list_node * n)158 static inline void list_add(struct list_head *h, struct list_node *n)
159 {
160 	n->next = h->n.next;
161 	n->prev = &h->n;
162 	h->n.next->prev = n;
163 	h->n.next = n;
164 	(void)list_debug(h);
165 }
166 
167 /**
168  * list_add_before - add an entry before another entry.
169  * @h: the list_head to add the node to (we use it for debug purposes, can be NULL)
170  * @n: the list_node to add to the list.
171  * @p: the list_node of the other entry
172  *
173  * The list_node does not need to be initialized; it will be overwritten.
174  */
list_add_before(struct list_head * h,struct list_node * n,struct list_node * p)175 static inline void list_add_before(struct list_head *h, struct list_node *n,
176 				   struct list_node *p)
177 {
178 	n->next = p;
179 	n->prev = p->prev;
180 	p->prev = n;
181 	n->prev->next = n;
182 	if (h)
183 		(void)list_debug(h);
184 }
185 
186 /**
187  * list_add_tail - add an entry at the end of a linked list.
188  * @h: the list_head to add the node to
189  * @n: the list_node to add to the list.
190  *
191  * The list_node does not need to be initialized; it will be overwritten.
192  * Example:
193  *	list_add_tail(&parent->children, &child->list);
194  *	parent->num_children++;
195  */
list_add_tail(struct list_head * h,struct list_node * n)196 static inline void list_add_tail(struct list_head *h, struct list_node *n)
197 {
198 	n->next = &h->n;
199 	n->prev = h->n.prev;
200 	h->n.prev->next = n;
201 	h->n.prev = n;
202 	(void)list_debug(h);
203 }
204 
205 /**
206  * list_empty - is a list empty?
207  * @h: the list_head
208  *
209  * If the list is empty, returns true.
210  *
211  * Example:
212  *	assert(list_empty(&parent->children) == (parent->num_children == 0));
213  */
list_empty(const struct list_head * h)214 static inline bool list_empty(const struct list_head *h)
215 {
216 	(void)list_debug(h);
217 	return h->n.next == &h->n;
218 }
219 
220 /**
221  * list_empty_nocheck - is a list empty?
222  * @h: the list_head
223  *
224  * If the list is empty, returns true. This doesn't perform any
225  * debug check for list consistency, so it can be called without
226  * locks, racing with the list being modified. This is ok for
227  * checks where an incorrect result is not an issue (optimized
228  * bail out path for example).
229  */
list_empty_nocheck(const struct list_head * h)230 static inline bool list_empty_nocheck(const struct list_head *h)
231 {
232 	return h->n.next == &h->n;
233 }
234 
235 /**
236  * list_del - delete an entry from an (unknown) linked list.
237  * @n: the list_node to delete from the list.
238  *
239  * Note that this leaves @n in an undefined state; it can be added to
240  * another list, but not deleted again.
241  *
242  * See also:
243  *	list_del_from()
244  *
245  * Example:
246  *	list_del(&child->list);
247  *	parent->num_children--;
248  */
list_del(struct list_node * n)249 static inline void list_del(struct list_node *n)
250 {
251 	(void)list_debug_node(n);
252 	n->next->prev = n->prev;
253 	n->prev->next = n->next;
254 #ifdef CCAN_LIST_DEBUG
255 	/* Catch use-after-del. */
256 	n->next = n->prev = NULL;
257 #endif
258 }
259 
260 /**
261  * list_del_from - delete an entry from a known linked list.
262  * @h: the list_head the node is in.
263  * @n: the list_node to delete from the list.
264  *
265  * This explicitly indicates which list a node is expected to be in,
266  * which is better documentation and can catch more bugs.
267  *
268  * See also: list_del()
269  *
270  * Example:
271  *	list_del_from(&parent->children, &child->list);
272  *	parent->num_children--;
273  */
list_del_from(struct list_head * h,struct list_node * n)274 static inline void list_del_from(struct list_head *h, struct list_node *n)
275 {
276 #ifdef CCAN_LIST_DEBUG
277 	{
278 		/* Thorough check: make sure it was in list! */
279 		struct list_node *i;
280 		for (i = h->n.next; i != n; i = i->next)
281 			assert(i != &h->n);
282 	}
283 #endif /* CCAN_LIST_DEBUG */
284 
285 	/* Quick test that catches a surprising number of bugs. */
286 	assert(!list_empty(h));
287 	list_del(n);
288 }
289 
290 /**
291  * list_entry - convert a list_node back into the structure containing it.
292  * @n: the list_node
293  * @type: the type of the entry
294  * @member: the list_node member of the type
295  *
296  * Example:
297  *	// First list entry is children.next; convert back to child.
298  *	child = list_entry(parent->children.n.next, struct child, list);
299  *
300  * See Also:
301  *	list_top(), list_for_each()
302  */
303 #define list_entry(n, type, member) container_of(n, type, member)
304 
305 /**
306  * list_top - get the first entry in a list
307  * @h: the list_head
308  * @type: the type of the entry
309  * @member: the list_node member of the type
310  *
311  * If the list is empty, returns NULL.
312  *
313  * Example:
314  *	struct child *first;
315  *	first = list_top(&parent->children, struct child, list);
316  *	if (!first)
317  *		printf("Empty list!\n");
318  */
319 #define list_top(h, type, member)					\
320 	((type *)list_top_((h), list_off_(type, member)))
321 
list_top_(const struct list_head * h,size_t off)322 static inline const void *list_top_(const struct list_head *h, size_t off)
323 {
324 	if (list_empty(h))
325 		return NULL;
326 	return (const char *)h->n.next - off;
327 }
328 
329 /**
330  * list_pop - get the first entry in a list and dequeue it
331  * @h: the list_head
332  * @type: the type of the entry
333  * @member: the list_node member of the type
334  */
335 #define list_pop(h, type, member)					\
336 	((type *)list_pop_((h), list_off_(type, member)))
list_pop_(struct list_head * h,size_t off)337 static inline const void *list_pop_(struct list_head *h, size_t off)
338 {
339 	struct list_node *n;
340 
341 	if (list_empty(h))
342 		return NULL;
343 	n = h->n.next;
344 	list_del(n);
345 	return (const char *)n - off;
346 }
347 
348 /**
349  * list_tail - get the last entry in a list
350  * @h: the list_head
351  * @type: the type of the entry
352  * @member: the list_node member of the type
353  *
354  * If the list is empty, returns NULL.
355  *
356  * Example:
357  *	struct child *last;
358  *	last = list_tail(&parent->children, struct child, list);
359  *	if (!last)
360  *		printf("Empty list!\n");
361  */
362 #define list_tail(h, type, member) \
363 	((type *)list_tail_((h), list_off_(type, member)))
364 
list_tail_(const struct list_head * h,size_t off)365 static inline const void *list_tail_(const struct list_head *h, size_t off)
366 {
367 	if (list_empty(h))
368 		return NULL;
369 	return (const char *)h->n.prev - off;
370 }
371 
372 /**
373  * list_for_each - iterate through a list.
374  * @h: the list_head (warning: evaluated multiple times!)
375  * @i: the structure containing the list_node
376  * @member: the list_node member of the structure
377  *
378  * This is a convenient wrapper to iterate @i over the entire list.  It's
379  * a for loop, so you can break and continue as normal.
380  *
381  * Example:
382  *	list_for_each(&parent->children, child, list)
383  *		printf("Name: %s\n", child->name);
384  */
385 #define list_for_each(h, i, member)					\
386 	list_for_each_off(h, i, list_off_var_(i, member))
387 
388 /**
389  * list_for_each_rev - iterate through a list backwards.
390  * @h: the list_head
391  * @i: the structure containing the list_node
392  * @member: the list_node member of the structure
393  *
394  * This is a convenient wrapper to iterate @i over the entire list.  It's
395  * a for loop, so you can break and continue as normal.
396  *
397  * Example:
398  *	list_for_each_rev(&parent->children, child, list)
399  *		printf("Name: %s\n", child->name);
400  */
401 #define list_for_each_rev(h, i, member)					\
402 	for (i = container_of_var(list_debug(h)->n.prev, i, member);	\
403 	     &i->member != &(h)->n;					\
404 	     i = container_of_var(i->member.prev, i, member))
405 
406 /**
407  * list_for_each_safe - iterate through a list, maybe during deletion
408  * @h: the list_head
409  * @i: the structure containing the list_node
410  * @nxt: the structure containing the list_node
411  * @member: the list_node member of the structure
412  *
413  * This is a convenient wrapper to iterate @i over the entire list.  It's
414  * a for loop, so you can break and continue as normal.  The extra variable
415  * @nxt is used to hold the next element, so you can delete @i from the list.
416  *
417  * Example:
418  *	struct child *next;
419  *	list_for_each_safe(&parent->children, child, next, list) {
420  *		list_del(&child->list);
421  *		parent->num_children--;
422  *	}
423  */
424 #define list_for_each_safe(h, i, nxt, member)				\
425 	list_for_each_safe_off(h, i, nxt, list_off_var_(i, member))
426 
427 /**
428  * list_for_each_off - iterate through a list of memory regions.
429  * @h: the list_head
430  * @i: the pointer to a memory region which contains list node data.
431  * @off: offset(relative to @i) at which list node data resides.
432  *
433  * This is a low-level wrapper to iterate @i over the entire list, used to
434  * implement all oher, more high-level, for-each constructs. It's a for loop,
435  * so you can break and continue as normal.
436  *
437  * WARNING! Being the low-level macro that it is, this wrapper doesn't know
438  * nor care about the type of @i. The only assumtion made is that @i points
439  * to a chunk of memory that at some @offset, relative to @i, contains a
440  * properly filled `struct node_list' which in turn contains pointers to
441  * memory chunks and it's turtles all the way down. With all that in mind
442  * remember that given the wrong pointer/offset couple this macro will
443  * happily churn all you memory until SEGFAULT stops it, in other words
444  * caveat emptor.
445  *
446  * It is worth mentioning that one of legitimate use-cases for that wrapper
447  * is operation on opaque types with known offset for `struct list_node'
448  * member(preferably 0), because it allows you not to disclose the type of
449  * @i.
450  *
451  * Example:
452  *	list_for_each_off(&parent->children, child,
453  *				offsetof(struct child, list))
454  *		printf("Name: %s\n", child->name);
455  */
456 #define list_for_each_off(h, i, off)                                    \
457   for (i = list_node_to_off_(list_debug(h)->n.next, (off));             \
458        list_node_from_off_((void *)i, (off)) != &(h)->n;                \
459        i = list_node_to_off_(list_node_from_off_((void *)i, (off))->next, \
460                              (off)))
461 
462 /**
463  * list_for_each_safe_off - iterate through a list of memory regions, maybe
464  * during deletion
465  * @h: the list_head
466  * @i: the pointer to a memory region which contains list node data.
467  * @nxt: the structure containing the list_node
468  * @off: offset(relative to @i) at which list node data resides.
469  *
470  * For details see `list_for_each_off' and `list_for_each_safe'
471  * descriptions.
472  *
473  * Example:
474  *	list_for_each_safe_off(&parent->children, child,
475  *		next, offsetof(struct child, list))
476  *		printf("Name: %s\n", child->name);
477  */
478 #define list_for_each_safe_off(h, i, nxt, off)                          \
479   for (i = list_node_to_off_(list_debug(h)->n.next, (off)),             \
480          nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
481                                  (off));                                \
482        list_node_from_off_(i, (off)) != &(h)->n;                        \
483        i = nxt,                                                         \
484          nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
485                                  (off)))
486 
487 
488 /* Other -off variants. */
489 #define list_entry_off(n, type, off)		\
490 	((type *)list_node_from_off_((n), (off)))
491 
492 #define list_head_off(h, type, off)		\
493 	((type *)list_head_off((h), (off)))
494 
495 #define list_tail_off(h, type, off)		\
496 	((type *)list_tail_((h), (off)))
497 
498 #define list_add_off(h, n, off)                 \
499 	list_add((h), list_node_from_off_((n), (off)))
500 
501 #define list_del_off(n, off)                    \
502 	list_del(list_node_from_off_((n), (off)))
503 
504 #define list_del_from_off(h, n, off)			\
505 	list_del_from(h, list_node_from_off_((n), (off)))
506 
507 /* Offset helper functions so we only single-evaluate. */
list_node_to_off_(struct list_node * node,size_t off)508 static inline void *list_node_to_off_(struct list_node *node, size_t off)
509 {
510 	return (void *)((char *)node - off);
511 }
list_node_from_off_(void * ptr,size_t off)512 static inline struct list_node *list_node_from_off_(void *ptr, size_t off)
513 {
514 	return (struct list_node *)((char *)ptr + off);
515 }
516 
517 /* Get the offset of the member, but make sure it's a list_node. */
518 #define list_off_(type, member)					\
519 	(container_off(type, member) +				\
520 	 check_type(((type *)0)->member, struct list_node))
521 
522 #define list_off_var_(var, member)			\
523 	(container_off_var(var, member) +		\
524 	 check_type(var->member, struct list_node))
525 
526 #endif /* CCAN_LIST_H */
527