list.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef CCAN_LIST_H
  2. #define CCAN_LIST_H
  3. #include <stdbool.h>
  4. #include "container_of/container_of.h"
  5. /**
  6. * struct list_node - an entry in a doubly-linked list
  7. * @next: next entry (self if empty)
  8. * @prev: previous entry (self if empty)
  9. *
  10. * This is used as an entry in a linked list.
  11. * Example:
  12. * struct child {
  13. * const char *name;
  14. * // Linked list of all us children.
  15. * struct list_node list;
  16. * };
  17. */
  18. struct list_node
  19. {
  20. struct list_node *next, *prev;
  21. };
  22. /**
  23. * struct list_head - the head of a doubly-linked list
  24. * @h: the list_head (containing next and prev pointers)
  25. *
  26. * This is used as the head of a linked list.
  27. * Example:
  28. * struct parent {
  29. * const char *name;
  30. * struct list_head children;
  31. * unsigned int num_children;
  32. * };
  33. */
  34. struct list_head
  35. {
  36. struct list_node n;
  37. };
  38. /**
  39. * list_check - check a list for consistency
  40. * @h: the list_head
  41. * @abortstr: the location to print on aborting, or NULL.
  42. *
  43. * Because list_nodes have redundant information, consistency checking between
  44. * the back and forward links can be done. This is useful as a debugging check.
  45. * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
  46. * is inconsistent, and the function will abort.
  47. *
  48. * Returns the list head if the list is consistent, NULL if not (it
  49. * can never return NULL if @abortstr is set).
  50. *
  51. * Example:
  52. * static void dump_parent(struct parent *p)
  53. * {
  54. * struct child *c;
  55. *
  56. * printf("%s (%u children):\n", p->name, parent->num_children);
  57. * list_check(&p->children, "bad child list");
  58. * list_for_each(&p->children, c, list)
  59. * printf(" -> %s\n", c->name);
  60. * }
  61. */
  62. struct list_head *list_check(struct list_head *h, const char *abortstr);
  63. #ifdef CCAN_LIST_DEBUG
  64. #define debug_list(h) list_check((h), __func__)
  65. #else
  66. #define debug_list(h) (h)
  67. #endif
  68. /**
  69. * list_head_init - initialize a list_head
  70. * @h: the list_head to set to the empty list
  71. *
  72. * Example:
  73. * list_head_init(&parent->children);
  74. * parent->num_children = 0;
  75. */
  76. static inline void list_head_init(struct list_head *h)
  77. {
  78. h->n.next = h->n.prev = &h->n;
  79. }
  80. /**
  81. * LIST_HEAD - define and initalized empty list_head
  82. * @name: the name of the list.
  83. *
  84. * The LIST_HEAD macro defines a list_head and initializes it to an empty
  85. * list. It can be prepended by "static" to define a static list_head.
  86. *
  87. * Example:
  88. * // Header:
  89. * extern struct list_head my_list;
  90. *
  91. * // C file:
  92. * LIST_HEAD(my_list);
  93. */
  94. #define LIST_HEAD(name) \
  95. struct list_head name = { { &name.n, &name.n } }
  96. /**
  97. * list_add - add an entry at the start of a linked list.
  98. * @h: the list_head to add the node to
  99. * @n: the list_node to add to the list.
  100. *
  101. * The list_node does not need to be initialized; it will be overwritten.
  102. * Example:
  103. * list_add(&parent->children, &child->list);
  104. * parent->num_children++;
  105. */
  106. static inline void list_add(struct list_head *h, struct list_node *n)
  107. {
  108. n->next = h->n.next;
  109. n->prev = &h->n;
  110. h->n.next->prev = n;
  111. h->n.next = n;
  112. (void)debug_list(h);
  113. }
  114. /**
  115. * list_add_tail - add an entry at the end of a linked list.
  116. * @h: the list_head to add the node to
  117. * @n: the list_node to add to the list.
  118. *
  119. * The list_node does not need to be initialized; it will be overwritten.
  120. * Example:
  121. * list_add_tail(&parent->children, &child->list);
  122. * parent->num_children++;
  123. */
  124. static inline void list_add_tail(struct list_head *h, struct list_node *n)
  125. {
  126. n->next = &h->n;
  127. n->prev = h->n.prev;
  128. h->n.prev->next = n;
  129. h->n.prev = n;
  130. (void)debug_list(h);
  131. }
  132. /**
  133. * list_del - delete an entry from a linked list.
  134. * @n: the list_node to delete from the list.
  135. *
  136. * Example:
  137. * list_del(&child->list);
  138. * parent->num_children--;
  139. */
  140. static inline void list_del(struct list_node *n)
  141. {
  142. n->next->prev = n->prev;
  143. n->prev->next = n->next;
  144. (void)debug_list(n->next);
  145. #ifdef CCAN_LIST_DEBUG
  146. /* Catch use-after-del. */
  147. n->next = n->prev = NULL;
  148. #endif
  149. }
  150. /**
  151. * list_empty - is a list empty?
  152. * @h: the list_head
  153. *
  154. * If the list is empty, returns true.
  155. *
  156. * Example:
  157. * assert(list_empty(&parent->children) == (parent->num_children == 0));
  158. */
  159. static inline bool list_empty(struct list_head *h)
  160. {
  161. (void)debug_list(h);
  162. return h->n.next == &h->n;
  163. }
  164. /**
  165. * list_entry - convert a list_node back into the structure containing it.
  166. * @n: the list_node
  167. * @type: the type of the entry
  168. * @member: the list_node member of the type
  169. *
  170. * Example:
  171. * struct child *c;
  172. * // First list entry is children.next; convert back to child.
  173. * c = list_entry(parent->children.next, struct child, list);
  174. */
  175. #define list_entry(n, type, member) container_of(n, type, member)
  176. /**
  177. * list_top - get the first entry in a list
  178. * @h: the list_head
  179. * @type: the type of the entry
  180. * @member: the list_node member of the type
  181. *
  182. * If the list is empty, returns NULL.
  183. *
  184. * Example:
  185. * struct child *first;
  186. * first = list_top(&parent->children, struct child, list);
  187. */
  188. #define list_top(h, type, member) \
  189. list_entry(_list_top(h), type, member)
  190. static inline struct list_node *_list_top(struct list_head *h)
  191. {
  192. (void)debug_list(h);
  193. if (list_empty(h))
  194. return NULL;
  195. return h->n.next;
  196. }
  197. /**
  198. * list_for_each - iterate through a list.
  199. * @h: the list_head
  200. * @i: the structure containing the list_node
  201. * @member: the list_node member of the structure
  202. *
  203. * This is a convenient wrapper to iterate @i over the entire list. It's
  204. * a for loop, so you can break and continue as normal.
  205. *
  206. * Example:
  207. * struct child *c;
  208. * list_for_each(&parent->children, c, list)
  209. * printf("Name: %s\n", c->name);
  210. */
  211. #define list_for_each(h, i, member) \
  212. for (i = container_of_var(debug_list(h)->n.next, i, member); \
  213. &i->member != &(h)->n; \
  214. i = container_of_var(i->member.next, i, member))
  215. /**
  216. * list_for_each_safe - iterate through a list, maybe during deletion
  217. * @h: the list_head
  218. * @i: the structure containing the list_node
  219. * @nxt: the structure containing the list_node
  220. * @member: the list_node member of the structure
  221. *
  222. * This is a convenient wrapper to iterate @i over the entire list. It's
  223. * a for loop, so you can break and continue as normal. The extra variable
  224. * @nxt is used to hold the next element, so you can delete @i from the list.
  225. *
  226. * Example:
  227. * struct child *c, *n;
  228. * list_for_each_safe(&parent->children, c, n, list) {
  229. * list_del(&c->list);
  230. * parent->num_children--;
  231. * }
  232. */
  233. #define list_for_each_safe(h, i, nxt, member) \
  234. for (i = container_of_var(debug_list(h)->n.next, i, member), \
  235. nxt = container_of_var(i->member.next, i, member); \
  236. &i->member != &(h)->n; \
  237. i = nxt, nxt = container_of_var(i->member.next, i, member))
  238. #endif /* CCAN_LIST_H */