7a3408
From d7efd0615b63be6fad7c0833f988430e79ced194 Mon Sep 17 00:00:00 2001
7a3408
Message-Id: <d7efd0615b63be6fad7c0833f988430e79ced194@dist-git>
7a3408
From: Andrea Bolognani <abologna@redhat.com>
7a3408
Date: Tue, 11 Aug 2015 17:16:04 +0200
7a3408
Subject: [PATCH] cpu: Align ppc64 CPU data with x86
7a3408
7a3408
Use a typedef instead of the plain struct and heap allocation. This
7a3408
will make it easier to extend the ppc64 specific CPU data later on.
7a3408
7a3408
(cherry picked from commit adb865df85c525f0cffa995b8fd4e7f897c8f35c)
7a3408
7a3408
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1250977
7a3408
7a3408
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
7a3408
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
7a3408
---
7a3408
 src/cpu/cpu.h            |  2 +-
7a3408
 src/cpu/cpu_ppc64.c      | 86 ++++++++++++++++++++++++++++++++++++------------
7a3408
 src/cpu/cpu_ppc64_data.h |  3 +-
7a3408
 3 files changed, 68 insertions(+), 23 deletions(-)
7a3408
7a3408
diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h
7a3408
index 49d4226..7375876 100644
7a3408
--- a/src/cpu/cpu.h
7a3408
+++ b/src/cpu/cpu.h
7a3408
@@ -38,7 +38,7 @@ struct _virCPUData {
7a3408
     virArch arch;
7a3408
     union {
7a3408
         virCPUx86Data *x86;
7a3408
-        struct cpuPPC64Data ppc64;
7a3408
+        virCPUppc64Data *ppc64;
7a3408
         /* generic driver needs no data */
7a3408
     } data;
7a3408
 };
7a3408
diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c
7a3408
index efac739..6c78ab8 100644
7a3408
--- a/src/cpu/cpu_ppc64.c
7a3408
+++ b/src/cpu/cpu_ppc64.c
7a3408
@@ -48,7 +48,7 @@ struct ppc64_vendor {
7a3408
 struct ppc64_model {
7a3408
     char *name;
7a3408
     const struct ppc64_vendor *vendor;
7a3408
-    struct cpuPPC64Data data;
7a3408
+    virCPUppc64Data *data;
7a3408
     struct ppc64_model *next;
7a3408
 };
7a3408
 
7a3408
@@ -58,6 +58,25 @@ struct ppc64_map {
7a3408
 };
7a3408
 
7a3408
 static void
7a3408
+ppc64DataFree(virCPUppc64Data *data)
7a3408
+{
7a3408
+    VIR_FREE(data);
7a3408
+}
7a3408
+
7a3408
+static virCPUppc64Data *
7a3408
+ppc64DataCopy(const virCPUppc64Data *data)
7a3408
+{
7a3408
+    virCPUppc64Data *copy;
7a3408
+
7a3408
+    if (VIR_ALLOC(copy) < 0)
7a3408
+        return NULL;
7a3408
+
7a3408
+    copy->pvr = data->pvr;
7a3408
+
7a3408
+    return copy;
7a3408
+}
7a3408
+
7a3408
+static void
7a3408
 ppc64VendorFree(struct ppc64_vendor *vendor)
7a3408
 {
7a3408
     if (!vendor)
7a3408
@@ -90,6 +109,7 @@ ppc64ModelFree(struct ppc64_model *model)
7a3408
     if (!model)
7a3408
         return;
7a3408
 
7a3408
+    ppc64DataFree(model->data);
7a3408
     VIR_FREE(model->name);
7a3408
     VIR_FREE(model);
7a3408
 }
7a3408
@@ -99,16 +119,22 @@ ppc64ModelCopy(const struct ppc64_model *model)
7a3408
 {
7a3408
     struct ppc64_model *copy;
7a3408
 
7a3408
-    if (VIR_ALLOC(copy) < 0 ||
7a3408
-        VIR_STRDUP(copy->name, model->name) < 0) {
7a3408
-        ppc64ModelFree(copy);
7a3408
-        return NULL;
7a3408
-    }
7a3408
+    if (VIR_ALLOC(copy) < 0)
7a3408
+        goto error;
7a3408
+
7a3408
+    if (VIR_STRDUP(copy->name, model->name) < 0)
7a3408
+        goto error;
7a3408
+
7a3408
+    if (!(copy->data = ppc64DataCopy(model->data)))
7a3408
+        goto error;
7a3408
 
7a3408
-    copy->data.pvr = model->data.pvr;
7a3408
     copy->vendor = model->vendor;
7a3408
 
7a3408
     return copy;
7a3408
+
7a3408
+ error:
7a3408
+    ppc64ModelFree(copy);
7a3408
+    return NULL;
7a3408
 }
7a3408
 
7a3408
 static struct ppc64_model *
7a3408
@@ -136,7 +162,7 @@ ppc64ModelFindPVR(const struct ppc64_map *map,
7a3408
 
7a3408
     model = map->models;
7a3408
     while (model) {
7a3408
-        if (model->data.pvr == pvr)
7a3408
+        if (model->data->pvr == pvr)
7a3408
             return model;
7a3408
 
7a3408
         model = model->next;
7a3408
@@ -237,6 +263,11 @@ ppc64ModelLoad(xmlXPathContextPtr ctxt,
7a3408
     if (VIR_ALLOC(model) < 0)
7a3408
         return -1;
7a3408
 
7a3408
+    if (VIR_ALLOC(model->data) < 0) {
7a3408
+        ppc64ModelFree(model);
7a3408
+        return -1;
7a3408
+    }
7a3408
+
7a3408
     model->name = virXPathString("string(@name)", ctxt);
7a3408
     if (!model->name) {
7a3408
         virReportError(VIR_ERR_INTERNAL_ERROR,
7a3408
@@ -274,7 +305,7 @@ ppc64ModelLoad(xmlXPathContextPtr ctxt,
7a3408
                        model->name);
7a3408
         goto ignore;
7a3408
     }
7a3408
-    model->data.pvr = pvr;
7a3408
+    model->data->pvr = pvr;
7a3408
 
7a3408
     if (!map->models) {
7a3408
         map->models = model;
7a3408
@@ -318,7 +349,7 @@ ppc64LoadMap(void)
7a3408
     struct ppc64_map *map;
7a3408
 
7a3408
     if (VIR_ALLOC(map) < 0)
7a3408
-        return NULL;
7a3408
+        goto error;
7a3408
 
7a3408
     if (cpuMapLoad("ppc64", ppc64MapLoadCallback, map) < 0)
7a3408
         goto error;
7a3408
@@ -332,7 +363,7 @@ ppc64LoadMap(void)
7a3408
 
7a3408
 static virCPUDataPtr
7a3408
 ppc64MakeCPUData(virArch arch,
7a3408
-                 struct cpuPPC64Data *data)
7a3408
+                 virCPUppc64Data *data)
7a3408
 {
7a3408
     virCPUDataPtr cpuData;
7a3408
 
7a3408
@@ -340,7 +371,9 @@ ppc64MakeCPUData(virArch arch,
7a3408
         return NULL;
7a3408
 
7a3408
     cpuData->arch = arch;
7a3408
-    cpuData->data.ppc64 = *data;
7a3408
+
7a3408
+    if (!(cpuData->data.ppc64 = ppc64DataCopy(data)))
7a3408
+        VIR_FREE(cpuData);
7a3408
 
7a3408
     return cpuData;
7a3408
 }
7a3408
@@ -421,7 +454,7 @@ ppc64Compute(virCPUDefPtr host,
7a3408
     }
7a3408
 
7a3408
     if (guestData)
7a3408
-        if (!(*guestData = ppc64MakeCPUData(arch, &guest_model->data)))
7a3408
+        if (!(*guestData = ppc64MakeCPUData(arch, guest_model->data)))
7a3408
             goto cleanup;
7a3408
 
7a3408
     ret = VIR_CPU_COMPARE_IDENTICAL;
7a3408
@@ -473,10 +506,10 @@ ppc64DriverDecode(virCPUDefPtr cpu,
7a3408
     if (!data || !(map = ppc64LoadMap()))
7a3408
         return -1;
7a3408
 
7a3408
-    if (!(model = ppc64ModelFindPVR(map, data->data.ppc64.pvr))) {
7a3408
+    if (!(model = ppc64ModelFindPVR(map, data->data.ppc64->pvr))) {
7a3408
         virReportError(VIR_ERR_OPERATION_FAILED,
7a3408
                        _("Cannot find CPU model with PVR 0x%08x"),
7a3408
-                       data->data.ppc64.pvr);
7a3408
+                       data->data.ppc64->pvr);
7a3408
         goto cleanup;
7a3408
     }
7a3408
 
7a3408
@@ -506,25 +539,36 @@ ppc64DriverFree(virCPUDataPtr data)
7a3408
     if (!data)
7a3408
         return;
7a3408
 
7a3408
+    ppc64DataFree(data->data.ppc64);
7a3408
     VIR_FREE(data);
7a3408
 }
7a3408
 
7a3408
 static virCPUDataPtr
7a3408
 ppc64DriverNodeData(virArch arch)
7a3408
 {
7a3408
-    virCPUDataPtr cpuData;
7a3408
+    virCPUDataPtr nodeData;
7a3408
+    virCPUppc64Data *data;
7a3408
 
7a3408
-    if (VIR_ALLOC(cpuData) < 0)
7a3408
-        return NULL;
7a3408
+    if (VIR_ALLOC(nodeData) < 0)
7a3408
+        goto error;
7a3408
 
7a3408
-    cpuData->arch = arch;
7a3408
+    if (VIR_ALLOC(data) < 0)
7a3408
+        goto error;
7a3408
+
7a3408
+    data = nodeData->data.ppc64;
7a3408
 
7a3408
 #if defined(__powerpc__) || defined(__powerpc64__)
7a3408
     asm("mfpvr %0"
7a3408
-        : "=r" (cpuData->data.ppc64.pvr));
7a3408
+        : "=r" (data->pvr));
7a3408
 #endif
7a3408
 
7a3408
-    return cpuData;
7a3408
+    nodeData->arch = arch;
7a3408
+
7a3408
+    return nodeData;
7a3408
+
7a3408
+ error:
7a3408
+    ppc64DriverFree(nodeData);
7a3408
+    return NULL;
7a3408
 }
7a3408
 
7a3408
 static virCPUCompareResult
7a3408
diff --git a/src/cpu/cpu_ppc64_data.h b/src/cpu/cpu_ppc64_data.h
7a3408
index 45152de..c18fc63 100644
7a3408
--- a/src/cpu/cpu_ppc64_data.h
7a3408
+++ b/src/cpu/cpu_ppc64_data.h
7a3408
@@ -26,7 +26,8 @@
7a3408
 
7a3408
 # include <stdint.h>
7a3408
 
7a3408
-struct cpuPPC64Data {
7a3408
+typedef struct _virCPUppc64Data virCPUppc64Data;
7a3408
+struct _virCPUppc64Data {
7a3408
     uint32_t pvr;
7a3408
 };
7a3408
 
7a3408
-- 
7a3408
2.5.0
7a3408