From nobody Thu May 15 11:29:55 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1507644307078541.8520131469165; Tue, 10 Oct 2017 07:05:07 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E803081E1B; Tue, 10 Oct 2017 14:05:04 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B5CC46C40F; Tue, 10 Oct 2017 14:05:04 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 6719518355DA; Tue, 10 Oct 2017 14:05:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v9ADsYRb015307 for ; Tue, 10 Oct 2017 09:54:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id D814618ABA; Tue, 10 Oct 2017 13:54:34 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-36.phx2.redhat.com [10.3.116.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id A03136C409 for ; Tue, 10 Oct 2017 13:54:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E803081E1B Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: John Ferlan To: libvir-list@redhat.com Date: Tue, 10 Oct 2017 09:54:28 -0400 Message-Id: <20171010135428.23097-4-jferlan@redhat.com> In-Reply-To: <20171010135428.23097-1-jferlan@redhat.com> References: <20171010135428.23097-1-jferlan@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/3] nodedev: Introduce virNodeDeviceObjListForEachCb X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 10 Oct 2017 14:05:06 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Rather than separate functions for NumOfDevices, GetNames, and ListExport - let's converge the code into one handler. Signed-off-by: John Ferlan --- src/conf/virnodedeviceobj.c | 191 +++++++++++++++++-----------------------= ---- 1 file changed, 75 insertions(+), 116 deletions(-) diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index 7db8fccbc3..c4be03d67d 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -730,29 +730,68 @@ virNodeDeviceCapMatch(virNodeDeviceObjPtr obj, } =20 =20 -struct virNodeDeviceCountData { +typedef bool (*virNodeDeviceObjListHasCap)(const virNodeDeviceObj *obj, + const char *cap); +typedef bool (*virNodeDeviceObjListMatch)(virNodeDeviceObjPtr obj, + unsigned int flags); +struct _virNodeDeviceObjForEachData { virConnectPtr conn; virNodeDeviceObjListFilter filter; - const char *matchstr; - int count; + virNodeDeviceObjListHasCap hascap; + const char *cap; + virNodeDeviceObjListMatch match; + unsigned int flags; + bool error; + int nElems; + int maxElems; + char **const elems; + virNodeDevicePtr *devices; }; =20 static int -virNodeDeviceObjListNumOfDevicesCallback(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) +virNodeDeviceObjListForEachCb(void *payload, + const void *name ATTRIBUTE_UNUSED, + void *opaque) { virNodeDeviceObjPtr obj =3D payload; - virNodeDeviceDefPtr def; - struct virNodeDeviceCountData *data =3D opaque; - virNodeDeviceObjListFilter filter =3D data->filter; + struct _virNodeDeviceObjForEachData *data =3D opaque; + virNodeDevicePtr device =3D NULL; + + if (data->error) + return 0; + + if (data->maxElems >=3D 0 && data->nElems =3D=3D data->maxElems) + return 0; =20 virObjectLock(obj); - def =3D obj->def; - if ((!filter || filter(data->conn, def)) && - (!data->matchstr || virNodeDeviceObjHasCap(obj, data->matchstr))) - data->count++; =20 + if (data->filter && !data->filter(data->conn, obj->def)) + goto cleanup; + + if (data->hascap && data->hascap(obj, data->cap)) + goto cleanup; + + if (data->match && !data->match(obj, data->flags)) + goto cleanup; + + if (data->elems) { + if (VIR_STRDUP(data->elems[data->nElems], obj->def->name) < 0) { + data->error =3D true; + goto cleanup; + } + } else if (data->devices) { + if (!(device =3D virGetNodeDevice(data->conn, obj->def->name)) || + VIR_STRDUP(device->parent, obj->def->parent) < 0) { + virObjectUnref(device); + data->error =3D true; + goto cleanup; + } + data->devices[data->nElems] =3D device; + } + + data->nElems++; + + cleanup: virObjectUnlock(obj); return 0; } @@ -764,55 +803,16 @@ virNodeDeviceObjListNumOfDevices(virNodeDeviceObjList= Ptr devs, const char *cap, virNodeDeviceObjListFilter filter) { - struct virNodeDeviceCountData data =3D { - .conn =3D conn, .filter =3D filter, .matchstr =3D cap, .count =3D = 0 }; + struct _virNodeDeviceObjForEachData data =3D { .conn =3D conn, + .filter =3D filter, .hascap =3D virNodeDeviceObjHasCap, .cap =3D c= ap, + .match =3D NULL, .flags =3D 0, .error =3D false, .nElems =3D 0, .m= axElems =3D -1, + .elems =3D NULL, .devices =3D NULL }; =20 virObjectRWLockRead(devs); - virHashForEach(devs->objs, virNodeDeviceObjListNumOfDevicesCallback, &= data); + virHashForEach(devs->objs, virNodeDeviceObjListForEachCb, &data); virObjectRWUnlock(devs); =20 - return data.count; -} - - -struct virNodeDeviceGetNamesData { - virConnectPtr conn; - virNodeDeviceObjListFilter filter; - const char *matchstr; - int nnames; - char **names; - int maxnames; - bool error; -}; - -static int -virNodeDeviceObjListGetNamesCallback(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) -{ - virNodeDeviceObjPtr obj =3D payload; - virNodeDeviceDefPtr def; - struct virNodeDeviceGetNamesData *data =3D opaque; - virNodeDeviceObjListFilter filter =3D data->filter; - - if (data->error) - return 0; - - virObjectLock(obj); - def =3D obj->def; - - if ((!filter || filter(data->conn, def)) && - (!data->matchstr || virNodeDeviceObjHasCap(obj, data->matchstr))) { - if (VIR_STRDUP(data->names[data->nnames], def->name) < 0) { - data->error =3D true; - goto cleanup; - } - data->nnames++; - } - - cleanup: - virObjectUnlock(obj); - return 0; + return data.nElems; } =20 =20 @@ -824,22 +824,23 @@ virNodeDeviceObjListGetNames(virNodeDeviceObjListPtr = devs, char **const names, int maxnames) { - struct virNodeDeviceGetNamesData data =3D { - .conn =3D conn, .filter =3D filter, .matchstr =3D cap, .names =3D = names, - .nnames =3D 0, .maxnames =3D maxnames, .error =3D false }; + struct _virNodeDeviceObjForEachData data =3D { .conn =3D conn, + .filter =3D filter, .hascap =3D virNodeDeviceObjHasCap, .cap =3D c= ap, + .match =3D NULL, .flags =3D 0, .error =3D false, .nElems =3D 0, + .maxElems =3D maxnames, .elems =3D names, .devices =3D NULL }; =20 virObjectRWLockRead(devs); - virHashForEach(devs->objs, virNodeDeviceObjListGetNamesCallback, &data= ); + virHashForEach(devs->objs, virNodeDeviceObjListForEachCb, &data); virObjectRWUnlock(devs); =20 if (data.error) goto error; =20 - return data.nnames; + return data.nElems; =20 error: - while (--data.nnames) - VIR_FREE(data.names[data.nnames]); + while (--data.nElems) + VIR_FREE(data.elems[data.nElems]); return -1; } =20 @@ -876,51 +877,6 @@ virNodeDeviceMatch(virNodeDeviceObjPtr obj, #undef MATCH =20 =20 -struct virNodeDeviceObjListExportData { - virConnectPtr conn; - virNodeDeviceObjListFilter filter; - unsigned int flags; - virNodeDevicePtr *devices; - int ndevices; - bool error; -}; - -static int -virNodeDeviceObjListExportCallback(void *payload, - const void *name ATTRIBUTE_UNUSED, - void *opaque) -{ - virNodeDeviceObjPtr obj =3D payload; - virNodeDeviceDefPtr def; - struct virNodeDeviceObjListExportData *data =3D opaque; - virNodeDevicePtr device =3D NULL; - - if (data->error) - return 0; - - virObjectLock(obj); - def =3D obj->def; - - if ((!data->filter || data->filter(data->conn, def)) && - virNodeDeviceMatch(obj, data->flags)) { - if (data->devices) { - if (!(device =3D virGetNodeDevice(data->conn, def->name)) || - VIR_STRDUP(device->parent, def->parent) < 0) { - virObjectUnref(device); - data->error =3D true; - goto cleanup; - } - data->devices[data->ndevices] =3D device; - } - data->ndevices++; - } - - cleanup: - virObjectUnlock(obj); - return 0; -} - - int virNodeDeviceObjListExport(virConnectPtr conn, virNodeDeviceObjListPtr devs, @@ -928,9 +884,10 @@ virNodeDeviceObjListExport(virConnectPtr conn, virNodeDeviceObjListFilter filter, unsigned int flags) { - struct virNodeDeviceObjListExportData data =3D { - .conn =3D conn, .filter =3D filter, .flags =3D flags, - .devices =3D NULL, .ndevices =3D 0, .error =3D false }; + struct _virNodeDeviceObjForEachData data =3D { .conn =3D conn, + .filter =3D filter, .hascap =3D NULL, .cap =3D NULL, + .match =3D virNodeDeviceMatch, .flags =3D flags, .error =3D false, + .nElems =3D 0, .maxElems =3D -1, .elems =3D NULL, .devices =3D NU= LL }; =20 virObjectRWLockRead(devs); if (devices && @@ -938,19 +895,21 @@ virNodeDeviceObjListExport(virConnectPtr conn, virObjectRWUnlock(devs); return -1; } + if (data.devices) + data.maxElems =3D virHashSize(devs->objs) + 1; =20 - virHashForEach(devs->objs, virNodeDeviceObjListExportCallback, &data); + virHashForEach(devs->objs, virNodeDeviceObjListForEachCb, &data); virObjectRWUnlock(devs); =20 if (data.error) goto cleanup; =20 if (data.devices) { - ignore_value(VIR_REALLOC_N(data.devices, data.ndevices + 1)); + ignore_value(VIR_REALLOC_N(data.devices, data.nElems + 1)); *devices =3D data.devices; } =20 - return data.ndevices; + return data.nElems; =20 cleanup: virObjectListFree(data.devices); --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list