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 <jdenemar@redhat.com>
This commit is contained in:
Jiri Denemark 2017-09-13 15:23:43 +02:00
parent 440838472d
commit 57f3999d72
3 changed files with 87 additions and 0 deletions

View File

@ -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);
}

View File

@ -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__ */

View File

@ -82,6 +82,8 @@ virCPUDefFree;
virCPUDefFreeFeatures;
virCPUDefFreeModel;
virCPUDefIsEqual;
virCPUDefListFree;
virCPUDefListParse;
virCPUDefParseXML;
virCPUDefStealModel;
virCPUDefUpdateFeature;