From: Lars-Peter Clausen Date: Mon, 16 May 2016 09:37:23 +0000 (+0200) Subject: Simplify single device list handling X-Git-Tag: libsigrok-0.5.0~367 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=43376f3324cddd257a0260b06d9db8180c35fdf3;p=libsigrok.git Simplify single device list handling Some driver scan() functions only ever return a single device. For those it is possible to slightly simplify the handling of the device list by creating it on demand when the function returns. Some drivers also have the following expression: devices = g_slist_append(devices, sdi); ... if (!devices) ... This check will never evaluate to false so it is dropped as well. Signed-off-by: Lars-Peter Clausen --- diff --git a/src/hardware/arachnid-labs-re-load-pro/api.c b/src/hardware/arachnid-labs-re-load-pro/api.c index 923f6b01..79fa4242 100644 --- a/src/hardware/arachnid-labs-re-load-pro/api.c +++ b/src/hardware/arachnid-labs-re-load-pro/api.c @@ -65,14 +65,13 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct sr_serial_dev_inst *serial; struct sr_channel_group *cg; struct sr_channel *ch; - GSList *l, *devices; + GSList *l; int ret, len; const char *conn, *serialcomm; char buf[100]; char *bufptr; double version; - devices = NULL; drvc = di->context; conn = serialcomm = NULL; @@ -145,13 +144,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devc = g_malloc0(sizeof(struct dev_context)); sdi->priv = devc; - devices = g_slist_append(devices, sdi); serial_close(serial); - if (!devices) - sr_serial_dev_inst_free(serial); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int config_list(uint32_t key, GVariant **data, diff --git a/src/hardware/asix-sigma/api.c b/src/hardware/asix-sigma/api.c index 8f04809e..a102b03b 100644 --- a/src/hardware/asix-sigma/api.c +++ b/src/hardware/asix-sigma/api.c @@ -65,7 +65,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct sr_dev_inst *sdi; struct dev_context *devc; - GSList *devices; struct ftdi_device_list *devlist; char serial_txt[10]; uint32_t serial; @@ -74,8 +73,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) (void)options; - devices = NULL; - devc = g_malloc0(sizeof(struct dev_context)); ftdi_init(&devc->ftdic); @@ -120,13 +117,12 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) for (i = 0; i < ARRAY_SIZE(channel_names); i++) sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]); - devices = g_slist_append(devices, sdi); sdi->priv = devc; /* We will open the device again when we need it. */ ftdi_list_free(&devlist); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); free: ftdi_deinit(&devc->ftdic); diff --git a/src/hardware/atten-pps3xxx/api.c b/src/hardware/atten-pps3xxx/api.c index 3b9f1133..34a31cea 100644 --- a/src/hardware/atten-pps3xxx/api.c +++ b/src/hardware/atten-pps3xxx/api.c @@ -84,7 +84,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options, int modelid) struct sr_channel *ch; struct sr_channel_group *cg; struct sr_serial_dev_inst *serial; - GSList *l, *devices; + GSList *l; const struct pps_model *model; uint8_t packet[PACKET_SIZE]; unsigned int i; @@ -92,7 +92,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options, int modelid) const char *conn, *serialcomm; char channel[10]; - devices = NULL; drvc = di->context; conn = serialcomm = NULL; @@ -175,13 +174,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options, int modelid) devc->config = g_malloc0(sizeof(struct per_channel_config) * model->num_channels); devc->delay_ms = delay_ms; sdi->priv = devc; - devices = g_slist_append(devices, sdi); serial_close(serial); - if (!devices) - sr_serial_dev_inst_free(serial); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static GSList *scan_3203(struct sr_dev_driver *di, GSList *options) diff --git a/src/hardware/baylibre-acme/api.c b/src/hardware/baylibre-acme/api.c index 2072ccbd..ed21b921 100644 --- a/src/hardware/baylibre-acme/api.c +++ b/src/hardware/baylibre-acme/api.c @@ -55,14 +55,11 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct dev_context *devc; struct sr_dev_inst *sdi; - GSList *devices; gboolean status; int i; (void)options; - devices = NULL; - devc = g_malloc0(sizeof(struct dev_context)); devc->samplerate = SR_HZ(10); @@ -120,9 +117,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) if (!sdi->channel_groups) goto err_out; - devices = g_slist_append(devices, sdi); - - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); err_out: g_free(devc); diff --git a/src/hardware/beaglelogic/api.c b/src/hardware/beaglelogic/api.c index a94a917a..f945c3aa 100644 --- a/src/hardware/beaglelogic/api.c +++ b/src/hardware/beaglelogic/api.c @@ -73,14 +73,12 @@ static struct dev_context *beaglelogic_devc_alloc(void) static GSList *scan(struct sr_dev_driver *di, GSList *options) { - GSList *devices, *l; + GSList *l; struct sr_config *src; struct sr_dev_inst *sdi; struct dev_context *devc; int i, maxch; - devices = NULL; - /* Probe for /dev/beaglelogic */ if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS)) return NULL; @@ -128,9 +126,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) channel_names[i]); sdi->priv = devc; - devices = g_slist_append(devices, sdi); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int dev_open(struct sr_dev_inst *sdi) diff --git a/src/hardware/center-3xx/api.c b/src/hardware/center-3xx/api.c index 6b9c0e39..468093c4 100644 --- a/src/hardware/center-3xx/api.c +++ b/src/hardware/center-3xx/api.c @@ -62,14 +62,12 @@ static GSList *center_scan(const char *conn, const char *serialcomm, int idx) struct sr_dev_inst *sdi; struct dev_context *devc; struct sr_serial_dev_inst *serial; - GSList *devices; serial = sr_serial_dev_inst_new(conn, serialcomm); if (serial_open(serial, SERIAL_RDWR) != SR_OK) return NULL; - devices = NULL; serial_flush(serial); sr_info("Found device on port %s.", conn); @@ -86,11 +84,9 @@ static GSList *center_scan(const char *conn, const char *serialcomm, int idx) for (i = 0; i < center_devs[idx].num_channels; i++) sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]); - devices = g_slist_append(devices, sdi); - serial_close(serial); - return devices; + return g_slist_append(NULL, sdi); } static GSList *scan(GSList *options, int idx) diff --git a/src/hardware/colead-slm/api.c b/src/hardware/colead-slm/api.c index ded82dd0..85ccd86c 100644 --- a/src/hardware/colead-slm/api.c +++ b/src/hardware/colead-slm/api.c @@ -47,11 +47,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_config *src; - GSList *devices, *l; + GSList *l; const char *conn, *serialcomm; - devices = NULL; - conn = serialcomm = NULL; for (l = options; l; l = l->next) { src = l->data; @@ -79,9 +77,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->inst_type = SR_INST_SERIAL; sdi->priv = devc; sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); - devices = g_slist_append(devices, sdi); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, diff --git a/src/hardware/conrad-digi-35-cpu/api.c b/src/hardware/conrad-digi-35-cpu/api.c index 442f6c78..04b50bd3 100644 --- a/src/hardware/conrad-digi-35-cpu/api.c +++ b/src/hardware/conrad-digi-35-cpu/api.c @@ -45,10 +45,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct drv_context *drvc; struct sr_config *src; struct sr_serial_dev_inst *serial; - GSList *l, *devices; + GSList *l; const char *conn, *serialcomm; - devices = NULL; drvc = di->context; conn = serialcomm = NULL; @@ -91,9 +90,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->conn = serial; sdi->priv = NULL; sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1"); - devices = g_slist_append(devices, sdi); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, diff --git a/src/hardware/demo/demo.c b/src/hardware/demo/demo.c index 083527c8..fec9065a 100644 --- a/src/hardware/demo/demo.c +++ b/src/hardware/demo/demo.c @@ -259,7 +259,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct sr_channel_group *cg, *acg; struct sr_config *src; struct analog_gen *ag; - GSList *devices, *l; + GSList *l; int num_logic_channels, num_analog_channels, pattern, i; char channel_name[16]; @@ -279,8 +279,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) } } - devices = NULL; - sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INACTIVE; sdi->model = g_strdup("Demo device"); @@ -344,9 +342,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) } sdi->priv = devc; - devices = g_slist_append(devices, sdi); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int dev_open(struct sr_dev_inst *sdi) diff --git a/src/hardware/ikalogic-scanaplus/api.c b/src/hardware/ikalogic-scanaplus/api.c index 30b62e3c..520bf6fe 100644 --- a/src/hardware/ikalogic-scanaplus/api.c +++ b/src/hardware/ikalogic-scanaplus/api.c @@ -68,7 +68,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct sr_dev_inst *sdi; struct drv_context *drvc; struct dev_context *devc; - GSList *devices; unsigned int i; int ret; @@ -76,8 +75,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) drvc = di->context; - devices = NULL; - /* Allocate memory for our private device context. */ devc = g_malloc0(sizeof(struct dev_context)); @@ -120,12 +117,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) for (i = 0; i < ARRAY_SIZE(channel_names); i++) sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]); - devices = g_slist_append(devices, sdi); - /* Close device. We'll reopen it again when we need it. */ scanaplus_close(devc); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); scanaplus_close(devc); err_free_ftdic: diff --git a/src/hardware/korad-kaxxxxp/api.c b/src/hardware/korad-kaxxxxp/api.c index be472568..15cc5294 100644 --- a/src/hardware/korad-kaxxxxp/api.c +++ b/src/hardware/korad-kaxxxxp/api.c @@ -65,7 +65,7 @@ static const struct korad_kaxxxxp_model models[] = { static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct dev_context *devc; - GSList *devices, *l; + GSList *l; struct sr_dev_inst *sdi; struct sr_config *src; const char *conn, *serialcomm; @@ -74,7 +74,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) int i, model_id; unsigned int len; - devices = NULL; conn = NULL; serialcomm = NULL; @@ -151,13 +150,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) /* Get current status of device. */ if (korad_kaxxxxp_get_all_values(serial, devc) < 0) goto exit_err; - devices = g_slist_append(devices, sdi); serial_close(serial); - if (!devices) - sr_serial_dev_inst_free(serial); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); exit_err: sr_dev_inst_free(sdi); diff --git a/src/hardware/manson-hcs-3xxx/api.c b/src/hardware/manson-hcs-3xxx/api.c index b6449cf6..6b6b12e4 100644 --- a/src/hardware/manson-hcs-3xxx/api.c +++ b/src/hardware/manson-hcs-3xxx/api.c @@ -79,12 +79,11 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_config *src; - GSList *devices, *l; + GSList *l; const char *conn, *serialcomm; struct sr_serial_dev_inst *serial; char reply[50], **tokens, *dummy; - devices = NULL; conn = NULL; serialcomm = NULL; devc = NULL; @@ -174,13 +173,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devc->voltage_max_device = g_strtod(tokens[0], &dummy) * devc->model->voltage[2]; g_strfreev(tokens); - devices = g_slist_append(devices, sdi); - serial_close(serial); - if (!devices) - sr_serial_dev_inst_free(serial); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); exit_err: sr_dev_inst_free(sdi); diff --git a/src/hardware/mic-985xx/api.c b/src/hardware/mic-985xx/api.c index d29cc7ef..f12aa845 100644 --- a/src/hardware/mic-985xx/api.c +++ b/src/hardware/mic-985xx/api.c @@ -62,14 +62,12 @@ static GSList *mic_scan(const char *conn, const char *serialcomm, int idx) struct sr_dev_inst *sdi; struct dev_context *devc; struct sr_serial_dev_inst *serial; - GSList *devices; serial = sr_serial_dev_inst_new(conn, serialcomm); if (serial_open(serial, SERIAL_RDWR) != SR_OK) return NULL; - devices = NULL; serial_flush(serial); /* TODO: Query device type. */ @@ -93,11 +91,9 @@ static GSList *mic_scan(const char *conn, const char *serialcomm, int idx) if (mic_devs[idx].has_humidity) sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity"); - devices = g_slist_append(devices, sdi); - serial_close(serial); - return std_scan_complete(mic_devs[idx].di, devices); + return std_scan_complete(mic_devs[idx].di, g_slist_append(NULL, sdi)); } static GSList *scan(GSList *options, int idx) diff --git a/src/hardware/motech-lps-30x/api.c b/src/hardware/motech-lps-30x/api.c index ee1f4f7e..abc53ce3 100644 --- a/src/hardware/motech-lps-30x/api.c +++ b/src/hardware/motech-lps-30x/api.c @@ -370,7 +370,6 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o struct sr_serial_dev_inst *serial; struct sr_channel *ch; struct sr_channel_group *cg; - GSList *devices; const char *conn, *serialcomm; int cnt, ret; gchar buf[LINELEN_MAX]; @@ -380,7 +379,6 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o sdi = NULL; devc = NULL; conn = serialcomm = NULL; - devices = NULL; sr_spew("scan() called!"); @@ -468,17 +466,13 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); } - devices = g_slist_append(devices, sdi); - /* Query status */ if (lps_query_status(sdi) != SR_OK) goto exit_err; serial_close(serial); - if (!devices) - sr_serial_dev_inst_free(serial); - return std_scan_complete(drv, devices); + return std_scan_complete(drv, g_slist_append(NULL, sdi)); exit_err: sr_info("%s: Error!", __func__); diff --git a/src/hardware/openbench-logic-sniffer/api.c b/src/hardware/openbench-logic-sniffer/api.c index ecd8a081..970d4a61 100644 --- a/src/hardware/openbench-logic-sniffer/api.c +++ b/src/hardware/openbench-logic-sniffer/api.c @@ -90,14 +90,12 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct sr_config *src; struct sr_dev_inst *sdi; struct sr_serial_dev_inst *serial; - GSList *l, *devices; + GSList *l; int ret; unsigned int i; const char *conn, *serialcomm; char buf[8]; - devices = NULL; - conn = serialcomm = NULL; for (l = options; l; l = l->next) { src = l->data; @@ -191,11 +189,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->inst_type = SR_INST_SERIAL; sdi->conn = serial; - devices = g_slist_append(devices, sdi); - serial_close(serial); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, diff --git a/src/hardware/pce-322a/api.c b/src/hardware/pce-322a/api.c index 25094470..d03b34a7 100644 --- a/src/hardware/pce-322a/api.c +++ b/src/hardware/pce-322a/api.c @@ -63,7 +63,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct sr_config *src; struct sr_serial_dev_inst *serial; struct sr_dev_inst *sdi; - GSList *l, *devices; + GSList *l; const char *conn; conn = NULL; @@ -80,8 +80,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) if (serial_open(serial, SERIAL_RDONLY) != SR_OK) return NULL; - devices = NULL; - sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INACTIVE; sdi->vendor = g_strdup("PCE"); @@ -92,11 +90,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->inst_type = SR_INST_SERIAL; sdi->priv = devc; sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); - devices = g_slist_append(devices, sdi); serial_close(serial); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int dev_clear(const struct sr_dev_driver *di) diff --git a/src/hardware/tondaj-sl-814/api.c b/src/hardware/tondaj-sl-814/api.c index cdb6f186..e21d1b5c 100644 --- a/src/hardware/tondaj-sl-814/api.c +++ b/src/hardware/tondaj-sl-814/api.c @@ -45,14 +45,12 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_config *src; - GSList *devices, *l; + GSList *l; const char *conn, *serialcomm; struct sr_serial_dev_inst *serial; drvc = di->context; - devices = NULL; - conn = serialcomm = NULL; for (l = options; l; l = l->next) { if (!(src = l->data)) { @@ -93,9 +91,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->priv = devc; sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); - devices = g_slist_append(devices, sdi); - return std_scan_complete(di, devices); + return std_scan_complete(di, g_slist_append(NULL, sdi)); } static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,