[libvirt PATCH 9/9] qemu: use g_new0

Ján Tomko jtomko at redhat.com
Mon Oct 5 10:36:39 UTC 2020


Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 src/qemu/qemu_agent.c          |  6 ++----
 src/qemu/qemu_block.c          | 15 +++++----------
 src/qemu/qemu_cgroup.c         |  6 ++----
 src/qemu/qemu_conf.c           | 20 ++++++++------------
 src/qemu/qemu_domain_address.c |  3 +--
 src/qemu/qemu_hotplug.c        | 29 +++++++++++------------------
 src/qemu/qemu_migration.c      |  6 ++----
 src/qemu/qemu_monitor.c        | 13 +++----------
 src/qemu/qemu_namespace.c      |  3 +--
 src/qemu/qemu_saveimage.c      |  3 +--
 src/qemu/qemu_vhost_user.c     | 17 +++++++----------
 11 files changed, 43 insertions(+), 78 deletions(-)

diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index 1239aceb46..09116e749c 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -1452,8 +1452,7 @@ qemuAgentGetVCPUs(qemuAgentPtr agent,
 
     ndata = virJSONValueArraySize(data);
 
-    if (VIR_ALLOC_N(*info, ndata) < 0)
-        goto cleanup;
+    *info = g_new0(qemuAgentCPUInfo, ndata);
 
     for (i = 0; i < ndata; i++) {
         virJSONValuePtr entry = virJSONValueArrayGet(data, i);
@@ -2148,8 +2147,7 @@ qemuAgentGetInterfaces(qemuAgentPtr agent,
             if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
                 goto error;
 
-            if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
-                goto error;
+            ifaces_ret[ifaces_count - 1] = g_new0(virDomainInterface, 1);
 
             if (virHashAddEntry(ifaces_store, ifname_s,
                                 ifaces_ret[ifaces_count - 1]) < 0)
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index 487b0a72e7..6bea21347a 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -169,8 +169,7 @@ qemuBlockNodeNameGetBackingChainBacking(virJSONValuePtr next,
         }
     }
 
-    if (VIR_ALLOC(data) < 0)
-        return -1;
+    data = g_new0(qemuBlockNodeNameBackingChainData, 1);
 
     data->nodeformat = g_strdup(nodename);
     data->nodestorage = g_strdup(parentnodename);
@@ -415,8 +414,7 @@ qemuBlockStorageSourceGetURI(virStorageSourcePtr src)
         return NULL;
     }
 
-    if (VIR_ALLOC(uri) < 0)
-        return NULL;
+    uri = g_new0(virURI, 1);
 
     if (src->hosts->transport == VIR_STORAGE_NET_HOST_TRANS_TCP) {
         uri->port = src->hosts->port;
@@ -1595,8 +1593,7 @@ qemuBlockStorageSourceAttachPrepareBlockdev(virStorageSourcePtr src,
     if (autoreadonly)
         backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY;
 
-    if (VIR_ALLOC(data) < 0)
-        return NULL;
+    data = g_new0(qemuBlockStorageSourceAttachData, 1);
 
     if (!(data->formatProps = qemuBlockStorageSourceGetBlockdevProps(src,
                                                                      backingStore)) ||
@@ -1886,8 +1883,7 @@ qemuBlockStorageSourceChainDetachPrepareBlockdev(virStorageSourcePtr src)
     g_autoptr(qemuBlockStorageSourceChainData) data = NULL;
     virStorageSourcePtr n;
 
-    if (VIR_ALLOC(data) < 0)
-        return NULL;
+    data = g_new0(qemuBlockStorageSourceChainData, 1);
 
     for (n = src; virStorageSourceIsBacking(n); n = n->backingStore) {
         if (!(backend = qemuBlockStorageSourceDetachPrepare(n, NULL)))
@@ -1916,8 +1912,7 @@ qemuBlockStorageSourceChainDetachPrepareDrive(virStorageSourcePtr src,
     g_autoptr(qemuBlockStorageSourceAttachData) backend = NULL;
     g_autoptr(qemuBlockStorageSourceChainData) data = NULL;
 
-    if (VIR_ALLOC(data) < 0)
-        return NULL;
+    data = g_new0(qemuBlockStorageSourceChainData, 1);
 
     if (!(backend = qemuBlockStorageSourceDetachPrepare(src, driveAlias)))
         return NULL;
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index e88da02341..b671991ad6 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -926,8 +926,7 @@ qemuInitCgroup(virDomainObjPtr vm,
     if (!vm->def->resource) {
         virDomainResourceDefPtr res;
 
-        if (VIR_ALLOC(res) < 0)
-            return -1;
+        res = g_new0(virDomainResourceDef, 1);
 
         res->partition = g_strdup("/machine");
 
@@ -1252,8 +1251,7 @@ qemuCgroupEmulatorAllNodesAllow(virCgroupPtr cgroup,
     if (!(all_nodes_str = virBitmapFormat(all_nodes)))
         goto cleanup;
 
-    if (VIR_ALLOC(data) < 0)
-        goto cleanup;
+    data = g_new0(qemuCgroupEmulatorAllNodesData, 1);
 
     if (virCgroupNewThread(cgroup, VIR_CGROUP_THREAD_EMULATOR, 0,
                            false, &data->emulatorCgroup) < 0)
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index f4ed1fa061..6d16c11017 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -641,9 +641,8 @@ virQEMUDriverConfigLoadProcessEntry(virQEMUDriverConfigPtr cfg,
         VIR_FREE(cfg->hugetlbfs);
 
         cfg->nhugetlbfs = virStringListLength((const char *const *)hugetlbfs);
-        if (hugetlbfs[0] &&
-            VIR_ALLOC_N(cfg->hugetlbfs, cfg->nhugetlbfs) < 0)
-            return -1;
+        if (hugetlbfs[0])
+            cfg->hugetlbfs = g_new0(virHugeTLBFS, cfg->nhugetlbfs);
 
         for (i = 0; hugetlbfs[i] != NULL; i++) {
             if (virQEMUDriverConfigHugeTLBFSInit(&cfg->hugetlbfs[i],
@@ -840,12 +839,11 @@ virQEMUDriverConfigLoadNVRAMEntry(virQEMUDriverConfigPtr cfg,
         }
 
         cfg->nfirmwares = virStringListLength((const char *const *)nvram);
-        if (nvram[0] && VIR_ALLOC_N(cfg->firmwares, cfg->nfirmwares) < 0)
-            return -1;
+        if (nvram[0])
+            cfg->firmwares = g_new0(virFirmwarePtr, cfg->nfirmwares);
 
         for (i = 0; nvram[i] != NULL; i++) {
-            if (VIR_ALLOC(cfg->firmwares[i]) < 0)
-                return -1;
+            cfg->firmwares[i] = g_new0(virFirmware, 1);
             if (virFirmwareParse(nvram[i], cfg->firmwares[i]) < 0)
                 return -1;
         }
@@ -1349,8 +1347,7 @@ virCapsPtr virQEMUDriverCreateCapabilities(virQEMUDriverPtr driver)
         ;
     caps->host.nsecModels = i;
 
-    if (VIR_ALLOC_N(caps->host.secModels, caps->host.nsecModels) < 0)
-        return NULL;
+    caps->host.secModels = g_new0(virCapsHostSecModel, caps->host.nsecModels);
 
     for (i = 0; sec_managers[i]; i++) {
         virCapsHostSecModelPtr sm = &caps->host.secModels[i];
@@ -1616,9 +1613,8 @@ qemuSharedDeviceEntryInsert(virQEMUDriverPtr driver,
             entry->domains[entry->ref - 1] = g_strdup(name);
         }
     } else {
-        if (VIR_ALLOC(entry) < 0 ||
-            VIR_ALLOC_N(entry->domains, 1) < 0)
-            goto error;
+        entry = g_new0(qemuSharedDeviceEntry, 1);
+        entry->domains = g_new0(char *, 1);
 
         entry->domains[0] = g_strdup(name);
 
diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index 61eb53ea2c..0199a620a0 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -3038,8 +3038,7 @@ qemuDomainUSBAddressAddHubs(virDomainDefPtr def)
               data.count, available_ports, hubs_needed);
 
     for (i = 0; i < hubs_needed; i++) {
-        if (VIR_ALLOC(hub) < 0)
-            return -1;
+        hub = g_new0(virDomainHubDef, 1);
         hub->type = VIR_DOMAIN_HUB_TYPE_USB;
 
         if (VIR_APPEND_ELEMENT(def->hubs, def->nhubs, hub) < 0)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 11b549b12b..09f8525cfa 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -928,8 +928,7 @@ qemuDomainFindOrCreateSCSIDiskController(virQEMUDriverPtr driver,
 
     /* No SCSI controller present, for backward compatibility we
      * now hotplug a controller */
-    if (VIR_ALLOC(cont) < 0)
-        return NULL;
+    cont = g_new0(virDomainControllerDef, 1);
     cont->type = VIR_DOMAIN_CONTROLLER_TYPE_SCSI;
     cont->idx = controller;
     if (model == VIR_DOMAIN_CONTROLLER_MODEL_SCSI_DEFAULT)
@@ -1243,11 +1242,9 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
         if (!tapfdSize)
             tapfdSize = vhostfdSize = 1;
         queueSize = tapfdSize;
-        if (VIR_ALLOC_N(tapfd, tapfdSize) < 0)
-            goto cleanup;
+        tapfd = g_new0(int, tapfdSize);
         memset(tapfd, -1, sizeof(*tapfd) * tapfdSize);
-        if (VIR_ALLOC_N(vhostfd, vhostfdSize) < 0)
-            goto cleanup;
+        vhostfd = g_new0(int, vhostfdSize);
         memset(vhostfd, -1, sizeof(*vhostfd) * vhostfdSize);
         if (qemuInterfaceBridgeConnect(vm->def, driver, net,
                                        tapfd, &tapfdSize) < 0)
@@ -1262,11 +1259,9 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
         if (!tapfdSize)
             tapfdSize = vhostfdSize = 1;
         queueSize = tapfdSize;
-        if (VIR_ALLOC_N(tapfd, tapfdSize) < 0)
-            goto cleanup;
+        tapfd = g_new0(int, tapfdSize);
         memset(tapfd, -1, sizeof(*tapfd) * tapfdSize);
-        if (VIR_ALLOC_N(vhostfd, vhostfdSize) < 0)
-            goto cleanup;
+        vhostfd = g_new0(int, vhostfdSize);
         memset(vhostfd, -1, sizeof(*vhostfd) * vhostfdSize);
         if (qemuInterfaceDirectConnect(vm->def, driver, net,
                                        tapfd, tapfdSize,
@@ -1282,10 +1277,9 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
         if (!tapfdSize)
             tapfdSize = vhostfdSize = 1;
         queueSize = tapfdSize;
-        if (VIR_ALLOC_N(tapfd, tapfdSize) < 0)
-            goto cleanup;
+        tapfd = g_new0(int, tapfdSize);
         memset(tapfd, -1, sizeof(*tapfd) * tapfdSize);
-        if (VIR_ALLOC_N(vhostfd, vhostfdSize) < 0)
+        vhostfd = g_new0(int, vhostfdSize);
             goto cleanup;
         memset(vhostfd, -1, sizeof(*vhostfd) * vhostfdSize);
         if (qemuInterfaceEthernetConnect(vm->def, driver, net,
@@ -1381,9 +1375,8 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
             goto cleanup;
     }
 
-    if (VIR_ALLOC_N(tapfdName, tapfdSize) < 0 ||
-        VIR_ALLOC_N(vhostfdName, vhostfdSize) < 0)
-        goto cleanup;
+    tapfdName = g_new0(char *, tapfdSize);
+    vhostfdName = g_new0(char *, vhostfdSize);
 
     for (i = 0; i < tapfdSize; i++)
         tapfdName[i] = g_strdup_printf("fd-%s%zu", net->info.alias, i);
@@ -1973,8 +1966,8 @@ qemuDomainChrPreInsert(virDomainDefPtr vmdef,
      */
     if (vmdef->nserials == 0 && vmdef->nconsoles == 0 &&
         chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL) {
-        if (!vmdef->consoles && VIR_ALLOC(vmdef->consoles) < 0)
-            return -1;
+        if (!vmdef->consoles)
+            vmdef->consoles = g_new0(virDomainChrDefPtr, 1);
 
         /* We'll be dealing with serials[0] directly, so NULL is fine here. */
         if (!(vmdef->consoles[0] = virDomainChrDefNew(NULL))) {
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 403f5d3e13..2000c86640 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -3309,8 +3309,7 @@ static void qemuMigrationSrcIOFunc(void *arg)
     VIR_DEBUG("Running migration tunnel; stream=%p, sock=%d",
               data->st, data->sock);
 
-    if (VIR_ALLOC_N(buffer, TUNNEL_SEND_BUF_SIZE) < 0)
-        goto abrt;
+    buffer = g_new0(char, TUNNEL_SEND_BUF_SIZE);
 
     fds[0].fd = data->sock;
     fds[1].fd = data->wakeupRecvFD;
@@ -3414,8 +3413,7 @@ qemuMigrationSrcStartTunnel(virStreamPtr st,
     if (virPipe(wakeupFD) < 0)
         goto error;
 
-    if (VIR_ALLOC(io) < 0)
-        goto error;
+    io = g_new0(qemuMigrationIOThread, 1);
 
     io->st = st;
     io->sock = sock;
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index eb4b04dc1a..27ad739983 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -1869,8 +1869,7 @@ qemuMonitorGetCPUInfo(qemuMonitorPtr mon,
 
     QEMU_CHECK_MONITOR(mon);
 
-    if (VIR_ALLOC_N(info, maxvcpus) < 0)
-        return -1;
+    info = g_new0(qemuMonitorCPUInfo, maxvcpus);
 
     /* initialize a few non-zero defaults */
     qemuMonitorCPUInfoClear(info, maxvcpus);
@@ -3685,11 +3684,9 @@ qemuMonitorCPUModelInfoCopy(const qemuMonitorCPUModelInfo *orig)
     qemuMonitorCPUModelInfoPtr copy;
     size_t i;
 
-    if (VIR_ALLOC(copy) < 0)
-        goto error;
+    copy = g_new0(qemuMonitorCPUModelInfo, 1);
 
-    if (VIR_ALLOC_N(copy->props, orig->nprops) < 0)
-        goto error;
+    copy->props = g_new0(qemuMonitorCPUProperty, orig->nprops);
 
     copy->name = g_strdup(orig->name);
 
@@ -3720,10 +3717,6 @@ qemuMonitorCPUModelInfoCopy(const qemuMonitorCPUModelInfo *orig)
     }
 
     return copy;
-
- error:
-    qemuMonitorCPUModelInfoFree(copy);
-    return NULL;
 }
 
 
diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c
index b0d1d0d083..5c58893693 100644
--- a/src/qemu/qemu_namespace.c
+++ b/src/qemu/qemu_namespace.c
@@ -155,8 +155,7 @@ qemuDomainGetPreservedMounts(virQEMUDriverConfigPtr cfg,
         }
     }
 
-    if (VIR_ALLOC_N(paths, nmounts) < 0)
-        goto error;
+    paths = g_new0(char *, nmounts);
 
     for (i = 0; i < nmounts; i++) {
         if (!(paths[i] = qemuDomainGetPreservedMountPath(cfg, vm, mounts[i])))
diff --git a/src/qemu/qemu_saveimage.c b/src/qemu/qemu_saveimage.c
index 52468056ad..0e588d463d 100644
--- a/src/qemu/qemu_saveimage.c
+++ b/src/qemu/qemu_saveimage.c
@@ -103,8 +103,7 @@ virQEMUSaveDataNew(char *domXML,
     virQEMUSaveDataPtr data = NULL;
     virQEMUSaveHeaderPtr header;
 
-    if (VIR_ALLOC(data) < 0)
-        return NULL;
+    data = g_new0(virQEMUSaveData, 1);
 
     data->xml = g_steal_pointer(&domXML);
 
diff --git a/src/qemu/qemu_vhost_user.c b/src/qemu/qemu_vhost_user.c
index be7c3dd5b8..260f3cd550 100644
--- a/src/qemu/qemu_vhost_user.c
+++ b/src/qemu/qemu_vhost_user.c
@@ -196,8 +196,7 @@ qemuVhostUserParse(const char *path)
         return NULL;
     }
 
-    if (VIR_ALLOC(vu) < 0)
-        return NULL;
+    vu = g_new0(qemuVhostUser, 1);
 
     if (qemuVhostUserTypeParse(path, doc, vu) < 0)
         return NULL;
@@ -253,8 +252,7 @@ qemuVhostUserFetchParsedConfigs(bool privileged,
 
     npaths = virStringListLength((const char **)paths);
 
-    if (VIR_ALLOC_N(vus, npaths) < 0)
-        return -1;
+    vus = g_new0(qemuVhostUserPtr, npaths);
 
     for (i = 0; i < npaths; i++) {
         if (!(vus[i] = qemuVhostUserParse(paths[i])))
@@ -292,8 +290,7 @@ qemuVhostUserGPUFillCapabilities(qemuVhostUserPtr vu,
     }
 
     nfeatures = virJSONValueArraySize(featuresJSON);
-    if (VIR_ALLOC_N(features, nfeatures) < 0)
-        return -1;
+    features = g_new0(qemuVhostUserGPUFeature, nfeatures);
 
     for (i = 0; i < nfeatures; i++) {
         virJSONValuePtr item = virJSONValueArrayGet(featuresJSON, i);
@@ -382,8 +379,8 @@ qemuVhostUserFillDomainGPU(virQEMUDriverPtr driver,
                 continue;
         }
 
-        if (!video->driver && VIR_ALLOC(video->driver) < 0)
-            goto end;
+        if (!video->driver)
+            video->driver = g_new0(virDomainVideoDriverDef, 1);
 
         VIR_FREE(video->driver->vhost_user_binary);
         video->driver->vhost_user_binary = g_strdup(vu->binary);
@@ -397,8 +394,8 @@ qemuVhostUserFillDomainGPU(virQEMUDriverPtr driver,
         goto end;
     }
 
-    if (!video->accel && VIR_ALLOC(video->accel) < 0)
-        goto end;
+    if (!video->accel)
+        video->accel = g_new0(virDomainVideoAccelDef, 1);
 
     if (!video->accel->rendernode &&
         qemuVhostUserGPUHasFeature(&vu->capabilities.gpu,
-- 
2.26.2




More information about the libvir-list mailing list