Whamcloud - gitweb
LU-17504 build: fix gcc-13 [-Werror=stringop-overread] error
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_hash.h
index d0e452c..6fe31a6 100644 (file)
@@ -27,7 +27,6 @@
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * libcfs/include/libcfs/libcfs_hash.h
  *
 #ifndef __LIBCFS_HASH_H__
 #define __LIBCFS_HASH_H__
 
-#include <linux/hash.h>
 #include <linux/spinlock.h>
 #include <linux/workqueue.h>
-
-/*
- * Knuth recommends primes in approximately golden ratio to the maximum
- * integer representable by a machine word for multiplicative hashing.
- * Chuck Lever verified the effectiveness of this technique:
- * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
- *
- * These primes are chosen to be bit-sparse, that is operations on
- * them can use shifts and additions instead of multiplications for
- * machines where multiplications are slow.
- */
-/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
-#define CFS_GOLDEN_RATIO_PRIME_32 0x9e370001UL
-/*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
-#define CFS_GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001ULL
+#include <linux/refcount.h>
+#include <libcfs/linux/linux-hash.h>
 
 /** disable debug */
 #define CFS_HASH_DEBUG_NONE    0
@@ -249,7 +234,7 @@ struct cfs_hash {
        /** rehash workitem */
        struct work_struct              hs_rehash_work;
        /** refcount on this hash table */
-       atomic_t                        hs_refcount;
+       struct kref                     hs_refcount;
        /** rehash buckets-table */
        struct cfs_hash_bucket          **hs_rehash_buckets;
 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
@@ -266,8 +251,8 @@ struct cfs_hash {
         /** workitem to output max depth */
        struct work_struct              hs_dep_work;
 #endif
-        /** name of htable */
-        char                        hs_name[0];
+       /** name of htable */
+       char                        hs_name[];
 };
 
 struct cfs_hash_lock_ops {
@@ -296,7 +281,8 @@ struct cfs_hash_hlist_ops {
 
 struct cfs_hash_ops {
        /** return hashed value from @key */
-       unsigned (*hs_hash)(struct cfs_hash *hs, const void *key, unsigned mask);
+       unsigned int (*hs_hash)(struct cfs_hash *hs, const void *key,
+                               const unsigned int bits);
        /** return key address of @hnode */
        void *   (*hs_key)(struct hlist_node *hnode);
        /** copy key from @hnode to @key */
@@ -459,10 +445,10 @@ cfs_hash_bkt_size(struct cfs_hash *hs)
                hs->hs_extra_bytes;
 }
 
-static inline unsigned
-cfs_hash_id(struct cfs_hash *hs, const void *key, unsigned mask)
+static inline unsigned int
+cfs_hash_id(struct cfs_hash *hs, const void *key, const unsigned int bits)
 {
-       return hs->hs_ops->hs_hash(hs, key, mask);
+       return hs->hs_ops->hs_hash(hs, key, bits);
 }
 
 static inline void *
@@ -625,10 +611,10 @@ void cfs_hash_bd_move_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd_old,
 
 static inline int
 cfs_hash_bd_dec_and_lock(struct cfs_hash *hs, struct cfs_hash_bd *bd,
-                        atomic_t *condition)
+                        refcount_t *condition)
 {
        LASSERT(cfs_hash_with_spin_bktlock(hs));
-       return atomic_dec_and_lock(condition, &bd->bd_bucket->hsb_lock.spin);
+       return refcount_dec_and_lock(condition, &bd->bd_bucket->hsb_lock.spin);
 }
 
 static inline struct hlist_head *
@@ -819,34 +805,16 @@ void cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m);
  * Generic djb2 hash algorithm for character arrays.
  */
 static inline unsigned
-cfs_hash_djb2_hash(const void *key, size_t size, unsigned mask)
+cfs_hash_djb2_hash(const void *key, size_t size, const unsigned int bits)
 {
-        unsigned i, hash = 5381;
+       unsigned int i, hash = 5381;
 
-        LASSERT(key != NULL);
+       LASSERT(key != NULL);
 
-        for (i = 0; i < size; i++)
-                hash = hash * 33 + ((char *)key)[i];
+       for (i = 0; i < size; i++)
+               hash = hash * 33 + ((char *)key)[i];
 
-        return (hash & mask);
-}
-
-/*
- * Generic u32 hash algorithm.
- */
-static inline unsigned
-cfs_hash_u32_hash(const __u32 key, unsigned mask)
-{
-        return ((key * CFS_GOLDEN_RATIO_PRIME_32) & mask);
-}
-
-/*
- * Generic u64 hash algorithm.
- */
-static inline unsigned
-cfs_hash_u64_hash(const __u64 key, unsigned mask)
-{
-        return ((unsigned)(key * CFS_GOLDEN_RATIO_PRIME_64) & mask);
+       return (hash & ((1U << bits) - 1));
 }
 
 /** iterate over all buckets in @bds (array of struct cfs_hash_bd) */