xref: /linux/scripts/include/hashtable.h (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef HASHTABLE_H
3 #define HASHTABLE_H
4 
5 #include "array_size.h"
6 #include "list.h"
7 
8 #define HASH_SIZE(name) (ARRAY_SIZE(name))
9 
10 #define HASHTABLE_DECLARE(name, size)		struct hlist_head name[size]
11 
12 #define HASHTABLE_DEFINE(name, size)						\
13 	HASHTABLE_DECLARE(name, size) =						\
14 			{ [0 ... ((size) - 1)] = HLIST_HEAD_INIT }
15 
16 #define hash_head(table, key)		(&(table)[(key) % HASH_SIZE(table)])
17 
18 static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
19 {
20 	unsigned int i;
21 
22 	for (i = 0; i < sz; i++)
23 		INIT_HLIST_HEAD(&ht[i]);
24 }
25 
26 /**
27  * hash_init - initialize a hash table
28  * @table: hashtable to be initialized
29  *
30  * This has to be a macro since HASH_SIZE() will not work on pointers since
31  * it calculates the size during preprocessing.
32  */
33 #define hash_init(table) __hash_init(table, HASH_SIZE(table))
34 
35 /**
36  * hash_add - add an object to a hashtable
37  * @table: hashtable to add to
38  * @node: the &struct hlist_node of the object to be added
39  * @key: the key of the object to be added
40  */
41 #define hash_add(table, node, key)						\
42 	hlist_add_head(node, hash_head(table, key))
43 
44 /**
45  * hash_del - remove an object from a hashtable
46  * @node: &struct hlist_node of the object to remove
47  */
48 static inline void hash_del(struct hlist_node *node)
49 {
50 	hlist_del_init(node);
51 }
52 
53 /**
54  * hash_for_each - iterate over a hashtable
55  * @table: hashtable to iterate
56  * @obj: the type * to use as a loop cursor for each entry
57  * @member: the name of the hlist_node within the struct
58  */
59 #define hash_for_each(table, obj, member)				\
60 	for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++)		\
61 		hlist_for_each_entry(obj, &table[_bkt], member)
62 
63 /**
64  * hash_for_each_safe - iterate over a hashtable safe against removal of
65  * hash entry
66  * @table: hashtable to iterate
67  * @obj: the type * to use as a loop cursor for each entry
68  * @tmp: a &struct hlist_node used for temporary storage
69  * @member: the name of the hlist_node within the struct
70  */
71 #define hash_for_each_safe(table, obj, tmp, member)			\
72 	for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++)		\
73 		hlist_for_each_entry_safe(obj, tmp, &table[_bkt], member)
74 
75 /**
76  * hash_for_each_possible - iterate over all possible objects hashing to the
77  * same bucket
78  * @table: hashtable to iterate
79  * @obj: the type * to use as a loop cursor for each entry
80  * @member: the name of the hlist_node within the struct
81  * @key: the key of the objects to iterate over
82  */
83 #define hash_for_each_possible(table, obj, member, key)			\
84 	hlist_for_each_entry(obj, hash_head(table, key), member)
85 
86 /**
87  * hash_for_each_possible_safe - iterate over all possible objects hashing to the
88  * same bucket safe against removals
89  * @table: hashtable to iterate
90  * @obj: the type * to use as a loop cursor for each entry
91  * @tmp: a &struct hlist_node used for temporary storage
92  * @member: the name of the hlist_node within the struct
93  * @key: the key of the objects to iterate over
94  */
95 #define hash_for_each_possible_safe(table, obj, tmp, member, key)	\
96 	hlist_for_each_entry_safe(obj, tmp, hash_head(table, key), member)
97 
98 #endif /* HASHTABLE_H */
99