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 = 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 <imammedo@redhat.com>
---
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 inherits
* from, the instance and class size, and constructor/destructor hooks.
*
+ * Alternatively several static types could be registered using helper macro
+ * DEFINE_TYPES()
+ *
+ * <example>
+ * <programlisting>
+ * static const TypeInfo device_types_info[] = {
+ * {
+ * .name = TYPE_MY_DEVICE_A,
+ * .parent = TYPE_DEVICE,
+ * .instance_size = sizeof(MyDeviceA),
+ * },
+ * {
+ * .name = TYPE_MY_DEVICE_B,
+ * .parent = TYPE_DEVICE,
+ * .instance_size = sizeof(MyDeviceB),
+ * },
+ * };
+ *
+ * DEFINE_TYPES(device_types_info)
+ * </programlisting>
+ * </example>
+ *
* Every type has an #ObjectClass associated with it. #ObjectClass derivatives
* are instantiated dynamically but there is only ever one instance for any
* given type. The #ObjectClass typically holds a table of function pointers
@@ -799,6 +821,20 @@ Type type_register(const TypeInfo *info);
void type_register_static_array(const TypeInfo *infos, int nr_infos);
/**
+ * 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.
--
2.7.4
On Wed, Oct 04, 2017 at 12:08:02PM +0200, Igor Mammedov wrote:
> 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 = 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 <imammedo@redhat.com>
Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
I will wait for 1 day or 2 before queueing it.
--
Eduardo
On Wed, 4 Oct 2017 18:04:59 -0300
Eduardo Habkost <ehabkost@redhat.com> wrote:
> On Wed, Oct 04, 2017 at 12:08:02PM +0200, Igor Mammedov wrote:
> > 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 = 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 <imammedo@redhat.com>
>
> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com>
>
> I will wait for 1 day or 2 before queueing it.
Thanks!
I'll respin whole series with amended "foo: cleanup cpu type name composition"
patches due to s/type_init_from_array/DEFINE_TYPES/ change
On 10/04/2017 07:08 AM, Igor Mammedov wrote:
> 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 = 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 <imammedo@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
> 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 inherits
> * from, the instance and class size, and constructor/destructor hooks.
> *
> + * Alternatively several static types could be registered using helper macro
> + * DEFINE_TYPES()
> + *
> + * <example>
> + * <programlisting>
> + * static const TypeInfo device_types_info[] = {
> + * {
> + * .name = TYPE_MY_DEVICE_A,
> + * .parent = TYPE_DEVICE,
> + * .instance_size = sizeof(MyDeviceA),
> + * },
> + * {
> + * .name = TYPE_MY_DEVICE_B,
> + * .parent = TYPE_DEVICE,
> + * .instance_size = sizeof(MyDeviceB),
> + * },
> + * };
> + *
> + * DEFINE_TYPES(device_types_info)
> + * </programlisting>
> + * </example>
> + *
> * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
> * are instantiated dynamically but there is only ever one instance for any
> * given type. The #ObjectClass typically holds a table of function pointers
> @@ -799,6 +821,20 @@ Type type_register(const TypeInfo *info);
> void type_register_static_array(const TypeInfo *infos, int nr_infos);
>
> /**
> + * 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.
>
© 2016 - 2026 Red Hat, Inc.