Linux内核中位图的使用

使用位图标记

 

#include 


#ifdef CONFIG_64BIT
#define BITS_PER_LONG 64
#else
#define BITS_PER_LONG 32
#endif /* CONFIG_64BIT */

/*
计算在第几个unsigned long数组中的掩码
*/
#define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
/*
第@nr个位在unsigned long数组中的下标
*/
#define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
#define BITS_PER_BYTE		8

#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

/*
表示@nr个位需要几个unsigned long数
*/
#define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))

/*
定义位图@name[]
使用unsigned long数组表示@bits个位
*/
#define DECLARE_BITMAP(name,bits) \
	unsigned long name[BITS_TO_LONGS(bits)]


/*
选择通用函数为例
include/asm-generic/bitops/non-atomic.h
*/

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);
	
	*p  |= mask;
}

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;
}

static inline void __change_bit(int nr, volatile unsigned long *addr)
{
	unsigned long mask = BIT_MASK(nr);
	unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);

	*p ^= mask;
}

/*
test_and_XX_bit返回之前@nr位的置位情况

1 - 调用函数前已经置位
0 - 调用函数前没有置位
*/

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;

	old = *p;
	*p = old | mask;

	return (old & mask) != 0;
}

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;

	old = *p;
	*p = old & ~mask;

	return (old & mask) != 0;
}

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;

	old = *p;
	*p = old ^ mask;

	return (old & mask) != 0;
}

#define BITS_NR		50
DECLARE_BITMAP(testbit, BITS_NR);

#define ARRAY_SIZE(__arr)	(sizeof(__arr) / sizeof(__arr[0]))

void
testbit_dump(void)
{
	int size = ARRAY_SIZE(testbit);
	printf("array size: %d\n", size);

	int i;
	for (i = 0; i < size; i++)
	{
		printf("[%d]: 0x%08lx\n", i, testbit[i]);
	}
	printf("\n");
}

int
main(void)
{
	int b;

	testbit_dump();

	printf("__set_bit 0\n");
	__set_bit(0, testbit);
	testbit_dump();

	printf("__test_and_clear_bit 0\n");
	b = __test_and_clear_bit(0, testbit);
	printf("b: %d\n", b);
	testbit_dump();

	printf("__set_bit 19\n");
	__set_bit(19, testbit);
	testbit_dump();

	printf("__clear_bit 0\n");
	__clear_bit(0, testbit);
	testbit_dump();

	printf("__test_and_change_bit 33\n");
	b = __test_and_change_bit(33, testbit);
	printf("b: %d\n", b);
	testbit_dump();

	printf("__test_and_clear_bit 33\n");
	b = __test_and_clear_bit(33, testbit);
	printf("b: %d\n", b);
	testbit_dump();

	return 0;
}





 

你可能感兴趣的:(Linux内核,Linux小程序)