[PATCH 2/5] qemu: Rewrite qemuFirmwareFetchParsedConfigs()

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

 



Instead of returning the list of paths exactly as obtained
from qemuFirmwareFetchConfigs(), and allocating the list of
firmwares to be exactly that size right away, start with two
empty lists and add elements to them one by one.

At the moment this only makes things more verbose, but later
we're going to change things so that it's possible that some
of the paths/firmwares are not included in the lists returned
to the caller, and at that point the changes will pay off.

Note that we can't use g_auto() for the new list of paths,
because until the very last moment it's not null-terminated,
so g_strfreev() wouldn't be able to handle it correctly.

Signed-off-by: Andrea Bolognani <abologna@xxxxxxxxxx>
---
 src/qemu/qemu_firmware.c | 46 ++++++++++++++++++++++++++--------------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c
index 3fd4f75778..4fc7dd3b71 100644
--- a/src/qemu/qemu_firmware.c
+++ b/src/qemu/qemu_firmware.c
@@ -1562,35 +1562,49 @@ qemuFirmwareFetchParsedConfigs(bool privileged,
                                qemuFirmware ***firmwaresRet,
                                char ***pathsRet)
 {
-    g_auto(GStrv) paths = NULL;
-    size_t npaths;
+    g_auto(GStrv) possiblePaths = NULL;
+    char **currentPath = NULL;
     qemuFirmware **firmwares = NULL;
-    size_t i;
+    char **paths = NULL;
+    size_t nfirmwares = 0;
+    size_t npaths = 0;
 
-    if (qemuFirmwareFetchConfigs(&paths, privileged) < 0)
+    if (qemuFirmwareFetchConfigs(&possiblePaths, privileged) < 0)
         return -1;
 
-    if (!paths)
+    if (!possiblePaths)
         return 0;
 
-    npaths = g_strv_length(paths);
+    for (currentPath = possiblePaths; *currentPath; currentPath++) {
+        qemuFirmware *firmware = qemuFirmwareParse(*currentPath);
 
-    firmwares = g_new0(qemuFirmware *, npaths);
-
-    for (i = 0; i < npaths; i++) {
-        if (!(firmwares[i] = qemuFirmwareParse(paths[i])))
+        if (!firmware)
             goto error;
+
+        VIR_APPEND_ELEMENT(firmwares, nfirmwares, firmware);
+
+        if (pathsRet) {
+            char *path = g_strdup(*currentPath);
+            VIR_APPEND_ELEMENT(paths, npaths, path);
+        }
+    }
+
+    *firmwaresRet = firmwares;
+    if (pathsRet) {
+        char *terminator = NULL;
+        VIR_APPEND_ELEMENT(paths, npaths, terminator);
+        *pathsRet = paths;
     }
 
-    *firmwaresRet = g_steal_pointer(&firmwares);
-    if (pathsRet)
-        *pathsRet = g_steal_pointer(&paths);
-    return npaths;
+    return nfirmwares;
 
  error:
-    while (i > 0)
-        qemuFirmwareFree(firmwares[--i]);
+    while (nfirmwares > 0)
+        qemuFirmwareFree(firmwares[--nfirmwares]);
     VIR_FREE(firmwares);
+    while (npaths > 0)
+        VIR_FREE(paths[--npaths]);
+    VIR_FREE(paths);
     return -1;
 }
 
-- 
2.43.2
_______________________________________________
Devel mailing list -- devel@xxxxxxxxxxxxxxxxx
To unsubscribe send an email to devel-leave@xxxxxxxxxxxxxxxxx




[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]

  Powered by Linux