list.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef LIST_H
  3. #define LIST_H
  4. #include <stddef.h>
  5. #include "list_types.h"
  6. /* Are two types/vars the same type (ignoring qualifiers)? */
  7. #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  8. /**
  9. * container_of - cast a member of a structure out to the containing structure
  10. * @ptr: the pointer to the member.
  11. * @type: the type of the container struct this is embedded in.
  12. * @member: the name of the member within the struct.
  13. *
  14. */
  15. #define container_of(ptr, type, member) ({ \
  16. void *__mptr = (void *)(ptr); \
  17. _Static_assert(__same_type(*(ptr), ((type *)0)->member) || \
  18. __same_type(*(ptr), void), \
  19. "pointer type mismatch in container_of()"); \
  20. ((type *)(__mptr - offsetof(type, member))); })
  21. #define LIST_POISON1 ((void *) 0x100)
  22. #define LIST_POISON2 ((void *) 0x122)
  23. /*
  24. * Circular doubly linked list implementation.
  25. *
  26. * Some of the internal functions ("__xxx") are useful when
  27. * manipulating whole lists rather than single entries, as
  28. * sometimes we already know the next/prev entries and we can
  29. * generate better code by using them directly rather than
  30. * using the generic single-entry routines.
  31. */
  32. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  33. #define LIST_HEAD(name) \
  34. struct list_head name = LIST_HEAD_INIT(name)
  35. /**
  36. * INIT_LIST_HEAD - Initialize a list_head structure
  37. * @list: list_head structure to be initialized.
  38. *
  39. * Initializes the list_head to point to itself. If it is a list header,
  40. * the result is an empty list.
  41. */
  42. static inline void INIT_LIST_HEAD(struct list_head *list)
  43. {
  44. list->next = list;
  45. list->prev = list;
  46. }
  47. /*
  48. * Insert a new entry between two known consecutive entries.
  49. *
  50. * This is only for internal list manipulation where we know
  51. * the prev/next entries already!
  52. */
  53. static inline void __list_add(struct list_head *new,
  54. struct list_head *prev,
  55. struct list_head *next)
  56. {
  57. next->prev = new;
  58. new->next = next;
  59. new->prev = prev;
  60. prev->next = new;
  61. }
  62. /**
  63. * list_add - add a new entry
  64. * @new: new entry to be added
  65. * @head: list head to add it after
  66. *
  67. * Insert a new entry after the specified head.
  68. * This is good for implementing stacks.
  69. */
  70. static inline void list_add(struct list_head *new, struct list_head *head)
  71. {
  72. __list_add(new, head, head->next);
  73. }
  74. /**
  75. * list_add_tail - add a new entry
  76. * @new: new entry to be added
  77. * @head: list head to add it before
  78. *
  79. * Insert a new entry before the specified head.
  80. * This is useful for implementing queues.
  81. */
  82. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  83. {
  84. __list_add(new, head->prev, head);
  85. }
  86. /*
  87. * Delete a list entry by making the prev/next entries
  88. * point to each other.
  89. *
  90. * This is only for internal list manipulation where we know
  91. * the prev/next entries already!
  92. */
  93. static inline void __list_del(struct list_head *prev, struct list_head *next)
  94. {
  95. next->prev = prev;
  96. prev->next = next;
  97. }
  98. static inline void __list_del_entry(struct list_head *entry)
  99. {
  100. __list_del(entry->prev, entry->next);
  101. }
  102. /**
  103. * list_del - deletes entry from list.
  104. * @entry: the element to delete from the list.
  105. * Note: list_empty() on entry does not return true after this, the entry is
  106. * in an undefined state.
  107. */
  108. static inline void list_del(struct list_head *entry)
  109. {
  110. __list_del_entry(entry);
  111. entry->next = LIST_POISON1;
  112. entry->prev = LIST_POISON2;
  113. }
  114. /**
  115. * list_replace - replace old entry by new one
  116. * @old : the element to be replaced
  117. * @new : the new element to insert
  118. *
  119. * If @old was empty, it will be overwritten.
  120. */
  121. static inline void list_replace(struct list_head *old,
  122. struct list_head *new)
  123. {
  124. new->next = old->next;
  125. new->next->prev = new;
  126. new->prev = old->prev;
  127. new->prev->next = new;
  128. }
  129. /**
  130. * list_replace_init - replace old entry by new one and initialize the old one
  131. * @old : the element to be replaced
  132. * @new : the new element to insert
  133. *
  134. * If @old was empty, it will be overwritten.
  135. */
  136. static inline void list_replace_init(struct list_head *old,
  137. struct list_head *new)
  138. {
  139. list_replace(old, new);
  140. INIT_LIST_HEAD(old);
  141. }
  142. /**
  143. * list_move - delete from one list and add as another's head
  144. * @list: the entry to move
  145. * @head: the head that will precede our entry
  146. */
  147. static inline void list_move(struct list_head *list, struct list_head *head)
  148. {
  149. __list_del_entry(list);
  150. list_add(list, head);
  151. }
  152. /**
  153. * list_move_tail - delete from one list and add as another's tail
  154. * @list: the entry to move
  155. * @head: the head that will follow our entry
  156. */
  157. static inline void list_move_tail(struct list_head *list,
  158. struct list_head *head)
  159. {
  160. __list_del_entry(list);
  161. list_add_tail(list, head);
  162. }
  163. /**
  164. * list_is_first -- tests whether @list is the first entry in list @head
  165. * @list: the entry to test
  166. * @head: the head of the list
  167. */
  168. static inline int list_is_first(const struct list_head *list, const struct list_head *head)
  169. {
  170. return list->prev == head;
  171. }
  172. /**
  173. * list_is_last - tests whether @list is the last entry in list @head
  174. * @list: the entry to test
  175. * @head: the head of the list
  176. */
  177. static inline int list_is_last(const struct list_head *list, const struct list_head *head)
  178. {
  179. return list->next == head;
  180. }
  181. /**
  182. * list_is_head - tests whether @list is the list @head
  183. * @list: the entry to test
  184. * @head: the head of the list
  185. */
  186. static inline int list_is_head(const struct list_head *list, const struct list_head *head)
  187. {
  188. return list == head;
  189. }
  190. /**
  191. * list_empty - tests whether a list is empty
  192. * @head: the list to test.
  193. */
  194. static inline int list_empty(const struct list_head *head)
  195. {
  196. return head->next == head;
  197. }
  198. /**
  199. * list_entry - get the struct for this entry
  200. * @ptr: the &struct list_head pointer.
  201. * @type: the type of the struct this is embedded in.
  202. * @member: the name of the list_head within the struct.
  203. */
  204. #define list_entry(ptr, type, member) \
  205. container_of(ptr, type, member)
  206. /**
  207. * list_first_entry - get the first element from a list
  208. * @ptr: the list head to take the element from.
  209. * @type: the type of the struct this is embedded in.
  210. * @member: the name of the list_head within the struct.
  211. *
  212. * Note, that list is expected to be not empty.
  213. */
  214. #define list_first_entry(ptr, type, member) \
  215. list_entry((ptr)->next, type, member)
  216. /**
  217. * list_last_entry - get the last element from a list
  218. * @ptr: the list head to take the element from.
  219. * @type: the type of the struct this is embedded in.
  220. * @member: the name of the list_head within the struct.
  221. *
  222. * Note, that list is expected to be not empty.
  223. */
  224. #define list_last_entry(ptr, type, member) \
  225. list_entry((ptr)->prev, type, member)
  226. /**
  227. * list_next_entry - get the next element in list
  228. * @pos: the type * to cursor
  229. * @member: the name of the list_head within the struct.
  230. */
  231. #define list_next_entry(pos, member) \
  232. list_entry((pos)->member.next, typeof(*(pos)), member)
  233. /**
  234. * list_prev_entry - get the prev element in list
  235. * @pos: the type * to cursor
  236. * @member: the name of the list_head within the struct.
  237. */
  238. #define list_prev_entry(pos, member) \
  239. list_entry((pos)->member.prev, typeof(*(pos)), member)
  240. /**
  241. * list_entry_is_head - test if the entry points to the head of the list
  242. * @pos: the type * to cursor
  243. * @head: the head for your list.
  244. * @member: the name of the list_head within the struct.
  245. */
  246. #define list_entry_is_head(pos, head, member) \
  247. (&pos->member == (head))
  248. /**
  249. * list_for_each_entry - iterate over list of given type
  250. * @pos: the type * to use as a loop cursor.
  251. * @head: the head for your list.
  252. * @member: the name of the list_head within the struct.
  253. */
  254. #define list_for_each_entry(pos, head, member) \
  255. for (pos = list_first_entry(head, typeof(*pos), member); \
  256. !list_entry_is_head(pos, head, member); \
  257. pos = list_next_entry(pos, member))
  258. /**
  259. * list_for_each_entry_reverse - iterate backwards over list of given type.
  260. * @pos: the type * to use as a loop cursor.
  261. * @head: the head for your list.
  262. * @member: the name of the list_head within the struct.
  263. */
  264. #define list_for_each_entry_reverse(pos, head, member) \
  265. for (pos = list_last_entry(head, typeof(*pos), member); \
  266. !list_entry_is_head(pos, head, member); \
  267. pos = list_prev_entry(pos, member))
  268. /**
  269. * list_for_each_entry_safe - iterate over list of given type. Safe against removal of list entry
  270. * @pos: the type * to use as a loop cursor.
  271. * @n: another type * to use as temporary storage
  272. * @head: the head for your list.
  273. * @member: the name of the list_head within the struct.
  274. */
  275. #define list_for_each_entry_safe(pos, n, head, member) \
  276. for (pos = list_first_entry(head, typeof(*pos), member), \
  277. n = list_next_entry(pos, member); \
  278. !list_entry_is_head(pos, head, member); \
  279. pos = n, n = list_next_entry(n, member))
  280. /*
  281. * Double linked lists with a single pointer list head.
  282. * Mostly useful for hash tables where the two pointer list head is
  283. * too wasteful.
  284. * You lose the ability to access the tail in O(1).
  285. */
  286. #define HLIST_HEAD_INIT { .first = NULL }
  287. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  288. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  289. {
  290. h->next = NULL;
  291. h->pprev = NULL;
  292. }
  293. /**
  294. * hlist_unhashed - Has node been removed from list and reinitialized?
  295. * @h: Node to be checked
  296. *
  297. * Not that not all removal functions will leave a node in unhashed
  298. * state. For example, hlist_nulls_del_init_rcu() does leave the
  299. * node in unhashed state, but hlist_nulls_del() does not.
  300. */
  301. static inline int hlist_unhashed(const struct hlist_node *h)
  302. {
  303. return !h->pprev;
  304. }
  305. static inline void __hlist_del(struct hlist_node *n)
  306. {
  307. struct hlist_node *next = n->next;
  308. struct hlist_node **pprev = n->pprev;
  309. *pprev = next;
  310. if (next)
  311. next->pprev = pprev;
  312. }
  313. /**
  314. * hlist_del - Delete the specified hlist_node from its list
  315. * @n: Node to delete.
  316. *
  317. * Note that this function leaves the node in hashed state. Use
  318. * hlist_del_init() or similar instead to unhash @n.
  319. */
  320. static inline void hlist_del(struct hlist_node *n)
  321. {
  322. __hlist_del(n);
  323. n->next = LIST_POISON1;
  324. n->pprev = LIST_POISON2;
  325. }
  326. /**
  327. * hlist_del_init - Delete the specified hlist_node from its list and initialize
  328. * @n: Node to delete.
  329. *
  330. * Note that this function leaves the node in unhashed state.
  331. */
  332. static inline void hlist_del_init(struct hlist_node *n)
  333. {
  334. if (!hlist_unhashed(n)) {
  335. __hlist_del(n);
  336. INIT_HLIST_NODE(n);
  337. }
  338. }
  339. /**
  340. * hlist_add_head - add a new entry at the beginning of the hlist
  341. * @n: new entry to be added
  342. * @h: hlist head to add it after
  343. *
  344. * Insert a new entry after the specified head.
  345. * This is good for implementing stacks.
  346. */
  347. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  348. {
  349. struct hlist_node *first = h->first;
  350. n->next = first;
  351. if (first)
  352. first->pprev = &n->next;
  353. h->first = n;
  354. n->pprev = &h->first;
  355. }
  356. #define hlist_entry(ptr, type, member) container_of(ptr, type, member)
  357. #define hlist_entry_safe(ptr, type, member) \
  358. ({ typeof(ptr) ____ptr = (ptr); \
  359. ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
  360. })
  361. /**
  362. * hlist_for_each_entry - iterate over list of given type
  363. * @pos: the type * to use as a loop cursor.
  364. * @head: the head for your list.
  365. * @member: the name of the hlist_node within the struct.
  366. */
  367. #define hlist_for_each_entry(pos, head, member) \
  368. for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
  369. pos; \
  370. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  371. /**
  372. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  373. * @pos: the type * to use as a loop cursor.
  374. * @n: a &struct hlist_node to use as temporary storage
  375. * @head: the head for your list.
  376. * @member: the name of the hlist_node within the struct.
  377. */
  378. #define hlist_for_each_entry_safe(pos, n, head, member) \
  379. for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
  380. pos && ({ n = pos->member.next; 1; }); \
  381. pos = hlist_entry_safe(n, typeof(*pos), member))
  382. #endif /* LIST_H */