linux(kernel)位操作

#define BIT(nr) (1UL << (nr))

#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))

#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)

#define BITS_PER_LONG 32

test_bit:测试相应地址的nr位是否为1.

static inline int test_bit(int nr, const volatile unsigned long *addr)

{

        return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));

}

clear_bit:设置相应设置地址的nr位为0.

/* TODO: Not atomic as it should be:

 * we don't use this for anything important. */

static inline void clear_bit(int nr, volatile unsigned long *addr)

{

unsigned long mask = BIT_MASK(nr);

unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

*p &= ~mask;

}

set_bit:设置相应设置地址的nr位为1

/**

 * set_bit - Atomically set a bit in memory

 * @nr: the bit to set

 * @addr: the address to start counting from

 *

 * This function is atomic and may not be reordered.  See __set_bit()

 * if you do not require the atomic guarantees.

 *

 * Note: there are no guarantees that this function will not be reordered

 * on non x86 architectures, so if you are writing portable code,

 * make sure not to rely on its reordering guarantees.

 *

 * Note that @nr may be almost arbitrarily large; this function is not

 * restricted to acting on a single-word quantity.

 */

static inline void set_bit(int nr, volatile unsigned long *addr)

{

unsigned long mask = BIT_MASK(nr);

unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

unsigned long flags;

_atomic_spin_lock_irqsave(p, flags);

*p  |= mask;

_atomic_spin_unlock_irqrestore(p, flags);

}

test_and_set_bit:设置相应设置地址的nr位为1,并返回原来这一位的值

/**

 * test_and_set_bit - Set a bit and return its old value

 * @nr: Bit to set

 * @addr: Address to count from

 *

 * This operation is atomic and cannot be reordered.

 * It may be reordered on other architectures than x86.

 * It also implies a memory barrier.

 */

static inline int test_and_set_bit(int nr, volatile unsigned long *addr)

{

unsigned long mask = BIT_MASK(nr);

unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

unsigned long old;

unsigned long flags;

_atomic_spin_lock_irqsave(p, flags);

old = *p;

*p = old | mask;

_atomic_spin_unlock_irqrestore(p, flags);

return (old & mask) != 0;

}

test_and_clear_bit:设置相应设置地址的nr位为0,并返回原来这一位的值

/**

 * test_and_clear_bit - Clear a bit and return its old value

 * @nr: Bit to clear

 * @addr: Address to count from

 *

 * This operation is atomic and cannot be reordered.

 * It can be reorderdered on other architectures other than x86.

 * It also implies a memory barrier.

 */

static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)

{

unsigned long mask = BIT_MASK(nr);

unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

unsigned long old;

unsigned long flags;

_atomic_spin_lock_irqsave(p, flags);

old = *p;

*p = old & ~mask;

_atomic_spin_unlock_irqrestore(p, flags);

return (old & mask) != 0;

}

test_and_change_bit:改变相应设置地址的nr位的值,并返回原来这一位的值

/**

 * test_and_change_bit - Change a bit and return its old value

 * @nr: Bit to change

 * @addr: Address to count from

 *

 * This operation is atomic and cannot be reordered.

 * It also implies a memory barrier.

 */

static inline int test_and_change_bit(int nr, volatile unsigned long *addr)

{

unsigned long mask = BIT_MASK(nr);

unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

unsigned long old;

unsigned long flags;

_atomic_spin_lock_irqsave(p, flags);

old = *p;

*p = old ^ mask;

_atomic_spin_unlock_irqrestore(p, flags);

return (old & mask) != 0;

}

(摘自 http://wmzjzwlzs.blog.163.com/blog/static/205014196201532294338242

猜你喜欢

转载自blog.csdn.net/qianxuedegushi/article/details/81699875