pub struct MapleTree<T: ForeignOwnable> { /* private fields */ }
Expand description
A maple tree optimized for storing non-overlapping ranges.
§Invariants
Each range in the maple tree owns an instance of T
.
Implementations§
Source§impl<T: ForeignOwnable> MapleTree<T>
impl<T: ForeignOwnable> MapleTree<T>
Source§impl<T: ForeignOwnable> MapleTree<T>
impl<T: ForeignOwnable> MapleTree<T>
Sourcepub fn new() -> impl PinInit<Self>
pub fn new() -> impl PinInit<Self>
Create a new maple tree.
The tree will use the regular implementation with a higher branching factor, rather than the allocation tree.
Sourcepub fn insert(
&self,
index: usize,
value: T,
gfp: Flags,
) -> Result<(), InsertError<T>>
pub fn insert( &self, index: usize, value: T, gfp: Flags, ) -> Result<(), InsertError<T>>
Insert the value at the given index.
§Errors
If the maple tree already contains a range using the given index, then this call will
return an InsertErrorKind::Occupied
. It may also fail if memory allocation fails.
§Examples
use kernel::maple_tree::{InsertErrorKind, MapleTree};
let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
let ten = KBox::new(10, GFP_KERNEL)?;
let twenty = KBox::new(20, GFP_KERNEL)?;
let the_answer = KBox::new(42, GFP_KERNEL)?;
// These calls will succeed.
tree.insert(100, ten, GFP_KERNEL)?;
tree.insert(101, twenty, GFP_KERNEL)?;
// This will fail because the index is already in use.
assert_eq!(
tree.insert(100, the_answer, GFP_KERNEL).unwrap_err().cause,
InsertErrorKind::Occupied,
);
Sourcepub fn insert_range<R>(
&self,
range: R,
value: T,
gfp: Flags,
) -> Result<(), InsertError<T>>where
R: RangeBounds<usize>,
pub fn insert_range<R>(
&self,
range: R,
value: T,
gfp: Flags,
) -> Result<(), InsertError<T>>where
R: RangeBounds<usize>,
Insert a value to the specified range, failing on overlap.
This accepts the usual types of Rust ranges using the ..
and ..=
syntax for exclusive
and inclusive ranges respectively. The range must not be empty, and must not overlap with
any existing range.
§Errors
If the maple tree already contains an overlapping range, then this call will return an
InsertErrorKind::Occupied
. It may also fail if memory allocation fails or if the
requested range is invalid (e.g. empty).
§Examples
use kernel::maple_tree::{InsertErrorKind, MapleTree};
let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
let ten = KBox::new(10, GFP_KERNEL)?;
let twenty = KBox::new(20, GFP_KERNEL)?;
let the_answer = KBox::new(42, GFP_KERNEL)?;
let hundred = KBox::new(100, GFP_KERNEL)?;
// Insert the value 10 at the indices 100 to 499.
tree.insert_range(100..500, ten, GFP_KERNEL)?;
// Insert the value 20 at the indices 500 to 1000.
tree.insert_range(500..=1000, twenty, GFP_KERNEL)?;
// This will fail due to overlap with the previous range on index 1000.
assert_eq!(
tree.insert_range(1000..1200, the_answer, GFP_KERNEL).unwrap_err().cause,
InsertErrorKind::Occupied,
);
// When using .. to specify the range, you must be careful to ensure that the range is
// non-empty.
assert_eq!(
tree.insert_range(72..72, hundred, GFP_KERNEL).unwrap_err().cause,
InsertErrorKind::InvalidRequest,
);
Sourcepub fn erase(&self, index: usize) -> Option<T>
pub fn erase(&self, index: usize) -> Option<T>
Erase the range containing the given index.
§Examples
use kernel::maple_tree::MapleTree;
let tree = KBox::pin_init(MapleTree::<KBox<i32>>::new(), GFP_KERNEL)?;
let ten = KBox::new(10, GFP_KERNEL)?;
let twenty = KBox::new(20, GFP_KERNEL)?;
tree.insert_range(100..500, ten, GFP_KERNEL)?;
tree.insert(67, twenty, GFP_KERNEL)?;
assert_eq!(tree.erase(67).map(|v| *v), Some(20));
assert_eq!(tree.erase(275).map(|v| *v), Some(10));
// The previous call erased the entire range, not just index 275.
assert!(tree.erase(127).is_none());
Sourcepub fn lock(&self) -> MapleGuard<'_, T>
pub fn lock(&self) -> MapleGuard<'_, T>
Lock the internal spinlock.
Trait Implementations§
Source§impl<T: ForeignOwnable> Drop for MapleTree<T>
impl<T: ForeignOwnable> Drop for MapleTree<T>
Source§impl<T: ForeignOwnable> HasPinData for MapleTree<T>
impl<T: ForeignOwnable> HasPinData for MapleTree<T>
Source§impl<T: ForeignOwnable> PinnedDrop for MapleTree<T>
impl<T: ForeignOwnable> PinnedDrop for MapleTree<T>
Auto Trait Implementations§
impl<T> !Freeze for MapleTree<T>
impl<T> !RefUnwindSafe for MapleTree<T>
impl<T> !Send for MapleTree<T>
impl<T> !Sync for MapleTree<T>
impl<T> UnwindSafe for MapleTree<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> PinInit<T> for T
impl<T> PinInit<T> for T
Source§unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>
slot
. Read more