range.c 3.0 KB

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