From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com [74.125.82.65]) by dpdk.org (Postfix) with ESMTP id 7B7041BA7A for ; Thu, 26 Oct 2017 12:06:42 +0200 (CEST) Received: by mail-wm0-f65.google.com with SMTP id y83so1865371wmc.4 for ; Thu, 26 Oct 2017 03:06:42 -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=6c0KXZR0EwToaImkkgGiqjGg+3o1437nUdGUF3tHk/A=; b=kIt+6dOgijY6TADWgcXBpS84mvqlBKivB2vy9lTrKMIThfwbD7sB0pkT+SMgNbfEYz qEFmd3bq1bcy3T8k5dUaa1rCvi6tCMF5xy6iw77fMAAhb6u28YN4FvjkhZ/14a2GkApp MBKHxsrJ2LX+4mhGKBapjU2R+E0onXGJ3QETHikqsVuQXciuxsrOTqlQbJITE77waUBq +q+6ObPTELuJFELJCnSlPRgN7JsZl2Y9nyOviMoxUL8w6kMoe9EqFLAmfF//0jULVHS7 qNkS2nYJW+EDKQWpNXJWNz/C+lajVCpSvl4WgDk73KW1I0f6+nBySyHpA7461OOJdB1T +rQw== 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=6c0KXZR0EwToaImkkgGiqjGg+3o1437nUdGUF3tHk/A=; b=T7J8fdkvuxHEjjnQsyJ9Eit2kYx8Ba+PshEMwsOdmrxUbNQJKQsxlGU5sKQza9SCR/ rvz2JGm+PJIXl6u1dHmH71wyiVbsAjc7/M5+Yr05ySEfWZ12F0DpLfffe7z/NBOhlZ54 9VMpTOeU9+bVxIATnJFE31VH0/jRwnKE1ds1ZvNq8Km6FIlhUApdgosWauufMBGgWKLe bjZY5GhYBXhgD5AnSG68LOUja/ohJq1z1zKFUPLgNwjnxEgIDu1jhnL+lVc8VNpS6IkE oPLDX/C4lMeK3W2DPB9b75qdnY8fYHOPHO+z7VL7VmQaadSfCTHQ11AgjkH7wH8DIsvq q30w== X-Gm-Message-State: AMCzsaUCfGIzEaTjqPv56Vxb0gqHS6wAcANgVHjL59gdxGGnoY7w5FKr ifSpx7Dkq8uEABFR94+mrGxYdpi+ X-Google-Smtp-Source: ABhQp+RLKnqFZl5qE5RgLK93XkaYjcbV8Xe3GhYOJjiHQT9q7f+quaxO51+K7HGixpRfcT2gBJbPBw== X-Received: by 10.80.158.194 with SMTP id a60mr24016734edf.182.1509012401777; Thu, 26 Oct 2017 03:06:41 -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 u12sm3339619edk.45.2017.10.26.03.06.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Oct 2017 03:06:41 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Thu, 26 Oct 2017 12:05:57 +0200 Message-Id: <7e8262d6e44b82ba43da64991852437d87b29352.1509012196.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 v8 12/25] 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: Thu, 26 Oct 2017 10:06:42 -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_eal/common/eal_common_pci.c | 67 +++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c index 54931d1..f896289 100644 --- a/lib/librte_eal/common/eal_common_pci.c +++ b/lib/librte_eal/common/eal_common_pci.c @@ -87,42 +87,67 @@ static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev) return NULL; } -/* 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; } -#undef GET_PCIADDR_FIELD - void rte_pci_device_name(const struct rte_pci_addr *addr, char *output, size_t size) -- 2.1.4