[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 04/33] cpu: Add helper cpu_model_from_type()
From: |
Gavin Shan |
Subject: |
[PATCH v4 04/33] cpu: Add helper cpu_model_from_type() |
Date: |
Thu, 2 Nov 2023 10:24:31 +1000 |
Add helper cpu_model_from_type() to extract the CPU model name from
the CPU type name in two circumstances: (1) The CPU type name is the
combination of the CPU model name and suffix. (2) The CPU type name
is same to the CPU model name.
The helper will be used in the subsequent commits to conver the
CPU type name to the CPU model name.
Suggested-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Gavin Shan <gshan@redhat.com>
---
cpu-target.c | 16 ++++++++++++++++
include/hw/core/cpu.h | 12 ++++++++++++
2 files changed, 28 insertions(+)
diff --git a/cpu-target.c b/cpu-target.c
index 876b498233..344bad5736 100644
--- a/cpu-target.c
+++ b/cpu-target.c
@@ -265,6 +265,22 @@ ObjectClass *cpu_class_by_name(const char *typename, const
char *cpu_model)
return NULL;
}
+char *cpu_model_from_type(const char *typename)
+{
+ const char *suffix = "-" CPU_RESOLVING_TYPE;
+
+ if (!object_class_by_name(typename)) {
+ return NULL;
+ }
+
+ if (strlen(typename) > strlen(suffix) &&
+ !strcmp(typename + strlen(typename) - strlen(suffix), suffix)) {
+ return g_strndup(typename, strlen(typename) - strlen(suffix));
+ }
+
+ return g_strdup(typename);
+}
+
const char *parse_cpu_option(const char *cpu_option)
{
ObjectClass *oc;
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index ee85aafdf5..8179c55759 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -779,6 +779,18 @@ void cpu_reset(CPUState *cpu);
*/
ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
+/**
+ * cpu_model_from_type:
+ * @typename: The CPU type name
+ *
+ * Extract the CPU model name from the CPU type name. The
+ * CPU type name is either the combination of the CPU model
+ * name and suffix, or same to the CPU model name.
+ *
+ * Returns: CPU model name or NULL if the CPU class doesn't exist
+ */
+char *cpu_model_from_type(const char *typename);
+
/**
* cpu_create:
* @typename: The CPU type.
--
2.41.0
- [PATCH v4 00/33] Unified CPU type check, Gavin Shan, 2023/11/01
- [PATCH v4 01/33] target/alpha: Tidy up alpha_cpu_class_by_name(), Gavin Shan, 2023/11/01
- [PATCH v4 02/33] hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name(), Gavin Shan, 2023/11/01
- [PATCH v4 03/33] cpu: Call object_class_dynamic_cast() once in cpu_class_by_name(), Gavin Shan, 2023/11/01
- [PATCH v4 04/33] cpu: Add helper cpu_model_from_type(),
Gavin Shan <=
- [PATCH v4 05/33] target/alpha: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 06/33] target/arm: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 07/33] target/avr: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 09/33] target/hexagon: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 08/33] target/cris: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 10/33] target/i386: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 11/33] target/loongarch: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 12/33] target/m68k: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 13/33] target/mips: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01
- [PATCH v4 14/33] target/openrisc: Use generic helper to show CPU model names, Gavin Shan, 2023/11/01