From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?iso-8859-1?Q?Ga=EBtan?= Rivet Subject: Re: [PATCH v5 5/5] eal: simplify parameters of hotplug functions Date: Thu, 4 Oct 2018 13:51:31 +0200 Message-ID: <20181004115131.do3mrikxujq7dx4o@bidouze.vm.6wind.com> References: <20180907222727.20521-1-thomas@monjalon.net> <20181003231046.26772-6-thomas@monjalon.net> <20181004094437.yx4tfebyxbsfp6ry@bidouze.vm.6wind.com> <3504838.42QrpyPWfC@xps> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Cc: dev@dpdk.org, ophirmu@mellanox.com, qi.z.zhang@intel.com, ferruh.yigit@intel.com, ktraynor@redhat.com To: Thomas Monjalon Return-path: Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) by dpdk.org (Postfix) with ESMTP id 74A451B273 for ; Thu, 4 Oct 2018 13:51:50 +0200 (CEST) Received: by mail-wm1-f68.google.com with SMTP id e187-v6so1224273wmf.0 for ; Thu, 04 Oct 2018 04:51:50 -0700 (PDT) Content-Disposition: inline In-Reply-To: <3504838.42QrpyPWfC@xps> List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" On Thu, Oct 04, 2018 at 01:46:54PM +0200, Thomas Monjalon wrote: > 04/10/2018 11:44, Gaëtan Rivet: > > On Thu, Oct 04, 2018 at 01:10:46AM +0200, Thomas Monjalon wrote: > > > --- a/lib/librte_eal/common/eal_common_dev.c > > > +++ b/lib/librte_eal/common/eal_common_dev.c > > > @@ -129,46 +129,61 @@ int rte_eal_dev_detach(struct rte_device *dev) > > > > > > int > > > rte_eal_hotplug_add(const char *busname, const char *devname, > > > - const char *devargs) > > > + const char *drvargs) > > > { > > > - struct rte_bus *bus; > > > - struct rte_device *dev; > > > - struct rte_devargs *da; > > > int ret; > > > + char *devargs = NULL; > > > + int size, length = -1; > > > > > > - bus = rte_bus_find_by_name(busname); > > > - if (bus == NULL) { > > > - RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname); > > > - return -ENOENT; > > > - } > > > + do { /* 2 iterations: first is to know string length */ > > > + size = length + 1; > > > + length = snprintf(devargs, size, "%s:%s,%s", busname, devname, drvargs); > > > + if (length >= size) > > > + devargs = malloc(length + 1); > > > + if (devargs == NULL) > > > + return -ENOMEM; > > > + } while (size == 0); > > > > I don't see a good reason for writing it this way. > > You use an additional state (size) and complicate something that is > > pretty straightforward. To make sure no error was written here, I had to > > do step-by-step careful reading, this should not be required by clean > > code. > > > > You should remove this loop, which then allow removing size, and forces using > > simple code-flow. > > The only reason for this loop is to avoid duplicating the snprintf format > in two calls. > > It could be replaced by this: > > length = snprintf(devargs, 0, "%s:%s,%s", busname, devname, drvargs); > if (length < 0) > return -EINVAL; > devargs = malloc(length + 1); > if (devargs == NULL) > return -ENOMEM; > snprintf(devargs, length + 1, "%s:%s,%s", busname, devname, drvargs); > > Any preference? > > Yes, the second is cleaner IMO. -- Gaëtan Rivet 6WIND