From: Honnappa Nagarahalli Date: Thu, 31 May 2018 15:30:49 +0000 (-0500) Subject: hash: validate hash bucket entries while compiling X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=7c872b96983a23f67d6cea37b232717c346a83ff;p=dpdk.git hash: validate hash bucket entries while compiling Validate RTE_HASH_BUCKET_ENTRIES during compilation instead of run time. Signed-off-by: Honnappa Nagarahalli Reviewed-by: Gavin Hu Acked-by: Pablo de Lara --- diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h index 434adfd455..a9df7c161a 100644 --- a/lib/librte_eal/common/include/rte_common.h +++ b/lib/librte_eal/common/include/rte_common.h @@ -293,6 +293,11 @@ rte_combine64ms1b(register uint64_t v) /*********** Macros to work with powers of 2 ********/ +/** + * Macro to return 1 if n is a power of 2, 0 otherwise + */ +#define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n))) + /** * Returns true if n is a power of 2 * @param n diff --git a/lib/librte_hash/rte_cuckoo_hash.c b/lib/librte_hash/rte_cuckoo_hash.c index a07543a29a..375e7d208a 100644 --- a/lib/librte_hash/rte_cuckoo_hash.c +++ b/lib/librte_hash/rte_cuckoo_hash.c @@ -107,7 +107,6 @@ rte_hash_create(const struct rte_hash_parameters *params) /* Check for valid parameters */ if ((params->entries > RTE_HASH_ENTRIES_MAX) || (params->entries < RTE_HASH_BUCKET_ENTRIES) || - !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) || (params->key_len == 0)) { rte_errno = EINVAL; RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n"); diff --git a/lib/librte_hash/rte_cuckoo_hash.h b/lib/librte_hash/rte_cuckoo_hash.h index 7a54e55575..bd6ad1bd66 100644 --- a/lib/librte_hash/rte_cuckoo_hash.h +++ b/lib/librte_hash/rte_cuckoo_hash.h @@ -97,6 +97,10 @@ enum add_key_case { /** Number of items per bucket. */ #define RTE_HASH_BUCKET_ENTRIES 8 +#if !RTE_IS_POWER_OF_2(RTE_HASH_BUCKET_ENTRIES) +#error RTE_HASH_BUCKET_ENTRIES must be a power of 2 +#endif + #define NULL_SIGNATURE 0 #define EMPTY_SLOT 0