[libvirt] [PATCH 1/7] cpu_conf: Introduce virCPUDefList{Parse, Free}

Jiri Denemark jdenemar at redhat.com
Thu Sep 14 10:57:14 UTC 2017


For parsing a list of CPU XMLs into a NULL-terminated list of CPU defs.

Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
---
 src/conf/cpu_conf.c      | 78 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/conf/cpu_conf.h      |  7 +++++
 src/libvirt_private.syms |  2 ++
 3 files changed, 87 insertions(+)

diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index c21d11d244..7514842059 100644
--- a/src/conf/cpu_conf.c
+++ b/src/conf/cpu_conf.c
@@ -29,9 +29,12 @@
 #include "cpu_conf.h"
 #include "domain_conf.h"
 #include "virstring.h"
+#include "virlog.h"
 
 #define VIR_FROM_THIS VIR_FROM_CPU
 
+VIR_LOG_INIT("conf.cpu_conf");
+
 VIR_ENUM_IMPL(virCPU, VIR_CPU_TYPE_LAST,
               "host", "guest", "auto")
 
@@ -939,3 +942,78 @@ virCPUDefIsEqual(virCPUDefPtr src,
  cleanup:
     return identical;
 }
+
+
+/*
+ * Parses a list of CPU XMLs into a NULL-terminated list of CPU defs.
+ */
+virCPUDefPtr *
+virCPUDefListParse(const char **xmlCPUs,
+                   unsigned int ncpus,
+                   virCPUType cpuType)
+{
+    xmlDocPtr doc = NULL;
+    xmlXPathContextPtr ctxt = NULL;
+    virCPUDefPtr *cpus = NULL;
+    size_t i;
+
+    VIR_DEBUG("xmlCPUs=%p, ncpus=%u", xmlCPUs, ncpus);
+
+    if (xmlCPUs) {
+        for (i = 0; i < ncpus; i++)
+            VIR_DEBUG("xmlCPUs[%zu]=%s", i, NULLSTR(xmlCPUs[i]));
+    }
+
+    if (!xmlCPUs && ncpus != 0) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("nonzero ncpus doesn't match with NULL xmlCPUs"));
+        goto error;
+    }
+
+    if (ncpus < 1) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s", _("no CPUs given"));
+        goto error;
+    }
+
+    if (VIR_ALLOC_N(cpus, ncpus + 1))
+        goto error;
+
+    for (i = 0; i < ncpus; i++) {
+        if (!(doc = virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition)"), &ctxt)))
+            goto error;
+
+        if (virCPUDefParseXML(ctxt, NULL, cpuType, &cpus[i]) < 0)
+            goto error;
+
+        xmlXPathFreeContext(ctxt);
+        xmlFreeDoc(doc);
+        ctxt = NULL;
+        doc = NULL;
+    }
+
+    return cpus;
+
+ error:
+    virCPUDefListFree(cpus);
+    xmlXPathFreeContext(ctxt);
+    xmlFreeDoc(doc);
+    return NULL;
+}
+
+
+/*
+ * Frees NULL-terminated list of CPUs created by virCPUDefListParse.
+ */
+void
+virCPUDefListFree(virCPUDefPtr *cpus)
+{
+    virCPUDefPtr *cpu;
+
+    if (!cpus)
+        return;
+
+    for (cpu = cpus; *cpu != NULL; cpu++)
+        virCPUDefFree(*cpu);
+
+    VIR_FREE(cpus);
+}
diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
index b44974f47e..d3e2c84102 100644
--- a/src/conf/cpu_conf.h
+++ b/src/conf/cpu_conf.h
@@ -218,4 +218,11 @@ virCPUDefUpdateFeature(virCPUDefPtr cpu,
                        const char *name,
                        int policy);
 
+virCPUDefPtr *
+virCPUDefListParse(const char **xmlCPUs,
+                   unsigned int ncpus,
+                   virCPUType cpuType);
+void
+virCPUDefListFree(virCPUDefPtr *cpus);
+
 #endif /* __VIR_CPU_CONF_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 247d1175ba..857e417f94 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -82,6 +82,8 @@ virCPUDefFree;
 virCPUDefFreeFeatures;
 virCPUDefFreeModel;
 virCPUDefIsEqual;
+virCPUDefListFree;
+virCPUDefListParse;
 virCPUDefParseXML;
 virCPUDefStealModel;
 virCPUDefUpdateFeature;
-- 
2.14.1




More information about the libvir-list mailing list