From nobody Wed Oct 23 00:30:45 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1729528743; cv=none; d=zohomail.com; s=zohoarc; b=GIEzzIxGEPChQEKnBrfo0lDyvIo1c5cdK7BoEjz+th/C8TRJMtJkhrERFoobRXV3fRomE7mYTf2T2lGIX22wTYi4YG0iSuLV8ovTMiGIVvsTgObgowXdiM+Ie/Oz48VFzNDY18AjravLs+yJIj99ADZeN+/wshaTkUYGnNFJ1tg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1729528743; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=kvb89qSp/JxmYfOj6SZi8Bx0KqHl+LVpfSQJUogsuq4=; b=kRJ3XoNQQoIMLNLp1rHAX0OCbkd+SORHwNRRE7REnwRMoUIWxDheELWwU8hxL+QQZQcrjTP1SaPPiHJYSggcftpCryRYSBjOQ5O3ec642DBqenn0/Ojzm2KSc4tCpr9E/BGQUTg2buMCLoXB1LCYTWTxA6z8NwfwIQ+woxqe9mI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1729528743580157.55500671571758; Mon, 21 Oct 2024 09:39:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t2vP2-0006yC-1k; Mon, 21 Oct 2024 12:37:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t2vOD-00067m-VV for qemu-devel@nongnu.org; Mon, 21 Oct 2024 12:36:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t2vO9-0006Jx-Li for qemu-devel@nongnu.org; Mon, 21 Oct 2024 12:36:17 -0400 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-385-IsaXhJRgMfC9XKIlxMzokA-1; Mon, 21 Oct 2024 12:36:11 -0400 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4315544642eso37410185e9.3 for ; Mon, 21 Oct 2024 09:36:11 -0700 (PDT) Received: from avogadro.local ([151.95.144.54]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4316f57fa4fsm62774405e9.16.2024.10.21.09.36.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Oct 2024 09:36:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729528572; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kvb89qSp/JxmYfOj6SZi8Bx0KqHl+LVpfSQJUogsuq4=; b=UxvnHXxUvAngz3NM7pQsn7W1Gpx3F0T690YPCi24AOnJk+w6Xe1xefvLMLRwMd+ujX6MNQ qFL6eVRzFZh+y7Nf4ixodcsYlZbo6iDQSWn7PXBlW7Nf8cmvyz4D5u4VhyOz8E7LHEw4Cr t+Og0WGDuaGzGvF0i/QQHJpFU34/aTc= X-MC-Unique: IsaXhJRgMfC9XKIlxMzokA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729528569; x=1730133369; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kvb89qSp/JxmYfOj6SZi8Bx0KqHl+LVpfSQJUogsuq4=; b=mVVkyBcrkE5nJEKV9PTTZGMDk4VTCFpShtfdtWt0G+iqWE0FyfolbCOwaZgUBIRsvK MBj6RVSHHnQUM8b/wFzOSjndi2/68AQSyDtziSv6QfXFgtBDjQekGnc/W5EpSX0zRKMl V6VGz6kGu/FwNfLrIm+aH/Z8Niu4DRyLsilkQZWOxURavFIeJmTRBXOQHIox/FWn8dLH q16fetw8Kr+/KTQRbC1XmbYutjAnjzd4XcQcvatOtdkEjHiSSm0VpirhEN1uF+sks5V2 znY+XDgpv/m54VKDWOq3WEGE19xcEdYoePGDjvLPjuheLreOfZLpi3lBulV+pOuslaNp Xoog== X-Gm-Message-State: AOJu0Yz/5yvw+LNTA6PiFRiDUlQHGckZtWxFzQ/wOAY2xJiw3rUSmTQ9 aX/BhO+4S/NeGzPHwapwWBa6iZ+0THHXInTwgID5kuXjSAMmqRDdQY5u0RouE3WWiWJMK/UILB0 Vo/KUP/szWzK4N92DAyRBuYj4MQHPq8Gd6H+lYzJwk9wSQCNyOefwFMga89p65FOKPt6YkGJheO yQX9ziN4/KIuxnCbZIn9oszFkOulu7WgqJiEyIgd4= X-Received: by 2002:a05:600c:1d01:b0:42c:b74c:d8c3 with SMTP id 5b1f17b1804b1-4316168ff01mr99496365e9.32.1729528569240; Mon, 21 Oct 2024 09:36:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGoo2DZlzYa5tO9scq9rSbVdszZ2ED+SvdnMr+4+YYOsddyVtq1HlTczBGmh8mLjTphc1v7jg== X-Received: by 2002:a05:600c:1d01:b0:42c:b74c:d8c3 with SMTP id 5b1f17b1804b1-4316168ff01mr99496045e9.32.1729528568746; Mon, 21 Oct 2024 09:36:08 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Manos Pitsidianakis , Junjie Mao Subject: [PATCH v2 08/13] rust: cleanup module_init!, use it from #[derive(Object)] Date: Mon, 21 Oct 2024 18:35:33 +0200 Message-ID: <20241021163538.136941-9-pbonzini@redhat.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241021163538.136941-1-pbonzini@redhat.com> References: <20241021163538.136941-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.421, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-1.699, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1729528744090116600 Content-Type: text/plain; charset="utf-8" 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 implement= ation of #[derive(Object)]. Signed-off-by: Paolo Bonzini Reviewed-by: Junjie Mao --- rust/qemu-api-macros/src/lib.rs | 33 +++------------- rust/qemu-api/src/definitions.rs | 66 ++++++++++++++------------------ 2 files changed, 33 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 =20 use proc_macro::TokenStream; -use quote::{format_ident, quote}; +use quote::quote; use syn::{parse_macro_input, DeriveInput}; =20 #[proc_macro_derive(Object)] pub fn derive_object(input: TokenStream) -> TokenStream { let input =3D parse_macro_input!(input as DeriveInput); - let name =3D input.ident; - let module_static =3D format_ident!("__{}_LOAD_MODULE", name); =20 let expanded =3D quote! { - #[allow(non_upper_case_globals)] - #[used] - #[cfg_attr( - not(any(target_vendor =3D "apple", target_os =3D "windows")), - link_section =3D ".init_array" - )] - #[cfg_attr(target_vendor =3D "apple", link_section =3D "__DATA,__m= od_init_func")] - #[cfg_attr(target_os =3D "windows", link_section =3D ".CRT$XCU")] - pub static #module_static: extern "C" fn() =3D { - 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 =3D> unsafe { + ::qemu_api::bindings::type_register_static(&<#name as ::qe= mu_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_INI= T_QOM - ); - } - } - - __load - }; + } }; =20 TokenStream::from(expanded) diff --git a/rust/qemu-api/src/definitions.rs b/rust/qemu-api/src/definitio= ns.rs index 3323a665d92..f180c38bfb2 100644 --- a/rust/qemu-api/src/definitions.rs +++ b/rust/qemu-api/src/definitions.rs @@ -29,51 +29,40 @@ pub trait Class { =20 #[macro_export] macro_rules! module_init { - ($func:expr, $type:expr) =3D> { - #[used] - #[cfg_attr( - not(any(target_vendor =3D "apple", target_os =3D "windows")), - link_section =3D ".init_array" - )] - #[cfg_attr(target_vendor =3D "apple", link_section =3D "__DATA,__m= od_init_func")] - #[cfg_attr(target_os =3D "windows", link_section =3D ".CRT$XCU")] - pub static LOAD_MODULE: extern "C" fn() =3D { - extern "C" fn __load() { - unsafe { - $crate::bindings::register_module_init(Some($func), $t= ype); - } - } - - __load - }; - }; - (qom: $func:ident =3D> $body:block) =3D> { - // NOTE: To have custom identifiers for the ctor func we need to e= ither supply - // them directly as a macro argument or create them with a proc ma= cro. - #[used] - #[cfg_attr( - not(any(target_vendor =3D "apple", target_os =3D "windows")), - link_section =3D ".init_array" - )] - #[cfg_attr(target_vendor =3D "apple", link_section =3D "__DATA,__m= od_init_func")] - #[cfg_attr(target_os =3D "windows", link_section =3D ".CRT$XCU")] - pub static LOAD_MODULE: extern "C" fn() =3D { - extern "C" fn __load() { - unsafe extern "C" fn $func() { + ($type:ident =3D> $body:block) =3D> { + const _: () =3D { + #[used] + #[cfg_attr( + not(any(target_vendor =3D "apple", target_os =3D "windows"= )), + link_section =3D ".init_array" + )] + #[cfg_attr(target_vendor =3D "apple", link_section =3D "__DATA= ,__mod_init_func")] + #[cfg_attr(target_os =3D "windows", link_section =3D ".CRT$XCU= ")] + pub static LOAD_MODULE: extern "C" fn() =3D { + extern "C" fn init_fn() { $body } =20 - unsafe { - $crate::bindings::register_module_init( - Some($func), - $crate::bindings::module_init_type::MODULE_INIT_QO= M, - ); + extern "C" fn ctor_fn() { + unsafe { + $crate::bindings::register_module_init( + Some(init_fn), + $crate::bindings::module_init_type::$type, + ); + } } - } =20 - __load + ctor_fn + }; }; }; + + // shortcut because it's quite common that $body needs unsafe {} + ($type:ident =3D> unsafe $body:block) =3D> { + $crate::module_init! { + $type =3D> { unsafe { $body } } + } + }; } =20 #[macro_export] --=20 2.46.2