]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: capabilities: Modernize virCapabilitiesFormatMemoryBandwidth
authorPeter Krempa <pkrempa@redhat.com>
Fri, 25 Oct 2019 14:26:35 +0000 (16:26 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 12 Nov 2019 13:37:59 +0000 (14:37 +0100)
Use virXMLFormatElement and the automatic memory handlers to simplfy the
code.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/conf/capabilities.c

index 1ab389bc65ac7ba6600f1085e7f762e7c620cbda..f3b34b70405e9e1e111d9c6ef8ef29d47df0f0e4 100644 (file)
@@ -1024,7 +1024,6 @@ virCapabilitiesFormatMemoryBandwidth(virBufferPtr buf,
                                      virCapsHostMemBWPtr memBW)
 {
     size_t i = 0;
-    virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
 
     if (!memBW->nnodes)
         return 0;
@@ -1033,17 +1032,18 @@ virCapabilitiesFormatMemoryBandwidth(virBufferPtr buf,
     virBufferAdjustIndent(buf, 2);
 
     for (i = 0; i < memBW->nnodes; i++) {
+        g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
+        g_auto(virBuffer) childrenBuf = VIR_BUFFER_INITIALIZER;
         virCapsHostMemBWNodePtr node = memBW->nodes[i];
         virResctrlInfoMemBWPerNodePtr control = &node->control;
-        char *cpus_str = virBitmapFormat(node->cpus);
+        g_autofree char *cpus_str = virBitmapFormat(node->cpus);
 
         if (!cpus_str)
             return -1;
 
-        virBufferAsprintf(buf,
-                          "<node id='%u' cpus='%s'",
+        virBufferAsprintf(&attrBuf,
+                          " id='%u' cpus='%s'",
                           node->id, cpus_str);
-        VIR_FREE(cpus_str);
 
         virBufferSetChildIndent(&childrenBuf, buf);
         virBufferAsprintf(&childrenBuf,
@@ -1052,13 +1052,7 @@ virCapabilitiesFormatMemoryBandwidth(virBufferPtr buf,
                           control->granularity, control->min,
                           control->max_allocation);
 
-        if (virBufferUse(&childrenBuf)) {
-            virBufferAddLit(buf, ">\n");
-            virBufferAddBuffer(buf, &childrenBuf);
-            virBufferAddLit(buf, "</node>\n");
-        } else {
-            virBufferAddLit(buf, "/>\n");
-        }
+        virXMLFormatElement(buf, "node", &attrBuf, &childrenBuf);
     }
 
     if (virCapabilitiesFormatResctrlMonitor(buf, memBW->monitor) < 0)