[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 27/48] rust: vmstate: add varray support to vmstate_of!
From: |
Paolo Bonzini |
Subject: |
[PULL 27/48] rust: vmstate: add varray support to vmstate_of! |
Date: |
Fri, 24 Jan 2025 10:44:21 +0100 |
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api/src/vmstate.rs | 42 ++++++++++++++++++++++++++++++++++--
1 file changed, 40 insertions(+), 2 deletions(-)
diff --git a/rust/qemu-api/src/vmstate.rs b/rust/qemu-api/src/vmstate.rs
index 211c3d096b7..2b14d4839df 100644
--- a/rust/qemu-api/src/vmstate.rs
+++ b/rust/qemu-api/src/vmstate.rs
@@ -72,6 +72,15 @@ pub unsafe trait VMState {
/// The base contents of a `VMStateField` (minus the name and offset) for
/// the type that is implementing the trait.
const BASE: VMStateField;
+
+ /// A flag that is added to another field's `VMStateField` to specify the
+ /// length's type in a variable-sized array. If this is not a supported
+ /// type for the length (i.e. if it is not `u8`, `u16`, `u32`), using it
+ /// in a call to [`vmstate_of!`](crate::vmstate_of) will cause a
+ /// compile-time error.
+ const VARRAY_FLAG: VMStateFlags = {
+ panic!("invalid type for variable-sized array");
+ };
}
/// Internal utility function to retrieve a type's `VMStateField`;
@@ -80,6 +89,13 @@ pub const fn vmstate_base<T: VMState>(_: PhantomData<T>) ->
VMStateField {
T::BASE
}
+/// Internal utility function to retrieve a type's `VMStateFlags` when it
+/// is used as the element count of a `VMSTATE_VARRAY`; used by
+/// [`vmstate_of!`](crate::vmstate_of).
+pub const fn vmstate_varray_flag<T: VMState>(_: PhantomData<T>) ->
VMStateFlags {
+ T::VARRAY_FLAG
+}
+
/// Return the `VMStateField` for a field of a struct. The field must be
/// visible in the current scope.
///
@@ -87,18 +103,23 @@ pub const fn vmstate_base<T: VMState>(_: PhantomData<T>)
-> VMStateField {
/// for them.
#[macro_export]
macro_rules! vmstate_of {
- ($struct_name:ty, $field_name:ident $(,)?) => {
+ ($struct_name:ty, $field_name:ident $([0 .. $num:ident $(*
$factor:expr)?])? $(,)?) => {
$crate::bindings::VMStateField {
name: ::core::concat!(::core::stringify!($field_name), "\0")
.as_bytes()
.as_ptr() as *const ::std::os::raw::c_char,
offset: $crate::offset_of!($struct_name, $field_name),
// Compute most of the VMStateField from the type of the field.
+ $(.num_offset: $crate::offset_of!($struct_name, $num),)?
..$crate::call_func_with_field!(
$crate::vmstate::vmstate_base,
$struct_name,
$field_name
- )
+ )$(.with_varray_flag($crate::call_func_with_field!(
+ $crate::vmstate::vmstate_varray_flag,
+ $struct_name,
+ $num))
+ $(.with_varray_multiply($factor))?)?
}
};
}
@@ -143,6 +164,22 @@ pub const fn with_pointer_flag(mut self) -> Self {
self.flags = VMStateFlags(self.flags.0 | VMStateFlags::VMS_POINTER.0);
self
}
+
+ #[must_use]
+ pub const fn with_varray_flag<T: VMState>(mut self, flag: VMStateFlags) ->
VMStateField {
+ assert!((self.flags.0 & VMStateFlags::VMS_ARRAY.0) != 0);
+ self.flags = VMStateFlags(self.flags.0 & !VMStateFlags::VMS_ARRAY.0);
+ self.flags = VMStateFlags(self.flags.0 | flag.0);
+ self
+ }
+
+ #[must_use]
+ pub const fn with_varray_multiply(mut self, num: u32) -> VMStateField {
+ assert!(num <= 0x7FFF_FFFFu32);
+ self.flags = VMStateFlags(self.flags.0 |
VMStateFlags::VMS_MULTIPLY_ELEMENTS.0);
+ self.num = num as i32;
+ self
+ }
}
// Transparent wrappers: just use the internal type
@@ -154,6 +191,7 @@ unsafe impl<$base> VMState for $type where $base: VMState
$($where)* {
size: mem::size_of::<$type>(),
..<$base as VMState>::BASE
};
+ const VARRAY_FLAG: VMStateFlags = <$base as VMState>::VARRAY_FLAG;
}
};
}
--
2.48.1
- [PULL 30/48] rust: vmstate: add public utility macros to implement VMState, (continued)
- [PULL 30/48] rust: vmstate: add public utility macros to implement VMState, Paolo Bonzini, 2025/01/24
- [PULL 36/48] rust: pl011: remove unnecessary "extern crate", Paolo Bonzini, 2025/01/24
- [PULL 37/48] rust: pl011: hide unnecessarily "pub" items from outside pl011::device, Paolo Bonzini, 2025/01/24
- [PULL 48/48] rust: qemu-api: add sub-subclass to the integration tests, Paolo Bonzini, 2025/01/24
- [PULL 21/48] rust/qdev: Make REALIZE safe, Paolo Bonzini, 2025/01/24
- [PULL 45/48] rust: pl011: drop use of ControlFlow, Paolo Bonzini, 2025/01/24
- [PULL 40/48] rust: pl011: pull interrupt updates out of read/write ops, Paolo Bonzini, 2025/01/24
- [PULL 32/48] rust: pl011: switch vmstate to new-style macros, Paolo Bonzini, 2025/01/24
- [PULL 38/48] rust: pl011: extract conversion to RegisterOffset, Paolo Bonzini, 2025/01/24
- [PULL 47/48] rust/zeroable: Implement Zeroable with const_zero macro, Paolo Bonzini, 2025/01/24
- [PULL 27/48] rust: vmstate: add varray support to vmstate_of!,
Paolo Bonzini <=
- [PULL 28/48] rust: vmstate: implement Zeroable for VMStateField, Paolo Bonzini, 2025/01/24
- [PULL 31/48] rust: qemu_api: add vmstate_struct, Paolo Bonzini, 2025/01/24
- [PULL 39/48] rust: pl011: extract CharBackend receive logic into a separate function, Paolo Bonzini, 2025/01/24
- [PULL 44/48] rust: pl011: pull device-specific code out of MemoryRegionOps callbacks, Paolo Bonzini, 2025/01/24
- [PULL 33/48] rust: vmstate: remove translation of C vmstate macros, Paolo Bonzini, 2025/01/24
- [PULL 41/48] rust: pl011: extract PL011Registers, Paolo Bonzini, 2025/01/24
- [PULL 46/48] rust: qdev: make reset take a shared reference, Paolo Bonzini, 2025/01/24
- Re: [PULL 00/48] i386, rust changes for 2024-01-24, Stefan Hajnoczi, 2025/01/24
- [PULL 00/48] i386, rust changes for 2024-01-24, Paolo Bonzini, 2025/01/28