blob: 09216ade16f1a92a8f907af1192deafc902f03d9 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
2 * Copyright (C) 2006-2010 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
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 */
31typedef 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 */
36typedef int (*hashdata_choose_cb)(void *, int);
37typedef void (*hashdata_free_cb)(void *, void *);
38
39struct element_t {
40 void *data; /* pointer to the data */
41 struct hlist_node hlist; /* bucket list pointer */
42};
43
44struct hashtable_t {
45 struct hlist_head *table; /* the hashtable itself, with the buckets */
46 int size; /* size of hashtable */
47};
48
49/* allocates and clears the hash */
50struct hashtable_t *hash_new(int size);
51
52/* remove element if you already found the element you want to delete and don't
53 * need the overhead to find it again with hash_remove(). But usually, you
54 * don't want to use this function, as it fiddles with hash-internals. */
55void *hash_remove_element(struct hashtable_t *hash, struct element_t *elem);
56
57/* free only the hashtable and the hash itself. */
58void hash_destroy(struct hashtable_t *hash);
59
60/* remove the hash structure. if hashdata_free_cb != NULL, this function will be
61 * called to remove the elements inside of the hash. if you don't remove the
62 * elements, memory might be leaked. */
63static inline void hash_delete(struct hashtable_t *hash,
64 hashdata_free_cb free_cb, void *arg)
65{
66 struct hlist_head *head;
67 struct hlist_node *walk, *safe;
68 struct element_t *bucket;
69 int i;
70
71 for (i = 0; i < hash->size; i++) {
72 head = &hash->table[i];
73
74 hlist_for_each_safe(walk, safe, head) {
75 bucket = hlist_entry(walk, struct element_t, hlist);
76 if (free_cb)
77 free_cb(bucket->data, arg);
78
79 hlist_del(walk);
80 kfree(bucket);
81 }
82 }
83
84 hash_destroy(hash);
85}
86
87/* adds data to the hashtable. returns 0 on success, -1 on error */
88static inline int hash_add(struct hashtable_t *hash,
89 hashdata_compare_cb compare,
90 hashdata_choose_cb choose, void *data)
91{
92 int index;
93 struct hlist_head *head;
94 struct hlist_node *walk, *safe;
95 struct element_t *bucket;
96
97 if (!hash)
98 return -1;
99
100 index = choose(data, hash->size);
101 head = &hash->table[index];
102
103 hlist_for_each_safe(walk, safe, head) {
104 bucket = hlist_entry(walk, struct element_t, hlist);
105 if (compare(bucket->data, data))
106 return -1;
107 }
108
109 /* no duplicate found in list, add new element */
110 bucket = kmalloc(sizeof(struct element_t), GFP_ATOMIC);
111
112 if (!bucket)
113 return -1;
114
115 bucket->data = data;
116 hlist_add_head(&bucket->hlist, head);
117
118 return 0;
119}
120
121/* removes data from hash, if found. returns pointer do data on success, so you
122 * can remove the used structure yourself, or NULL on error . data could be the
123 * structure you use with just the key filled, we just need the key for
124 * comparing. */
125static inline void *hash_remove(struct hashtable_t *hash,
126 hashdata_compare_cb compare,
127 hashdata_choose_cb choose, void *data)
128{
129 size_t index;
130 struct hlist_node *walk;
131 struct element_t *bucket;
132 struct hlist_head *head;
133 void *data_save;
134
135 index = choose(data, hash->size);
136 head = &hash->table[index];
137
138 hlist_for_each_entry(bucket, walk, head, hlist) {
139 if (compare(bucket->data, data)) {
140 data_save = bucket->data;
141 hlist_del(walk);
142 kfree(bucket);
143 return data_save;
144 }
145 }
146
147 return NULL;
148}
149
150/* finds data, based on the key in keydata. returns the found data on success,
151 * or NULL on error */
152static inline void *hash_find(struct hashtable_t *hash,
153 hashdata_compare_cb compare,
154 hashdata_choose_cb choose, void *keydata)
155{
156 int index;
157 struct hlist_head *head;
158 struct hlist_node *walk;
159 struct element_t *bucket;
160
161 if (!hash)
162 return NULL;
163
164 index = choose(keydata , hash->size);
165 head = &hash->table[index];
166
167 hlist_for_each(walk, head) {
168 bucket = hlist_entry(walk, struct element_t, hlist);
169 if (compare(bucket->data, keydata))
170 return bucket->data;
171 }
172
173 return NULL;
174}
175
176#endif /* _NET_BATMAN_ADV_HASH_H_ */