1 /* $OpenBSD: hashtable.h,v 1.1 2019/04/14 10:14:53 jsg Exp $ */ 2 /* 3 * Copyright (c) 2017 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #ifndef _LINUX_HASHTABLE_H 19 #define _LINUX_HASHTABLE_H 20 21 #include <linux/list.h> 22 23 #define DECLARE_HASHTABLE(name, bits) struct hlist_head name[1 << (bits)] 24 25 static inline void 26 __hash_init(struct hlist_head *table, u_int size) 27 { 28 u_int i; 29 30 for (i = 0; i < size; i++) 31 INIT_HLIST_HEAD(&table[i]); 32 } 33 34 static inline bool 35 __hash_empty(struct hlist_head *table, u_int size) 36 { 37 u_int i; 38 39 for (i = 0; i < size; i++) { 40 if (!hlist_empty(&table[i])) 41 return false; 42 } 43 44 return true; 45 } 46 47 #define __hash(table, key) &table[key % (nitems(table) - 1)] 48 49 #define hash_init(table) __hash_init(table, nitems(table)) 50 #define hash_add(table, node, key) \ 51 hlist_add_head(node, __hash(table, key)) 52 #define hash_del(node) hlist_del_init(node) 53 #define hash_empty(table) __hash_empty(table, nitems(table)) 54 #define hash_for_each_possible(table, obj, member, key) \ 55 hlist_for_each_entry(obj, __hash(table, key), member) 56 #define hash_for_each_safe(table, i, tmp, obj, member) \ 57 for (i = 0; i < nitems(table); i++) \ 58 hlist_for_each_entry_safe(obj, tmp, &table[i], member) 59 60 #endif 61