[PATCH v5 6/6] target-info-qom: use a single class_init for target-info-* classes

Pierrick Bouvier posted 6 patches 2 months, 1 week ago
Maintainers: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>
There is a newer version of this series
[PATCH v5 6/6] target-info-qom: use a single class_init for target-info-* classes
Posted by Pierrick Bouvier 2 months, 1 week ago
Instead of defining a class_init per class, just use a common
constructor and set class_data to corresponding TargetInfo structure.

Suggested-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
---
 include/qemu/target-info-init.h | 14 ++------------
 include/qemu/target-info-qom.h  |  1 +
 target-info-qom.c               |  6 ++++++
 3 files changed, 9 insertions(+), 12 deletions(-)

diff --git a/include/qemu/target-info-init.h b/include/qemu/target-info-init.h
index 176fd975a16..3945f2998e0 100644
--- a/include/qemu/target-info-init.h
+++ b/include/qemu/target-info-init.h
@@ -37,8 +37,6 @@ const TargetInfo *target_info(void)     \
 #include "qemu/target-info-qom.h"
 #include "qom/object.h"
 
-#define TYPE_TARGET_INFO_TARGET TYPE_TARGET_INFO"-"TARGET_NAME
-
 typedef struct TargetInfoQomTarget {
     TargetInfoQom parent;
 } TargetInfoQomTarget;
@@ -47,24 +45,16 @@ typedef struct TargetInfoQomTargetClass {
     TargetInfoQomClass parent_class;
 } TargetInfoQomTargetClass;
 
-OBJECT_DECLARE_TYPE(TargetInfoQomTarget, TargetInfoQomTargetClass, TARGET_INFO_TARGET)
-
 #define target_info_init(ti_var)                                            \
-static void target_info_qom_class_init(ObjectClass *oc, const void * data)  \
-{                                                                           \
-    TargetInfoQomTargetClass *klass = TARGET_INFO_TARGET_CLASS(oc);         \
-    klass->parent_class.target_info = &ti_var;                              \
-}                                                                           \
-                                                                            \
 static const TypeInfo target_info_qom_target_type_info = {                  \
-    .name = TYPE_TARGET_INFO_TARGET,                                        \
+    .name =  TYPE_TARGET_INFO"-"TARGET_NAME,                                \
     .parent = TYPE_TARGET_INFO,                                             \
     .instance_size = sizeof(TargetInfoQomTarget),                           \
     .class_size = sizeof(TargetInfoQomTargetClass),                         \
     .class_init = target_info_qom_class_init,                               \
+    .class_data = &ti_var,                                                  \
     .abstract = false,                                                      \
 };                                                                          \
-                                                                            \
 DEFINE_TARGET_INFO_TYPE(target_info_qom_target_type_info)
 
 #endif /* CONFIG_USER_ONLY */
diff --git a/include/qemu/target-info-qom.h b/include/qemu/target-info-qom.h
index 91be415ed33..a37c3e101e0 100644
--- a/include/qemu/target-info-qom.h
+++ b/include/qemu/target-info-qom.h
@@ -25,6 +25,7 @@ typedef struct TargetInfoQomClass {
 
 OBJECT_DECLARE_TYPE(TargetInfoQom, TargetInfoQomClass, TARGET_INFO)
 
+void target_info_qom_class_init(ObjectClass *oc, const void * data);
 void target_info_qom_set_target(void);
 
 #endif /* QEMU_TARGET_INFO_QOM_H */
diff --git a/target-info-qom.c b/target-info-qom.c
index cc470b3b4d6..5b0498ca654 100644
--- a/target-info-qom.c
+++ b/target-info-qom.c
@@ -37,6 +37,12 @@ static const TypeInfo target_info_parent_type = {
 
 DEFINE_TARGET_INFO_TYPE(target_info_parent_type)
 
+void target_info_qom_class_init(ObjectClass *oc, const void * data)
+{
+    TargetInfoQomClass *klass = TARGET_INFO_CLASS(oc);
+    klass->target_info = data;
+}
+
 static const TargetInfo *target_info_ptr;
 
 const TargetInfo *target_info(void)
-- 
2.43.0
Re: [PATCH v5 6/6] target-info-qom: use a single class_init for target-info-* classes
Posted by Pierrick Bouvier 2 months, 1 week ago
On 5/8/2026 5:54 PM, Pierrick Bouvier wrote:
> Instead of defining a class_init per class, just use a common
> constructor and set class_data to corresponding TargetInfo structure.
> 
> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
> ---
>  include/qemu/target-info-init.h | 14 ++------------
>  include/qemu/target-info-qom.h  |  1 +
>  target-info-qom.c               |  6 ++++++
>  3 files changed, 9 insertions(+), 12 deletions(-)
> 
> diff --git a/include/qemu/target-info-init.h b/include/qemu/target-info-init.h
> index 176fd975a16..3945f2998e0 100644
> --- a/include/qemu/target-info-init.h
> +++ b/include/qemu/target-info-init.h
> @@ -37,8 +37,6 @@ const TargetInfo *target_info(void)     \
>  #include "qemu/target-info-qom.h"
>  #include "qom/object.h"
>  
> -#define TYPE_TARGET_INFO_TARGET TYPE_TARGET_INFO"-"TARGET_NAME
> -
>  typedef struct TargetInfoQomTarget {
>      TargetInfoQom parent;
>  } TargetInfoQomTarget;
> @@ -47,24 +45,16 @@ typedef struct TargetInfoQomTargetClass {
>      TargetInfoQomClass parent_class;
>  } TargetInfoQomTargetClass;
>  
> -OBJECT_DECLARE_TYPE(TargetInfoQomTarget, TargetInfoQomTargetClass, TARGET_INFO_TARGET)
> -
>  #define target_info_init(ti_var)                                            \
> -static void target_info_qom_class_init(ObjectClass *oc, const void * data)  \
> -{                                                                           \
> -    TargetInfoQomTargetClass *klass = TARGET_INFO_TARGET_CLASS(oc);         \
> -    klass->parent_class.target_info = &ti_var;                              \
> -}                                                                           \
> -                                                                            \
>  static const TypeInfo target_info_qom_target_type_info = {                  \
> -    .name = TYPE_TARGET_INFO_TARGET,                                        \
> +    .name =  TYPE_TARGET_INFO"-"TARGET_NAME,                                \
>      .parent = TYPE_TARGET_INFO,                                             \
>      .instance_size = sizeof(TargetInfoQomTarget),                           \
>      .class_size = sizeof(TargetInfoQomTargetClass),                         \
>      .class_init = target_info_qom_class_init,                               \
> +    .class_data = &ti_var,                                                  \
>      .abstract = false,                                                      \
>  };                                                                          \
> -                                                                            \
>  DEFINE_TARGET_INFO_TYPE(target_info_qom_target_type_info)
>  
>  #endif /* CONFIG_USER_ONLY */
> diff --git a/include/qemu/target-info-qom.h b/include/qemu/target-info-qom.h
> index 91be415ed33..a37c3e101e0 100644
> --- a/include/qemu/target-info-qom.h
> +++ b/include/qemu/target-info-qom.h
> @@ -25,6 +25,7 @@ typedef struct TargetInfoQomClass {
>  
>  OBJECT_DECLARE_TYPE(TargetInfoQom, TargetInfoQomClass, TARGET_INFO)
>  
> +void target_info_qom_class_init(ObjectClass *oc, const void * data);
>  void target_info_qom_set_target(void);
>  
>  #endif /* QEMU_TARGET_INFO_QOM_H */
> diff --git a/target-info-qom.c b/target-info-qom.c
> index cc470b3b4d6..5b0498ca654 100644
> --- a/target-info-qom.c
> +++ b/target-info-qom.c
> @@ -37,6 +37,12 @@ static const TypeInfo target_info_parent_type = {
>  
>  DEFINE_TARGET_INFO_TYPE(target_info_parent_type)
>  
> +void target_info_qom_class_init(ObjectClass *oc, const void * data)
> +{
> +    TargetInfoQomClass *klass = TARGET_INFO_CLASS(oc);
> +    klass->target_info = data;
> +}
> +
>  static const TargetInfo *target_info_ptr;
>  
>  const TargetInfo *target_info(void)

This patch can be squashed with 3, but I thought it might be better for
review to keep it on top for now.

Regards,
Pierrick
Re: [PATCH v5 6/6] target-info-qom: use a single class_init for target-info-* classes
Posted by Richard Henderson 2 months, 1 week ago
On 5/8/26 19:59, Pierrick Bouvier wrote:
> On 5/8/2026 5:54 PM, Pierrick Bouvier wrote:
>> Instead of defining a class_init per class, just use a common
>> constructor and set class_data to corresponding TargetInfo structure.
>>
>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
>> ---
>>   include/qemu/target-info-init.h | 14 ++------------
>>   include/qemu/target-info-qom.h  |  1 +
>>   target-info-qom.c               |  6 ++++++
>>   3 files changed, 9 insertions(+), 12 deletions(-)
>>
>> diff --git a/include/qemu/target-info-init.h b/include/qemu/target-info-init.h
>> index 176fd975a16..3945f2998e0 100644
>> --- a/include/qemu/target-info-init.h
>> +++ b/include/qemu/target-info-init.h
>> @@ -37,8 +37,6 @@ const TargetInfo *target_info(void)     \
>>   #include "qemu/target-info-qom.h"
>>   #include "qom/object.h"
>>   
>> -#define TYPE_TARGET_INFO_TARGET TYPE_TARGET_INFO"-"TARGET_NAME
>> -
>>   typedef struct TargetInfoQomTarget {
>>       TargetInfoQom parent;
>>   } TargetInfoQomTarget;
>> @@ -47,24 +45,16 @@ typedef struct TargetInfoQomTargetClass {
>>       TargetInfoQomClass parent_class;
>>   } TargetInfoQomTargetClass;
>>   
>> -OBJECT_DECLARE_TYPE(TargetInfoQomTarget, TargetInfoQomTargetClass, TARGET_INFO_TARGET)
>> -
>>   #define target_info_init(ti_var)                                            \
>> -static void target_info_qom_class_init(ObjectClass *oc, const void * data)  \
>> -{                                                                           \
>> -    TargetInfoQomTargetClass *klass = TARGET_INFO_TARGET_CLASS(oc);         \
>> -    klass->parent_class.target_info = &ti_var;                              \
>> -}                                                                           \
>> -                                                                            \
>>   static const TypeInfo target_info_qom_target_type_info = {                  \
>> -    .name = TYPE_TARGET_INFO_TARGET,                                        \
>> +    .name =  TYPE_TARGET_INFO"-"TARGET_NAME,                                \
>>       .parent = TYPE_TARGET_INFO,                                             \
>>       .instance_size = sizeof(TargetInfoQomTarget),                           \
>>       .class_size = sizeof(TargetInfoQomTargetClass),                         \
>>       .class_init = target_info_qom_class_init,                               \
>> +    .class_data = &ti_var,                                                  \
>>       .abstract = false,                                                      \
>>   };                                                                          \
>> -                                                                            \
>>   DEFINE_TARGET_INFO_TYPE(target_info_qom_target_type_info)
>>   
>>   #endif /* CONFIG_USER_ONLY */
>> diff --git a/include/qemu/target-info-qom.h b/include/qemu/target-info-qom.h
>> index 91be415ed33..a37c3e101e0 100644
>> --- a/include/qemu/target-info-qom.h
>> +++ b/include/qemu/target-info-qom.h
>> @@ -25,6 +25,7 @@ typedef struct TargetInfoQomClass {
>>   
>>   OBJECT_DECLARE_TYPE(TargetInfoQom, TargetInfoQomClass, TARGET_INFO)
>>   
>> +void target_info_qom_class_init(ObjectClass *oc, const void * data);
>>   void target_info_qom_set_target(void);
>>   
>>   #endif /* QEMU_TARGET_INFO_QOM_H */
>> diff --git a/target-info-qom.c b/target-info-qom.c
>> index cc470b3b4d6..5b0498ca654 100644
>> --- a/target-info-qom.c
>> +++ b/target-info-qom.c
>> @@ -37,6 +37,12 @@ static const TypeInfo target_info_parent_type = {
>>   
>>   DEFINE_TARGET_INFO_TYPE(target_info_parent_type)
>>   
>> +void target_info_qom_class_init(ObjectClass *oc, const void * data)
>> +{
>> +    TargetInfoQomClass *klass = TARGET_INFO_CLASS(oc);
>> +    klass->target_info = data;
>> +}
>> +
>>   static const TargetInfo *target_info_ptr;
>>   
>>   const TargetInfo *target_info(void)
> 
> This patch can be squashed with 3, but I thought it might be better for
> review to keep it on top for now.

Mmm.  You've missed the part about using class_base_init instead of class_init, so that 
you don't need the extern function declaration.  See review of patch 3.


r~
Re: [PATCH v5 6/6] target-info-qom: use a single class_init for target-info-* classes
Posted by Pierrick Bouvier 2 months, 1 week ago
On 5/9/2026 8:03 AM, Richard Henderson wrote:
> On 5/8/26 19:59, Pierrick Bouvier wrote:
>> On 5/8/2026 5:54 PM, Pierrick Bouvier wrote:
>>> Instead of defining a class_init per class, just use a common
>>> constructor and set class_data to corresponding TargetInfo structure.
>>>
>>> Suggested-by: Richard Henderson <richard.henderson@linaro.org>
>>> Signed-off-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
>>> ---
>>>   include/qemu/target-info-init.h | 14 ++------------
>>>   include/qemu/target-info-qom.h  |  1 +
>>>   target-info-qom.c               |  6 ++++++
>>>   3 files changed, 9 insertions(+), 12 deletions(-)
>>>
>>> diff --git a/include/qemu/target-info-init.h b/include/qemu/target-
>>> info-init.h
>>> index 176fd975a16..3945f2998e0 100644
>>> --- a/include/qemu/target-info-init.h
>>> +++ b/include/qemu/target-info-init.h
>>> @@ -37,8 +37,6 @@ const TargetInfo *target_info(void)     \
>>>   #include "qemu/target-info-qom.h"
>>>   #include "qom/object.h"
>>>   -#define TYPE_TARGET_INFO_TARGET TYPE_TARGET_INFO"-"TARGET_NAME
>>> -
>>>   typedef struct TargetInfoQomTarget {
>>>       TargetInfoQom parent;
>>>   } TargetInfoQomTarget;
>>> @@ -47,24 +45,16 @@ typedef struct TargetInfoQomTargetClass {
>>>       TargetInfoQomClass parent_class;
>>>   } TargetInfoQomTargetClass;
>>>   -OBJECT_DECLARE_TYPE(TargetInfoQomTarget, TargetInfoQomTargetClass,
>>> TARGET_INFO_TARGET)
>>> -
>>>   #define
>>> target_info_init(ti_var)                                            \
>>> -static void target_info_qom_class_init(ObjectClass *oc, const void *
>>> data)  \
>>> -
>>> {                                                                           \
>>> -    TargetInfoQomTargetClass *klass =
>>> TARGET_INFO_TARGET_CLASS(oc);         \
>>> -    klass->parent_class.target_info =
>>> &ti_var;                              \
>>> -}                                                                           \
>>> -
>>>                                                                             \
>>>   static const TypeInfo target_info_qom_target_type_info =
>>> {                  \
>>> -    .name =
>>> TYPE_TARGET_INFO_TARGET,                                        \
>>> +    .name = 
>>> TYPE_TARGET_INFO"-"TARGET_NAME,                                \
>>>       .parent =
>>> TYPE_TARGET_INFO,                                             \
>>>       .instance_size =
>>> sizeof(TargetInfoQomTarget),                           \
>>>       .class_size =
>>> sizeof(TargetInfoQomTargetClass),                         \
>>>       .class_init =
>>> target_info_qom_class_init,                               \
>>> +    .class_data =
>>> &ti_var,                                                  \
>>>       .abstract =
>>> false,                                                      \
>>>   };                                                                          \
>>> -
>>>                                                                             \
>>>   DEFINE_TARGET_INFO_TYPE(target_info_qom_target_type_info)
>>>     #endif /* CONFIG_USER_ONLY */
>>> diff --git a/include/qemu/target-info-qom.h b/include/qemu/target-
>>> info-qom.h
>>> index 91be415ed33..a37c3e101e0 100644
>>> --- a/include/qemu/target-info-qom.h
>>> +++ b/include/qemu/target-info-qom.h
>>> @@ -25,6 +25,7 @@ typedef struct TargetInfoQomClass {
>>>     OBJECT_DECLARE_TYPE(TargetInfoQom, TargetInfoQomClass, TARGET_INFO)
>>>   +void target_info_qom_class_init(ObjectClass *oc, const void * data);
>>>   void target_info_qom_set_target(void);
>>>     #endif /* QEMU_TARGET_INFO_QOM_H */
>>> diff --git a/target-info-qom.c b/target-info-qom.c
>>> index cc470b3b4d6..5b0498ca654 100644
>>> --- a/target-info-qom.c
>>> +++ b/target-info-qom.c
>>> @@ -37,6 +37,12 @@ static const TypeInfo target_info_parent_type = {
>>>     DEFINE_TARGET_INFO_TYPE(target_info_parent_type)
>>>   +void target_info_qom_class_init(ObjectClass *oc, const void * data)
>>> +{
>>> +    TargetInfoQomClass *klass = TARGET_INFO_CLASS(oc);
>>> +    klass->target_info = data;
>>> +}
>>> +
>>>   static const TargetInfo *target_info_ptr;
>>>     const TargetInfo *target_info(void)
>>
>> This patch can be squashed with 3, but I thought it might be better for
>> review to keep it on top for now.
> 
> Mmm.  You've missed the part about using class_base_init instead of
> class_init, so that you don't need the extern function declaration.  See
> review of patch 3.
>

I feel that this pattern of using a super ctor with a data set by child
is very counter intuitive, but I'll trust your judgment on how QOM is
supposed to work.

I'll squash this with patch 3 in next version so there is no confusion also.

> 
> r~

Regards,
Pierrick