]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: a-n: use g_new0
authorJán Tomko <jtomko@redhat.com>
Mon, 5 Oct 2020 17:12:37 +0000 (19:12 +0200)
committerJán Tomko <jtomko@redhat.com>
Tue, 6 Oct 2020 10:31:34 +0000 (12:31 +0200)
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
18 files changed:
src/util/iohelper.c
src/util/virarptable.c
src/util/virauthconfig.c
src/util/virbitmap.c
src/util/vircgroup.c
src/util/vircrypto.c
src/util/virdnsmasq.c
src/util/virebtables.c
src/util/virfile.c
src/util/virfirmware.c
src/util/virhash.c
src/util/virhostcpu.c
src/util/virjson.c
src/util/virlockspace.c
src/util/virlog.c
src/util/virmdev.c
src/util/virnetlink.c
src/util/virnuma.c

index c3eb6f8c5445c38667665dd6986aa14b46b4112f..49d939d290113d893fd4775d17d6657a2ce1af4d 100644 (file)
@@ -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
index c4b46604a9fbecaea74907a228ccaf0325b5ca63..01a27c0093e286fc092fdf18cab7282ac814c90f 100644 (file)
@@ -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;
 
index 2e50609531edae13211971cef4c285e20b903e57..6b5487c4b0a35f4ffcd9ddaa96aa796781ee0395 100644 (file)
@@ -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);
 
index 319bd7b7cea6d02cd4a153c32a2ff2902a98a144..4fde6f4fd2613fc76fa6028805c32974a96520da 100644 (file)
@@ -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);
index 08758b530601f37872b0ccb55014e3388e6b73f8..5c0543c66a4807a95670399e623f62c6a2b1ae87 100644 (file)
@@ -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;
index 90aed32c53e526ee2f0327c17bc703e2f40e0ec0..c4874550afd93da6eaf63ec6c9b277b766d8e434 100644 (file)
@@ -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
index 44aa7ad95d16263650a6b07071b8df82e369de9d..9030f9218ad2a1523317df882356e3e02dadd069 100644 (file)
@@ -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);
 
index 1887d718a378fcd918dae58b62464304f3ca8695..b23ce21ae963fe949c41f7ac644edfb5d113c21b 100644 (file)
@@ -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);
 
index e120d277d0983c588845bdd9379eab82f4d67c87..7d0a40b0fb7fc75af2e80e9fc6e834ae65d970e1 100644 (file)
@@ -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)
index 513ec5662124cd31df97c65dcc9db01f1e4c151a..6b8a64eafa007d3111c7aa79cd96ce1f82963438 100644 (file)
@@ -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]);
         }
index abec757d9909dd44a77968f4b01daedb017a4aaa..4b503a03132a89fa85d5ab8e972089b50240f214 100644 (file)
@@ -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);
 
index 28f032b9728c517b6f36b6b10a3603b71c98e757..4ae4dca0678a6040dc7511ec9af24a8f0a15cdde 100644 (file)
@@ -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;
 
index a4fc9e990e278fce2dcfdd463b35424efb1be8da..3fa9a9ca24f64776b742e423dda6ce0225b0b6c0 100644 (file)
@@ -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;
 
index 9a54192cd18b2487d24cd0f234d7559b43df8e1e..ef370936a3aa9940928037bf1b9e7ecf88e84514 100644 (file)
@@ -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++) {
index 285c130d36caa50d8c9468f14de9a195ed70c6e5..6b7a4512e93f7b7f7449d0aaa98cd72f91393eb4 100644 (file)
@@ -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;
index 26cb8300ff4dcff12ab9901c723ee2bc4d127cce..31994631eda6bd3c98fe255df72a8fca5d7383f1 100644 (file)
@@ -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);
 
index ea06db51ee494c493bbb5629df9cfcddc50853ff..fdd3a6a4d0b3e2df11b63f0a2828d8b99a50ec03 100644 (file)
@@ -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);
index 6728f62a87df079c5b1a4c711ff15abb97905168..5d40d13977356693df7cfdd4156595647d1b4251 100644 (file)
@@ -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++) {