diff mbox series

[v3,1/5] qemu/target-info: Factor target_system_arch() out

Message ID 20250502214551.80401-2-philmd@linaro.org
State New
Headers show
Series hw/virtio: Build virtio-mem.c once | expand

Commit Message

Philippe Mathieu-Daudé May 2, 2025, 9:45 p.m. UTC
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/qemu/target-info.h |  9 +++++++++
 hw/core/machine-qmp-cmds.c |  6 ++----
 target-info.c              | 12 ++++++++++++
 3 files changed, 23 insertions(+), 4 deletions(-)

Comments

Pierrick Bouvier May 4, 2025, 9:16 p.m. UTC | #1
On 5/2/25 2:45 PM, Philippe Mathieu-Daudé wrote:
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/qemu/target-info.h |  9 +++++++++
>   hw/core/machine-qmp-cmds.c |  6 ++----
>   target-info.c              | 12 ++++++++++++
>   3 files changed, 23 insertions(+), 4 deletions(-)
> 
> diff --git a/include/qemu/target-info.h b/include/qemu/target-info.h
> index 850a2958b9c..87b4b0e0cbb 100644
> --- a/include/qemu/target-info.h
> +++ b/include/qemu/target-info.h
> @@ -9,6 +9,8 @@
>   #ifndef QEMU_TARGET_INFO_H
>   #define QEMU_TARGET_INFO_H
>   
> +#include "qapi/qapi-types-machine.h"
> +
>   /**
>    * target_name:
>    *
> @@ -16,6 +18,13 @@
>    */
>   const char *target_name(void);
>   
> +/**
> + * target_system_arch:
> + *
> + * Returns: QAPI SysEmuTarget enum (i.e. SYS_EMU_TARGET_I386).
> + */
> +SysEmuTarget target_system_arch(void);
> +
>   /**
>    * target_long_bits:
>    *
> diff --git a/hw/core/machine-qmp-cmds.c b/hw/core/machine-qmp-cmds.c
> index d82043e1c68..8f491dba441 100644
> --- a/hw/core/machine-qmp-cmds.c
> +++ b/hw/core/machine-qmp-cmds.c
> @@ -37,8 +37,7 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
>       MachineState *ms = MACHINE(qdev_get_machine());
>       MachineClass *mc = MACHINE_GET_CLASS(ms);
>       CpuInfoFastList *head = NULL, **tail = &head;
> -    SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, target_name(),
> -                                          -1, &error_abort);
> +    SysEmuTarget target = target_system_arch();
>       CPUState *cpu;
>   
>       CPU_FOREACH(cpu) {
> @@ -139,8 +138,7 @@ QemuTargetInfo *qmp_query_target(Error **errp)
>   {
>       QemuTargetInfo *info = g_malloc0(sizeof(*info));
>   
> -    info->arch = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), -1,
> -                                 &error_abort);
> +    info->arch = target_system_arch();
>   
>       return info;
>   }
> diff --git a/target-info.c b/target-info.c
> index 16fdca7aaaf..8232d488870 100644
> --- a/target-info.c
> +++ b/target-info.c
> @@ -9,6 +9,7 @@
>   #include "qemu/osdep.h"
>   #include "qemu/target-info.h"
>   #include "qemu/target-info-impl.h"
> +#include "qapi/error.h"
>   
>   const char *target_name(void)
>   {
> @@ -20,6 +21,17 @@ unsigned target_long_bits(void)
>       return target_info()->long_bits;
>   }
>   
> +SysEmuTarget target_system_arch(void)
> +{
> +    static SysEmuTarget system_arch = SYS_EMU_TARGET__MAX;
> +

It would be better to squash next commit, and modify 
target_info()->target_arch directly, instead of introducing this static 
variable.

> +    if (system_arch == SYS_EMU_TARGET__MAX) {
> +        system_arch = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), -1,
> +                                      &error_abort);
> +    }
> +    return system_arch;
> +}
> +
>   const char *target_cpu_type(void)
>   {
>       return target_info()->cpu_type;
diff mbox series

Patch

diff --git a/include/qemu/target-info.h b/include/qemu/target-info.h
index 850a2958b9c..87b4b0e0cbb 100644
--- a/include/qemu/target-info.h
+++ b/include/qemu/target-info.h
@@ -9,6 +9,8 @@ 
 #ifndef QEMU_TARGET_INFO_H
 #define QEMU_TARGET_INFO_H
 
+#include "qapi/qapi-types-machine.h"
+
 /**
  * target_name:
  *
@@ -16,6 +18,13 @@ 
  */
 const char *target_name(void);
 
+/**
+ * target_system_arch:
+ *
+ * Returns: QAPI SysEmuTarget enum (i.e. SYS_EMU_TARGET_I386).
+ */
+SysEmuTarget target_system_arch(void);
+
 /**
  * target_long_bits:
  *
diff --git a/hw/core/machine-qmp-cmds.c b/hw/core/machine-qmp-cmds.c
index d82043e1c68..8f491dba441 100644
--- a/hw/core/machine-qmp-cmds.c
+++ b/hw/core/machine-qmp-cmds.c
@@ -37,8 +37,7 @@  CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
     MachineState *ms = MACHINE(qdev_get_machine());
     MachineClass *mc = MACHINE_GET_CLASS(ms);
     CpuInfoFastList *head = NULL, **tail = &head;
-    SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, target_name(),
-                                          -1, &error_abort);
+    SysEmuTarget target = target_system_arch();
     CPUState *cpu;
 
     CPU_FOREACH(cpu) {
@@ -139,8 +138,7 @@  QemuTargetInfo *qmp_query_target(Error **errp)
 {
     QemuTargetInfo *info = g_malloc0(sizeof(*info));
 
-    info->arch = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), -1,
-                                 &error_abort);
+    info->arch = target_system_arch();
 
     return info;
 }
diff --git a/target-info.c b/target-info.c
index 16fdca7aaaf..8232d488870 100644
--- a/target-info.c
+++ b/target-info.c
@@ -9,6 +9,7 @@ 
 #include "qemu/osdep.h"
 #include "qemu/target-info.h"
 #include "qemu/target-info-impl.h"
+#include "qapi/error.h"
 
 const char *target_name(void)
 {
@@ -20,6 +21,17 @@  unsigned target_long_bits(void)
     return target_info()->long_bits;
 }
 
+SysEmuTarget target_system_arch(void)
+{
+    static SysEmuTarget system_arch = SYS_EMU_TARGET__MAX;
+
+    if (system_arch == SYS_EMU_TARGET__MAX) {
+        system_arch = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), -1,
+                                      &error_abort);
+    }
+    return system_arch;
+}
+
 const char *target_cpu_type(void)
 {
     return target_info()->cpu_type;