plist.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * Priority list.
  22. *
  23. * This container acts as a doubly-linked list sorted by priority in
  24. * ascending order. All operations behave as with a regular linked list
  25. * except insertion, which is O(k), k being the number of priorities
  26. * among the entries.
  27. */
  28. #ifndef KERN_PLIST_H
  29. #define KERN_PLIST_H
  30. #include <stdbool.h>
  31. #include <kern/list.h>
  32. #include <kern/macros.h>
  33. #include <kern/plist_types.h>
  34. // Priority list.
  35. struct plist;
  36. // Priority list node.
  37. struct plist_node;
  38. // Static priority list initializer.
  39. #define PLIST_INITIALIZER(plist) \
  40. { LIST_INITIALIZER ((plist).list), LIST_INITIALIZER ((plist).prio_list) }
  41. // Initialize a priority list.
  42. static inline void
  43. plist_init (struct plist *plist)
  44. {
  45. list_init (&plist->list);
  46. list_init (&plist->prio_list);
  47. }
  48. // Unlink a priority list node.
  49. static inline void
  50. plist_node_unlink (struct plist_node *pnode)
  51. {
  52. list_node_init (&pnode->node);
  53. list_node_init (&pnode->prio_node);
  54. }
  55. // Initialize a priority list node.
  56. static inline void
  57. plist_node_init (struct plist_node *pnode, unsigned int priority)
  58. {
  59. pnode->priority = priority;
  60. plist_node_unlink (pnode);
  61. }
  62. // Return the priority associated with a node.
  63. static inline unsigned int
  64. plist_node_priority (const struct plist_node *pnode)
  65. {
  66. return (pnode->priority);
  67. }
  68. // Update the priority of an already initialized node.
  69. static inline void
  70. plist_node_set_priority (struct plist_node *pnode, unsigned int priority)
  71. {
  72. pnode->priority = priority;
  73. }
  74. // Return true if pnode is in no priority lists.
  75. static inline bool
  76. plist_node_unlinked (const struct plist_node *pnode)
  77. {
  78. return (list_node_unlinked (&pnode->node));
  79. }
  80. /*
  81. * Macro that evaluates to the address of the structure containing the
  82. * given node based on the given type and member.
  83. */
  84. #define plist_entry(pnode, type, member) structof (pnode, type, member)
  85. // Return the first node of a priority list.
  86. static inline struct plist_node*
  87. plist_first (const struct plist *plist)
  88. {
  89. return (list_first_entry (&plist->list, struct plist_node, node));
  90. }
  91. // Return the last node of a priority list.
  92. static inline struct plist_node*
  93. plist_last (const struct plist *plist)
  94. {
  95. return (list_last_entry (&plist->list, struct plist_node, node));
  96. }
  97. // Return the node next to the given node.
  98. static inline struct plist_node*
  99. plist_next (const struct plist_node *pnode)
  100. {
  101. return ((struct plist_node *)list_next_entry (pnode, node));
  102. }
  103. // Return the node previous to the given node.
  104. static inline struct plist_node*
  105. plist_prev (const struct plist_node *pnode)
  106. {
  107. return ((struct plist_node *)list_prev_entry (pnode, node));
  108. }
  109. // Get the first entry of a priority list.
  110. #define plist_first_entry(plist, type, member) \
  111. plist_entry (plist_first (plist), type, member)
  112. // Get the last entry of a priority list.
  113. #define plist_last_entry(plist, type, member) \
  114. plist_entry (plist_last (plist), type, member)
  115. // Get the entry next to the given entry.
  116. #define plist_next_entry(entry, member) \
  117. plist_entry (plist_next (&(entry)->member), typeof (*(entry)), member)
  118. // Get the entry previous to the given entry.
  119. #define plist_prev_entry(entry, member) \
  120. plist_entry (plist_prev (&(entry)->member), typeof (*(entry)), member)
  121. /*
  122. * Return true if node is after the last or before the first node of
  123. * a priority list.
  124. */
  125. static inline bool
  126. plist_end (const struct plist *plist, const struct plist_node *pnode)
  127. {
  128. return (list_end (&plist->list, &pnode->node));
  129. }
  130. // Return true if plist is empty.
  131. static inline bool
  132. plist_empty (const struct plist *plist)
  133. {
  134. return (list_empty (&plist->list));
  135. }
  136. // Return true if plist contains exactly one node.
  137. static inline bool
  138. plist_singular (const struct plist *plist)
  139. {
  140. return (list_singular (&plist->list));
  141. }
  142. /*
  143. * Add a node to a priority list.
  144. *
  145. * If the priority list already contains nodes with the same priority
  146. * as the given node, it is inserted before them.
  147. *
  148. * The node must be initialized before calling this function.
  149. */
  150. void plist_add (struct plist *plist, struct plist_node *pnode);
  151. /*
  152. * Remove a node from a priority list.
  153. *
  154. * After completion, the node is stale.
  155. */
  156. void plist_remove (struct plist *plist, struct plist_node *pnode);
  157. /*
  158. * Forge a loop to process all nodes of a priority list.
  159. *
  160. * The node must not be altered during the loop.
  161. */
  162. #define plist_for_each(plist, pnode) \
  163. for (struct plist_node *pnode = plist_first (plist); \
  164. !plist_end (plist, pnode); \
  165. pnode = plist_next (pnode))
  166. // Forge a loop to process all nodes of a priority list.
  167. #define plist_for_each_safe(plist, pnode, tmp) \
  168. for (struct plist_node *pnode = plist_first (plist), \
  169. *tmp = plist_next (pnode); \
  170. !plist_end (plist, pnode); \
  171. pnode = tmp, tmp = plist_next (pnode))
  172. // Version of plist_for_each() that processes nodes backward.
  173. #define plist_for_each_reverse(plist, pnode) \
  174. for (struct plist_node *pnode = plist_last (plist); \
  175. !plist_end (plist, pnode); \
  176. pnode = plist_prev (pnode))
  177. // Version of plist_for_each_safe() that processes nodes backward.
  178. #define plist_for_each_reverse_safe(plist, pnode, tmp) \
  179. for (struct plist_node *pnode = plist_last (plist), \
  180. *tmp = plist_prev (pnode); \
  181. !plist_end (plist, pnode); \
  182. pnode = tmp, tmp = plist_prev (pnode))
  183. /*
  184. * Forge a loop to process all entries of a priority list.
  185. *
  186. * The entry node must not be altered during the loop.
  187. */
  188. #define plist_for_each_entry(plist, entry, member) \
  189. list_for_each_entry (&(plist)->list, entry, member.node)
  190. // Forge a loop to process all entries of a priority list.
  191. #define plist_for_each_entry_safe(plist, entry, tmp, member) \
  192. list_for_each_entry_safe (&(plist)->list, entry, tmp, member.node)
  193. // Version of plist_for_each_entry() that processes entries backward.
  194. #define plist_for_each_entry_reverse(plist, entry, member) \
  195. list_for_each_entry_reverse (&(plist)->list, entry, member.node)
  196. // Version of plist_for_each_entry_safe() that processes entries backward.
  197. #define plist_for_each_entry_reverse_safe(plist, entry, tmp, member) \
  198. list_for_each_entry_reverse_safe (&(plist)->list, entry, tmp, member.node)
  199. #endif