[libvirt PATCH 11/12] util: a-n: use g_new0

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

 



Signed-off-by: Ján Tomko <jtomko@xxxxxxxxxx>
---
 src/util/iohelper.c      |  3 +--
 src/util/virarptable.c   |  3 +--
 src/util/virauthconfig.c |  6 ++----
 src/util/virbitmap.c     |  4 +---
 src/util/vircgroup.c     |  7 ++-----
 src/util/vircrypto.c     |  6 ++----
 src/util/virdnsmasq.c    | 12 ++++--------
 src/util/virebtables.c   |  3 +--
 src/util/virfile.c       | 12 +++---------
 src/util/virfirmware.c   |  6 ++----
 src/util/virhash.c       |  8 ++------
 src/util/virhostcpu.c    |  9 +++------
 src/util/virjson.c       | 15 +++++----------
 src/util/virlockspace.c  | 17 +++++------------
 src/util/virlog.c        |  5 +----
 src/util/virmdev.c       |  6 ++----
 src/util/virnetlink.c    |  3 +--
 src/util/virnuma.c       |  7 ++-----
 18 files changed, 40 insertions(+), 92 deletions(-)

diff --git a/src/util/iohelper.c b/src/util/iohelper.c
index c3eb6f8c54..49d939d290 100644
--- a/src/util/iohelper.c
+++ b/src/util/iohelper.c
@@ -63,8 +63,7 @@ runIO(const char *path, int fd, int oflags)
     }
     buf = base;
 #else
-    if (VIR_ALLOC_N(buf, buflen + alignMask) < 0)
-        goto cleanup;
+    buf = g_new0(char, buflen + alignMask);
     base = buf;
     buf = (char *) (((intptr_t) base + alignMask) & ~alignMask);
 #endif
diff --git a/src/util/virarptable.c b/src/util/virarptable.c
index c4b46604a9..01a27c0093 100644
--- a/src/util/virarptable.c
+++ b/src/util/virarptable.c
@@ -74,8 +74,7 @@ virArpTableGet(void)
     if (msglen < 0)
         return NULL;
 
-    if (VIR_ALLOC(table) < 0)
-        return NULL;
+    table = g_new0(virArpTable, 1);
 
     nh = (struct nlmsghdr*)nlData;
 
diff --git a/src/util/virauthconfig.c b/src/util/virauthconfig.c
index 2e50609531..6b5487c4b0 100644
--- a/src/util/virauthconfig.c
+++ b/src/util/virauthconfig.c
@@ -40,8 +40,7 @@ virAuthConfigPtr virAuthConfigNew(const char *path)
 {
     virAuthConfigPtr auth;
 
-    if (VIR_ALLOC(auth) < 0)
-        goto error;
+    auth = g_new0(virAuthConfig, 1);
 
     auth->path = g_strdup(path);
 
@@ -65,8 +64,7 @@ virAuthConfigPtr virAuthConfigNewData(const char *path,
 {
     virAuthConfigPtr auth;
 
-    if (VIR_ALLOC(auth) < 0)
-        goto error;
+    auth = g_new0(virAuthConfig, 1);
 
     auth->path = g_strdup(path);
 
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index 319bd7b7ce..4fde6f4fd2 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -704,9 +704,7 @@ virBitmapToData(virBitmapPtr bitmap,
     else
         len = (len + CHAR_BIT) / CHAR_BIT;
 
-    if (VIR_ALLOC_N(*data, len) < 0)
-        return -1;
-
+    *data = g_new0(unsigned char, len);
     *dataLen = len;
 
     virBitmapToDataBuf(bitmap, *data, *dataLen);
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 08758b5306..5c0543c66a 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -664,10 +664,8 @@ virCgroupNew(pid_t pid,
 {
     VIR_DEBUG("pid=%lld path=%s parent=%p controllers=%d group=%p",
               (long long) pid, path, parent, controllers, group);
-    *group = NULL;
 
-    if (VIR_ALLOC((*group)) < 0)
-        goto error;
+    *group = g_new0(virCgroup, 1);
 
     if (path[0] == '/' || !parent) {
         (*group)->path = g_strdup(path);
@@ -2170,8 +2168,7 @@ virCgroupGetPercpuStats(virCgroupPtr group,
     param_idx = 1;
 
     if (guestvcpus && param_idx < nparams) {
-        if (VIR_ALLOC_N(sum_cpu_time, need_cpus) < 0)
-            goto cleanup;
+        sum_cpu_time = g_new0(unsigned long long, need_cpus);
         if (virCgroupGetPercpuVcpuSum(group, guestvcpus, sum_cpu_time,
                                       need_cpus, cpumap) < 0)
             goto cleanup;
diff --git a/src/util/vircrypto.c b/src/util/vircrypto.c
index 90aed32c53..c4874550af 100644
--- a/src/util/vircrypto.c
+++ b/src/util/vircrypto.c
@@ -88,8 +88,7 @@ virCryptoHashString(virCryptoHash hash,
 
     hashstrlen = (rc * 2) + 1;
 
-    if (VIR_ALLOC_N(*output, hashstrlen) < 0)
-        return -1;
+    *output = g_new0(char, hashstrlen);
 
     for (i = 0; i < rc; i++) {
         (*output)[i * 2] = hex[(buf[i] >> 4) & 0xf];
@@ -167,8 +166,7 @@ virCryptoEncryptDataAESgnutls(gnutls_cipher_algorithm_t gnutls_enc_alg,
      * data from non-padded data. Hence datalen + 1
      */
     ciphertextlen = VIR_ROUND_UP(datalen + 1, 16);
-    if (VIR_ALLOC_N(ciphertext, ciphertextlen) < 0)
-        return -1;
+    ciphertext = g_new0(uint8_t, ciphertextlen);
     memcpy(ciphertext, data, datalen);
 
      /* Fill in the padding of the buffer with the size of the padding
diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c
index 44aa7ad95d..9030f9218a 100644
--- a/src/util/virdnsmasq.c
+++ b/src/util/virdnsmasq.c
@@ -109,8 +109,7 @@ addnhostsAdd(dnsmasqAddnHostsfile *addnhostsfile,
             goto error;
 
         idx = addnhostsfile->nhosts;
-        if (VIR_ALLOC(addnhostsfile->hosts[idx].hostnames) < 0)
-            goto error;
+        addnhostsfile->hosts[idx].hostnames = g_new0(char *, 1);
 
         addnhostsfile->hosts[idx].ip = g_strdup(ipstr);
 
@@ -141,8 +140,7 @@ addnhostsNew(const char *name,
     dnsmasqAddnHostsfile *addnhostsfile;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
-    if (VIR_ALLOC(addnhostsfile) < 0)
-        return NULL;
+    addnhostsfile = g_new0(dnsmasqAddnHostsfile, 1);
 
     addnhostsfile->hosts = NULL;
     addnhostsfile->nhosts = 0;
@@ -343,8 +341,7 @@ hostsfileNew(const char *name,
     dnsmasqHostsfile *hostsfile;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
-    if (VIR_ALLOC(hostsfile) < 0)
-        return NULL;
+    hostsfile = g_new0(dnsmasqHostsfile, 1);
 
     hostsfile->hosts = NULL;
     hostsfile->nhosts = 0;
@@ -444,8 +441,7 @@ dnsmasqContextNew(const char *network_name,
 {
     dnsmasqContext *ctx;
 
-    if (VIR_ALLOC(ctx) < 0)
-        return NULL;
+    ctx = g_new0(dnsmasqContext, 1);
 
     ctx->config_dir = g_strdup(config_dir);
 
diff --git a/src/util/virebtables.c b/src/util/virebtables.c
index 1887d718a3..b23ce21ae9 100644
--- a/src/util/virebtables.c
+++ b/src/util/virebtables.c
@@ -55,8 +55,7 @@ ebtablesContextNew(const char *driver)
 {
     ebtablesContext *ctx = NULL;
 
-    if (VIR_ALLOC(ctx) < 0)
-        return NULL;
+    ctx = g_new0(ebtablesContext, 1);
 
     ctx->chain = g_strdup_printf("libvirt_%s_FORWARD", driver);
 
diff --git a/src/util/virfile.c b/src/util/virfile.c
index e120d277d0..7d0a40b0fb 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -258,8 +258,7 @@ virFileWrapperFdNew(int *fd, const char *name, unsigned int flags)
         return NULL;
     }
 
-    if (VIR_ALLOC(ret) < 0)
-        return NULL;
+    ret = g_new0(virFileWrapperFd, 1);
 
     mode = fcntl(*fd, F_GETFL);
 
@@ -1178,11 +1177,7 @@ safezero_slow(int fd, off_t offset, off_t len)
     remain = len;
     bytes = MIN(1024 * 1024, len);
 
-    r = VIR_ALLOC_N(buf, bytes);
-    if (r < 0) {
-        errno = ENOMEM;
-        return -1;
-    }
+    buf = g_new0(char, bytes);
 
     while (remain) {
         if (bytes > remain)
@@ -3160,8 +3155,7 @@ virFileOpenTty(int *ttyprimary, char **ttyName, int rawmode)
         size_t len = 64;
         int rc;
 
-        if (VIR_ALLOC_N(name, len) < 0)
-            goto cleanup;
+        name = g_new0(char, len);
 
         while ((rc = ttyname_r(secondary, name, len)) == ERANGE) {
             if (VIR_RESIZE_N(name, len, len, len) < 0)
diff --git a/src/util/virfirmware.c b/src/util/virfirmware.c
index 513ec56621..6b8a64eafa 100644
--- a/src/util/virfirmware.c
+++ b/src/util/virfirmware.c
@@ -112,15 +112,13 @@ virFirmwareParseList(const char *list,
     }
 
     if (i) {
-        if (VIR_ALLOC_N(*firmwares, i / 2) < 0)
-            goto cleanup;
+        *firmwares = g_new0(virFirmwarePtr, i / 2);
         *nfirmwares = i / 2;
 
         for (j = 0; j < i / 2; j++) {
             virFirmwarePtr *fws = *firmwares;
 
-            if (VIR_ALLOC(fws[j]) < 0)
-                goto cleanup;
+            fws[j] = g_new0(virFirmware, 1);
             fws[j]->name = g_strdup(token[2 * j]);
             fws[j]->nvram = g_strdup(token[2 * j + 1]);
         }
diff --git a/src/util/virhash.c b/src/util/virhash.c
index abec757d99..4b503a0313 100644
--- a/src/util/virhash.c
+++ b/src/util/virhash.c
@@ -271,10 +271,7 @@ virHashGrow(virHashTablePtr table, size_t size)
     if (oldtable == NULL)
         return -1;
 
-    if (VIR_ALLOC_N(table->table, size) < 0) {
-        table->table = oldtable;
-        return -1;
-    }
+    table->table = g_new0(virHashEntryPtr, size);
     table->size = size;
 
     for (i = 0; i < oldsize; i++) {
@@ -792,8 +789,7 @@ virHashKeyValuePairPtr virHashGetItems(virHashTablePtr table,
     if (numElems < 0)
         return NULL;
 
-    if (VIR_ALLOC_N(iter.sortArray, numElems + 1))
-        return NULL;
+    iter.sortArray = g_new0(virHashKeyValuePair, numElems + 1);
 
     virHashForEach(table, virHashGetKeysIterator, &iter);
 
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index 28f032b972..4ae4dca067 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -145,8 +145,7 @@ virHostCPUGetStatsFreeBSD(int cpuNum,
 
     cpu_times_size = sizeof(long) * cpu_times_num * CPUSTATES;
 
-    if (VIR_ALLOC_N(cpu_times, cpu_times_num * CPUSTATES) < 0)
-        goto cleanup;
+    cpu_times = g_new0(long, cpu_times_num * CPUSTATES);
 
     if (sysctlbyname(sysctl_name, cpu_times, &cpu_times_size, NULL, 0) < 0) {
         virReportSystemError(errno,
@@ -366,8 +365,7 @@ virHostCPUParseNode(const char *node,
     sock_max++;
 
     /* allocate cores maps for each socket */
-    if (VIR_ALLOC_N(cores_maps, sock_max) < 0)
-        goto cleanup;
+    cores_maps = g_new0(virBitmapPtr, sock_max);
 
     for (i = 0; i < sock_max; i++)
         cores_maps[i] = virBitmapNew(0);
@@ -1385,8 +1383,7 @@ virHostCPUGetTscInfo(void)
         return NULL;
     }
 
-    if (VIR_ALLOC(info) < 0)
-        return NULL;
+    info = g_new0(virHostCPUTscInfo, 1);
 
     info->frequency = rc * 1000ULL;
 
diff --git a/src/util/virjson.c b/src/util/virjson.c
index a4fc9e990e..3fa9a9ca24 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -438,8 +438,7 @@ virJSONValueNewString(const char *data)
     if (!data)
         return virJSONValueNewNull();
 
-    if (VIR_ALLOC(val) < 0)
-        return NULL;
+    val = g_new0(virJSONValue, 1);
 
     val->type = VIR_JSON_TYPE_STRING;
     val->data.string = g_strdup(data);
@@ -457,8 +456,7 @@ virJSONValueNewStringLen(const char *data,
     if (!data)
         return virJSONValueNewNull();
 
-    if (VIR_ALLOC(val) < 0)
-        return NULL;
+    val = g_new0(virJSONValue, 1);
 
     val->type = VIR_JSON_TYPE_STRING;
     val->data.string = g_strndup(data, length);
@@ -472,8 +470,7 @@ virJSONValueNewNumber(const char *data)
 {
     virJSONValuePtr val;
 
-    if (VIR_ALLOC(val) < 0)
-        return NULL;
+    val = g_new0(virJSONValue, 1);
 
     val->type = VIR_JSON_TYPE_NUMBER;
     val->data.number = g_strdup(data);
@@ -533,8 +530,7 @@ virJSONValueNewBoolean(int boolean_)
 {
     virJSONValuePtr val;
 
-    if (VIR_ALLOC(val) < 0)
-        return NULL;
+    val = g_new0(virJSONValue, 1);
 
     val->type = VIR_JSON_TYPE_BOOLEAN;
     val->data.boolean = boolean_;
@@ -548,8 +544,7 @@ virJSONValueNewNull(void)
 {
     virJSONValuePtr val;
 
-    if (VIR_ALLOC(val) < 0)
-        return NULL;
+    val = g_new0(virJSONValue, 1);
 
     val->type = VIR_JSON_TYPE_NULL;
 
diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index 9a54192cd1..ef370936a3 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -119,8 +119,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
     virLockSpaceResourcePtr res;
     bool shared = !!(flags & VIR_LOCK_SPACE_ACQUIRE_SHARED);
 
-    if (VIR_ALLOC(res) < 0)
-        return NULL;
+    res = g_new0(virLockSpaceResource, 1);
 
     res->fd = -1;
     res->flags = flags;
@@ -241,8 +240,7 @@ virLockSpacePtr virLockSpaceNew(const char *directory)
 
     VIR_DEBUG("directory=%s", NULLSTR(directory));
 
-    if (VIR_ALLOC(lockspace) < 0)
-        return NULL;
+    lockspace = g_new0(virLockSpace, 1);
 
     if (virMutexInit(&lockspace->lock) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -292,8 +290,7 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr object)
 
     VIR_DEBUG("object=%p", object);
 
-    if (VIR_ALLOC(lockspace) < 0)
-        return NULL;
+    lockspace = g_new0(virLockSpace, 1);
 
     if (virMutexInit(&lockspace->lock) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -331,8 +328,7 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr object)
         size_t j;
         size_t m;
 
-        if (VIR_ALLOC(res) < 0)
-            goto error;
+        res = g_new0(virLockSpaceResource, 1);
         res->fd = -1;
 
         if (!(tmp = virJSONValueObjectGetString(child, "name"))) {
@@ -391,10 +387,7 @@ virLockSpacePtr virLockSpaceNewPostExecRestart(virJSONValuePtr object)
         }
 
         m = virJSONValueArraySize(owners);
-        if (VIR_ALLOC_N(res->owners, res->nOwners) < 0) {
-            virLockSpaceResourceFree(res);
-            goto error;
-        }
+        res->owners = g_new0(pid_t, res->nOwners);
         res->nOwners = m;
 
         for (j = 0; j < res->nOwners; j++) {
diff --git a/src/util/virlog.c b/src/util/virlog.c
index 285c130d36..6b7a4512e9 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -1271,10 +1271,7 @@ virLogOutputNew(virLogOutputFunc f,
         ndup = g_strdup(name);
     }
 
-    if (VIR_ALLOC(ret) < 0) {
-        VIR_FREE(ndup);
-        return NULL;
-    }
+    ret = g_new0(virLogOutput, 1);
 
     ret->logInitMessage = true;
     ret->f = f;
diff --git a/src/util/virmdev.c b/src/util/virmdev.c
index 26cb8300ff..31994631ed 100644
--- a/src/util/virmdev.c
+++ b/src/util/virmdev.c
@@ -150,8 +150,7 @@ virMediatedDeviceNew(const char *uuidstr, virMediatedDeviceModelType model)
         return NULL;
     }
 
-    if (VIR_ALLOC(dev) < 0)
-        return NULL;
+    dev = g_new0(virMediatedDevice, 1);
 
     dev->path = g_steal_pointer(&sysfspath);
 
@@ -494,8 +493,7 @@ virMediatedDeviceTypeReadAttrs(const char *sysfspath,
         } \
     } while (0)
 
-    if (VIR_ALLOC(tmp) < 0)
-        return -1;
+    tmp = g_new0(virMediatedDeviceType, 1);
 
     tmp->id = g_path_get_basename(sysfspath);
 
diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c
index ea06db51ee..fdd3a6a4d0 100644
--- a/src/util/virnetlink.c
+++ b/src/util/virnetlink.c
@@ -1018,8 +1018,7 @@ virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
 
     VIR_INFO("starting netlink event service with protocol %d", protocol);
 
-    if (VIR_ALLOC(srv) < 0)
-        return -1;
+    srv = g_new0(virNetlinkEventSrvPrivate, 1);
 
     if (virMutexInit(&srv->lock) < 0) {
         VIR_FREE(srv);
diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index 6728f62a87..5d40d13977 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -265,8 +265,7 @@ virNumaGetNodeCPUs(int node,
         return -2;
     }
 
-    if (VIR_ALLOC_N(mask, mask_n_bytes / sizeof(*mask)) < 0)
-        return -1;
+    mask = g_new0(unsigned long, mask_n_bytes / sizeof(*mask));
 
     if (numa_node_to_cpus(node, mask, mask_n_bytes) < 0) {
         VIR_WARN("NUMA topology for cell %d is not available, ignoring", node);
@@ -477,9 +476,7 @@ virNumaGetDistances(int node,
     if ((max_node = virNumaGetMaxNode()) < 0)
         return -1;
 
-    if (VIR_ALLOC_N(*distances, max_node + 1) < 0)
-        return -1;
-
+    *distances = g_new0(int, max_node + 1);
     *ndistances = max_node + 1;
 
     for (i = 0; i <= max_node; i++) {
-- 
2.26.2




[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