2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _LINUX_BITOPS_H_
29 #define _LINUX_BITOPS_H_
31 #include <sys/types.h>
32 #include <sys/systm.h>
34 #define BIT(nr) (1UL << (nr))
36 #define BITS_PER_LONG 64
38 #define BITS_PER_LONG 32
40 #define BIT_MASK(n) (~0UL >> (BITS_PER_LONG - (n)))
41 #define BITS_TO_LONGS(n) howmany((n), BITS_PER_LONG)
42 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
47 return (ffs(mask) - 1);
53 return (fls(mask) - 1);
59 return (ffsl(mask) - 1);
65 return (flsl(mask) - 1);
69 #define ffz(mask) __ffs(~(mask))
71 static inline int get_count_order(unsigned int count)
75 order = fls(count) - 1;
76 if (count & (count - 1))
81 static inline unsigned long
82 find_first_bit(unsigned long *addr, unsigned long size)
87 for (bit = 0; size >= BITS_PER_LONG;
88 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
91 return (bit + __ffsl(*addr));
94 mask = (*addr) & BIT_MASK(size);
103 static inline unsigned long
104 find_first_zero_bit(unsigned long *addr, unsigned long size)
109 for (bit = 0; size >= BITS_PER_LONG;
110 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
113 return (bit + __ffsl(~(*addr)));
116 mask = ~(*addr) & BIT_MASK(size);
125 static inline unsigned long
126 find_last_bit(unsigned long *addr, unsigned long size)
133 pos = size / BITS_PER_LONG;
134 offs = size % BITS_PER_LONG;
135 bit = BITS_PER_LONG * pos;
138 mask = (*addr) & BIT_MASK(offs);
140 return (bit + __flsl(mask));
144 bit -= BITS_PER_LONG;
146 return (bit + __flsl(mask));
151 static inline unsigned long
152 find_next_bit(unsigned long *addr, unsigned long size, unsigned long offset)
161 pos = offset / BITS_PER_LONG;
162 offs = offset % BITS_PER_LONG;
163 bit = BITS_PER_LONG * pos;
166 mask = (*addr) & ~BIT_MASK(offs);
168 return (bit + __ffsl(mask));
169 bit += BITS_PER_LONG;
172 for (size -= bit; size >= BITS_PER_LONG;
173 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
176 return (bit + __ffsl(*addr));
179 mask = (*addr) & BIT_MASK(size);
188 static inline unsigned long
189 find_next_zero_bit(unsigned long *addr, unsigned long size,
190 unsigned long offset)
199 pos = offset / BITS_PER_LONG;
200 offs = offset % BITS_PER_LONG;
201 bit = BITS_PER_LONG * pos;
204 mask = ~(*addr) & ~BIT_MASK(offs);
206 return (bit + __ffsl(mask));
207 bit += BITS_PER_LONG;
210 for (size -= bit; size >= BITS_PER_LONG;
211 size -= BITS_PER_LONG, bit += BITS_PER_LONG, addr++) {
214 return (bit + __ffsl(~(*addr)));
217 mask = ~(*addr) & BIT_MASK(size);
227 bitmap_zero(unsigned long *addr, int size)
231 len = BITS_TO_LONGS(size) * sizeof(long);
232 memset(addr, 0, len);
236 bitmap_fill(unsigned long *addr, int size)
241 len = (size / BITS_PER_LONG) * sizeof(long);
242 memset(addr, 0xff, len);
243 tail = size & (BITS_PER_LONG - 1);
245 addr[size / BITS_PER_LONG] = BIT_MASK(tail);
249 bitmap_full(unsigned long *addr, int size)
256 len = size / BITS_PER_LONG;
257 for (i = 0; i < len; i++)
260 tail = size & (BITS_PER_LONG - 1);
262 mask = BIT_MASK(tail);
263 if ((addr[i] & mask) != mask)
270 bitmap_empty(unsigned long *addr, int size)
277 len = size / BITS_PER_LONG;
278 for (i = 0; i < len; i++)
281 tail = size & (BITS_PER_LONG - 1);
283 mask = BIT_MASK(tail);
284 if ((addr[i] & mask) != 0)
290 #define NBLONG (NBBY * sizeof(long))
292 #define set_bit(i, a) \
293 atomic_set_long(&((volatile long *)(a))[(i)/NBLONG], 1 << (i) % NBLONG)
295 #define clear_bit(i, a) \
296 atomic_clear_long(&((volatile long *)(a))[(i)/NBLONG], 1 << (i) % NBLONG)
298 #define test_bit(i, a) \
299 !!(atomic_load_acq_long(&((volatile long *)(a))[(i)/NBLONG]) & \
303 test_and_clear_bit(long bit, long *var)
307 var += bit / (sizeof(long) * NBBY);
308 bit %= sizeof(long) * NBBY;
311 val = *(volatile long *)var;
312 } while (atomic_cmpset_long(var, val, val & ~bit) == 0);
314 return !!(val & bit);
318 test_and_set_bit(long bit, volatile unsigned long *var)
322 var += bit / (sizeof(long) * NBBY);
323 bit %= sizeof(long) * NBBY;
326 val = *(volatile long *)var;
327 } while (atomic_cmpset_long(var, val, val | bit) == 0);
329 return !!(val & bit);
333 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
334 #define BITMAP_LAST_WORD_MASK(nbits) \
336 ((nbits) % BITS_PER_LONG) ? \
337 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
342 bitmap_set(unsigned long *map, int start, int nr)
344 unsigned long *p = map + BIT_WORD(start);
345 const int size = start + nr;
346 int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
347 unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
349 while (nr - bits_to_set >= 0) {
352 bits_to_set = BITS_PER_LONG;
357 mask_to_set &= BITMAP_LAST_WORD_MASK(size);
363 bitmap_clear(unsigned long *map, int start, int nr)
365 unsigned long *p = map + BIT_WORD(start);
366 const int size = start + nr;
367 int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
368 unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
370 while (nr - bits_to_clear >= 0) {
371 *p &= ~mask_to_clear;
373 bits_to_clear = BITS_PER_LONG;
374 mask_to_clear = ~0UL;
378 mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
379 *p &= ~mask_to_clear;
384 REG_OP_ISFREE, /* true if region is all zero bits */
385 REG_OP_ALLOC, /* set all bits in region */
386 REG_OP_RELEASE, /* clear all bits in region */
389 static int __reg_op(unsigned long *bitmap, int pos, int order, int reg_op)
391 int nbits_reg; /* number of bits in region */
392 int index; /* index first long of region in bitmap */
393 int offset; /* bit offset region in bitmap[index] */
394 int nlongs_reg; /* num longs spanned by region in bitmap */
395 int nbitsinlong; /* num bits of region in each spanned long */
396 unsigned long mask; /* bitmask for one long of region */
397 int i; /* scans bitmap by longs */
398 int ret = 0; /* return value */
401 * Either nlongs_reg == 1 (for small orders that fit in one long)
402 * or (offset == 0 && mask == ~0UL) (for larger multiword orders.)
404 nbits_reg = 1 << order;
405 index = pos / BITS_PER_LONG;
406 offset = pos - (index * BITS_PER_LONG);
407 nlongs_reg = BITS_TO_LONGS(nbits_reg);
408 nbitsinlong = min(nbits_reg, BITS_PER_LONG);
411 * Can't do "mask = (1UL << nbitsinlong) - 1", as that
412 * overflows if nbitsinlong == BITS_PER_LONG.
414 mask = (1UL << (nbitsinlong - 1));
420 for (i = 0; i < nlongs_reg; i++) {
421 if (bitmap[index + i] & mask)
424 ret = 1; /* all bits in region free (zero) */
428 for (i = 0; i < nlongs_reg; i++)
429 bitmap[index + i] |= mask;
433 for (i = 0; i < nlongs_reg; i++)
434 bitmap[index + i] &= ~mask;
442 * bitmap_find_free_region - find a contiguous aligned mem region
443 * @bitmap: array of unsigned longs corresponding to the bitmap
444 * @bits: number of bits in the bitmap
445 * @order: region size (log base 2 of number of bits) to find
447 * Find a region of free (zero) bits in a @bitmap of @bits bits and
448 * allocate them (set them to one). Only consider regions of length
449 * a power (@order) of two, aligned to that power of two, which
450 * makes the search algorithm much faster.
452 * Return the bit offset in bitmap of the allocated region,
453 * or -errno on failure.
456 bitmap_find_free_region(unsigned long *bitmap, int bits, int order)
458 int pos, end; /* scans bitmap by regions of size order */
460 for (pos = 0 ; (end = pos + (1 << order)) <= bits; pos = end) {
461 if (!__reg_op(bitmap, pos, order, REG_OP_ISFREE))
463 __reg_op(bitmap, pos, order, REG_OP_ALLOC);
470 * bitmap_release_region - release allocated bitmap region
471 * @bitmap: array of unsigned longs corresponding to the bitmap
472 * @pos: beginning of bit region to release
473 * @order: region size (log base 2 of number of bits) to release
475 * This is the complement to __bitmap_find_free_region() and releases
476 * the found region (by clearing it in the bitmap).
481 bitmap_release_region(unsigned long *bitmap, int pos, int order)
483 __reg_op(bitmap, pos, order, REG_OP_RELEASE);
486 #include <asm/bitops/non-atomic.h>
487 #include <asm/bitops/const_hweight.h>
489 #endif /* _LINUX_BITOPS_H_ */