hlist.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 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 specialized for forward traversals and O(1) removals.
  22. */
  23. #ifndef KERN_HLIST_H
  24. #define KERN_HLIST_H
  25. #include <stdbool.h>
  26. #include <stddef.h>
  27. #include <kern/hlist_types.h>
  28. #include <kern/macros.h>
  29. #include <kern/rcu.h>
  30. struct hlist;
  31. struct hlist_node;
  32. /*
  33. * Static list initializer.
  34. */
  35. #define HLIST_INITIALIZER(list) { NULL }
  36. /*
  37. * Initialize a list.
  38. */
  39. static inline void
  40. hlist_init(struct hlist *list)
  41. {
  42. list->first = NULL;
  43. }
  44. /*
  45. * Initialize a list node.
  46. *
  47. * A node is in no list when its pprev member points to NULL.
  48. */
  49. static inline void
  50. hlist_node_init(struct hlist_node *node)
  51. {
  52. node->pprev = NULL;
  53. }
  54. /*
  55. * Return true if node is in no list.
  56. */
  57. static inline bool
  58. hlist_node_unlinked(const struct hlist_node *node)
  59. {
  60. return node->pprev == NULL;
  61. }
  62. /*
  63. * Return the first node of a list.
  64. */
  65. static inline struct hlist_node *
  66. hlist_first(const struct hlist *list)
  67. {
  68. return list->first;
  69. }
  70. /*
  71. * Return the node next to the given node.
  72. */
  73. static inline struct hlist_node *
  74. hlist_next(const struct hlist_node *node)
  75. {
  76. return node->next;
  77. }
  78. /*
  79. * Return true if node is invalid and denotes the end of the list.
  80. */
  81. static inline bool
  82. hlist_end(const struct hlist_node *node)
  83. {
  84. return node == NULL;
  85. }
  86. /*
  87. * Return true if list is empty.
  88. */
  89. static inline bool
  90. hlist_empty(const struct hlist *list)
  91. {
  92. return list->first == NULL;
  93. }
  94. /*
  95. * Return true if list contains exactly one node.
  96. */
  97. static inline bool
  98. hlist_singular(const struct hlist *list)
  99. {
  100. return !hlist_empty(list) && hlist_end(list->first->next);
  101. }
  102. /*
  103. * Set the new head of a list.
  104. *
  105. * After completion, old_head is stale.
  106. */
  107. static inline void
  108. hlist_set_head(struct hlist *new_head, const struct hlist *old_head)
  109. {
  110. *new_head = *old_head;
  111. if (!hlist_empty(new_head)) {
  112. new_head->first->pprev = &new_head->first;
  113. }
  114. }
  115. /*
  116. * Insert a node at the head of a list.
  117. */
  118. static inline void
  119. hlist_insert_head(struct hlist *list, struct hlist_node *node)
  120. {
  121. struct hlist_node *first;
  122. first = list->first;
  123. node->next = first;
  124. node->pprev = &list->first;
  125. if (first != NULL) {
  126. first->pprev = &node->next;
  127. }
  128. list->first = node;
  129. }
  130. /*
  131. * Insert a node before another node.
  132. */
  133. static inline void
  134. hlist_insert_before(struct hlist_node *node, struct hlist_node *next)
  135. {
  136. node->next = next;
  137. node->pprev = next->pprev;
  138. next->pprev = &node->next;
  139. *node->pprev = node;
  140. }
  141. /*
  142. * Insert a node after another node.
  143. */
  144. static inline void
  145. hlist_insert_after(struct hlist_node *node, struct hlist_node *prev)
  146. {
  147. node->next = prev->next;
  148. node->pprev = &prev->next;
  149. if (node->next != NULL) {
  150. node->next->pprev = &node->next;
  151. }
  152. prev->next = node;
  153. }
  154. /*
  155. * Remove a node from a list.
  156. */
  157. static inline void
  158. hlist_remove(struct hlist_node *node)
  159. {
  160. if (node->next != NULL) {
  161. node->next->pprev = node->pprev;
  162. }
  163. *node->pprev = node->next;
  164. }
  165. /*
  166. * Macro that evaluates to the address of the structure containing the
  167. * given node based on the given type and member.
  168. */
  169. #define hlist_entry(node, type, member) structof(node, type, member)
  170. /*
  171. * Get the first entry of a list.
  172. */
  173. #define hlist_first_entry(list, type, member) \
  174. MACRO_BEGIN \
  175. struct hlist_node *first_; \
  176. \
  177. first_ = (list)->first; \
  178. hlist_end(first_) ? NULL : hlist_entry(first_, type, member); \
  179. MACRO_END
  180. /*
  181. * Get the entry next to the given entry.
  182. */
  183. #define hlist_next_entry(entry, member) \
  184. MACRO_BEGIN \
  185. struct hlist_node *next_; \
  186. \
  187. next_ = (entry)->member.next; \
  188. hlist_end(next_) \
  189. ? NULL \
  190. : hlist_entry(next_, typeof(*entry), member); \
  191. MACRO_END
  192. /*
  193. * Forge a loop to process all nodes of a list.
  194. *
  195. * The node must not be altered during the loop.
  196. */
  197. #define hlist_for_each(list, node) \
  198. for (node = hlist_first(list); \
  199. !hlist_end(node); \
  200. node = hlist_next(node))
  201. /*
  202. * Forge a loop to process all nodes of a list.
  203. */
  204. #define hlist_for_each_safe(list, node, tmp) \
  205. for (node = hlist_first(list), \
  206. tmp = hlist_end(node) ? NULL : hlist_next(node); \
  207. !hlist_end(node); \
  208. node = tmp, \
  209. tmp = hlist_end(node) ? NULL : hlist_next(node))
  210. /*
  211. * Forge a loop to process all entries of a list.
  212. *
  213. * The entry node must not be altered during the loop.
  214. */
  215. #define hlist_for_each_entry(list, entry, member) \
  216. for (entry = hlist_first_entry(list, typeof(*entry), member); \
  217. entry != NULL; \
  218. entry = hlist_next_entry(entry, member))
  219. /*
  220. * Forge a loop to process all entries of a list.
  221. */
  222. #define hlist_for_each_entry_safe(list, entry, tmp, member) \
  223. for (entry = hlist_first_entry(list, typeof(*entry), member), \
  224. tmp = (entry == NULL) ? NULL : hlist_next_entry(entry, member); \
  225. entry != NULL; \
  226. entry = tmp, \
  227. tmp = (entry == NULL) ? NULL : hlist_next_entry(entry, member)) \
  228. /*
  229. * Lockless variants
  230. *
  231. * The hlist_end() function may be used from read-side critical sections.
  232. */
  233. /*
  234. * Return the first node of a list.
  235. */
  236. static inline struct hlist_node *
  237. hlist_rcu_first(const struct hlist *list)
  238. {
  239. return rcu_load_ptr(list->first);
  240. }
  241. /*
  242. * Return the node next to the given node.
  243. */
  244. static inline struct hlist_node *
  245. hlist_rcu_next(const struct hlist_node *node)
  246. {
  247. return rcu_load_ptr(node->next);
  248. }
  249. /*
  250. * Insert a node at the head of a list.
  251. */
  252. static inline void
  253. hlist_rcu_insert_head(struct hlist *list, struct hlist_node *node)
  254. {
  255. struct hlist_node *first;
  256. first = list->first;
  257. node->next = first;
  258. node->pprev = &list->first;
  259. if (first != NULL) {
  260. first->pprev = &node->next;
  261. }
  262. rcu_store_ptr(list->first, node);
  263. }
  264. /*
  265. * Insert a node before another node.
  266. */
  267. static inline void
  268. hlist_rcu_insert_before(struct hlist_node *node, struct hlist_node *next)
  269. {
  270. node->next = next;
  271. node->pprev = next->pprev;
  272. next->pprev = &node->next;
  273. rcu_store_ptr(*node->pprev, node);
  274. }
  275. /*
  276. * Insert a node after another node.
  277. */
  278. static inline void
  279. hlist_rcu_insert_after(struct hlist_node *node, struct hlist_node *prev)
  280. {
  281. node->next = prev->next;
  282. node->pprev = &prev->next;
  283. if (node->next != NULL) {
  284. node->next->pprev = &node->next;
  285. }
  286. rcu_store_ptr(prev->next, node);
  287. }
  288. /*
  289. * Remove a node from a list.
  290. */
  291. static inline void
  292. hlist_rcu_remove(struct hlist_node *node)
  293. {
  294. if (node->next != NULL) {
  295. node->next->pprev = node->pprev;
  296. }
  297. rcu_store_ptr(*node->pprev, node->next);
  298. }
  299. /*
  300. * Macro that evaluates to the address of the structure containing the
  301. * given node based on the given type and member.
  302. */
  303. #define hlist_rcu_entry(node, type, member) \
  304. structof(rcu_load_ptr(node), type, member)
  305. /*
  306. * Get the first entry of a list.
  307. */
  308. #define hlist_rcu_first_entry(list, type, member) \
  309. MACRO_BEGIN \
  310. struct hlist_node *first_; \
  311. \
  312. first_ = hlist_rcu_first(list); \
  313. hlist_end(first_) ? NULL : hlist_entry(first_, type, member); \
  314. MACRO_END
  315. /*
  316. * Get the entry next to the given entry.
  317. */
  318. #define hlist_rcu_next_entry(entry, member) \
  319. MACRO_BEGIN \
  320. struct hlist_node *next_; \
  321. \
  322. next_ = hlist_rcu_next(&entry->member); \
  323. hlist_end(next_) \
  324. ? NULL \
  325. : hlist_entry(next_, typeof(*entry), member); \
  326. MACRO_END
  327. /*
  328. * Forge a loop to process all nodes of a list.
  329. */
  330. #define hlist_rcu_for_each(list, node) \
  331. for (node = hlist_rcu_first(list); \
  332. !hlist_end(node); \
  333. node = hlist_rcu_next(node))
  334. /*
  335. * Forge a loop to process all entries of a list.
  336. */
  337. #define hlist_rcu_for_each_entry(list, entry, member) \
  338. for (entry = hlist_rcu_first_entry(list, typeof(*entry), member); \
  339. entry != NULL; \
  340. entry = hlist_rcu_next_entry(entry, member))
  341. #endif /* KERN_HLIST_H */