开发者

What is the return value of sched_find_first_bit if it doesn't find anything?

The kernel is 2.4.

On a side note, does anybody knows a good place where I can search for that kind of information? Searching Google for functi开发者_运维知识库on definitions is frustrating.


If you plan on spending any significant time searching through or understanding the Linux kernel, I recommend downloading a copy and using Cscope.

Using Cscope on large projects (example: the Linux kernel)


I found the following in a copy of the Linux kernel 2.4.18.

The key seems to be the comment before this last piece of code below. It appears that the return value of sched_find_first_bit is undefined if no bit is set.

From linux-2.4/include/linux/sched.h:185


/*
 * The maximum RT priority is configurable.  If the resulting
 * bitmap is 160-bits , we can use a hand-coded routine which
 * is optimal.  Otherwise, we fall back on a generic routine for
 * finding the first set bit from an arbitrarily-sized bitmap.
 */
#if MAX_PRIO  127
#define sched_find_first_bit(map)   _sched_find_first_bit(map)
#else
#define sched_find_first_bit(map)   find_first_bit(map, MAX_PRIO)
#endif

From linux-2.4/include/asm-i386/bitops.h:303


/**
 * find_first_bit - find the first set bit in a memory region
 * @addr: The address to start the search at
 * @size: The maximum size to search
 *
 * Returns the bit-number of the first set bit, not the number of the byte
 * containing a bit.
 */
static __inline__ int find_first_bit(void * addr, unsigned size)
{
    int d0, d1;
    int res;

    /* This looks at memory. Mark it volatile to tell gcc not to move it around */
    __asm__ __volatile__(
        "xorl %%eax,%%eax\n\t"
        "repe; scasl\n\t"
        "jz 1f\n\t"
        "leal -4(%%edi),%%edi\n\t"
        "bsfl (%%edi),%%eax\n"
        "1:\tsubl %%ebx,%%edi\n\t"
        "shll $3,%%edi\n\t"
        "addl %%edi,%%eax"
        :"=a" (res), "=&c" (d0), "=&D" (d1)
        :"1" ((size + 31) >> 5), "2" (addr), "b" (addr));
    return res;
}

From linux-2.4/include/asm-i386/bitops.h:425


/*
 * Every architecture must define this function. It's the fastest
 * way of searching a 140-bit bitmap where the first 100 bits are
 * unlikely to be set. It's guaranteed that at least one of the 140
 * bits is cleared.
 */
static inline int _sched_find_first_bit(unsigned long *b)
{
    if (unlikely(b[0]))
        return __ffs(b[0]);
    if (unlikely(b[1]))
        return __ffs(b[1]) + 32;
    if (unlikely(b[2]))
        return __ffs(b[2]) + 64;
    if (b[3])
        return __ffs(b[3]) + 96;
    return __ffs(b[4]) + 128;
}

From linux-2.4/include/asm-i386/bitops.h:409


/**
 * __ffs - find first bit in word.
 * @word: The word to search
 *
 * Undefined if no bit exists, so code should check against 0 first.
 */
static __inline__ unsigned long __ffs(unsigned long word)
{
    __asm__("bsfl %1,%0"
        :"=r" (word)
        :"rm" (word));
    return word;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜