src/uml/uml_conf.c
src/uml/uml_driver.c
src/util/iohelper.c
+src/util/viralloc.c
src/util/viraudit.c
src/util/virauth.c
src/util/virauthconfig.c
return NULL;
}
- if (VIR_ALLOC_N(ret, size) < 0) {
+ if (VIR_ALLOC_N_QUIET(ret, size) < 0) {
PyErr_NoMemory();
return NULL;
}
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
sumparams = nparams * MIN(ncpus, 128);
- if (VIR_ALLOC_N(params, sumparams) < 0) {
+ if (VIR_ALLOC_N_QUIET(params, sumparams) < 0) {
error = PyErr_NoMemory();
goto error;
}
if (nparams) {
sumparams = nparams;
- if (VIR_ALLOC_N(params, nparams) < 0) {
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0) {
error = PyErr_NoMemory();
goto error;
}
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (i_retval < 0)
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();
cpumaplen = VIR_CPU_MAPLEN(cpunum);
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();
goto cleanup;
}
}
cpumaplen = VIR_CPU_MAPLEN(cpunum);
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
+ if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory();
for (i = 0; i < tuple_size; i++) {
}
cpumaplen = VIR_CPU_MAPLEN(cpunum);
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
+ if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory();
for (i = 0; i < tuple_size; i++) {
cpumaplen = VIR_CPU_MAPLEN(cpunum);
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;
LIBVIRT_BEGIN_ALLOW_THREADS;
if ((tuple_size = PyTuple_Size(pycpumap)) == -1)
return NULL;
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
+ if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory();
for (i = 0; i < tuple_size; i++) {
cpumaplen = VIR_CPU_MAPLEN(cpunum);
- if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
+ if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
auth.ncredtype = PyList_Size(pycredtype);
if (auth.ncredtype) {
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;
for (i = 0; i < auth.ncredtype; i++) {
PyObject *val;
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(ids, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(ids, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedDomains(conn, names, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virDomainSnapshotListNames(dom, names, c_retval, flags);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virDomainSnapshotListChildrenNames(snap, names, c_retval,
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListNetworks(conn, names, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedNetworks(conn, names, c_retval);
return VIR_PY_NONE;
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;
LIBVIRT_BEGIN_ALLOW_THREADS;
return VIR_PY_NONE;
if (nparams) {
- if (VIR_ALLOC_N(stats, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(stats, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return VIR_PY_NONE;
if (nparams) {
- if (VIR_ALLOC_N(stats, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(stats, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListStoragePools(conn, names, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedStoragePools(conn, names, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virStoragePoolListVolumes(pool, names, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virNodeListDevices(conn, cap, names, c_retval, flags);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virNodeDeviceListCaps(dev, names, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(uuids, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(uuids, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListSecrets(conn, uuids, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(uuids, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(uuids, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListNWFilters(conn, uuids, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListInterfaces(conn, names, c_retval);
return VIR_PY_NONE;
if (c_retval) {
- if (VIR_ALLOC_N(names, c_retval) < 0)
+ if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virConnectListDefinedInterfaces(conn, names, c_retval);
int i;
ncpus = PyList_Size(list);
- if (VIR_ALLOC_N(xmlcpus, ncpus) < 0)
+ if (VIR_ALLOC_N_QUIET(xmlcpus, ncpus) < 0)
return VIR_PY_INT_FAIL;
for (i = 0; i < ncpus; i++) {
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
ndisks = count;
if (ndisks) {
- if (VIR_ALLOC_N(disks, ndisks) < 0)
+ if (VIR_ALLOC_N_QUIET(disks, ndisks) < 0)
return VIR_PY_NONE;
LIBVIRT_BEGIN_ALLOW_THREADS;
}
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;
LIBVIRT_BEGIN_ALLOW_THREADS;
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;
LIBVIRT_BEGIN_ALLOW_THREADS;
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;
LIBVIRT_BEGIN_ALLOW_THREADS;
return NULL;
}
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
if (!nparams)
return PyDict_New();
- if (VIR_ALLOC_N(params, nparams) < 0)
+ if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
return PyErr_NoMemory();
LIBVIRT_BEGIN_ALLOW_THREADS;
/* create the serial port definition from the console definition */
if (def->nserials == 0) {
- if (VIR_APPEND_ELEMENT(def->serials, def->nserials,
- def->consoles[0]) < 0)
+ if (VIR_APPEND_ELEMENT_QUIET(def->serials,
+ def->nserials,
+ def->consoles[0]) < 0)
goto no_memory;
/* modify it to be a serial port */
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);
virReportOOMError();
return -1;
ii = 0;
primaryVideo = true;
}
- if (VIR_INSERT_ELEMENT_INPLACE(def->videos,
- ii,
- def->nvideos,
- video) < 0) {
+ if (VIR_INSERT_ELEMENT_INPLACE_QUIET(def->videos,
+ ii,
+ def->nvideos,
+ video) < 0) {
virDomainVideoDefFree(video);
goto error;
}
}
/* add to beginning/end of list */
- if (VIR_INSERT_ELEMENT(ipdef->hosts,
- command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
- ? 0 : ipdef->nhosts,
- ipdef->nhosts, host) < 0) {
+ if (VIR_INSERT_ELEMENT_QUIET(ipdef->hosts,
+ command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
+ ? 0 : ipdef->nhosts,
+ ipdef->nhosts, host) < 0) {
virReportOOMError();
goto cleanup;
}
}
/* add to beginning/end of list */
- if (VIR_INSERT_ELEMENT(ipdef->ranges,
- command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
- ? 0 : ipdef->nranges,
- ipdef->nranges, range) < 0) {
+ if (VIR_INSERT_ELEMENT_QUIET(ipdef->ranges,
+ command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
+ ? 0 : ipdef->nranges,
+ ipdef->nranges, range) < 0) {
virReportOOMError();
goto cleanup;
}
}
/* add to beginning/end of list */
- if (VIR_INSERT_ELEMENT(def->forward.ifs,
- command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
- ? 0 : def->forward.nifs,
- def->forward.nifs, iface) < 0) {
+ if (VIR_INSERT_ELEMENT_QUIET(def->forward.ifs,
+ command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
+ ? 0 : def->forward.nifs,
+ def->forward.nifs, iface) < 0) {
virReportOOMError();
goto cleanup;
}
(command == VIR_NETWORK_UPDATE_COMMAND_ADD_LAST)) {
/* add to beginning/end of list */
- if (VIR_INSERT_ELEMENT(def->portGroups,
- command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
- ? 0 : def->nPortGroups,
- def->nPortGroups, portgroup) < 0) {
+ if (VIR_INSERT_ELEMENT_QUIET(def->portGroups,
+ command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
+ ? 0 : def->nPortGroups,
+ def->nPortGroups, portgroup) < 0) {
virReportOOMError();
goto cleanup;
}
}
/* add to beginning/end of list */
- if (VIR_INSERT_ELEMENT(dns->hosts,
- command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
- ? 0 : dns->nhosts, dns->nhosts, host) < 0) {
+ if (VIR_INSERT_ELEMENT_QUIET(dns->hosts,
+ command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
+ ? 0 : dns->nhosts, dns->nhosts, host) < 0) {
virReportOOMError();
goto cleanup;
}
}
/* add to beginning/end of list */
- if (VIR_INSERT_ELEMENT(dns->srvs,
- command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
- ? 0 : dns->nsrvs, dns->nsrvs, srv) < 0) {
+ if (VIR_INSERT_ELEMENT_QUIET(dns->srvs,
+ command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
+ ? 0 : dns->nsrvs, dns->nsrvs, srv) < 0) {
virReportOOMError();
goto cleanup;
}
}
/* add to beginning/end of list */
- if (VIR_INSERT_ELEMENT(dns->txts,
- command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
- ? 0 : dns->ntxts, dns->ntxts, txt) < 0) {
+ if (VIR_INSERT_ELEMENT_QUIET(dns->txts,
+ command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST
+ ? 0 : dns->ntxts, dns->ntxts, txt) < 0) {
virReportOOMError();
goto cleanup;
}
return -1;
}
- if (virAllocN(ptrptr, size, 1) < 0) {
+ if (virAllocN(ptrptr, size, 1, false, 0, NULL, NULL, 0) < 0) {
virReportOOMError();
return -1;
}
VIR_STRDUP(root->dst, "/") < 0)
goto error;
- if (VIR_INSERT_ELEMENT(vm->def->fss,
- 0,
- vm->def->nfss,
- root) < 0)
+ if (VIR_INSERT_ELEMENT_QUIET(vm->def->fss,
+ 0,
+ vm->def->nfss,
+ root) < 0)
goto no_memory;
return 0;
/* The seclabel must be added to @vm prior calling domainGenSecurityLabel
* which may require seclabel to be presented already */
if (generated &&
- VIR_APPEND_ELEMENT(vm->seclabels, vm->nseclabels, seclabel) < 0) {
+ VIR_APPEND_ELEMENT_QUIET(vm->seclabels, vm->nseclabels, seclabel) < 0) {
virReportOOMError();
goto cleanup;
}
if (!disk_seclabel)
return -1;
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();
virSecurityDeviceLabelDefFree(disk_seclabel);
return -1;
#include "viralloc.h"
#include "virlog.h"
+#include "virerror.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
#if TEST_OOM
static int testMallocNext = 0;
* virAlloc:
* @ptrptr: pointer to pointer for address of allocated memory
* @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
* 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
*/
-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 (virAllocTestFail()) {
#endif
*(void **)ptrptr = calloc(1, size);
- if (*(void **)ptrptr == NULL)
+ if (*(void **)ptrptr == NULL) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
+ }
return 0;
}
* @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes 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,
* each with 'size' bytes. Return the address of the
* 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
*/
-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 (virAllocTestFail()) {
#endif
*(void**)ptrptr = calloc(count, size);
- if (*(void**)ptrptr == NULL)
+ if (*(void**)ptrptr == NULL) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
+ }
return 0;
}
* @ptrptr: pointer to pointer for address of allocated memory
* @size: number of bytes to allocate
* @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
* 'count' elements, each 'size' bytes in length. Update 'ptrptr'
* with the address of the newly allocated memory. On failure,
* 'ptrptr' is not changed and still points to the original memory
* 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
*/
-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;
#if TEST_OOM
#endif
if (xalloc_oversized(count, size)) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
tmp = realloc(*(void**)ptrptr, size * count);
- if (!tmp && (size * count))
+ if (!tmp && (size * count)) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
+ }
*(void**)ptrptr = tmp;
return 0;
}
* @size: number of bytes per element
* @countptr: pointer to number of elements in array
* @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
* '*countptr' + 'add' elements, each 'size' bytes in length.
* Update 'ptrptr' and 'countptr' with the details of the newly
* allocated memory. On failure, 'ptrptr' and 'countptr' are not
* 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
*/
-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;
if (*countptr + add < *countptr) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
- ret = virReallocN(ptrptr, size, *countptr + add);
+ ret = virReallocN(ptrptr, size, *countptr + add, report,
+ domcode, filename, funcname, linenr);
if (ret == 0) {
memset(*(char **)ptrptr + (size * *countptr), 0, size * add);
*countptr += add;
* @allocptr: pointer to number of elements allocated in array
* @count: number of elements currently used 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
* block of memory in 'ptrptr' to be an array of at least 'count' +
* 'add' elements, each 'size' bytes in length. Update 'ptrptr' and
* 'allocptr' with the details of the newly allocated memory. On
* 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
*/
-int virResizeN(void *ptrptr, size_t size, size_t *allocptr, size_t count,
- size_t add)
+int virResizeN(void *ptrptr,
+ 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;
if (count + add < count) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
delta = count + add - *allocptr;
if (delta < *allocptr / 2)
delta = *allocptr / 2;
- return virExpandN(ptrptr, size, allocptr, delta);
+ return virExpandN(ptrptr, size, allocptr, delta, report,
+ domcode, filename, funcname, linenr);
}
/**
void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove)
{
if (toremove < *countptr)
- ignore_value(virReallocN(ptrptr, size, *countptr -= toremove));
+ ignore_value(virReallocN(ptrptr, size, *countptr -= toremove,
+ false, 0, NULL, NULL, 0));
else {
virFree(ptrptr);
*countptr = 0;
* @inPlace: false if we should expand the allocated memory before
* moving, true if we should assume someone else *has
* 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
* long, to be *countptr+add elements long, then appropriately move
* allocated memory in *ptrptr and the new size in *countptr. If
* newelems is NULL, the new elements at ptrptr[at] are instead filled
* 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
*/
virInsertElementsN(void *ptrptr, size_t size, size_t at,
size_t *countptr,
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) {
at = *countptr;
} else if (at > *countptr) {
- VIR_WARN("out of bounds index - count %zu at %zu add %zu",
- *countptr, at, add);
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("out of bounds index - count %zu at %zu add %zu"),
+ *countptr, at, add);
return -1;
}
if (inPlace) {
*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;
}
* @struct_size: size of initial struct
* @element_size: size of array elements
* @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
* of size element_size. This sort of allocation is useful for
* 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.
+ * objects as structs containing flexible array members. If @report
+ * is true, OOM errors are reported automatically.
*
* 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;
#endif
if (VIR_ALLOC_VAR_OVERSIZED(struct_size, count, element_size)) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
errno = ENOMEM;
return -1;
}
alloc_size = struct_size + (element_size * count);
*(void **)ptrptr = calloc(1, alloc_size);
- if (*(void **)ptrptr == NULL)
+ if (*(void **)ptrptr == NULL) {
+ if (report)
+ virReportOOMErrorFull(domcode, filename, funcname, linenr);
return -1;
+ }
return 0;
}
/* Don't call these directly - use the macros below */
-int virAlloc(void *ptrptr, size_t size) ATTRIBUTE_RETURN_CHECK
- ATTRIBUTE_NONNULL(1);
-int virAllocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK
- ATTRIBUTE_NONNULL(1);
-int virReallocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK
- ATTRIBUTE_NONNULL(1);
-int virExpandN(void *ptrptr, size_t size, size_t *count, size_t add)
+int virAlloc(void *ptrptr, size_t size, bool report, int domcode,
+ const char *filename, const char *funcname, size_t linenr)
+ ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
+int virAllocN(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 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);
-int virResizeN(void *ptrptr, size_t size, size_t *alloc, size_t count,
- size_t desired)
+int virResizeN(void *ptrptr, size_t size, size_t *alloc, size_t count, 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);
void virShrinkN(void *ptrptr, size_t size, size_t *count, size_t toremove)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int virInsertElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
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);
int virDeleteElementsN(void *ptrptr, size_t size, size_t at, size_t *countptr,
size_t toremove, bool inPlace)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
-int virAllocVar(void *ptrptr,
- size_t struct_size,
- size_t element_size,
- size_t count) ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
+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)
+ ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
/**
*
* 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
*/
-# 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:
*
* 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
*/
-# 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:
*
* 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
*/
-# 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:
*
* 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) \
- 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:
*
* 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) \
- 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:
*
* 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) \
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) \
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) \
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) \
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:
*
* 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) \
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) \
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) \
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) \
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:
* @ptr: pointer to array of objects (*not* ptr to ptr)
*
* 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) \
- 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:
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);
return -1;
}
}
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);
return;
}
}
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);
return;
}
if (*str) {
len = strlen(str);
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);
return;
}
if ((rc = virCgroupPartitionNeedsEscaping(*path)) <= 0)
return rc;
- if (VIR_INSERT_ELEMENT(*path, 0, len, escape) < 0)
+ if (VIR_INSERT_ELEMENT_QUIET(*path, 0, len, escape) < 0)
return -ENOMEM;
return 0;
virErrorPtr err;
err = virThreadLocalGet(&virLastErr);
if (!err) {
- if (VIR_ALLOC(err) < 0)
+ if (VIR_ALLOC_QUIET(err) < 0)
return NULL;
if (virThreadLocalSet(&virLastErr, err) < 0)
VIR_FREE(err);
virErrorPtr to;
int saved_errno = errno;
- if (VIR_ALLOC(to) < 0)
+ if (VIR_ALLOC_QUIET(to) < 0)
return NULL;
virCopyLastError(to);
return -1;
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
* even in case of failure to allocate it in case of a
* configuration mistake.
*/
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";
virLogSize = 0;
} else {
virLogEnd = 0;
virLogDefaultPriority = VIR_LOG_DEFAULT;
- if (VIR_ALLOC(virLogRegex) >= 0) {
+ if (VIR_ALLOC_QUIET(virLogRegex) >= 0) {
if (regcomp(virLogRegex, VIR_LOG_REGEX, REG_EXTENDED) != 0)
VIR_FREE(virLogRegex);
}
}
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";
virLogBuffer = oldLogBuffer;
virLogSize = oldsize;
goto cleanup;
}
i = virLogNbFilters;
- if (VIR_REALLOC_N(virLogFilters, virLogNbFilters + 1)) {
+ if (VIR_REALLOC_N_QUIET(virLogFilters, virLogNbFilters + 1)) {
i = -1;
VIR_FREE(mdup);
goto cleanup;
}
virLogLock();
- if (VIR_REALLOC_N(virLogOutputs, virLogNbOutputs + 1)) {
+ if (VIR_REALLOC_N_QUIET(virLogOutputs, virLogNbOutputs + 1)) {
VIR_FREE(ndup);
goto cleanup;
}
*copyAddr = *newDevAddr;
- if (VIR_APPEND_ELEMENT(*addrList->iommuGroupDevices,
- *addrList->nIommuGroupDevices, copyAddr) < 0) {
+ if (VIR_APPEND_ELEMENT_QUIET(*addrList->iommuGroupDevices,
+ *addrList->nIommuGroupDevices, copyAddr) < 0) {
virReportOOMError();
goto cleanup;
}
if ((err = pthread_attr_init(&attr)) != 0)
goto cleanup;
- if (VIR_ALLOC(args) < 0) {
+ if (VIR_ALLOC_QUIET(args) < 0) {
err = ENOMEM;
goto cleanup;
}
#ifndef WIN32
+# define VIR_FROM_THIS VIR_FROM_NONE
static int envsort(const void *a, const void *b) {
const char *const*astrptr = a;
#include "network/bridge_driver.h"
#include "virstring.h"
+#define VIR_FROM_THIS VIR_FROM_NONE
+
static int
testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps)
{
goto cleanup;
}
- if (VIR_ALLOC_N(buffer, len) < 0) {
+ if (VIR_ALLOC_N_QUIET(buffer, len) < 0) {
fprintf(stderr, "out of memory\n");
goto cleanup;
}
#include "viralloc.h"
#include "virstring.h"
+#define VIR_FROM_THIS VIR_FROM_NONE
+
static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;