Skip to content

Commit

Permalink
cpu: Add helper cpu_model_from_type()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Gavin Shan <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Reviewed-by: Richard Henderson <[email protected]>
Message-ID: <[email protected]>
[PMD: Mention returned string must be released with g_free()]
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
  • Loading branch information
Gavin Shan authored and philmd committed Jan 5, 2024
1 parent d5be19f commit 445946f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cpu-target.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions include/hw/core/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 445946f

Please sign in to comment.