]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/include/linux/hashtable.h
tools include: Drop ARRAY_SIZE() definition from linux/hashtable.h
[karo-tx-linux.git] / tools / include / linux / hashtable.h
1 /*
2  * Statically sized hash table implementation
3  * (C) 2012  Sasha Levin <levinsasha928@gmail.com>
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/bitops.h>
13 #include <linux/hash.h>
14 #include <linux/log2.h>
15
16 #define DEFINE_HASHTABLE(name, bits)                                            \
17         struct hlist_head name[1 << (bits)] =                                   \
18                         { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
19
20 #define DECLARE_HASHTABLE(name, bits)                                           \
21         struct hlist_head name[1 << (bits)]
22
23 #define HASH_SIZE(name) (ARRAY_SIZE(name))
24 #define HASH_BITS(name) ilog2(HASH_SIZE(name))
25
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))
29
30 static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
31 {
32         unsigned int i;
33
34         for (i = 0; i < sz; i++)
35                 INIT_HLIST_HEAD(&ht[i]);
36 }
37
38 /**
39  * hash_init - initialize a hash table
40  * @hashtable: hashtable to be initialized
41  *
42  * Calculates the size of the hashtable from the given parameter, otherwise
43  * same as hash_init_size.
44  *
45  * This has to be a macro since HASH_BITS() will not work on pointers since
46  * it calculates the size during preprocessing.
47  */
48 #define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
49
50 /**
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
55  */
56 #define hash_add(hashtable, node, key)                                          \
57         hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
58
59 /**
60  * hash_hashed - check whether an object is in any hashtable
61  * @node: the &struct hlist_node of the object to be checked
62  */
63 static inline bool hash_hashed(struct hlist_node *node)
64 {
65         return !hlist_unhashed(node);
66 }
67
68 static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
69 {
70         unsigned int i;
71
72         for (i = 0; i < sz; i++)
73                 if (!hlist_empty(&ht[i]))
74                         return false;
75
76         return true;
77 }
78
79 /**
80  * hash_empty - check whether a hashtable is empty
81  * @hashtable: hashtable to check
82  *
83  * This has to be a macro since HASH_BITS() will not work on pointers since
84  * it calculates the size during preprocessing.
85  */
86 #define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
87
88 /**
89  * hash_del - remove an object from a hashtable
90  * @node: &struct hlist_node of the object to remove
91  */
92 static inline void hash_del(struct hlist_node *node)
93 {
94         hlist_del_init(node);
95 }
96
97 /**
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
103  */
104 #define hash_for_each(name, bkt, obj, member)                           \
105         for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
106                         (bkt)++)\
107                 hlist_for_each_entry(obj, &name[bkt], member)
108
109 /**
110  * hash_for_each_safe - iterate over a hashtable safe against removal of
111  * hash entry
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
117  */
118 #define hash_for_each_safe(name, bkt, tmp, obj, member)                 \
119         for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
120                         (bkt)++)\
121                 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
122
123 /**
124  * hash_for_each_possible - iterate over all possible objects hashing to the
125  * same bucket
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
130  */
131 #define hash_for_each_possible(name, obj, member, key)                  \
132         hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
133
134 /**
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
142  */
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)
146
147
148 #endif