1 /* SPDX-License-Identifier: GPL-2.0 */ << 2 /* 1 /* 3 * Statically sized hash table implementation 2 * Statically sized hash table implementation 4 * (C) 2012 Sasha Levin <levinsasha928@gmail. 3 * (C) 2012 Sasha Levin <levinsasha928@gmail.com> 5 */ 4 */ 6 5 7 #ifndef _LINUX_HASHTABLE_H 6 #ifndef _LINUX_HASHTABLE_H 8 #define _LINUX_HASHTABLE_H 7 #define _LINUX_HASHTABLE_H 9 8 10 #include <linux/list.h> 9 #include <linux/list.h> 11 #include <linux/types.h> 10 #include <linux/types.h> 12 #include <linux/kernel.h> 11 #include <linux/kernel.h> 13 #include <linux/hash.h> 12 #include <linux/hash.h> 14 #include <linux/rculist.h> 13 #include <linux/rculist.h> 15 14 16 #define DEFINE_HASHTABLE(name, bits) 15 #define DEFINE_HASHTABLE(name, bits) \ 17 struct hlist_head name[1 << (bits)] = 16 struct hlist_head name[1 << (bits)] = \ 18 { [0 ... ((1 << (bits) 17 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT } 19 18 20 #define DEFINE_READ_MOSTLY_HASHTABLE(name, bit << 21 struct hlist_head name[1 << (bits)] __ << 22 { [0 ... ((1 << (bits) << 23 << 24 #define DECLARE_HASHTABLE(name, bits) 19 #define DECLARE_HASHTABLE(name, bits) \ 25 struct hlist_head name[1 << (bits)] 20 struct hlist_head name[1 << (bits)] 26 21 27 #define HASH_SIZE(name) (ARRAY_SIZE(name)) 22 #define HASH_SIZE(name) (ARRAY_SIZE(name)) 28 #define HASH_BITS(name) ilog2(HASH_SIZE(name)) 23 #define HASH_BITS(name) ilog2(HASH_SIZE(name)) 29 24 30 /* Use hash_32 when possible to allow for fast 25 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */ 31 #define hash_min(val, bits) 26 #define hash_min(val, bits) \ 32 (sizeof(val) <= 4 ? hash_32(val, bits) 27 (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits)) 33 28 34 static inline void __hash_init(struct hlist_he 29 static inline void __hash_init(struct hlist_head *ht, unsigned int sz) 35 { 30 { 36 unsigned int i; 31 unsigned int i; 37 32 38 for (i = 0; i < sz; i++) 33 for (i = 0; i < sz; i++) 39 INIT_HLIST_HEAD(&ht[i]); 34 INIT_HLIST_HEAD(&ht[i]); 40 } 35 } 41 36 42 /** 37 /** 43 * hash_init - initialize a hash table 38 * hash_init - initialize a hash table 44 * @hashtable: hashtable to be initialized 39 * @hashtable: hashtable to be initialized 45 * 40 * 46 * Calculates the size of the hashtable from t 41 * Calculates the size of the hashtable from the given parameter, otherwise 47 * same as hash_init_size. 42 * same as hash_init_size. 48 * 43 * 49 * This has to be a macro since HASH_BITS() wi 44 * This has to be a macro since HASH_BITS() will not work on pointers since 50 * it calculates the size during preprocessing 45 * it calculates the size during preprocessing. 51 */ 46 */ 52 #define hash_init(hashtable) __hash_init(hasht 47 #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable)) 53 48 54 /** 49 /** 55 * hash_add - add an object to a hashtable 50 * hash_add - add an object to a hashtable 56 * @hashtable: hashtable to add to 51 * @hashtable: hashtable to add to 57 * @node: the &struct hlist_node of the object 52 * @node: the &struct hlist_node of the object to be added 58 * @key: the key of the object to be added 53 * @key: the key of the object to be added 59 */ 54 */ 60 #define hash_add(hashtable, node, key) 55 #define hash_add(hashtable, node, key) \ 61 hlist_add_head(node, &hashtable[hash_m 56 hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))]) 62 57 63 /** 58 /** 64 * hash_add_rcu - add an object to a rcu enabl 59 * hash_add_rcu - add an object to a rcu enabled hashtable 65 * @hashtable: hashtable to add to 60 * @hashtable: hashtable to add to 66 * @node: the &struct hlist_node of the object 61 * @node: the &struct hlist_node of the object to be added 67 * @key: the key of the object to be added 62 * @key: the key of the object to be added 68 */ 63 */ 69 #define hash_add_rcu(hashtable, node, key) 64 #define hash_add_rcu(hashtable, node, key) \ 70 hlist_add_head_rcu(node, &hashtable[ha 65 hlist_add_head_rcu(node, &hashtable[hash_min(key, HASH_BITS(hashtable))]) 71 66 72 /** 67 /** 73 * hash_hashed - check whether an object is in 68 * hash_hashed - check whether an object is in any hashtable 74 * @node: the &struct hlist_node of the object 69 * @node: the &struct hlist_node of the object to be checked 75 */ 70 */ 76 static inline bool hash_hashed(struct hlist_no 71 static inline bool hash_hashed(struct hlist_node *node) 77 { 72 { 78 return !hlist_unhashed(node); 73 return !hlist_unhashed(node); 79 } 74 } 80 75 81 static inline bool __hash_empty(struct hlist_h 76 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz) 82 { 77 { 83 unsigned int i; 78 unsigned int i; 84 79 85 for (i = 0; i < sz; i++) 80 for (i = 0; i < sz; i++) 86 if (!hlist_empty(&ht[i])) 81 if (!hlist_empty(&ht[i])) 87 return false; 82 return false; 88 83 89 return true; 84 return true; 90 } 85 } 91 86 92 /** 87 /** 93 * hash_empty - check whether a hashtable is e 88 * hash_empty - check whether a hashtable is empty 94 * @hashtable: hashtable to check 89 * @hashtable: hashtable to check 95 * 90 * 96 * This has to be a macro since HASH_BITS() wi 91 * This has to be a macro since HASH_BITS() will not work on pointers since 97 * it calculates the size during preprocessing 92 * it calculates the size during preprocessing. 98 */ 93 */ 99 #define hash_empty(hashtable) __hash_empty(has 94 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable)) 100 95 101 /** 96 /** 102 * hash_del - remove an object from a hashtabl 97 * hash_del - remove an object from a hashtable 103 * @node: &struct hlist_node of the object to 98 * @node: &struct hlist_node of the object to remove 104 */ 99 */ 105 static inline void hash_del(struct hlist_node 100 static inline void hash_del(struct hlist_node *node) 106 { 101 { 107 hlist_del_init(node); 102 hlist_del_init(node); 108 } 103 } 109 104 110 /** 105 /** 111 * hash_del_rcu - remove an object from a rcu 106 * hash_del_rcu - remove an object from a rcu enabled hashtable 112 * @node: &struct hlist_node of the object to 107 * @node: &struct hlist_node of the object to remove 113 */ 108 */ 114 static inline void hash_del_rcu(struct hlist_n 109 static inline void hash_del_rcu(struct hlist_node *node) 115 { 110 { 116 hlist_del_init_rcu(node); 111 hlist_del_init_rcu(node); 117 } 112 } 118 113 119 /** 114 /** 120 * hash_for_each - iterate over a hashtable 115 * hash_for_each - iterate over a hashtable 121 * @name: hashtable to iterate 116 * @name: hashtable to iterate 122 * @bkt: integer to use as bucket loop cursor 117 * @bkt: integer to use as bucket loop cursor 123 * @obj: the type * to use as a loop cursor fo 118 * @obj: the type * to use as a loop cursor for each entry 124 * @member: the name of the hlist_node within 119 * @member: the name of the hlist_node within the struct 125 */ 120 */ 126 #define hash_for_each(name, bkt, obj, member) 121 #define hash_for_each(name, bkt, obj, member) \ 127 for ((bkt) = 0, obj = NULL; obj == NUL 122 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\ 128 (bkt)++)\ 123 (bkt)++)\ 129 hlist_for_each_entry(obj, &nam 124 hlist_for_each_entry(obj, &name[bkt], member) 130 125 131 /** 126 /** 132 * hash_for_each_rcu - iterate over a rcu enab 127 * hash_for_each_rcu - iterate over a rcu enabled hashtable 133 * @name: hashtable to iterate 128 * @name: hashtable to iterate 134 * @bkt: integer to use as bucket loop cursor 129 * @bkt: integer to use as bucket loop cursor 135 * @obj: the type * to use as a loop cursor fo 130 * @obj: the type * to use as a loop cursor for each entry 136 * @member: the name of the hlist_node within 131 * @member: the name of the hlist_node within the struct 137 */ 132 */ 138 #define hash_for_each_rcu(name, bkt, obj, memb 133 #define hash_for_each_rcu(name, bkt, obj, member) \ 139 for ((bkt) = 0, obj = NULL; obj == NUL 134 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\ 140 (bkt)++)\ 135 (bkt)++)\ 141 hlist_for_each_entry_rcu(obj, 136 hlist_for_each_entry_rcu(obj, &name[bkt], member) 142 137 143 /** 138 /** 144 * hash_for_each_safe - iterate over a hashtab 139 * hash_for_each_safe - iterate over a hashtable safe against removal of 145 * hash entry 140 * hash entry 146 * @name: hashtable to iterate 141 * @name: hashtable to iterate 147 * @bkt: integer to use as bucket loop cursor 142 * @bkt: integer to use as bucket loop cursor 148 * @tmp: a &struct hlist_node used for tempora !! 143 * @tmp: a &struct used for temporary storage 149 * @obj: the type * to use as a loop cursor fo 144 * @obj: the type * to use as a loop cursor for each entry 150 * @member: the name of the hlist_node within 145 * @member: the name of the hlist_node within the struct 151 */ 146 */ 152 #define hash_for_each_safe(name, bkt, tmp, obj 147 #define hash_for_each_safe(name, bkt, tmp, obj, member) \ 153 for ((bkt) = 0, obj = NULL; obj == NUL 148 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\ 154 (bkt)++)\ 149 (bkt)++)\ 155 hlist_for_each_entry_safe(obj, 150 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member) 156 151 157 /** 152 /** 158 * hash_for_each_possible - iterate over all p 153 * hash_for_each_possible - iterate over all possible objects hashing to the 159 * same bucket 154 * same bucket 160 * @name: hashtable to iterate 155 * @name: hashtable to iterate 161 * @obj: the type * to use as a loop cursor fo 156 * @obj: the type * to use as a loop cursor for each entry 162 * @member: the name of the hlist_node within 157 * @member: the name of the hlist_node within the struct 163 * @key: the key of the objects to iterate ove 158 * @key: the key of the objects to iterate over 164 */ 159 */ 165 #define hash_for_each_possible(name, obj, memb 160 #define hash_for_each_possible(name, obj, member, key) \ 166 hlist_for_each_entry(obj, &name[hash_m 161 hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member) 167 162 168 /** 163 /** 169 * hash_for_each_possible_rcu - iterate over a 164 * hash_for_each_possible_rcu - iterate over all possible objects hashing to the 170 * same bucket in an rcu enabled hashtable 165 * same bucket in an rcu enabled hashtable >> 166 * in a rcu enabled hashtable 171 * @name: hashtable to iterate 167 * @name: hashtable to iterate 172 * @obj: the type * to use as a loop cursor fo 168 * @obj: the type * to use as a loop cursor for each entry 173 * @member: the name of the hlist_node within 169 * @member: the name of the hlist_node within the struct 174 * @key: the key of the objects to iterate ove 170 * @key: the key of the objects to iterate over 175 */ 171 */ 176 #define hash_for_each_possible_rcu(name, obj, !! 172 #define hash_for_each_possible_rcu(name, obj, member, key) \ 177 hlist_for_each_entry_rcu(obj, &name[ha 173 hlist_for_each_entry_rcu(obj, &name[hash_min(key, HASH_BITS(name))],\ 178 member, ## cond) !! 174 member) 179 175 180 /** 176 /** 181 * hash_for_each_possible_rcu_notrace - iterat 177 * hash_for_each_possible_rcu_notrace - iterate over all possible objects hashing 182 * to the same bucket in an rcu enabled hashta 178 * to the same bucket in an rcu enabled hashtable in a rcu enabled hashtable 183 * @name: hashtable to iterate 179 * @name: hashtable to iterate 184 * @obj: the type * to use as a loop cursor fo 180 * @obj: the type * to use as a loop cursor for each entry 185 * @member: the name of the hlist_node within 181 * @member: the name of the hlist_node within the struct 186 * @key: the key of the objects to iterate ove 182 * @key: the key of the objects to iterate over 187 * 183 * 188 * This is the same as hash_for_each_possible_ 184 * This is the same as hash_for_each_possible_rcu() except that it does 189 * not do any RCU debugging or tracing. 185 * not do any RCU debugging or tracing. 190 */ 186 */ 191 #define hash_for_each_possible_rcu_notrace(nam 187 #define hash_for_each_possible_rcu_notrace(name, obj, member, key) \ 192 hlist_for_each_entry_rcu_notrace(obj, 188 hlist_for_each_entry_rcu_notrace(obj, \ 193 &name[hash_min(key, HASH_BITS( 189 &name[hash_min(key, HASH_BITS(name))], member) 194 190 195 /** 191 /** 196 * hash_for_each_possible_safe - iterate over 192 * hash_for_each_possible_safe - iterate over all possible objects hashing to the 197 * same bucket safe against removals 193 * same bucket safe against removals 198 * @name: hashtable to iterate 194 * @name: hashtable to iterate 199 * @obj: the type * to use as a loop cursor fo 195 * @obj: the type * to use as a loop cursor for each entry 200 * @tmp: a &struct hlist_node used for tempora !! 196 * @tmp: a &struct used for temporary storage 201 * @member: the name of the hlist_node within 197 * @member: the name of the hlist_node within the struct 202 * @key: the key of the objects to iterate ove 198 * @key: the key of the objects to iterate over 203 */ 199 */ 204 #define hash_for_each_possible_safe(name, obj, 200 #define hash_for_each_possible_safe(name, obj, tmp, member, key) \ 205 hlist_for_each_entry_safe(obj, tmp,\ 201 hlist_for_each_entry_safe(obj, tmp,\ 206 &name[hash_min(key, HASH_BITS( 202 &name[hash_min(key, HASH_BITS(name))], member) 207 203 208 204 209 #endif 205 #endif 210 206
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.