Rev 3391 | Rev 5270 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3391 | Serge | 1 | /* |
2 | * lib/bitmap.c |
||
3 | * Helper functions for bitmap.h. |
||
4 | * |
||
5056 | serge | 5 | * This source code is licensed under the GNU General Public License, |
3391 | Serge | 6 | * Version 2. See the file COPYING for more details. |
7 | */ |
||
8 | #include |
||
9 | #include |
||
10 | //#include |
||
11 | #include |
||
12 | #include |
||
13 | #include |
||
14 | #include |
||
15 | #include |
||
16 | //#include |
||
17 | |||
18 | /* |
||
19 | * bitmaps provide an array of bits, implemented using an an |
||
20 | * array of unsigned longs. The number of valid bits in a |
||
21 | * given bitmap does _not_ need to be an exact multiple of |
||
22 | * BITS_PER_LONG. |
||
23 | * |
||
24 | * The possible unused bits in the last, partially used word |
||
25 | * of a bitmap are 'don't care'. The implementation makes |
||
26 | * no particular effort to keep them zero. It ensures that |
||
27 | * their value will not affect the results of any operation. |
||
28 | * The bitmap operations that return Boolean (bitmap_empty, |
||
29 | * for example) or scalar (bitmap_weight, for example) results |
||
30 | * carefully filter out these unused bits from impacting their |
||
31 | * results. |
||
32 | * |
||
33 | * These operations actually hold to a slightly stronger rule: |
||
34 | * if you don't input any bitmaps to these ops that have some |
||
35 | * unused bits set, then they won't output any set unused bits |
||
36 | * in output bitmaps. |
||
37 | * |
||
38 | * The byte ordering of bitmaps is more natural on little |
||
39 | * endian architectures. See the big-endian headers |
||
40 | * include/asm-ppc64/bitops.h and include/asm-s390/bitops.h |
||
41 | * for the best explanations of this ordering. |
||
42 | */ |
||
43 | |||
5056 | serge | 44 | int __bitmap_empty(const unsigned long *bitmap, unsigned int bits) |
3391 | Serge | 45 | { |
5056 | serge | 46 | unsigned int k, lim = bits/BITS_PER_LONG; |
3391 | Serge | 47 | for (k = 0; k < lim; ++k) |
48 | if (bitmap[k]) |
||
49 | return 0; |
||
50 | |||
51 | if (bits % BITS_PER_LONG) |
||
52 | if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) |
||
53 | return 0; |
||
54 | |||
55 | return 1; |
||
56 | } |
||
57 | EXPORT_SYMBOL(__bitmap_empty); |
||
58 | |||
5056 | serge | 59 | int __bitmap_full(const unsigned long *bitmap, unsigned int bits) |
3391 | Serge | 60 | { |
5056 | serge | 61 | unsigned int k, lim = bits/BITS_PER_LONG; |
3391 | Serge | 62 | for (k = 0; k < lim; ++k) |
63 | if (~bitmap[k]) |
||
64 | return 0; |
||
65 | |||
66 | if (bits % BITS_PER_LONG) |
||
67 | if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) |
||
68 | return 0; |
||
69 | |||
70 | return 1; |
||
71 | } |
||
72 | EXPORT_SYMBOL(__bitmap_full); |
||
73 | |||
74 | int __bitmap_equal(const unsigned long *bitmap1, |
||
5056 | serge | 75 | const unsigned long *bitmap2, unsigned int bits) |
3391 | Serge | 76 | { |
5056 | serge | 77 | unsigned int k, lim = bits/BITS_PER_LONG; |
3391 | Serge | 78 | for (k = 0; k < lim; ++k) |
79 | if (bitmap1[k] != bitmap2[k]) |
||
80 | return 0; |
||
81 | |||
82 | if (bits % BITS_PER_LONG) |
||
83 | if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) |
||
84 | return 0; |
||
85 | |||
86 | return 1; |
||
87 | } |
||
88 | EXPORT_SYMBOL(__bitmap_equal); |
||
89 | |||
5056 | serge | 90 | void __bitmap_complement(unsigned long *dst, const unsigned long *src, unsigned int bits) |
3391 | Serge | 91 | { |
5056 | serge | 92 | unsigned int k, lim = bits/BITS_PER_LONG; |
3391 | Serge | 93 | for (k = 0; k < lim; ++k) |
94 | dst[k] = ~src[k]; |
||
95 | |||
96 | if (bits % BITS_PER_LONG) |
||
5056 | serge | 97 | dst[k] = ~src[k]; |
3391 | Serge | 98 | } |
99 | EXPORT_SYMBOL(__bitmap_complement); |
||
100 | |||
101 | /** |
||
102 | * __bitmap_shift_right - logical right shift of the bits in a bitmap |
||
103 | * @dst : destination bitmap |
||
104 | * @src : source bitmap |
||
105 | * @shift : shift by this many bits |
||
106 | * @bits : bitmap size, in bits |
||
107 | * |
||
108 | * Shifting right (dividing) means moving bits in the MS -> LS bit |
||
109 | * direction. Zeros are fed into the vacated MS positions and the |
||
110 | * LS bits shifted off the bottom are lost. |
||
111 | */ |
||
112 | void __bitmap_shift_right(unsigned long *dst, |
||
113 | const unsigned long *src, int shift, int bits) |
||
114 | { |
||
115 | int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG; |
||
116 | int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG; |
||
117 | unsigned long mask = (1UL << left) - 1; |
||
118 | for (k = 0; off + k < lim; ++k) { |
||
119 | unsigned long upper, lower; |
||
120 | |||
121 | /* |
||
122 | * If shift is not word aligned, take lower rem bits of |
||
123 | * word above and make them the top rem bits of result. |
||
124 | */ |
||
125 | if (!rem || off + k + 1 >= lim) |
||
126 | upper = 0; |
||
127 | else { |
||
128 | upper = src[off + k + 1]; |
||
129 | if (off + k + 1 == lim - 1 && left) |
||
130 | upper &= mask; |
||
131 | } |
||
132 | lower = src[off + k]; |
||
133 | if (left && off + k == lim - 1) |
||
134 | lower &= mask; |
||
135 | dst[k] = upper << (BITS_PER_LONG - rem) | lower >> rem; |
||
136 | if (left && k == lim - 1) |
||
137 | dst[k] &= mask; |
||
138 | } |
||
139 | if (off) |
||
140 | memset(&dst[lim - off], 0, off*sizeof(unsigned long)); |
||
141 | } |
||
142 | EXPORT_SYMBOL(__bitmap_shift_right); |
||
143 | |||
144 | |||
145 | /** |
||
146 | * __bitmap_shift_left - logical left shift of the bits in a bitmap |
||
147 | * @dst : destination bitmap |
||
148 | * @src : source bitmap |
||
149 | * @shift : shift by this many bits |
||
150 | * @bits : bitmap size, in bits |
||
151 | * |
||
152 | * Shifting left (multiplying) means moving bits in the LS -> MS |
||
153 | * direction. Zeros are fed into the vacated LS bit positions |
||
154 | * and those MS bits shifted off the top are lost. |
||
155 | */ |
||
156 | |||
157 | void __bitmap_shift_left(unsigned long *dst, |
||
158 | const unsigned long *src, int shift, int bits) |
||
159 | { |
||
160 | int k, lim = BITS_TO_LONGS(bits), left = bits % BITS_PER_LONG; |
||
161 | int off = shift/BITS_PER_LONG, rem = shift % BITS_PER_LONG; |
||
162 | for (k = lim - off - 1; k >= 0; --k) { |
||
163 | unsigned long upper, lower; |
||
164 | |||
165 | /* |
||
166 | * If shift is not word aligned, take upper rem bits of |
||
167 | * word below and make them the bottom rem bits of result. |
||
168 | */ |
||
169 | if (rem && k > 0) |
||
170 | lower = src[k - 1]; |
||
171 | else |
||
172 | lower = 0; |
||
173 | upper = src[k]; |
||
174 | if (left && k == lim - 1) |
||
175 | upper &= (1UL << left) - 1; |
||
176 | dst[k + off] = lower >> (BITS_PER_LONG - rem) | upper << rem; |
||
177 | if (left && k + off == lim - 1) |
||
178 | dst[k + off] &= (1UL << left) - 1; |
||
179 | } |
||
180 | if (off) |
||
181 | memset(dst, 0, off*sizeof(unsigned long)); |
||
182 | } |
||
183 | EXPORT_SYMBOL(__bitmap_shift_left); |
||
184 | |||
185 | int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
||
5056 | serge | 186 | const unsigned long *bitmap2, unsigned int bits) |
3391 | Serge | 187 | { |
5056 | serge | 188 | unsigned int k; |
189 | unsigned int lim = bits/BITS_PER_LONG; |
||
3391 | Serge | 190 | unsigned long result = 0; |
191 | |||
5056 | serge | 192 | for (k = 0; k < lim; k++) |
3391 | Serge | 193 | result |= (dst[k] = bitmap1[k] & bitmap2[k]); |
5056 | serge | 194 | if (bits % BITS_PER_LONG) |
195 | result |= (dst[k] = bitmap1[k] & bitmap2[k] & |
||
196 | BITMAP_LAST_WORD_MASK(bits)); |
||
3391 | Serge | 197 | return result != 0; |
198 | } |
||
199 | EXPORT_SYMBOL(__bitmap_and); |
||
200 | |||
201 | void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
||
5056 | serge | 202 | const unsigned long *bitmap2, unsigned int bits) |
3391 | Serge | 203 | { |
5056 | serge | 204 | unsigned int k; |
205 | unsigned int nr = BITS_TO_LONGS(bits); |
||
3391 | Serge | 206 | |
207 | for (k = 0; k < nr; k++) |
||
208 | dst[k] = bitmap1[k] | bitmap2[k]; |
||
209 | } |
||
210 | EXPORT_SYMBOL(__bitmap_or); |
||
211 | |||
212 | void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, |
||
5056 | serge | 213 | const unsigned long *bitmap2, unsigned int bits) |
3391 | Serge | 214 | { |
5056 | serge | 215 | unsigned int k; |
216 | unsigned int nr = BITS_TO_LONGS(bits); |
||
3391 | Serge | 217 | |
218 | for (k = 0; k < nr; k++) |
||
219 | dst[k] = bitmap1[k] ^ bitmap2[k]; |
||
220 | } |
||
221 | EXPORT_SYMBOL(__bitmap_xor); |
||
222 | |||
223 | int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, |
||
5056 | serge | 224 | const unsigned long *bitmap2, unsigned int bits) |
3391 | Serge | 225 | { |
5056 | serge | 226 | unsigned int k; |
227 | unsigned int lim = bits/BITS_PER_LONG; |
||
3391 | Serge | 228 | unsigned long result = 0; |
229 | |||
5056 | serge | 230 | for (k = 0; k < lim; k++) |
3391 | Serge | 231 | result |= (dst[k] = bitmap1[k] & ~bitmap2[k]); |
5056 | serge | 232 | if (bits % BITS_PER_LONG) |
233 | result |= (dst[k] = bitmap1[k] & ~bitmap2[k] & |
||
234 | BITMAP_LAST_WORD_MASK(bits)); |
||
3391 | Serge | 235 | return result != 0; |
236 | } |
||
237 | EXPORT_SYMBOL(__bitmap_andnot); |
||
238 | |||
239 | int __bitmap_intersects(const unsigned long *bitmap1, |
||
5056 | serge | 240 | const unsigned long *bitmap2, unsigned int bits) |
3391 | Serge | 241 | { |
5056 | serge | 242 | unsigned int k, lim = bits/BITS_PER_LONG; |
3391 | Serge | 243 | for (k = 0; k < lim; ++k) |
244 | if (bitmap1[k] & bitmap2[k]) |
||
245 | return 1; |
||
246 | |||
247 | if (bits % BITS_PER_LONG) |
||
248 | if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) |
||
249 | return 1; |
||
250 | return 0; |
||
251 | } |
||
252 | EXPORT_SYMBOL(__bitmap_intersects); |
||
253 | |||
254 | int __bitmap_subset(const unsigned long *bitmap1, |
||
5056 | serge | 255 | const unsigned long *bitmap2, unsigned int bits) |
3391 | Serge | 256 | { |
5056 | serge | 257 | unsigned int k, lim = bits/BITS_PER_LONG; |
3391 | Serge | 258 | for (k = 0; k < lim; ++k) |
259 | if (bitmap1[k] & ~bitmap2[k]) |
||
260 | return 0; |
||
261 | |||
262 | if (bits % BITS_PER_LONG) |
||
263 | if ((bitmap1[k] & ~bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) |
||
264 | return 0; |
||
265 | return 1; |
||
266 | } |
||
267 | EXPORT_SYMBOL(__bitmap_subset); |
||
268 | |||
5056 | serge | 269 | int __bitmap_weight(const unsigned long *bitmap, unsigned int bits) |
3391 | Serge | 270 | { |
5056 | serge | 271 | unsigned int k, lim = bits/BITS_PER_LONG; |
272 | int w = 0; |
||
3391 | Serge | 273 | |
274 | for (k = 0; k < lim; k++) |
||
275 | w += hweight_long(bitmap[k]); |
||
276 | |||
277 | if (bits % BITS_PER_LONG) |
||
278 | w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits)); |
||
279 | |||
280 | return w; |
||
281 | } |
||
282 | EXPORT_SYMBOL(__bitmap_weight); |
||
283 | |||
5056 | serge | 284 | void bitmap_set(unsigned long *map, unsigned int start, int len) |
3391 | Serge | 285 | { |
286 | unsigned long *p = map + BIT_WORD(start); |
||
5056 | serge | 287 | const unsigned int size = start + len; |
3391 | Serge | 288 | int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); |
289 | unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); |
||
290 | |||
5056 | serge | 291 | while (len - bits_to_set >= 0) { |
3391 | Serge | 292 | *p |= mask_to_set; |
5056 | serge | 293 | len -= bits_to_set; |
3391 | Serge | 294 | bits_to_set = BITS_PER_LONG; |
295 | mask_to_set = ~0UL; |
||
296 | p++; |
||
297 | } |
||
5056 | serge | 298 | if (len) { |
3391 | Serge | 299 | mask_to_set &= BITMAP_LAST_WORD_MASK(size); |
300 | *p |= mask_to_set; |
||
301 | } |
||
302 | } |
||
303 | EXPORT_SYMBOL(bitmap_set); |
||
304 | |||
5056 | serge | 305 | void bitmap_clear(unsigned long *map, unsigned int start, int len) |
3391 | Serge | 306 | { |
307 | unsigned long *p = map + BIT_WORD(start); |
||
5056 | serge | 308 | const unsigned int size = start + len; |
3391 | Serge | 309 | int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); |
310 | unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); |
||
311 | |||
5056 | serge | 312 | while (len - bits_to_clear >= 0) { |
3391 | Serge | 313 | *p &= ~mask_to_clear; |
5056 | serge | 314 | len -= bits_to_clear; |
3391 | Serge | 315 | bits_to_clear = BITS_PER_LONG; |
316 | mask_to_clear = ~0UL; |
||
317 | p++; |
||
318 | } |
||
5056 | serge | 319 | if (len) { |
3391 | Serge | 320 | mask_to_clear &= BITMAP_LAST_WORD_MASK(size); |
321 | *p &= ~mask_to_clear; |
||
322 | } |
||
323 | } |
||
324 | EXPORT_SYMBOL(bitmap_clear); |
||
325 | |||
326 | /* |
||
327 | * bitmap_find_next_zero_area - find a contiguous aligned zero area |
||
328 | * @map: The address to base the search on |
||
329 | * @size: The bitmap size in bits |
||
330 | * @start: The bitnumber to start searching at |
||
331 | * @nr: The number of zeroed bits we're looking for |
||
332 | * @align_mask: Alignment mask for zero area |
||
333 | * |
||
334 | * The @align_mask should be one less than a power of 2; the effect is that |
||
335 | * the bit offset of all zero areas this function finds is multiples of that |
||
336 | * power of 2. A @align_mask of 0 means no alignment is required. |
||
337 | */ |
||
338 | unsigned long bitmap_find_next_zero_area(unsigned long *map, |
||
339 | unsigned long size, |
||
340 | unsigned long start, |
||
341 | unsigned int nr, |
||
342 | unsigned long align_mask) |
||
343 | { |
||
344 | unsigned long index, end, i; |
||
345 | again: |
||
346 | index = find_next_zero_bit(map, size, start); |
||
347 | |||
348 | /* Align allocation */ |
||
349 | index = __ALIGN_MASK(index, align_mask); |
||
350 | |||
351 | end = index + nr; |
||
352 | if (end > size) |
||
353 | return end; |
||
354 | i = find_next_bit(map, end, index); |
||
355 | if (i < end) { |
||
356 | start = i + 1; |
||
357 | goto again; |
||
358 | } |
||
359 | return index; |
||
360 | } |
||
361 | EXPORT_SYMBOL(bitmap_find_next_zero_area); |
||
362 | |||
363 | /* |
||
364 | * Bitmap printing & parsing functions: first version by Nadia Yvette Chambers, |
||
365 | * second version by Paul Jackson, third by Joe Korty. |
||
366 | */ |
||
367 | |||
368 | #define CHUNKSZ 32 |
||
369 | #define nbits_to_hold_value(val) fls(val) |
||
370 | #define BASEDEC 10 /* fancier cpuset lists input in decimal */ |
||
371 | |||
372 | |||
373 | |||
374 | |||
375 | |||
376 | /** |
||
377 | * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap |
||
378 | * @buf: pointer to a bitmap |
||
379 | * @pos: a bit position in @buf (0 <= @pos < @bits) |
||
380 | * @bits: number of valid bit positions in @buf |
||
381 | * |
||
382 | * Map the bit at position @pos in @buf (of length @bits) to the |
||
383 | * ordinal of which set bit it is. If it is not set or if @pos |
||
384 | * is not a valid bit position, map to -1. |
||
385 | * |
||
386 | * If for example, just bits 4 through 7 are set in @buf, then @pos |
||
387 | * values 4 through 7 will get mapped to 0 through 3, respectively, |
||
5056 | serge | 388 | * and other @pos values will get mapped to -1. When @pos value 7 |
3391 | Serge | 389 | * gets mapped to (returns) @ord value 3 in this example, that means |
390 | * that bit 7 is the 3rd (starting with 0th) set bit in @buf. |
||
391 | * |
||
392 | * The bit positions 0 through @bits are valid positions in @buf. |
||
393 | */ |
||
394 | static int bitmap_pos_to_ord(const unsigned long *buf, int pos, int bits) |
||
395 | { |
||
396 | int i, ord; |
||
397 | |||
398 | if (pos < 0 || pos >= bits || !test_bit(pos, buf)) |
||
399 | return -1; |
||
400 | |||
401 | i = find_first_bit(buf, bits); |
||
402 | ord = 0; |
||
403 | while (i < pos) { |
||
404 | i = find_next_bit(buf, bits, i + 1); |
||
405 | ord++; |
||
406 | } |
||
407 | BUG_ON(i != pos); |
||
408 | |||
409 | return ord; |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * bitmap_ord_to_pos - find position of n-th set bit in bitmap |
||
414 | * @buf: pointer to bitmap |
||
415 | * @ord: ordinal bit position (n-th set bit, n >= 0) |
||
416 | * @bits: number of valid bit positions in @buf |
||
417 | * |
||
418 | * Map the ordinal offset of bit @ord in @buf to its position in @buf. |
||
419 | * Value of @ord should be in range 0 <= @ord < weight(buf), else |
||
420 | * results are undefined. |
||
421 | * |
||
422 | * If for example, just bits 4 through 7 are set in @buf, then @ord |
||
423 | * values 0 through 3 will get mapped to 4 through 7, respectively, |
||
424 | * and all other @ord values return undefined values. When @ord value 3 |
||
425 | * gets mapped to (returns) @pos value 7 in this example, that means |
||
426 | * that the 3rd set bit (starting with 0th) is at position 7 in @buf. |
||
427 | * |
||
428 | * The bit positions 0 through @bits are valid positions in @buf. |
||
429 | */ |
||
430 | int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits) |
||
431 | { |
||
432 | int pos = 0; |
||
433 | |||
434 | if (ord >= 0 && ord < bits) { |
||
435 | int i; |
||
436 | |||
437 | for (i = find_first_bit(buf, bits); |
||
438 | i < bits && ord > 0; |
||
439 | i = find_next_bit(buf, bits, i + 1)) |
||
440 | ord--; |
||
441 | if (i < bits && ord == 0) |
||
442 | pos = i; |
||
443 | } |
||
444 | |||
445 | return pos; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * bitmap_remap - Apply map defined by a pair of bitmaps to another bitmap |
||
450 | * @dst: remapped result |
||
451 | * @src: subset to be remapped |
||
452 | * @old: defines domain of map |
||
453 | * @new: defines range of map |
||
454 | * @bits: number of bits in each of these bitmaps |
||
455 | * |
||
456 | * Let @old and @new define a mapping of bit positions, such that |
||
457 | * whatever position is held by the n-th set bit in @old is mapped |
||
458 | * to the n-th set bit in @new. In the more general case, allowing |
||
459 | * for the possibility that the weight 'w' of @new is less than the |
||
460 | * weight of @old, map the position of the n-th set bit in @old to |
||
461 | * the position of the m-th set bit in @new, where m == n % w. |
||
462 | * |
||
463 | * If either of the @old and @new bitmaps are empty, or if @src and |
||
464 | * @dst point to the same location, then this routine copies @src |
||
465 | * to @dst. |
||
466 | * |
||
467 | * The positions of unset bits in @old are mapped to themselves |
||
468 | * (the identify map). |
||
469 | * |
||
470 | * Apply the above specified mapping to @src, placing the result in |
||
471 | * @dst, clearing any bits previously set in @dst. |
||
472 | * |
||
473 | * For example, lets say that @old has bits 4 through 7 set, and |
||
474 | * @new has bits 12 through 15 set. This defines the mapping of bit |
||
475 | * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other |
||
476 | * bit positions unchanged. So if say @src comes into this routine |
||
477 | * with bits 1, 5 and 7 set, then @dst should leave with bits 1, |
||
478 | * 13 and 15 set. |
||
479 | */ |
||
480 | void bitmap_remap(unsigned long *dst, const unsigned long *src, |
||
481 | const unsigned long *old, const unsigned long *new, |
||
482 | int bits) |
||
483 | { |
||
484 | int oldbit, w; |
||
485 | |||
486 | if (dst == src) /* following doesn't handle inplace remaps */ |
||
487 | return; |
||
488 | bitmap_zero(dst, bits); |
||
489 | |||
490 | w = bitmap_weight(new, bits); |
||
491 | for_each_set_bit(oldbit, src, bits) { |
||
492 | int n = bitmap_pos_to_ord(old, oldbit, bits); |
||
493 | |||
494 | if (n < 0 || w == 0) |
||
495 | set_bit(oldbit, dst); /* identity map */ |
||
496 | else |
||
497 | set_bit(bitmap_ord_to_pos(new, n % w, bits), dst); |
||
498 | } |
||
499 | } |
||
500 | EXPORT_SYMBOL(bitmap_remap); |
||
501 | |||
502 | /** |
||
503 | * bitmap_bitremap - Apply map defined by a pair of bitmaps to a single bit |
||
504 | * @oldbit: bit position to be mapped |
||
505 | * @old: defines domain of map |
||
506 | * @new: defines range of map |
||
507 | * @bits: number of bits in each of these bitmaps |
||
508 | * |
||
509 | * Let @old and @new define a mapping of bit positions, such that |
||
510 | * whatever position is held by the n-th set bit in @old is mapped |
||
511 | * to the n-th set bit in @new. In the more general case, allowing |
||
512 | * for the possibility that the weight 'w' of @new is less than the |
||
513 | * weight of @old, map the position of the n-th set bit in @old to |
||
514 | * the position of the m-th set bit in @new, where m == n % w. |
||
515 | * |
||
516 | * The positions of unset bits in @old are mapped to themselves |
||
517 | * (the identify map). |
||
518 | * |
||
519 | * Apply the above specified mapping to bit position @oldbit, returning |
||
520 | * the new bit position. |
||
521 | * |
||
522 | * For example, lets say that @old has bits 4 through 7 set, and |
||
523 | * @new has bits 12 through 15 set. This defines the mapping of bit |
||
524 | * position 4 to 12, 5 to 13, 6 to 14 and 7 to 15, and of all other |
||
525 | * bit positions unchanged. So if say @oldbit is 5, then this routine |
||
526 | * returns 13. |
||
527 | */ |
||
528 | int bitmap_bitremap(int oldbit, const unsigned long *old, |
||
529 | const unsigned long *new, int bits) |
||
530 | { |
||
531 | int w = bitmap_weight(new, bits); |
||
532 | int n = bitmap_pos_to_ord(old, oldbit, bits); |
||
533 | if (n < 0 || w == 0) |
||
534 | return oldbit; |
||
535 | else |
||
536 | return bitmap_ord_to_pos(new, n % w, bits); |
||
537 | } |
||
538 | EXPORT_SYMBOL(bitmap_bitremap); |
||
539 | |||
540 | /** |
||
541 | * bitmap_onto - translate one bitmap relative to another |
||
542 | * @dst: resulting translated bitmap |
||
543 | * @orig: original untranslated bitmap |
||
544 | * @relmap: bitmap relative to which translated |
||
545 | * @bits: number of bits in each of these bitmaps |
||
546 | * |
||
547 | * Set the n-th bit of @dst iff there exists some m such that the |
||
548 | * n-th bit of @relmap is set, the m-th bit of @orig is set, and |
||
549 | * the n-th bit of @relmap is also the m-th _set_ bit of @relmap. |
||
550 | * (If you understood the previous sentence the first time your |
||
551 | * read it, you're overqualified for your current job.) |
||
552 | * |
||
553 | * In other words, @orig is mapped onto (surjectively) @dst, |
||
554 | * using the the map { |
||
555 | * m-th set bit of @relmap }. |
||
556 | * |
||
557 | * Any set bits in @orig above bit number W, where W is the |
||
558 | * weight of (number of set bits in) @relmap are mapped nowhere. |
||
559 | * In particular, if for all bits m set in @orig, m >= W, then |
||
560 | * @dst will end up empty. In situations where the possibility |
||
561 | * of such an empty result is not desired, one way to avoid it is |
||
562 | * to use the bitmap_fold() operator, below, to first fold the |
||
563 | * @orig bitmap over itself so that all its set bits x are in the |
||
564 | * range 0 <= x < W. The bitmap_fold() operator does this by |
||
565 | * setting the bit (m % W) in @dst, for each bit (m) set in @orig. |
||
566 | * |
||
567 | * Example [1] for bitmap_onto(): |
||
568 | * Let's say @relmap has bits 30-39 set, and @orig has bits |
||
569 | * 1, 3, 5, 7, 9 and 11 set. Then on return from this routine, |
||
570 | * @dst will have bits 31, 33, 35, 37 and 39 set. |
||
571 | * |
||
572 | * When bit 0 is set in @orig, it means turn on the bit in |
||
573 | * @dst corresponding to whatever is the first bit (if any) |
||
574 | * that is turned on in @relmap. Since bit 0 was off in the |
||
575 | * above example, we leave off that bit (bit 30) in @dst. |
||
576 | * |
||
577 | * When bit 1 is set in @orig (as in the above example), it |
||
578 | * means turn on the bit in @dst corresponding to whatever |
||
579 | * is the second bit that is turned on in @relmap. The second |
||
580 | * bit in @relmap that was turned on in the above example was |
||
581 | * bit 31, so we turned on bit 31 in @dst. |
||
582 | * |
||
583 | * Similarly, we turned on bits 33, 35, 37 and 39 in @dst, |
||
584 | * because they were the 4th, 6th, 8th and 10th set bits |
||
585 | * set in @relmap, and the 4th, 6th, 8th and 10th bits of |
||
586 | * @orig (i.e. bits 3, 5, 7 and 9) were also set. |
||
587 | * |
||
588 | * When bit 11 is set in @orig, it means turn on the bit in |
||
589 | * @dst corresponding to whatever is the twelfth bit that is |
||
590 | * turned on in @relmap. In the above example, there were |
||
591 | * only ten bits turned on in @relmap (30..39), so that bit |
||
592 | * 11 was set in @orig had no affect on @dst. |
||
593 | * |
||
594 | * Example [2] for bitmap_fold() + bitmap_onto(): |
||
595 | * Let's say @relmap has these ten bits set: |
||
596 | * 40 41 42 43 45 48 53 61 74 95 |
||
597 | * (for the curious, that's 40 plus the first ten terms of the |
||
598 | * Fibonacci sequence.) |
||
599 | * |
||
600 | * Further lets say we use the following code, invoking |
||
601 | * bitmap_fold() then bitmap_onto, as suggested above to |
||
602 | * avoid the possitility of an empty @dst result: |
||
603 | * |
||
604 | * unsigned long *tmp; // a temporary bitmap's bits |
||
605 | * |
||
606 | * bitmap_fold(tmp, orig, bitmap_weight(relmap, bits), bits); |
||
607 | * bitmap_onto(dst, tmp, relmap, bits); |
||
608 | * |
||
609 | * Then this table shows what various values of @dst would be, for |
||
610 | * various @orig's. I list the zero-based positions of each set bit. |
||
611 | * The tmp column shows the intermediate result, as computed by |
||
612 | * using bitmap_fold() to fold the @orig bitmap modulo ten |
||
613 | * (the weight of @relmap). |
||
614 | * |
||
615 | * @orig tmp @dst |
||
616 | * 0 0 40 |
||
617 | * 1 1 41 |
||
618 | * 9 9 95 |
||
619 | * 10 0 40 (*) |
||
620 | * 1 3 5 7 1 3 5 7 41 43 48 61 |
||
621 | * 0 1 2 3 4 0 1 2 3 4 40 41 42 43 45 |
||
622 | * 0 9 18 27 0 9 8 7 40 61 74 95 |
||
623 | * 0 10 20 30 0 40 |
||
624 | * 0 11 22 33 0 1 2 3 40 41 42 43 |
||
625 | * 0 12 24 36 0 2 4 6 40 42 45 53 |
||
626 | * 78 102 211 1 2 8 41 42 74 (*) |
||
627 | * |
||
628 | * (*) For these marked lines, if we hadn't first done bitmap_fold() |
||
629 | * into tmp, then the @dst result would have been empty. |
||
630 | * |
||
631 | * If either of @orig or @relmap is empty (no set bits), then @dst |
||
632 | * will be returned empty. |
||
633 | * |
||
634 | * If (as explained above) the only set bits in @orig are in positions |
||
635 | * m where m >= W, (where W is the weight of @relmap) then @dst will |
||
636 | * once again be returned empty. |
||
637 | * |
||
638 | * All bits in @dst not set by the above rule are cleared. |
||
639 | */ |
||
640 | void bitmap_onto(unsigned long *dst, const unsigned long *orig, |
||
641 | const unsigned long *relmap, int bits) |
||
642 | { |
||
643 | int n, m; /* same meaning as in above comment */ |
||
644 | |||
645 | if (dst == orig) /* following doesn't handle inplace mappings */ |
||
646 | return; |
||
647 | bitmap_zero(dst, bits); |
||
648 | |||
649 | /* |
||
650 | * The following code is a more efficient, but less |
||
651 | * obvious, equivalent to the loop: |
||
652 | * for (m = 0; m < bitmap_weight(relmap, bits); m++) { |
||
653 | * n = bitmap_ord_to_pos(orig, m, bits); |
||
654 | * if (test_bit(m, orig)) |
||
655 | * set_bit(n, dst); |
||
656 | * } |
||
657 | */ |
||
658 | |||
659 | m = 0; |
||
660 | for_each_set_bit(n, relmap, bits) { |
||
661 | /* m == bitmap_pos_to_ord(relmap, n, bits) */ |
||
662 | if (test_bit(m, orig)) |
||
663 | set_bit(n, dst); |
||
664 | m++; |
||
665 | } |
||
666 | } |
||
667 | EXPORT_SYMBOL(bitmap_onto); |
||
668 | |||
669 | /** |
||
670 | * bitmap_fold - fold larger bitmap into smaller, modulo specified size |
||
671 | * @dst: resulting smaller bitmap |
||
672 | * @orig: original larger bitmap |
||
673 | * @sz: specified size |
||
674 | * @bits: number of bits in each of these bitmaps |
||
675 | * |
||
676 | * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst. |
||
677 | * Clear all other bits in @dst. See further the comment and |
||
678 | * Example [2] for bitmap_onto() for why and how to use this. |
||
679 | */ |
||
680 | void bitmap_fold(unsigned long *dst, const unsigned long *orig, |
||
681 | int sz, int bits) |
||
682 | { |
||
683 | int oldbit; |
||
684 | |||
685 | if (dst == orig) /* following doesn't handle inplace mappings */ |
||
686 | return; |
||
687 | bitmap_zero(dst, bits); |
||
688 | |||
689 | for_each_set_bit(oldbit, orig, bits) |
||
690 | set_bit(oldbit % sz, dst); |
||
691 | } |
||
692 | EXPORT_SYMBOL(bitmap_fold); |
||
693 | |||
694 | /* |
||
695 | * Common code for bitmap_*_region() routines. |
||
696 | * bitmap: array of unsigned longs corresponding to the bitmap |
||
697 | * pos: the beginning of the region |
||
698 | * order: region size (log base 2 of number of bits) |
||
699 | * reg_op: operation(s) to perform on that region of bitmap |
||
700 | * |
||
701 | * Can set, verify and/or release a region of bits in a bitmap, |
||
702 | * depending on which combination of REG_OP_* flag bits is set. |
||
703 | * |
||
704 | * A region of a bitmap is a sequence of bits in the bitmap, of |
||
705 | * some size '1 << order' (a power of two), aligned to that same |
||
706 | * '1 << order' power of two. |
||
707 | * |
||
708 | * Returns 1 if REG_OP_ISFREE succeeds (region is all zero bits). |
||
709 | * Returns 0 in all other cases and reg_ops. |
||
710 | */ |
||
711 | |||
712 | enum { |
||
713 | REG_OP_ISFREE, /* true if region is all zero bits */ |
||
714 | REG_OP_ALLOC, /* set all bits in region */ |
||
715 | REG_OP_RELEASE, /* clear all bits in region */ |
||
716 | }; |
||
717 | |||
5056 | serge | 718 | static int __reg_op(unsigned long *bitmap, unsigned int pos, int order, int reg_op) |
3391 | Serge | 719 | { |
720 | int nbits_reg; /* number of bits in region */ |
||
721 | int index; /* index first long of region in bitmap */ |
||
722 | int offset; /* bit offset region in bitmap[index] */ |
||
723 | int nlongs_reg; /* num longs spanned by region in bitmap */ |
||
724 | int nbitsinlong; /* num bits of region in each spanned long */ |
||
725 | unsigned long mask; /* bitmask for one long of region */ |
||
726 | int i; /* scans bitmap by longs */ |
||
727 | int ret = 0; /* return value */ |
||
728 | |||
729 | /* |
||
730 | * Either nlongs_reg == 1 (for small orders that fit in one long) |
||
731 | * or (offset == 0 && mask == ~0UL) (for larger multiword orders.) |
||
732 | */ |
||
733 | nbits_reg = 1 << order; |
||
734 | index = pos / BITS_PER_LONG; |
||
735 | offset = pos - (index * BITS_PER_LONG); |
||
736 | nlongs_reg = BITS_TO_LONGS(nbits_reg); |
||
737 | nbitsinlong = min(nbits_reg, BITS_PER_LONG); |
||
738 | |||
739 | /* |
||
740 | * Can't do "mask = (1UL << nbitsinlong) - 1", as that |
||
741 | * overflows if nbitsinlong == BITS_PER_LONG. |
||
742 | */ |
||
743 | mask = (1UL << (nbitsinlong - 1)); |
||
744 | mask += mask - 1; |
||
745 | mask <<= offset; |
||
746 | |||
747 | switch (reg_op) { |
||
748 | case REG_OP_ISFREE: |
||
749 | for (i = 0; i < nlongs_reg; i++) { |
||
750 | if (bitmap[index + i] & mask) |
||
751 | goto done; |
||
752 | } |
||
753 | ret = 1; /* all bits in region free (zero) */ |
||
754 | break; |
||
755 | |||
756 | case REG_OP_ALLOC: |
||
757 | for (i = 0; i < nlongs_reg; i++) |
||
758 | bitmap[index + i] |= mask; |
||
759 | break; |
||
760 | |||
761 | case REG_OP_RELEASE: |
||
762 | for (i = 0; i < nlongs_reg; i++) |
||
763 | bitmap[index + i] &= ~mask; |
||
764 | break; |
||
765 | } |
||
766 | done: |
||
767 | return ret; |
||
768 | } |
||
769 | |||
770 | /** |
||
771 | * bitmap_find_free_region - find a contiguous aligned mem region |
||
772 | * @bitmap: array of unsigned longs corresponding to the bitmap |
||
773 | * @bits: number of bits in the bitmap |
||
774 | * @order: region size (log base 2 of number of bits) to find |
||
775 | * |
||
776 | * Find a region of free (zero) bits in a @bitmap of @bits bits and |
||
777 | * allocate them (set them to one). Only consider regions of length |
||
778 | * a power (@order) of two, aligned to that power of two, which |
||
779 | * makes the search algorithm much faster. |
||
780 | * |
||
781 | * Return the bit offset in bitmap of the allocated region, |
||
782 | * or -errno on failure. |
||
783 | */ |
||
5056 | serge | 784 | int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order) |
3391 | Serge | 785 | { |
5056 | serge | 786 | unsigned int pos, end; /* scans bitmap by regions of size order */ |
3391 | Serge | 787 | |
5056 | serge | 788 | for (pos = 0 ; (end = pos + (1U << order)) <= bits; pos = end) { |
3391 | Serge | 789 | if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) |
790 | continue; |
||
791 | __reg_op(bitmap, pos, order, REG_OP_ALLOC); |
||
792 | return pos; |
||
793 | } |
||
794 | return -ENOMEM; |
||
795 | } |
||
796 | EXPORT_SYMBOL(bitmap_find_free_region); |
||
797 | |||
798 | /** |
||
799 | * bitmap_release_region - release allocated bitmap region |
||
800 | * @bitmap: array of unsigned longs corresponding to the bitmap |
||
801 | * @pos: beginning of bit region to release |
||
802 | * @order: region size (log base 2 of number of bits) to release |
||
803 | * |
||
804 | * This is the complement to __bitmap_find_free_region() and releases |
||
805 | * the found region (by clearing it in the bitmap). |
||
806 | * |
||
807 | * No return value. |
||
808 | */ |
||
5056 | serge | 809 | void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order) |
3391 | Serge | 810 | { |
811 | __reg_op(bitmap, pos, order, REG_OP_RELEASE); |
||
812 | } |
||
813 | EXPORT_SYMBOL(bitmap_release_region); |
||
814 | |||
815 | /** |
||
816 | * bitmap_allocate_region - allocate bitmap region |
||
817 | * @bitmap: array of unsigned longs corresponding to the bitmap |
||
818 | * @pos: beginning of bit region to allocate |
||
819 | * @order: region size (log base 2 of number of bits) to allocate |
||
820 | * |
||
821 | * Allocate (set bits in) a specified region of a bitmap. |
||
822 | * |
||
823 | * Return 0 on success, or %-EBUSY if specified region wasn't |
||
824 | * free (not all bits were zero). |
||
825 | */ |
||
5056 | serge | 826 | int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order) |
3391 | Serge | 827 | { |
828 | if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE)) |
||
829 | return -EBUSY; |
||
5056 | serge | 830 | return __reg_op(bitmap, pos, order, REG_OP_ALLOC); |
3391 | Serge | 831 | } |
832 | EXPORT_SYMBOL(bitmap_allocate_region); |
||
833 | |||
834 | /** |
||
835 | * bitmap_copy_le - copy a bitmap, putting the bits into little-endian order. |
||
836 | * @dst: destination buffer |
||
837 | * @src: bitmap to copy |
||
838 | * @nbits: number of bits in the bitmap |
||
839 | * |
||
840 | * Require nbits % BITS_PER_LONG == 0. |
||
841 | */ |
||
842 | void bitmap_copy_le(void *dst, const unsigned long *src, int nbits) |
||
843 | { |
||
844 | unsigned long *d = dst; |
||
845 | int i; |
||
846 | |||
847 | for (i = 0; i < nbits/BITS_PER_LONG; i++) { |
||
848 | if (BITS_PER_LONG == 64) |
||
849 | d[i] = cpu_to_le64(src[i]); |
||
850 | else |
||
851 | d[i] = cpu_to_le32(src[i]); |
||
852 | } |
||
853 | } |
||
854 | EXPORT_SYMBOL(bitmap_copy_le);>=>><>>>>=><=>><>><>><>><>><>>>=>>>>>>>=>>>>=>>>>>>>>>><>><>><>><>>><>>>>> |