hash.h (1a8eaf0733ca754533a03d6cfa4463def2b81ce3) hash.h (9cfc7bd608b97463993b4f3e4775d99022253f8d)
1/*
2 * Copyright (C) 2006-2012 B.A.T.M.A.N. contributors:
1/* Copyright (C) 2006-2012 B.A.T.M.A.N. contributors:
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
2 *
3 * Simon Wunderlich, Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 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
18 */
19
20#ifndef _NET_BATMAN_ADV_HASH_H_
21#define _NET_BATMAN_ADV_HASH_H_
22
23#include <linux/list.h>
24
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 */
25/* callback to a compare function. should compare 2 element datas for their
26 * keys, return 0 if same and not 0 if not same
27 */
31typedef int (*hashdata_compare_cb)(const struct hlist_node *, const void *);
32
33/* the hashfunction, should return an index
34 * based on the key in the data of the first
28typedef int (*hashdata_compare_cb)(const struct hlist_node *, const void *);
29
30/* the hashfunction, should return an index
31 * based on the key in the data of the first
35 * argument and the size the second */
32 * argument and the size the second
33 */
36typedef uint32_t (*hashdata_choose_cb)(const void *, uint32_t);
37typedef void (*hashdata_free_cb)(struct hlist_node *, void *);
38
39struct hashtable_t {
40 struct hlist_head *table; /* the hashtable itself with the buckets */
41 spinlock_t *list_locks; /* spinlock for each hash list entry */
42 uint32_t size; /* size of hashtable */
43};

--- 5 unchanged lines hidden (view full) ---

49void batadv_hash_set_lock_class(struct hashtable_t *hash,
50 struct lock_class_key *key);
51
52/* free only the hashtable and the hash itself. */
53void batadv_hash_destroy(struct hashtable_t *hash);
54
55/* remove the hash structure. if hashdata_free_cb != NULL, this function will be
56 * called to remove the elements inside of the hash. if you don't remove the
34typedef uint32_t (*hashdata_choose_cb)(const void *, uint32_t);
35typedef void (*hashdata_free_cb)(struct hlist_node *, void *);
36
37struct hashtable_t {
38 struct hlist_head *table; /* the hashtable itself with the buckets */
39 spinlock_t *list_locks; /* spinlock for each hash list entry */
40 uint32_t size; /* size of hashtable */
41};

--- 5 unchanged lines hidden (view full) ---

47void batadv_hash_set_lock_class(struct hashtable_t *hash,
48 struct lock_class_key *key);
49
50/* free only the hashtable and the hash itself. */
51void batadv_hash_destroy(struct hashtable_t *hash);
52
53/* remove the hash structure. if hashdata_free_cb != NULL, this function will be
54 * called to remove the elements inside of the hash. if you don't remove the
57 * elements, memory might be leaked. */
55 * elements, memory might be leaked.
56 */
58static inline void hash_delete(struct hashtable_t *hash,
59 hashdata_free_cb free_cb, void *arg)
60{
61 struct hlist_head *head;
62 struct hlist_node *node, *node_tmp;
63 spinlock_t *list_lock; /* spinlock to protect write access */
64 uint32_t i;
65

--- 9 unchanged lines hidden (view full) ---

75 free_cb(node, arg);
76 }
77 spin_unlock_bh(list_lock);
78 }
79
80 batadv_hash_destroy(hash);
81}
82
57static inline void hash_delete(struct hashtable_t *hash,
58 hashdata_free_cb free_cb, void *arg)
59{
60 struct hlist_head *head;
61 struct hlist_node *node, *node_tmp;
62 spinlock_t *list_lock; /* spinlock to protect write access */
63 uint32_t i;
64

--- 9 unchanged lines hidden (view full) ---

74 free_cb(node, arg);
75 }
76 spin_unlock_bh(list_lock);
77 }
78
79 batadv_hash_destroy(hash);
80}
81
83/**
84 * hash_add - adds data to the hashtable
82/* hash_add - adds data to the hashtable
85 * @hash: storage hash table
86 * @compare: callback to determine if 2 hash elements are identical
87 * @choose: callback calculating the hash index
88 * @data: data passed to the aforementioned callbacks as argument
89 * @data_node: to be added element
90 *
91 * Returns 0 on success, 1 if the element already is in the hash
92 * and -1 on error.
93 */
83 * @hash: storage hash table
84 * @compare: callback to determine if 2 hash elements are identical
85 * @choose: callback calculating the hash index
86 * @data: data passed to the aforementioned callbacks as argument
87 * @data_node: to be added element
88 *
89 * Returns 0 on success, 1 if the element already is in the hash
90 * and -1 on error.
91 */
94
95static inline int hash_add(struct hashtable_t *hash,
96 hashdata_compare_cb compare,
97 hashdata_choose_cb choose,
98 const void *data, struct hlist_node *data_node)
99{
100 uint32_t index;
101 int ret = -1;
102 struct hlist_head *head;

--- 26 unchanged lines hidden (view full) ---

129 spin_unlock_bh(list_lock);
130out:
131 return ret;
132}
133
134/* removes data from hash, if found. returns pointer do data on success, so you
135 * can remove the used structure yourself, or NULL on error . data could be the
136 * structure you use with just the key filled, we just need the key for
92static inline int hash_add(struct hashtable_t *hash,
93 hashdata_compare_cb compare,
94 hashdata_choose_cb choose,
95 const void *data, struct hlist_node *data_node)
96{
97 uint32_t index;
98 int ret = -1;
99 struct hlist_head *head;

--- 26 unchanged lines hidden (view full) ---

126 spin_unlock_bh(list_lock);
127out:
128 return ret;
129}
130
131/* removes data from hash, if found. returns pointer do data on success, so you
132 * can remove the used structure yourself, or NULL on error . data could be the
133 * structure you use with just the key filled, we just need the key for
137 * comparing. */
134 * comparing.
135 */
138static inline void *hash_remove(struct hashtable_t *hash,
139 hashdata_compare_cb compare,
140 hashdata_choose_cb choose, void *data)
141{
142 uint32_t index;
143 struct hlist_node *node;
144 struct hlist_head *head;
145 void *data_save = NULL;

--- 19 unchanged lines hidden ---
136static inline void *hash_remove(struct hashtable_t *hash,
137 hashdata_compare_cb compare,
138 hashdata_choose_cb choose, void *data)
139{
140 uint32_t index;
141 struct hlist_node *node;
142 struct hlist_head *head;
143 void *data_save = NULL;

--- 19 unchanged lines hidden ---