range.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Range add and subtract
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/sort.h>
  8. #include <linux/string.h>
  9. #include <linux/range.h>
  10. int add_range(struct range *range, int az, int nr_range, u64 start, u64 end)
  11. {
  12. if (start >= end)
  13. return nr_range;
  14. /* Out of slots: */
  15. if (nr_range >= az)
  16. return nr_range;
  17. range[nr_range].start = start;
  18. range[nr_range].end = end;
  19. nr_range++;
  20. return nr_range;
  21. }
  22. int add_range_with_merge(struct range *range, int az, int nr_range,
  23. u64 start, u64 end)
  24. {
  25. int i;
  26. if (start >= end)
  27. return nr_range;
  28. /* get new start/end: */
  29. for (i = 0; i < nr_range; i++) {
  30. u64 common_start, common_end;
  31. if (!range[i].end)
  32. continue;
  33. common_start = max(range[i].start, start);
  34. common_end = min(range[i].end, end);
  35. if (common_start > common_end)
  36. continue;
  37. /* new start/end, will add it back at last */
  38. start = min(range[i].start, start);
  39. end = max(range[i].end, end);
  40. memmove(&range[i], &range[i + 1],
  41. (nr_range - (i + 1)) * sizeof(range[i]));
  42. range[nr_range - 1].start = 0;
  43. range[nr_range - 1].end = 0;
  44. nr_range--;
  45. i--;
  46. }
  47. /* Need to add it: */
  48. return add_range(range, az, nr_range, start, end);
  49. }
  50. void subtract_range(struct range *range, int az, u64 start, u64 end)
  51. {
  52. int i, j;
  53. if (start >= end)
  54. return;
  55. for (j = 0; j < az; j++) {
  56. if (!range[j].end)
  57. continue;
  58. if (start <= range[j].start && end >= range[j].end) {
  59. range[j].start = 0;
  60. range[j].end = 0;
  61. continue;
  62. }
  63. if (start <= range[j].start && end < range[j].end &&
  64. range[j].start < end) {
  65. range[j].start = end;
  66. continue;
  67. }
  68. if (start > range[j].start && end >= range[j].end &&
  69. range[j].end > start) {
  70. range[j].end = start;
  71. continue;
  72. }
  73. if (start > range[j].start && end < range[j].end) {
  74. /* Find the new spare: */
  75. for (i = 0; i < az; i++) {
  76. if (range[i].end == 0)
  77. break;
  78. }
  79. if (i < az) {
  80. range[i].end = range[j].end;
  81. range[i].start = end;
  82. } else {
  83. pr_err("%s: run out of slot in ranges\n",
  84. __func__);
  85. }
  86. range[j].end = start;
  87. continue;
  88. }
  89. }
  90. }
  91. static int cmp_range(const void *x1, const void *x2)
  92. {
  93. const struct range *r1 = x1;
  94. const struct range *r2 = x2;
  95. if (r1->start < r2->start)
  96. return -1;
  97. if (r1->start > r2->start)
  98. return 1;
  99. return 0;
  100. }
  101. int clean_sort_range(struct range *range, int az)
  102. {
  103. int i, j, k = az - 1, nr_range = az;
  104. for (i = 0; i < k; i++) {
  105. if (range[i].end)
  106. continue;
  107. for (j = k; j > i; j--) {
  108. if (range[j].end) {
  109. k = j;
  110. break;
  111. }
  112. }
  113. if (j == i)
  114. break;
  115. range[i].start = range[k].start;
  116. range[i].end = range[k].end;
  117. range[k].start = 0;
  118. range[k].end = 0;
  119. k--;
  120. }
  121. /* count it */
  122. for (i = 0; i < az; i++) {
  123. if (!range[i].end) {
  124. nr_range = i;
  125. break;
  126. }
  127. }
  128. /* sort them */
  129. sort(range, nr_range, sizeof(struct range), cmp_range, NULL);
  130. return nr_range;
  131. }
  132. void sort_range(struct range *range, int nr_range)
  133. {
  134. /* sort them */
  135. sort(range, nr_range, sizeof(struct range), cmp_range, NULL);
  136. }