[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 05/71] cpu: Add helper cpu_model_from_type()
From: |
Philippe Mathieu-Daudé |
Subject: |
[PULL 05/71] cpu: Add helper cpu_model_from_type() |
Date: |
Fri, 5 Jan 2024 16:41:58 +0100 |
From: Gavin Shan <gshan@redhat.com>
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>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20231114235628.534334-6-gshan@redhat.com>
[PMD: Mention returned string must be released with g_free()]
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
include/hw/core/cpu.h | 13 +++++++++++++
cpu-target.c | 15 +++++++++++++++
2 files changed, 28 insertions(+)
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index c0c8320413..76ef59de0a 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -779,6 +779,19 @@ 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
+ * The user should g_free() the string once no longer needed.
+ */
+char *cpu_model_from_type(const char *typename);
+
/**
* cpu_create:
* @typename: The CPU type.
diff --git a/cpu-target.c b/cpu-target.c
index 430dc53566..6f4afc1dbc 100644
--- a/cpu-target.c
+++ b/cpu-target.c
@@ -241,6 +241,21 @@ void cpu_exec_initfn(CPUState *cpu)
#endif
}
+char *cpu_model_from_type(const char *typename)
+{
+ const char *suffix = "-" CPU_RESOLVING_TYPE;
+
+ if (!object_class_by_name(typename)) {
+ return NULL;
+ }
+
+ if (g_str_has_suffix(typename, suffix)) {
+ return g_strndup(typename, strlen(typename) - strlen(suffix));
+ }
+
+ return g_strdup(typename);
+}
+
const char *parse_cpu_option(const char *cpu_option)
{
ObjectClass *oc;
--
2.41.0
- [PULL 00/71] HW core patches for 2024-01-05, Philippe Mathieu-Daudé, 2024/01/05
- [PULL 01/71] meson: Allow building binary with no target-specific files in hw/, Philippe Mathieu-Daudé, 2024/01/05
- [PULL 02/71] target/alpha: Remove fallback to ev67 cpu class, Philippe Mathieu-Daudé, 2024/01/05
- [PULL 03/71] target/hppa: Remove object_class_is_abstract(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 04/71] cpu: Call object_class_dynamic_cast() once in cpu_class_by_name(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 05/71] cpu: Add helper cpu_model_from_type(),
Philippe Mathieu-Daudé <=
- [PULL 06/71] cpu: Add generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 07/71] target/alpha: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 08/71] target/arm: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 09/71] target/avr: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 10/71] target/cris: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 11/71] target/hexagon: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 12/71] target/hppa: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 13/71] target/loongarch: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 14/71] target/m68k: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05
- [PULL 15/71] target/mips: Use generic cpu_list(), Philippe Mathieu-Daudé, 2024/01/05