From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thomas Monjalon Subject: Re: [PATCH v5 5/5] eal: simplify parameters of hotplug functions Date: Thu, 04 Oct 2018 13:46:54 +0200 Message-ID: <3504838.42QrpyPWfC@xps> References: <20180907222727.20521-1-thomas@monjalon.net> <20181003231046.26772-6-thomas@monjalon.net> <20181004094437.yx4tfebyxbsfp6ry@bidouze.vm.6wind.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Cc: dev@dpdk.org, ophirmu@mellanox.com, qi.z.zhang@intel.com, ferruh.yigit@intel.com, ktraynor@redhat.com To: =?ISO-8859-1?Q?Ga=EBtan?= Rivet Return-path: Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by dpdk.org (Postfix) with ESMTP id F3DED1B2B0 for ; Thu, 4 Oct 2018 13:46:57 +0200 (CEST) In-Reply-To: <20181004094437.yx4tfebyxbsfp6ry@bidouze.vm.6wind.com> List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 04/10/2018 11:44, Ga=EBtan 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) > > =20 > > 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 =3D NULL; > > + int size, length =3D -1; > > =20 > > - bus =3D rte_bus_find_by_name(busname); > > - if (bus =3D=3D NULL) { > > - RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname); > > - return -ENOENT; > > - } > > + do { /* 2 iterations: first is to know string length */ > > + size =3D length + 1; > > + length =3D snprintf(devargs, size, "%s:%s,%s", busname, devname, drv= args); > > + if (length >=3D size) > > + devargs =3D malloc(length + 1); > > + if (devargs =3D=3D NULL) > > + return -ENOMEM; > > + } while (size =3D=3D 0); >=20 > 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. >=20 > You should remove this loop, which then allow removing size, and forces u= sing > 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 =3D snprintf(devargs, 0, "%s:%s,%s", busname, devname, drvargs); if (length < 0) return -EINVAL; devargs =3D malloc(length + 1); if (devargs =3D=3D NULL) return -ENOMEM; snprintf(devargs, length + 1, "%s:%s,%s", busname, devname, drvargs); Any preference?