The function filters all CPU features through a given callback while
copying CPU model related parts of a CPU definition.
Signed-off-by: Jiri Denemark <jdenemar(a)redhat.com>
---
Notes:
Version 2:
- removed ATTRIBUTE_NONNULL from cpu_conf.c
src/conf/cpu_conf.c | 31 ++++++++++++++++++++++++-------
src/conf/cpu_conf.h | 14 ++++++++++++++
src/libvirt_private.syms | 1 +
3 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index 2e78b76..f174529 100644
--- a/src/conf/cpu_conf.c
+++ b/src/conf/cpu_conf.c
@@ -86,28 +86,45 @@ virCPUDefCopyModel(virCPUDefPtr dst,
const virCPUDef *src,
bool resetPolicy)
{
+ return virCPUDefCopyModelFilter(dst, src, resetPolicy, NULL, NULL);
+}
+
+
+int
+virCPUDefCopyModelFilter(virCPUDefPtr dst,
+ const virCPUDef *src,
+ bool resetPolicy,
+ virCPUDefFeatureFilter filter,
+ void *opaque)
+{
size_t i;
+ size_t n;
if (VIR_STRDUP(dst->model, src->model) < 0 ||
VIR_STRDUP(dst->vendor, src->vendor) < 0 ||
VIR_STRDUP(dst->vendor_id, src->vendor_id) < 0 ||
VIR_ALLOC_N(dst->features, src->nfeatures) < 0)
return -1;
- dst->nfeatures_max = dst->nfeatures = src->nfeatures;
+ dst->nfeatures_max = src->nfeatures;
+ dst->nfeatures = 0;
- for (i = 0; i < dst->nfeatures; i++) {
+ for (i = 0; i < src->nfeatures; i++) {
+ if (filter && !filter(src->features[i].name, opaque))
+ continue;
+
+ n = dst->nfeatures++;
if (dst->type != src->type && resetPolicy) {
if (dst->type == VIR_CPU_TYPE_HOST)
- dst->features[i].policy = -1;
+ dst->features[n].policy = -1;
else if (src->features[i].policy == -1)
- dst->features[i].policy = VIR_CPU_FEATURE_REQUIRE;
+ dst->features[n].policy = VIR_CPU_FEATURE_REQUIRE;
else
- dst->features[i].policy = src->features[i].policy;
+ dst->features[n].policy = src->features[i].policy;
} else {
- dst->features[i].policy = src->features[i].policy;
+ dst->features[n].policy = src->features[i].policy;
}
- if (VIR_STRDUP(dst->features[i].name, src->features[i].name) < 0)
+ if (VIR_STRDUP(dst->features[n].name, src->features[i].name) < 0)
return -1;
}
diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
index a5dd208..e084392 100644
--- a/src/conf/cpu_conf.h
+++ b/src/conf/cpu_conf.h
@@ -123,6 +123,20 @@ virCPUDefCopyModel(virCPUDefPtr dst,
const virCPUDef *src,
bool resetPolicy);
+/*
+ * Returns true if feature @name should copied, false otherwise.
+ */
+typedef bool (*virCPUDefFeatureFilter)(const char *name,
+ void *opaque);
+
+int
+virCPUDefCopyModelFilter(virCPUDefPtr dst,
+ const virCPUDef *src,
+ bool resetPolicy,
+ virCPUDefFeatureFilter filter,
+ void *opaque)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
void
virCPUDefStealModel(virCPUDefPtr dst,
virCPUDefPtr src);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index adf330a..3f5df71 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -66,6 +66,7 @@ virCapabilitiesSetNetPrefix;
virCPUDefAddFeature;
virCPUDefCopy;
virCPUDefCopyModel;
+virCPUDefCopyModelFilter;
virCPUDefCopyWithoutModel;
virCPUDefFormat;
virCPUDefFormatBuf;
--
2.10.0