]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: Move iothread formatter into a separate function
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 16 May 2022 09:59:48 +0000 (11:59 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 10 Jun 2022 11:53:52 +0000 (13:53 +0200)
Formatting iothreads is currently open coded inside of
virDomainDefFormatInternalSetRootName(). While this works, it
makes the function needlessly long, especially if the formatting
code will expand in near future. Therefore, move it into a
separate function. At the same time, make
virDomainDefIothreadShouldFormat() accept const domain definition
so that the new function can also accept const domain definition.
Formatters shouldn't need to change definition.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
src/conf/domain_conf.c

index 22b58e32122552006dcd932fec0d021e22579d40..f7b86be5fc29eb33d9ba559be83b19a951e9b3b3 100644 (file)
@@ -27570,7 +27570,7 @@ virDomainCpuDefFormat(virBuffer *buf,
 
 
 static bool
-virDomainDefIothreadShouldFormat(virDomainDef *def)
+virDomainDefIothreadShouldFormat(const virDomainDef *def)
 {
     size_t i;
 
@@ -27583,6 +27583,31 @@ virDomainDefIothreadShouldFormat(virDomainDef *def)
 }
 
 
+static void
+virDomainDefIOThreadsFormat(virBuffer *buf,
+                            const virDomainDef *def)
+{
+    g_auto(virBuffer) childrenBuf = VIR_BUFFER_INIT_CHILD(buf);
+    size_t i;
+
+    if (def->niothreadids == 0)
+        return;
+
+    virBufferAsprintf(buf, "<iothreads>%zu</iothreads>\n",
+                      def->niothreadids);
+
+    if (!virDomainDefIothreadShouldFormat(def))
+        return;
+
+    for (i = 0; i < def->niothreadids; i++) {
+        virBufferAsprintf(&childrenBuf, "<iothread id='%u'/>\n",
+                          def->iothreadids[i]->iothread_id);
+    }
+
+    virXMLFormatElement(buf, "iothreadids", NULL, &childrenBuf);
+}
+
+
 static void
 virDomainIOMMUDefFormat(virBuffer *buf,
                         const virDomainIOMMUDef *iommu)
@@ -28228,20 +28253,7 @@ virDomainDefFormatInternalSetRootName(virDomainDef *def,
     if (virDomainCpuDefFormat(buf, def) < 0)
         return -1;
 
-    if (def->niothreadids > 0) {
-        virBufferAsprintf(buf, "<iothreads>%zu</iothreads>\n",
-                          def->niothreadids);
-        if (virDomainDefIothreadShouldFormat(def)) {
-            virBufferAddLit(buf, "<iothreadids>\n");
-            virBufferAdjustIndent(buf, 2);
-            for (i = 0; i < def->niothreadids; i++) {
-                virBufferAsprintf(buf, "<iothread id='%u'/>\n",
-                                  def->iothreadids[i]->iothread_id);
-            }
-            virBufferAdjustIndent(buf, -2);
-            virBufferAddLit(buf, "</iothreadids>\n");
-        }
-    }
+    virDomainDefIOThreadsFormat(buf, def);
 
     if (virDomainCputuneDefFormat(buf, def, flags) < 0)
         return -1;