Skip to content

Commit

Permalink
scan.c: Simplify iio_scan_result_add()
Browse files Browse the repository at this point in the history
- The iio_scan_result_add() function does not need to take an extra
  'num' parameter if that parameter will always take the value 1.

- There's no need to return a double pointer if it's going to be
  deferenced every time into a single pointer.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed May 24, 2021
1 parent f20ffd9 commit e1f6a84
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
6 changes: 3 additions & 3 deletions dns_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ void remove_dup_discovery_data(struct dns_sd_discovery_data **ddata)

int dnssd_context_scan(struct iio_scan_result *scan_result)
{
struct iio_context_info **info;
struct dns_sd_discovery_data *ddata, *ndata;
struct iio_context_info *info;
int ret = 0;

ret = dnssd_find_hosts(&ddata);
Expand All @@ -251,14 +251,14 @@ int dnssd_context_scan(struct iio_scan_result *scan_result)
goto fail;

for (ndata = ddata; ndata->next != NULL; ndata = ndata->next) {
info = iio_scan_result_add(scan_result, 1);
info = iio_scan_result_add(scan_result);
if (!info) {
IIO_ERROR("Out of memory when adding new scan result\n");
ret = -ENOMEM;
break;
}

ret = dnssd_fill_context_info(*info,
ret = dnssd_fill_context_info(info,
ndata->hostname, ndata->addr_str,ndata->port);
if (ret < 0) {
IIO_DEBUG("Failed to add %s (%s) err: %d\n", ndata->hostname, ndata->addr_str, ret);
Expand Down
4 changes: 2 additions & 2 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ struct iio_scan_result {
struct iio_context_info **info;
};

struct iio_context_info ** iio_scan_result_add(
struct iio_scan_result *scan_result, size_t num);
struct iio_context_info *
iio_scan_result_add(struct iio_scan_result *scan_result);

void free_channel(struct iio_channel *chn);
void free_device(struct iio_device *dev);
Expand Down
9 changes: 5 additions & 4 deletions local.c
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,7 @@ static int check_device(void *d, const char *path)

int local_context_scan(struct iio_scan_result *scan_result)
{
struct iio_context_info **info;
struct iio_context_info *info;
bool exists = false;
char *desc, *uri, *machine, buf[2 * BUF_SIZE], names[BUF_SIZE];
int ret;
Expand Down Expand Up @@ -2169,12 +2169,13 @@ int local_context_scan(struct iio_scan_result *scan_result)
if (!uri)
goto err_free_desc;

info = iio_scan_result_add(scan_result, 1);
info = iio_scan_result_add(scan_result);
if (!info)
goto err_free_uri;

info[0]->description = desc;
info[0]->uri = uri;
info->description = desc;
info->uri = uri;

return 0;

err_free_uri:
Expand Down
28 changes: 11 additions & 17 deletions scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,27 @@ void iio_context_info_list_free(struct iio_context_info **list)
free(list);
}

struct iio_context_info ** iio_scan_result_add(
struct iio_scan_result *scan_result, size_t num)
struct iio_context_info *
iio_scan_result_add(struct iio_scan_result *scan_result)
{
struct iio_context_info **info;
size_t old_size, new_size;
size_t i;
size_t size = scan_result->size;

old_size = scan_result->size;
new_size = old_size + num;

info = realloc(scan_result->info, (new_size + 1) * sizeof(*info));
info = realloc(scan_result->info, (size + 2) * sizeof(*info));
if (!info)
return NULL;

scan_result->info = info;
scan_result->size = new_size;
scan_result->size = size + 1;

for (i = old_size; i < new_size; i++) {
/* Make sure iio_context_info_list_free won't overflow */
info[i + 1] = NULL;
/* Make sure iio_context_info_list_free won't overflow */
info[size + 1] = NULL;

info[i] = zalloc(sizeof(**info));
if (!info[i])
return NULL;
}
info[size] = zalloc(sizeof(**info));
if (!info[size])
return NULL;

return &info[old_size];
return info[size];
}

struct iio_scan_context * iio_create_scan_context(
Expand Down
8 changes: 4 additions & 4 deletions usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ static int usb_fill_context_info(struct iio_context_info *info,

int usb_context_scan(struct iio_scan_result *scan_result)
{
struct iio_context_info **info;
struct iio_context_info *info;
libusb_device **device_list;
libusb_context *ctx;
unsigned int i;
Expand All @@ -1228,12 +1228,12 @@ int usb_context_scan(struct iio_scan_result *scan_result)
continue;

if (!iio_usb_match_device(dev, hdl, &intrfc)) {
info = iio_scan_result_add(scan_result, 1);
info = iio_scan_result_add(scan_result);
if (!info)
ret = -ENOMEM;
else
ret = usb_fill_context_info(*info, dev, hdl,
intrfc);
ret = usb_fill_context_info(info, dev, hdl,
intrfc);
}

libusb_close(hdl);
Expand Down

0 comments on commit e1f6a84

Please sign in to comment.