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