diff options
author | Konstantin Neumoin <kneumoin@virtuozzo.com> | 2016-11-03 20:05:51 +0300 |
---|---|---|
committer | Peter Krempa <pkrempa@redhat.com> | 2016-11-11 13:44:44 +0100 |
commit | a03d782b3e19cb9b4681783ec1f6c88615128627 (patch) | |
tree | b2afc851dba75bd729c0e5dacaec70cc31cf1473 /libvirt-utils.c | |
parent | 653ca92463788c0f1873f53b81c6d5f5a37784c1 (diff) | |
download | libvirt-python-a03d782b3e19cb9b4681783ec1f6c88615128627.tar.gz |
move cpumap conversion code to a common helper
All libvirt_virDomainPin* functions do the same thing for convert
pycpumap to cpumap, so this patch moves all common logic to new
helper - virPyCpumapConvert.
Signed-off-by: Konstantin Neumoin <kneumoin@virtuozzo.com>
Diffstat (limited to 'libvirt-utils.c')
-rw-r--r-- | libvirt-utils.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libvirt-utils.c b/libvirt-utils.c index 2bf7519..09cc1c3 100644 --- a/libvirt-utils.c +++ b/libvirt-utils.c @@ -586,3 +586,60 @@ virPyDictToTypedParams(PyObject *dict, return ret; } #endif /* LIBVIR_CHECK_VERSION(1, 1, 0) */ + + +/* virPyCpumapConvert + * @cpunum: the number of physical cpus of the host. + * @pycpumap: source cpu map, python tuple of bools. + * @cpumapptr: destination cpu map. + * @cpumaplen: destination cpu map length. + * + * Helper function to convert a pycpumap to char*. + * + * Returns 0 on success, -1 on failure with error set. + */ +int +virPyCpumapConvert(int cpunum, + PyObject *pycpumap, + unsigned char **cpumapptr, + int *cpumaplen) +{ + int tuple_size; + size_t i; + *cpumapptr = NULL; + + if (!PyTuple_Check(pycpumap)) { + PyErr_SetString(PyExc_TypeError, "Unexpected type, tuple is required"); + return -1; + } + + *cpumaplen = VIR_CPU_MAPLEN(cpunum); + + if ((tuple_size = PyTuple_Size(pycpumap)) == -1) + return -1; + + if (VIR_ALLOC_N(*cpumapptr, *cpumaplen) < 0) { + PyErr_NoMemory(); + return -1; + } + + for (i = 0; i < tuple_size; i++) { + PyObject *flag = PyTuple_GetItem(pycpumap, i); + bool b; + + if (!flag || libvirt_boolUnwrap(flag, &b) < 0) { + VIR_FREE(*cpumapptr); + return -1; + } + + if (b) + VIR_USE_CPU(*cpumapptr, i); + else + VIR_UNUSE_CPU(*cpumapptr, i); + } + + for (; i < cpunum; i++) + VIR_UNUSE_CPU(*cpumapptr, i); + + return 0; +} |