]> xenbits.xensource.com Git - libvirt.git/commitdiff
virResctrlInfoGetMonitorPrefix: Don't use 'virStringListAdd' to construct list
authorPeter Krempa <pkrempa@redhat.com>
Fri, 5 Feb 2021 14:28:46 +0000 (15:28 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 11 Feb 2021 16:05:32 +0000 (17:05 +0100)
Pre-allocate a buffer for the upper limit and shrink it afterwards to
avoid use of 'virStringListAdd' in a loop.

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

index 29df51c652e99585eca932bb40c8f3f750a85f85..86b4b9d73bc6afcfb1735b0cdaaeb6da54fb6eed 100644 (file)
@@ -1009,15 +1009,15 @@ virResctrlInfoGetMonitorPrefix(virResctrlInfoPtr resctrl,
         mon->cache_level = mongrp_info->cache_level;
     }
 
+    mon->features = g_new0(char *, mongrp_info->nfeatures + 1);
+
     for (i = 0; i < mongrp_info->nfeatures; i++) {
-        if (STRPREFIX(mongrp_info->features[i], prefix)) {
-            if (virStringListAdd(&mon->features,
-                                 mongrp_info->features[i]) < 0)
-                goto cleanup;
-            mon->nfeatures++;
-        }
+        if (STRPREFIX(mongrp_info->features[i], prefix))
+            mon->features[mon->nfeatures++] = g_strdup(mongrp_info->features[i]);
     }
 
+    mon->features = g_renew(char *, mon->features, mon->nfeatures + 1);
+
     ret = 0;
 
     /* In case *monitor is pointed to some monitor, clean it. */