rust/kernel/bits.rs
Source file repositories/reference/linux-study-clean/rust/kernel/bits.rs
File Facts
- System
- Linux kernel
- Corpus path
rust/kernel/bits.rs- Extension
.rs- Size
- 6480 bytes
- Lines
- 206
- Domain
- Rust Kernel Layer
- Bucket
- Rust API Membrane
- Inferred role
- Rust Kernel Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.
- Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
//! Bit manipulation macros.
//!
//! C header: [`include/linux/bits.h`](srctree/include/linux/bits.h)
use crate::prelude::*;
use core::ops::RangeInclusive;
use macros::paste;
macro_rules! impl_bit_fn {
(
$ty:ty
) => {
paste! {
/// Computes `1 << n` if `n` is in bounds, i.e.: if `n` is smaller than
/// the maximum number of bits supported by the type.
///
/// Returns [`None`] otherwise.
#[inline]
pub fn [<checked_bit_ $ty>](n: u32) -> Option<$ty> {
(1 as $ty).checked_shl(n)
}
/// Computes `1 << n` by performing a compile-time assertion that `n` is
/// in bounds.
///
/// This version is the default and should be used if `n` is known at
/// compile time.
// Always inline to optimize out error path of `build_assert`.
#[inline(always)]
pub const fn [<bit_ $ty>](n: u32) -> $ty {
build_assert!(n < <$ty>::BITS);
(1 as $ty) << n
}
}
};
}
impl_bit_fn!(u64);
impl_bit_fn!(u32);
impl_bit_fn!(u16);
impl_bit_fn!(u8);
macro_rules! impl_genmask_fn {
(
$ty:ty,
$(#[$genmask_checked_ex:meta])*,
$(#[$genmask_ex:meta])*
) => {
paste! {
/// Creates a contiguous bitmask for the given range by validating
/// the range at runtime.
///
/// Returns [`None`] if the range is invalid, i.e.: if the start is
/// greater than the end or if the range is outside of the
/// representable range for the type.
$(#[$genmask_checked_ex])*
#[inline]
pub fn [<genmask_checked_ $ty>](range: RangeInclusive<u32>) -> Option<$ty> {
let start = *range.start();
let end = *range.end();
if start > end {
return None;
}
let high = [<checked_bit_ $ty>](end)?;
let low = [<checked_bit_ $ty>](start)?;
Some((high | (high - 1)) & !(low - 1))
}
/// Creates a compile-time contiguous bitmask for the given range by
/// performing a compile-time assertion that the range is valid.
///
/// This version is the default and should be used if the range is known
/// at compile time.
$(#[$genmask_ex])*
// Always inline to optimize out error path of `build_assert`.
#[inline(always)]
pub const fn [<genmask_ $ty>](range: RangeInclusive<u32>) -> $ty {
let start = *range.start();
let end = *range.end();
build_assert!(start <= end);
let high = [<bit_ $ty>](end);
let low = [<bit_ $ty>](start);
(high | (high - 1)) & !(low - 1)
}
Annotation
- Atlas domain: Rust Kernel Layer / Rust API Membrane.
- Implementation status: source 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.