rhashtable: optimize rhashtable_lookup routine
Define an internal compare function and relevant compare argument, and then make use of rhashtable_lookup_compare() to lookup key in hash table, reducing duplicated code between rhashtable_lookup() and rhashtable_lookup_compare(). Signed-off-by: Ying Xue <ying.xue@windriver.com> Cc: Thomas Graf <tgraf@suug.ch> Acked-by: Thomas Graf <tgraf@suug.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
7c1b70234e
commit
efb975a67e
1 changed files with 18 additions and 23 deletions
|
@ -612,6 +612,19 @@ bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(rhashtable_remove);
|
EXPORT_SYMBOL_GPL(rhashtable_remove);
|
||||||
|
|
||||||
|
struct rhashtable_compare_arg {
|
||||||
|
struct rhashtable *ht;
|
||||||
|
const void *key;
|
||||||
|
};
|
||||||
|
|
||||||
|
static bool rhashtable_compare(void *ptr, void *arg)
|
||||||
|
{
|
||||||
|
struct rhashtable_compare_arg *x = arg;
|
||||||
|
struct rhashtable *ht = x->ht;
|
||||||
|
|
||||||
|
return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* rhashtable_lookup - lookup key in hash table
|
* rhashtable_lookup - lookup key in hash table
|
||||||
* @ht: hash table
|
* @ht: hash table
|
||||||
|
@ -627,32 +640,14 @@ EXPORT_SYMBOL_GPL(rhashtable_remove);
|
||||||
*/
|
*/
|
||||||
void *rhashtable_lookup(struct rhashtable *ht, const void *key)
|
void *rhashtable_lookup(struct rhashtable *ht, const void *key)
|
||||||
{
|
{
|
||||||
const struct bucket_table *tbl, *old_tbl;
|
struct rhashtable_compare_arg arg = {
|
||||||
struct rhash_head *he;
|
.ht = ht,
|
||||||
u32 hash;
|
.key = key,
|
||||||
|
};
|
||||||
|
|
||||||
BUG_ON(!ht->p.key_len);
|
BUG_ON(!ht->p.key_len);
|
||||||
|
|
||||||
rcu_read_lock();
|
return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
|
||||||
old_tbl = rht_dereference_rcu(ht->tbl, ht);
|
|
||||||
tbl = rht_dereference_rcu(ht->future_tbl, ht);
|
|
||||||
hash = key_hashfn(ht, key, ht->p.key_len);
|
|
||||||
restart:
|
|
||||||
rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
|
|
||||||
if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key,
|
|
||||||
ht->p.key_len))
|
|
||||||
continue;
|
|
||||||
rcu_read_unlock();
|
|
||||||
return rht_obj(ht, he);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (unlikely(tbl != old_tbl)) {
|
|
||||||
tbl = old_tbl;
|
|
||||||
goto restart;
|
|
||||||
}
|
|
||||||
|
|
||||||
rcu_read_unlock();
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(rhashtable_lookup);
|
EXPORT_SYMBOL_GPL(rhashtable_lookup);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue