[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*m
From: |
Zhao Liu |
Subject: |
[PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState] |
Date: |
Sat, 25 Jan 2025 20:51:30 +0800 |
This is useful to hanlde InterruptSource slice and pass it to C
bindings.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
Changes since RFC:
* New commit.
---
rust/qemu-api/src/irq.rs | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/rust/qemu-api/src/irq.rs b/rust/qemu-api/src/irq.rs
index 638545c3a649..7dca3b9ee5a8 100644
--- a/rust/qemu-api/src/irq.rs
+++ b/rust/qemu-api/src/irq.rs
@@ -5,12 +5,10 @@
//! Bindings for interrupt sources
use core::ptr;
-use std::{marker::PhantomData, os::raw::c_int};
+use std::{marker::PhantomData, os::raw::c_int, slice};
-use crate::{
- bindings::{qemu_set_irq, IRQState},
- prelude::*,
-};
+pub(crate) use crate::bindings::IRQState;
+use crate::{bindings::qemu_set_irq, prelude::*};
/// Interrupt sources are used by devices to pass changes to a value (typically
/// a boolean). The interrupt sink is usually an interrupt controller or
@@ -81,6 +79,16 @@ pub fn set(&self, level: T) {
pub(crate) const fn as_ptr(&self) -> *mut *mut IRQState {
self.cell.as_ptr()
}
+
+ #[allow(dead_code)]
+ pub(crate) fn as_slice_of_qemu_irq(slice: &[Self]) -> &[*mut IRQState] {
+ unsafe {
+ // SAFETY: InterruptSource has the same memory layout as *mut
IRQState.
+ // Additionally, the slice parameter itself, as a reference to a
slice type,
+ // can be converted into a valid pointer and has a valid length.
+ slice::from_raw_parts(slice.as_ptr().cast::<*mut IRQState>(),
slice.len())
+ }
+ }
}
impl Default for InterruptSource {
--
2.34.1
- [PATCH 00/10] rust: Add HPET timer device, Zhao Liu, 2025/01/25
- [PATCH 02/10] rust/qdev: add the macro to define bit property, Zhao Liu, 2025/01/25
- [PATCH 03/10] rust/irq: Add a helper to convert [InterruptSource] to [*mut IRQState],
Zhao Liu <=
- [PATCH 04/10] rust: add bindings for gpio_{in|out} initialization, Zhao Liu, 2025/01/25
- [PATCH 05/10] rust: add bindings for memattrs, Zhao Liu, 2025/01/25
- [PATCH 01/10] i386/fw_cfg: move hpet_cfg definition to hpet.c, Zhao Liu, 2025/01/25
- [PATCH 06/10] rust: add bindings for timer, Zhao Liu, 2025/01/25
- [PATCH 07/10] rust/timer/hpet: define hpet_cfg, Zhao Liu, 2025/01/25
- [PATCH 08/10] rust/timer/hpet: add basic HPET timer and HPETState, Zhao Liu, 2025/01/25