range.c 2.9 KB

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