1
2
3
4
5
6
7#ifndef _LINUX_HASHTABLE_H
8#define _LINUX_HASHTABLE_H
9
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>
16
17#define DEFINE_HASHTABLE(name, bits) \
18 struct hlist_head name[1 << (bits)] = \
19 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
20
21#define DECLARE_HASHTABLE(name, bits) \
22 struct hlist_head name[1 << (bits)]
23
24#define HASH_SIZE(name) (ARRAY_SIZE(name))
25#define HASH_BITS(name) ilog2(HASH_SIZE(name))
26
27
28#define hash_min(val, bits) \
29 (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
30
31static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
32{
33 unsigned int i;
34
35 for (i = 0; i < sz; i++)
36 INIT_HLIST_HEAD(&ht[i]);
37}
38
39
40
41
42
43
44
45
46
47
48
49#define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
50
51
52
53
54
55
56
57#define hash_add(hashtable, node, key) \
58 hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
59
60
61
62
63
64static inline bool hash_hashed(struct hlist_node *node)
65{
66 return !hlist_unhashed(node);
67}
68
69static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
70{
71 unsigned int i;
72
73 for (i = 0; i < sz; i++)
74 if (!hlist_empty(&ht[i]))
75 return false;
76
77 return true;
78}
79
80
81
82
83
84
85
86
87#define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
88
89
90
91
92
93static inline void hash_del(struct hlist_node *node)
94{
95 hlist_del_init(node);
96}
97
98
99
100
101
102
103
104
105#define hash_for_each(name, bkt, obj, member) \
106 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
107 (bkt)++)\
108 hlist_for_each_entry(obj, &name[bkt], member)
109
110
111
112
113
114
115
116
117
118
119#define hash_for_each_safe(name, bkt, tmp, obj, member) \
120 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
121 (bkt)++)\
122 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
123
124
125
126
127
128
129
130
131
132#define hash_for_each_possible(name, obj, member, key) \
133 hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
134
135
136
137
138
139
140
141
142
143
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)
147
148
149#endif
150