diff options
Diffstat (limited to 'drivers/gpu/nova-core/util.rs')
| -rw-r--r-- | drivers/gpu/nova-core/util.rs | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/drivers/gpu/nova-core/util.rs b/drivers/gpu/nova-core/util.rs index bf35f00cb732..4b503249a3ef 100644 --- a/drivers/gpu/nova-core/util.rs +++ b/drivers/gpu/nova-core/util.rs @@ -1,27 +1,16 @@ // SPDX-License-Identifier: GPL-2.0 -use kernel::prelude::*; -use kernel::time::{Delta, Instant, Monotonic}; - -/// Wait until `cond` is true or `timeout` elapsed. -/// -/// When `cond` evaluates to `Some`, its return value is returned. +/// Converts a null-terminated byte slice to a string, or `None` if the array does not +/// contains any null byte or contains invalid characters. /// -/// `Err(ETIMEDOUT)` is returned if `timeout` has been reached without `cond` evaluating to -/// `Some`. -/// -/// TODO[DLAY]: replace with `read_poll_timeout` once it is available. -/// (https://lore.kernel.org/lkml/20250220070611.214262-8-fujita.tomonori@gmail.com/) -pub(crate) fn wait_on<R, F: Fn() -> Option<R>>(timeout: Delta, cond: F) -> Result<R> { - let start_time = Instant::<Monotonic>::now(); - - loop { - if let Some(ret) = cond() { - return Ok(ret); - } +/// Contrary to [`kernel::str::CStr::from_bytes_with_nul`], the null byte can be anywhere in the +/// slice, and not only in the last position. +pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> { + use kernel::str::CStr; - if start_time.elapsed().as_nanos() > timeout.as_nanos() { - return Err(ETIMEDOUT); - } - } + bytes + .iter() + .position(|&b| b == 0) + .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok()) + .and_then(|cstr| cstr.to_str().ok()) } |
