From d6a6ed94f2ef945c21924acd17368fbbf8032d6d Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Wed, 26 May 2021 15:39:38 +0200 Subject: [PATCH] capabilities: Separate formatting into a function The way we format element for capabilities is not ideal, because if there are no CPUs, i.e. no child elements, we still output opening and closing element. To solve this, virXMLFormatElement() could be used but that would introduce more variables into the loop. Therefore, move the formatter into a separate function and use virXMLFormatElement(). Signed-off-by: Michal Privoznik Reviewed-by: Martin Kletzander --- src/conf/capabilities.c | 58 ++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index d3b22f7dd0..c825cb0503 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -802,6 +802,41 @@ virCapabilitiesAddStoragePool(virCaps *caps, } +static int +virCapsHostNUMACellCPUFormat(virBuffer *buf, + const virCapsHostNUMACellCPU *cpus, + int ncpus) +{ + g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf); + size_t j; + + virBufferAsprintf(&attrBuf, " num='%d'", ncpus); + + for (j = 0; j < ncpus; j++) { + virBufferAsprintf(&childBuf, "\n"); + } + + virXMLFormatElement(buf, "cpus", &attrBuf, &childBuf); + return 0; +} + + static int virCapabilitiesHostNUMAFormat(virBuffer *buf, virCapsHostNUMA *caps) @@ -835,28 +870,9 @@ virCapabilitiesHostNUMAFormat(virBuffer *buf, virNumaDistanceFormat(buf, cell->distances, cell->ndistances); - virBufferAsprintf(buf, "\n", cell->ncpus); - virBufferAdjustIndent(buf, 2); - for (j = 0; j < cell->ncpus; j++) { - virBufferAsprintf(buf, "cpus[j].id); - - if (cell->cpus[j].siblings) { - g_autofree char *siblings = NULL; - - if (!(siblings = virBitmapFormat(cell->cpus[j].siblings))) - return -1; + if (virCapsHostNUMACellCPUFormat(buf, cell->cpus, cell->ncpus) < 0) + return -1; - virBufferAsprintf(buf, - " socket_id='%d' die_id='%d' core_id='%d' siblings='%s'", - cell->cpus[j].socket_id, - cell->cpus[j].die_id, - cell->cpus[j].core_id, - siblings); - } - virBufferAddLit(buf, "/>\n"); - } - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "\n"); } -- 2.47.2