list.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright (c) 2009-2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. *
  20. *
  21. * Doubly-linked list.
  22. */
  23. #ifndef KERN_LIST_H
  24. #define KERN_LIST_H
  25. #include <stdbool.h>
  26. #include <stddef.h>
  27. #include <kern/list_types.h>
  28. #include <kern/macros.h>
  29. #include <kern/rcu.h>
  30. /*
  31. * Structure used as both head and node.
  32. *
  33. * This implementation relies on using the same type for both heads and nodes.
  34. *
  35. * It is recommended to encode the use of struct list variables in their names,
  36. * e.g. struct list free_list or struct list free_objects is a good hint for a
  37. * list of free objects. A declaration like struct list free_node clearly
  38. * indicates it is used as part of a node in the free list.
  39. */
  40. struct list;
  41. // Static list initializer.
  42. #define LIST_INITIALIZER(list) { &(list), &(list) }
  43. // Initialize a list.
  44. static inline void
  45. list_init (struct list *list)
  46. {
  47. list->prev = list;
  48. list->next = list;
  49. }
  50. /*
  51. * Initialize a list node.
  52. *
  53. * A node is in no list when its node members point to NULL.
  54. */
  55. static inline void
  56. list_node_init (struct list *node)
  57. {
  58. node->prev = NULL;
  59. node->next = NULL;
  60. }
  61. // Return true if node is in no list.
  62. static inline bool
  63. list_node_unlinked (const struct list *node)
  64. {
  65. return (!node->prev);
  66. }
  67. // Return the first node of a list.
  68. static inline struct list*
  69. list_first (const struct list *list)
  70. {
  71. return (list->next);
  72. }
  73. // Return the last node of a list.
  74. static inline struct list*
  75. list_last (const struct list *list)
  76. {
  77. return (list->prev);
  78. }
  79. // Return the node next to the given node.
  80. static inline struct list*
  81. list_next (const struct list *node)
  82. {
  83. return (node->next);
  84. }
  85. // Return the node previous to the given node.
  86. static inline struct list*
  87. list_prev (const struct list *node)
  88. {
  89. return (node->prev);
  90. }
  91. // Return true if node is invalid and denotes one of the ends of the list.
  92. static inline bool
  93. list_end (const struct list *list, const struct list *node)
  94. {
  95. return (list == node);
  96. }
  97. // Return true if list is empty.
  98. static inline bool
  99. list_empty (const struct list *list)
  100. {
  101. return (list == list->next);
  102. }
  103. // Return true if list contains exactly one node.
  104. static inline bool
  105. list_singular (const struct list *list)
  106. {
  107. return (!list_empty (list) && list->next == list->prev);
  108. }
  109. /*
  110. * Split list2 by moving its nodes up to, but not including, the given
  111. * node into list1, which can be in a stale state.
  112. *
  113. * If list2 is empty, or node is list2 or list2->next, list1 is merely
  114. * initialized.
  115. */
  116. static inline void
  117. list_split (struct list *list1, struct list *list2, struct list *node)
  118. {
  119. if (list_empty (list2) || list2->next == node || list_end (list2, node))
  120. {
  121. list_init (list1);
  122. return;
  123. }
  124. list1->next = list2->next;
  125. list1->next->prev = list1;
  126. list1->prev = node->prev;
  127. node->prev->next = list1;
  128. list2->next = node;
  129. node->prev = list2;
  130. }
  131. /*
  132. * Append the nodes of list2 at the end of list1.
  133. *
  134. * After completion, list2 is stale.
  135. */
  136. static inline void
  137. list_concat (struct list *list1, const struct list *list2)
  138. {
  139. if (list_empty (list2))
  140. return;
  141. struct list *last1 = list1->prev,
  142. *first2 = list2->next,
  143. *last2 = list2->prev;
  144. last1->next = first2;
  145. first2->prev = last1;
  146. last2->next = list1;
  147. list1->prev = last2;
  148. }
  149. /*
  150. * Set the new head of a list.
  151. *
  152. * This function is an optimized version of :
  153. * list_init(&new_list);
  154. * list_concat(&new_list, &old_list);
  155. *
  156. * After completion, old_head is stale.
  157. */
  158. static inline void
  159. list_set_head (struct list *new_head, const struct list *old_head)
  160. {
  161. if (list_empty (old_head))
  162. {
  163. list_init (new_head);
  164. return;
  165. }
  166. *new_head = *old_head;
  167. new_head->next->prev = new_head;
  168. new_head->prev->next = new_head;
  169. }
  170. /*
  171. * Add a node between two nodes.
  172. *
  173. * This function is private.
  174. */
  175. static inline void
  176. list_add (struct list *prev, struct list *next, struct list *node)
  177. {
  178. next->prev = node;
  179. node->next = next;
  180. prev->next = node;
  181. node->prev = prev;
  182. }
  183. // Insert a node at the head of a list.
  184. static inline void
  185. list_insert_head (struct list *list, struct list *node)
  186. {
  187. list_add (list, list->next, node);
  188. }
  189. // Insert a node at the tail of a list.
  190. static inline void
  191. list_insert_tail (struct list *list, struct list *node)
  192. {
  193. list_add (list->prev, list, node);
  194. }
  195. // Insert a node before another node.
  196. static inline void
  197. list_insert_before (struct list *node, struct list *next)
  198. {
  199. list_add (next->prev, next, node);
  200. }
  201. // Insert a node after another node.
  202. static inline void
  203. list_insert_after (struct list *node, struct list *prev)
  204. {
  205. list_add (prev, prev->next, node);
  206. }
  207. /*
  208. * Remove a node from a list.
  209. *
  210. * After completion, the node is stale.
  211. */
  212. static inline void
  213. list_remove (struct list *node)
  214. {
  215. node->prev->next = node->next;
  216. node->next->prev = node->prev;
  217. }
  218. /*
  219. * Macro that evaluates to the address of the structure containing the
  220. * given node based on the given type and member.
  221. */
  222. #define list_entry(node, type, member) structof (node, type, member)
  223. // Get the first entry of a list.
  224. #define list_first_entry(list, type, member) \
  225. list_entry (list_first (list), type, member)
  226. // Get the last entry of a list.
  227. #define list_last_entry(list, type, member) \
  228. list_entry (list_last (list), type, member)
  229. // Get the entry next to the given entry.
  230. #define list_next_entry(entry, member) \
  231. list_entry (list_next (&(entry)->member), typeof (*(entry)), member)
  232. // Get the entry previous to the given entry.
  233. #define list_prev_entry(entry, member) \
  234. list_entry (list_prev (&(entry)->member), typeof (*(entry)), member)
  235. /*
  236. * Forge a loop to process all nodes of a list.
  237. *
  238. * The node must not be altered during the loop.
  239. */
  240. #define list_for_each(lst, node) \
  241. for (struct list *node = list_first (lst); \
  242. !list_end (lst, node); \
  243. node = list_next (node))
  244. // Forge a loop to process all nodes of a list.
  245. #define list_for_each_safe(lst, node, tmp) \
  246. for (struct list *node = list_first (lst), *tmp = list_next (node); \
  247. !list_end (lst, node); \
  248. node = tmp, tmp = list_next (node))
  249. // Version of list_for_each() that processes nodes backward.
  250. #define list_for_each_reverse(list, node) \
  251. for (node = list_last (list); \
  252. !list_end (list, node); \
  253. node = list_prev (node))
  254. // Version of list_for_each_safe() that processes nodes backward.
  255. #define list_for_each_reverse_safe(lst, node, tmp) \
  256. for (struct list *node = list_last (lst), tmp = list_prev (node); \
  257. !list_end (lst, node); \
  258. node = tmp, tmp = list_prev (node))
  259. /*
  260. * Forge a loop to process all entries of a list.
  261. *
  262. * The entry node must not be altered during the loop.
  263. */
  264. #define list_for_each_entry(list, entry, member) \
  265. for (entry = list_first_entry (list, typeof (*entry), member); \
  266. !list_end (list, &entry->member); \
  267. entry = list_next_entry (entry, member))
  268. // Forge a loop to process all entries of a list.
  269. #define list_for_each_entry_safe(list, entry, tmp, member) \
  270. for (entry = list_first_entry (list, typeof (*entry), member), \
  271. tmp = list_next_entry (entry, member); \
  272. !list_end (list, &entry->member); \
  273. entry = tmp, tmp = list_next_entry (entry, member))
  274. // Version of list_for_each_entry() that processes entries backward.
  275. #define list_for_each_entry_reverse(list, entry, member) \
  276. for (entry = list_last_entry (list, typeof (*entry), member); \
  277. !list_end (list, &entry->member); \
  278. entry = list_prev_entry (entry, member))
  279. // Version of list_for_each_entry_safe() that processes entries backward.
  280. #define list_for_each_entry_reverse_safe(list, entry, tmp, member) \
  281. for (entry = list_last_entry (list, typeof(*entry), member), \
  282. tmp = list_prev_entry (entry, member); \
  283. !list_end (list, &entry->member); \
  284. entry = tmp, tmp = list_prev_entry (entry, member))
  285. // Pop an element from a list.
  286. #define list_pop(lst, type, member) \
  287. ({ \
  288. _Auto tmp_ = list_first_entry (lst, type, member); \
  289. list_remove (&tmp_->member); \
  290. tmp_; \
  291. })
  292. /*
  293. * Lockless variants
  294. *
  295. * This is a subset of the main interface that only supports forward traversal.
  296. * In addition, list_end() is also allowed in read-side critical sections.
  297. */
  298. // Return the first node of a list.
  299. static inline struct list*
  300. list_rcu_first (const struct list *list)
  301. {
  302. return (rcu_load (&list->next));
  303. }
  304. // Return the node next to the given node.
  305. static inline struct list*
  306. list_rcu_next (const struct list *node)
  307. {
  308. return (rcu_load (&node->next));
  309. }
  310. /*
  311. * Add a node between two nodes.
  312. *
  313. * This function is private.
  314. */
  315. static inline void
  316. list_rcu_add (struct list *prev, struct list *next, struct list *node)
  317. {
  318. node->next = next;
  319. node->prev = prev;
  320. rcu_store (&prev->next, node);
  321. next->prev = node;
  322. }
  323. // Insert a node at the head of a list.
  324. static inline void
  325. list_rcu_insert_head (struct list *list, struct list *node)
  326. {
  327. list_rcu_add (list, list->next, node);
  328. }
  329. // Insert a node at the tail of a list.
  330. static inline void
  331. list_rcu_insert_tail (struct list *list, struct list *node)
  332. {
  333. list_rcu_add (list->prev, list, node);
  334. }
  335. // Insert a node before another node.
  336. static inline void
  337. list_rcu_insert_before (struct list *node, struct list *next)
  338. {
  339. list_rcu_add (next->prev, next, node);
  340. }
  341. // Insert a node after another node.
  342. static inline void
  343. list_rcu_insert_after (struct list *node, struct list *prev)
  344. {
  345. list_rcu_add (prev, prev->next, node);
  346. }
  347. /*
  348. * Remove a node from a list.
  349. *
  350. * After completion, the node is stale.
  351. */
  352. static inline void
  353. list_rcu_remove (struct list *node)
  354. {
  355. node->next->prev = node->prev;
  356. rcu_store (&node->prev->next, node->next);
  357. }
  358. /*
  359. * Macro that evaluates to the address of the structure containing the
  360. * given node based on the given type and member.
  361. */
  362. #define list_rcu_entry(node, type, member) \
  363. structof (rcu_load (&(node)), type, member)
  364. /*
  365. * Get the first entry of a list.
  366. *
  367. * Unlike list_first_entry(), this macro may evaluate to NULL, because
  368. * the node pointer can only be read once, preventing the combination
  369. * of lockless list_empty()/list_first_entry() variants.
  370. */
  371. #define list_rcu_first_entry(head, type, member) \
  372. MACRO_BEGIN \
  373. \
  374. struct list *list_ = (head), *first_ = list_rcu_first (list_); \
  375. list_end (list_, first_) ? NULL : list_entry (first_, type, member); \
  376. MACRO_END
  377. /*
  378. * Get the entry next to the given entry.
  379. *
  380. * Unlike list_next_entry(), this macro may evaluate to NULL, because
  381. * the node pointer can only be read once, preventing the combination
  382. * of lockless list_empty()/list_next_entry() variants.
  383. */
  384. #define list_rcu_next_entry(head, entry, member) \
  385. MACRO_BEGIN \
  386. struct list *list_ = (head), *next_ = list_rcu_next (&entry->member); \
  387. list_end (list_, next_) ? \
  388. NULL : list_entry (next_, typeof (*entry), member); \
  389. MACRO_END
  390. /*
  391. * Forge a loop to process all nodes of a list.
  392. *
  393. * The node must not be altered during the loop.
  394. */
  395. #define list_rcu_for_each(lst, node) \
  396. for (struct list *node = list_rcu_first (lst); \
  397. !list_end (lst, node); \
  398. node = list_rcu_next (node))
  399. /*
  400. * Forge a loop to process all entries of a list.
  401. *
  402. * The entry node must not be altered during the loop.
  403. */
  404. #define list_rcu_for_each_entry(list, entry, member) \
  405. for (entry = list_rcu_first_entry (list, typeof (*entry), member); \
  406. entry != NULL; \
  407. entry = list_rcu_next_entry (list, entry, member))
  408. #endif