ALSA: ice1724 - Fix ESI Maya44 capture source control
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / range.c
blob74e2e6114927e7efa1c88bc098c74934f765f8de
1 /*
2 * Range add and subtract
3 */
4 #include <linux/module.h>
5 #include <linux/init.h>
6 #include <linux/sort.h>
8 #include <linux/range.h>
10 #ifndef ARRAY_SIZE
11 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
12 #endif
14 int add_range(struct range *range, int az, int nr_range, u64 start, u64 end)
16 if (start >= end)
17 return nr_range;
19 /* Out of slots: */
20 if (nr_range >= az)
21 return nr_range;
23 range[nr_range].start = start;
24 range[nr_range].end = end;
26 nr_range++;
28 return nr_range;
31 int add_range_with_merge(struct range *range, int az, int nr_range,
32 u64 start, u64 end)
34 int i;
36 if (start >= end)
37 return nr_range;
39 /* Try to merge it with old one: */
40 for (i = 0; i < nr_range; i++) {
41 u64 final_start, final_end;
42 u64 common_start, common_end;
44 if (!range[i].end)
45 continue;
47 common_start = max(range[i].start, start);
48 common_end = min(range[i].end, end);
49 if (common_start > common_end)
50 continue;
52 final_start = min(range[i].start, start);
53 final_end = max(range[i].end, end);
55 range[i].start = final_start;
56 range[i].end = final_end;
57 return nr_range;
60 /* Need to add it: */
61 return add_range(range, az, nr_range, start, end);
64 void subtract_range(struct range *range, int az, u64 start, u64 end)
66 int i, j;
68 if (start >= end)
69 return;
71 for (j = 0; j < az; j++) {
72 if (!range[j].end)
73 continue;
75 if (start <= range[j].start && end >= range[j].end) {
76 range[j].start = 0;
77 range[j].end = 0;
78 continue;
81 if (start <= range[j].start && end < range[j].end &&
82 range[j].start < end) {
83 range[j].start = end;
84 continue;
88 if (start > range[j].start && end >= range[j].end &&
89 range[j].end > start) {
90 range[j].end = start;
91 continue;
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;
100 if (i < az) {
101 range[i].end = range[j].end;
102 range[i].start = end;
103 } else {
104 printk(KERN_ERR "run of slot in ranges\n");
106 range[j].end = start;
107 continue;
112 static int cmp_range(const void *x1, const void *x2)
114 const struct range *r1 = x1;
115 const struct range *r2 = x2;
116 s64 start1, start2;
118 start1 = r1->start;
119 start2 = r2->start;
121 return start1 - start2;
124 int clean_sort_range(struct range *range, int az)
126 int i, j, k = az - 1, nr_range = 0;
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;
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--;
145 /* count it */
146 for (i = 0; i < az; i++) {
147 if (!range[i].end) {
148 nr_range = i;
149 break;
153 /* sort them */
154 sort(range, nr_range, sizeof(struct range), cmp_range, NULL);
156 return nr_range;
159 void sort_range(struct range *range, int nr_range)
161 /* sort them */
162 sort(range, nr_range, sizeof(struct range), cmp_range, NULL);