Commit 7bbcf7e1 authored by Lars-Peter Clausen's avatar Lars-Peter Clausen Committed by Jonathan Cameron

iio: Avoid unnecessary kasprintf

name_format already contains the final name and no format characters. So the
code basically reads:

	dev_attr->attr.name = kstrdup(GFP_KERNEL, name_format);
	if (dev_attr->attr.name == NULL)
		...
	kfree(name_format);

Which means we can save one alloc and free pair per attribute name if we
directly assign name_format to dev_attr->attr.name.

The patch also renames name_format to name to denote that this is indeed the
final name and has no format characters in it.
Signed-off-by: default avatarLars-Peter Clausen <lars@metafoo.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent 77bfa8ba
...@@ -540,7 +540,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, ...@@ -540,7 +540,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
enum iio_shared_by shared_by) enum iio_shared_by shared_by)
{ {
int ret = 0; int ret = 0;
char *name_format = NULL; char *name = NULL;
char *full_postfix; char *full_postfix;
sysfs_attr_init(&dev_attr->attr); sysfs_attr_init(&dev_attr->attr);
...@@ -572,16 +572,15 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, ...@@ -572,16 +572,15 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
if (chan->differential) { /* Differential can not have modifier */ if (chan->differential) { /* Differential can not have modifier */
switch (shared_by) { switch (shared_by) {
case IIO_SHARED_BY_ALL: case IIO_SHARED_BY_ALL:
name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); name = kasprintf(GFP_KERNEL, "%s", full_postfix);
break; break;
case IIO_SHARED_BY_DIR: case IIO_SHARED_BY_DIR:
name_format = kasprintf(GFP_KERNEL, "%s_%s", name = kasprintf(GFP_KERNEL, "%s_%s",
iio_direction[chan->output], iio_direction[chan->output],
full_postfix); full_postfix);
break; break;
case IIO_SHARED_BY_TYPE: case IIO_SHARED_BY_TYPE:
name_format name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
= kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
iio_direction[chan->output], iio_direction[chan->output],
iio_chan_type_name_spec[chan->type], iio_chan_type_name_spec[chan->type],
iio_chan_type_name_spec[chan->type], iio_chan_type_name_spec[chan->type],
...@@ -593,8 +592,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, ...@@ -593,8 +592,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
ret = -EINVAL; ret = -EINVAL;
goto error_free_full_postfix; goto error_free_full_postfix;
} }
name_format name = kasprintf(GFP_KERNEL,
= kasprintf(GFP_KERNEL,
"%s_%s%d-%s%d_%s", "%s_%s%d-%s%d_%s",
iio_direction[chan->output], iio_direction[chan->output],
iio_chan_type_name_spec[chan->type], iio_chan_type_name_spec[chan->type],
...@@ -607,16 +605,15 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, ...@@ -607,16 +605,15 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
} else { /* Single ended */ } else { /* Single ended */
switch (shared_by) { switch (shared_by) {
case IIO_SHARED_BY_ALL: case IIO_SHARED_BY_ALL:
name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); name = kasprintf(GFP_KERNEL, "%s", full_postfix);
break; break;
case IIO_SHARED_BY_DIR: case IIO_SHARED_BY_DIR:
name_format = kasprintf(GFP_KERNEL, "%s_%s", name = kasprintf(GFP_KERNEL, "%s_%s",
iio_direction[chan->output], iio_direction[chan->output],
full_postfix); full_postfix);
break; break;
case IIO_SHARED_BY_TYPE: case IIO_SHARED_BY_TYPE:
name_format name = kasprintf(GFP_KERNEL, "%s_%s_%s",
= kasprintf(GFP_KERNEL, "%s_%s_%s",
iio_direction[chan->output], iio_direction[chan->output],
iio_chan_type_name_spec[chan->type], iio_chan_type_name_spec[chan->type],
full_postfix); full_postfix);
...@@ -624,33 +621,24 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, ...@@ -624,33 +621,24 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
case IIO_SEPARATE: case IIO_SEPARATE:
if (chan->indexed) if (chan->indexed)
name_format name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
= kasprintf(GFP_KERNEL, "%s_%s%d_%s",
iio_direction[chan->output], iio_direction[chan->output],
iio_chan_type_name_spec[chan->type], iio_chan_type_name_spec[chan->type],
chan->channel, chan->channel,
full_postfix); full_postfix);
else else
name_format name = kasprintf(GFP_KERNEL, "%s_%s_%s",
= kasprintf(GFP_KERNEL, "%s_%s_%s",
iio_direction[chan->output], iio_direction[chan->output],
iio_chan_type_name_spec[chan->type], iio_chan_type_name_spec[chan->type],
full_postfix); full_postfix);
break; break;
} }
} }
if (name_format == NULL) { if (name == NULL) {
ret = -ENOMEM; ret = -ENOMEM;
goto error_free_full_postfix; goto error_free_full_postfix;
} }
dev_attr->attr.name = kasprintf(GFP_KERNEL, dev_attr->attr.name = name;
name_format,
chan->channel,
chan->channel2);
if (dev_attr->attr.name == NULL) {
ret = -ENOMEM;
goto error_free_name_format;
}
if (readfunc) { if (readfunc) {
dev_attr->attr.mode |= S_IRUGO; dev_attr->attr.mode |= S_IRUGO;
...@@ -661,8 +649,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, ...@@ -661,8 +649,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
dev_attr->attr.mode |= S_IWUSR; dev_attr->attr.mode |= S_IWUSR;
dev_attr->store = writefunc; dev_attr->store = writefunc;
} }
error_free_name_format:
kfree(name_format);
error_free_full_postfix: error_free_full_postfix:
kfree(full_postfix); kfree(full_postfix);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment