diff options
author | Akinobu Mita <akinobu.mita@gmail.com> | 2009-12-15 19:48:25 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-12-16 10:20:18 -0500 |
commit | c1a2a962a2ad103846e7950b4591471fabecece7 (patch) | |
tree | 9a06ab8d1c65037456bad02c821033197f67f03f /lib/bitmap.c | |
parent | 868d64812ae84e8f094e0bcf95157c7d79d625ec (diff) |
bitmap: introduce bitmap_set, bitmap_clear, bitmap_find_next_zero_area
This introduces new bitmap functions:
bitmap_set: Set specified bit area
bitmap_clear: Clear specified bit area
bitmap_find_next_zero_area: Find free bit area
These are mostly stolen from iommu helper. The differences are:
- Use find_next_bit instead of doing test_bit for each bit
- Rewrite bitmap_set and bitmap_clear
Instead of setting or clearing for each bit.
- Check the last bit of the limit
iommu-helper doesn't want to find such area
- The return value if there is no zero area
find_next_zero_area in iommu helper: returns -1
bitmap_find_next_zero_area: return >= bitmap size
Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
Cc: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Greg Kroah-Hartman <gregkh@suse.de>
Cc: Lothar Wassmann <LW@KARO-electronics.de>
Cc: Roland Dreier <rolandd@cisco.com>
Cc: Yevgeny Petrilin <yevgenyp@mellanox.co.il>
Cc: Tony Luck <tony.luck@intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: Joerg Roedel <joerg.roedel@amd.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/bitmap.c')
-rw-r--r-- | lib/bitmap.c | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/bitmap.c b/lib/bitmap.c index 702565821c99..11bf49750583 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c | |||
@@ -271,6 +271,87 @@ int __bitmap_weight(const unsigned long *bitmap, int bits) | |||
271 | } | 271 | } |
272 | EXPORT_SYMBOL(__bitmap_weight); | 272 | EXPORT_SYMBOL(__bitmap_weight); |
273 | 273 | ||
274 | #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG)) | ||
275 | |||
276 | void bitmap_set(unsigned long *map, int start, int nr) | ||
277 | { | ||
278 | unsigned long *p = map + BIT_WORD(start); | ||
279 | const int size = start + nr; | ||
280 | int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG); | ||
281 | unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start); | ||
282 | |||
283 | while (nr - bits_to_set >= 0) { | ||
284 | *p |= mask_to_set; | ||
285 | nr -= bits_to_set; | ||
286 | bits_to_set = BITS_PER_LONG; | ||
287 | mask_to_set = ~0UL; | ||
288 | p++; | ||
289 | } | ||
290 | if (nr) { | ||
291 | mask_to_set &= BITMAP_LAST_WORD_MASK(size); | ||
292 | *p |= mask_to_set; | ||
293 | } | ||
294 | } | ||
295 | EXPORT_SYMBOL(bitmap_set); | ||
296 | |||
297 | void bitmap_clear(unsigned long *map, int start, int nr) | ||
298 | { | ||
299 | unsigned long *p = map + BIT_WORD(start); | ||
300 | const int size = start + nr; | ||
301 | int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG); | ||
302 | unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start); | ||
303 | |||
304 | while (nr - bits_to_clear >= 0) { | ||
305 | *p &= ~mask_to_clear; | ||
306 | nr -= bits_to_clear; | ||
307 | bits_to_clear = BITS_PER_LONG; | ||
308 | mask_to_clear = ~0UL; | ||
309 | p++; | ||
310 | } | ||
311 | if (nr) { | ||
312 | mask_to_clear &= BITMAP_LAST_WORD_MASK(size); | ||
313 | *p &= ~mask_to_clear; | ||
314 | } | ||
315 | } | ||
316 | EXPORT_SYMBOL(bitmap_clear); | ||
317 | |||
318 | /* | ||
319 | * bitmap_find_next_zero_area - find a contiguous aligned zero area | ||
320 | * @map: The address to base the search on | ||
321 | * @size: The bitmap size in bits | ||
322 | * @start: The bitnumber to start searching at | ||
323 | * @nr: The number of zeroed bits we're looking for | ||
324 | * @align_mask: Alignment mask for zero area | ||
325 | * | ||
326 | * The @align_mask should be one less than a power of 2; the effect is that | ||
327 | * the bit offset of all zero areas this function finds is multiples of that | ||
328 | * power of 2. A @align_mask of 0 means no alignment is required. | ||
329 | */ | ||
330 | unsigned long bitmap_find_next_zero_area(unsigned long *map, | ||
331 | unsigned long size, | ||
332 | unsigned long start, | ||
333 | unsigned int nr, | ||
334 | unsigned long align_mask) | ||
335 | { | ||
336 | unsigned long index, end, i; | ||
337 | again: | ||
338 | index = find_next_zero_bit(map, size, start); | ||
339 | |||
340 | /* Align allocation */ | ||
341 | index = __ALIGN_MASK(index, align_mask); | ||
342 | |||
343 | end = index + nr; | ||
344 | if (end > size) | ||
345 | return end; | ||
346 | i = find_next_bit(map, end, index); | ||
347 | if (i < end) { | ||
348 | start = i + 1; | ||
349 | goto again; | ||
350 | } | ||
351 | return index; | ||
352 | } | ||
353 | EXPORT_SYMBOL(bitmap_find_next_zero_area); | ||
354 | |||
274 | /* | 355 | /* |
275 | * Bitmap printing & parsing functions: first version by Bill Irwin, | 356 | * Bitmap printing & parsing functions: first version by Bill Irwin, |
276 | * second version by Paul Jackson, third by Joe Korty. | 357 | * second version by Paul Jackson, third by Joe Korty. |