[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 13/40] rust: cleanup module_init!, use it from #[derive(Object)]
From: |
Paolo Bonzini |
Subject: |
[PULL 13/40] rust: cleanup module_init!, use it from #[derive(Object)] |
Date: |
Mon, 4 Nov 2024 18:26:52 +0100 |
Remove the duplicate code by using the module_init! macro; at the same time,
simplify how module_init! is used, by taking inspiration from the implementation
of #[derive(Object)].
Reviewed-by: Junjie Mao <junjie.mao@hotmail.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api-macros/src/lib.rs | 33 +++-------------
rust/qemu-api/src/definitions.rs | 65 +++++++++++++-------------------
2 files changed, 32 insertions(+), 66 deletions(-)
diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
index 70e3f920460..a4bc5d01ee8 100644
--- a/rust/qemu-api-macros/src/lib.rs
+++ b/rust/qemu-api-macros/src/lib.rs
@@ -3,43 +3,20 @@
// SPDX-License-Identifier: GPL-2.0-or-later
use proc_macro::TokenStream;
-use quote::{format_ident, quote};
+use quote::quote;
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(Object)]
pub fn derive_object(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
-
let name = input.ident;
- let module_static = format_ident!("__{}_LOAD_MODULE", name);
let expanded = quote! {
- #[allow(non_upper_case_globals)]
- #[used]
- #[cfg_attr(
- not(any(target_vendor = "apple", target_os = "windows")),
- link_section = ".init_array"
- )]
- #[cfg_attr(target_vendor = "apple", link_section =
"__DATA,__mod_init_func")]
- #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
- pub static #module_static: extern "C" fn() = {
- extern "C" fn __register() {
- unsafe {
- ::qemu_api::bindings::type_register_static(&<#name as
::qemu_api::definitions::ObjectImpl>::TYPE_INFO);
- }
+ ::qemu_api::module_init! {
+ MODULE_INIT_QOM => unsafe {
+ ::qemu_api::bindings::type_register_static(&<#name as
::qemu_api::definitions::ObjectImpl>::TYPE_INFO);
}
-
- extern "C" fn __load() {
- unsafe {
- ::qemu_api::bindings::register_module_init(
- Some(__register),
- ::qemu_api::bindings::module_init_type::MODULE_INIT_QOM
- );
- }
- }
-
- __load
- };
+ }
};
TokenStream::from(expanded)
diff --git a/rust/qemu-api/src/definitions.rs b/rust/qemu-api/src/definitions.rs
index 3323a665d92..064afe60549 100644
--- a/rust/qemu-api/src/definitions.rs
+++ b/rust/qemu-api/src/definitions.rs
@@ -29,51 +29,40 @@ pub trait Class {
#[macro_export]
macro_rules! module_init {
- ($func:expr, $type:expr) => {
- #[used]
- #[cfg_attr(
- not(any(target_vendor = "apple", target_os = "windows")),
- link_section = ".init_array"
- )]
- #[cfg_attr(target_vendor = "apple", link_section =
"__DATA,__mod_init_func")]
- #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
- pub static LOAD_MODULE: extern "C" fn() = {
- extern "C" fn __load() {
- unsafe {
- $crate::bindings::register_module_init(Some($func), $type);
- }
- }
-
- __load
- };
- };
- (qom: $func:ident => $body:block) => {
- // NOTE: To have custom identifiers for the ctor func we need to
either supply
- // them directly as a macro argument or create them with a proc macro.
- #[used]
- #[cfg_attr(
- not(any(target_vendor = "apple", target_os = "windows")),
- link_section = ".init_array"
- )]
- #[cfg_attr(target_vendor = "apple", link_section =
"__DATA,__mod_init_func")]
- #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
- pub static LOAD_MODULE: extern "C" fn() = {
- extern "C" fn __load() {
- unsafe extern "C" fn $func() {
+ ($type:ident => $body:block) => {
+ const _: () = {
+ #[used]
+ #[cfg_attr(
+ not(any(target_vendor = "apple", target_os = "windows")),
+ link_section = ".init_array"
+ )]
+ #[cfg_attr(target_vendor = "apple", link_section =
"__DATA,__mod_init_func")]
+ #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
+ pub static LOAD_MODULE: extern "C" fn() = {
+ extern "C" fn init_fn() {
$body
}
- unsafe {
- $crate::bindings::register_module_init(
- Some($func),
- $crate::bindings::module_init_type::MODULE_INIT_QOM,
- );
+ extern "C" fn ctor_fn() {
+ unsafe {
+ $crate::bindings::register_module_init(
+ Some(init_fn),
+ $crate::bindings::module_init_type::$type,
+ );
+ }
}
- }
- __load
+ ctor_fn
+ };
};
};
+
+ // shortcut because it's quite common that $body needs unsafe {}
+ ($type:ident => unsafe $body:block) => {
+ $crate::module_init! {
+ $type => { unsafe { $body } }
+ }
+ };
}
#[macro_export]
--
2.47.0
- [PULL 03/40] Revert "rust: add PL011 device model", (continued)
- [PULL 03/40] Revert "rust: add PL011 device model", Paolo Bonzini, 2024/11/04
- [PULL 06/40] meson: remove repeated search for rust_root_crate.sh, Paolo Bonzini, 2024/11/04
- [PULL 05/40] meson: import rust module into a global variable, Paolo Bonzini, 2024/11/04
- [PULL 04/40] rust: add PL011 device model, Paolo Bonzini, 2024/11/04
- [PULL 07/40] meson: pass rustc_args when building all crates, Paolo Bonzini, 2024/11/04
- [PULL 09/40] rust: do not use --no-size_t-is-usize, Paolo Bonzini, 2024/11/04
- [PULL 10/40] rust: remove uses of #[no_mangle], Paolo Bonzini, 2024/11/04
- [PULL 08/40] rust: do not always select X_PL011_RUST, Paolo Bonzini, 2024/11/04
- [PULL 11/40] rust: modernize link_section usage for ELF platforms, Paolo Bonzini, 2024/11/04
- [PULL 12/40] rust: build integration test for the qemu_api crate, Paolo Bonzini, 2024/11/04
- [PULL 13/40] rust: cleanup module_init!, use it from #[derive(Object)],
Paolo Bonzini <=
- [PULL 16/40] rust: provide safe wrapper for MaybeUninit::zeroed(), Paolo Bonzini, 2024/11/04
- [PULL 15/40] rust: make properties array immutable, Paolo Bonzini, 2024/11/04
- [PULL 17/40] rust: do not use TYPE_CHARDEV unnecessarily, Paolo Bonzini, 2024/11/04
- [PULL 19/40] rust/pl011: fix default value for migrate-clock, Paolo Bonzini, 2024/11/04
- [PULL 14/40] rust: clean up define_property macro, Paolo Bonzini, 2024/11/04
- [PULL 18/40] rust: add definitions for vmstate, Paolo Bonzini, 2024/11/04
- [PULL 20/40] rust/pl011: add support for migration, Paolo Bonzini, 2024/11/04
- [PULL 22/40] rust/pl011: add TYPE_PL011_LUMINARY device, Paolo Bonzini, 2024/11/04
- [PULL 21/40] rust/pl011: move CLK_NAME static to function scope, Paolo Bonzini, 2024/11/04
- [PULL 23/40] rust/pl011: remove commented out C code, Paolo Bonzini, 2024/11/04