[PATCH 05/16] conf: Move iothread formatter into a separate function

Michal Privoznik posted 16 patches 3 years, 8 months ago
There is a newer version of this series
[PATCH 05/16] conf: Move iothread formatter into a separate function
Posted by Michal Privoznik 3 years, 8 months ago
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>
---
 src/conf/domain_conf.c | 42 +++++++++++++++++++++++++++---------------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index eec5941089..c60ab4e064 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -27569,7 +27569,7 @@ virDomainCpuDefFormat(virBuffer *buf,
 
 
 static bool
-virDomainDefIothreadShouldFormat(virDomainDef *def)
+virDomainDefIothreadShouldFormat(const virDomainDef *def)
 {
     size_t i;
 
@@ -27582,6 +27582,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)
@@ -28227,20 +28252,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;
-- 
2.35.1
Re: [PATCH 05/16] conf: Move iothread formatter into a separate function
Posted by Peter Krempa 3 years, 8 months ago
On Thu, Jun 02, 2022 at 09:17:55 +0200, Michal Privoznik wrote:
> 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>
> ---
>  src/conf/domain_conf.c | 42 +++++++++++++++++++++++++++---------------
>  1 file changed, 27 insertions(+), 15 deletions(-)

Reviewed-by: Peter Krempa <pkrempa@redhat.com>