Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hashtable.h
Go to the documentation of this file.
1 /*
2  * Statically sized hash table implementation
3  * (C) 2012 Sasha Levin <[email protected]>
4  */
5 
6 #ifndef _LINUX_HASHTABLE_H
7 #define _LINUX_HASHTABLE_H
8 
9 #include <linux/list.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/hash.h>
13 #include <linux/rculist.h>
14 
15 #define DEFINE_HASHTABLE(name, bits) \
16  struct hlist_head name[1 << (bits)] = \
17  { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
18 
19 #define DECLARE_HASHTABLE(name, bits) \
20  struct hlist_head name[1 << (bits)]
21 
22 #define HASH_SIZE(name) (ARRAY_SIZE(name))
23 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
24 
25 /* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
26 #define hash_min(val, bits) \
27  (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
28 
29 static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
30 {
31  unsigned int i;
32 
33  for (i = 0; i < sz; i++)
34  INIT_HLIST_HEAD(&ht[i]);
35 }
36 
47 #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
48 
55 #define hash_add(hashtable, node, key) \
56  hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
57 
64 #define hash_add_rcu(hashtable, node, key) \
65  hlist_add_head_rcu(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
66 
71 static inline bool hash_hashed(struct hlist_node *node)
72 {
73  return !hlist_unhashed(node);
74 }
75 
76 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
77 {
78  unsigned int i;
79 
80  for (i = 0; i < sz; i++)
81  if (!hlist_empty(&ht[i]))
82  return false;
83 
84  return true;
85 }
86 
94 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
95 
100 static inline void hash_del(struct hlist_node *node)
101 {
102  hlist_del_init(node);
103 }
104 
109 static inline void hash_del_rcu(struct hlist_node *node)
110 {
111  hlist_del_init_rcu(node);
112 }
113 
122 #define hash_for_each(name, bkt, node, obj, member) \
123  for ((bkt) = 0, node = NULL; node == NULL && (bkt) < HASH_SIZE(name); (bkt)++)\
124  hlist_for_each_entry(obj, node, &name[bkt], member)
125 
134 #define hash_for_each_rcu(name, bkt, node, obj, member) \
135  for ((bkt) = 0, node = NULL; node == NULL && (bkt) < HASH_SIZE(name); (bkt)++)\
136  hlist_for_each_entry_rcu(obj, node, &name[bkt], member)
137 
148 #define hash_for_each_safe(name, bkt, node, tmp, obj, member) \
149  for ((bkt) = 0, node = NULL; node == NULL && (bkt) < HASH_SIZE(name); (bkt)++)\
150  hlist_for_each_entry_safe(obj, node, tmp, &name[bkt], member)
151 
161 #define hash_for_each_possible(name, obj, node, member, key) \
162  hlist_for_each_entry(obj, node, &name[hash_min(key, HASH_BITS(name))], member)
163 
174 #define hash_for_each_possible_rcu(name, obj, node, member, key) \
175  hlist_for_each_entry_rcu(obj, node, &name[hash_min(key, HASH_BITS(name))], member)
176 
187 #define hash_for_each_possible_safe(name, obj, node, tmp, member, key) \
188  hlist_for_each_entry_safe(obj, node, tmp, \
189  &name[hash_min(key, HASH_BITS(name))], member)
190 
191 
192 #endif