1*6c54b3a6SFrançois Tigeot /* $NetBSD: hashtable.h,v 1.5 2018/08/27 06:39:27 riastradh Exp $ */
2*6c54b3a6SFrançois Tigeot
3*6c54b3a6SFrançois Tigeot /*-
4*6c54b3a6SFrançois Tigeot * Copyright (c) 2018 The NetBSD Foundation, Inc.
5f480168bSFrançois Tigeot * All rights reserved.
6f480168bSFrançois Tigeot *
7*6c54b3a6SFrançois Tigeot * This code is derived from software contributed to The NetBSD Foundation
8*6c54b3a6SFrançois Tigeot * by Taylor R. Campbell.
9*6c54b3a6SFrançois Tigeot *
10f480168bSFrançois Tigeot * Redistribution and use in source and binary forms, with or without
11f480168bSFrançois Tigeot * modification, are permitted provided that the following conditions
12f480168bSFrançois Tigeot * are met:
13f480168bSFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
14*6c54b3a6SFrançois Tigeot * notice, this list of conditions and the following disclaimer.
15f480168bSFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
16f480168bSFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
17f480168bSFrançois Tigeot * documentation and/or other materials provided with the distribution.
18f480168bSFrançois Tigeot *
19*6c54b3a6SFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*6c54b3a6SFrançois Tigeot * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*6c54b3a6SFrançois Tigeot * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*6c54b3a6SFrançois Tigeot * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*6c54b3a6SFrançois Tigeot * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*6c54b3a6SFrançois Tigeot * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*6c54b3a6SFrançois Tigeot * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*6c54b3a6SFrançois Tigeot * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*6c54b3a6SFrançois Tigeot * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*6c54b3a6SFrançois Tigeot * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*6c54b3a6SFrançois Tigeot * POSSIBILITY OF SUCH DAMAGE.
30f480168bSFrançois Tigeot */
31f480168bSFrançois Tigeot
32f480168bSFrançois Tigeot #ifndef _LINUX_HASHTABLE_H_
33f480168bSFrançois Tigeot #define _LINUX_HASHTABLE_H_
34f480168bSFrançois Tigeot
35*6c54b3a6SFrançois Tigeot #include <linux/list.h>
36*6c54b3a6SFrançois Tigeot #include <linux/types.h>
37*6c54b3a6SFrançois Tigeot #include <linux/kernel.h>
38*6c54b3a6SFrançois Tigeot #include <linux/hash.h>
39d2a9170aSFrançois Tigeot #include <linux/rculist.h>
40d2a9170aSFrançois Tigeot
41*6c54b3a6SFrançois Tigeot #define DECLARE_HASHTABLE(name, bits) \
42*6c54b3a6SFrançois Tigeot struct hlist_head name[1u << (bits)]
43f480168bSFrançois Tigeot
44*6c54b3a6SFrançois Tigeot static inline void
__hash_init(struct hlist_head * hash,unsigned n)45*6c54b3a6SFrançois Tigeot __hash_init(struct hlist_head *hash, unsigned n)
46*6c54b3a6SFrançois Tigeot {
47*6c54b3a6SFrançois Tigeot
48*6c54b3a6SFrançois Tigeot while (n --> 0)
49*6c54b3a6SFrançois Tigeot INIT_HLIST_HEAD(&hash[n]);
50*6c54b3a6SFrançois Tigeot }
51*6c54b3a6SFrançois Tigeot
52*6c54b3a6SFrançois Tigeot static inline bool
__hash_empty(struct hlist_head * hash,unsigned n)53*6c54b3a6SFrançois Tigeot __hash_empty(struct hlist_head *hash, unsigned n)
54*6c54b3a6SFrançois Tigeot {
55*6c54b3a6SFrançois Tigeot
56*6c54b3a6SFrançois Tigeot while (n --> 0) {
57*6c54b3a6SFrançois Tigeot if (!hlist_empty(&hash[n]))
58*6c54b3a6SFrançois Tigeot return false;
59*6c54b3a6SFrançois Tigeot }
60*6c54b3a6SFrançois Tigeot
61*6c54b3a6SFrançois Tigeot return true;
62*6c54b3a6SFrançois Tigeot }
63*6c54b3a6SFrançois Tigeot
64*6c54b3a6SFrançois Tigeot #define __arraycount(array) NELEM(array)
65*6c54b3a6SFrançois Tigeot
66*6c54b3a6SFrançois Tigeot #define hash_init(h) __hash_init((h), __arraycount(h))
67*6c54b3a6SFrançois Tigeot #define hash_add(h, n, k) hlist_add_head(n, &(h)[(k) % __arraycount(h)])
68*6c54b3a6SFrançois Tigeot #define hash_del(n) hlist_del_init(n)
69*6c54b3a6SFrançois Tigeot #define hash_empty(h) __hash_empty((h), __arraycount(h))
70*6c54b3a6SFrançois Tigeot
71*6c54b3a6SFrançois Tigeot #define hash_for_each_possible(h, n, f, k) \
72*6c54b3a6SFrançois Tigeot hlist_for_each_entry(n, &(h)[(k) % __arraycount(h)], f)
73*6c54b3a6SFrançois Tigeot
74*6c54b3a6SFrançois Tigeot #define hash_for_each_safe(h, i, t, n, f) \
75*6c54b3a6SFrançois Tigeot for ((i) = 0; (i) < __arraycount(h); (i)++) \
76*6c54b3a6SFrançois Tigeot hlist_for_each_entry_safe(n, t, &(h)[i], f)
77d2a9170aSFrançois Tigeot
78f480168bSFrançois Tigeot #endif /*_LINUX_HASHTABLE_H_*/
79