i8k: Add support for fan labels
[cascardo/linux.git] / kernel / range.c
index eb911db..82cfc28 100644 (file)
@@ -4,7 +4,7 @@
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/sort.h>
-
+#include <linux/string.h>
 #include <linux/range.h>
 
 int add_range(struct range *range, int az, int nr_range, u64 start, u64 end)
@@ -32,9 +32,8 @@ int add_range_with_merge(struct range *range, int az, int nr_range,
        if (start >= end)
                return nr_range;
 
-       /* Try to merge it with old one: */
+       /* get new start/end: */
        for (i = 0; i < nr_range; i++) {
-               u64 final_start, final_end;
                u64 common_start, common_end;
 
                if (!range[i].end)
@@ -45,14 +44,16 @@ int add_range_with_merge(struct range *range, int az, int nr_range,
                if (common_start > common_end)
                        continue;
 
-               final_start = min(range[i].start, start);
-               final_end = max(range[i].end, end);
+               /* new start/end, will add it back at last */
+               start = min(range[i].start, start);
+               end = max(range[i].end, end);
 
-               /* clear it and add it back for further merge */
-               range[i].start = 0;
-               range[i].end =  0;
-               return add_range_with_merge(range, az, nr_range,
-                       final_start, final_end);
+               memmove(&range[i], &range[i + 1],
+                       (nr_range - (i + 1)) * sizeof(range[i]));
+               range[nr_range - 1].start = 0;
+               range[nr_range - 1].end   = 0;
+               nr_range--;
+               i--;
        }
 
        /* Need to add it: */
@@ -112,12 +113,12 @@ static int cmp_range(const void *x1, const void *x2)
 {
        const struct range *r1 = x1;
        const struct range *r2 = x2;
-       s64 start1, start2;
-
-       start1 = r1->start;
-       start2 = r2->start;
 
-       return start1 - start2;
+       if (r1->start < r2->start)
+               return -1;
+       if (r1->start > r2->start)
+               return 1;
+       return 0;
 }
 
 int clean_sort_range(struct range *range, int az)