hash: customize compare function

Give user a chance to customize the hash key compare function.
The default rte_hash_cmp_eq function is set in the rte_hash_create
function, but these builtin ones may not good enough, so the user
may call this to override the default one.

Signed-off-by: Yu Nemo Wenbin <yuwb_bjy@ctbri.com.cn>
Acked-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
This commit is contained in:
Yu Nemo Wenbin 2015-12-04 11:11:41 +08:00 committed by Thomas Monjalon
parent 5ddfbc850b
commit 95da2f8e9c
3 changed files with 28 additions and 2 deletions

View File

@ -102,8 +102,6 @@ EAL_REGISTER_TAILQ(rte_hash_tailq)
#define LCORE_CACHE_SIZE 8 #define LCORE_CACHE_SIZE 8
typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
struct lcore_cache { struct lcore_cache {
unsigned len; /**< Cache len */ unsigned len; /**< Cache len */
void *objs[LCORE_CACHE_SIZE]; /**< Cache objects */ void *objs[LCORE_CACHE_SIZE]; /**< Cache objects */
@ -187,6 +185,11 @@ rte_hash_find_existing(const char *name)
return h; return h;
} }
void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
{
h->rte_hash_cmp_eq = func;
}
struct rte_hash * struct rte_hash *
rte_hash_create(const struct rte_hash_parameters *params) rte_hash_create(const struct rte_hash_parameters *params)
{ {

View File

@ -66,6 +66,9 @@ typedef uint32_t hash_sig_t;
typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len, typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
uint32_t init_val); uint32_t init_val);
/** Type of function used to compare the hash key. */
typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
/** /**
* Parameters used when creating the hash table. * Parameters used when creating the hash table.
*/ */
@ -103,6 +106,19 @@ struct rte_hash;
struct rte_hash * struct rte_hash *
rte_hash_create(const struct rte_hash_parameters *params); rte_hash_create(const struct rte_hash_parameters *params);
/**
* Set a new hash compare function other than the default one.
*
* @note Function pointer does not work with multi-process, so do not use it
* in multi-process mode.
*
* @param h
* Hash table to reset
* @param func
* New compare function
*/
void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
/** /**
* Find an existing hash table object and return a pointer to it. * Find an existing hash table object and return a pointer to it.
* *

View File

@ -31,3 +31,10 @@ DPDK_2.1 {
rte_hash_reset; rte_hash_reset;
} DPDK_2.0; } DPDK_2.0;
DPDK_2.2 {
global:
rte_hash_set_cmp_func;
} DPDK_2.1;