lib/find_bit.c
Source file repositories/reference/linux-study-clean/lib/find_bit.c
File Facts
- System
- Linux kernel
- Corpus path
lib/find_bit.c- Extension
.c- Size
- 8363 bytes
- Lines
- 311
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
Dependency Surface
linux/bitops.hlinux/bitmap.hlinux/export.hlinux/math.hlinux/minmax.hlinux/swab.hlinux/random.h
Detected Declarations
function _find_first_bitfunction _find_first_and_bitfunction _find_first_andnot_bitfunction _find_first_and_and_bitfunction _find_first_zero_bitfunction _find_next_bitfunction __find_nth_bitfunction __find_nth_and_bitfunction __find_nth_and_andnot_bitfunction _find_next_and_bitfunction _find_next_andnot_bitfunction _find_next_or_bitfunction _find_next_zero_bitfunction _find_last_bitfunction find_next_clump8function _find_first_zero_bit_lefunction _find_next_zero_bit_lefunction _find_next_bit_lefunction find_random_bitexport _find_first_bitexport _find_first_and_bitexport _find_first_andnot_bitexport _find_first_and_and_bitexport _find_first_zero_bitexport _find_next_bitexport __find_nth_bitexport __find_nth_and_bitexport __find_nth_and_andnot_bitexport _find_next_and_bitexport _find_next_andnot_bitexport _find_next_or_bitexport _find_next_zero_bitexport _find_last_bitexport find_next_clump8export _find_first_zero_bit_leexport _find_next_zero_bit_leexport _find_next_bit_leexport find_random_bit
Annotated Snippet
if (val) { \
sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz); \
break; \
} \
} \
\
sz; \
})
/*
* Common helper for find_next_bit() function family
* @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
* @MUNGE: The expression that post-processes a word containing found bit (may be empty)
* @size: The bitmap size in bits
* @start: The bitnumber to start searching at
*/
#define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \
({ \
unsigned long mask, idx, tmp, sz = (size), __start = (start); \
\
if (unlikely(__start >= sz)) \
goto out; \
\
mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \
idx = __start / BITS_PER_LONG; \
\
for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \
if ((idx + 1) * BITS_PER_LONG >= sz) \
goto out; \
idx++; \
} \
\
sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \
out: \
sz; \
})
#define FIND_NTH_BIT(FETCH, size, num) \
({ \
unsigned long sz = (size), nr = (num), idx, w, tmp = 0; \
\
for (idx = 0; (idx + 1) * BITS_PER_LONG <= sz; idx++) { \
if (idx * BITS_PER_LONG + nr >= sz) \
goto out; \
\
tmp = (FETCH); \
w = hweight_long(tmp); \
if (w > nr) \
goto found; \
\
nr -= w; \
} \
\
if (sz % BITS_PER_LONG) \
tmp = (FETCH) & BITMAP_LAST_WORD_MASK(sz); \
found: \
sz = idx * BITS_PER_LONG + fns(tmp, nr); \
out: \
sz; \
})
#ifndef find_first_bit
/*
* Find the first set bit in a memory region.
*/
unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
{
return FIND_FIRST_BIT(addr[idx], /* nop */, size);
}
EXPORT_SYMBOL(_find_first_bit);
#endif
#ifndef find_first_and_bit
/*
* Find the first set bit in two memory regions.
*/
unsigned long _find_first_and_bit(const unsigned long *addr1,
const unsigned long *addr2,
unsigned long size)
{
return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);
}
EXPORT_SYMBOL(_find_first_and_bit);
#endif
/*
* Find the first bit set in 1st memory region and unset in 2nd.
*/
unsigned long _find_first_andnot_bit(const unsigned long *addr1,
const unsigned long *addr2,
Annotation
- Immediate include surface: `linux/bitops.h`, `linux/bitmap.h`, `linux/export.h`, `linux/math.h`, `linux/minmax.h`, `linux/swab.h`, `linux/random.h`.
- Detected declarations: `function _find_first_bit`, `function _find_first_and_bit`, `function _find_first_andnot_bit`, `function _find_first_and_and_bit`, `function _find_first_zero_bit`, `function _find_next_bit`, `function __find_nth_bit`, `function __find_nth_and_bit`, `function __find_nth_and_andnot_bit`, `function _find_next_and_bit`.
- Atlas domain: Kernel Services / lib.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.