slist.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. * Singly-linked list.
  22. */
  23. #ifndef KERN_SLIST_H
  24. #define KERN_SLIST_H
  25. #include <stdbool.h>
  26. #include <stddef.h>
  27. #include <kern/macros.h>
  28. #include <kern/rcu.h>
  29. #include <kern/slist_types.h>
  30. struct slist;
  31. struct slist_node;
  32. /*
  33. * Static list initializer.
  34. */
  35. #define SLIST_INITIALIZER(list) { NULL, NULL }
  36. /*
  37. * Initialize a list.
  38. */
  39. static inline void
  40. slist_init(struct slist *list)
  41. {
  42. list->first = NULL;
  43. list->last = NULL;
  44. }
  45. /*
  46. * Return the first node of a list.
  47. */
  48. static inline struct slist_node *
  49. slist_first(const struct slist *list)
  50. {
  51. return list->first;
  52. }
  53. /*
  54. * Return the last node of a list.
  55. */
  56. static inline struct slist_node *
  57. slist_last(const struct slist *list)
  58. {
  59. return list->last;
  60. }
  61. /*
  62. * Return the node next to the given node.
  63. */
  64. static inline struct slist_node *
  65. slist_next(const struct slist_node *node)
  66. {
  67. return node->next;
  68. }
  69. /*
  70. * Return true if node is invalid and denotes one of the ends of the list.
  71. */
  72. static inline bool
  73. slist_end(const struct slist_node *node)
  74. {
  75. return node == NULL;
  76. }
  77. /*
  78. * Return true if list is empty.
  79. */
  80. static inline bool
  81. slist_empty(const struct slist *list)
  82. {
  83. return list->first == NULL;
  84. }
  85. /*
  86. * Return true if list contains exactly one node.
  87. */
  88. static inline bool
  89. slist_singular(const struct slist *list)
  90. {
  91. return !slist_empty(list) && (list->first == list->last);
  92. }
  93. /*
  94. * Append the nodes of list2 at the end of list1.
  95. *
  96. * After completion, list2 is stale.
  97. */
  98. static inline void
  99. slist_concat(struct slist *list1, const struct slist *list2)
  100. {
  101. if (slist_empty(list2)) {
  102. return;
  103. }
  104. if (slist_empty(list1)) {
  105. list1->first = list2->first;
  106. } else {
  107. list1->last->next = list2->first;
  108. }
  109. list1->last = list2->last;
  110. }
  111. /*
  112. * Set the new head of a list.
  113. *
  114. * This function is an optimized version of :
  115. * list_init(&new_list);
  116. * list_concat(&new_list, &old_list);
  117. */
  118. static inline void
  119. slist_set_head(struct slist *new_head, const struct slist *old_head)
  120. {
  121. *new_head = *old_head;
  122. }
  123. /*
  124. * Insert a node at the head of a list.
  125. */
  126. static inline void
  127. slist_insert_head(struct slist *list, struct slist_node *node)
  128. {
  129. if (slist_empty(list)) {
  130. list->last = node;
  131. }
  132. node->next = list->first;
  133. list->first = node;
  134. }
  135. /*
  136. * Insert a node at the tail of a list.
  137. */
  138. static inline void
  139. slist_insert_tail(struct slist *list, struct slist_node *node)
  140. {
  141. node->next = NULL;
  142. if (slist_empty(list)) {
  143. list->first = node;
  144. } else {
  145. list->last->next = node;
  146. }
  147. list->last = node;
  148. }
  149. /*
  150. * Insert a node after another node.
  151. *
  152. * The prev node must be valid.
  153. */
  154. static inline void
  155. slist_insert_after(struct slist *list, struct slist_node *node,
  156. struct slist_node *prev)
  157. {
  158. node->next = prev->next;
  159. prev->next = node;
  160. if (list->last == prev) {
  161. list->last = node;
  162. }
  163. }
  164. /*
  165. * Remove a node from a list.
  166. *
  167. * The prev argument must point to the node immediately preceding the target
  168. * node. It may safely denote the end of the given list (NULL), in which case
  169. * the first node is removed.
  170. */
  171. static inline void
  172. slist_remove(struct slist *list, struct slist_node *prev)
  173. {
  174. struct slist_node *node;
  175. if (slist_end(prev)) {
  176. node = list->first;
  177. list->first = node->next;
  178. if (list->last == node) {
  179. list->last = NULL;
  180. }
  181. } else {
  182. node = prev->next;
  183. prev->next = node->next;
  184. if (list->last == node) {
  185. list->last = prev;
  186. }
  187. }
  188. }
  189. /*
  190. * Macro that evaluates to the address of the structure containing the
  191. * given node based on the given type and member.
  192. */
  193. #define slist_entry(node, type, member) structof(node, type, member)
  194. /*
  195. * Get the first entry of a list.
  196. */
  197. #define slist_first_entry(list, type, member) \
  198. MACRO_BEGIN \
  199. struct slist_node *first_; \
  200. \
  201. first_ = (list)->first; \
  202. slist_end(first_) ? NULL : slist_entry(first_, type, member); \
  203. MACRO_END
  204. /*
  205. * Get the last entry of a list.
  206. */
  207. #define slist_last_entry(list, type, member) \
  208. MACRO_BEGIN \
  209. struct slist_node *last_; \
  210. \
  211. last_ = (list)->last; \
  212. slist_end(last_) ? NULL : slist_entry(last_, type, member); \
  213. MACRO_END
  214. /*
  215. * Get the entry next to the given entry.
  216. */
  217. #define slist_next_entry(entry, member) \
  218. MACRO_BEGIN \
  219. struct slist_node *next_; \
  220. \
  221. next_ = (entry)->member.next; \
  222. slist_end(next_) \
  223. ? NULL \
  224. : slist_entry(next_, typeof(*entry), member); \
  225. MACRO_END
  226. /*
  227. * Forge a loop to process all nodes of a list.
  228. *
  229. * The node must not be altered during the loop.
  230. */
  231. #define slist_for_each(list, node) \
  232. for (node = slist_first(list); \
  233. !slist_end(node); \
  234. node = slist_next(node))
  235. /*
  236. * Forge a loop to process all nodes of a list.
  237. */
  238. #define slist_for_each_safe(list, node, tmp) \
  239. for (node = slist_first(list), \
  240. tmp = slist_end(node) ? NULL : slist_next(node); \
  241. !slist_end(node); \
  242. node = tmp, \
  243. tmp = slist_end(node) ? NULL : slist_next(node))
  244. /*
  245. * Forge a loop to process all entries of a list.
  246. *
  247. * The entry node must not be altered during the loop.
  248. */
  249. #define slist_for_each_entry(list, entry, member) \
  250. for (entry = slist_first_entry(list, typeof(*entry), member); \
  251. entry != NULL; \
  252. entry = slist_next_entry(entry, member))
  253. /*
  254. * Forge a loop to process all entries of a list.
  255. */
  256. #define slist_for_each_entry_safe(list, entry, tmp, member) \
  257. for (entry = slist_first_entry(list, typeof(*entry), member), \
  258. tmp = (entry == NULL) ? NULL : slist_next_entry(entry, member); \
  259. entry != NULL; \
  260. entry = tmp, \
  261. tmp = (entry == NULL) ? NULL : slist_next_entry(entry, member)) \
  262. /*
  263. * Lockless variants
  264. *
  265. * The slist_end() function may be used from read-side critical sections.
  266. */
  267. /*
  268. * Return the first node of a list.
  269. */
  270. static inline struct slist_node *
  271. slist_rcu_first(const struct slist *list)
  272. {
  273. return rcu_load_ptr(list->first);
  274. }
  275. /*
  276. * Return the node next to the given node.
  277. */
  278. static inline struct slist_node *
  279. slist_rcu_next(const struct slist_node *node)
  280. {
  281. return rcu_load_ptr(node->next);
  282. }
  283. /*
  284. * Insert a node at the head of a list.
  285. */
  286. static inline void
  287. slist_rcu_insert_head(struct slist *list, struct slist_node *node)
  288. {
  289. if (slist_empty(list)) {
  290. list->last = node;
  291. }
  292. node->next = list->first;
  293. rcu_store_ptr(list->first, node);
  294. }
  295. /*
  296. * Insert a node at the tail of a list.
  297. */
  298. static inline void
  299. slist_rcu_insert_tail(struct slist *list, struct slist_node *node)
  300. {
  301. node->next = NULL;
  302. if (slist_empty(list)) {
  303. rcu_store_ptr(list->first, node);
  304. } else {
  305. rcu_store_ptr(list->last->next, node);
  306. }
  307. list->last = node;
  308. }
  309. /*
  310. * Insert a node after another node.
  311. *
  312. * The prev node must be valid.
  313. */
  314. static inline void
  315. slist_rcu_insert_after(struct slist *list, struct slist_node *node,
  316. struct slist_node *prev)
  317. {
  318. node->next = prev->next;
  319. rcu_store_ptr(prev->next, node);
  320. if (list->last == prev) {
  321. list->last = node;
  322. }
  323. }
  324. /*
  325. * Remove a node from a list.
  326. *
  327. * The prev argument must point to the node immediately preceding the target
  328. * node. It may safely denote the end of the given list, in which case the
  329. * first node is removed.
  330. */
  331. static inline void
  332. slist_rcu_remove(struct slist *list, struct slist_node *prev)
  333. {
  334. struct slist_node *node;
  335. if (slist_end(prev)) {
  336. node = list->first;
  337. rcu_store_ptr(list->first, node->next);
  338. if (list->last == node) {
  339. list->last = NULL;
  340. }
  341. } else {
  342. node = prev->next;
  343. rcu_store_ptr(prev->next, node->next);
  344. if (list->last == node) {
  345. list->last = prev;
  346. }
  347. }
  348. }
  349. /*
  350. * Macro that evaluates to the address of the structure containing the
  351. * given node based on the given type and member.
  352. */
  353. #define slist_rcu_entry(node, type, member) \
  354. structof(rcu_load_ptr(node), type, member)
  355. /*
  356. * Get the first entry of a list.
  357. */
  358. #define slist_rcu_first_entry(list, type, member) \
  359. MACRO_BEGIN \
  360. struct slist_node *first_; \
  361. \
  362. first_ = slist_rcu_first(list); \
  363. slist_end(first_) ? NULL : slist_entry(first_, type, member); \
  364. MACRO_END
  365. /*
  366. * Get the entry next to the given entry.
  367. */
  368. #define slist_rcu_next_entry(entry, member) \
  369. MACRO_BEGIN \
  370. struct slist_node *next_; \
  371. \
  372. next_ = slist_rcu_next(&entry->member); \
  373. slist_end(next_) \
  374. ? NULL \
  375. : slist_entry(next_, typeof(*entry), member); \
  376. MACRO_END
  377. /*
  378. * Forge a loop to process all nodes of a list.
  379. */
  380. #define slist_rcu_for_each(list, node) \
  381. for (node = slist_rcu_first(list); \
  382. !slist_end(node); \
  383. node = slist_rcu_next(node))
  384. /*
  385. * Forge a loop to process all entries of a list.
  386. */
  387. #define slist_rcu_for_each_entry(list, entry, member) \
  388. for (entry = slist_rcu_first_entry(list, typeof(*entry), member); \
  389. entry != NULL; \
  390. entry = slist_rcu_next_entry(entry, member))
  391. #endif /* KERN_SLIST_H */