Skip to contentSkip to conversation

staging: most: replace multiple if..else with table lookup

PATCH Superseded Gujulan Elango, Hari Prasath (H.) 2 participants 3 messages Dec 24, 2015 – Dec 30, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: most: replace multiple if..else with table lookup

From: Hari Prasath Gujulan Elango <[email protected]>

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

---
 drivers/staging/most/mostcore/core.c | 39 ++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
drivers/staging/most/mostcore/core.c +22 -17
index ed1ed25..7b4636b 100644
@@ -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;
 	}
Signature · 1 line
1.9.1
#1 lore raw
Quoted 58 lines · Hari Prasath (H.)

From: Hari Prasath Gujulan Elango <[email protected]>

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

---
 drivers/staging/most/mostcore/core.c | 39 ++++++++++++++++++++----------------

 1 file changed, 22 insertions(+), 17 deletions(-)
drivers/staging/most/mostcore/core.c +0 -0
index ed1ed25..7b4636b 100644
@@ -82,6 +82,14 @@ struct most_inst_obj {
  	struct list_head list;
  };

drivers/staging/most/mostcore/core.c +8 -0
+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)
 /**

drivers/staging/most/mostcore/core.c +6 -8
@@ -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");
  }

drivers/staging/most/mostcore/core.c +0 -4
@@ -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;

drivers/staging/most/mostcore/core.c static ssize_t store_set_datatype(struct most_c_obj *c, · line 443

Quoted 10 lines
drivers/staging/most/mostcore/core.c +5 -5
-	} 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;

drivers/staging/most/mostcore/core.c static ssize_t store_set_datatype(struct most_c_obj *c, · line 449

Quoted 6 lines
drivers/staging/most/mostcore/core.c +3 -0
+	}
+
+	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.

#2 lore raw
Quoted 72 lines · Joe Perches
Quoted 58 lines · Hari Prasath (H.)

From: Hari Prasath Gujulan Elango <[email protected]>

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

---
 drivers/staging/most/mostcore/core.c | 39 ++++++++++++++++++++----------------

 1 file changed, 22 insertions(+), 17 deletions(-)
drivers/staging/most/mostcore/core.c +0 -0
index ed1ed25..7b4636b 100644
@@ -82,6 +82,14 @@ struct most_inst_obj {
  	struct list_head list;
  };

drivers/staging/most/mostcore/core.c +8 -0
+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)
 /**

drivers/staging/most/mostcore/core.c +6 -8
@@ -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");
  }

drivers/staging/most/mostcore/core.c +0 -4
@@ -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;

drivers/staging/most/mostcore/core.c static ssize_t store_set_datatype(struct most_c_obj *c, · line 443

Quoted 10 lines
drivers/staging/most/mostcore/core.c +5 -5
-	} 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;

Quoted 9 lines

drivers/staging/most/mostcore/core.c static ssize_t store_set_datatype(struct most_c_obj *c, · line 449

Quoted 6 lines
drivers/staging/most/mostcore/core.c +3 -0
+	}
+
+	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