Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Statically sized hash table implementation
0004  * (C) 2012  Sasha Levin <levinsasha928@gmail.com>
0005  */
0006 
0007 #ifndef _LINUX_HASHTABLE_H
0008 #define _LINUX_HASHTABLE_H
0009 
0010 #include <linux/list.h>
0011 #include <linux/types.h>
0012 #include <linux/kernel.h>
0013 #include <linux/bitops.h>
0014 #include <linux/hash.h>
0015 #include <linux/log2.h>
0016 
0017 #define DEFINE_HASHTABLE(name, bits)                        \
0018     struct hlist_head name[1 << (bits)] =                   \
0019             { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
0020 
0021 #define DECLARE_HASHTABLE(name, bits)                                       \
0022     struct hlist_head name[1 << (bits)]
0023 
0024 #define HASH_SIZE(name) (ARRAY_SIZE(name))
0025 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
0026 
0027 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
0028 #define hash_min(val, bits)                         \
0029     (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
0030 
0031 static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
0032 {
0033     unsigned int i;
0034 
0035     for (i = 0; i < sz; i++)
0036         INIT_HLIST_HEAD(&ht[i]);
0037 }
0038 
0039 /**
0040  * hash_init - initialize a hash table
0041  * @hashtable: hashtable to be initialized
0042  *
0043  * Calculates the size of the hashtable from the given parameter, otherwise
0044  * same as hash_init_size.
0045  *
0046  * This has to be a macro since HASH_BITS() will not work on pointers since
0047  * it calculates the size during preprocessing.
0048  */
0049 #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
0050 
0051 /**
0052  * hash_add - add an object to a hashtable
0053  * @hashtable: hashtable to add to
0054  * @node: the &struct hlist_node of the object to be added
0055  * @key: the key of the object to be added
0056  */
0057 #define hash_add(hashtable, node, key)                      \
0058     hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
0059 
0060 /**
0061  * hash_hashed - check whether an object is in any hashtable
0062  * @node: the &struct hlist_node of the object to be checked
0063  */
0064 static inline bool hash_hashed(struct hlist_node *node)
0065 {
0066     return !hlist_unhashed(node);
0067 }
0068 
0069 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
0070 {
0071     unsigned int i;
0072 
0073     for (i = 0; i < sz; i++)
0074         if (!hlist_empty(&ht[i]))
0075             return false;
0076 
0077     return true;
0078 }
0079 
0080 /**
0081  * hash_empty - check whether a hashtable is empty
0082  * @hashtable: hashtable to check
0083  *
0084  * This has to be a macro since HASH_BITS() will not work on pointers since
0085  * it calculates the size during preprocessing.
0086  */
0087 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
0088 
0089 /**
0090  * hash_del - remove an object from a hashtable
0091  * @node: &struct hlist_node of the object to remove
0092  */
0093 static inline void hash_del(struct hlist_node *node)
0094 {
0095     hlist_del_init(node);
0096 }
0097 
0098 /**
0099  * hash_for_each - iterate over a hashtable
0100  * @name: hashtable to iterate
0101  * @bkt: integer to use as bucket loop cursor
0102  * @obj: the type * to use as a loop cursor for each entry
0103  * @member: the name of the hlist_node within the struct
0104  */
0105 #define hash_for_each(name, bkt, obj, member)               \
0106     for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
0107             (bkt)++)\
0108         hlist_for_each_entry(obj, &name[bkt], member)
0109 
0110 /**
0111  * hash_for_each_safe - iterate over a hashtable safe against removal of
0112  * hash entry
0113  * @name: hashtable to iterate
0114  * @bkt: integer to use as bucket loop cursor
0115  * @tmp: a &struct used for temporary storage
0116  * @obj: the type * to use as a loop cursor for each entry
0117  * @member: the name of the hlist_node within the struct
0118  */
0119 #define hash_for_each_safe(name, bkt, tmp, obj, member)         \
0120     for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
0121             (bkt)++)\
0122         hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
0123 
0124 /**
0125  * hash_for_each_possible - iterate over all possible objects hashing to the
0126  * same bucket
0127  * @name: hashtable to iterate
0128  * @obj: the type * to use as a loop cursor for each entry
0129  * @member: the name of the hlist_node within the struct
0130  * @key: the key of the objects to iterate over
0131  */
0132 #define hash_for_each_possible(name, obj, member, key)          \
0133     hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
0134 
0135 /**
0136  * hash_for_each_possible_safe - iterate over all possible objects hashing to the
0137  * same bucket safe against removals
0138  * @name: hashtable to iterate
0139  * @obj: the type * to use as a loop cursor for each entry
0140  * @tmp: a &struct used for temporary storage
0141  * @member: the name of the hlist_node within the struct
0142  * @key: the key of the objects to iterate over
0143  */
0144 #define hash_for_each_possible_safe(name, obj, tmp, member, key)    \
0145     hlist_for_each_entry_safe(obj, tmp,\
0146         &name[hash_min(key, HASH_BITS(name))], member)
0147 
0148 
0149 #endif