This hashtable implementation is using hlist buckets to provide a simple hashtable to prevent it from getting reimplemented all over the kernel. Signed-off-by: Sasha Levin <levinsasha928@xxxxxxxxx> --- include/linux/hashtable.h | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-) create mode 100644 include/linux/hashtable.h diff --git a/include/linux/hashtable.h b/include/linux/hashtable.h new file mode 100644 index 0000000..9b29fd1 --- /dev/null +++ b/include/linux/hashtable.h @@ -0,0 +1,46 @@ +#ifndef _LINUX_HASHTABLE_H +#define _LINUX_HASHTABLE_H + +#include <linux/list.h> +#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/hash.h> + +#define DEFINE_HASHTABLE(name, bits) \ + struct hlist_head name[1 << bits]; + +#define HASH_BITS(name) (order_base_2(ARRAY_SIZE(name))) +#define HASH_SIZE(name) (1 << (HASH_BITS(name))) + +#define HASH_INIT(name) \ +({ \ + int __i; \ + for (__i = 0 ; __i < HASH_SIZE(name) ; __i++) \ + INIT_HLIST_HEAD(&name[__i]); \ +}) + +#define HASH_ADD(name, obj, key) \ + hlist_add_head(obj, &name[ \ + hash_long((unsigned long)key, HASH_BITS(name))]); + +#define HASH_GET(name, key, type, member, cmp_fn) \ +({ \ + struct hlist_node *__node; \ + typeof(key) __key = key; \ + type *__obj = NULL; \ + hlist_for_each_entry(__obj, __node, &name[ \ + hash_long((unsigned long) __key, \ + HASH_BITS(name))], member) \ + if (cmp_fn(__obj, __key)) \ + break; \ + __obj; \ +}) + +#define HASH_DEL(obj, member) \ + hlist_del(&obj->member) + +#define HASH_FOR_EACH(bkt, node, name, obj, member) \ + for (bkt = 0; bkt < HASH_SIZE(name); bkt++) \ + hlist_for_each_entry(obj, node, &name[i], member) + +#endif -- 1.7.8.6 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@xxxxxxxxx. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"dont@xxxxxxxxx"> email@xxxxxxxxx </a>