Linux 内核数据结构bitmap

 

    bitmap将一片连续的空间作为一个数据类型,其中的成员都是1位,长度是bitmap的容量。

#include
#include

#define MAX_PRIO 10000
#define BITS_PER_LONG 32
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))

#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_BYTE        8

#define BITS_TO_LONGS(nr)    DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))

#define DECLARE_BITMAP(name,bits) \
    unsigned long name[BITS_TO_LONGS(bits)]

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_set_bit - 设置一个位并且返回它原先的值
* @nr: 要设置的位/bitmap中的哪一位
* @addr: bitmap首地址
*/
 

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

    *p = old | mask;
    return (old & mask) != 0;
}

/**
* __test_and_clear_bit - 清空一个位并且返回它的原先的值
* @nr: bitmap中的哪一位
* @addr: bitmap的首地址
*/
 

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

    *p = old & ~mask;
    return (old & mask) != 0;
}

/**
* __test_and_clear_bit - 取反某一位并返回它的原先的值
* @nr: bitmap中的哪一位
* @addr: bitmap的首地址
*/
 

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

    *p = old ^ mask;
    return (old & mask) != 0;
}

static inline int test_bit(int nr, const volatile unsigned long *addr)
{
    return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
}

#ifndef container_of
/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:    the type of the container struct this is embedded in.
 * @member:    the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({            \
    const typeof(((type *)0)->member) * __mptr = (ptr);    \
    (type *)((char *)__mptr - offsetof(type, member)); })
#endif

#define list_entry(ptr, type, member) container_of(ptr, type, member)

int _tmain(int argc, _TCHAR* argv[])
{
    DECLARE_BITMAP(bitmap1, MAX_PRIO);
    DECLARE_BITMAP(bitmap2, MAX_PRIO);
    int i;

    /* 00000000 - 99999999 使用一个bitmap */
    int num1 = 9999;
    int num2 = 9999;

    char buff[32] = {0};

    for (i = 0; i < MAX_PRIO; i++) {
       
        __clear_bit(i, bitmap1);
        __clear_bit(i, bitmap2);
    }

    __set_bit(num1, bitmap1);
    __set_bit(num2, bitmap2);

    printf("bitmap size = %d\n", sizeof(bitmap1)/sizeof(long));
    printf("bitmap = %d\n", test_bit(num1, bitmap1));
    printf("bitmap = %d\n", test_bit(num2, bitmap2));
    printf("atoi : %d\n", atoi("0010"));
    printf("%.4d%.4d\n", num1, num2);
121     return 0;
}

你可能感兴趣的:(linux编程)