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 --- 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 { 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 { + 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