diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2015-03-23 09:50:21 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2015-03-23 22:07:51 -0400 |
commit | 31ccde2dacea8375c3a7d6fffbf0060ee0d40214 (patch) | |
tree | 82c65efdd9369d9793996e568ada542ab54bc3c7 /lib/rhashtable.c | |
parent | de91b25c8011089f5dd99b9d24743db1f550ca4b (diff) |
rhashtable: Allow hashfn to be unset
Since every current rhashtable user uses jhash as their hash
function, the fact that jhash is an inline function causes each
user to generate a copy of its code.
This function provides a solution to this problem by allowing
hashfn to be unset. In which case rhashtable will automatically
set it to jhash. Furthermore, if the key length is a multiple
of 4, we will switch over to jhash2.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Acked-by: Thomas Graf <tgraf@suug.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'lib/rhashtable.c')
-rw-r--r-- | lib/rhashtable.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/rhashtable.c b/lib/rhashtable.c index 618a3f00d712..798f01d64ab0 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c | |||
@@ -532,6 +532,11 @@ static size_t rounded_hashtable_size(const struct rhashtable_params *params) | |||
532 | (unsigned long)params->min_size); | 532 | (unsigned long)params->min_size); |
533 | } | 533 | } |
534 | 534 | ||
535 | static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) | ||
536 | { | ||
537 | return jhash2(key, length, seed); | ||
538 | } | ||
539 | |||
535 | /** | 540 | /** |
536 | * rhashtable_init - initialize a new hash table | 541 | * rhashtable_init - initialize a new hash table |
537 | * @ht: hash table to be initialized | 542 | * @ht: hash table to be initialized |
@@ -583,7 +588,7 @@ int rhashtable_init(struct rhashtable *ht, | |||
583 | 588 | ||
584 | size = HASH_DEFAULT_SIZE; | 589 | size = HASH_DEFAULT_SIZE; |
585 | 590 | ||
586 | if ((!(params->key_len && params->hashfn) && !params->obj_hashfn) || | 591 | if ((!params->key_len && !params->obj_hashfn) || |
587 | (params->obj_hashfn && !params->obj_cmpfn)) | 592 | (params->obj_hashfn && !params->obj_cmpfn)) |
588 | return -EINVAL; | 593 | return -EINVAL; |
589 | 594 | ||
@@ -610,6 +615,16 @@ int rhashtable_init(struct rhashtable *ht, | |||
610 | else | 615 | else |
611 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; | 616 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; |
612 | 617 | ||
618 | ht->key_len = ht->p.key_len; | ||
619 | if (!params->hashfn) { | ||
620 | ht->p.hashfn = jhash; | ||
621 | |||
622 | if (!(ht->key_len & (sizeof(u32) - 1))) { | ||
623 | ht->key_len /= sizeof(u32); | ||
624 | ht->p.hashfn = rhashtable_jhash2; | ||
625 | } | ||
626 | } | ||
627 | |||
613 | tbl = bucket_table_alloc(ht, size); | 628 | tbl = bucket_table_alloc(ht, size); |
614 | if (tbl == NULL) | 629 | if (tbl == NULL) |
615 | return -ENOMEM; | 630 | return -ENOMEM; |