]>
git.ipfire.org Git - people/arne_f/kernel.git/blob - tools/include/linux/hashtable.h
1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Statically sized hash table implementation
4 * (C) 2012 Sasha Levin <levinsasha928@gmail.com>
7 #ifndef _LINUX_HASHTABLE_H
8 #define _LINUX_HASHTABLE_H
10 #include <linux/list.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/bitops.h>
14 #include <linux/hash.h>
15 #include <linux/log2.h>
17 #define DEFINE_HASHTABLE(name, bits) \
18 struct hlist_head name[1 << (bits)] = \
19 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
21 #define DECLARE_HASHTABLE(name, bits) \
22 struct hlist_head name[1 << (bits)]
24 #define HASH_SIZE(name) (ARRAY_SIZE(name))
25 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
27 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
28 #define hash_min(val, bits) \
29 (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
31 static inline void __hash_init(struct hlist_head
*ht
, unsigned int sz
)
35 for (i
= 0; i
< sz
; i
++)
36 INIT_HLIST_HEAD(&ht
[i
]);
40 * hash_init - initialize a hash table
41 * @hashtable: hashtable to be initialized
43 * Calculates the size of the hashtable from the given parameter, otherwise
44 * same as hash_init_size.
46 * This has to be a macro since HASH_BITS() will not work on pointers since
47 * it calculates the size during preprocessing.
49 #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
52 * hash_add - add an object to a hashtable
53 * @hashtable: hashtable to add to
54 * @node: the &struct hlist_node of the object to be added
55 * @key: the key of the object to be added
57 #define hash_add(hashtable, node, key) \
58 hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
61 * hash_hashed - check whether an object is in any hashtable
62 * @node: the &struct hlist_node of the object to be checked
64 static inline bool hash_hashed(struct hlist_node
*node
)
66 return !hlist_unhashed(node
);
69 static inline bool __hash_empty(struct hlist_head
*ht
, unsigned int sz
)
73 for (i
= 0; i
< sz
; i
++)
74 if (!hlist_empty(&ht
[i
]))
81 * hash_empty - check whether a hashtable is empty
82 * @hashtable: hashtable to check
84 * This has to be a macro since HASH_BITS() will not work on pointers since
85 * it calculates the size during preprocessing.
87 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
90 * hash_del - remove an object from a hashtable
91 * @node: &struct hlist_node of the object to remove
93 static inline void hash_del(struct hlist_node
*node
)
99 * hash_for_each - iterate over a hashtable
100 * @name: hashtable to iterate
101 * @bkt: integer to use as bucket loop cursor
102 * @obj: the type * to use as a loop cursor for each entry
103 * @member: the name of the hlist_node within the struct
105 #define hash_for_each(name, bkt, obj, member) \
106 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
108 hlist_for_each_entry(obj, &name[bkt], member)
111 * hash_for_each_safe - iterate over a hashtable safe against removal of
113 * @name: hashtable to iterate
114 * @bkt: integer to use as bucket loop cursor
115 * @tmp: a &struct used for temporary storage
116 * @obj: the type * to use as a loop cursor for each entry
117 * @member: the name of the hlist_node within the struct
119 #define hash_for_each_safe(name, bkt, tmp, obj, member) \
120 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
122 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
125 * hash_for_each_possible - iterate over all possible objects hashing to the
127 * @name: hashtable to iterate
128 * @obj: the type * to use as a loop cursor for each entry
129 * @member: the name of the hlist_node within the struct
130 * @key: the key of the objects to iterate over
132 #define hash_for_each_possible(name, obj, member, key) \
133 hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
136 * hash_for_each_possible_safe - iterate over all possible objects hashing to the
137 * same bucket safe against removals
138 * @name: hashtable to iterate
139 * @obj: the type * to use as a loop cursor for each entry
140 * @tmp: a &struct used for temporary storage
141 * @member: the name of the hlist_node within the struct
142 * @key: the key of the objects to iterate over
144 #define hash_for_each_possible_safe(name, obj, tmp, member, key) \
145 hlist_for_each_entry_safe(obj, tmp,\
146 &name[hash_min(key, HASH_BITS(name))], member)