linux/kernel/range.c
<<
>>
Prefs
   1/*
   2 * Range add and subtract
   3 */
   4#include <linux/kernel.h>
   5#include <linux/init.h>
   6#include <linux/sort.h>
   7
   8#include <linux/range.h>
   9
  10int add_range(struct range *range, int az, int nr_range, u64 start, u64 end)
  11{
  12        if (start >= end)
  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
  27int add_range_with_merge(struct range *range, int az, int nr_range,
  28                     u64 start, u64 end)
  29{
  30        int i;
  31
  32        if (start >= end)
  33                return nr_range;
  34
  35        /* Try to merge it with old one: */
  36        for (i = 0; i < nr_range; i++) {
  37                u64 final_start, final_end;
  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);
  45                if (common_start > common_end)
  46                        continue;
  47
  48                final_start = min(range[i].start, start);
  49                final_end = max(range[i].end, end);
  50
  51                /* clear it and add it back for further merge */
  52                range[i].start = 0;
  53                range[i].end =  0;
  54                return add_range_with_merge(range, az, nr_range,
  55                        final_start, final_end);
  56        }
  57
  58        /* Need to add it: */
  59        return add_range(range, az, nr_range, start, end);
  60}
  61
  62void subtract_range(struct range *range, int az, u64 start, u64 end)
  63{
  64        int i, j;
  65
  66        if (start >= end)
  67                return;
  68
  69        for (j = 0; j < az; j++) {
  70                if (!range[j].end)
  71                        continue;
  72
  73                if (start <= range[j].start && end >= range[j].end) {
  74                        range[j].start = 0;
  75                        range[j].end = 0;
  76                        continue;
  77                }
  78
  79                if (start <= range[j].start && end < range[j].end &&
  80                    range[j].start < end) {
  81                        range[j].start = end;
  82                        continue;
  83                }
  84
  85
  86                if (start > range[j].start && end >= range[j].end &&
  87                    range[j].end > start) {
  88                        range[j].end = start;
  89                        continue;
  90                }
  91
  92                if (start > range[j].start && end < range[j].end) {
  93                        /* Find the new spare: */
  94                        for (i = 0; i < az; i++) {
  95                                if (range[i].end == 0)
  96                                        break;
  97                        }
  98                        if (i < az) {
  99                                range[i].end = range[j].end;
 100                                range[i].start = end;
 101                        } else {
 102                                printk(KERN_ERR "run of slot in ranges\n");
 103                        }
 104                        range[j].end = start;
 105                        continue;
 106                }
 107        }
 108}
 109
 110static int cmp_range(const void *x1, const void *x2)
 111{
 112        const struct range *r1 = x1;
 113        const struct range *r2 = x2;
 114        s64 start1, start2;
 115
 116        start1 = r1->start;
 117        start2 = r2->start;
 118
 119        return start1 - start2;
 120}
 121
 122int clean_sort_range(struct range *range, int az)
 123{
 124        int i, j, k = az - 1, nr_range = az;
 125
 126        for (i = 0; i < k; i++) {
 127                if (range[i].end)
 128                        continue;
 129                for (j = k; j > i; j--) {
 130                        if (range[j].end) {
 131                                k = j;
 132                                break;
 133                        }
 134                }
 135                if (j == i)
 136                        break;
 137                range[i].start = range[k].start;
 138                range[i].end   = range[k].end;
 139                range[k].start = 0;
 140                range[k].end   = 0;
 141                k--;
 142        }
 143        /* count it */
 144        for (i = 0; i < az; i++) {
 145                if (!range[i].end) {
 146                        nr_range = i;
 147                        break;
 148                }
 149        }
 150
 151        /* sort them */
 152        sort(range, nr_range, sizeof(struct range), cmp_range, NULL);
 153
 154        return nr_range;
 155}
 156
 157void sort_range(struct range *range, int nr_range)
 158{
 159        /* sort them */
 160        sort(range, nr_range, sizeof(struct range), cmp_range, NULL);
 161}
 162
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.