[libvirt PATCH 4/4] vbox: use g_new0 instead of VIR_ALLOC

Ján Tomko jtomko at redhat.com
Sun Oct 4 22:22:26 UTC 2020


Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 src/vbox/vbox_common.c        | 90 +++++++++++------------------------
 src/vbox/vbox_network.c       | 13 ++---
 src/vbox/vbox_snapshot_conf.c | 48 ++++++++-----------
 3 files changed, 53 insertions(+), 98 deletions(-)

diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c
index f59af697ea..317fb5ce92 100644
--- a/src/vbox/vbox_common.c
+++ b/src/vbox/vbox_common.c
@@ -3010,8 +3010,7 @@ vboxHostDeviceGetXMLDesc(vboxDriverPtr data, virDomainDefPtr def, IMachine *mach
         goto release_filters;
 
     /* Alloc mem needed for the filters now */
-    if (VIR_ALLOC_N(def->hostdevs, def->nhostdevs) < 0)
-        goto release_filters;
+    def->hostdevs = g_new0(virDomainHostdevDefPtr, def->nhostdevs);
 
     for (i = 0; i < def->nhostdevs; i++) {
         def->hostdevs[i] = virDomainHostdevDefNew();
@@ -3219,8 +3218,7 @@ vboxDumpDisks(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine)
     }
 
     /* Allocate mem, if fails return error */
-    if (VIR_ALLOC_N(def->disks, def->ndisks) < 0)
-        goto cleanup;
+    def->disks = g_new0(virDomainDiskDefPtr, def->ndisks);
 
     for (i = 0; i < def->ndisks; i++) {
         disk = virDomainDiskDefNew(NULL);
@@ -3452,12 +3450,10 @@ vboxDumpVideo(virDomainDefPtr def, vboxDriverPtr data G_GNUC_UNUSED,
     PRBool accelerate2DEnabled = PR_FALSE;
 
     /* Currently supports only one graphics card */
-    if (VIR_ALLOC_N(def->videos, 1) < 0)
-        return -1;
+    def->videos = g_new0(virDomainVideoDefPtr, 1);
     def->nvideos = 1;
 
-    if (VIR_ALLOC(def->videos[0]) < 0)
-        return -1;
+    def->videos[0] = g_new0(virDomainVideoDef, 1);
 
     gVBoxAPI.UIMachine.GetVRAMSize(machine, &VRAMSize);
     gVBoxAPI.UIMachine.GetMonitorCount(machine, &monitorCount);
@@ -3467,8 +3463,7 @@ vboxDumpVideo(virDomainDefPtr def, vboxDriverPtr data G_GNUC_UNUSED,
     def->videos[0]->type = VIR_DOMAIN_VIDEO_TYPE_VBOX;
     def->videos[0]->vram = VRAMSize * 1024;
     def->videos[0]->heads = monitorCount;
-    if (VIR_ALLOC(def->videos[0]->accel) < 0)
-        return -1;
+    def->videos[0]->accel = g_new0(virDomainVideoAccelDef, 1);
     def->videos[0]->accel->accel3d = accelerate3DEnabled ?
         VIR_TRISTATE_BOOL_YES : VIR_TRISTATE_BOOL_NO;
     def->videos[0]->accel->accel2d = accelerate2DEnabled ?
@@ -3506,8 +3501,7 @@ vboxDumpDisplay(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine)
         PRUnichar *valueDisplayUtf16 = NULL;
         char *valueDisplayUtf8 = NULL;
 
-        if (VIR_ALLOC(graphics) < 0)
-            goto cleanup;
+        graphics = g_new0(virDomainGraphicsDef, 1);
 
         VBOX_UTF8_TO_UTF16("FRONTEND/Display", &keyUtf16);
         gVBoxAPI.UIMachine.GetExtraData(machine, keyUtf16, &valueDisplayUtf16);
@@ -3534,8 +3528,7 @@ vboxDumpDisplay(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine)
         }
         VBOX_UTF8_FREE(valueDisplayUtf8);
     } else if (STRNEQ_NULLABLE(valueTypeUtf8, "vrdp")) {
-        if (VIR_ALLOC(graphics) < 0)
-            goto cleanup;
+        graphics = g_new0(virDomainGraphicsDef, 1);
 
         graphics->type = VIR_DOMAIN_GRAPHICS_TYPE_DESKTOP;
         graphics->data.desktop.display = g_strdup(getenv("DISPLAY"));
@@ -3554,8 +3547,7 @@ vboxDumpDisplay(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine)
         PRBool allowMultiConnection = PR_FALSE;
         PRBool reuseSingleConnection = PR_FALSE;
 
-        if (VIR_ALLOC(graphics) < 0)
-            goto cleanup;
+        graphics = g_new0(virDomainGraphicsDef, 1);
 
         gVBoxAPI.UIVRDEServer.GetPorts(data, VRDEServer, machine, graphics);
 
@@ -3612,8 +3604,7 @@ vboxDumpSharedFolders(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine
         goto cleanup;
     }
 
-    if (VIR_ALLOC_N(def->fss, sharedFolders.count) < 0)
-        goto cleanup;
+    def->fss = g_new0(virDomainFSDefPtr, sharedFolders.count);
 
     for (i = 0; i < sharedFolders.count; i++) {
         ISharedFolder *sharedFolder = sharedFolders.items[i];
@@ -3798,10 +3789,8 @@ vboxDumpAudio(virDomainDefPtr def, vboxDriverPtr data G_GNUC_UNUSED,
             PRUint32 audioController = AudioControllerType_AC97;
 
             def->nsounds = 1;
-            if (VIR_ALLOC_N(def->sounds, def->nsounds) < 0)
-                return;
-            if (VIR_ALLOC(def->sounds[0]) < 0)
-                return;
+            def->sounds = g_new0(virDomainSoundDefPtr, def->nsounds);
+            def->sounds[0] = g_new0(virDomainSoundDef, 1);
 
             gVBoxAPI.UIAudioAdapter.GetAudioController(audioAdapter, &audioController);
             if (audioController == AudioControllerType_SB16) {
@@ -3839,8 +3828,7 @@ vboxDumpSerial(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine, PRUin
 
     /* Allocate memory for the serial ports which are enabled */
     if (def->nserials > 0) {
-        if (VIR_ALLOC_N(def->serials, def->nserials) < 0)
-            return -1;
+        def->serials = g_new0(virDomainChrDefPtr, def->nserials);
 
         for (i = 0; i < def->nserials; i++) {
             def->serials[i] = virDomainChrDefNew(NULL);
@@ -3934,8 +3922,7 @@ vboxDumpParallel(virDomainDefPtr def, vboxDriverPtr data, IMachine *machine, PRU
 
     /* Allocate memory for the parallel ports which are enabled */
     if (def->nparallels > 0) {
-        if (VIR_ALLOC_N(def->parallels, def->nparallels) < 0)
-            return -1;
+        def->parallels = g_new0(virDomainChrDefPtr, def->nparallels);
 
         for (i = 0; i < def->nparallels; i++) {
             def->parallels[i] = virDomainChrDefNew(NULL);
@@ -4715,10 +4702,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
             VBOX_UTF16_TO_UTF8(formatUtf, &format);
             VBOX_UTF16_FREE(formatUtf);
 
-            if (VIR_ALLOC(readWriteDisk) < 0) {
-                VIR_FREE(formatUtf);
-                goto cleanup;
-            }
+            readWriteDisk = g_new0(virVBoxSnapshotConfHardDisk, 1);
 
             readWriteDisk->format = format;
             readWriteDisk->uuid = uuid;
@@ -4855,11 +4839,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
             goto cleanup;
         }
 
-        if (VIR_ALLOC(readOnlyDisk) < 0) {
-            VIR_FREE(uuid);
-            VIR_FREE(parentUuid);
-            goto cleanup;
-        }
+        readOnlyDisk = g_new0(virVBoxSnapshotConfHardDisk, 1);
 
         readOnlyDisk->format = format;
         readOnlyDisk->uuid = uuid;
@@ -4953,8 +4933,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
     /* Here, all disks are closed or deleted */
 
     /* We are now going to create and fill the Snapshot xml struct */
-    if (VIR_ALLOC(newSnapshotPtr) < 0)
-        goto cleanup;
+    newSnapshotPtr = g_new0(virVBoxSnapshotConfSnapshot, 1);
 
     if (virUUIDGenerate(snapshotUuid) < 0)
         goto cleanup;
@@ -5088,8 +5067,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
             }
             VBOX_UTF16_FREE(locationUtf16);
 
-            if (VIR_ALLOC(disk) < 0)
-                goto cleanup;
+            disk = g_new0(virVBoxSnapshotConfHardDisk, 1);
 
             rc = gVBoxAPI.UIMedium.GetFormat(medium, &formatUtf16);
             if (NS_FAILED(rc)) {
@@ -5248,8 +5226,7 @@ vboxSnapshotRedefine(virDomainPtr dom,
              * media registry and the machine storage controllers.
              */
 
-            if (VIR_ALLOC(newHardDisk) < 0)
-                goto cleanup;
+            newHardDisk = g_new0(virVBoxSnapshotConfHardDisk, 1);
 
             rc = gVBoxAPI.UIMedium.GetId(newMedium, &iid);
             if (NS_FAILED(rc)) {
@@ -5559,8 +5536,7 @@ vboxDomainSnapshotGetAll(virDomainPtr dom,
     if (count == 0)
         goto out;
 
-    if (VIR_ALLOC_N(list, count) < 0)
-        goto error;
+    list = g_new0(ISnapshot *, count);
 
     rc = gVBoxAPI.UIMachine.FindSnapshot(machine, &empty, list);
     if (NS_FAILED(rc) || !list[0]) {
@@ -5742,11 +5718,9 @@ vboxSnapshotGetReadWriteDisks(virDomainSnapshotDefPtr def,
         }
     }
     /* Allocate mem, if fails return error */
-    if (VIR_ALLOC_N(def->disks, def->ndisks) < 0)
-        goto cleanup;
+    def->disks = g_new0(virDomainSnapshotDiskDef, def->ndisks);
     for (i = 0; i < def->ndisks; i++) {
-        if (VIR_ALLOC(def->disks[i].src) < 0)
-            goto cleanup;
+        def->disks[i].src = g_new0(virStorageSource, 1);
     }
 
     /* get the attachment details here */
@@ -5971,8 +5945,7 @@ vboxSnapshotGetReadOnlyDisks(virDomainSnapshotDefPtr def,
     }
 
     /* Allocate mem, if fails return error */
-    if (VIR_ALLOC_N(defdom->disks, defdom->ndisks) < 0)
-        goto cleanup;
+    defdom->disks = g_new0(virDomainDiskDefPtr, defdom->ndisks);
 
     for (i = 0; i < defdom->ndisks; i++) {
         virDomainDiskDefPtr diskDef = virDomainDiskDefNew(NULL);
@@ -6339,8 +6312,7 @@ static int vboxDomainSnapshotListNames(virDomainPtr dom, char **names,
         vboxIID empty;
 
         VBOX_IID_INITIALIZE(&empty);
-        if (VIR_ALLOC_N(snapshots, 1) < 0)
-            goto cleanup;
+        snapshots = g_new0(ISnapshot *, 1);
         rc = gVBoxAPI.UIMachine.FindSnapshot(machine, &empty, snapshots);
         if (NS_FAILED(rc) || !snapshots[0]) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -7015,8 +6987,7 @@ vboxDomainSnapshotDeleteMetadataOnly(virDomainSnapshotPtr snapshot)
                  * the machine storage controller.
                  */
 
-                if (VIR_ALLOC(disk) < 0)
-                    goto cleanup;
+                disk = g_new0(virVBoxSnapshotConfHardDisk, 1);
 
                 rc = gVBoxAPI.UIMedium.GetId(newMedium, &iid);
                 if (NS_FAILED(rc)) {
@@ -7508,9 +7479,8 @@ vboxConnectListAllDomains(virConnectPtr conn,
          !MATCH(VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART)) ||
         (MATCH(VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE) &&
          !MATCH(VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE))) {
-        if (domains &&
-            VIR_ALLOC_N(*domains, 1) < 0)
-            goto cleanup;
+        if (domains)
+            *domains = g_new0(virDomainPtr, 1);
 
         ret = 0;
         goto cleanup;
@@ -7523,9 +7493,8 @@ vboxConnectListAllDomains(virConnectPtr conn,
         goto cleanup;
     }
 
-    if (domains &&
-        VIR_ALLOC_N(doms, machines.count + 1) < 0)
-        goto cleanup;
+    if (domains)
+        doms = g_new0(virDomainPtr, machines.count + 1);
 
     for (i = 0; i < machines.count; i++) {
         IMachine *machine = machines.items[i];
@@ -7784,8 +7753,7 @@ vboxDomainSendKey(virDomainPtr dom,
 
     keyDownCodes = (PRInt32 *) keycodes;
 
-    if (VIR_ALLOC_N(keyUpCodes, nkeycodes) < 0)
-        return ret;
+    keyUpCodes = g_new0(PRInt32, nkeycodes);
 
     /* translate keycodes to xt and generate keyup scancodes */
     for (i = 0; i < nkeycodes; i++) {
diff --git a/src/vbox/vbox_network.c b/src/vbox/vbox_network.c
index ceffc2053d..c3c9a55248 100644
--- a/src/vbox/vbox_network.c
+++ b/src/vbox/vbox_network.c
@@ -785,10 +785,8 @@ static char *vboxNetworkGetXMLDesc(virNetworkPtr network, unsigned int flags)
     VBOX_IID_INITIALIZE(&vboxnet0IID);
     virCheckFlags(0, NULL);
 
-    if (VIR_ALLOC(def) < 0)
-        goto cleanup;
-    if (VIR_ALLOC(ipdef) < 0)
-        goto cleanup;
+    def = g_new0(virNetworkDef, 1);
+    ipdef = g_new0(virNetworkIPDef, 1);
     def->ips = ipdef;
     def->nips = 1;
 
@@ -829,9 +827,7 @@ static char *vboxNetworkGetXMLDesc(virNetworkPtr network, unsigned int flags)
         bool errorOccurred = false;
 
         ipdef->nranges = 1;
-
-        if (VIR_ALLOC_N(ipdef->ranges, ipdef->nranges) < 0)
-            goto cleanup;
+        ipdef->ranges = g_new0(virNetworkDHCPRangeDef, ipdef->nranges);
 
         gVBoxAPI.UIDHCPServer.GetIPAddress(dhcpServer, &ipAddressUtf16);
         gVBoxAPI.UIDHCPServer.GetNetworkMask(dhcpServer, &networkMaskUtf16);
@@ -861,8 +857,7 @@ static char *vboxNetworkGetXMLDesc(virNetworkPtr network, unsigned int flags)
             goto cleanup;
 
         ipdef->nhosts = 1;
-        if (VIR_ALLOC_N(ipdef->hosts, ipdef->nhosts) < 0)
-            goto cleanup;
+        ipdef->hosts = g_new0(virNetworkDHCPHostDef, ipdef->nhosts);
 
         ipdef->hosts[0].name = g_strdup(network->name);
         gVBoxAPI.UIHNInterface.GetHardwareAddress(networkInterface, &macAddressUtf16);
diff --git a/src/vbox/vbox_snapshot_conf.c b/src/vbox/vbox_snapshot_conf.c
index a18958fcc0..cb201e777e 100644
--- a/src/vbox/vbox_snapshot_conf.c
+++ b/src/vbox/vbox_snapshot_conf.c
@@ -45,8 +45,8 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode,
     char *location = NULL;
     char *tmp = NULL;
     int n = 0;
-    if (VIR_ALLOC(hardDisk) < 0)
-        goto cleanup;
+
+    hardDisk = g_new0(virVBoxSnapshotConfHardDisk, 1);
 
     xPathContext->node = diskNode;
 
@@ -54,8 +54,8 @@ virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(xmlNodePtr diskNode,
     if (n < 0)
         goto cleanup;
 
-    if (n && VIR_ALLOC_N(hardDisk->children, n) < 0)
-        goto cleanup;
+    if (n)
+        hardDisk->children = g_new0(virVBoxSnapshotConfHardDiskPtr, n);
     hardDisk->nchildren = n;
     for (i = 0; i < hardDisk->nchildren; i++) {
         hardDisk->children[i] = virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(nodes[i], xPathContext, machineLocation);
@@ -123,8 +123,7 @@ virVBoxSnapshotConfRetrieveMediaRegistry(xmlNodePtr mediaRegistryNode,
     int result = -1;
     int n = 0;
 
-    if (VIR_ALLOC(mediaRegistry) < 0)
-        goto cleanup;
+    mediaRegistry = g_new0(virVBoxSnapshotConfMediaRegistry, 1);
 
     xPathContext->node = mediaRegistryNode;
     hardDisksNode = virXPathNode("./vbox:HardDisks", xPathContext);
@@ -133,8 +132,8 @@ virVBoxSnapshotConfRetrieveMediaRegistry(xmlNodePtr mediaRegistryNode,
     n = virXPathNodeSet("./vbox:HardDisk", xPathContext, &nodes);
     if (n < 0)
         goto cleanup;
-    if (n && VIR_ALLOC_N(mediaRegistry->disks, n) < 0)
-        goto cleanup;
+    if (n)
+        mediaRegistry->disks = g_new0(virVBoxSnapshotConfHardDiskPtr, n);
     mediaRegistry->ndisks = n;
     for (i = 0; i < mediaRegistry->ndisks; i++) {
         mediaRegistry->disks[i] = virVBoxSnapshotConfCreateVBoxSnapshotConfHardDiskPtr(nodes[i],
@@ -154,8 +153,9 @@ virVBoxSnapshotConfRetrieveMediaRegistry(xmlNodePtr mediaRegistryNode,
                                                  xPathContext, &nodes);
     if (n < 0)
         goto cleanup;
-    if (n && VIR_ALLOC_N(mediaRegistry->otherMedia, n) < 0)
-        goto cleanup;
+    if (n)
+        mediaRegistry->otherMedia = g_new0(char *, n);
+
     mediaRegistry->notherMedia = n;
     for (i = 0; i < mediaRegistry->notherMedia; i++) {
         mediaRegistry->otherMedia[i] = virXMLNodeToString(mediaRegistryNode->doc,
@@ -190,8 +190,7 @@ virVBoxSnapshotConfRetrieveSnapshot(xmlNodePtr snapshotNode,
     int result = -1;
     int n = 0;
 
-    if (VIR_ALLOC(snapshot) < 0)
-        goto cleanup;
+    snapshot = g_new0(virVBoxSnapshotConfSnapshot, 1);
 
     uuid = virXMLPropString(snapshotNode, "uuid");
     /* we use virStringSearch because the uuid is between brackets */
@@ -248,8 +247,8 @@ virVBoxSnapshotConfRetrieveSnapshot(xmlNodePtr snapshotNode,
         n = virXPathNodeSet("./vbox:Snapshot", xPathContext, &nodes);
         if (n < 0)
             goto cleanup;
-        if (n && VIR_ALLOC_N(snapshot->children, n) < 0)
-            goto cleanup;
+        if (n)
+            snapshot->children = g_new0(virVBoxSnapshotConfSnapshotPtr, n);
         snapshot->nchildren = n;
         for (i = 0; i < snapshot->nchildren; i++) {
             snapshot->children[i] = virVBoxSnapshotConfRetrieveSnapshot(nodes[i], xPathContext);
@@ -469,8 +468,7 @@ virVBoxSnapshotConfAllChildren(virVBoxSnapshotConfHardDiskPtr disk,
     size_t i = 0;
     size_t j = 0;
 
-    if (VIR_ALLOC_N(ret, 0) < 0)
-        return 0;
+    ret = g_new0(virVBoxSnapshotConfHardDiskPtr, 0);
 
     for (i = 0; i < disk->nchildren; i++) {
         tempSize = virVBoxSnapshotConfAllChildren(disk->children[i], &tempList);
@@ -601,8 +599,7 @@ virVBoxSnapshotConfLoadVboxFile(const char *filePath,
         goto cleanup;
     }
 
-    if (VIR_ALLOC(machineDescription) < 0)
-        goto cleanup;
+    machineDescription = g_new0(virVBoxSnapshotConfMachine, 1);
 
     xml = virXMLParse(filePath, NULL, NULL);
     if (xml == NULL) {
@@ -1283,8 +1280,7 @@ virVBoxSnapshotConfGetRWDisksPathsFromLibvirtXML(const char *filePath,
                                     xPathContext, &nodes)) < 0)
         goto cleanup;
 
-    if (VIR_ALLOC_N(ret, nodeSize) < 0)
-        goto cleanup;
+    ret = g_new0(char *, nodeSize);
 
     for (i = 0; i < nodeSize; i++) {
         xmlNodePtr node = nodes[i];
@@ -1346,8 +1342,7 @@ virVBoxSnapshotConfGetRODisksPathsFromLibvirtXML(const char *filePath,
                                     xPathContext,
                                     &nodes)) < 0)
         goto cleanup;
-    if (VIR_ALLOC_N(ret, nodeSize) < 0)
-        goto cleanup;
+    ret = g_new0(char *, nodeSize);
 
     for (i = 0; i < nodeSize; i++) {
         xmlNodePtr node = nodes[i];
@@ -1414,8 +1409,7 @@ virVBoxSnapshotConfDiskListToOpen(virVBoxSnapshotConfMachinePtr machine,
     }
     if (hardDisk == NULL)
         return 0;
-    if (VIR_ALLOC_N(ret, 1) < 0)
-        return 0;
+    ret = g_new0(virVBoxSnapshotConfHardDiskPtr, 1);
 
     returnSize = 1;
     ret[returnSize - 1] = hardDisk;
@@ -1446,8 +1440,7 @@ virVBoxSnapshotConfRemoveFakeDisks(virVBoxSnapshotConfMachinePtr machine)
     virVBoxSnapshotConfHardDiskPtr *tempList = NULL;
     virVBoxSnapshotConfHardDiskPtr *diskList = NULL;
 
-    if (VIR_ALLOC_N(diskList, 0) < 0)
-        return -1;
+    diskList = g_new0(virVBoxSnapshotConfHardDiskPtr, 0);
 
     for (i = 0; i < machine->mediaRegistry->ndisks; i++) {
         tempSize = virVBoxSnapshotConfAllChildren(machine->mediaRegistry->disks[i], &tempList);
@@ -1498,8 +1491,7 @@ virVBoxSnapshotConfDiskIsInMediaRegistry(virVBoxSnapshotConfMachinePtr machine,
     virVBoxSnapshotConfHardDiskPtr *tempList = NULL;
     virVBoxSnapshotConfHardDiskPtr *diskList = NULL;
 
-    if (VIR_ALLOC_N(diskList, 0) < 0)
-        return -1;
+    diskList = g_new0(virVBoxSnapshotConfHardDiskPtr, 0);
 
     for (i = 0; i < machine->mediaRegistry->ndisks; i++) {
         tempSize = virVBoxSnapshotConfAllChildren(machine->mediaRegistry->disks[i], &tempList);
-- 
2.26.2




More information about the libvir-list mailing list