From 57f3999d72cb8d4ac9fbc950e396020563c92ff5 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Wed, 13 Sep 2017 15:23:43 +0200 Subject: [PATCH] cpu_conf: Introduce virCPUDefList{Parse,Free} For parsing a list of CPU XMLs into a NULL-terminated list of CPU defs. Signed-off-by: Jiri Denemark --- 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..0bd56c9d28 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 == 0) { + 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 e4fa771d9d..528bc9496b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -82,6 +82,8 @@ virCPUDefFree; virCPUDefFreeFeatures; virCPUDefFreeModel; virCPUDefIsEqual; +virCPUDefListFree; +virCPUDefListParse; virCPUDefParseXML; virCPUDefStealModel; virCPUDefUpdateFeature;