[Spice-devel] [spice-gtk 09/13] win-usb-dev: do not allocate memory for hub devices

Yuri Benditovich yuri.benditovich at daynix.com
Sun Mar 10 14:46:08 UTC 2019


When processing list of USB devices, avoid allocating memory
for devices which later will be skipped. Use existing libusb
struct first to check whether the device shall be excluded.

Signed-off-by: Yuri Benditovich <yuri.benditovich at daynix.com>
---
 src/win-usb-dev.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/src/win-usb-dev.c b/src/win-usb-dev.c
index c74dd57..1ab704d 100644
--- a/src/win-usb-dev.c
+++ b/src/win-usb-dev.c
@@ -97,7 +97,7 @@ static void g_udev_device_print_list(GList *l, const gchar *msg) {}
 #endif
 static void g_udev_device_print(GUdevDevice *udev, const gchar *msg);
 
-static gboolean g_udev_skip_search(GUdevDevice *udev);
+static gboolean g_udev_skip_search(libusb_device *dev);
 
 GQuark g_udev_client_error_quark(void)
 {
@@ -152,13 +152,12 @@ g_udev_client_list_devices(GUdevClient *self, GList **devs,
 
     n = 0;
     for (dev = lusb_list; *dev; dev++) {
+        if (g_udev_skip_search(*dev)) {
+            continue;
+        }
         udevinfo = g_new0(GUdevDeviceInfo, 1);
         get_usb_dev_info(*dev, udevinfo);
         udevice = g_udev_device_new(udevinfo);
-        if (g_udev_skip_search(udevice)) {
-            g_object_unref(udevice);
-            continue;
-        }
         *devs = g_list_prepend(*devs, udevice);
         n++;
     }
@@ -549,21 +548,19 @@ static void g_udev_device_print(GUdevDevice *udev, const gchar *msg)
                 udevinfo->vid, udevinfo->pid, udevinfo->class);
 }
 
-static gboolean g_udev_skip_search(GUdevDevice *udev)
+static gboolean g_udev_skip_search(libusb_device *dev)
 {
-    GUdevDeviceInfo* udevinfo;
     gboolean skip;
+    uint8_t addr  = libusb_get_device_address(dev);
+    struct libusb_device_descriptor desc;
 
-    g_return_val_if_fail(G_UDEV_DEVICE(udev), FALSE);
-
-    udevinfo = udev->priv->udevinfo;
-    g_return_val_if_fail(udevinfo != NULL, FALSE);
+    libusb_get_device_descriptor(dev, &desc);
 
-    skip = ((udevinfo->addr == 0xff) ||  /* root hub (HCD) */
+    skip = ((addr == 0xff) ||  /* root hub (HCD) */
 #if defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x010000FF)
-            (udevinfo->addr == 1) || /* root hub addr for libusbx >= 1.0.13 */
+            (addr == 1) || /* root hub addr for libusbx >= 1.0.13 */
 #endif
-            (udevinfo->class == LIBUSB_CLASS_HUB) || /* hub*/
-            (udevinfo->addr == 0)); /* bad address */
+            (desc.bDeviceClass == LIBUSB_CLASS_HUB) || /* hub*/
+            (addr == 0)); /* bad address */
     return skip;
 }
-- 
2.17.1



More information about the Spice-devel mailing list