From fdfe4b2837c3408edc57f6d3bf393dc16cd0ac64 Mon Sep 17 00:00:00 2001 From: Jonathon Jongsma Date: Thu, 15 Jul 2021 17:07:32 -0500 Subject: [PATCH] nodedev: add internal virNodeDeviceObjListFind() This is a generic function that you can provide your own predicate function to search for a particular device. It will be used in an upcoming commit. Signed-off-by: Jonathon Jongsma Reviewed-by: Michal Privoznik --- src/conf/virnodedeviceobj.c | 53 +++++++++++++++++++++++++++++++------ src/conf/virnodedeviceobj.h | 11 +++++--- src/libvirt_private.syms | 1 + 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index e8f6792138..6cf4d8b15a 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -1023,9 +1023,9 @@ virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj, } -struct virNodeDeviceObjListRemoveHelperData -{ - virNodeDeviceObjListRemoveIterator callback; +typedef struct _PredicateHelperData PredicateHelperData; +struct _PredicateHelperData { + virNodeDeviceObjListPredicate predicate; void *opaque; }; @@ -1033,9 +1033,9 @@ static int virNodeDeviceObjListRemoveHelper(void *key G_GNUC_UNUSED, void *value, void *opaque) { - struct virNodeDeviceObjListRemoveHelperData *data = opaque; + PredicateHelperData *data = opaque; - return data->callback(value, data->opaque); + return data->predicate(value, data->opaque); } @@ -1051,11 +1051,11 @@ static int virNodeDeviceObjListRemoveHelper(void *key G_GNUC_UNUSED, */ void virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs, - virNodeDeviceObjListRemoveIterator callback, + virNodeDeviceObjListPredicate callback, void *opaque) { - struct virNodeDeviceObjListRemoveHelperData data = { - .callback = callback, + PredicateHelperData data = { + .predicate = callback, .opaque = opaque }; @@ -1065,3 +1065,40 @@ virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs, &data); virObjectRWUnlock(devs); } + + +static int virNodeDeviceObjListFindHelper(const void *payload, + const char *name G_GNUC_UNUSED, + const void *opaque) +{ + PredicateHelperData *data = (PredicateHelperData *) opaque; + virNodeDeviceObj *obj = (virNodeDeviceObj *) payload; + + return data->predicate(obj, data->opaque); +} + + +/** + * virNodeDeviceObjListFind + * @devs: Pointer to object list + * @predicate: function to test the device for a certain property + * @opaque: Opaque data to use as argument to helper + * + * For each object in @devs, call the @predicate helper using @opaque as + * an argument until it returns TRUE. The list may not be modified while + * iterating. + */ +virNodeDeviceObj * +virNodeDeviceObjListFind(virNodeDeviceObjList *devs, + virNodeDeviceObjListPredicate predicate, + void *opaque) +{ + PredicateHelperData data = { + .predicate = predicate, + .opaque = opaque + }; + + return virNodeDeviceObjListSearch(devs, + virNodeDeviceObjListFindHelper, + &data); +} diff --git a/src/conf/virnodedeviceobj.h b/src/conf/virnodedeviceobj.h index 7353e4619b..0cb78748a4 100644 --- a/src/conf/virnodedeviceobj.h +++ b/src/conf/virnodedeviceobj.h @@ -136,9 +136,14 @@ void virNodeDeviceObjSetPersistent(virNodeDeviceObj *obj, bool persistent); -typedef bool (*virNodeDeviceObjListRemoveIterator)(virNodeDeviceObj *obj, - const void *opaque); +typedef bool (*virNodeDeviceObjListPredicate)(virNodeDeviceObj *obj, + const void *opaque); void virNodeDeviceObjListForEachRemove(virNodeDeviceObjList *devs, - virNodeDeviceObjListRemoveIterator callback, + virNodeDeviceObjListPredicate callback, void *opaque); + +virNodeDeviceObj * +virNodeDeviceObjListFind(virNodeDeviceObjList *devs, + virNodeDeviceObjListPredicate callback, + void *opaque); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e4168a06b9..6fc8239d2e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1290,6 +1290,7 @@ virNodeDeviceObjIsActive; virNodeDeviceObjIsPersistent; virNodeDeviceObjListAssignDef; virNodeDeviceObjListExport; +virNodeDeviceObjListFind; virNodeDeviceObjListFindByName; virNodeDeviceObjListFindBySysfsPath; virNodeDeviceObjListFindMediatedDeviceByUUID;