From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f180.google.com (mail-wr0-f180.google.com [209.85.128.180]) by dpdk.org (Postfix) with ESMTP id CBC4E1B1A7 for ; Mon, 18 Sep 2017 11:32:16 +0200 (CEST) Received: by mail-wr0-f180.google.com with SMTP id u96so5675051wrb.6 for ; Mon, 18 Sep 2017 02:32:16 -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=QH8RA/tQu6lxxc6tVxTdfSqj901rpOZhDcEQSIkBc+8tpNU4Pzk6BWtQjE8wyycpRX 9dZIbCkgMp3rSKrFOKxfbOsyn0jNVJJ8XOYZd3Hd9w3fDKCQ7K9MTSD9sl5kX5l7jyRj n6xbYsWFUD2U+iPZrHKa7iQ5r+njhrLZSdzWu7xWOGfo0jbNn8S6yeE2CborBrIlSGFo qBTzu9ZwkG4hv1d8uSeDRRmUgfz4hCWnziZUPfmK4GhTecPspRGfYHliNVJ5s8MDRHjY kodXb2xRBIt1PNnLSikewBrGjX2JGzmfWjn1RpK6lHXDe8/3FCVana481y/L14R1QR7s SLrw== 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=TeYg1Pe3Ljf52KSZUviJE2bV09XO856zNxAQQpw6yrqbb9oSA1HrYUf4it/D+qYimR 6/Xe0p4Io7neT+xtdkt9FDDRZm0weB9uxPEVGFX+m4XBrkpdud7gdRp3cVyAFhYSehoU adPTqrGTD74Q79uRbbdvm+mscfzH5VI4CQ5l3qZDrPxSWQfKh9lWDAkMxlmg444OA7RI yXlwk9LhT7Xxyl2giSgTnlw5L0XJsRFdXh4Ezlvz2QJJT+8dn8DlzXolGRXL56JuIILy 9Y7pqA2w+BBL3br3CyUpiQ7LH4lj7voLs0AT+XeBmcYfqYNEC1kBLKS4IEsEbNjOMznE cnWA== X-Gm-Message-State: AHPjjUj6Au21BlTrV68lgW7uEDHJhgaRajh4YbxhtPx/qs7TFeis2SWv UX5PaG+2+exkNPADhAs= X-Google-Smtp-Source: AOwi7QBZqARV1gsG+q2u+YhwEszBKko+EVzCAUzk24YT8zRroU2F26xVywHdnXigJ/bZk1hqzF1hrQ== X-Received: by 10.223.158.129 with SMTP id a1mr6302030wrf.65.1505727135899; Mon, 18 Sep 2017 02:32:15 -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 f89sm6416859wmh.18.2017.09.18.02.32.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 18 Sep 2017 02:32:15 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Mon, 18 Sep 2017 11:31:46 +0200 Message-Id: <9bac4f9a1feb396d1616070133e863c8895122a8.1505726803.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 v2 12/14] 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: Mon, 18 Sep 2017 09:32:17 -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