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>
This commit is contained in:
Eliot Courtney 2026-09-30 11:42:56 +09:00 committed by Miguel Ojeda
parent b666bd22be
commit 3132a951a7

View File

@ -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 @@ pub fn find_unused_id(&mut self, offset: usize) -> Option<UnusedId<'_>> {
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`].