Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755404AbbLXQGc (ORCPT ); Thu, 24 Dec 2015 11:06:32 -0500 Received: from smtprelay0167.hostedemail.com ([216.40.44.167]:44668 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752006AbbLXQGa (ORCPT ); Thu, 24 Dec 2015 11:06:30 -0500 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::::::::::,RULES_HIT:41:69:355:379:541:599:800:960:973:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1534:1542:1593:1594:1711:1730:1747:1777:1792:2393:2559:2562:2828:3138:3139:3140:3141:3142:3354:3622:3866:3868:3870:4321:5007:6261:7576:9010:10004:10400:10848:11026:11232:11473:11657:11658:11783:11914:12043:12048:12291:12296:12517:12519:12555:12683:12740:13894:14110:14659:21080:21324:30012:30029:30054:30091,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:2,LUA_SUMMARY:none X-HE-Tag: floor74_1b61d6ba5264e X-Filterd-Recvd-Size: 3942 Message-ID: <1450973186.3554.13.camel@perches.com> Subject: Re: [PATCH] staging: most: replace multiple if..else with table lookup From: Joe Perches 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" Date: Thu, 24 Dec 2015 08:06:26 -0800 In-Reply-To: <20151224104946.GA15381@IND12F0122> References: <20151224104946.GA15381@IND12F0122> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.3-1ubuntu1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2938 Lines: 86 On Thu, 2015-12-24 at 10:49 +0000, Gujulan Elango, Hari Prasath (H.) wrote: > From: Hari Prasath Gujulan Elango > > Replace multiple if..else if..statements with simple table lookup in two > functions. > > Signed-off-by: Hari Prasath Gujulan Elango > --- > ?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. -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/