pqueue.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2023 Agustina Arzille.
  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. * Adjustable priority queue.
  18. *
  19. * Unlike the standard priority list bundled in the kernel, this data
  20. * structure can increase the priority of every member in O(1) time.
  21. * For this reason, its usage is encouraged when starvation needs to
  22. * be avoided.
  23. */
  24. #ifndef KERN_PQUEUE_H
  25. #define KERN_PQUEUE_H
  26. #include <stdbool.h>
  27. #include <stdint.h>
  28. #include <kern/macros.h>
  29. /*
  30. * Priority queue node.
  31. *
  32. * Note that the 'priority' member is not really meaningful to clients,
  33. * as it's dynamically adjusted as the queue is modified.
  34. */
  35. struct pqueue_node
  36. {
  37. struct pqueue_node *prev;
  38. struct pqueue_node *next;
  39. uint32_t prio;
  40. };
  41. struct pqueue
  42. {
  43. struct pqueue_node *head;
  44. };
  45. static inline void
  46. pqueue_init (struct pqueue *pq)
  47. {
  48. pq->head = NULL;
  49. }
  50. static inline void
  51. pqueue_node_init (struct pqueue_node *node, uint32_t prio)
  52. {
  53. node->prev = node->next = NULL;
  54. node->prio = prio;
  55. }
  56. static inline bool
  57. pqueue_node_unlinked (const struct pqueue_node *node)
  58. {
  59. return (node->next == NULL && node->prev == NULL);
  60. }
  61. static inline bool
  62. pqueue_empty (const struct pqueue *pqueue)
  63. {
  64. return (pqueue->head == NULL);
  65. }
  66. // Insert a node in the priority queue.
  67. void pqueue_insert (struct pqueue *pqueue, struct pqueue_node *node);
  68. /*
  69. * Pop a node from the priority list, returning the top-most one,
  70. * or NULL if empty.
  71. */
  72. struct pqueue_node* pqueue_pop (struct pqueue *pqueue);
  73. // Remove a node from the queue.
  74. void pqueue_remove (struct pqueue *pqueue, struct pqueue_node *node);
  75. // Increment the priority of all nodes in the queue.
  76. static inline void
  77. pqueue_inc (struct pqueue *pqueue, uint32_t off)
  78. {
  79. if (!pqueue->head)
  80. return;
  81. _Auto head = pqueue->head;
  82. if (likely (head->prio < UINT32_MAX - off))
  83. head->prio += off;
  84. else
  85. head->prio = UINT32_MAX;
  86. }
  87. static inline struct pqueue_node*
  88. pqueue_node_next (const struct pqueue *pqueue, struct pqueue_node *node)
  89. {
  90. return (!node || node->next == pqueue->head ? NULL : node->next);
  91. }
  92. #define pqueue_entry structof
  93. #define pqueue_first_entry(pq, type, member) \
  94. ({ \
  95. _Auto pq_ = (pq); \
  96. pqueue_empty (pq_) ? NULL : pqueue_entry (pq_->head, type, member); \
  97. })
  98. #define pqueue_pop_entry(pq, type, member) \
  99. pqueue_entry (pqueue_pop (pq), type, member)
  100. #define pqueue_next_entry(pq, node, type, member) \
  101. ({ \
  102. _Auto nxt_ = pqueue_node_next ((pq), (node)); \
  103. nxt_ ? pqueue_entry (nxt_, type, member) : NULL; \
  104. })
  105. #define pqueue_for_each(pq, node) \
  106. for (struct pqueue_node *node = (pq)->head; \
  107. node != NULL; node = pqueue_node_next ((pq), node))
  108. #define pqueue_for_each_safe(pq, node, aux) \
  109. for (struct pqueue_node *node = (pq)->head, \
  110. *aux = pqueue_node_next ((pq), node); \
  111. node != NULL; node = aux, aux = pqueue_node_next ((pq), aux))
  112. #define pqueue_for_each_entry(pq, entry, member) \
  113. for (entry = pqueue_first_entry ((pq), typeof (*entry), member); \
  114. entry != NULL; \
  115. entry = pqueue_next_entry ((pq), entry, typeof (*entry), member))
  116. #define pqueue_for_each_entry_safe(pq, entry, aux, member) \
  117. for (entry = pqueue_first_entry ((pq), typeof (*entry), member), \
  118. aux = entry ? pqueue_next_entry (pq, &entry->member, \
  119. typeof (*entry), member) : NULL; \
  120. entry != NULL; entry = aux, \
  121. aux = pqueue_next_entry ((pq), &aux->member, typeof (*aux), member))
  122. #endif