2 * Range add and subtract
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>
10 int add_range(struct range
*range
, int az
, int nr_range
, u64 start
, u64 end
)
19 range
[nr_range
].start
= start
;
20 range
[nr_range
].end
= end
;
27 int add_range_with_merge(struct range
*range
, int az
, int nr_range
,
35 /* get new start/end: */
36 for (i
= 0; i
< nr_range
; i
++) {
37 u64 common_start
, common_end
;
42 common_start
= max(range
[i
].start
, start
);
43 common_end
= min(range
[i
].end
, end
);
44 if (common_start
> common_end
)
47 /* new start/end, will add it back at last */
48 start
= min(range
[i
].start
, start
);
49 end
= max(range
[i
].end
, end
);
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;
60 return add_range(range
, az
, nr_range
, start
, end
);
63 void subtract_range(struct range
*range
, int az
, u64 start
, u64 end
)
70 for (j
= 0; j
< az
; j
++) {
74 if (start
<= range
[j
].start
&& end
>= range
[j
].end
) {
80 if (start
<= range
[j
].start
&& end
< range
[j
].end
&&
81 range
[j
].start
< end
) {
87 if (start
> range
[j
].start
&& end
>= range
[j
].end
&&
88 range
[j
].end
> start
) {
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)
100 range
[i
].end
= range
[j
].end
;
101 range
[i
].start
= end
;
103 pr_err("%s: run out of slot in ranges\n",
106 range
[j
].end
= start
;
112 static int cmp_range(const void *x1
, const void *x2
)
114 const struct range
*r1
= x1
;
115 const struct range
*r2
= x2
;
121 return start1
- start2
;
124 int clean_sort_range(struct range
*range
, int az
)
126 int i
, j
, k
= az
- 1, nr_range
= az
;
128 for (i
= 0; i
< k
; i
++) {
131 for (j
= k
; j
> i
; j
--) {
139 range
[i
].start
= range
[k
].start
;
140 range
[i
].end
= range
[k
].end
;
146 for (i
= 0; i
< az
; i
++) {
154 sort(range
, nr_range
, sizeof(struct range
), cmp_range
, NULL
);
159 void sort_range(struct range
*range
, int nr_range
)
162 sort(range
, nr_range
, sizeof(struct range
), cmp_range
, NULL
);
This page took 0.032998 seconds and 5 git commands to generate.