[PATCH 13/40] virResctrlInfoGetMonitorPrefix: Don't use 'virStringListAdd' to construct list

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


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 at redhat.com>
---
 src/util/virresctrl.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c
index 29df51c652..86b4b9d73b 100644
--- a/src/util/virresctrl.c
+++ b/src/util/virresctrl.c
@@ -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. */
-- 
2.29.2




More information about the libvir-list mailing list