FROMLIST: rust: rbtree: add RBTreeIteratorMut

Add mutable Iterator implementation (`RBTreeIteratorMut`) for `RBTree`,
allowing iteration over (key, value) pairs in key order. Only values are
mutable, as mutating keys implies modifying a node's position in the tree.

Mutable iteration is used by the binder driver during shutdown to
clean up the tree maintained by the "range allocator" [1].

Link: https://lore.kernel.org/rust-for-linux/20231101-rust-binder-v1-6-08ba9197f637@google.com/ [1]
Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
Signed-off-by: Matt Gilbride <mattgilbride@gmail.com>

Bug: 324206405
Link: https://lore.kernel.org/all/20240205-b4-rbtree-v1-4-995e3eee38c0@google.com/
Change-Id: I1cd8938036c1c8e5a4237b89c7917adbac5ce371
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
This commit is contained in:
Wedson Almeida Filho 2024-02-05 15:50:04 +00:00 committed by Treehugger Robot
parent 97cbf6e271
commit a3e1a10677

View File

@ -221,6 +221,15 @@ impl<K, V> RBTree<K, V> {
}
}
/// Returns a mutable iterator over the tree nodes, sorted by key.
pub fn iter_mut(&mut self) -> RBTreeIteratorMut<'_, K, V> {
RBTreeIteratorMut {
_tree: PhantomData,
// SAFETY: `root` is valid as it's embedded in `self` and we have a valid `self`.
next: unsafe { bindings::rb_first(&self.root) },
}
}
/// Returns an iterator over the keys of the nodes in the tree, in sorted order.
pub fn keys(&self) -> impl Iterator<Item = &'_ K> {
self.iter().map(|(k, _)| k)
@ -230,6 +239,11 @@ impl<K, V> RBTree<K, V> {
pub fn values(&self) -> impl Iterator<Item = &'_ V> {
self.iter().map(|(_, v)| v)
}
/// Returns a mutable iterator over the values of the nodes in the tree, sorted by key.
pub fn values_mut(&mut self) -> impl Iterator<Item = &'_ mut V> {
self.iter_mut().map(|(_, v)| v)
}
}
impl<K, V> RBTree<K, V>
@ -438,6 +452,53 @@ impl<'a, K, V> Iterator for RBTreeIterator<'a, K, V> {
}
}
impl<'a, K, V> IntoIterator for &'a mut RBTree<K, V> {
type Item = (&'a K, &'a mut V);
type IntoIter = RBTreeIteratorMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
/// A mutable iterator over the nodes of a [`RBTree`].
///
/// Instances are created by calling [`RBTree::iter_mut`].
pub struct RBTreeIteratorMut<'a, K, V> {
_tree: PhantomData<&'a RBTree<K, V>>,
next: *mut bindings::rb_node,
}
// SAFETY: An [`RBTree`] allows the same kinds of access to its values that a struct allows to its
// fields, so we use the same Send condition as would be used for a struct with K and V fields.
unsafe impl<'a, K: Send, V: Send> Send for RBTreeIteratorMut<'a, K, V> {}
// SAFETY: An [`RBTree`] allows the same kinds of access to its values that a struct allows to its
// fields, so we use the same Sync condition as would be used for a struct with K and V fields.
unsafe impl<'a, K: Sync, V: Sync> Sync for RBTreeIteratorMut<'a, K, V> {}
impl<'a, K, V> Iterator for RBTreeIteratorMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
if self.next.is_null() {
return None;
}
// SAFETY: All links fields we create are in a `Node<K, V>`.
let cur = unsafe { crate::container_of!(self.next, Node<K, V>, links) }.cast_mut();
// SAFETY: The reference to the tree used to create the iterator outlives the iterator, so
// the tree cannot change (except for the value of previous nodes, but those don't affect
// the iteration process). By the tree invariant, all nodes are valid.
self.next = unsafe { bindings::rb_next(self.next) };
// SAFETY: By the same reasoning above, it is safe to dereference the node. Additionally,
// it is ok to return a reference to members because the iterator must outlive it.
Some(unsafe { (&(*cur).key, &mut (*cur).value) })
}
}
/// A memory reservation for a red-black tree node.
///
/// It contains the memory needed to hold a node that can be inserted into a red-black tree. One