1 #ifndef BITMAP_H
   2 #define BITMAP_H
   3 
   4 #define BITS_IN_LONG    (sizeof(unsigned long)*8)
   5 #define LONGS(x)        ((x + BITS_IN_LONG - 1) & -BITS_IN_LONG)
   6 
   7 /* Every bitmap gets its own type */
   8 #define DECLARE_BITMAP(name, x) unsigned long name[LONGS(x)]
   9 
  10 static inline int test_bit(unsigned int nr, unsigned long *bitmap)
  11 {
  12         unsigned long offset = nr / BITS_IN_LONG;
  13         unsigned long bit = nr & (BITS_IN_LONG-1);
  14         return (bitmap[offset] >> bit) & 1;
  15 }
  16 
  17 static inline void set_bit(unsigned int nr, unsigned long *bitmap)
  18 {
  19         unsigned long offset = nr / BITS_IN_LONG;
  20         unsigned long bit = nr & (BITS_IN_LONG-1);
  21         bitmap[offset] |= 1UL << bit;
  22 }
  23 
  24 static inline void clear_bit(unsigned int nr, unsigned long *bitmap)
  25 {
  26         unsigned long offset = nr / BITS_IN_LONG;
  27         unsigned long bit = nr & (BITS_IN_LONG-1);
  28         bitmap[offset] &= ~(1UL << bit);
  29 }
  30 
  31 static inline int test_and_set_bit(unsigned int nr, unsigned long *bitmap)
  32 {
  33         unsigned long offset = nr / BITS_IN_LONG;
  34         unsigned long bit = nr & (BITS_IN_LONG-1);
  35         unsigned long old = bitmap[offset];
  36         unsigned long mask = 1UL << bit;
  37         bitmap[offset] = old | mask;
  38         return (old & mask) != 0;
  39 }
  40 
  41 static inline int test_and_clear_bit(unsigned int nr, unsigned long *bitmap)
  42 {
  43         unsigned long offset = nr / BITS_IN_LONG;
  44         unsigned long bit = nr & (BITS_IN_LONG-1);
  45         unsigned long old = bitmap[offset];
  46         unsigned long mask = 1UL << bit;
  47         bitmap[offset] = old & ~mask;
  48         return (old & mask) != 0;
  49 }
  50 
  51 #endif /* BITMAP_H */