diff mbox series

[RFC,1/3] cpu: Add new API cpu_type_by_name

Message ID 20230825121651.1534-2-zhiwei_liu@linux.alibaba.com
State New
Headers show
Series Add API for list cpu extensions | expand

Commit Message

LIU Zhiwei Aug. 25, 2023, 12:16 p.m. UTC
cpu_type_by_name is used to get the cpu type name from the command
line -cpu.

Currently it is only used by parse_cpu_option. In the next patch, it
will be used by other cpu query functions.

Signed-off-by: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
---
 cpu.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

Comments

Philippe Mathieu-Daudé Aug. 28, 2023, 12:25 p.m. UTC | #1
On 25/8/23 14:16, LIU Zhiwei wrote:
> cpu_type_by_name is used to get the cpu type name from the command
> line -cpu.
> 
> Currently it is only used by parse_cpu_option. In the next patch, it
> will be used by other cpu query functions.
> 
> Signed-off-by: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
> ---
>   cpu.c | 31 +++++++++++++++++++------------
>   1 file changed, 19 insertions(+), 12 deletions(-)

Alternative patch subject:
"cpu: Extract cpu_type_by_name() from parse_cpu_option()"

Otherwise,

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
diff mbox series

Patch

diff --git a/cpu.c b/cpu.c
index 1c948d1161..e1a9239d0f 100644
--- a/cpu.c
+++ b/cpu.c
@@ -257,28 +257,35 @@  void cpu_exec_initfn(CPUState *cpu)
 #endif
 }
 
-const char *parse_cpu_option(const char *cpu_option)
+static const char *cpu_type_by_name(const char *cpu_model)
 {
     ObjectClass *oc;
-    CPUClass *cc;
-    gchar **model_pieces;
     const char *cpu_type;
 
-    model_pieces = g_strsplit(cpu_option, ",", 2);
-    if (!model_pieces[0]) {
-        error_report("-cpu option cannot be empty");
-        exit(1);
-    }
 
-    oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
+    oc = cpu_class_by_name(CPU_RESOLVING_TYPE, cpu_model);
     if (oc == NULL) {
-        error_report("unable to find CPU model '%s'", model_pieces[0]);
-        g_strfreev(model_pieces);
+        error_report("unable to find CPU model '%s'", cpu_model);
         exit(EXIT_FAILURE);
     }
 
     cpu_type = object_class_get_name(oc);
-    cc = CPU_CLASS(oc);
+    return cpu_type;
+}
+
+const char *parse_cpu_option(const char *cpu_option)
+{
+    const char *cpu_type;
+    CPUClass *cc;
+    gchar **model_pieces;
+
+    model_pieces = g_strsplit(cpu_option, ",", 2);
+    if (!model_pieces[0]) {
+        error_report("-cpu option cannot be empty");
+        exit(1);
+    }
+    cpu_type = cpu_type_by_name(model_pieces[0]);
+    cc = CPU_CLASS(object_class_by_name(cpu_type));
     cc->parse_features(cpu_type, model_pieces[1], &error_fatal);
     g_strfreev(model_pieces);
     return cpu_type;