From nobody Fri May 3 16:09:33 2024 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 15076442805831022.0475533165925; Tue, 10 Oct 2017 07:04:40 -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 C6C517E44F; Tue, 10 Oct 2017 14:04:38 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 71D866C403; Tue, 10 Oct 2017 14:04:38 +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 61E29B3486; Tue, 10 Oct 2017 14:04:37 +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 v9ADsXWf015286 for ; Tue, 10 Oct 2017 09:54:33 -0400 Received: by smtp.corp.redhat.com (Postfix) id D78076C400; Tue, 10 Oct 2017 13:54:33 +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 9E15F18ABA for ; Tue, 10 Oct 2017 13:54:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C6C517E44F Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.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:26 -0400 Message-Id: <20171010135428.23097-2-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 1/3] nodedev: Convert virNodeDeviceObjList to use RWObjectLockable 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.27]); Tue, 10 Oct 2017 14:04:39 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Let's use the RWObjectLockable for the various list lock mgmt. Only time need Write lock will be for Add/Remove logic. Signed-off-by: John Ferlan --- src/conf/virnodedeviceobj.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index b0dcee16f1..49f4f436b8 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -40,7 +40,7 @@ struct _virNodeDeviceObj { }; =20 struct _virNodeDeviceObjList { - virObjectLockable parent; + virObjectRWLockable parent; =20 /* name string -> virNodeDeviceObj mapping * for O(1), lockless lookup-by-name */ @@ -63,7 +63,7 @@ virNodeDeviceObjOnceInit(void) virNodeDeviceObjDispose))) return -1; =20 - if (!(virNodeDeviceObjListClass =3D virClassNew(virClassForObjectLocka= ble(), + if (!(virNodeDeviceObjListClass =3D virClassNew(virClassForObjectRWLoc= kable(), "virNodeDeviceObjList", sizeof(virNodeDeviceObjL= ist), virNodeDeviceObjListDisp= ose))) @@ -231,10 +231,10 @@ virNodeDeviceObjListSearch(virNodeDeviceObjListPtr de= vs, { virNodeDeviceObjPtr obj; =20 - virObjectLock(devs); + virObjectRWLockRead(devs); obj =3D virHashSearch(devs->objs, callback, data, NULL); virObjectRef(obj); - virObjectUnlock(devs); + virObjectRWUnlock(devs); =20 if (obj) virObjectLock(obj); @@ -284,9 +284,9 @@ virNodeDeviceObjListFindByName(virNodeDeviceObjListPtr = devs, { virNodeDeviceObjPtr obj; =20 - virObjectLock(devs); + virObjectRWLockRead(devs); obj =3D virNodeDeviceObjListFindByNameLocked(devs, name); - virObjectUnlock(devs); + virObjectRWUnlock(devs); if (obj) virObjectLock(obj); =20 @@ -462,7 +462,7 @@ virNodeDeviceObjListNew(void) if (virNodeDeviceObjInitialize() < 0) return NULL; =20 - if (!(devs =3D virObjectLockableNew(virNodeDeviceObjListClass))) + if (!(devs =3D virObjectRWLockableNew(virNodeDeviceObjListClass))) return NULL; =20 if (!(devs->objs =3D virHashCreate(50, virObjectFreeHashData))) { @@ -487,7 +487,7 @@ virNodeDeviceObjListAssignDef(virNodeDeviceObjListPtr d= evs, { virNodeDeviceObjPtr obj; =20 - virObjectLock(devs); + virObjectRWLockWrite(devs); =20 if ((obj =3D virNodeDeviceObjListFindByNameLocked(devs, def->name))) { virObjectLock(obj); @@ -507,7 +507,7 @@ virNodeDeviceObjListAssignDef(virNodeDeviceObjListPtr d= evs, } =20 cleanup: - virObjectUnlock(devs); + virObjectRWUnlock(devs); return obj; } =20 @@ -524,12 +524,12 @@ virNodeDeviceObjListRemove(virNodeDeviceObjListPtr de= vs, =20 virObjectRef(obj); virObjectUnlock(obj); - virObjectLock(devs); + virObjectRWLockWrite(devs); virObjectLock(obj); virHashRemoveEntry(devs->objs, def->name); virObjectUnlock(obj); virObjectUnref(obj); - virObjectUnlock(devs); + virObjectRWUnlock(devs); } =20 =20 @@ -767,9 +767,9 @@ virNodeDeviceObjListNumOfDevices(virNodeDeviceObjListPt= r devs, struct virNodeDeviceCountData data =3D { .conn =3D conn, .filter =3D filter, .matchstr =3D cap, .count =3D = 0 }; =20 - virObjectLock(devs); + virObjectRWLockRead(devs); virHashForEach(devs->objs, virNodeDeviceObjListNumOfDevicesCallback, &= data); - virObjectUnlock(devs); + virObjectRWUnlock(devs); =20 return data.count; } @@ -828,9 +828,9 @@ virNodeDeviceObjListGetNames(virNodeDeviceObjListPtr de= vs, .conn =3D conn, .filter =3D filter, .matchstr =3D cap, .names =3D = names, .nnames =3D 0, .maxnames =3D maxnames, .error =3D false }; =20 - virObjectLock(devs); + virObjectRWLockRead(devs); virHashForEach(devs->objs, virNodeDeviceObjListGetNamesCallback, &data= ); - virObjectUnlock(devs); + virObjectRWUnlock(devs); =20 if (data.error) goto error; @@ -932,15 +932,15 @@ virNodeDeviceObjListExport(virConnectPtr conn, .conn =3D conn, .filter =3D filter, .flags =3D flags, .devices =3D NULL, .ndevices =3D 0, .error =3D false }; =20 - virObjectLock(devs); + virObjectRWLockRead(devs); if (devices && VIR_ALLOC_N(data.devices, virHashSize(devs->objs) + 1) < 0) { - virObjectUnlock(devs); + virObjectRWUnlock(devs); return -1; } =20 virHashForEach(devs->objs, virNodeDeviceObjListExportCallback, &data); - virObjectUnlock(devs); + virObjectRWUnlock(devs); =20 if (data.error) goto cleanup; --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 16:09:33 2024 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 1507644856917981.6527078723993; Tue, 10 Oct 2017 07:14:16 -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 1411E5F2974; Tue, 10 Oct 2017 14:14:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BB08F6C412; Tue, 10 Oct 2017 14:14:14 +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 84E2A410B3; Tue, 10 Oct 2017 14:14:13 +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 v9ADsYFA015298 for ; Tue, 10 Oct 2017 09:54:34 -0400 Received: by smtp.corp.redhat.com (Postfix) id 659CB18ABA; 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 2E8E96C407 for ; Tue, 10 Oct 2017 13:54:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1411E5F2974 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.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:27 -0400 Message-Id: <20171010135428.23097-3-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 2/3] nodedev: Convert virNodeDeviceObjHasCap to bool 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.38]); Tue, 10 Oct 2017 14:14:15 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" It only returns 0 or 1 anyway, let's be realistic Signed-off-by: John Ferlan Reviewed-by: Erik Skultety --- src/conf/virnodedeviceobj.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/conf/virnodedeviceobj.c b/src/conf/virnodedeviceobj.c index 49f4f436b8..7db8fccbc3 100644 --- a/src/conf/virnodedeviceobj.c +++ b/src/conf/virnodedeviceobj.c @@ -120,7 +120,7 @@ virNodeDeviceObjGetDef(virNodeDeviceObjPtr obj) } =20 =20 -static int +static bool virNodeDeviceObjHasCap(const virNodeDeviceObj *obj, const char *cap) { @@ -134,13 +134,13 @@ virNodeDeviceObjHasCap(const virNodeDeviceObj *obj, =20 while (caps) { if (STREQ(cap, virNodeDevCapTypeToString(caps->data.type))) { - return 1; + return true; } else { switch (caps->data.type) { case VIR_NODE_DEV_CAP_PCI_DEV: if ((STREQ(cap, mdev_types)) && (caps->data.pci_dev.flags & VIR_NODE_DEV_CAP_FLAG_PCI_= MDEV)) - return 1; + return true; break; =20 case VIR_NODE_DEV_CAP_SCSI_HOST: @@ -148,7 +148,7 @@ virNodeDeviceObjHasCap(const virNodeDeviceObj *obj, (caps->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HB= A_FC_HOST)) || (STREQ(cap, vports_cap) && (caps->data.scsi_host.flags & VIR_NODE_DEV_CAP_FLAG_HB= A_VPORT_OPS))) - return 1; + return true; break; =20 case VIR_NODE_DEV_CAP_SYSTEM: @@ -172,7 +172,7 @@ virNodeDeviceObjHasCap(const virNodeDeviceObj *obj, =20 caps =3D caps->next; } - return 0; + return false; } =20 =20 --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 16:09:33 2024 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