/*1* Copyright (C) 2006-2011 B.A.T.M.A.N. contributors:2*3* Simon Wunderlich, Marek Lindner4*5* This program is free software; you can redistribute it and/or6* modify it under the terms of version 2 of the GNU General Public7* License as published by the Free Software Foundation.8*9* This program is distributed in the hope that it will be useful, but10* WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* General Public License for more details.13*14* You should have received a copy of the GNU General Public License15* along with this program; if not, write to the Free Software16* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA17* 02110-1301, USA18*19*/2021#ifndef _NET_BATMAN_ADV_HASH_H_22#define _NET_BATMAN_ADV_HASH_H_2324#include <linux/list.h>2526/* callback to a compare function. should27* compare 2 element datas for their keys,28* return 0 if same and not 0 if not29* same */30typedef int (*hashdata_compare_cb)(struct hlist_node *, void *);3132/* the hashfunction, should return an index33* based on the key in the data of the first34* argument and the size the second */35typedef int (*hashdata_choose_cb)(void *, int);36typedef void (*hashdata_free_cb)(struct hlist_node *, void *);3738struct hashtable_t {39struct hlist_head *table; /* the hashtable itself with the buckets */40spinlock_t *list_locks; /* spinlock for each hash list entry */41int size; /* size of hashtable */42};4344/* allocates and clears the hash */45struct hashtable_t *hash_new(int size);4647/* free only the hashtable and the hash itself. */48void hash_destroy(struct hashtable_t *hash);4950/* remove the hash structure. if hashdata_free_cb != NULL, this function will be51* called to remove the elements inside of the hash. if you don't remove the52* elements, memory might be leaked. */53static inline void hash_delete(struct hashtable_t *hash,54hashdata_free_cb free_cb, void *arg)55{56struct hlist_head *head;57struct hlist_node *node, *node_tmp;58spinlock_t *list_lock; /* spinlock to protect write access */59int i;6061for (i = 0; i < hash->size; i++) {62head = &hash->table[i];63list_lock = &hash->list_locks[i];6465spin_lock_bh(list_lock);66hlist_for_each_safe(node, node_tmp, head) {67hlist_del_rcu(node);6869if (free_cb)70free_cb(node, arg);71}72spin_unlock_bh(list_lock);73}7475hash_destroy(hash);76}7778/* adds data to the hashtable. returns 0 on success, -1 on error */79static inline int hash_add(struct hashtable_t *hash,80hashdata_compare_cb compare,81hashdata_choose_cb choose,82void *data, struct hlist_node *data_node)83{84int index;85struct hlist_head *head;86struct hlist_node *node;87spinlock_t *list_lock; /* spinlock to protect write access */8889if (!hash)90goto err;9192index = choose(data, hash->size);93head = &hash->table[index];94list_lock = &hash->list_locks[index];9596rcu_read_lock();97__hlist_for_each_rcu(node, head) {98if (!compare(node, data))99continue;100101goto err_unlock;102}103rcu_read_unlock();104105/* no duplicate found in list, add new element */106spin_lock_bh(list_lock);107hlist_add_head_rcu(data_node, head);108spin_unlock_bh(list_lock);109110return 0;111112err_unlock:113rcu_read_unlock();114err:115return -1;116}117118/* removes data from hash, if found. returns pointer do data on success, so you119* can remove the used structure yourself, or NULL on error . data could be the120* structure you use with just the key filled, we just need the key for121* comparing. */122static inline void *hash_remove(struct hashtable_t *hash,123hashdata_compare_cb compare,124hashdata_choose_cb choose, void *data)125{126size_t index;127struct hlist_node *node;128struct hlist_head *head;129void *data_save = NULL;130131index = choose(data, hash->size);132head = &hash->table[index];133134spin_lock_bh(&hash->list_locks[index]);135hlist_for_each(node, head) {136if (!compare(node, data))137continue;138139data_save = node;140hlist_del_rcu(node);141break;142}143spin_unlock_bh(&hash->list_locks[index]);144145return data_save;146}147148#endif /* _NET_BATMAN_ADV_HASH_H_ */149150151