public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] staging: most: replace multiple if..else with table lookup
@ 2015-12-24 10:49 Gujulan Elango, Hari Prasath (H.)
  2015-12-24 16:06 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Gujulan Elango, Hari Prasath (H.) @ 2015-12-24 10:49 UTC (permalink / raw)
  To: gregkh@linuxfoundation.org, christian.gromm@microchip.com,
	andrey.shvetsov@k2l.de, adrianremonda@gmail.com,
	sudipm.mukherjee@gmail.com
  Cc: devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org

From: Hari Prasath Gujulan Elango <hgujulan@visteon.com>

Replace multiple if..else if..statements with simple table lookup in two
functions.

Signed-off-by: Hari Prasath Gujulan Elango <hgujulan@visteon.com>
---
 drivers/staging/most/mostcore/core.c | 39 ++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/drivers/staging/most/mostcore/core.c b/drivers/staging/most/mostcore/core.c
index ed1ed25..7b4636b 100644
--- a/drivers/staging/most/mostcore/core.c
+++ b/drivers/staging/most/mostcore/core.c
@@ -82,6 +82,14 @@ struct most_inst_obj {
 	struct list_head list;
 };
 
+static const struct {
+	int most_ch_data_type;
+	char *name;
+} ch_data_type[] = { { MOST_CH_CONTROL, "control\n" },
+	{ MOST_CH_ASYNC, "async\n" },
+	{ MOST_CH_SYNC, "sync\n" },
+	{ MOST_CH_ISOC_AVP, "isoc_avp\n"} };
+
 #define to_inst_obj(d) container_of(d, struct most_inst_obj, kobj)
 
 /**
@@ -414,14 +422,12 @@ static ssize_t show_set_datatype(struct most_c_obj *c,
 				 struct most_c_attr *attr,
 				 char *buf)
 {
-	if (c->cfg.data_type & MOST_CH_CONTROL)
-		return snprintf(buf, PAGE_SIZE, "control\n");
-	else if (c->cfg.data_type & MOST_CH_ASYNC)
-		return snprintf(buf, PAGE_SIZE, "async\n");
-	else if (c->cfg.data_type & MOST_CH_SYNC)
-		return snprintf(buf, PAGE_SIZE, "sync\n");
-	else if (c->cfg.data_type & MOST_CH_ISOC_AVP)
-		return snprintf(buf, PAGE_SIZE, "isoc_avp\n");
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
+		if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
+			return snprintf(buf, PAGE_SIZE, ch_data_type[i].name);
+	}
 	return snprintf(buf, PAGE_SIZE, "unconfigured\n");
 }
 
@@ -430,15 +436,14 @@ static ssize_t store_set_datatype(struct most_c_obj *c,
 				  const char *buf,
 				  size_t count)
 {
-	if (!strcmp(buf, "control\n")) {
-		c->cfg.data_type = MOST_CH_CONTROL;
-	} else if (!strcmp(buf, "async\n")) {
-		c->cfg.data_type = MOST_CH_ASYNC;
-	} else if (!strcmp(buf, "sync\n")) {
-		c->cfg.data_type = MOST_CH_SYNC;
-	} else if (!strcmp(buf, "isoc_avp\n")) {
-		c->cfg.data_type = MOST_CH_ISOC_AVP;
-	} else {
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
+		if (!strcmp(buf, ch_data_type[i].name))
+			c->cfg.data_type = ch_data_type[i].most_ch_data_type;
+	}
+
+	if (i == ARRAY_SIZE(ch_data_type)) {
 		pr_info("WARN: invalid attribute settings\n");
 		return -EINVAL;
 	}
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] staging: most: replace multiple if..else with table lookup
  2015-12-24 10:49 [PATCH] staging: most: replace multiple if..else with table lookup Gujulan Elango, Hari Prasath (H.)
@ 2015-12-24 16:06 ` Joe Perches
  2015-12-30 10:44   ` Gujulan Elango, Hari Prasath (H.)
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2015-12-24 16:06 UTC (permalink / raw)
  To: Gujulan Elango, Hari Prasath (H.), gregkh@linuxfoundation.org,
	christian.gromm@microchip.com, andrey.shvetsov@k2l.de,
	adrianremonda@gmail.com, sudipm.mukherjee@gmail.com
  Cc: devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org

On Thu, 2015-12-24 at 10:49 +0000, Gujulan Elango, Hari Prasath (H.) wrote:
> From: Hari Prasath Gujulan Elango <hgujulan@visteon.com>
> 
> Replace multiple if..else if..statements with simple table lookup in two
> functions.
> 
> Signed-off-by: Hari Prasath Gujulan Elango <hgujulan@visteon.com>
> ---
>  drivers/staging/most/mostcore/core.c | 39 ++++++++++++++++++++----------------
>  1 file changed, 22 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/staging/most/mostcore/core.c b/drivers/staging/most/mostcore/core.c
> index ed1ed25..7b4636b 100644
> --- a/drivers/staging/most/mostcore/core.c
> +++ b/drivers/staging/most/mostcore/core.c
> @@ -82,6 +82,14 @@ struct most_inst_obj {
>  	struct list_head list;
>  };
>  
> +static const struct {
> +	int most_ch_data_type;
> +	char *name;
> +} ch_data_type[] = { { MOST_CH_CONTROL, "control\n" },
> +	{ MOST_CH_ASYNC, "async\n" },
> +	{ MOST_CH_SYNC, "sync\n" },
> +	{ MOST_CH_ISOC_AVP, "isoc_avp\n"} };
> +
>  #define to_inst_obj(d) container_of(d, struct most_inst_obj, kobj)
>  
>  /**
> @@ -414,14 +422,12 @@ static ssize_t show_set_datatype(struct most_c_obj *c,
>  				 struct most_c_attr *attr,
>  				 char *buf)
>  {
> -	if (c->cfg.data_type & MOST_CH_CONTROL)
> -		return snprintf(buf, PAGE_SIZE, "control\n");
> -	else if (c->cfg.data_type & MOST_CH_ASYNC)
> -		return snprintf(buf, PAGE_SIZE, "async\n");
> -	else if (c->cfg.data_type & MOST_CH_SYNC)
> -		return snprintf(buf, PAGE_SIZE, "sync\n");
> -	else if (c->cfg.data_type & MOST_CH_ISOC_AVP)
> -		return snprintf(buf, PAGE_SIZE, "isoc_avp\n");
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
> +		if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
> +			return snprintf(buf, PAGE_SIZE, ch_data_type[i].name);
> +	}
>  	return snprintf(buf, PAGE_SIZE, "unconfigured\n");
>  }
>  
> @@ -430,15 +436,14 @@ static ssize_t store_set_datatype(struct most_c_obj *c,
>  				  const char *buf,
>  				  size_t count)
>  {
> -	if (!strcmp(buf, "control\n")) {
> -		c->cfg.data_type = MOST_CH_CONTROL;
> -	} else if (!strcmp(buf, "async\n")) {
> -		c->cfg.data_type = MOST_CH_ASYNC;
> -	} else if (!strcmp(buf, "sync\n")) {
> -		c->cfg.data_type = MOST_CH_SYNC;
> -	} else if (!strcmp(buf, "isoc_avp\n")) {
> -		c->cfg.data_type = MOST_CH_ISOC_AVP;
> -	} else {
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
> +		if (!strcmp(buf, ch_data_type[i].name))
> +			c->cfg.data_type = ch_data_type[i].most_ch_data_type;

Missing braces and break;

> +	}
> +
> +	if (i == ARRAY_SIZE(ch_data_type)) {
>  		pr_info("WARN: invalid attribute settings\n");
>  		return -EINVAL;
>  	}

This seems like a lot of code for a simple test.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] staging: most: replace multiple if..else with table lookup
  2015-12-24 16:06 ` Joe Perches
@ 2015-12-30 10:44   ` Gujulan Elango, Hari Prasath (H.)
  0 siblings, 0 replies; 3+ messages in thread
From: Gujulan Elango, Hari Prasath (H.) @ 2015-12-30 10:44 UTC (permalink / raw)
  To: Joe Perches
  Cc: gregkh@linuxfoundation.org, christian.gromm@microchip.com,
	andrey.shvetsov@k2l.de, adrianremonda@gmail.com,
	sudipm.mukherjee@gmail.com, devel@driverdev.osuosl.org,
	linux-kernel@vger.kernel.org

On Thu, Dec 24, 2015 at 08:06:26AM -0800, Joe Perches wrote:
> On Thu, 2015-12-24 at 10:49 +0000, Gujulan Elango, Hari Prasath (H.) wrote:
> > From: Hari Prasath Gujulan Elango <hgujulan@visteon.com>
> > 
> > Replace multiple if..else if..statements with simple table lookup in two
> > functions.
> > 
> > Signed-off-by: Hari Prasath Gujulan Elango <hgujulan@visteon.com>
> > ---
> >  drivers/staging/most/mostcore/core.c | 39 ++++++++++++++++++++----------------
> >  1 file changed, 22 insertions(+), 17 deletions(-)
> > 
> > diff --git a/drivers/staging/most/mostcore/core.c b/drivers/staging/most/mostcore/core.c
> > index ed1ed25..7b4636b 100644
> > --- a/drivers/staging/most/mostcore/core.c
> > +++ b/drivers/staging/most/mostcore/core.c
> > @@ -82,6 +82,14 @@ struct most_inst_obj {
> >  	struct list_head list;
> >  };
> >  
> > +static const struct {
> > +	int most_ch_data_type;
> > +	char *name;
> > +} ch_data_type[] = { { MOST_CH_CONTROL, "control\n" },
> > +	{ MOST_CH_ASYNC, "async\n" },
> > +	{ MOST_CH_SYNC, "sync\n" },
> > +	{ MOST_CH_ISOC_AVP, "isoc_avp\n"} };
> > +
> >  #define to_inst_obj(d) container_of(d, struct most_inst_obj, kobj)
> >  
> >  /**
> > @@ -414,14 +422,12 @@ static ssize_t show_set_datatype(struct most_c_obj *c,
> >  				 struct most_c_attr *attr,
> >  				 char *buf)
> >  {
> > -	if (c->cfg.data_type & MOST_CH_CONTROL)
> > -		return snprintf(buf, PAGE_SIZE, "control\n");
> > -	else if (c->cfg.data_type & MOST_CH_ASYNC)
> > -		return snprintf(buf, PAGE_SIZE, "async\n");
> > -	else if (c->cfg.data_type & MOST_CH_SYNC)
> > -		return snprintf(buf, PAGE_SIZE, "sync\n");
> > -	else if (c->cfg.data_type & MOST_CH_ISOC_AVP)
> > -		return snprintf(buf, PAGE_SIZE, "isoc_avp\n");
> > +	int i;
> > +
> > +	for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
> > +		if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
> > +			return snprintf(buf, PAGE_SIZE, ch_data_type[i].name);
> > +	}
> >  	return snprintf(buf, PAGE_SIZE, "unconfigured\n");
> >  }
> >  
> > @@ -430,15 +436,14 @@ static ssize_t store_set_datatype(struct most_c_obj *c,
> >  				  const char *buf,
> >  				  size_t count)
> >  {
> > -	if (!strcmp(buf, "control\n")) {
> > -		c->cfg.data_type = MOST_CH_CONTROL;
> > -	} else if (!strcmp(buf, "async\n")) {
> > -		c->cfg.data_type = MOST_CH_ASYNC;
> > -	} else if (!strcmp(buf, "sync\n")) {
> > -		c->cfg.data_type = MOST_CH_SYNC;
> > -	} else if (!strcmp(buf, "isoc_avp\n")) {
> > -		c->cfg.data_type = MOST_CH_ISOC_AVP;
> > -	} else {
> > +	int i;
> > +
> > +	for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
> > +		if (!strcmp(buf, ch_data_type[i].name))
> > +			c->cfg.data_type = ch_data_type[i].most_ch_data_type;
> 
> Missing braces and break;
> 
> > +	}
> > +
> > +	if (i == ARRAY_SIZE(ch_data_type)) {
> >  		pr_info("WARN: invalid attribute settings\n");
> >  		return -EINVAL;
> >  	}
> 
> This seems like a lot of code for a simple test.
> 
Hello Joe,
I have sent a v2 for this patch with corrections made.

Regards,
Hari Prasath

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-12-30 10:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-24 10:49 [PATCH] staging: most: replace multiple if..else with table lookup Gujulan Elango, Hari Prasath (H.)
2015-12-24 16:06 ` Joe Perches
2015-12-30 10:44   ` Gujulan Elango, Hari Prasath (H.)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox