hash.h 4.5 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2
/* Copyright (C) 2006-2018  B.A.T.M.A.N. contributors:
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Simon Wunderlich, Marek Lindner
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21
 */

#ifndef _NET_BATMAN_ADV_HASH_H_
#define _NET_BATMAN_ADV_HASH_H_

22 23
#include "main.h"

24
#include <linux/atomic.h>
25
#include <linux/compiler.h>
26
#include <linux/list.h>
27 28 29 30 31 32
#include <linux/rculist.h>
#include <linux/spinlock.h>
#include <linux/stddef.h>
#include <linux/types.h>

struct lock_class_key;
33

34
/* callback to a compare function.  should compare 2 element datas for their
35 36
 * keys
 *
37
 * Return: true if same and false if not same
38
 */
39 40
typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
					   const void *);
41

42 43 44 45
/* the hashfunction
 *
 * Return: an index based on the key in the data of the first argument and the
 * size the second
46
 */
47
typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
48
typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
49

50 51 52
/**
 * struct batadv_hashtable - Wrapper of simple hlist based hashtable
 */
53
struct batadv_hashtable {
54 55 56 57 58 59 60 61
	/** @table: the hashtable itself with the buckets */
	struct hlist_head *table;

	/** @list_locks: spinlock for each hash list entry */
	spinlock_t *list_locks;

	/** @size: size of hashtable */
	u32 size;
62 63 64

	/** @generation: current (generation) sequence number */
	atomic_t generation;
65 66 67
};

/* allocates and clears the hash */
68
struct batadv_hashtable *batadv_hash_new(u32 size);
69

70
/* set class key for all locks */
71
void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
72 73
				struct lock_class_key *key);

74
/* free only the hashtable and the hash itself. */
75
void batadv_hash_destroy(struct batadv_hashtable *hash);
76

77
/**
78
 *	batadv_hash_add() - adds data to the hashtable
79 80 81 82 83 84
 *	@hash: storage hash table
 *	@compare: callback to determine if 2 hash elements are identical
 *	@choose: callback calculating the hash index
 *	@data: data passed to the aforementioned callbacks as argument
 *	@data_node: to be added element
 *
85
 *	Return: 0 on success, 1 if the element already is in the hash
86 87
 *	and -1 on error.
 */
88 89 90
static inline int batadv_hash_add(struct batadv_hashtable *hash,
				  batadv_hashdata_compare_cb compare,
				  batadv_hashdata_choose_cb choose,
91 92
				  const void *data,
				  struct hlist_node *data_node)
93
{
94
	u32 index;
95
	int ret = -1;
96
	struct hlist_head *head;
97
	struct hlist_node *node;
98
	spinlock_t *list_lock; /* spinlock to protect write access */
99 100

	if (!hash)
101
		goto out;
102 103 104

	index = choose(data, hash->size);
	head = &hash->table[index];
105
	list_lock = &hash->list_locks[index];
106

107 108 109
	spin_lock_bh(list_lock);

	hlist_for_each(node, head) {
110 111 112
		if (!compare(node, data))
			continue;

113
		ret = 1;
114
		goto unlock;
115 116 117
	}

	/* no duplicate found in list, add new element */
118
	hlist_add_head_rcu(data_node, head);
119
	atomic_inc(&hash->generation);
120

121
	ret = 0;
122

123 124
unlock:
	spin_unlock_bh(list_lock);
125 126
out:
	return ret;
127 128
}

129 130 131 132 133 134 135 136 137
/**
 * batadv_hash_remove() - Removes data from hash, if found
 * @hash: hash table
 * @compare: callback to determine if 2 hash elements are identical
 * @choose: callback calculating the hash index
 * @data: data passed to the aforementioned callbacks as argument
 *
 * ata could be the structure you use with  just the key filled, we just need
 * the key for comparing.
138 139 140
 *
 * Return: returns pointer do data on success, so you can remove the used
 * structure yourself, or NULL on error
141
 */
142 143 144 145
static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
				       batadv_hashdata_compare_cb compare,
				       batadv_hashdata_choose_cb choose,
				       void *data)
146
{
147
	u32 index;
148
	struct hlist_node *node;
149
	struct hlist_head *head;
150
	void *data_save = NULL;
151 152 153 154

	index = choose(data, hash->size);
	head = &hash->table[index];

155
	spin_lock_bh(&hash->list_locks[index]);
156 157 158 159 160 161
	hlist_for_each(node, head) {
		if (!compare(node, data))
			continue;

		data_save = node;
		hlist_del_rcu(node);
162
		atomic_inc(&hash->generation);
163
		break;
164
	}
165
	spin_unlock_bh(&hash->list_locks[index]);
166

167
	return data_save;
168 169 170
}

#endif /* _NET_BATMAN_ADV_HASH_H_ */