hash.h 4.0 KB
Newer Older
1
/*
2
 * Copyright (C) 2006-2011 B.A.T.M.A.N. contributors:
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 *
 */

#ifndef _NET_BATMAN_ADV_HASH_H_
#define _NET_BATMAN_ADV_HASH_H_

#include <linux/list.h>

/* callback to a compare function.  should
 * compare 2 element datas for their keys,
 * return 0 if same and not 0 if not
 * same */
31
typedef int (*hashdata_compare_cb)(const struct hlist_node *, const void *);
32 33 34 35

/* the hashfunction, should return an index
 * based on the key in the data of the first
 * argument and the size the second */
36
typedef int (*hashdata_choose_cb)(const void *, int);
37
typedef void (*hashdata_free_cb)(struct hlist_node *, void *);
38 39

struct hashtable_t {
40 41
	struct hlist_head *table;   /* the hashtable itself with the buckets */
	spinlock_t *list_locks;     /* spinlock for each hash list entry */
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	int size;		    /* size of hashtable */
};

/* allocates and clears the hash */
struct hashtable_t *hash_new(int size);

/* free only the hashtable and the hash itself. */
void hash_destroy(struct hashtable_t *hash);

/* remove the hash structure. if hashdata_free_cb != NULL, this function will be
 * called to remove the elements inside of the hash.  if you don't remove the
 * elements, memory might be leaked. */
static inline void hash_delete(struct hashtable_t *hash,
			       hashdata_free_cb free_cb, void *arg)
{
	struct hlist_head *head;
58
	struct hlist_node *node, *node_tmp;
59
	spinlock_t *list_lock; /* spinlock to protect write access */
60 61 62 63
	int i;

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
64
		list_lock = &hash->list_locks[i];
65

66
		spin_lock_bh(list_lock);
67 68
		hlist_for_each_safe(node, node_tmp, head) {
			hlist_del_rcu(node);
69

70 71
			if (free_cb)
				free_cb(node, arg);
72
		}
73
		spin_unlock_bh(list_lock);
74 75 76 77 78 79 80 81
	}

	hash_destroy(hash);
}

/* adds data to the hashtable. returns 0 on success, -1 on error */
static inline int hash_add(struct hashtable_t *hash,
			   hashdata_compare_cb compare,
82
			   hashdata_choose_cb choose,
83
			   const void *data, struct hlist_node *data_node)
84 85 86
{
	int index;
	struct hlist_head *head;
87
	struct hlist_node *node;
88
	spinlock_t *list_lock; /* spinlock to protect write access */
89 90

	if (!hash)
91
		goto err;
92 93 94

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

97
	rcu_read_lock();
98 99 100 101 102
	__hlist_for_each_rcu(node, head) {
		if (!compare(node, data))
			continue;

		goto err_unlock;
103
	}
104
	rcu_read_unlock();
105 106

	/* no duplicate found in list, add new element */
107
	spin_lock_bh(list_lock);
108
	hlist_add_head_rcu(data_node, head);
109
	spin_unlock_bh(list_lock);
110 111

	return 0;
112 113 114 115 116

err_unlock:
	rcu_read_unlock();
err:
	return -1;
117 118 119 120 121 122 123 124 125 126 127
}

/* removes data from hash, if found. returns pointer do data on success, so you
 * can remove the used structure yourself, or NULL on error .  data could be the
 * structure you use with just the key filled, we just need the key for
 * comparing. */
static inline void *hash_remove(struct hashtable_t *hash,
				hashdata_compare_cb compare,
				hashdata_choose_cb choose, void *data)
{
	size_t index;
128
	struct hlist_node *node;
129
	struct hlist_head *head;
130
	void *data_save = NULL;
131 132 133 134

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

135
	spin_lock_bh(&hash->list_locks[index]);
136 137 138 139 140 141 142
	hlist_for_each(node, head) {
		if (!compare(node, data))
			continue;

		data_save = node;
		hlist_del_rcu(node);
		break;
143
	}
144
	spin_unlock_bh(&hash->list_locks[index]);
145

146
	return data_save;
147 148 149
}

#endif /* _NET_BATMAN_ADV_HASH_H_ */