[PATCH 01/13] hw/boards: Extend DEFINE_MACHINE macro to cover more use cases

BALATON Zoltan posted 13 patches 4 months ago
Maintainers: BALATON Zoltan <balaton@eik.bme.hu>, Alexey Kardashevskiy <aik@ozlabs.ru>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>
There is a newer version of this series
[PATCH 01/13] hw/boards: Extend DEFINE_MACHINE macro to cover more use cases
Posted by BALATON Zoltan 4 months ago
Add a more general DEFINE_MACHINE_EXTENDED macro and define simpler
versions with less parameters based on that. This is inspired by how
the OBJECT_DEFINE macros do this in a similar way to allow using the
shortened definition in more complex cases too.

Signed-off-by: BALATON Zoltan <balaton@eik.bme.hu>
---
 include/hw/boards.h | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/include/hw/boards.h b/include/hw/boards.h
index 765dc8dd35..6e52d4d10c 100644
--- a/include/hw/boards.h
+++ b/include/hw/boards.h
@@ -744,7 +744,8 @@ struct MachineState {
         } \
     } while (0)
 
-#define DEFINE_MACHINE(namestr, machine_initfn) \
+#define DEFINE_MACHINE_EXTENDED(namestr, PARENT_NAME, InstanceName, \
+                                machine_initfn, ABSTRACT, ...) \
     static void machine_initfn##_class_init(ObjectClass *oc, const void *data) \
     { \
         MachineClass *mc = MACHINE_CLASS(oc); \
@@ -752,8 +753,11 @@ struct MachineState {
     } \
     static const TypeInfo machine_initfn##_typeinfo = { \
         .name       = MACHINE_TYPE_NAME(namestr), \
-        .parent     = TYPE_MACHINE, \
+        .parent     = TYPE_##PARENT_NAME, \
         .class_init = machine_initfn##_class_init, \
+        .instance_size = sizeof(InstanceName), \
+        .abstract = ABSTRACT, \
+        .interfaces = (const InterfaceInfo[]) { __VA_ARGS__ } , \
     }; \
     static void machine_initfn##_register_types(void) \
     { \
@@ -761,6 +765,14 @@ struct MachineState {
     } \
     type_init(machine_initfn##_register_types)
 
+#define DEFINE_MACHINE(namestr, machine_initfn) \
+    DEFINE_MACHINE_EXTENDED(namestr, MACHINE, MachineState, machine_initfn, \
+                            false, { })
+
+#define DEFINE_MACHINE_WITH_INTERFACES(namestr, machine_initfn, ...) \
+    DEFINE_MACHINE_EXTENDED(namestr, MACHINE, MachineState, machine_initfn, \
+                            false, __VA_ARGS__)
+
 extern GlobalProperty hw_compat_10_0[];
 extern const size_t hw_compat_10_0_len;
 
-- 
2.41.3
Re: [PATCH 01/13] hw/boards: Extend DEFINE_MACHINE macro to cover more use cases
Posted by Philippe Mathieu-Daudé 2 months, 3 weeks ago
Hi Zoltan,

On 2/5/25 01:20, BALATON Zoltan wrote:
> Add a more general DEFINE_MACHINE_EXTENDED macro and define simpler
> versions with less parameters based on that. This is inspired by how
> the OBJECT_DEFINE macros do this in a similar way to allow using the
> shortened definition in more complex cases too.
> 
> Signed-off-by: BALATON Zoltan <balaton@eik.bme.hu>
> ---
>   include/hw/boards.h | 16 ++++++++++++++--
>   1 file changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/include/hw/boards.h b/include/hw/boards.h
> index 765dc8dd35..6e52d4d10c 100644
> --- a/include/hw/boards.h
> +++ b/include/hw/boards.h
> @@ -744,7 +744,8 @@ struct MachineState {
>           } \
>       } while (0)
>   
> -#define DEFINE_MACHINE(namestr, machine_initfn) \
> +#define DEFINE_MACHINE_EXTENDED(namestr, PARENT_NAME, InstanceName, \
> +                                machine_initfn, ABSTRACT, ...) \
>       static void machine_initfn##_class_init(ObjectClass *oc, const void *data) \
>       { \
>           MachineClass *mc = MACHINE_CLASS(oc); \
> @@ -752,8 +753,11 @@ struct MachineState {
>       } \
>       static const TypeInfo machine_initfn##_typeinfo = { \
>           .name       = MACHINE_TYPE_NAME(namestr), \
> -        .parent     = TYPE_MACHINE, \
> +        .parent     = TYPE_##PARENT_NAME, \

As it doesn't save much, lets simply pass the full PARENT_TYPE,
not PARENT_NAME. But, do we really need it?

>           .class_init = machine_initfn##_class_init, \
> +        .instance_size = sizeof(InstanceName), \
> +        .abstract = ABSTRACT, \
> +        .interfaces = (const InterfaceInfo[]) { __VA_ARGS__ } , \
>       }; \
>       static void machine_initfn##_register_types(void) \
>       { \
> @@ -761,6 +765,14 @@ struct MachineState {
>       } \
>       type_init(machine_initfn##_register_types)
>   
> +#define DEFINE_MACHINE(namestr, machine_initfn) \
> +    DEFINE_MACHINE_EXTENDED(namestr, MACHINE, MachineState, machine_initfn, \
> +                            false, { })
> +
> +#define DEFINE_MACHINE_WITH_INTERFACES(namestr, machine_initfn, ...) \
> +    DEFINE_MACHINE_EXTENDED(namestr, MACHINE, MachineState, machine_initfn, \
> +                            false, __VA_ARGS__)
> +
>   extern GlobalProperty hw_compat_10_0[];
>   extern const size_t hw_compat_10_0_len;
>
Re: [PATCH 01/13] hw/boards: Extend DEFINE_MACHINE macro to cover more use cases
Posted by BALATON Zoltan 2 months, 3 weeks ago
On Tue, 10 Jun 2025, Philippe Mathieu-Daudé wrote:
> Hi Zoltan,
>
> On 2/5/25 01:20, BALATON Zoltan wrote:
>> Add a more general DEFINE_MACHINE_EXTENDED macro and define simpler
>> versions with less parameters based on that. This is inspired by how
>> the OBJECT_DEFINE macros do this in a similar way to allow using the
>> shortened definition in more complex cases too.
>> 
>> Signed-off-by: BALATON Zoltan <balaton@eik.bme.hu>
>> ---
>>   include/hw/boards.h | 16 ++++++++++++++--
>>   1 file changed, 14 insertions(+), 2 deletions(-)
>> 
>> diff --git a/include/hw/boards.h b/include/hw/boards.h
>> index 765dc8dd35..6e52d4d10c 100644
>> --- a/include/hw/boards.h
>> +++ b/include/hw/boards.h
>> @@ -744,7 +744,8 @@ struct MachineState {
>>           } \
>>       } while (0)
>>   -#define DEFINE_MACHINE(namestr, machine_initfn) \
>> +#define DEFINE_MACHINE_EXTENDED(namestr, PARENT_NAME, InstanceName, \
>> +                                machine_initfn, ABSTRACT, ...) \
>>       static void machine_initfn##_class_init(ObjectClass *oc, const void 
>> *data) \
>>       { \
>>           MachineClass *mc = MACHINE_CLASS(oc); \
>> @@ -752,8 +753,11 @@ struct MachineState {
>>       } \
>>       static const TypeInfo machine_initfn##_typeinfo = { \
>>           .name       = MACHINE_TYPE_NAME(namestr), \
>> -        .parent     = TYPE_MACHINE, \
>> +        .parent     = TYPE_##PARENT_NAME, \
>
> As it doesn't save much, lets simply pass the full PARENT_TYPE,
> not PARENT_NAME. But, do we really need it?

I think this is to keep it similar to corresponding OBJECT_* macros so I'd 
keep it the same.

Regards,
BALATON Zoltan