Another case where we can do the same amount of work with fewer lines of redundant code, which will make adding new filters easier. * src/conf/domain_conf.c (virDomainSnapshotNameData): Adjust struct. (virDomainSnapshotObjListCount): Delete, now taken care of... (virDomainSnapshotObjListCopyNames): ...here. (virDomainSnapshotObjListGetNames): Adjust caller to handle counting. (virDomainSnapshotObjListNum): Simplify. --- v2: new patch src/conf/domain_conf.c | 90 ++++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 65 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index adc3b3c..6967557 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -14256,11 +14256,11 @@ virDomainSnapshotObjListDeinit(virDomainSnapshotObjListPtr snapshots) } struct virDomainSnapshotNameData { - int oom; - int numnames; - int maxnames; char **const names; + int maxnames; unsigned int flags; + int count; + bool error; }; static void virDomainSnapshotObjListCopyNames(void *payload, @@ -14270,19 +14270,20 @@ static void virDomainSnapshotObjListCopyNames(void *payload, virDomainSnapshotObjPtr obj = payload; struct virDomainSnapshotNameData *data = opaque; - if (data->oom) + if (data->error) return; /* LIST_ROOTS/LIST_DESCENDANTS was handled by caller, * LIST_METADATA is a no-op if we get this far. */ if ((data->flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) && obj->nchildren) return; - if (data->numnames < data->maxnames) { - if (!(data->names[data->numnames] = strdup(obj->def->name))) - data->oom = 1; - else - data->numnames++; + if (data->names && data->count < data->maxnames && + !(data->names[data->count] = strdup(obj->def->name))) { + data->error = true; + virReportOOMError(); + return; } + data->count++; } int @@ -14291,7 +14292,8 @@ virDomainSnapshotObjListGetNames(virDomainSnapshotObjListPtr snapshots, char **const names, int maxnames, unsigned int flags) { - struct virDomainSnapshotNameData data = { 0, 0, maxnames, names, 0 }; + struct virDomainSnapshotNameData data = { names, maxnames, flags, 0, + false }; int i; if (!from) { @@ -14299,51 +14301,32 @@ virDomainSnapshotObjListGetNames(virDomainSnapshotObjListPtr snapshots, from = &snapshots->metaroot; } - data.flags = flags & ~VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS; + data.flags &= ~VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS; if (flags & VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS) { if (from->def) virDomainSnapshotForEachDescendant(from, virDomainSnapshotObjListCopyNames, &data); - else + else if (names || data.flags) virHashForEach(snapshots->objs, virDomainSnapshotObjListCopyNames, &data); - } else { + else + data.count = virHashSize(snapshots->objs); + } else if (names || data.flags) { virDomainSnapshotForEachChild(from, virDomainSnapshotObjListCopyNames, &data); + } else { + data.count = from->nchildren; } - if (data.oom) { - virReportOOMError(); - goto cleanup; + if (data.error) { + for (i = 0; i < data.count; i++) + VIR_FREE(names[i]); + return -1; } - return data.numnames; - -cleanup: - for (i = 0; i < data.numnames; i++) - VIR_FREE(data.names[i]); - return -1; -} - -struct virDomainSnapshotNumData { - int count; - unsigned int flags; -}; - -static void virDomainSnapshotObjListCount(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) -{ - virDomainSnapshotObjPtr obj = payload; - struct virDomainSnapshotNumData *data = opaque; - - /* LIST_ROOTS/LIST_DESCENDANTS was handled by caller, - * LIST_METADATA is a no-op if we get this far. */ - if ((data->flags & VIR_DOMAIN_SNAPSHOT_LIST_LEAVES) && obj->nchildren) - return; - data->count++; + return data.count; } int @@ -14351,30 +14334,7 @@ virDomainSnapshotObjListNum(virDomainSnapshotObjListPtr snapshots, virDomainSnapshotObjPtr from, unsigned int flags) { - struct virDomainSnapshotNumData data = { 0, 0 }; - - if (!from) { - flags ^= VIR_DOMAIN_SNAPSHOT_LIST_ROOTS; - from = &snapshots->metaroot; - } - - data.flags = flags & ~VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS; - - if (flags & VIR_DOMAIN_SNAPSHOT_LIST_DESCENDANTS) { - if (data.flags || from->def) - virDomainSnapshotForEachDescendant(from, - virDomainSnapshotObjListCount, - &data); - else - data.count = virHashSize(snapshots->objs); - } else if (data.flags) { - virDomainSnapshotForEachChild(from, - virDomainSnapshotObjListCount, &data); - } else { - data.count = from->nchildren; - } - - return data.count; + return virDomainSnapshotObjListGetNames(snapshots, from, NULL, 0, flags); } virDomainSnapshotObjPtr -- 1.7.10.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list