[libvirt-python PATCHv2 09/44] Import VIR_ALLOC / VIR_ALLOC_N / VIR_REALLOC_N functions

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Import the libvirt memory allocation functions, stripping the OOM
testing and error reporting pieces.

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 libvirt-lxc-override.c  |   1 +
 libvirt-override.c      | 106 +++++++++++++++++++++----------------------
 libvirt-qemu-override.c |   1 +
 libvirt-utils.c         | 117 ++++++++++++++++++++++++++++++++++++++++++++++++
 libvirt-utils.h         |  77 +++++++++++++++++++++++++++++++
 typewrappers.c          |   1 +
 6 files changed, 250 insertions(+), 53 deletions(-)
 create mode 100644 libvirt-utils.c

diff --git a/libvirt-lxc-override.c b/libvirt-lxc-override.c
index 9b8cd4c..03b00b0 100644
--- a/libvirt-lxc-override.c
+++ b/libvirt-lxc-override.c
@@ -18,6 +18,7 @@
 #include <libvirt/libvirt-lxc.h>
 #include <libvirt/virterror.h>
 #include "typewrappers.h"
+#include "libvirt-utils.h"
 #include "build/libvirt-lxc.h"
 
 #ifndef __CYGWIN__
diff --git a/libvirt-override.c b/libvirt-override.c
index 860d3ab..d8bbbab 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -166,7 +166,7 @@ setPyVirTypedParameter(PyObject *info,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(ret, size) < 0) {
+    if (VIR_ALLOC_N(ret, size) < 0) {
         PyErr_NoMemory();
         return NULL;
     }
@@ -506,7 +506,7 @@ libvirt_virDomainBlockStatsFlags(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -573,7 +573,7 @@ libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
 
         sumparams = nparams * MIN(ncpus, 128);
 
-        if (VIR_ALLOC_N_QUIET(params, sumparams) < 0) {
+        if (VIR_ALLOC_N(params, sumparams) < 0) {
             error = PyErr_NoMemory();
             goto error;
         }
@@ -625,7 +625,7 @@ libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
         if (nparams) {
             sumparams = nparams;
 
-            if (VIR_ALLOC_N_QUIET(params, nparams) < 0) {
+            if (VIR_ALLOC_N(params, nparams) < 0) {
                 error = PyErr_NoMemory();
                 goto error;
             }
@@ -806,7 +806,7 @@ libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -854,7 +854,7 @@ libvirt_virDomainGetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -914,7 +914,7 @@ libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -990,7 +990,7 @@ libvirt_virDomainSetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1064,7 +1064,7 @@ libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1124,7 +1124,7 @@ libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1184,7 +1184,7 @@ libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1244,7 +1244,7 @@ libvirt_virDomainGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1304,7 +1304,7 @@ libvirt_virDomainSetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1364,7 +1364,7 @@ libvirt_virDomainGetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1425,7 +1425,7 @@ libvirt_virDomainSetInterfaceParameters(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1486,7 +1486,7 @@ libvirt_virDomainGetInterfaceParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1532,12 +1532,12 @@ libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
     if (i_retval < 0)
         return VIR_PY_INT_FAIL;
 
-    if (VIR_ALLOC_N_QUIET(cpuinfo, dominfo.nrVirtCpu) < 0)
+    if (VIR_ALLOC_N(cpuinfo, dominfo.nrVirtCpu) < 0)
         return PyErr_NoMemory();
 
     cpumaplen = VIR_CPU_MAPLEN(cpunum);
     if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) ||
-        VIR_ALLOC_N_QUIET(cpumap, dominfo.nrVirtCpu * cpumaplen) < 0) {
+        VIR_ALLOC_N(cpumap, dominfo.nrVirtCpu * cpumaplen) < 0) {
         error = PyErr_NoMemory();
         goto cleanup;
     }
@@ -1660,7 +1660,7 @@ libvirt_virDomainPinVcpu(PyObject *self ATTRIBUTE_UNUSED,
     }
 
     cpumaplen = VIR_CPU_MAPLEN(cpunum);
-    if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
+    if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
         return PyErr_NoMemory();
 
     for (i = 0; i < tuple_size; i++) {
@@ -1725,7 +1725,7 @@ libvirt_virDomainPinVcpuFlags(PyObject *self ATTRIBUTE_UNUSED,
     }
 
     cpumaplen = VIR_CPU_MAPLEN(cpunum);
-    if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
+    if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
         return PyErr_NoMemory();
 
     for (i = 0; i < tuple_size; i++) {
@@ -1785,7 +1785,7 @@ libvirt_virDomainGetVcpuPinInfo(PyObject *self ATTRIBUTE_UNUSED,
 
     cpumaplen = VIR_CPU_MAPLEN(cpunum);
     if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) ||
-        VIR_ALLOC_N_QUIET(cpumaps, dominfo.nrVirtCpu * cpumaplen) < 0)
+        VIR_ALLOC_N(cpumaps, dominfo.nrVirtCpu * cpumaplen) < 0)
         goto cleanup;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -1854,7 +1854,7 @@ libvirt_virDomainPinEmulator(PyObject *self ATTRIBUTE_UNUSED,
     if ((tuple_size = PyTuple_Size(pycpumap)) == -1)
         return NULL;
 
-    if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
+    if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
         return PyErr_NoMemory();
 
     for (i = 0; i < tuple_size; i++) {
@@ -1913,7 +1913,7 @@ libvirt_virDomainGetEmulatorPinInfo(PyObject *self ATTRIBUTE_UNUSED,
 
     cpumaplen = VIR_CPU_MAPLEN(cpunum);
 
-    if (VIR_ALLOC_N_QUIET(cpumap, cpumaplen) < 0)
+    if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -2189,7 +2189,7 @@ libvirt_virConnectOpenAuth(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
     auth.ncredtype = PyList_Size(pycredtype);
     if (auth.ncredtype) {
         size_t i;
-        if (VIR_ALLOC_N_QUIET(auth.credtype, auth.ncredtype) < 0)
+        if (VIR_ALLOC_N(auth.credtype, auth.ncredtype) < 0)
             return VIR_PY_NONE;
         for (i = 0; i < auth.ncredtype; i++) {
             PyObject *val;
@@ -2370,7 +2370,7 @@ libvirt_virConnectListDomainsID(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(ids, c_retval) < 0)
+        if (VIR_ALLOC_N(ids, c_retval) < 0)
             return VIR_PY_NONE;
 
         LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -2462,7 +2462,7 @@ libvirt_virConnectListDefinedDomains(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListDefinedDomains(conn, names, c_retval);
@@ -2510,7 +2510,7 @@ libvirt_virDomainSnapshotListNames(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return PyErr_NoMemory();
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virDomainSnapshotListNames(dom, names, c_retval, flags);
@@ -2612,7 +2612,7 @@ libvirt_virDomainSnapshotListChildrenNames(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return PyErr_NoMemory();
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virDomainSnapshotListChildrenNames(snap, names, c_retval,
@@ -2946,7 +2946,7 @@ libvirt_virConnectListNetworks(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListNetworks(conn, names, c_retval);
@@ -2992,7 +2992,7 @@ libvirt_virConnectListDefinedNetworks(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListDefinedNetworks(conn, names, c_retval);
@@ -3201,7 +3201,7 @@ libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *arg
         return VIR_PY_NONE;
 
     conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
-    if (VIR_ALLOC_N_QUIET(freeMems, maxCells) < 0)
+    if (VIR_ALLOC_N(freeMems, maxCells) < 0)
         return VIR_PY_NONE;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -3246,7 +3246,7 @@ libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
         return VIR_PY_NONE;
 
     if (nparams) {
-        if (VIR_ALLOC_N_QUIET(stats, nparams) < 0)
+        if (VIR_ALLOC_N(stats, nparams) < 0)
             return PyErr_NoMemory();
 
         LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -3310,7 +3310,7 @@ libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
         return VIR_PY_NONE;
 
     if (nparams) {
-        if (VIR_ALLOC_N_QUIET(stats, nparams) < 0)
+        if (VIR_ALLOC_N(stats, nparams) < 0)
             return PyErr_NoMemory();
 
         LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -3371,7 +3371,7 @@ libvirt_virConnectListStoragePools(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListStoragePools(conn, names, c_retval);
@@ -3425,7 +3425,7 @@ libvirt_virConnectListDefinedStoragePools(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListDefinedStoragePools(conn, names, c_retval);
@@ -3525,7 +3525,7 @@ libvirt_virStoragePoolListVolumes(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virStoragePoolListVolumes(pool, names, c_retval);
@@ -3787,7 +3787,7 @@ libvirt_virNodeListDevices(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virNodeListDevices(conn, cap, names, c_retval, flags);
@@ -3878,7 +3878,7 @@ libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virNodeDeviceListCaps(dev, names, c_retval);
@@ -3998,7 +3998,7 @@ libvirt_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(uuids, c_retval) < 0)
+        if (VIR_ALLOC_N(uuids, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListSecrets(conn, uuids, c_retval);
@@ -4218,7 +4218,7 @@ libvirt_virConnectListNWFilters(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(uuids, c_retval) < 0)
+        if (VIR_ALLOC_N(uuids, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListNWFilters(conn, uuids, c_retval);
@@ -4310,7 +4310,7 @@ libvirt_virConnectListInterfaces(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListInterfaces(conn, names, c_retval);
@@ -4365,7 +4365,7 @@ libvirt_virConnectListDefinedInterfaces(PyObject *self ATTRIBUTE_UNUSED,
         return VIR_PY_NONE;
 
     if (c_retval) {
-        if (VIR_ALLOC_N_QUIET(names, c_retval) < 0)
+        if (VIR_ALLOC_N(names, c_retval) < 0)
             return VIR_PY_NONE;
         LIBVIRT_BEGIN_ALLOW_THREADS;
         c_retval = virConnectListDefinedInterfaces(conn, names, c_retval);
@@ -4465,7 +4465,7 @@ libvirt_virConnectBaselineCPU(PyObject *self ATTRIBUTE_UNUSED,
         size_t i;
 
         ncpus = PyList_Size(list);
-        if (VIR_ALLOC_N_QUIET(xmlcpus, ncpus) < 0)
+        if (VIR_ALLOC_N(xmlcpus, ncpus) < 0)
             return VIR_PY_INT_FAIL;
 
         for (i = 0; i < ncpus; i++) {
@@ -4675,7 +4675,7 @@ libvirt_virDomainSetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -4736,7 +4736,7 @@ libvirt_virDomainGetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -4779,7 +4779,7 @@ libvirt_virDomainGetDiskErrors(PyObject *self ATTRIBUTE_UNUSED,
     ndisks = count;
 
     if (ndisks) {
-        if (VIR_ALLOC_N_QUIET(disks, ndisks) < 0)
+        if (VIR_ALLOC_N(disks, ndisks) < 0)
             return VIR_PY_NONE;
 
         LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -6650,7 +6650,7 @@ libvirt_virStreamRecv(PyObject *self ATTRIBUTE_UNUSED,
     }
     stream = PyvirStream_Get(pyobj_stream);
 
-    if (VIR_ALLOC_N_QUIET(buf, nbytes+1 > 0 ? nbytes+1 : 1) < 0)
+    if (VIR_ALLOC_N(buf, nbytes+1 > 0 ? nbytes+1 : 1) < 0)
         return VIR_PY_NONE;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -6876,7 +6876,7 @@ libvirt_virDomainBlockPeek(PyObject *self ATTRIBUTE_UNUSED,
 
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
-    if (VIR_ALLOC_N_QUIET(buf, size) < 0)
+    if (VIR_ALLOC_N(buf, size) < 0)
         return VIR_PY_NONE;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -6913,7 +6913,7 @@ libvirt_virDomainMemoryPeek(PyObject *self ATTRIBUTE_UNUSED,
 
     domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
 
-    if (VIR_ALLOC_N_QUIET(buf, size) < 0)
+    if (VIR_ALLOC_N(buf, size) < 0)
         return VIR_PY_NONE;
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -6973,7 +6973,7 @@ libvirt_virNodeSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
         return NULL;
     }
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -7033,7 +7033,7 @@ libvirt_virNodeGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
     if (!nparams)
         return PyDict_New();
 
-    if (VIR_ALLOC_N_QUIET(params, nparams) < 0)
+    if (VIR_ALLOC_N(params, nparams) < 0)
         return PyErr_NoMemory();
 
     LIBVIRT_BEGIN_ALLOW_THREADS;
@@ -7141,7 +7141,7 @@ libvirt_virDomainCreateWithFiles(PyObject *self ATTRIBUTE_UNUSED, PyObject *args
 
     nfiles = PyList_Size(pyobj_files);
 
-    if (VIR_ALLOC_N_QUIET(files, nfiles) < 0)
+    if (VIR_ALLOC_N(files, nfiles) < 0)
         return PyErr_NoMemory();
 
     for (i = 0; i < nfiles; i++) {
@@ -7187,7 +7187,7 @@ libvirt_virDomainCreateXMLWithFiles(PyObject *self ATTRIBUTE_UNUSED, PyObject *a
 
     nfiles = PyList_Size(pyobj_files);
 
-    if (VIR_ALLOC_N_QUIET(files, nfiles) < 0)
+    if (VIR_ALLOC_N(files, nfiles) < 0)
         return PyErr_NoMemory();
 
     for (i = 0; i < nfiles; i++) {
diff --git a/libvirt-qemu-override.c b/libvirt-qemu-override.c
index 4008b10..676e8bf 100644
--- a/libvirt-qemu-override.c
+++ b/libvirt-qemu-override.c
@@ -18,6 +18,7 @@
 #include <libvirt/libvirt-qemu.h>
 #include <libvirt/virterror.h>
 #include "typewrappers.h"
+#include "libvirt-utils.h"
 #include "build/libvirt-qemu.h"
 
 #ifndef __CYGWIN__
diff --git a/libvirt-utils.c b/libvirt-utils.c
new file mode 100644
index 0000000..873552c
--- /dev/null
+++ b/libvirt-utils.c
@@ -0,0 +1,117 @@
+/*
+ * libvirt-utils.c: misc helper APIs for python binding
+ *
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "libvirt-utils.h"
+
+/**
+ * virAlloc:
+ * @ptrptr: pointer to pointer for address of allocated memory
+ * @size: number of bytes to allocate
+ *
+ * Allocate  'size' bytes of memory. Return the address of the
+ * allocated memory in 'ptrptr'. The newly allocated memory is
+ * filled with zeros.
+ *
+ * Returns -1 on failure to allocate, zero on success
+ */
+int virAlloc(void *ptrptr,
+             size_t size)
+{
+    *(void **)ptrptr = calloc(1, size);
+    if (*(void **)ptrptr == NULL) {
+        return -1;
+    }
+    return 0;
+}
+
+/**
+ * virAllocN:
+ * @ptrptr: pointer to pointer for address of allocated memory
+ * @size: number of bytes to allocate
+ * @count: number of elements to allocate
+ *
+ * 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.
+ *
+ * Returns -1 on failure to allocate, zero on success
+ */
+int virAllocN(void *ptrptr,
+              size_t size,
+              size_t count)
+{
+    *(void**)ptrptr = calloc(count, size);
+    if (*(void**)ptrptr == NULL) {
+        return -1;
+    }
+    return 0;
+}
+
+/**
+ * virReallocN:
+ * @ptrptr: pointer to pointer for address of allocated memory
+ * @size: number of bytes to allocate
+ * @count: number of elements in array
+ *
+ * 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.
+ *
+ * Returns -1 on failure to allocate, zero on success
+ */
+int virReallocN(void *ptrptr,
+                size_t size,
+                size_t count)
+{
+    void *tmp;
+
+    if (xalloc_oversized(count, size)) {
+        errno = ENOMEM;
+        return -1;
+    }
+    tmp = realloc(*(void**)ptrptr, size * count);
+    if (!tmp && (size * count)) {
+        return -1;
+    }
+    *(void**)ptrptr = tmp;
+    return 0;
+}
+
+
+/**
+ * virFree:
+ * @ptrptr: pointer to pointer for address of memory to be freed
+ *
+ * Release the chunk of memory in the pointer pointed to by
+ * the 'ptrptr' variable. After release, 'ptrptr' will be
+ * updated to point to NULL.
+ */
+void virFree(void *ptrptr)
+{
+    int save_errno = errno;
+
+    free(*(void**)ptrptr);
+    *(void**)ptrptr = NULL;
+    errno = save_errno;
+}
diff --git a/libvirt-utils.h b/libvirt-utils.h
index f608eb4..30380a3 100644
--- a/libvirt-utils.h
+++ b/libvirt-utils.h
@@ -106,4 +106,81 @@
 # endif                         /* __GNUC__ */
 
 
+/* 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);
+void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
+
+/**
+ * VIR_ALLOC:
+ * @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 (with OOM error reported), 0 on success
+ */
+# define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)))
+
+/**
+ * VIR_ALLOC_N:
+ * @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 (with OOM error reported), 0 on success
+ */
+# define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count))
+
+/**
+ * VIR_REALLOC_N:
+ * @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 (with OOM error reported), 0 on success
+ */
+# define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count))
+
+/**
+ * VIR_FREE:
+ * @ptr: pointer holding address to be freed
+ *
+ * Free the memory stored in 'ptr' and update to point
+ * to NULL.
+ *
+ * This macro is safe to use on arguments with side effects.
+ */
+# if !STATIC_ANALYSIS
+/* The ternary ensures that ptr is a pointer and not an integer type,
+ * while evaluating ptr only once.  This gives us extra compiler
+ * safety when compiling under gcc.  For now, we intentionally cast
+ * away const, since a number of callers safely pass const char *.
+ */
+#  define VIR_FREE(ptr) virFree((void *) (1 ? (const void *) &(ptr) : (ptr)))
+# else
+/* The Coverity static analyzer considers the else path of the "?:" and
+ * flags the VIR_FREE() of the address of the address of memory as a
+ * RESOURCE_LEAK resulting in numerous false positives (eg, VIR_FREE(&ptr))
+ */
+#  define VIR_FREE(ptr) virFree((void *) &(ptr))
+# endif
+
 #endif /* __LIBVIRT_UTILS_H__ */
diff --git a/typewrappers.c b/typewrappers.c
index ba02d6a..d6cbbf1 100644
--- a/typewrappers.c
+++ b/typewrappers.c
@@ -13,6 +13,7 @@
 #undef HAVE_PTHREAD_H
 
 #include "typewrappers.h"
+#include "libvirt-utils.h"
 
 #ifndef Py_CAPSULE_H
 typedef void(*PyCapsule_Destructor)(void *, void *);
-- 
1.8.3.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]