Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 64afe35 | 2011-01-27 10:38:15 +0100 | [diff] [blame] | 2 | * Copyright (C) 2006-2011 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Simon Wunderlich, Marek Lindner |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of version 2 of the GNU General Public |
| 8 | * License as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #ifndef _NET_BATMAN_ADV_HASH_H_ |
| 23 | #define _NET_BATMAN_ADV_HASH_H_ |
| 24 | |
| 25 | #include <linux/list.h> |
| 26 | |
| 27 | /* callback to a compare function. should |
| 28 | * compare 2 element datas for their keys, |
| 29 | * return 0 if same and not 0 if not |
| 30 | * same */ |
| 31 | typedef int (*hashdata_compare_cb)(void *, void *); |
| 32 | |
| 33 | /* the hashfunction, should return an index |
| 34 | * based on the key in the data of the first |
| 35 | * argument and the size the second */ |
| 36 | typedef int (*hashdata_choose_cb)(void *, int); |
| 37 | typedef void (*hashdata_free_cb)(void *, void *); |
| 38 | |
| 39 | struct element_t { |
| 40 | void *data; /* pointer to the data */ |
| 41 | struct hlist_node hlist; /* bucket list pointer */ |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 42 | struct rcu_head rcu; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct hashtable_t { |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 46 | struct hlist_head *table; /* the hashtable itself with the buckets */ |
| 47 | spinlock_t *list_locks; /* spinlock for each hash list entry */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 48 | int size; /* size of hashtable */ |
| 49 | }; |
| 50 | |
| 51 | /* allocates and clears the hash */ |
| 52 | struct hashtable_t *hash_new(int size); |
| 53 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 54 | /* free only the hashtable and the hash itself. */ |
| 55 | void hash_destroy(struct hashtable_t *hash); |
| 56 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 57 | void bucket_free_rcu(struct rcu_head *rcu); |
| 58 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 59 | /* remove the hash structure. if hashdata_free_cb != NULL, this function will be |
| 60 | * called to remove the elements inside of the hash. if you don't remove the |
| 61 | * elements, memory might be leaked. */ |
| 62 | static inline void hash_delete(struct hashtable_t *hash, |
| 63 | hashdata_free_cb free_cb, void *arg) |
| 64 | { |
| 65 | struct hlist_head *head; |
| 66 | struct hlist_node *walk, *safe; |
| 67 | struct element_t *bucket; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 68 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 69 | int i; |
| 70 | |
| 71 | for (i = 0; i < hash->size; i++) { |
| 72 | head = &hash->table[i]; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 73 | list_lock = &hash->list_locks[i]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 74 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 75 | spin_lock_bh(list_lock); |
| 76 | hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 77 | if (free_cb) |
| 78 | free_cb(bucket->data, arg); |
| 79 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 80 | hlist_del_rcu(walk); |
| 81 | call_rcu(&bucket->rcu, bucket_free_rcu); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 82 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 83 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | hash_destroy(hash); |
| 87 | } |
| 88 | |
| 89 | /* adds data to the hashtable. returns 0 on success, -1 on error */ |
| 90 | static inline int hash_add(struct hashtable_t *hash, |
| 91 | hashdata_compare_cb compare, |
| 92 | hashdata_choose_cb choose, void *data) |
| 93 | { |
| 94 | int index; |
| 95 | struct hlist_head *head; |
| 96 | struct hlist_node *walk, *safe; |
| 97 | struct element_t *bucket; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 98 | spinlock_t *list_lock; /* spinlock to protect write access */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 99 | |
| 100 | if (!hash) |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 101 | goto err; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 102 | |
| 103 | index = choose(data, hash->size); |
| 104 | head = &hash->table[index]; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 105 | list_lock = &hash->list_locks[index]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 106 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 107 | rcu_read_lock(); |
| 108 | hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 109 | if (compare(bucket->data, data)) |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 110 | goto err_unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 111 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 112 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 113 | |
| 114 | /* no duplicate found in list, add new element */ |
| 115 | bucket = kmalloc(sizeof(struct element_t), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 116 | if (!bucket) |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 117 | goto err; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 118 | |
| 119 | bucket->data = data; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 120 | |
| 121 | spin_lock_bh(list_lock); |
| 122 | hlist_add_head_rcu(&bucket->hlist, head); |
| 123 | spin_unlock_bh(list_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 124 | |
| 125 | return 0; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 126 | |
| 127 | err_unlock: |
| 128 | rcu_read_unlock(); |
| 129 | err: |
| 130 | return -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* removes data from hash, if found. returns pointer do data on success, so you |
| 134 | * can remove the used structure yourself, or NULL on error . data could be the |
| 135 | * structure you use with just the key filled, we just need the key for |
| 136 | * comparing. */ |
| 137 | static inline void *hash_remove(struct hashtable_t *hash, |
| 138 | hashdata_compare_cb compare, |
| 139 | hashdata_choose_cb choose, void *data) |
| 140 | { |
| 141 | size_t index; |
| 142 | struct hlist_node *walk; |
| 143 | struct element_t *bucket; |
| 144 | struct hlist_head *head; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 145 | void *data_save = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 146 | |
| 147 | index = choose(data, hash->size); |
| 148 | head = &hash->table[index]; |
| 149 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 150 | spin_lock_bh(&hash->list_locks[index]); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 151 | hlist_for_each_entry(bucket, walk, head, hlist) { |
| 152 | if (compare(bucket->data, data)) { |
| 153 | data_save = bucket->data; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 154 | hlist_del_rcu(walk); |
| 155 | call_rcu(&bucket->rcu, bucket_free_rcu); |
| 156 | break; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 159 | spin_unlock_bh(&hash->list_locks[index]); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 160 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 161 | return data_save; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 164 | /** |
| 165 | * finds data, based on the key in keydata. returns the found data on success, |
| 166 | * or NULL on error |
| 167 | * |
| 168 | * caller must lock with rcu_read_lock() / rcu_read_unlock() |
| 169 | **/ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 170 | static inline void *hash_find(struct hashtable_t *hash, |
| 171 | hashdata_compare_cb compare, |
| 172 | hashdata_choose_cb choose, void *keydata) |
| 173 | { |
| 174 | int index; |
| 175 | struct hlist_head *head; |
| 176 | struct hlist_node *walk; |
| 177 | struct element_t *bucket; |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 178 | void *bucket_data = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 179 | |
| 180 | if (!hash) |
| 181 | return NULL; |
| 182 | |
| 183 | index = choose(keydata , hash->size); |
| 184 | head = &hash->table[index]; |
| 185 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 186 | hlist_for_each_entry(bucket, walk, head, hlist) { |
| 187 | if (compare(bucket->data, keydata)) { |
| 188 | bucket_data = bucket->data; |
| 189 | break; |
| 190 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame^] | 193 | return bucket_data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | #endif /* _NET_BATMAN_ADV_HASH_H_ */ |