[12/17] target-i386: cpu_x86_find_by_name(): split CPU model and feature string first

Submitted by Eduardo Habkost on Nov. 12, 2012, 9:38 p.m.

Details

Message ID 1352756342-13716-13-git-send-email-ehabkost@redhat.com
State New
Headers show

Commit Message

Eduardo Habkost Nov. 12, 2012, 9:38 p.m.
Instead of using strtok() for the whole cpu_model string, first split it
into the CPU model name and the full feature string, then parse the
feature string into pieces.

When using CPU model classes, those two pieces of information will be
used at different moments (CPU model name will be used to find CPU
class, feature string will be used after CPU object was created), so
making the split in two steps will make it easier to refactor the code
later.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 target-i386/cpu.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

Patch hide | download patch | download mbox

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 214a292..3a85989 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1151,9 +1151,10 @@  static int cpu_x86_find_by_name(X86CPUDefinition *x86_cpu_def,
 {
     unsigned int i;
     X86CPUDefinition *def;
-
-    char *s = g_strdup(cpu_model);
-    char *featurestr, *name = strtok(s, ",");
+    char *name; /* CPU model name */
+    char *features; /* Full feature "key=value,..." string */
+    char *featurestr; /* Single 'key=value" string being parsed */
+    gchar **model_pieces; /* array after split of name,features */
     /* Features to be added*/
     uint32_t plus_features = 0, plus_ext_features = 0;
     uint32_t plus_ext2_features = 0, plus_ext3_features = 0;
@@ -1166,6 +1167,14 @@  static int cpu_x86_find_by_name(X86CPUDefinition *x86_cpu_def,
     uint32_t minus_7_0_ebx_features = 0;
     uint32_t numvalue;
 
+    model_pieces = g_strsplit(cpu_model, ",", 2);
+    if (!model_pieces[0]) {
+        goto error;
+    }
+
+    name = model_pieces[0];
+    features = model_pieces[1];
+
     for (def = x86_defs; def; def = def->next)
         if (name && !strcmp(name, def->name))
             break;
@@ -1181,7 +1190,7 @@  static int cpu_x86_find_by_name(X86CPUDefinition *x86_cpu_def,
             &plus_ext_features, &plus_ext2_features, &plus_ext3_features,
             &plus_kvm_features, &plus_svm_features,  &plus_7_0_ebx_features);
 
-    featurestr = strtok(NULL, ",");
+    featurestr = features ? strtok(features, ",") : NULL;
 
     while (featurestr) {
         char *val;
@@ -1315,11 +1324,11 @@  static int cpu_x86_find_by_name(X86CPUDefinition *x86_cpu_def,
     if (x86_cpu_def->cpuid_7_0_ebx_features && x86_cpu_def->level < 7) {
         x86_cpu_def->level = 7;
     }
-    g_free(s);
+    g_strfreev(model_pieces);
     return 0;
 
 error:
-    g_free(s);
+    g_strfreev(model_pieces);
     if (!error_is_set(errp)) {
         error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
     }