lib: Simplify device discovery

Use GPtrArray instead of open-coding a NULL terminated array.

https://bugs.freedesktop.org/show_bug.cgi?id=106279
This commit is contained in:
Bastien Nocera 2018-04-26 16:25:18 +02:00
parent ba49677794
commit db34837d2d

View file

@ -253,11 +253,9 @@ static struct fp_dscv_dev *discover_dev(libusb_device *udev)
*/ */
API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void) API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void)
{ {
GSList *tmplist = NULL; GPtrArray *tmparray;
struct fp_dscv_dev **list;
libusb_device *udev; libusb_device *udev;
libusb_device **devs; libusb_device **devs;
int dscv_count = 0;
int r; int r;
int i = 0; int i = 0;
@ -270,11 +268,10 @@ API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void)
return NULL; return NULL;
} }
tmparray = g_ptr_array_new ();
/* Check each device against each driver, temporarily storing successfully /* Check each device against each driver, temporarily storing successfully
* discovered devices in a GSList. * discovered devices in a GPtrArray. */
*
* Quite inefficient but excusable as we'll only be dealing with small
* sets of drivers against small sets of USB devices */
while ((udev = devs[i++]) != NULL) { while ((udev = devs[i++]) != NULL) {
struct fp_dscv_dev *ddev = discover_dev(udev); struct fp_dscv_dev *ddev = discover_dev(udev);
if (!ddev) if (!ddev)
@ -282,25 +279,14 @@ API_EXPORTED struct fp_dscv_dev **fp_discover_devs(void)
/* discover_dev() doesn't hold a reference to the udev, /* discover_dev() doesn't hold a reference to the udev,
* so increase the reference for ddev to hold this ref */ * so increase the reference for ddev to hold this ref */
libusb_ref_device(udev); libusb_ref_device(udev);
tmplist = g_slist_prepend(tmplist, (gpointer) ddev); g_ptr_array_add (tmparray, (gpointer) ddev);
dscv_count++;
} }
libusb_free_device_list(devs, 1); 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. */ * array. */
list = g_malloc(sizeof(*list) * (dscv_count + 1)); g_ptr_array_add (tmparray, NULL);
if (dscv_count > 0) { return (struct fp_dscv_dev **) g_ptr_array_free (tmparray, FALSE);
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;
} }
/** /**