1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
6 #include <ccan/container_of/container_of.h>
7 #include <ccan/check_type/check_type.h>
10 * struct ccan_list_node - an entry in a doubly-linked list
11 * @next: next entry (self if empty)
12 * @prev: previous entry (self if empty)
14 * This is used as an entry in a linked list.
18 * // Linked list of all us children.
19 * struct ccan_list_node list;
24 struct ccan_list_node
*next
, *prev
;
28 * struct ccan_list_head - the head of a doubly-linked list
29 * @h: the ccan_list_head (containing next and prev pointers)
31 * This is used as the head of a linked list.
35 * struct ccan_list_head children;
36 * unsigned int num_children;
41 struct ccan_list_node n
;
45 * ccan_list_check - check head of a list for consistency
46 * @h: the ccan_list_head
47 * @abortstr: the location to print on aborting, or NULL.
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.
54 * Returns the list head if the list is consistent, NULL if not (it
55 * can never return NULL if @abortstr is set).
57 * See also: ccan_list_check_node()
60 * static void dump_parent(struct parent *p)
64 * printf("%s (%u children):\n", p->name, p->num_children);
65 * ccan_list_check(&p->children, "bad child list");
66 * ccan_list_for_each(&p->children, c, list)
67 * printf(" -> %s\n", c->name);
70 struct ccan_list_head
*ccan_list_check(const struct ccan_list_head
*h
, const char *abortstr
);
73 * ccan_list_check_node - check node of a list for consistency
74 * @n: the ccan_list_node
75 * @abortstr: the location to print on aborting, or NULL.
77 * Check consistency of the list node is in (it must be in one).
79 * See also: ccan_list_check()
82 * static void dump_child(const struct child *c)
84 * ccan_list_check_node(&c->list, "bad child list");
85 * printf("%s\n", c->name);
88 struct ccan_list_node
*ccan_list_check_node(const struct ccan_list_node
*n
,
89 const char *abortstr
);
91 #ifdef CCAN_LIST_DEBUG
92 #define ccan_list_debug(h) ccan_list_check((h), __func__)
93 #define ccan_list_debug_node(n) ccan_list_check_node((n), __func__)
95 #define ccan_list_debug(h) (h)
96 #define ccan_list_debug_node(n) (n)
100 * CCAN_LIST_HEAD_INIT - initializer for an empty ccan_list_head
101 * @name: the name of the list.
103 * Explicit initializer for an empty list.
106 * CCAN_LIST_HEAD, ccan_list_head_init()
109 * static struct ccan_list_head my_list = CCAN_LIST_HEAD_INIT(my_list);
111 #define CCAN_LIST_HEAD_INIT(name) { { &name.n, &name.n } }
114 * CCAN_LIST_HEAD - define and initialize an empty ccan_list_head
115 * @name: the name of the list.
117 * The CCAN_LIST_HEAD macro defines a ccan_list_head and initializes it to an empty
118 * list. It can be prepended by "static" to define a static ccan_list_head.
121 * CCAN_LIST_HEAD_INIT, ccan_list_head_init()
124 * static CCAN_LIST_HEAD(my_global_list);
126 #define CCAN_LIST_HEAD(name) \
127 struct ccan_list_head name = CCAN_LIST_HEAD_INIT(name)
130 * ccan_list_head_init - initialize a ccan_list_head
131 * @h: the ccan_list_head to set to the empty list
135 * struct parent *parent = malloc(sizeof(*parent));
137 * ccan_list_head_init(&parent->children);
138 * parent->num_children = 0;
140 static inline void ccan_list_head_init(struct ccan_list_head
*h
)
142 h
->n
.next
= h
->n
.prev
= &h
->n
;
146 * ccan_list_add - add an entry at the start of a linked list.
147 * @h: the ccan_list_head to add the node to
148 * @n: the ccan_list_node to add to the list.
150 * The ccan_list_node does not need to be initialized; it will be overwritten.
152 * struct child *child = malloc(sizeof(*child));
154 * child->name = "marvin";
155 * ccan_list_add(&parent->children, &child->list);
156 * parent->num_children++;
158 static inline void ccan_list_add(struct ccan_list_head
*h
, struct ccan_list_node
*n
)
164 (void)ccan_list_debug(h
);
168 * ccan_list_add_tail - add an entry at the end of a linked list.
169 * @h: the ccan_list_head to add the node to
170 * @n: the ccan_list_node to add to the list.
172 * The ccan_list_node does not need to be initialized; it will be overwritten.
174 * ccan_list_add_tail(&parent->children, &child->list);
175 * parent->num_children++;
177 static inline void ccan_list_add_tail(struct ccan_list_head
*h
, struct ccan_list_node
*n
)
183 (void)ccan_list_debug(h
);
187 * ccan_list_empty - is a list empty?
188 * @h: the ccan_list_head
190 * If the list is empty, returns true.
193 * assert(ccan_list_empty(&parent->children) == (parent->num_children == 0));
195 static inline bool ccan_list_empty(const struct ccan_list_head
*h
)
197 (void)ccan_list_debug(h
);
198 return h
->n
.next
== &h
->n
;
202 * ccan_list_del - delete an entry from an (unknown) linked list.
203 * @n: the ccan_list_node to delete from the list.
205 * Note that this leaves @n in an undefined state; it can be added to
206 * another list, but not deleted again.
209 * ccan_list_del_from()
212 * ccan_list_del(&child->list);
213 * parent->num_children--;
215 static inline void ccan_list_del(struct ccan_list_node
*n
)
217 (void)ccan_list_debug_node(n
);
218 n
->next
->prev
= n
->prev
;
219 n
->prev
->next
= n
->next
;
220 #ifdef CCAN_LIST_DEBUG
221 /* Catch use-after-del. */
222 n
->next
= n
->prev
= NULL
;
227 * ccan_list_del_from - delete an entry from a known linked list.
228 * @h: the ccan_list_head the node is in.
229 * @n: the ccan_list_node to delete from the list.
231 * This explicitly indicates which list a node is expected to be in,
232 * which is better documentation and can catch more bugs.
234 * See also: ccan_list_del()
237 * ccan_list_del_from(&parent->children, &child->list);
238 * parent->num_children--;
240 static inline void ccan_list_del_from(struct ccan_list_head
*h
, struct ccan_list_node
*n
)
242 #ifdef CCAN_LIST_DEBUG
244 /* Thorough check: make sure it was in list! */
245 struct ccan_list_node
*i
;
246 for (i
= h
->n
.next
; i
!= n
; i
= i
->next
)
249 #endif /* CCAN_LIST_DEBUG */
251 /* Quick test that catches a surprising number of bugs. */
252 assert(!ccan_list_empty(h
));
257 * ccan_list_entry - convert a ccan_list_node back into the structure containing it.
258 * @n: the ccan_list_node
259 * @type: the type of the entry
260 * @member: the ccan_list_node member of the type
263 * // First list entry is children.next; convert back to child.
264 * child = ccan_list_entry(parent->children.n.next, struct child, list);
267 * ccan_list_top(), ccan_list_for_each()
269 #define ccan_list_entry(n, type, member) container_of(n, type, member)
272 * ccan_list_top - get the first entry in a list
273 * @h: the ccan_list_head
274 * @type: the type of the entry
275 * @member: the ccan_list_node member of the type
277 * If the list is empty, returns NULL.
280 * struct child *first;
281 * first = ccan_list_top(&parent->children, struct child, list);
283 #define ccan_list_top(h, type, member) \
284 ((type *)ccan_list_top_((h), ccan_list_off_(type, member)))
286 static inline const void *ccan_list_top_(const struct ccan_list_head
*h
, size_t off
)
288 if (ccan_list_empty(h
))
290 return (const char *)h
->n
.next
- off
;
294 * ccan_list_tail - get the last entry in a list
295 * @h: the ccan_list_head
296 * @type: the type of the entry
297 * @member: the ccan_list_node member of the type
299 * If the list is empty, returns NULL.
302 * struct child *last;
303 * last = ccan_list_tail(&parent->children, struct child, list);
305 #define ccan_list_tail(h, type, member) \
306 ((type *)ccan_list_tail_((h), ccan_list_off_(type, member)))
308 static inline const void *ccan_list_tail_(const struct ccan_list_head
*h
, size_t off
)
310 if (ccan_list_empty(h
))
312 return (const char *)h
->n
.prev
- off
;
316 * ccan_list_for_each - iterate through a list.
317 * @h: the ccan_list_head (warning: evaluated multiple times!)
318 * @i: the structure containing the ccan_list_node
319 * @member: the ccan_list_node member of the structure
321 * This is a convenient wrapper to iterate @i over the entire list. It's
322 * a for loop, so you can break and continue as normal.
325 * ccan_list_for_each(&parent->children, child, list)
326 * printf("Name: %s\n", child->name);
328 #define ccan_list_for_each(h, i, member) \
329 ccan_list_for_each_off(h, i, ccan_list_off_var_(i, member))
332 * ccan_list_for_each_rev - iterate through a list backwards.
333 * @h: the ccan_list_head
334 * @i: the structure containing the ccan_list_node
335 * @member: the ccan_list_node member of the structure
337 * This is a convenient wrapper to iterate @i over the entire list. It's
338 * a for loop, so you can break and continue as normal.
341 * ccan_list_for_each_rev(&parent->children, child, list)
342 * printf("Name: %s\n", child->name);
344 #define ccan_list_for_each_rev(h, i, member) \
345 for (i = container_of_var(ccan_list_debug(h)->n.prev, i, member); \
346 &i->member != &(h)->n; \
347 i = container_of_var(i->member.prev, i, member))
350 * ccan_list_for_each_safe - iterate through a list, maybe during deletion
351 * @h: the ccan_list_head
352 * @i: the structure containing the ccan_list_node
353 * @nxt: the structure containing the ccan_list_node
354 * @member: the ccan_list_node member of the structure
356 * This is a convenient wrapper to iterate @i over the entire list. It's
357 * a for loop, so you can break and continue as normal. The extra variable
358 * @nxt is used to hold the next element, so you can delete @i from the list.
361 * struct child *next;
362 * ccan_list_for_each_safe(&parent->children, child, next, list) {
363 * ccan_list_del(&child->list);
364 * parent->num_children--;
367 #define ccan_list_for_each_safe(h, i, nxt, member) \
368 ccan_list_for_each_safe_off(h, i, nxt, ccan_list_off_var_(i, member))
371 * ccan_list_for_each_off - iterate through a list of memory regions.
372 * @h: the ccan_list_head
373 * @i: the pointer to a memory region wich contains list node data.
374 * @off: offset(relative to @i) at which list node data resides.
376 * This is a low-level wrapper to iterate @i over the entire list, used to
377 * implement all oher, more high-level, for-each constructs. It's a for loop,
378 * so you can break and continue as normal.
380 * WARNING! Being the low-level macro that it is, this wrapper doesn't know
381 * nor care about the type of @i. The only assumtion made is that @i points
382 * to a chunk of memory that at some @offset, relative to @i, contains a
383 * properly filled `struct node_list' which in turn contains pointers to
384 * memory chunks and it's turtles all the way down. Whith all that in mind
385 * remember that given the wrong pointer/offset couple this macro will
386 * happilly churn all you memory untill SEGFAULT stops it, in other words
389 * It is worth mentioning that one of legitimate use-cases for that wrapper
390 * is operation on opaque types with known offset for `struct ccan_list_node'
391 * member(preferably 0), because it allows you not to disclose the type of
395 * ccan_list_for_each_off(&parent->children, child,
396 * offsetof(struct child, list))
397 * printf("Name: %s\n", child->name);
399 #define ccan_list_for_each_off(h, i, off) \
400 for (i = ccan_list_node_to_off_(ccan_list_debug(h)->n.next, (off)); \
401 ccan_list_node_from_off_((void *)i, (off)) != &(h)->n; \
402 i = ccan_list_node_to_off_(ccan_list_node_from_off_((void *)i, (off))->next, \
406 * ccan_list_for_each_safe_off - iterate through a list of memory regions, maybe
408 * @h: the ccan_list_head
409 * @i: the pointer to a memory region wich contains list node data.
410 * @nxt: the structure containing the ccan_list_node
411 * @off: offset(relative to @i) at which list node data resides.
413 * For details see `ccan_list_for_each_off' and `ccan_list_for_each_safe'
417 * ccan_list_for_each_safe_off(&parent->children, child,
418 * next, offsetof(struct child, list))
419 * printf("Name: %s\n", child->name);
421 #define ccan_list_for_each_safe_off(h, i, nxt, off) \
422 for (i = ccan_list_node_to_off_(ccan_list_debug(h)->n.next, (off)), \
423 nxt = ccan_list_node_to_off_(ccan_list_node_from_off_(i, (off))->next, \
425 ccan_list_node_from_off_(i, (off)) != &(h)->n; \
427 nxt = ccan_list_node_to_off_(ccan_list_node_from_off_(i, (off))->next, \
431 /* Other -off variants. */
432 #define ccan_list_entry_off(n, type, off) \
433 ((type *)ccan_list_node_from_off_((n), (off)))
435 #define ccan_list_head_off(h, type, off) \
436 ((type *)ccan_list_head_off((h), (off)))
438 #define ccan_list_tail_off(h, type, off) \
439 ((type *)ccan_list_tail_((h), (off)))
441 #define ccan_list_add_off(h, n, off) \
442 ccan_list_add((h), ccan_list_node_from_off_((n), (off)))
444 #define ccan_list_del_off(n, off) \
445 ccan_list_del(ccan_list_node_from_off_((n), (off)))
447 #define ccan_list_del_from_off(h, n, off) \
448 ccan_list_del_from(h, ccan_list_node_from_off_((n), (off)))
450 /* Offset helper functions so we only single-evaluate. */
451 static inline void *ccan_list_node_to_off_(struct ccan_list_node
*node
, size_t off
)
453 return (void *)((char *)node
- off
);
455 static inline struct ccan_list_node
*ccan_list_node_from_off_(void *ptr
, size_t off
)
457 return (struct ccan_list_node
*)((char *)ptr
+ off
);
460 /* Get the offset of the member, but make sure it's a ccan_list_node. */
461 #define ccan_list_off_(type, member) \
462 (container_off(type, member) + \
463 check_type(((type *)0)->member, struct ccan_list_node))
465 #define ccan_list_off_var_(var, member) \
466 (container_off_var(var, member) + \
467 check_type(var->member, struct ccan_list_node))
469 #endif /* CCAN_LIST_H */