interval_tree.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/interval_tree.h>
  3. #include <linux/interval_tree_generic.h>
  4. #include <linux/compiler.h>
  5. #include <linux/export.h>
  6. #define START(node) ((node)->start)
  7. #define LAST(node) ((node)->last)
  8. INTERVAL_TREE_DEFINE(struct interval_tree_node, rb,
  9. unsigned long, __subtree_last,
  10. START, LAST,, interval_tree)
  11. EXPORT_SYMBOL_GPL(interval_tree_insert);
  12. EXPORT_SYMBOL_GPL(interval_tree_remove);
  13. EXPORT_SYMBOL_GPL(interval_tree_subtree_search);
  14. EXPORT_SYMBOL_GPL(interval_tree_iter_first);
  15. EXPORT_SYMBOL_GPL(interval_tree_iter_next);
  16. #ifdef CONFIG_INTERVAL_TREE_SPAN_ITER
  17. /*
  18. * Roll nodes[1] into nodes[0] by advancing nodes[1] to the end of a contiguous
  19. * span of nodes. This makes nodes[0]->last the end of that contiguous used span
  20. * of indexes that started at the original nodes[1]->start.
  21. *
  22. * If there is an interior hole, nodes[1] is now the first node starting the
  23. * next used span. A hole span is between nodes[0]->last and nodes[1]->start.
  24. *
  25. * If there is a tailing hole, nodes[1] is now NULL. A hole span is between
  26. * nodes[0]->last and last_index.
  27. *
  28. * If the contiguous used range span to last_index, nodes[1] is set to NULL.
  29. */
  30. static void
  31. interval_tree_span_iter_next_gap(struct interval_tree_span_iter *state)
  32. {
  33. struct interval_tree_node *cur = state->nodes[1];
  34. state->nodes[0] = cur;
  35. do {
  36. if (cur->last > state->nodes[0]->last)
  37. state->nodes[0] = cur;
  38. cur = interval_tree_iter_next(cur, state->first_index,
  39. state->last_index);
  40. } while (cur && (state->nodes[0]->last >= cur->start ||
  41. state->nodes[0]->last + 1 == cur->start));
  42. state->nodes[1] = cur;
  43. }
  44. void interval_tree_span_iter_first(struct interval_tree_span_iter *iter,
  45. struct rb_root_cached *itree,
  46. unsigned long first_index,
  47. unsigned long last_index)
  48. {
  49. iter->first_index = first_index;
  50. iter->last_index = last_index;
  51. iter->nodes[0] = NULL;
  52. iter->nodes[1] =
  53. interval_tree_iter_first(itree, first_index, last_index);
  54. if (!iter->nodes[1]) {
  55. /* No nodes intersect the span, whole span is hole */
  56. iter->start_hole = first_index;
  57. iter->last_hole = last_index;
  58. iter->is_hole = 1;
  59. return;
  60. }
  61. if (iter->nodes[1]->start > first_index) {
  62. /* Leading hole on first iteration */
  63. iter->start_hole = first_index;
  64. iter->last_hole = iter->nodes[1]->start - 1;
  65. iter->is_hole = 1;
  66. interval_tree_span_iter_next_gap(iter);
  67. return;
  68. }
  69. /* Starting inside a used */
  70. iter->start_used = first_index;
  71. iter->is_hole = 0;
  72. interval_tree_span_iter_next_gap(iter);
  73. iter->last_used = iter->nodes[0]->last;
  74. if (iter->last_used >= last_index) {
  75. iter->last_used = last_index;
  76. iter->nodes[0] = NULL;
  77. iter->nodes[1] = NULL;
  78. }
  79. }
  80. EXPORT_SYMBOL_GPL(interval_tree_span_iter_first);
  81. void interval_tree_span_iter_next(struct interval_tree_span_iter *iter)
  82. {
  83. if (!iter->nodes[0] && !iter->nodes[1]) {
  84. iter->is_hole = -1;
  85. return;
  86. }
  87. if (iter->is_hole) {
  88. iter->start_used = iter->last_hole + 1;
  89. iter->last_used = iter->nodes[0]->last;
  90. if (iter->last_used >= iter->last_index) {
  91. iter->last_used = iter->last_index;
  92. iter->nodes[0] = NULL;
  93. iter->nodes[1] = NULL;
  94. }
  95. iter->is_hole = 0;
  96. return;
  97. }
  98. if (!iter->nodes[1]) {
  99. /* Trailing hole */
  100. iter->start_hole = iter->nodes[0]->last + 1;
  101. iter->last_hole = iter->last_index;
  102. iter->nodes[0] = NULL;
  103. iter->is_hole = 1;
  104. return;
  105. }
  106. /* must have both nodes[0] and [1], interior hole */
  107. iter->start_hole = iter->nodes[0]->last + 1;
  108. iter->last_hole = iter->nodes[1]->start - 1;
  109. iter->is_hole = 1;
  110. interval_tree_span_iter_next_gap(iter);
  111. }
  112. EXPORT_SYMBOL_GPL(interval_tree_span_iter_next);
  113. /*
  114. * Advance the iterator index to a specific position. The returned used/hole is
  115. * updated to start at new_index. This is faster than calling
  116. * interval_tree_span_iter_first() as it can avoid full searches in several
  117. * cases where the iterator is already set.
  118. */
  119. void interval_tree_span_iter_advance(struct interval_tree_span_iter *iter,
  120. struct rb_root_cached *itree,
  121. unsigned long new_index)
  122. {
  123. if (iter->is_hole == -1)
  124. return;
  125. iter->first_index = new_index;
  126. if (new_index > iter->last_index) {
  127. iter->is_hole = -1;
  128. return;
  129. }
  130. /* Rely on the union aliasing hole/used */
  131. if (iter->start_hole <= new_index && new_index <= iter->last_hole) {
  132. iter->start_hole = new_index;
  133. return;
  134. }
  135. if (new_index == iter->last_hole + 1)
  136. interval_tree_span_iter_next(iter);
  137. else
  138. interval_tree_span_iter_first(iter, itree, new_index,
  139. iter->last_index);
  140. }
  141. EXPORT_SYMBOL_GPL(interval_tree_span_iter_advance);
  142. #endif