From 66656d0bf9e261993feff6bd908a13698c40e5df Mon Sep 17 00:00:00 2001 From: Anatoly Burakov Date: Mon, 11 Jun 2018 21:55:36 +0100 Subject: [PATCH] fbarray: reduce duplication in chunk finding 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 e37fe1e4d9..c5ee017dd7 100644 --- a/lib/librte_eal/common/eal_common_fbarray.c +++ b/lib/librte_eal/common/eal_common_fbarray.c @@ -723,54 +723,63 @@ out: 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.20.1