From mboxrd@z Thu Jan 1 00:00:00 1970 From: Anatoly Burakov Subject: [PATCH 2/9] fbarray: reduce duplication in find_contig code Date: Mon, 11 Jun 2018 21:55:35 +0100 Message-ID: <4c43dd7982c8211249d45bf0e1ffbdd1ca788b1c.1528749451.git.anatoly.burakov@intel.com> References: To: dev@dpdk.org Return-path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 6CB891DB37 for ; Mon, 11 Jun 2018 22:55:46 +0200 (CEST) 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 w5BKtguh026371 for ; Mon, 11 Jun 2018 21:55:42 +0100 Received: from sivswdev01.ir.intel.com (localhost [127.0.0.1]) by sivswdev01.ir.intel.com with ESMTP id w5BKtgCf021574 for ; Mon, 11 Jun 2018 21:55:42 +0100 Received: (from aburakov@localhost) by sivswdev01.ir.intel.com with LOCAL id w5BKtgS4021570 for dev@dpdk.org; Mon, 11 Jun 2018 21:55:42 +0100 In-Reply-To: In-Reply-To: References: List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Mostly a code move, to have all code related to find_contig in one place. This slightly changes the API in that previously, calling find_contig_free() on a full fbarray would've been an error, but equivalent call to find_contig_used() on an empty array does not return an error, leading to an inconsistency in the API. The decision was made to not treat this condition as an error, because it is equivalent to calling find_contig() on an index that just happens to be used/free, which is not an error and will return 0. Signed-off-by: Anatoly Burakov --- lib/librte_eal/common/eal_common_fbarray.c | 52 ++++++++++++---------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/librte_eal/common/eal_common_fbarray.c b/lib/librte_eal/common/eal_common_fbarray.c index 9ae942bfe..e37fe1e4d 100644 --- a/lib/librte_eal/common/eal_common_fbarray.c +++ b/lib/librte_eal/common/eal_common_fbarray.c @@ -773,8 +773,8 @@ rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start, return ret; } -int __rte_experimental -rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start) +static int +fbarray_find_contig(struct rte_fbarray *arr, unsigned int start, bool used) { int ret = -1; @@ -786,14 +786,25 @@ rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start) /* prevent array from changing under us */ rte_rwlock_read_lock(&arr->rwlock); - if (arr->len == arr->count) { - rte_errno = ENOSPC; - goto out; - } - - if (arr->count == 0) { - ret = arr->len - start; - goto out; + /* cheap checks to prevent doing useless work */ + if (used) { + if (arr->count == 0) { + ret = 0; + goto out; + } + if (arr->len == arr->count) { + ret = arr->len - start; + goto out; + } + } else { + if (arr->len == arr->count) { + ret = 0; + goto out; + } + if (arr->count == 0) { + ret = arr->len - start; + goto out; + } } ret = find_contig(arr, start, false); @@ -803,22 +814,15 @@ rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start) } int __rte_experimental -rte_fbarray_find_contig_used(struct rte_fbarray *arr, unsigned int start) +rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start) { - int ret = -1; - - if (arr == NULL || start >= arr->len) { - rte_errno = EINVAL; - return -1; - } - - /* prevent array from changing under us */ - rte_rwlock_read_lock(&arr->rwlock); - - ret = find_contig(arr, start, true); + return fbarray_find_contig(arr, start, false); +} - rte_rwlock_read_unlock(&arr->rwlock); - return ret; +int __rte_experimental +rte_fbarray_find_contig_used(struct rte_fbarray *arr, unsigned int start) +{ + return fbarray_find_contig(arr, start, true); } int __rte_experimental -- 2.17.1