diff options
author | Masahiro Yamada <masahiroy@kernel.org> | 2024-09-05 08:54:48 +0900 |
---|---|---|
committer | Masahiro Yamada <masahiroy@kernel.org> | 2024-09-10 13:56:37 +0900 |
commit | 16ff3f606c7e639d815ede92c258b63484242783 (patch) | |
tree | d86bcefb0aaf5a0186602b96d6ad774515ed3eb7 /scripts/include/hashtable.h | |
parent | e7e2941300d258d551dda6ca9a370e29e085fa73 (diff) |
scripts: import more hash table macros
Add more macros used for removing hash table entries.
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Diffstat (limited to 'scripts/include/hashtable.h')
-rw-r--r-- | scripts/include/hashtable.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/scripts/include/hashtable.h b/scripts/include/hashtable.h index a0a2c8f5f639..45abcb12bfce 100644 --- a/scripts/include/hashtable.h +++ b/scripts/include/hashtable.h @@ -15,6 +15,23 @@ #define hash_head(table, key) (&(table)[(key) % HASH_SIZE(table)]) +static inline void __hash_init(struct hlist_head *ht, unsigned int sz) +{ + unsigned int i; + + for (i = 0; i < sz; i++) + INIT_HLIST_HEAD(&ht[i]); +} + +/** + * hash_init - initialize a hash table + * @table: hashtable to be initialized + * + * This has to be a macro since HASH_SIZE() will not work on pointers since + * it calculates the size during preprocessing. + */ +#define hash_init(table) __hash_init(table, HASH_SIZE(table)) + /** * hash_add - add an object to a hashtable * @table: hashtable to add to @@ -25,6 +42,15 @@ hlist_add_head(node, hash_head(table, key)) /** + * hash_del - remove an object from a hashtable + * @node: &struct hlist_node of the object to remove + */ +static inline void hash_del(struct hlist_node *node) +{ + hlist_del_init(node); +} + +/** * hash_for_each - iterate over a hashtable * @table: hashtable to iterate * @obj: the type * to use as a loop cursor for each entry @@ -35,6 +61,18 @@ hlist_for_each_entry(obj, &table[_bkt], member) /** + * hash_for_each_safe - iterate over a hashtable safe against removal of + * hash entry + * @table: hashtable to iterate + * @obj: the type * to use as a loop cursor for each entry + * @tmp: a &struct hlist_node used for temporary storage + * @member: the name of the hlist_node within the struct + */ +#define hash_for_each_safe(table, obj, tmp, member) \ + for (int _bkt = 0; _bkt < HASH_SIZE(table); _bkt++) \ + hlist_for_each_entry_safe(obj, tmp, &table[_bkt], member) + +/** * hash_for_each_possible - iterate over all possible objects hashing to the * same bucket * @table: hashtable to iterate @@ -45,4 +83,16 @@ #define hash_for_each_possible(table, obj, member, key) \ hlist_for_each_entry(obj, hash_head(table, key), member) +/** + * hash_for_each_possible_safe - iterate over all possible objects hashing to the + * same bucket safe against removals + * @table: hashtable to iterate + * @obj: the type * to use as a loop cursor for each entry + * @tmp: a &struct hlist_node used for temporary storage + * @member: the name of the hlist_node within the struct + * @key: the key of the objects to iterate over + */ +#define hash_for_each_possible_safe(table, obj, tmp, member, key) \ + hlist_for_each_entry_safe(obj, tmp, hash_head(table, key), member) + #endif /* HASHTABLE_H */ |