diff mbox series

[v1,2/2] iio: core: Calculate alloc_size only once in iio_device_alloc()

Message ID 20240304140650.977784-3-andriy.shevchenko@linux.intel.com (mailing list archive)
State Accepted
Headers show
Series iio: core: Fix and cleanup to iio_device_alloc() | expand

Commit Message

Andy Shevchenko March 4, 2024, 2:04 p.m. UTC
No need to rewrite the value, instead use 'else' branch.
This will also help further refactoring the code later on.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/iio/industrialio-core.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

David Lechner March 4, 2024, 2:38 p.m. UTC | #1
On Mon, Mar 4, 2024 at 8:07 AM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> No need to rewrite the value, instead use 'else' branch.
> This will also help further refactoring the code later on.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---

Reviewed-by: David Lechner <dlechner@baylibre.com>
Christophe JAILLET March 9, 2024, 6:42 p.m. UTC | #2
Le 04/03/2024 à 15:04, Andy Shevchenko a écrit :
> No need to rewrite the value, instead use 'else' branch.
> This will also help further refactoring the code later on.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
> ---
>   drivers/iio/industrialio-core.c | 9 ++++-----
>   1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 8684ba246969..c7ad88932015 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -1643,11 +1643,10 @@ struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
>   	struct iio_dev *indio_dev;
>   	size_t alloc_size;
>   
> -	alloc_size = sizeof(struct iio_dev_opaque);
> -	if (sizeof_priv) {
> -		alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
> -		alloc_size += sizeof_priv;
> -	}
> +	if (sizeof_priv)
> +		alloc_size = ALIGN(sizeof(*iio_dev_opaque), IIO_DMA_MINALIGN) + sizeof_priv;

maybe size_add() to be super-safe?

CJ

> +	else
> +		alloc_size = sizeof(*iio_dev_opaque);
>   
>   	iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
>   	if (!iio_dev_opaque)
diff mbox series

Patch

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 8684ba246969..c7ad88932015 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1643,11 +1643,10 @@  struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
 	struct iio_dev *indio_dev;
 	size_t alloc_size;
 
-	alloc_size = sizeof(struct iio_dev_opaque);
-	if (sizeof_priv) {
-		alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN);
-		alloc_size += sizeof_priv;
-	}
+	if (sizeof_priv)
+		alloc_size = ALIGN(sizeof(*iio_dev_opaque), IIO_DMA_MINALIGN) + sizeof_priv;
+	else
+		alloc_size = sizeof(*iio_dev_opaque);
 
 	iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
 	if (!iio_dev_opaque)