mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-12 15:52:55 +00:00
util: a-n: use g_new0
Signed-off-by: Ján Tomko <jtomko@redhat.com> Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
parent
b5682a1330
commit
e59b8f96f7
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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]);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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++) {
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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++) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user