Several algorithms depend on performing a really fast search of a bitmask; several scheduling algorithms (such as SCHED_FIFO and SCHED_RR, which you learned about in the companion guide Linux Kernel Programming - Chapter 10, The CPU Scheduler – Part 1, and Chapter 11, The CPU Scheduler – Part 2) often internally require this. Implementing this efficiently becomes important (especially for OS-level performance-sensitive code paths). Hence, the kernel provides a few APIs to scan a given bitmask (these prototypes are found in include/asm-generic/bitops/find.h):
- unsigned long find_first_bit(const unsigned long *addr, unsigned long size): Finds the first set bit in a memory region; returns the bit number of the first set bit, else (no bits are set) returns @size.
- unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size): Finds the first cleared bit in a memory region...