[PATCH 4/4] rust: xarray: Add Guard::find() helper
From: Alvin Sun
Date: Thu Apr 16 2026 - 21:15:32 EST
Add a helper to find the first present entry in the XArray.
Returns the index of the first present entry, or None if the array
is empty.
Signed-off-by: Alvin Sun <alvin.sun@xxxxxxxxx>
---
rust/kernel/xarray.rs | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
index 235fda0e394ba..e43129d032d9d 100644
--- a/rust/kernel/xarray.rs
+++ b/rust/kernel/xarray.rs
@@ -217,6 +217,28 @@ pub fn remove(&mut self, index: usize) -> Option<T> {
unsafe { T::try_from_foreign(ptr) }
}
+ /// Finds the first present entry.
+ ///
+ /// Returns the index of the first present entry, or `None` if the array is empty.
+ pub fn find(&mut self) -> Option<usize> {
+ let mut index = 0usize;
+ // SAFETY: `self.xa.xa` is always valid by the type invariant, and we hold the lock.
+ let ptr = unsafe {
+ bindings::xa_find(
+ self.xa.xa.get(),
+ &mut index,
+ usize::MAX,
+ bindings::XA_PRESENT,
+ )
+ };
+
+ if ptr.is_null() {
+ None
+ } else {
+ Some(index)
+ }
+ }
+
/// Stores an element at the given index.
///
/// May drop the lock if needed to allocate memory, and then reacquire it afterwards.
--
2.43.0