Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Range add and subtract |
| 3 | */ |
Paul Gortmaker | 9a41845 | 2011-05-25 09:52:21 -0400 | [diff] [blame] | 4 | #include <linux/kernel.h> |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 5 | #include <linux/init.h> |
| 6 | #include <linux/sort.h> |
Yinghai Lu | 0541881 | 2013-06-13 13:17:02 -0700 | [diff] [blame] | 7 | #include <linux/string.h> |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 8 | #include <linux/range.h> |
| 9 | |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 10 | int add_range(struct range *range, int az, int nr_range, u64 start, u64 end) |
| 11 | { |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 12 | if (start >= end) |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 13 | return nr_range; |
| 14 | |
| 15 | /* Out of slots: */ |
| 16 | if (nr_range >= az) |
| 17 | return nr_range; |
| 18 | |
| 19 | range[nr_range].start = start; |
| 20 | range[nr_range].end = end; |
| 21 | |
| 22 | nr_range++; |
| 23 | |
| 24 | return nr_range; |
| 25 | } |
| 26 | |
| 27 | int add_range_with_merge(struct range *range, int az, int nr_range, |
| 28 | u64 start, u64 end) |
| 29 | { |
| 30 | int i; |
| 31 | |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 32 | if (start >= end) |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 33 | return nr_range; |
| 34 | |
Yinghai Lu | 0541881 | 2013-06-13 13:17:02 -0700 | [diff] [blame] | 35 | /* get new start/end: */ |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 36 | for (i = 0; i < nr_range; i++) { |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 37 | u64 common_start, common_end; |
| 38 | |
| 39 | if (!range[i].end) |
| 40 | continue; |
| 41 | |
| 42 | common_start = max(range[i].start, start); |
| 43 | common_end = min(range[i].end, end); |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 44 | if (common_start > common_end) |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 45 | continue; |
| 46 | |
Yinghai Lu | 0541881 | 2013-06-13 13:17:02 -0700 | [diff] [blame] | 47 | /* new start/end, will add it back at last */ |
| 48 | start = min(range[i].start, start); |
| 49 | end = max(range[i].end, end); |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 50 | |
Yinghai Lu | 0541881 | 2013-06-13 13:17:02 -0700 | [diff] [blame] | 51 | memmove(&range[i], &range[i + 1], |
| 52 | (nr_range - (i + 1)) * sizeof(range[i])); |
| 53 | range[nr_range - 1].start = 0; |
| 54 | range[nr_range - 1].end = 0; |
| 55 | nr_range--; |
| 56 | i--; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* Need to add it: */ |
| 60 | return add_range(range, az, nr_range, start, end); |
| 61 | } |
| 62 | |
| 63 | void subtract_range(struct range *range, int az, u64 start, u64 end) |
| 64 | { |
| 65 | int i, j; |
| 66 | |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 67 | if (start >= end) |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 68 | return; |
| 69 | |
| 70 | for (j = 0; j < az; j++) { |
| 71 | if (!range[j].end) |
| 72 | continue; |
| 73 | |
| 74 | if (start <= range[j].start && end >= range[j].end) { |
| 75 | range[j].start = 0; |
| 76 | range[j].end = 0; |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | if (start <= range[j].start && end < range[j].end && |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 81 | range[j].start < end) { |
| 82 | range[j].start = end; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 83 | continue; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | if (start > range[j].start && end >= range[j].end && |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 88 | range[j].end > start) { |
| 89 | range[j].end = start; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 90 | continue; |
| 91 | } |
| 92 | |
| 93 | if (start > range[j].start && end < range[j].end) { |
| 94 | /* Find the new spare: */ |
| 95 | for (i = 0; i < az; i++) { |
| 96 | if (range[i].end == 0) |
| 97 | break; |
| 98 | } |
| 99 | if (i < az) { |
| 100 | range[i].end = range[j].end; |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 101 | range[i].start = end; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 102 | } else { |
Lin Feng | 7fba2c2 | 2013-04-30 15:27:07 -0700 | [diff] [blame] | 103 | pr_err("%s: run out of slot in ranges\n", |
| 104 | __func__); |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 105 | } |
Yinghai Lu | e9a0064 | 2010-02-10 01:20:13 -0800 | [diff] [blame] | 106 | range[j].end = start; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 107 | continue; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static int cmp_range(const void *x1, const void *x2) |
| 113 | { |
| 114 | const struct range *r1 = x1; |
| 115 | const struct range *r2 = x2; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 116 | |
Louis Langholtz | fc7f0dd | 2015-01-15 22:04:46 -0700 | [diff] [blame] | 117 | if (r1->start < r2->start) |
| 118 | return -1; |
| 119 | if (r1->start > r2->start) |
| 120 | return 1; |
| 121 | return 0; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int clean_sort_range(struct range *range, int az) |
| 125 | { |
Alexey Khoroshilov | 834b403 | 2010-11-11 14:05:14 -0800 | [diff] [blame] | 126 | int i, j, k = az - 1, nr_range = az; |
Yinghai Lu | 27811d8 | 2010-02-10 01:20:07 -0800 | [diff] [blame] | 127 | |
| 128 | for (i = 0; i < k; i++) { |
| 129 | if (range[i].end) |
| 130 | continue; |
| 131 | for (j = k; j > i; j--) { |
| 132 | if (range[j].end) { |
| 133 | k = j; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | if (j == i) |
| 138 | break; |
| 139 | range[i].start = range[k].start; |
| 140 | range[i].end = range[k].end; |
| 141 | range[k].start = 0; |
| 142 | range[k].end = 0; |
| 143 | k--; |
| 144 | } |
| 145 | /* count it */ |
| 146 | for (i = 0; i < az; i++) { |
| 147 | if (!range[i].end) { |
| 148 | nr_range = i; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* sort them */ |
| 154 | sort(range, nr_range, sizeof(struct range), cmp_range, NULL); |
| 155 | |
| 156 | return nr_range; |
| 157 | } |
| 158 | |
| 159 | void sort_range(struct range *range, int nr_range) |
| 160 | { |
| 161 | /* sort them */ |
| 162 | sort(range, nr_range, sizeof(struct range), cmp_range, NULL); |
| 163 | } |