From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f46.google.com (mail-wm0-f46.google.com [74.125.82.46]) by dpdk.org (Postfix) with ESMTP id 5CB297D7A for ; Fri, 25 Aug 2017 11:04:59 +0200 (CEST) Received: by mail-wm0-f46.google.com with SMTP id b189so6985458wmd.0 for ; Fri, 25 Aug 2017 02:04:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=6ec+uCBCg6bB59/P2XJrvX/j5Zq1ZlDpR3r4K26woBU=; b=arguzCFosMlZspwTbrncg20r0vKvA+wrzjLns3douHYi5JcauEiE8p+Ya8nr0K6RpU QdJwx920cDKY9tcAkS+3TMUoCH5/6Oq/+vS1H0R9JO6n+lftJE6Sw6JlUH563xwICj8g mTHMgPAUhJhl/T7OhvAaduqgcrhXFmrWLPgrh2lHYImhjKiwsOB9vgU7uzBFG4MxuKve lMIR2IqK3GB7kin6iat5qTO3lPaCt4mtBAh+knpPWq0RFWv+03+tYk44JcFXY/RThfqQ iiA4531nz0Btcimj/jFyIEBM9Pg/zXL3xcUAgYND+hO9RxPsMreD/1/+7CVuHE0wjb7u JsEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=6ec+uCBCg6bB59/P2XJrvX/j5Zq1ZlDpR3r4K26woBU=; b=aj/7KCMNYQVXyjWKO/1vYxX0TQYgCy/0XEED+OKiss2FrMZjkMkM3FY84uuvzyZxj2 nKG7wDoLxXKGo4br5C8uhKmpTP/9MHHDQ5Euck/j546Yaoq0rqRS+8WbQ889FKDlRItb DFAmRbAA8uXu/x8YE+WxjizLG/aToEeo3A5uWKWYc0i0+8ucLghHd0xuTqF0rzphNnSJ godBuVkIi/+2pGRsQ0L972wCCb/n5QCs5WbjOCYD8fkBvtwtGCtTi5mo4bwV4p03pIa7 ZsZTIfz0LNLo2AvYVz4Moympd3pVWLKOv18bkybCps3e4wOt2nQ8N+rQA22TNqXHMNvR Q4AQ== X-Gm-Message-State: AHYfb5gH6nFETqBOpg22M3VH74bhud2yoPbDcoj+lRjO5ywXiRg5q+wc V/v0G3l5L2t03fXsRtc= X-Received: by 10.28.55.193 with SMTP id e184mr873617wma.75.1503651898717; Fri, 25 Aug 2017 02:04:58 -0700 (PDT) Received: from bidouze.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id q141sm782350wmb.11.2017.08.25.02.04.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 25 Aug 2017 02:04:57 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Fri, 25 Aug 2017 11:04:29 +0200 Message-Id: <30a3a685f0b42f2b7b80e9c23c731c6140958cb2.1503651392.git.gaetan.rivet@6wind.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 07/13] pci: avoid over-complicated macro X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Aug 2017 09:04:59 -0000 Using a macro helps writing the code to the detriment of the reader in this case. This is backward. Write once, read many. The few LOCs gained is not worth the opacity of the implementation. Signed-off-by: Gaetan Rivet --- lib/librte_pci/rte_pci.c | 65 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/lib/librte_pci/rte_pci.c b/lib/librte_pci/rte_pci.c index 8584b55..cbb5359 100644 --- a/lib/librte_pci/rte_pci.c +++ b/lib/librte_pci/rte_pci.c @@ -53,36 +53,63 @@ #include "rte_pci.h" -/* Macro used by pci addr parsing functions. **/ -#define GET_PCIADDR_FIELD(in, fd, lim, dlm) \ -do { \ - unsigned long val; \ - char *end; \ - errno = 0; \ - val = strtoul((in), &end, 16); \ - if (errno != 0 || end[0] != (dlm) || val > (lim)) \ - return -EINVAL; \ - (fd) = (typeof (fd))val; \ - (in) = end + 1; \ -} while(0) +static inline const char * +get_u8_pciaddr_field(const char *in, void *_u8, char dlm) +{ + unsigned long val; + uint8_t *u8 = _u8; + char *end; + + errno = 0; + val = strtoul(in, &end, 16); + if (errno != 0 || end[0] != dlm || val > UINT8_MAX) { + errno = errno ? errno : EINVAL; + return NULL; + } + *u8 = (uint8_t)val; + return end + 1; +} int eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr) { + const char *in = input; + dev_addr->domain = 0; - GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':'); - GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.'); - GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0); + in = get_u8_pciaddr_field(in, &dev_addr->bus, ':'); + if (in == NULL) + return -EINVAL; + in = get_u8_pciaddr_field(in, &dev_addr->devid, '.'); + if (in == NULL) + return -EINVAL; + in = get_u8_pciaddr_field(in, &dev_addr->function, '\0'); + if (in == NULL) + return -EINVAL; return 0; } int eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr) { - GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':'); - GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':'); - GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.'); - GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0); + const char *in = input; + unsigned long val; + char *end; + + errno = 0; + val = strtoul(in, &end, 16); + if (errno != 0 || end[0] != ':' || val > UINT16_MAX) + return -EINVAL; + dev_addr->domain = (uint16_t)val; + in = end + 1; + in = get_u8_pciaddr_field(in, &dev_addr->bus, ':'); + if (in == NULL) + return -EINVAL; + in = get_u8_pciaddr_field(in, &dev_addr->devid, '.'); + if (in == NULL) + return -EINVAL; + in = get_u8_pciaddr_field(in, &dev_addr->function, '\0'); + if (in == NULL) + return -EINVAL; return 0; } -- 2.1.4