From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 167331D940 for ; Mon, 11 Jun 2018 22:55:46 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Jun 2018 13:55:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,211,1526367600"; d="scan'208";a="48522982" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by orsmga008.jf.intel.com with ESMTP; 11 Jun 2018 13:55:43 -0700 Received: from sivswdev01.ir.intel.com (sivswdev01.ir.intel.com [10.237.217.45]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id w5BKthcq026374 for ; Mon, 11 Jun 2018 21:55:43 +0100 Received: from sivswdev01.ir.intel.com (localhost [127.0.0.1]) by sivswdev01.ir.intel.com with ESMTP id w5BKtg3b021583 for ; Mon, 11 Jun 2018 21:55:42 +0100 Received: (from aburakov@localhost) by sivswdev01.ir.intel.com with LOCAL id w5BKtgkD021577 for dev@dpdk.org; Mon, 11 Jun 2018 21:55:42 +0100 From: Anatoly Burakov To: dev@dpdk.org Date: Mon, 11 Jun 2018 21:55:36 +0100 Message-Id: <26cd3a33611c40bcb09cd09d8a1d36d686635271.1528749451.git.anatoly.burakov@intel.com> X-Mailer: git-send-email 1.7.0.7 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH 3/9] fbarray: reduce duplication in find_next_n code 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, 11 Jun 2018 20:55:48 -0000 Mostly code move, aside from more quick checks done to avoid doing computations in obviously hopeless cases. Signed-off-by: Anatoly Burakov --- lib/librte_eal/common/eal_common_fbarray.c | 63 ++++++++++++---------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/lib/librte_eal/common/eal_common_fbarray.c b/lib/librte_eal/common/eal_common_fbarray.c index e37fe1e4d..c5ee017dd 100644 --- a/lib/librte_eal/common/eal_common_fbarray.c +++ b/lib/librte_eal/common/eal_common_fbarray.c @@ -723,54 +723,63 @@ rte_fbarray_find_next_used(struct rte_fbarray *arr, unsigned int start) return ret; } -int __rte_experimental -rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start, - unsigned int n) +static int +fbarray_find_n(struct rte_fbarray *arr, unsigned int start, unsigned int n, + bool used) { int ret = -1; - if (arr == NULL || start >= arr->len || n > arr->len) { + if (arr == NULL || start >= arr->len || n > arr->len || n == 0) { rte_errno = EINVAL; return -1; } + if (arr->len - start < n) { + rte_errno = used ? ENOENT : ENOSPC; + return -1; + } /* prevent array from changing under us */ rte_rwlock_read_lock(&arr->rwlock); - if (arr->len == arr->count || arr->len - arr->count < n) { - rte_errno = ENOSPC; - goto out; + /* cheap checks to prevent doing useless work */ + if (!used) { + if (arr->len == arr->count || arr->len - arr->count < n) { + rte_errno = ENOSPC; + goto out; + } + if (arr->count == 0) { + ret = start; + goto out; + } + } else { + if (arr->count < n) { + rte_errno = ENOENT; + goto out; + } + if (arr->count == arr->len) { + ret = start; + goto out; + } } - ret = find_next_n(arr, start, n, false); + ret = find_next_n(arr, start, n, used); out: rte_rwlock_read_unlock(&arr->rwlock); return ret; } int __rte_experimental -rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start, +rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start, unsigned int n) { - int ret = -1; - - if (arr == NULL || start >= arr->len || n > arr->len) { - rte_errno = EINVAL; - return -1; - } - - /* prevent array from changing under us */ - rte_rwlock_read_lock(&arr->rwlock); - - if (arr->count < n) { - rte_errno = ENOENT; - goto out; - } + return fbarray_find_n(arr, start, n, false); +} - ret = find_next_n(arr, start, n, true); -out: - rte_rwlock_read_unlock(&arr->rwlock); - return ret; +int __rte_experimental +rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start, + unsigned int n) +{ + return fbarray_find_n(arr, start, n, true); } static int -- 2.17.1