2 * Statically sized hash table implementation
3 * (C) 2012 Sasha Levin <levinsasha928@gmail.com>
6 #ifndef _LINUX_HASHTABLE_H
7 #define _LINUX_HASHTABLE_H
9 #include <linux/list.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/bitops.h>
13 #include <linux/hash.h>
14 #include <linux/log2.h>
16 #define DEFINE_HASHTABLE(name, bits) \
17 struct hlist_head name[1 << (bits)] = \
18 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
20 #define DECLARE_HASHTABLE(name, bits) \
21 struct hlist_head name[1 << (bits)]
23 #define HASH_SIZE(name) (ARRAY_SIZE(name))
24 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
26 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
27 #define hash_min(val, bits) \
28 (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
30 static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
34 for (i = 0; i < sz; i++)
35 INIT_HLIST_HEAD(&ht[i]);
39 * hash_init - initialize a hash table
40 * @hashtable: hashtable to be initialized
42 * Calculates the size of the hashtable from the given parameter, otherwise
43 * same as hash_init_size.
45 * This has to be a macro since HASH_BITS() will not work on pointers since
46 * it calculates the size during preprocessing.
48 #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
51 * hash_add - add an object to a hashtable
52 * @hashtable: hashtable to add to
53 * @node: the &struct hlist_node of the object to be added
54 * @key: the key of the object to be added
56 #define hash_add(hashtable, node, key) \
57 hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
60 * hash_hashed - check whether an object is in any hashtable
61 * @node: the &struct hlist_node of the object to be checked
63 static inline bool hash_hashed(struct hlist_node *node)
65 return !hlist_unhashed(node);
68 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
72 for (i = 0; i < sz; i++)
73 if (!hlist_empty(&ht[i]))
80 * hash_empty - check whether a hashtable is empty
81 * @hashtable: hashtable to check
83 * This has to be a macro since HASH_BITS() will not work on pointers since
84 * it calculates the size during preprocessing.
86 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
89 * hash_del - remove an object from a hashtable
90 * @node: &struct hlist_node of the object to remove
92 static inline void hash_del(struct hlist_node *node)
98 * hash_for_each - iterate over a hashtable
99 * @name: hashtable to iterate
100 * @bkt: integer to use as bucket loop cursor
101 * @obj: the type * to use as a loop cursor for each entry
102 * @member: the name of the hlist_node within the struct
104 #define hash_for_each(name, bkt, obj, member) \
105 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
107 hlist_for_each_entry(obj, &name[bkt], member)
110 * hash_for_each_safe - iterate over a hashtable safe against removal of
112 * @name: hashtable to iterate
113 * @bkt: integer to use as bucket loop cursor
114 * @tmp: a &struct used for temporary storage
115 * @obj: the type * to use as a loop cursor for each entry
116 * @member: the name of the hlist_node within the struct
118 #define hash_for_each_safe(name, bkt, tmp, obj, member) \
119 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
121 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
124 * hash_for_each_possible - iterate over all possible objects hashing to the
126 * @name: hashtable to iterate
127 * @obj: the type * to use as a loop cursor for each entry
128 * @member: the name of the hlist_node within the struct
129 * @key: the key of the objects to iterate over
131 #define hash_for_each_possible(name, obj, member, key) \
132 hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
135 * hash_for_each_possible_safe - iterate over all possible objects hashing to the
136 * same bucket safe against removals
137 * @name: hashtable to iterate
138 * @obj: the type * to use as a loop cursor for each entry
139 * @tmp: a &struct used for temporary storage
140 * @member: the name of the hlist_node within the struct
141 * @key: the key of the objects to iterate over
143 #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
144 hlist_for_each_entry_safe(obj, tmp,\
145 &name[hash_min(key, HASH_BITS(name))], member)