mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 20:33:54 -06:00
rust: cleanup module_init!, use it from #[derive(Object)]
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>
This commit is contained in:
parent
cde3c425d1
commit
e90d470733
2 changed files with 32 additions and 66 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue