Merge branch 'omap-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / batman-adv / hash.h
blob09216ade16f1a92a8f907af1192deafc902f03d9
1 /*
2 * Copyright (C) 2006-2010 B.A.T.M.A.N. contributors:
4 * Simon Wunderlich, Marek Lindner
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.
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.
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
22 #ifndef _NET_BATMAN_ADV_HASH_H_
23 #define _NET_BATMAN_ADV_HASH_H_
25 #include <linux/list.h>
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 *);
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 *);
39 struct element_t {
40 void *data; /* pointer to the data */
41 struct hlist_node hlist; /* bucket list pointer */
44 struct hashtable_t {
45 struct hlist_head *table; /* the hashtable itself, with the buckets */
46 int size; /* size of hashtable */
49 /* allocates and clears the hash */
50 struct hashtable_t *hash_new(int size);
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. */
55 void *hash_remove_element(struct hashtable_t *hash, struct element_t *elem);
57 /* free only the hashtable and the hash itself. */
58 void hash_destroy(struct hashtable_t *hash);
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. */
63 static inline void hash_delete(struct hashtable_t *hash,
64 hashdata_free_cb free_cb, void *arg)
66 struct hlist_head *head;
67 struct hlist_node *walk, *safe;
68 struct element_t *bucket;
69 int i;
71 for (i = 0; i < hash->size; i++) {
72 head = &hash->table[i];
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);
79 hlist_del(walk);
80 kfree(bucket);
84 hash_destroy(hash);
87 /* adds data to the hashtable. returns 0 on success, -1 on error */
88 static inline int hash_add(struct hashtable_t *hash,
89 hashdata_compare_cb compare,
90 hashdata_choose_cb choose, void *data)
92 int index;
93 struct hlist_head *head;
94 struct hlist_node *walk, *safe;
95 struct element_t *bucket;
97 if (!hash)
98 return -1;
100 index = choose(data, hash->size);
101 head = &hash->table[index];
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;
109 /* no duplicate found in list, add new element */
110 bucket = kmalloc(sizeof(struct element_t), GFP_ATOMIC);
112 if (!bucket)
113 return -1;
115 bucket->data = data;
116 hlist_add_head(&bucket->hlist, head);
118 return 0;
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. */
125 static inline void *hash_remove(struct hashtable_t *hash,
126 hashdata_compare_cb compare,
127 hashdata_choose_cb choose, void *data)
129 size_t index;
130 struct hlist_node *walk;
131 struct element_t *bucket;
132 struct hlist_head *head;
133 void *data_save;
135 index = choose(data, hash->size);
136 head = &hash->table[index];
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;
147 return NULL;
150 /* finds data, based on the key in keydata. returns the found data on success,
151 * or NULL on error */
152 static inline void *hash_find(struct hashtable_t *hash,
153 hashdata_compare_cb compare,
154 hashdata_choose_cb choose, void *keydata)
156 int index;
157 struct hlist_head *head;
158 struct hlist_node *walk;
159 struct element_t *bucket;
161 if (!hash)
162 return NULL;
164 index = choose(keydata , hash->size);
165 head = &hash->table[index];
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;
173 return NULL;
176 #endif /* _NET_BATMAN_ADV_HASH_H_ */