blob: acaf6b91a07f63cd283362e9856e4f2a8dabdd9e [file] [log] [blame]
Harald Welte7101ca22020-12-04 21:44:44 +01001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Statically sized hash table implementation
4 * (C) 2012 Sasha Levin <levinsasha928@gmail.com>
5 */
6
7#pragma once
8
9#include <osmocom/core/linuxlist.h>
10#include <osmocom/core/hash.h>
11
12#define DEFINE_HASHTABLE(name, bits) \
13 struct hlist_head name[1 << (bits)] = \
14 { [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
15
16#define DECLARE_HASHTABLE(name, bits) \
17 struct hlist_head name[1 << (bits)]
18
19#define HASH_SIZE(name) (ARRAY_SIZE(name))
20#define HASH_BITS(name) ilog2(HASH_SIZE(name))
21
22/* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
23#define hash_min(val, bits) \
24 (sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
25
26static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
27{
28 unsigned int i;
29
30 for (i = 0; i < sz; i++)
31 INIT_HLIST_HEAD(&ht[i]);
32}
33
34/**
35 * hash_init - initialize a hash table
36 * @hashtable: hashtable to be initialized
37 *
38 * Calculates the size of the hashtable from the given parameter, otherwise
39 * same as hash_init_size.
40 *
41 * This has to be a macro since HASH_BITS() will not work on pointers since
42 * it calculates the size during preprocessing.
43 */
44#define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
45
46/**
47 * hash_add - add an object to a hashtable
48 * @hashtable: hashtable to add to
49 * @node: the &struct hlist_node of the object to be added
50 * @key: the key of the object to be added
51 */
52#define hash_add(hashtable, node, key) \
53 hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
54
55/**
56 * hash_hashed - check whether an object is in any hashtable
57 * @node: the &struct hlist_node of the object to be checked
58 */
59static inline bool hash_hashed(struct hlist_node *node)
60{
61 return !hlist_unhashed(node);
62}
63
64static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
65{
66 unsigned int i;
67
68 for (i = 0; i < sz; i++)
69 if (!hlist_empty(&ht[i]))
70 return false;
71
72 return true;
73}
74
75/**
76 * hash_empty - check whether a hashtable is empty
77 * @hashtable: hashtable to check
78 *
79 * This has to be a macro since HASH_BITS() will not work on pointers since
80 * it calculates the size during preprocessing.
81 */
82#define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
83
84/**
85 * hash_del - remove an object from a hashtable
86 * @node: &struct hlist_node of the object to remove
87 */
88static inline void hash_del(struct hlist_node *node)
89{
90 hlist_del_init(node);
91}
92
93/**
94 * hash_for_each - iterate over a hashtable
95 * @name: hashtable to iterate
96 * @bkt: integer to use as bucket loop cursor
97 * @obj: the type * to use as a loop cursor for each entry
98 * @member: the name of the hlist_node within the struct
99 */
100#define hash_for_each(name, bkt, obj, member) \
101 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
102 (bkt)++)\
103 hlist_for_each_entry(obj, &name[bkt], member)
104
105/**
106 * hash_for_each_safe - iterate over a hashtable safe against removal of
107 * hash entry
108 * @name: hashtable to iterate
109 * @bkt: integer to use as bucket loop cursor
110 * @tmp: a &struct hlist_node used for temporary storage
111 * @obj: the type * to use as a loop cursor for each entry
112 * @member: the name of the hlist_node within the struct
113 */
114#define hash_for_each_safe(name, bkt, tmp, obj, member) \
115 for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
116 (bkt)++)\
117 hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
118
119/**
120 * hash_for_each_possible - iterate over all possible objects hashing to the
121 * same bucket
122 * @name: hashtable to iterate
123 * @obj: the type * to use as a loop cursor for each entry
124 * @member: the name of the hlist_node within the struct
125 * @key: the key of the objects to iterate over
126 */
127#define hash_for_each_possible(name, obj, member, key) \
128 hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
129
130/**
131 * hash_for_each_possible_safe - iterate over all possible objects hashing to the
132 * same bucket safe against removals
133 * @name: hashtable to iterate
134 * @obj: the type * to use as a loop cursor for each entry
135 * @tmp: a &struct hlist_node used for temporary storage
136 * @member: the name of the hlist_node within the struct
137 * @key: the key of the objects to iterate over
138 */
139#define hash_for_each_possible_safe(name, obj, tmp, member, key) \
140 hlist_for_each_entry_safe(obj, tmp,\
141 &name[hash_min(key, HASH_BITS(name))], member)