find_next_zero_bit() -- 查找第一个不为0的位_儒雅

导读:


choose_address() --> find_next_zero_bit()
/usr/src/linux-2.6.21.5/arch/i386/lib/bitops.c

int find_next_zero_bit(const unsigned long *addr, int size, int offset)
查询*addr中,从第offset位开始,第一个不为0的位的位数(最低位从0开始),注: offset最小值为0,最大值为sizeof(unsigned long)*8 - 1
---------------------------------------------------------------
int find_next_zero_bit(const unsigned long *addr, int size, int offset)
{
    unsigned long * p = ((unsigned long *) addr) + (offset >> 5);
    int set = 0, bit = offset & 31, res;
    if (bit) {
        __asm__("bsfl %1,%0/n/t"
            "jne 1f/n/t"
            "movl $32, %0/n"
            "1:"
            : "=r" (set)
            : "r" (~(*p >> bit)));
        if (set < (32 - bit))
            return set + offset;
        set = 32 - bit;
        p++;
    }
    res = find_first_zero_bit (p, size - 32 * (p - (unsigned long *) addr));
    return (offset + set + res);
}




例如查找位图bitmap(共32位)的第5位开始第一个不为0的位的位数(查询结果为5)
--5   4   3   2   1   0
|---|---|---|---|---|---|
| 0 | 1 | 1 | 1 | 1 | 1 |           
|---|---|---|---|---|---|  

bitmap = 31 = 011111
---------------------------------------------------------------
#include //Needed by all modules
#include //Needed for KERN_ALERT

int init_module(void)
{
    int devnum;
    unsigned long bitmap = 31;
    devnum = find_next_zero_bit(&bitmap, 32, 5);
    printk("<1> devnum = %d/n", devnum);
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_ALERT "Goodbye./n");
}



本文转自
http://hi.baidu.com/zengzhaonong/blog/item/12bf9894ebbfda1cd31b7064.html

你可能感兴趣的:(find_next_zero_bit() -- 查找第一个不为0的位_儒雅)