diff options
author | Lars-Peter Clausen <lars@metafoo.de> | 2014-02-14 09:19:00 -0500 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2014-02-18 05:31:11 -0500 |
commit | 7bbcf7e13695c70f13b2cae59392016c0fa2e7a6 (patch) | |
tree | 0924f4ccf137451f6186fb889b8c0a6bdb9a5e90 /drivers/iio | |
parent | 77bfa8baa0c230eb3d8acccd7d341f406a32cdf4 (diff) |
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: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r-- | drivers/iio/industrialio-core.c | 39 |
1 files changed, 13 insertions, 26 deletions
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 39fb8cc46373..ede16aec20fb 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c | |||
@@ -540,7 +540,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, | |||
540 | enum iio_shared_by shared_by) | 540 | enum iio_shared_by shared_by) |
541 | { | 541 | { |
542 | int ret = 0; | 542 | int ret = 0; |
543 | char *name_format = NULL; | 543 | char *name = NULL; |
544 | char *full_postfix; | 544 | char *full_postfix; |
545 | sysfs_attr_init(&dev_attr->attr); | 545 | sysfs_attr_init(&dev_attr->attr); |
546 | 546 | ||
@@ -572,16 +572,15 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, | |||
572 | if (chan->differential) { /* Differential can not have modifier */ | 572 | if (chan->differential) { /* Differential can not have modifier */ |
573 | switch (shared_by) { | 573 | switch (shared_by) { |
574 | case IIO_SHARED_BY_ALL: | 574 | case IIO_SHARED_BY_ALL: |
575 | name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); | 575 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
576 | break; | 576 | break; |
577 | case IIO_SHARED_BY_DIR: | 577 | case IIO_SHARED_BY_DIR: |
578 | name_format = kasprintf(GFP_KERNEL, "%s_%s", | 578 | name = kasprintf(GFP_KERNEL, "%s_%s", |
579 | iio_direction[chan->output], | 579 | iio_direction[chan->output], |
580 | full_postfix); | 580 | full_postfix); |
581 | break; | 581 | break; |
582 | case IIO_SHARED_BY_TYPE: | 582 | case IIO_SHARED_BY_TYPE: |
583 | name_format | 583 | name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", |
584 | = kasprintf(GFP_KERNEL, "%s_%s-%s_%s", | ||
585 | iio_direction[chan->output], | 584 | iio_direction[chan->output], |
586 | iio_chan_type_name_spec[chan->type], | 585 | iio_chan_type_name_spec[chan->type], |
587 | iio_chan_type_name_spec[chan->type], | 586 | iio_chan_type_name_spec[chan->type], |
@@ -593,8 +592,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, | |||
593 | ret = -EINVAL; | 592 | ret = -EINVAL; |
594 | goto error_free_full_postfix; | 593 | goto error_free_full_postfix; |
595 | } | 594 | } |
596 | name_format | 595 | name = kasprintf(GFP_KERNEL, |
597 | = kasprintf(GFP_KERNEL, | ||
598 | "%s_%s%d-%s%d_%s", | 596 | "%s_%s%d-%s%d_%s", |
599 | iio_direction[chan->output], | 597 | iio_direction[chan->output], |
600 | iio_chan_type_name_spec[chan->type], | 598 | iio_chan_type_name_spec[chan->type], |
@@ -607,16 +605,15 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, | |||
607 | } else { /* Single ended */ | 605 | } else { /* Single ended */ |
608 | switch (shared_by) { | 606 | switch (shared_by) { |
609 | case IIO_SHARED_BY_ALL: | 607 | case IIO_SHARED_BY_ALL: |
610 | name_format = kasprintf(GFP_KERNEL, "%s", full_postfix); | 608 | name = kasprintf(GFP_KERNEL, "%s", full_postfix); |
611 | break; | 609 | break; |
612 | case IIO_SHARED_BY_DIR: | 610 | case IIO_SHARED_BY_DIR: |
613 | name_format = kasprintf(GFP_KERNEL, "%s_%s", | 611 | name = kasprintf(GFP_KERNEL, "%s_%s", |
614 | iio_direction[chan->output], | 612 | iio_direction[chan->output], |
615 | full_postfix); | 613 | full_postfix); |
616 | break; | 614 | break; |
617 | case IIO_SHARED_BY_TYPE: | 615 | case IIO_SHARED_BY_TYPE: |
618 | name_format | 616 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
619 | = kasprintf(GFP_KERNEL, "%s_%s_%s", | ||
620 | iio_direction[chan->output], | 617 | iio_direction[chan->output], |
621 | iio_chan_type_name_spec[chan->type], | 618 | iio_chan_type_name_spec[chan->type], |
622 | full_postfix); | 619 | full_postfix); |
@@ -624,33 +621,24 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, | |||
624 | 621 | ||
625 | case IIO_SEPARATE: | 622 | case IIO_SEPARATE: |
626 | if (chan->indexed) | 623 | if (chan->indexed) |
627 | name_format | 624 | name = kasprintf(GFP_KERNEL, "%s_%s%d_%s", |
628 | = kasprintf(GFP_KERNEL, "%s_%s%d_%s", | ||
629 | iio_direction[chan->output], | 625 | iio_direction[chan->output], |
630 | iio_chan_type_name_spec[chan->type], | 626 | iio_chan_type_name_spec[chan->type], |
631 | chan->channel, | 627 | chan->channel, |
632 | full_postfix); | 628 | full_postfix); |
633 | else | 629 | else |
634 | name_format | 630 | name = kasprintf(GFP_KERNEL, "%s_%s_%s", |
635 | = kasprintf(GFP_KERNEL, "%s_%s_%s", | ||
636 | iio_direction[chan->output], | 631 | iio_direction[chan->output], |
637 | iio_chan_type_name_spec[chan->type], | 632 | iio_chan_type_name_spec[chan->type], |
638 | full_postfix); | 633 | full_postfix); |
639 | break; | 634 | break; |
640 | } | 635 | } |
641 | } | 636 | } |
642 | if (name_format == NULL) { | 637 | if (name == NULL) { |
643 | ret = -ENOMEM; | 638 | ret = -ENOMEM; |
644 | goto error_free_full_postfix; | 639 | goto error_free_full_postfix; |
645 | } | 640 | } |
646 | dev_attr->attr.name = kasprintf(GFP_KERNEL, | 641 | dev_attr->attr.name = name; |
647 | name_format, | ||
648 | chan->channel, | ||
649 | chan->channel2); | ||
650 | if (dev_attr->attr.name == NULL) { | ||
651 | ret = -ENOMEM; | ||
652 | goto error_free_name_format; | ||
653 | } | ||
654 | 642 | ||
655 | if (readfunc) { | 643 | if (readfunc) { |
656 | dev_attr->attr.mode |= S_IRUGO; | 644 | dev_attr->attr.mode |= S_IRUGO; |
@@ -661,8 +649,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr, | |||
661 | dev_attr->attr.mode |= S_IWUSR; | 649 | dev_attr->attr.mode |= S_IWUSR; |
662 | dev_attr->store = writefunc; | 650 | dev_attr->store = writefunc; |
663 | } | 651 | } |
664 | error_free_name_format: | 652 | |
665 | kfree(name_format); | ||
666 | error_free_full_postfix: | 653 | error_free_full_postfix: |
667 | kfree(full_postfix); | 654 | kfree(full_postfix); |
668 | 655 | ||