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>
---
src/conf/cpu_conf.c | 31 ++++++++++++++++++++++++-------
src/conf/cpu_conf.h | 12 ++++++++++++
src/libvirt_private.syms | 1 +
3 files changed, 37 insertions(+), 7 deletions(-)
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
index 13f3da3..b74c48b 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 ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+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 d866a89..9d2d9b6 100644
--- a/src/conf/cpu_conf.h
+++ b/src/conf/cpu_conf.h
@@ -123,6 +123,18 @@ 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 ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
+virCPUDefCopyModelFilter(virCPUDefPtr dst,
+ const virCPUDef *src,
+ bool resetPolicy,
+ virCPUDefFeatureFilter filter,
+ void *opaque);
void
virCPUDefMoveModel(virCPUDefPtr dst,
virCPUDefPtr src);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1cfebd5..3a33956 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -66,6 +66,7 @@ virCapabilitiesSetNetPrefix;
virCPUDefAddFeature;
virCPUDefCopy;
virCPUDefCopyModel;
+virCPUDefCopyModelFilter;
virCPUDefCopyWithoutModel;
virCPUDefFormat;
virCPUDefFormatBuf;
--
2.9.2