diff options
| author | Eliot Courtney <ecourtney@nvidia.com> | 2026-09-30 11:42:56 +0900 |
|---|---|---|
| committer | Miguel Ojeda <ojeda@kernel.org> | 2026-10-13 12:52:26 +0200 |
| commit | 3132a951a7e4ec2e18bc314877137deee96ab83e (patch) | |
| tree | eb91ac0c2bdc98cb818d06782d5b3598c322a7cd /rust/kernel | |
| parent | b666bd22be8f211aceeeb8fbc827ebdb7abe98c0 (diff) | |
| download | linux-3132a951a7e4ec2e18bc314877137deee96ab83e.tar.gz linux-3132a951a7e4ec2e18bc314877137deee96ab83e.zip | |
rust: id_pool: add contiguous ID reservation
Add `IdPool::reserve_ids` which allocates a contiguous range with the
given offset, count, and alignment.
Reviewed-by: Burak Emir <burak.emir@gmail.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Eliot Courtney <ecourtney@nvidia.com>
Reviewed-by: Yury Norov <ynorov@nvidia.com>
Link: https://patch.msgid.link/20260930-chid-v9-7-0d6cca376cff@nvidia.com
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust/kernel')
| -rw-r--r-- | rust/kernel/id_pool.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/rust/kernel/id_pool.rs b/rust/kernel/id_pool.rs index 384753fe0e44..06a4c71c4c6c 100644 --- a/rust/kernel/id_pool.rs +++ b/rust/kernel/id_pool.rs @@ -4,8 +4,14 @@ //! Rust API for an ID pool backed by a [`BitmapVec`]. +use core::{ + num::NonZero, + ops::Range, // +}; + use crate::alloc::{AllocError, Flags}; use crate::bitmap::BitmapVec; +use crate::ptr::Alignment; /// Represents a dynamic ID pool backed by a [`BitmapVec`]. /// @@ -240,6 +246,32 @@ impl IdPool { pub fn release_id(&mut self, id: usize) { self.map.clear_bit(id); } + + /// Reserves a contiguous area of `count` IDs at or after `offset`. + /// + /// The start of the returned area is a multiple of `align`. + /// + /// Returns the reserved range upon success, or [`None`] if no such area could be found. + #[inline] + #[must_use] + pub fn reserve_ids( + &mut self, + offset: usize, + count: NonZero<usize>, + align: Alignment, + ) -> Option<Range<usize>> { + let start = self.map.next_zero_area(offset, count, align)?; + self.map.set(start, count); + Some(start..start + count.get()) + } + + /// Releases a contiguous area of IDs. + #[inline] + pub fn release_ids(&mut self, range: &Range<usize>) { + if let Some(nbits) = NonZero::new(range.len()) { + self.map.clear(range.start, nbits); + } + } } /// Represents an unused id in an [`IdPool`]. |
