1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

viralloc: Report OOM error on failure

Similarly to VIR_STRDUP, we want the OOM error to be reported in
VIR_ALLOC and friends.
This commit is contained in:
Michal Privoznik 2013-06-07 10:37:25 +02:00
parent 1cdaebf237
commit 8290cbbc38
20 changed files with 459 additions and 163 deletions

View File

@ -141,6 +141,7 @@ src/test/test_driver.c
src/uml/uml_conf.c src/uml/uml_conf.c
src/uml/uml_driver.c src/uml/uml_driver.c
src/util/iohelper.c src/util/iohelper.c
src/util/viralloc.c
src/util/viraudit.c src/util/viraudit.c
src/util/virauth.c src/util/virauth.c
src/util/virauthconfig.c src/util/virauthconfig.c

View File

@ -171,7 +171,7 @@ setPyVirTypedParameter(PyObject *info,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(ret, size) < 0) { if (VIR_ALLOC_N_QUIET(ret, size) < 0) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
@ -511,7 +511,7 @@ libvirt_virDomainBlockStatsFlags(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -577,7 +577,7 @@ libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
sumparams = nparams * MIN(ncpus, 128); sumparams = nparams * MIN(ncpus, 128);
if (VIR_ALLOC_N(params, sumparams) < 0) { if (VIR_ALLOC_N_QUIET(params, sumparams) < 0) {
error = PyErr_NoMemory(); error = PyErr_NoMemory();
goto error; goto error;
} }
@ -629,7 +629,7 @@ libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
if (nparams) { if (nparams) {
sumparams = nparams; sumparams = nparams;
if (VIR_ALLOC_N(params, nparams) < 0) { if (VIR_ALLOC_N_QUIET(params, nparams) < 0) {
error = PyErr_NoMemory(); error = PyErr_NoMemory();
goto error; goto error;
} }
@ -809,7 +809,7 @@ libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -857,7 +857,7 @@ libvirt_virDomainGetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -917,7 +917,7 @@ libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -993,7 +993,7 @@ libvirt_virDomainSetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1067,7 +1067,7 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1127,7 +1127,7 @@ libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1187,7 +1187,7 @@ libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1247,7 +1247,7 @@ libvirt_virDomainGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1307,7 +1307,7 @@ libvirt_virDomainSetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1367,7 +1367,7 @@ libvirt_virDomainGetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1428,7 +1428,7 @@ libvirt_virDomainSetInterfaceParameters(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1489,7 +1489,7 @@ libvirt_virDomainGetInterfaceParameters(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1535,12 +1535,12 @@ libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
if (i_retval < 0) if (i_retval < 0)
return VIR_PY_INT_FAIL; return VIR_PY_INT_FAIL;
if (VIR_ALLOC_N(cpuinfo, dominfo.nrVirtCpu) < 0) if (VIR_ALLOC_N_QUIET(cpuinfo, dominfo.nrVirtCpu) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
cpumaplen = VIR_CPU_MAPLEN(cpunum); cpumaplen = VIR_CPU_MAPLEN(cpunum);
if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) || if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) ||
VIR_ALLOC_N(cpumap, dominfo.nrVirtCpu * cpumaplen) < 0) { VIR_ALLOC_N_QUIET(cpumap, dominfo.nrVirtCpu * cpumaplen) < 0) {
error = PyErr_NoMemory(); error = PyErr_NoMemory();
goto cleanup; goto cleanup;
} }
@ -1662,7 +1662,7 @@ libvirt_virDomainPinVcpu(PyObject *self ATTRIBUTE_UNUSED,
} }
cpumaplen = VIR_CPU_MAPLEN(cpunum); cpumaplen = VIR_CPU_MAPLEN(cpunum);
if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
for (i = 0; i < tuple_size; i++) { for (i = 0; i < tuple_size; i++) {
@ -1726,7 +1726,7 @@ libvirt_virDomainPinVcpuFlags(PyObject *self ATTRIBUTE_UNUSED,
} }
cpumaplen = VIR_CPU_MAPLEN(cpunum); cpumaplen = VIR_CPU_MAPLEN(cpunum);
if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
for (i = 0; i < tuple_size; i++) { for (i = 0; i < tuple_size; i++) {
@ -1786,7 +1786,7 @@ libvirt_virDomainGetVcpuPinInfo(PyObject *self ATTRIBUTE_UNUSED,
cpumaplen = VIR_CPU_MAPLEN(cpunum); cpumaplen = VIR_CPU_MAPLEN(cpunum);
if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) || if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) ||
VIR_ALLOC_N(cpumaps, dominfo.nrVirtCpu * cpumaplen) < 0) VIR_ALLOC_N_QUIET(cpumaps, dominfo.nrVirtCpu * cpumaplen) < 0)
goto cleanup; goto cleanup;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -1854,7 +1854,7 @@ libvirt_virDomainPinEmulator(PyObject *self ATTRIBUTE_UNUSED,
if ((tuple_size = PyTuple_Size(pycpumap)) == -1) if ((tuple_size = PyTuple_Size(pycpumap)) == -1)
return NULL; return NULL;
if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
for (i = 0; i < tuple_size; i++) { for (i = 0; i < tuple_size; i++) {
@ -1913,7 +1913,7 @@ libvirt_virDomainGetEmulatorPinInfo(PyObject *self ATTRIBUTE_UNUSED,
cpumaplen = VIR_CPU_MAPLEN(cpunum); cpumaplen = VIR_CPU_MAPLEN(cpunum);
if (VIR_ALLOC_N(cpumap, cpumaplen) < 0) if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -2188,7 +2188,7 @@ libvirt_virConnectOpenAuth(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
auth.ncredtype = PyList_Size(pycredtype); auth.ncredtype = PyList_Size(pycredtype);
if (auth.ncredtype) { if (auth.ncredtype) {
int i; int i;
if (VIR_ALLOC_N(auth.credtype, auth.ncredtype) < 0) if (VIR_ALLOC_N_QUIET(auth.credtype, auth.ncredtype) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
for (i = 0; i < auth.ncredtype; i++) { for (i = 0; i < auth.ncredtype; i++) {
PyObject *val; PyObject *val;
@ -2316,7 +2316,7 @@ libvirt_virConnectListDomainsID(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(ids, c_retval) < 0) if (VIR_ALLOC_N_QUIET(ids, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -2407,7 +2407,7 @@ libvirt_virConnectListDefinedDomains(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedDomains(conn, names, c_retval); c_retval = virConnectListDefinedDomains(conn, names, c_retval);
@ -2454,7 +2454,7 @@ libvirt_virDomainSnapshotListNames(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virDomainSnapshotListNames(dom, names, c_retval, flags); c_retval = virDomainSnapshotListNames(dom, names, c_retval, flags);
@ -2554,7 +2554,7 @@ libvirt_virDomainSnapshotListChildrenNames(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virDomainSnapshotListChildrenNames(snap, names, c_retval, c_retval = virDomainSnapshotListChildrenNames(snap, names, c_retval,
@ -2886,7 +2886,7 @@ libvirt_virConnectListNetworks(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListNetworks(conn, names, c_retval); c_retval = virConnectListNetworks(conn, names, c_retval);
@ -2931,7 +2931,7 @@ libvirt_virConnectListDefinedNetworks(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedNetworks(conn, names, c_retval); c_retval = virConnectListDefinedNetworks(conn, names, c_retval);
@ -3139,7 +3139,7 @@ libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *arg
return VIR_PY_NONE; return VIR_PY_NONE;
conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn); conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
if (VIR_ALLOC_N(freeMems, maxCells) < 0) if (VIR_ALLOC_N_QUIET(freeMems, maxCells) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -3183,7 +3183,7 @@ libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
return VIR_PY_NONE; return VIR_PY_NONE;
if (nparams) { if (nparams) {
if (VIR_ALLOC_N(stats, nparams) < 0) if (VIR_ALLOC_N_QUIET(stats, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -3246,7 +3246,7 @@ libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
return VIR_PY_NONE; return VIR_PY_NONE;
if (nparams) { if (nparams) {
if (VIR_ALLOC_N(stats, nparams) < 0) if (VIR_ALLOC_N_QUIET(stats, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -3306,7 +3306,7 @@ libvirt_virConnectListStoragePools(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListStoragePools(conn, names, c_retval); c_retval = virConnectListStoragePools(conn, names, c_retval);
@ -3359,7 +3359,7 @@ libvirt_virConnectListDefinedStoragePools(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedStoragePools(conn, names, c_retval); c_retval = virConnectListDefinedStoragePools(conn, names, c_retval);
@ -3458,7 +3458,7 @@ libvirt_virStoragePoolListVolumes(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virStoragePoolListVolumes(pool, names, c_retval); c_retval = virStoragePoolListVolumes(pool, names, c_retval);
@ -3719,7 +3719,7 @@ libvirt_virNodeListDevices(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virNodeListDevices(conn, cap, names, c_retval, flags); c_retval = virNodeListDevices(conn, cap, names, c_retval, flags);
@ -3809,7 +3809,7 @@ libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virNodeDeviceListCaps(dev, names, c_retval); c_retval = virNodeDeviceListCaps(dev, names, c_retval);
@ -3928,7 +3928,7 @@ libvirt_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(uuids, c_retval) < 0) if (VIR_ALLOC_N_QUIET(uuids, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListSecrets(conn, uuids, c_retval); c_retval = virConnectListSecrets(conn, uuids, c_retval);
@ -4147,7 +4147,7 @@ libvirt_virConnectListNWFilters(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(uuids, c_retval) < 0) if (VIR_ALLOC_N_QUIET(uuids, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListNWFilters(conn, uuids, c_retval); c_retval = virConnectListNWFilters(conn, uuids, c_retval);
@ -4238,7 +4238,7 @@ libvirt_virConnectListInterfaces(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListInterfaces(conn, names, c_retval); c_retval = virConnectListInterfaces(conn, names, c_retval);
@ -4292,7 +4292,7 @@ libvirt_virConnectListDefinedInterfaces(PyObject *self ATTRIBUTE_UNUSED,
return VIR_PY_NONE; return VIR_PY_NONE;
if (c_retval) { if (c_retval) {
if (VIR_ALLOC_N(names, c_retval) < 0) if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedInterfaces(conn, names, c_retval); c_retval = virConnectListDefinedInterfaces(conn, names, c_retval);
@ -4392,7 +4392,7 @@ libvirt_virConnectBaselineCPU(PyObject *self ATTRIBUTE_UNUSED,
int i; int i;
ncpus = PyList_Size(list); ncpus = PyList_Size(list);
if (VIR_ALLOC_N(xmlcpus, ncpus) < 0) if (VIR_ALLOC_N_QUIET(xmlcpus, ncpus) < 0)
return VIR_PY_INT_FAIL; return VIR_PY_INT_FAIL;
for (i = 0; i < ncpus; i++) { for (i = 0; i < ncpus; i++) {
@ -4578,7 +4578,7 @@ libvirt_virDomainSetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -4639,7 +4639,7 @@ libvirt_virDomainGetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -4682,7 +4682,7 @@ libvirt_virDomainGetDiskErrors(PyObject *self ATTRIBUTE_UNUSED,
ndisks = count; ndisks = count;
if (ndisks) { if (ndisks) {
if (VIR_ALLOC_N(disks, ndisks) < 0) if (VIR_ALLOC_N_QUIET(disks, ndisks) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -6503,7 +6503,7 @@ libvirt_virStreamRecv(PyObject *self ATTRIBUTE_UNUSED,
} }
stream = PyvirStream_Get(pyobj_stream); stream = PyvirStream_Get(pyobj_stream);
if (VIR_ALLOC_N(buf, nbytes+1 > 0 ? nbytes+1 : 1) < 0) if (VIR_ALLOC_N_QUIET(buf, nbytes+1 > 0 ? nbytes+1 : 1) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -6729,7 +6729,7 @@ libvirt_virDomainBlockPeek(PyObject *self ATTRIBUTE_UNUSED,
domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain); domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
if (VIR_ALLOC_N(buf, size) < 0) if (VIR_ALLOC_N_QUIET(buf, size) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -6766,7 +6766,7 @@ libvirt_virDomainMemoryPeek(PyObject *self ATTRIBUTE_UNUSED,
domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain); domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
if (VIR_ALLOC_N(buf, size) < 0) if (VIR_ALLOC_N_QUIET(buf, size) < 0)
return VIR_PY_NONE; return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -6826,7 +6826,7 @@ libvirt_virNodeSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
return NULL; return NULL;
} }
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;
@ -6886,7 +6886,7 @@ libvirt_virNodeGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
if (!nparams) if (!nparams)
return PyDict_New(); return PyDict_New();
if (VIR_ALLOC_N(params, nparams) < 0) if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory(); return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS; LIBVIRT_BEGIN_ALLOW_THREADS;

View File

@ -2749,8 +2749,9 @@ virDomainDefPostParseInternal(virDomainDefPtr def,
/* create the serial port definition from the console definition */ /* create the serial port definition from the console definition */
if (def->nserials == 0) { if (def->nserials == 0) {
if (VIR_APPEND_ELEMENT(def->serials, def->nserials, if (VIR_APPEND_ELEMENT_QUIET(def->serials,
def->consoles[0]) < 0) def->nserials,
def->consoles[0]) < 0)
goto no_memory; goto no_memory;
/* modify it to be a serial port */ /* modify it to be a serial port */
@ -10435,7 +10436,7 @@ virDomainDefMaybeAddController(virDomainDefPtr def,
cont->opts.vioserial.vectors = -1; cont->opts.vioserial.vectors = -1;
} }
if (VIR_APPEND_ELEMENT(def->controllers, def->ncontrollers, cont) < 0) { if (VIR_APPEND_ELEMENT_QUIET(def->controllers, def->ncontrollers, cont) < 0) {
VIR_FREE(cont); VIR_FREE(cont);
virReportOOMError(); virReportOOMError();
return -1; return -1;
@ -11898,10 +11899,10 @@ virDomainDefParseXML(xmlDocPtr xml,
ii = 0; ii = 0;
primaryVideo = true; primaryVideo = true;
} }
if (VIR_INSERT_ELEMENT_INPLACE(def->videos, if (VIR_INSERT_ELEMENT_INPLACE_QUIET(def->videos,
ii, ii,
def->nvideos, def->nvideos,
video) < 0) { video) < 0) {
virDomainVideoDefFree(video); virDomainVideoDefFree(video);
goto error; goto error;
} }

View File

@ -3411,10 +3411,10 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDefPtr def,
} }
/* add to beginning/end of list */ /* add to beginning/end of list */
if (VIR_INSERT_ELEMENT(ipdef->hosts, if (VIR_INSERT_ELEMENT_QUIET(ipdef->hosts,
command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
? 0 : ipdef->nhosts, ? 0 : ipdef->nhosts,
ipdef->nhosts, host) < 0) { ipdef->nhosts, host) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -3517,10 +3517,10 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
} }
/* add to beginning/end of list */ /* add to beginning/end of list */
if (VIR_INSERT_ELEMENT(ipdef->ranges, if (VIR_INSERT_ELEMENT_QUIET(ipdef->ranges,
command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
? 0 : ipdef->nranges, ? 0 : ipdef->nranges,
ipdef->nranges, range) < 0) { ipdef->nranges, range) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -3612,10 +3612,10 @@ virNetworkDefUpdateForwardInterface(virNetworkDefPtr def,
} }
/* add to beginning/end of list */ /* add to beginning/end of list */
if (VIR_INSERT_ELEMENT(def->forward.ifs, if (VIR_INSERT_ELEMENT_QUIET(def->forward.ifs,
command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
? 0 : def->forward.nifs, ? 0 : def->forward.nifs,
def->forward.nifs, iface) < 0) { def->forward.nifs, iface) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -3739,10 +3739,10 @@ virNetworkDefUpdatePortGroup(virNetworkDefPtr def,
(command == VIR_NETWORK_UPDATE_COMMAND_ADD_LAST)) { (command == VIR_NETWORK_UPDATE_COMMAND_ADD_LAST)) {
/* add to beginning/end of list */ /* add to beginning/end of list */
if (VIR_INSERT_ELEMENT(def->portGroups, if (VIR_INSERT_ELEMENT_QUIET(def->portGroups,
command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
? 0 : def->nPortGroups, ? 0 : def->nPortGroups,
def->nPortGroups, portgroup) < 0) { def->nPortGroups, portgroup) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -3823,9 +3823,9 @@ virNetworkDefUpdateDNSHost(virNetworkDefPtr def,
} }
/* add to beginning/end of list */ /* add to beginning/end of list */
if (VIR_INSERT_ELEMENT(dns->hosts, if (VIR_INSERT_ELEMENT_QUIET(dns->hosts,
command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
? 0 : dns->nhosts, dns->nhosts, host) < 0) { ? 0 : dns->nhosts, dns->nhosts, host) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -3911,9 +3911,9 @@ virNetworkDefUpdateDNSSrv(virNetworkDefPtr def,
} }
/* add to beginning/end of list */ /* add to beginning/end of list */
if (VIR_INSERT_ELEMENT(dns->srvs, if (VIR_INSERT_ELEMENT_QUIET(dns->srvs,
command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
? 0 : dns->nsrvs, dns->nsrvs, srv) < 0) { ? 0 : dns->nsrvs, dns->nsrvs, srv) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
@ -3993,9 +3993,9 @@ virNetworkDefUpdateDNSTxt(virNetworkDefPtr def,
} }
/* add to beginning/end of list */ /* add to beginning/end of list */
if (VIR_INSERT_ELEMENT(dns->txts, if (VIR_INSERT_ELEMENT_QUIET(dns->txts,
command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
? 0 : dns->ntxts, dns->ntxts, txt) < 0) { ? 0 : dns->ntxts, dns->ntxts, txt) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }

View File

@ -1750,7 +1750,7 @@ esxVI_Alloc(void **ptrptr, size_t size)
return -1; return -1;
} }
if (virAllocN(ptrptr, size, 1) < 0) { if (virAllocN(ptrptr, size, 1, false, 0, NULL, NULL, 0) < 0) {
virReportOOMError(); virReportOOMError();
return -1; return -1;
} }

View File

@ -1000,10 +1000,10 @@ virLXCProcessEnsureRootFS(virDomainObjPtr vm)
VIR_STRDUP(root->dst, "/") < 0) VIR_STRDUP(root->dst, "/") < 0)
goto error; goto error;
if (VIR_INSERT_ELEMENT(vm->def->fss, if (VIR_INSERT_ELEMENT_QUIET(vm->def->fss,
0, 0,
vm->def->nfss, vm->def->nfss,
root) < 0) root) < 0)
goto no_memory; goto no_memory;
return 0; return 0;

View File

@ -487,7 +487,7 @@ int virSecurityManagerGenLabel(virSecurityManagerPtr mgr,
/* The seclabel must be added to @vm prior calling domainGenSecurityLabel /* The seclabel must be added to @vm prior calling domainGenSecurityLabel
* which may require seclabel to be presented already */ * which may require seclabel to be presented already */
if (generated && if (generated &&
VIR_APPEND_ELEMENT(vm->seclabels, vm->nseclabels, seclabel) < 0) { VIR_APPEND_ELEMENT_QUIET(vm->seclabels, vm->nseclabels, seclabel) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }

View File

@ -1224,7 +1224,8 @@ virSecuritySELinuxSetSecurityFileLabel(virDomainDiskDefPtr disk,
if (!disk_seclabel) if (!disk_seclabel)
return -1; return -1;
disk_seclabel->norelabel = true; disk_seclabel->norelabel = true;
if (VIR_APPEND_ELEMENT(disk->seclabels, disk->nseclabels, disk_seclabel) < 0) { if (VIR_APPEND_ELEMENT_QUIET(disk->seclabels, disk->nseclabels,
disk_seclabel) < 0) {
virReportOOMError(); virReportOOMError();
virSecurityDeviceLabelDefFree(disk_seclabel); virSecurityDeviceLabelDefFree(disk_seclabel);
return -1; return -1;

View File

@ -25,6 +25,9 @@
#include "viralloc.h" #include "viralloc.h"
#include "virlog.h" #include "virlog.h"
#include "virerror.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#if TEST_OOM #if TEST_OOM
static int testMallocNext = 0; static int testMallocNext = 0;
@ -105,14 +108,26 @@ void virAllocTestHook(void (*func)(int, void*) ATTRIBUTE_UNUSED,
* virAlloc: * virAlloc:
* @ptrptr: pointer to pointer for address of allocated memory * @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes to allocate * @size: number of bytes to allocate
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Allocate 'size' bytes of memory. Return the address of the * Allocate 'size' bytes of memory. Return the address of the
* allocated memory in 'ptrptr'. The newly allocated memory is * allocated memory in 'ptrptr'. The newly allocated memory is
* filled with zeros. * filled with zeros. If @report is true, OOM errors are
* reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns -1 on failure to allocate, zero on success
*/ */
int virAlloc(void *ptrptr, size_t size) int virAlloc(void *ptrptr,
size_t size,
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
#if TEST_OOM #if TEST_OOM
if (virAllocTestFail()) { if (virAllocTestFail()) {
@ -122,8 +137,11 @@ int virAlloc(void *ptrptr, size_t size)
#endif #endif
*(void **)ptrptr = calloc(1, size); *(void **)ptrptr = calloc(1, size);
if (*(void **)ptrptr == NULL) if (*(void **)ptrptr == NULL) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1; return -1;
}
return 0; return 0;
} }
@ -132,15 +150,28 @@ int virAlloc(void *ptrptr, size_t size)
* @ptrptr: pointer to pointer for address of allocated memory * @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes to allocate * @size: number of bytes to allocate
* @count: number of elements to allocate * @count: number of elements to allocate
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Allocate an array of memory 'count' elements long, * Allocate an array of memory 'count' elements long,
* each with 'size' bytes. Return the address of the * each with 'size' bytes. Return the address of the
* allocated memory in 'ptrptr'. The newly allocated * allocated memory in 'ptrptr'. The newly allocated
* memory is filled with zeros. * memory is filled with zeros. If @report is true,
* OOM errors are reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns -1 on failure to allocate, zero on success
*/ */
int virAllocN(void *ptrptr, size_t size, size_t count) int virAllocN(void *ptrptr,
size_t size,
size_t count,
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
#if TEST_OOM #if TEST_OOM
if (virAllocTestFail()) { if (virAllocTestFail()) {
@ -150,8 +181,11 @@ int virAllocN(void *ptrptr, size_t size, size_t count)
#endif #endif
*(void**)ptrptr = calloc(count, size); *(void**)ptrptr = calloc(count, size);
if (*(void**)ptrptr == NULL) if (*(void**)ptrptr == NULL) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1; return -1;
}
return 0; return 0;
} }
@ -160,16 +194,29 @@ int virAllocN(void *ptrptr, size_t size, size_t count)
* @ptrptr: pointer to pointer for address of allocated memory * @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes to allocate * @size: number of bytes to allocate
* @count: number of elements in array * @count: number of elements in array
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Resize the block of memory in 'ptrptr' to be an array of * Resize the block of memory in 'ptrptr' to be an array of
* 'count' elements, each 'size' bytes in length. Update 'ptrptr' * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
* with the address of the newly allocated memory. On failure, * with the address of the newly allocated memory. On failure,
* 'ptrptr' is not changed and still points to the original memory * 'ptrptr' is not changed and still points to the original memory
* block. Any newly allocated memory in 'ptrptr' is uninitialized. * block. Any newly allocated memory in 'ptrptr' is uninitialized.
* If @report is true, OOM errors are reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns -1 on failure to allocate, zero on success
*/ */
int virReallocN(void *ptrptr, size_t size, size_t count) int virReallocN(void *ptrptr,
size_t size,
size_t count,
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
void *tmp; void *tmp;
#if TEST_OOM #if TEST_OOM
@ -178,12 +225,17 @@ int virReallocN(void *ptrptr, size_t size, size_t count)
#endif #endif
if (xalloc_oversized(count, size)) { if (xalloc_oversized(count, size)) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
tmp = realloc(*(void**)ptrptr, size * count); tmp = realloc(*(void**)ptrptr, size * count);
if (!tmp && (size * count)) if (!tmp && (size * count)) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1; return -1;
}
*(void**)ptrptr = tmp; *(void**)ptrptr = tmp;
return 0; return 0;
} }
@ -194,24 +246,41 @@ int virReallocN(void *ptrptr, size_t size, size_t count)
* @size: number of bytes per element * @size: number of bytes per element
* @countptr: pointer to number of elements in array * @countptr: pointer to number of elements in array
* @add: number of elements to add * @add: number of elements to add
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Resize the block of memory in 'ptrptr' to be an array of * Resize the block of memory in 'ptrptr' to be an array of
* '*countptr' + 'add' elements, each 'size' bytes in length. * '*countptr' + 'add' elements, each 'size' bytes in length.
* Update 'ptrptr' and 'countptr' with the details of the newly * Update 'ptrptr' and 'countptr' with the details of the newly
* allocated memory. On failure, 'ptrptr' and 'countptr' are not * allocated memory. On failure, 'ptrptr' and 'countptr' are not
* changed. Any newly allocated memory in 'ptrptr' is zero-filled. * changed. Any newly allocated memory in 'ptrptr' is zero-filled.
* If @report is true, OOM errors are reported automatically.
* *
* Returns -1 on failure to allocate, zero on success * Returns -1 on failure to allocate, zero on success
*/ */
int virExpandN(void *ptrptr, size_t size, size_t *countptr, size_t add) int virExpandN(void *ptrptr,
size_t size,
size_t *countptr,
size_t add,
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
int ret; int ret;
if (*countptr + add < *countptr) { if (*countptr + add < *countptr) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
ret = virReallocN(ptrptr, size, *countptr + add); ret = virReallocN(ptrptr, size, *countptr + add, report,
domcode, filename, funcname, linenr);
if (ret == 0) { if (ret == 0) {
memset(*(char **)ptrptr + (size * *countptr), 0, size * add); memset(*(char **)ptrptr + (size * *countptr), 0, size * add);
*countptr += add; *countptr += add;
@ -226,22 +295,39 @@ int virExpandN(void *ptrptr, size_t size, size_t *countptr, size_t add)
* @allocptr: pointer to number of elements allocated in array * @allocptr: pointer to number of elements allocated in array
* @count: number of elements currently used in array * @count: number of elements currently used in array
* @add: minimum number of additional elements to support in array * @add: minimum number of additional elements to support in array
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* If 'count' + 'add' is larger than '*allocptr', then resize the * If 'count' + 'add' is larger than '*allocptr', then resize the
* block of memory in 'ptrptr' to be an array of at least 'count' + * block of memory in 'ptrptr' to be an array of at least 'count' +
* 'add' elements, each 'size' bytes in length. Update 'ptrptr' and * 'add' elements, each 'size' bytes in length. Update 'ptrptr' and
* 'allocptr' with the details of the newly allocated memory. On * 'allocptr' with the details of the newly allocated memory. On
* failure, 'ptrptr' and 'allocptr' are not changed. Any newly * failure, 'ptrptr' and 'allocptr' are not changed. Any newly
* allocated memory in 'ptrptr' is zero-filled. * allocated memory in 'ptrptr' is zero-filled. If @report is true,
* OOM errors are reported automatically.
*
* *
* Returns -1 on failure to allocate, zero on success * Returns -1 on failure to allocate, zero on success
*/ */
int virResizeN(void *ptrptr, size_t size, size_t *allocptr, size_t count, int virResizeN(void *ptrptr,
size_t add) size_t size,
size_t *allocptr,
size_t count,
size_t add,
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
size_t delta; size_t delta;
if (count + add < count) { if (count + add < count) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
@ -251,7 +337,8 @@ int virResizeN(void *ptrptr, size_t size, size_t *allocptr, size_t count,
delta = count + add - *allocptr; delta = count + add - *allocptr;
if (delta < *allocptr / 2) if (delta < *allocptr / 2)
delta = *allocptr / 2; delta = *allocptr / 2;
return virExpandN(ptrptr, size, allocptr, delta); return virExpandN(ptrptr, size, allocptr, delta, report,
domcode, filename, funcname, linenr);
} }
/** /**
@ -270,7 +357,8 @@ int virResizeN(void *ptrptr, size_t size, size_t *allocptr, size_t count,
void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove) void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
{ {
if (toremove < *countptr) if (toremove < *countptr)
ignore_value(virReallocN(ptrptr, size, *countptr -= toremove)); ignore_value(virReallocN(ptrptr, size, *countptr -= toremove,
false, 0, NULL, NULL, 0));
else { else {
virFree(ptrptr); virFree(ptrptr);
*countptr = 0; *countptr = 0;
@ -293,6 +381,11 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
* @inPlace: false if we should expand the allocated memory before * @inPlace: false if we should expand the allocated memory before
* moving, true if we should assume someone else *has * moving, true if we should assume someone else *has
* already* done that. * already* done that.
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes * Re-allocate an array of *countptr elements, each sizeof(*ptrptr) bytes
* long, to be *countptr+add elements long, then appropriately move * long, to be *countptr+add elements long, then appropriately move
@ -301,7 +394,8 @@ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
* allocated memory in *ptrptr and the new size in *countptr. If * allocated memory in *ptrptr and the new size in *countptr. If
* newelems is NULL, the new elements at ptrptr[at] are instead filled * newelems is NULL, the new elements at ptrptr[at] are instead filled
* with zero. at must be between [0,*countptr], except that -1 is * with zero. at must be between [0,*countptr], except that -1 is
* treated the same as *countptr for convenience. * treated the same as *countptr for convenience. If @report is true,
* OOM errors are reported automatically.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
@ -309,19 +403,26 @@ int
virInsertElementsN(void *ptrptr, size_t size, size_t at, virInsertElementsN(void *ptrptr, size_t size, size_t at,
size_t *countptr, size_t *countptr,
size_t add, void *newelems, size_t add, void *newelems,
bool clearOriginal, bool inPlace) bool clearOriginal, bool inPlace,
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
if (at == -1) { if (at == -1) {
at = *countptr; at = *countptr;
} else if (at > *countptr) { } else if (at > *countptr) {
VIR_WARN("out of bounds index - count %zu at %zu add %zu", virReportError(VIR_ERR_INTERNAL_ERROR,
*countptr, at, add); _("out of bounds index - count %zu at %zu add %zu"),
*countptr, at, add);
return -1; return -1;
} }
if (inPlace) { if (inPlace) {
*countptr += add; *countptr += add;
} else if (virExpandN(ptrptr, size, countptr, add) < 0) { } else if (virExpandN(ptrptr, size, countptr, add, report,
domcode, filename, funcname, linenr) < 0) {
return -1; return -1;
} }
@ -397,6 +498,11 @@ virDeleteElementsN(void *ptrptr, size_t size, size_t at,
* @struct_size: size of initial struct * @struct_size: size of initial struct
* @element_size: size of array elements * @element_size: size of array elements
* @count: number of array elements to allocate * @count: number of array elements to allocate
* @report: whether to report OOM error, if there is one
* @domcode: error domain code
* @filename: caller's filename
* @funcname: caller's funcname
* @linenr: caller's line number
* *
* Allocate struct_size bytes plus an array of 'count' elements, each * Allocate struct_size bytes plus an array of 'count' elements, each
* of size element_size. This sort of allocation is useful for * of size element_size. This sort of allocation is useful for
@ -405,11 +511,20 @@ virDeleteElementsN(void *ptrptr, size_t size, size_t at,
* The caller of this type of API is expected to know the length of * The caller of this type of API is expected to know the length of
* the array that will be returned and allocate a suitable buffer to * the array that will be returned and allocate a suitable buffer to
* contain the returned data. C99 refers to these variable length * contain the returned data. C99 refers to these variable length
* objects as structs containing flexible array members. * objects as structs containing flexible array members. If @report
* is true, OOM errors are reported automatically.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t count) int virAllocVar(void *ptrptr,
size_t struct_size,
size_t element_size,
size_t count,
bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr)
{ {
size_t alloc_size = 0; size_t alloc_size = 0;
@ -419,14 +534,19 @@ int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t co
#endif #endif
if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size)) { if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size)) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
alloc_size = struct_size + (element_size * count); alloc_size = struct_size + (element_size * count);
*(void **)ptrptr = calloc(1, alloc_size); *(void **)ptrptr = calloc(1, alloc_size);
if (*(void **)ptrptr == NULL) if (*(void **)ptrptr == NULL) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1; return -1;
}
return 0; return 0;
} }

View File

@ -46,30 +46,36 @@
/* Don't call these directly - use the macros below */ /* Don't call these directly - use the macros below */
int virAlloc(void *ptrptr, size_t size) ATTRIBUTE_RETURN_CHECK int virAlloc(void *ptrptr, size_t size, bool report, int domcode,
ATTRIBUTE_NONNULL(1); const char *filename, const char *funcname, size_t linenr)
int virAllocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
ATTRIBUTE_NONNULL(1); int virAllocN(void *ptrptr, size_t size, size_t count, bool report, int domcode,
int virReallocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_NONNULL(1); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virExpandN(void *ptrptr, size_t size, size_t *count, size_t add) int virReallocN(void *ptrptr, size_t size, size_t count, bool report, int domcode,
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virExpandN(void *ptrptr, size_t size, size_t *count, size_t add, bool report,
int domcode, const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int virResizeN(void *ptrptr, size_t size, size_t *alloc, size_t count, int virResizeN(void *ptrptr, size_t size, size_t *alloc, size_t count, size_t desired,
size_t desired) bool report, int domcode, const char *filename,
const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
void virShrinkN(void *ptrptr, size_t size, size_t *count, size_t toremove) void virShrinkN(void *ptrptr, size_t size, size_t *count, size_t toremove)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr, int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
size_t add, void *newelem, size_t add, void *newelem,
bool clearOriginal, bool inPlace) bool clearOriginal, bool inPlace, bool report, int domcode,
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr, int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
size_t toremove, bool inPlace) size_t toremove, bool inPlace)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
int virAllocVar(void *ptrptr, int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t count,
size_t struct_size, bool report, int domcode, const char *filename,
size_t element_size, const char *funcname, size_t linenr)
size_t count) ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1); ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1); void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
/** /**
@ -82,9 +88,24 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success
*/
# define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)), true, VIR_FROM_THIS, \
__FILE__, __FUNCTION__, __LINE__)
/**
* VIR_ALLOC_QUIET:
* @ptr: pointer to hold address of allocated memory
*
* Allocate sizeof(*ptr) bytes of memory and store
* the address of allocated memory in 'ptr'. Fill the
* newly allocated memory with zeros.
*
* This macro is safe to use on arguments with side effects.
*
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
# define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr))) # define VIR_ALLOC_QUIET(ptr) virAlloc(&(ptr), sizeof(*(ptr)), false, 0, NULL, NULL, 0)
/** /**
* VIR_ALLOC_N: * VIR_ALLOC_N:
@ -97,9 +118,26 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success
*/
# define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count), true, \
VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/**
* VIR_ALLOC_N_QUIET:
* @ptr: pointer to hold address of allocated memory
* @count: number of elements to allocate
*
* Allocate an array of 'count' elements, each sizeof(*ptr)
* bytes long and store the address of allocated memory in
* 'ptr'. Fill the newly allocated memory with zeros.
*
* This macro is safe to use on arguments with side effects.
*
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
# define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count)) # define VIR_ALLOC_N_QUIET(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count), \
false, 0, NULL, NULL, 0)
/** /**
* VIR_REALLOC_N: * VIR_REALLOC_N:
@ -112,9 +150,27 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure (with OOM error reported), 0 on success
*/
# define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count), \
true, VIR_FROM_THIS, __FILE__, \
__FUNCTION__, __LINE__)
/**
* VIR_REALLOC_N_QUIET:
* @ptr: pointer to hold address of allocated memory
* @count: number of elements to allocate
*
* Re-allocate an array of 'count' elements, each sizeof(*ptr)
* bytes long and store the address of allocated memory in
* 'ptr'. If 'ptr' grew, the added memory is uninitialized.
*
* This macro is safe to use on arguments with side effects.
*
* Returns -1 on failure, 0 on success * Returns -1 on failure, 0 on success
*/ */
# define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count)) # define VIR_REALLOC_N_QUIET(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count), \
false, 0, NULL, NULL, 0)
/** /**
* VIR_EXPAND_N: * VIR_EXPAND_N:
@ -129,10 +185,29 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure (with OOM error reported), 0 on success
*/ */
# define VIR_EXPAND_N(ptr, count, add) \ # define VIR_EXPAND_N(ptr, count, add) \
virExpandN(&(ptr), sizeof(*(ptr)), &(count), add) virExpandN(&(ptr), sizeof(*(ptr)), &(count), add, true, VIR_FROM_THIS, \
__FILE__, __FUNCTION__, __LINE__)
/**
* VIR_EXPAND_N_QUIET:
* @ptr: pointer to hold address of allocated memory
* @count: variable tracking number of elements currently allocated
* @add: number of elements to add
*
* Re-allocate an array of 'count' elements, each sizeof(*ptr)
* bytes long, to be 'count' + 'add' elements long, then store the
* address of allocated memory in 'ptr' and the new size in 'count'.
* The new elements are filled with zero.
*
* This macro is safe to use on arguments with side effects.
*
* Returns -1 on failure, 0 on success
*/
# define VIR_EXPAND_N_QUIET(ptr, count, add) \
virExpandN(&(ptr), sizeof(*(ptr)), &(count), add, false, 0, NULL, NULL, 0)
/** /**
* VIR_RESIZE_N: * VIR_RESIZE_N:
@ -154,10 +229,37 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure (with OOM error reported), 0 on success
*/ */
# define VIR_RESIZE_N(ptr, alloc, count, add) \ # define VIR_RESIZE_N(ptr, alloc, count, add) \
virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add) virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add, true, \
VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/**
* VIR_RESIZE_N_QUIET:
* @ptr: pointer to hold address of allocated memory
* @alloc: variable tracking number of elements currently allocated
* @count: number of elements currently in use
* @add: minimum number of elements to additionally support
*
* Blindly using VIR_EXPAND_N(array, alloc, 1) in a loop scales
* quadratically, because every iteration must copy contents from
* all prior iterations. But amortized linear scaling can be achieved
* by tracking allocation size separately from the number of used
* elements, and growing geometrically only as needed.
*
* If 'count' + 'add' is larger than 'alloc', then geometrically reallocate
* the array of 'alloc' elements, each sizeof(*ptr) bytes long, and store
* the address of allocated memory in 'ptr' and the new size in 'alloc'.
* The new elements are filled with zero.
*
* This macro is safe to use on arguments with side effects.
*
* Returns -1 on failure, 0 on success
*/
# define VIR_RESIZE_N_QUIET(ptr, alloc, count, add) \
virResizeN(&(ptr), sizeof(*(ptr)), &(alloc), count, add, \
false, 0, NULL, NULL, 0)
/** /**
* VIR_SHRINK_N: * VIR_SHRINK_N:
@ -250,20 +352,42 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* These macros are safe to use on arguments with side effects. * These macros are safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure (with OOM error reported), 0 on success
*/ */
# define VIR_INSERT_ELEMENT(ptr, at, count, newelem) \ # define VIR_INSERT_ELEMENT(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
# define VIR_INSERT_ELEMENT_COPY(ptr, at, count, newelem) \ # define VIR_INSERT_ELEMENT_COPY(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
# define VIR_INSERT_ELEMENT_INPLACE(ptr, at, count, newelem) \ # define VIR_INSERT_ELEMENT_INPLACE(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
# define VIR_INSERT_ELEMENT_COPY_INPLACE(ptr, at, count, newelem) \ # define VIR_INSERT_ELEMENT_COPY_INPLACE(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/* Quiet version of macros above */
# define VIR_INSERT_ELEMENT_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
false, 0, NULL, NULL, 0)
# define VIR_INSERT_ELEMENT_COPY_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
false, 0, NULL, NULL, 0)
# define VIR_INSERT_ELEMENT_INPLACE_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
false, 0, NULL, NULL, 0)
# define VIR_INSERT_ELEMENT_COPY_INPLACE_QUIET(ptr, at, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), at, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
false, 0, NULL, NULL, 0)
/** /**
* VIR_APPEND_ELEMENT: * VIR_APPEND_ELEMENT:
@ -298,21 +422,42 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* These macros are safe to use on arguments with side effects. * These macros are safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure (with OOM error reported), 0 on success
*/ */
# define VIR_APPEND_ELEMENT(ptr, count, newelem) \ # define VIR_APPEND_ELEMENT(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
# define VIR_APPEND_ELEMENT_COPY(ptr, count, newelem) \ # define VIR_APPEND_ELEMENT_COPY(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
# define VIR_APPEND_ELEMENT_INPLACE(ptr, count, newelem) \ # define VIR_APPEND_ELEMENT_INPLACE(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
# define VIR_APPEND_ELEMENT_COPY_INPLACE(ptr, count, newelem) \ # define VIR_APPEND_ELEMENT_COPY_INPLACE(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \ virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true) VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/* Quiet version of macros above */
# define VIR_APPEND_ELEMENT_QUIET(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, false, \
false, 0, NULL, NULL, 0)
# define VIR_APPEND_ELEMENT_COPY_QUIT(Eptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, false, \
false, 0, NULL, NULL, 0)
# define VIR_APPEND_ELEMENT_INPLACE_QUIET(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), true, true, \
false. 0, NULL, NULL, 0)
# define VIR_APPEND_ELEMENT_COPY_INPLACE_QUIET(ptr, count, newelem) \
virInsertElementsN(&(ptr), sizeof(*(ptr)), -1, &(count), \
VIR_TYPEMATCH(ptr, &(newelem)), &(newelem), false, true, \
false, 0, NULL, NULL, 0)
/** /**
* VIR_DELETE_ELEMENT: * VIR_DELETE_ELEMENT:
* @ptr: pointer to array of objects (*not* ptr to ptr) * @ptr: pointer to array of objects (*not* ptr to ptr)
@ -365,10 +510,33 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
* *
* This macro is safe to use on arguments with side effects. * This macro is safe to use on arguments with side effects.
* *
* Returns -1 on failure, 0 on success * Returns -1 on failure (with OOM error reported), 0 on success
*/ */
# define VIR_ALLOC_VAR(ptr, type, count) \ # define VIR_ALLOC_VAR(ptr, type, count) \
virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count)) virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count), true, \
VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
/**
* VIR_ALLOC_VAR_QUIET:
* @ptr: pointer to hold address of allocated memory
* @type: element type of trailing array
* @count: number of array elements to allocate
*
* Allocate sizeof(*ptr) bytes plus an array of 'count' elements, each
* sizeof('type'). This sort of allocation is useful for receiving
* the data of certain ioctls and other APIs which return a struct in
* which the last element is an array of undefined length. The caller
* of this type of API is expected to know the length of the array
* that will be returned and allocate a suitable buffer to contain the
* returned data. C99 refers to these variable length objects as
* structs containing flexible array members.
*
* This macro is safe to use on arguments with side effects.
*
* Returns -1 on failure, 0 on success
*/
# define VIR_ALLOC_VAR_QUIET(ptr, type, count) \
virAllocVar(&(ptr), sizeof(*(ptr)), sizeof(type), (count), false, 0, NULL, NULL, 0)
/** /**
* VIR_FREE: * VIR_FREE:

View File

@ -126,7 +126,7 @@ virBufferGrow(virBufferPtr buf, unsigned int len)
size = buf->use + len + 1000; size = buf->use + len + 1000;
if (VIR_REALLOC_N(buf->content, size) < 0) { if (VIR_REALLOC_N_QUIET(buf->content, size) < 0) {
virBufferSetError(buf, errno); virBufferSetError(buf, errno);
return -1; return -1;
} }
@ -382,7 +382,7 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)
} }
if (xalloc_oversized(6, len) || if (xalloc_oversized(6, len) ||
VIR_ALLOC_N(escaped, 6 * len + 1) < 0) { VIR_ALLOC_N_QUIET(escaped, 6 * len + 1) < 0) {
virBufferSetError(buf, errno); virBufferSetError(buf, errno);
return; return;
} }
@ -499,7 +499,7 @@ virBufferEscape(virBufferPtr buf, char escape, const char *toescape,
} }
if (xalloc_oversized(2, len) || if (xalloc_oversized(2, len) ||
VIR_ALLOC_N(escaped, 2 * len + 1) < 0) { VIR_ALLOC_N_QUIET(escaped, 2 * len + 1) < 0) {
virBufferSetError(buf, errno); virBufferSetError(buf, errno);
return; return;
} }
@ -597,7 +597,7 @@ virBufferEscapeShell(virBufferPtr buf, const char *str)
if (*str) { if (*str) {
len = strlen(str); len = strlen(str);
if (xalloc_oversized(4, len) || if (xalloc_oversized(4, len) ||
VIR_ALLOC_N(escaped, 4 * len + 3) < 0) { VIR_ALLOC_N_QUIET(escaped, 4 * len + 3) < 0) {
virBufferSetError(buf, errno); virBufferSetError(buf, errno);
return; return;
} }

View File

@ -1171,7 +1171,7 @@ static int virCgroupPartitionEscape(char **path)
if ((rc = virCgroupPartitionNeedsEscaping(*path)) <= 0) if ((rc = virCgroupPartitionNeedsEscaping(*path)) <= 0)
return rc; return rc;
if (VIR_INSERT_ELEMENT(*path, 0, len, escape) < 0) if (VIR_INSERT_ELEMENT_QUIET(*path, 0, len, escape) < 0)
return -ENOMEM; return -ENOMEM;
return 0; return 0;

View File

@ -209,7 +209,7 @@ virLastErrorObject(void)
virErrorPtr err; virErrorPtr err;
err = virThreadLocalGet(&virLastErr); err = virThreadLocalGet(&virLastErr);
if (!err) { if (!err) {
if (VIR_ALLOC(err) < 0) if (VIR_ALLOC_QUIET(err) < 0)
return NULL; return NULL;
if (virThreadLocalSet(&virLastErr, err) < 0) if (virThreadLocalSet(&virLastErr, err) < 0)
VIR_FREE(err); VIR_FREE(err);
@ -330,7 +330,7 @@ virSaveLastError(void)
virErrorPtr to; virErrorPtr to;
int saved_errno = errno; int saved_errno = errno;
if (VIR_ALLOC(to) < 0) if (VIR_ALLOC_QUIET(to) < 0)
return NULL; return NULL;
virCopyLastError(to); virCopyLastError(to);

View File

@ -204,14 +204,14 @@ virLogOnceInit(void)
return -1; return -1;
virLogLock(); virLogLock();
if (VIR_ALLOC_N(virLogBuffer, virLogSize + 1) < 0) { if (VIR_ALLOC_N_QUIET(virLogBuffer, virLogSize + 1) < 0) {
/* /*
* The debug buffer is not a critical component, allow startup * The debug buffer is not a critical component, allow startup
* even in case of failure to allocate it in case of a * even in case of failure to allocate it in case of a
* configuration mistake. * configuration mistake.
*/ */
virLogSize = 64 * 1024; virLogSize = 64 * 1024;
if (VIR_ALLOC_N(virLogBuffer, virLogSize + 1) < 0) { if (VIR_ALLOC_N_QUIET(virLogBuffer, virLogSize + 1) < 0) {
pbm = "Failed to allocate debug buffer: deactivating debug log\n"; pbm = "Failed to allocate debug buffer: deactivating debug log\n";
virLogSize = 0; virLogSize = 0;
} else { } else {
@ -223,7 +223,7 @@ virLogOnceInit(void)
virLogEnd = 0; virLogEnd = 0;
virLogDefaultPriority = VIR_LOG_DEFAULT; virLogDefaultPriority = VIR_LOG_DEFAULT;
if (VIR_ALLOC(virLogRegex) >= 0) { if (VIR_ALLOC_QUIET(virLogRegex) >= 0) {
if (regcomp(virLogRegex, VIR_LOG_REGEX, REG_EXTENDED) != 0) if (regcomp(virLogRegex, VIR_LOG_REGEX, REG_EXTENDED) != 0)
VIR_FREE(virLogRegex); VIR_FREE(virLogRegex);
} }
@ -276,7 +276,7 @@ virLogSetBufferSize(int size)
} }
virLogSize = size * 1024; virLogSize = size * 1024;
if (VIR_ALLOC_N(virLogBuffer, virLogSize + 1) < 0) { if (VIR_ALLOC_N_QUIET(virLogBuffer, virLogSize + 1) < 0) {
pbm = "Failed to allocate debug buffer of %d kB\n"; pbm = "Failed to allocate debug buffer of %d kB\n";
virLogBuffer = oldLogBuffer; virLogBuffer = oldLogBuffer;
virLogSize = oldsize; virLogSize = oldsize;
@ -563,7 +563,7 @@ virLogDefineFilter(const char *match,
goto cleanup; goto cleanup;
} }
i = virLogNbFilters; i = virLogNbFilters;
if (VIR_REALLOC_N(virLogFilters, virLogNbFilters + 1)) { if (VIR_REALLOC_N_QUIET(virLogFilters, virLogNbFilters + 1)) {
i = -1; i = -1;
VIR_FREE(mdup); VIR_FREE(mdup);
goto cleanup; goto cleanup;
@ -676,7 +676,7 @@ virLogDefineOutput(virLogOutputFunc f,
} }
virLogLock(); virLogLock();
if (VIR_REALLOC_N(virLogOutputs, virLogNbOutputs + 1)) { if (VIR_REALLOC_N_QUIET(virLogOutputs, virLogNbOutputs + 1)) {
VIR_FREE(ndup); VIR_FREE(ndup);
goto cleanup; goto cleanup;
} }

View File

@ -2008,8 +2008,8 @@ virPCIGetIOMMUGroupAddressesAddOne(virPCIDeviceAddressPtr newDevAddr, void *opaq
*copyAddr = *newDevAddr; *copyAddr = *newDevAddr;
if (VIR_APPEND_ELEMENT(*addrList->iommuGroupDevices, if (VIR_APPEND_ELEMENT_QUIET(*addrList->iommuGroupDevices,
*addrList->nIommuGroupDevices, copyAddr) < 0) { *addrList->nIommuGroupDevices, copyAddr) < 0) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }

View File

@ -174,7 +174,7 @@ int virThreadCreate(virThreadPtr thread,
if ((err = pthread_attr_init(&attr)) != 0) if ((err = pthread_attr_init(&attr)) != 0)
goto cleanup; goto cleanup;
if (VIR_ALLOC(args) < 0) { if (VIR_ALLOC_QUIET(args) < 0) {
err = ENOMEM; err = ENOMEM;
goto cleanup; goto cleanup;
} }

View File

@ -35,6 +35,7 @@
#ifndef WIN32 #ifndef WIN32
# define VIR_FROM_THIS VIR_FROM_NONE
static int envsort(const void *a, const void *b) { static int envsort(const void *a, const void *b) {
const char *const*astrptr = a; const char *const*astrptr = a;

View File

@ -16,6 +16,8 @@
#include "network/bridge_driver.h" #include "network/bridge_driver.h"
#include "virstring.h" #include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int static int
testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps) testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps)
{ {

View File

@ -20,7 +20,7 @@ int main(int argc, char **argv)
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(buffer, len) < 0) { if (VIR_ALLOC_N_QUIET(buffer, len) < 0) {
fprintf(stderr, "out of memory\n"); fprintf(stderr, "out of memory\n");
goto cleanup; goto cleanup;
} }

View File

@ -37,6 +37,8 @@
#include "viralloc.h" #include "viralloc.h"
#include "virstring.h" #include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static virCapsPtr caps; static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt; static virDomainXMLOptionPtr xmlopt;