diff options
author | Lars-Peter Clausen <lars@metafoo.de> | 2013-10-07 12:50:00 +0100 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2013-10-12 12:17:34 +0100 |
commit | 84088ebd14aebf1b8499409a037094b9b88e2796 (patch) | |
tree | a239085ff20650319449d811f5e55207ea890df3 /drivers/iio/industrialio-core.c | |
parent | a95194569f697a6cc10d00f9b9b3d21b0b820520 (diff) |
iio: Add a helper to free a list of IIO device attributes
We have the same code to free a IIO device attribute list in multiple place.
This patch adds a new helper function to take care of this and replaces the
custom instances with a call to the helper function. Note that we do not need to
call list_del() for each of the list items since we will never look at any of
the list items nor the list itself again.
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio/industrialio-core.c')
-rw-r--r-- | drivers/iio/industrialio-core.c | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index dc24a9b3d325..572982fe3155 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -794,11 +794,22 @@ static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev, return attrcount; } -static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev, - struct iio_dev_attr *p) +/** + * iio_free_chan_devattr_list() - Free a list of IIO device attributes + * @attr_list: List of IIO device attributes + * + * This function frees the memory allocated for each of the IIO device + * attributes in the list. Note: if you want to reuse the list after calling + * this function you have to reinitialize it using INIT_LIST_HEAD(). + */ +void iio_free_chan_devattr_list(struct list_head *attr_list) { - kfree(p->dev_attr.attr.name); - kfree(p); + struct iio_dev_attr *p, *n; + + list_for_each_entry_safe(p, n, attr_list, l) { + kfree(p->dev_attr.attr.name); + kfree(p); + } } static ssize_t iio_show_dev_name(struct device *dev, @@ -814,7 +825,7 @@ static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); static int iio_device_register_sysfs(struct iio_dev *indio_dev) { int i, ret = 0, attrcount, attrn, attrcount_orig = 0; - struct iio_dev_attr *p, *n; + struct iio_dev_attr *p; struct attribute **attr; /* First count elements in any existing group */ @@ -867,11 +878,7 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev) return 0; error_clear_attrs: - list_for_each_entry_safe(p, n, - &indio_dev->channel_attr_list, l) { - list_del(&p->l); - iio_device_remove_and_free_read_attr(indio_dev, p); - } + iio_free_chan_devattr_list(&indio_dev->channel_attr_list); return ret; } @@ -879,12 +886,7 @@ error_clear_attrs: static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) { - struct iio_dev_attr *p, *n; - - list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) { - list_del(&p->l); - iio_device_remove_and_free_read_attr(indio_dev, p); - } + iio_free_chan_devattr_list(&indio_dev->channel_attr_list); kfree(indio_dev->chan_attr_group.attrs); } |