From 8dfb386936ab6d405a731217a95c020a0c0b02e9 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Thu, 26 Apr 2018 16:25:18 +0200 Subject: [PATCH] lib: Simplify device discovery Use GPtrArray instead of open-coding a NULL terminated array. https://bugs.freedesktop.org/show_bug.cgi?id=106279 --- libfprint/core.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/libfprint/core.c b/libfprint/core.c index d9c1ba4..fc5b94b 100644 --- a/libfprint/core.c +++ b/libfprint/core.c @@ -539,11 +539,9 @@ static struct fp_dscv_dev *discover_dev(libusb_device *udev) */ API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void) { - GSList *tmplist = NULL; - struct fp_dscv_dev **list; + GPtrArray *tmparray; libusb_device *udev; libusb_device **devs; - int dscv_count = 0; int r; int i = 0; @@ -556,11 +554,10 @@ API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void) return NULL; } + tmparray = g_ptr_array_new (); + /* Check each device against each driver, temporarily storing successfully - * discovered devices in a GSList. - * - * Quite inefficient but excusable as we'll only be dealing with small - * sets of drivers against small sets of USB devices */ + * discovered devices in a GPtrArray. */ while ((udev = devs[i++]) != NULL) { struct fp_dscv_dev *ddev = discover_dev(udev); if (!ddev) @@ -568,25 +565,14 @@ API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void) /* discover_dev() doesn't hold a reference to the udev, * so increase the reference for ddev to hold this ref */ libusb_ref_device(udev); - tmplist = g_slist_prepend(tmplist, (gpointer) ddev); - dscv_count++; + g_ptr_array_add (tmparray, (gpointer) ddev); } libusb_free_device_list(devs, 1); - /* Convert our temporary GSList into a standard NULL-terminated pointer + /* Convert our temporary array into a standard NULL-terminated pointer * array. */ - list = g_malloc(sizeof(*list) * (dscv_count + 1)); - if (dscv_count > 0) { - GSList *elem = tmplist; - i = 0; - do { - list[i++] = elem->data; - } while ((elem = g_slist_next(elem))); - } - list[dscv_count] = NULL; /* NULL-terminate */ - - g_slist_free(tmplist); - return list; + g_ptr_array_add (tmparray, NULL); + return (struct fp_dscv_dev **) g_ptr_array_free (tmparray, FALSE); } /** \ingroup dscv_dev -- 2.17.0