[PATCH v3 3/4] logical: Clean up allocation when building regex on the fly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rather than a loop reallocating space to build the regex, just allocate
it once up front, then if there's more than 1 nextent, append a comma and
another regex_unit string.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/storage/storage_backend_logical.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index cd0fec0..eb22fd0 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -120,12 +120,11 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
         goto cleanup;
     }
 
-    if (VIR_STRDUP(regex, regex_unit) < 0)
+    /* Allocate space for 'nextents' regex_unit strings plus a comma for each */
+    if (VIR_ALLOC_N(regex, nextents * (strlen(regex_unit) + 1) + 1) < 0)
         goto cleanup;
-
+    strncat(regex, regex_unit, strlen(regex_unit));
     for (i = 1; i < nextents; i++) {
-        if (VIR_REALLOC_N(regex, strlen(regex) + strlen(regex_unit) + 2) < 0)
-            goto cleanup;
         /* "," is the separator of "devices" field */
         strcat(regex, ",");
         strncat(regex, regex_unit, strlen(regex_unit));
-- 
2.5.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]