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