From nobody Mon Feb 9 23:01:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 150711188218223.24163355112546; Wed, 4 Oct 2017 03:11:22 -0700 (PDT) Received: from localhost ([::1]:34052 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dzge0-0002eV-FT for importer@patchew.org; Wed, 04 Oct 2017 06:11:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40777) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dzgb7-0000hK-83 for qemu-devel@nongnu.org; Wed, 04 Oct 2017 06:08:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dzgb6-0005vD-4Q for qemu-devel@nongnu.org; Wed, 04 Oct 2017 06:08:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57232) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dzgb5-0005uk-St for qemu-devel@nongnu.org; Wed, 04 Oct 2017 06:08:12 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0B6251E328; Wed, 4 Oct 2017 10:08:11 +0000 (UTC) Received: from dell-r430-03.lab.eng.brq.redhat.com (dell-r430-03.lab.eng.brq.redhat.com [10.34.112.60]) by smtp.corp.redhat.com (Postfix) with ESMTP id 338D262679; Wed, 4 Oct 2017 10:08:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0B6251E328 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=imammedo@redhat.com From: Igor Mammedov To: qemu-devel@nongnu.org Date: Wed, 4 Oct 2017 12:08:02 +0200 Message-Id: <1507111682-66171-4-git-send-email-imammedo@redhat.com> In-Reply-To: <1507111682-66171-1-git-send-email-imammedo@redhat.com> References: <1506935300-132598-2-git-send-email-imammedo@redhat.com> <1507111682-66171-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 04 Oct 2017 10:08:11 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 3/3] qom: add helper macro DEFINE_TYPES() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, ehabkost@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" DEFINE_TYPES() will help to simplify following routine patterns: static void foo_register_types(void) { type_register_static(&foo1_type_info); type_register_static(&foo2_type_info); ... } type_init(foo_register_types) or static void foo_register_types(void) { int i; for (i =3D 0; i < ARRAY_SIZE(type_infos); i++) { type_register_static(&type_infos[i]); } } type_init(foo_register_types) with a single line DEFINE_TYPES(type_infos) where types have static definition which could be consolidated in a single array of TypeInfo structures. It saves us ~6-10LOC per use case and would help to replace imperative foo_register_types() there with declarative style of type registration. Signed-off-by: Igor Mammedov Reviewed-by: Eduardo Habkost Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- include/qom/object.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/include/qom/object.h b/include/qom/object.h index ce25567..a615066 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -79,6 +79,28 @@ typedef struct InterfaceInfo InterfaceInfo; * #TypeInfo describes information about the type including what it inheri= ts * from, the instance and class size, and constructor/destructor hooks. * + * Alternatively several static types could be registered using helper mac= ro + * DEFINE_TYPES() + * + * + * + * static const TypeInfo device_types_info[] =3D { + * { + * .name =3D TYPE_MY_DEVICE_A, + * .parent =3D TYPE_DEVICE, + * .instance_size =3D sizeof(MyDeviceA), + * }, + * { + * .name =3D TYPE_MY_DEVICE_B, + * .parent =3D TYPE_DEVICE, + * .instance_size =3D sizeof(MyDeviceB), + * }, + * }; + * + * DEFINE_TYPES(device_types_info) + * + * + * * Every type has an #ObjectClass associated with it. #ObjectClass deriva= tives * are instantiated dynamically but there is only ever one instance for any * given type. The #ObjectClass typically holds a table of function point= ers @@ -799,6 +821,20 @@ Type type_register(const TypeInfo *info); void type_register_static_array(const TypeInfo *infos, int nr_infos); =20 /** + * DEFINE_TYPES: + * @type_array: The array containing #TypeInfo structures to register + * + * @type_array should be static constant that exists for the life time + * that the type is registered. + */ +#define DEFINE_TYPES(type_array) = \ +static void do_qemu_init_ ## type_array(void) = \ +{ = \ + type_register_static_array(type_array, ARRAY_SIZE(type_array)); = \ +} = \ +type_init(do_qemu_init_ ## type_array) + +/** * object_class_dynamic_cast_assert: * @klass: The #ObjectClass to attempt to cast. * @typename: The QOM typename of the class to cast to. --=20 2.7.4