[PATCH 17/40] xenParseXLNamespaceData: Pre-calculate the length of array

Peter Krempa pkrempa at redhat.com
Sat Feb 6 08:32:39 UTC 2021


Precalculate the lenght to avoid use of 'virStringListAdd' in a loop.
The code is also simplified by using APIs which don't return errors.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/libxl/xen_xl.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/src/libxl/xen_xl.c b/src/libxl/xen_xl.c
index 941832ce4e..a8af54a845 100644
--- a/src/libxl/xen_xl.c
+++ b/src/libxl/xen_xl.c
@@ -1150,33 +1150,36 @@ static int
 xenParseXLNamespaceData(virConfPtr conf, virDomainDefPtr def)
 {
     virConfValuePtr list = virConfGetValue(conf, "device_model_args");
-    g_auto(GStrv) args = NULL;
-    size_t nargs;
+    virConfValuePtr next;
+    size_t nargs = 0;
     libxlDomainXmlNsDefPtr nsdata = NULL;
+    size_t n = 0;

-    if (list && list->type == VIR_CONF_LIST) {
-        list = list->list;
-        while (list) {
-            if ((list->type != VIR_CONF_STRING) || (list->str == NULL)) {
-                list = list->next;
-                continue;
-            }
+    if (!list || list->type != VIR_CONF_LIST)
+        return 0;

-            virStringListAdd(&args, list->str);
-            list = list->next;
-        }
+    list = list->list;
+
+    for (next = list; next; next = next->next) {
+        if (next->type != VIR_CONF_STRING || !next->str)
+            continue;
+
+        nargs++;
     }

-    if (!args)
+    if (nargs == 0)
         return 0;

-    nargs = g_strv_length(args);
-    if (nargs > 0) {
-        nsdata = g_new0(libxlDomainXmlNsDef, 1);
+    nsdata = g_new0(libxlDomainXmlNsDef, 1);
+    def->namespaceData = nsdata;
+    nsdata->args = g_new0(char *, nargs + 1);
+    nsdata->num_args = nargs;
+
+    for (next = list; next; next = next->next) {
+        if (next->type != VIR_CONF_STRING || !next->str)
+            continue;

-        nsdata->args = g_steal_pointer(&args);
-        nsdata->num_args = nargs;
-        def->namespaceData = nsdata;
+        nsdata->args[n++] = g_strdup(next->str);
     }

     return 0;
-- 
2.29.2




More information about the libvir-list mailing list