Commit | Line | Data |
---|---|---|
c6c8fea2 | 1 | /* |
64afe353 | 2 | * Copyright (C) 2006-2011 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
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 */ | |
42 | }; | |
43 | ||
44 | struct 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 */ | |
50 | struct hashtable_t *hash_new(int size); | |
51 | ||
c6c8fea2 SE |
52 | /* free only the hashtable and the hash itself. */ |
53 | void 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 | |
57 | * elements, memory might be leaked. */ | |
58 | static 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 *walk, *safe; | |
63 | struct element_t *bucket; | |
64 | int i; | |
65 | ||
66 | for (i = 0; i < hash->size; i++) { | |
67 | head = &hash->table[i]; | |
68 | ||
69 | hlist_for_each_safe(walk, safe, head) { | |
70 | bucket = hlist_entry(walk, struct element_t, hlist); | |
71 | if (free_cb) | |
72 | free_cb(bucket->data, arg); | |
73 | ||
74 | hlist_del(walk); | |
75 | kfree(bucket); | |
76 | } | |
77 | } | |
78 | ||
79 | hash_destroy(hash); | |
80 | } | |
81 | ||
82 | /* adds data to the hashtable. returns 0 on success, -1 on error */ | |
83 | static inline int hash_add(struct hashtable_t *hash, | |
84 | hashdata_compare_cb compare, | |
85 | hashdata_choose_cb choose, void *data) | |
86 | { | |
87 | int index; | |
88 | struct hlist_head *head; | |
89 | struct hlist_node *walk, *safe; | |
90 | struct element_t *bucket; | |
91 | ||
92 | if (!hash) | |
93 | return -1; | |
94 | ||
95 | index = choose(data, hash->size); | |
96 | head = &hash->table[index]; | |
97 | ||
98 | hlist_for_each_safe(walk, safe, head) { | |
99 | bucket = hlist_entry(walk, struct element_t, hlist); | |
100 | if (compare(bucket->data, data)) | |
101 | return -1; | |
102 | } | |
103 | ||
104 | /* no duplicate found in list, add new element */ | |
105 | bucket = kmalloc(sizeof(struct element_t), GFP_ATOMIC); | |
106 | ||
107 | if (!bucket) | |
108 | return -1; | |
109 | ||
110 | bucket->data = data; | |
111 | hlist_add_head(&bucket->hlist, head); | |
112 | ||
113 | return 0; | |
114 | } | |
115 | ||
116 | /* removes data from hash, if found. returns pointer do data on success, so you | |
117 | * can remove the used structure yourself, or NULL on error . data could be the | |
118 | * structure you use with just the key filled, we just need the key for | |
119 | * comparing. */ | |
120 | static inline void *hash_remove(struct hashtable_t *hash, | |
121 | hashdata_compare_cb compare, | |
122 | hashdata_choose_cb choose, void *data) | |
123 | { | |
124 | size_t index; | |
125 | struct hlist_node *walk; | |
126 | struct element_t *bucket; | |
127 | struct hlist_head *head; | |
128 | void *data_save; | |
129 | ||
130 | index = choose(data, hash->size); | |
131 | head = &hash->table[index]; | |
132 | ||
133 | hlist_for_each_entry(bucket, walk, head, hlist) { | |
134 | if (compare(bucket->data, data)) { | |
135 | data_save = bucket->data; | |
136 | hlist_del(walk); | |
137 | kfree(bucket); | |
138 | return data_save; | |
139 | } | |
140 | } | |
141 | ||
142 | return NULL; | |
143 | } | |
144 | ||
145 | /* finds data, based on the key in keydata. returns the found data on success, | |
146 | * or NULL on error */ | |
147 | static inline void *hash_find(struct hashtable_t *hash, | |
148 | hashdata_compare_cb compare, | |
149 | hashdata_choose_cb choose, void *keydata) | |
150 | { | |
151 | int index; | |
152 | struct hlist_head *head; | |
153 | struct hlist_node *walk; | |
154 | struct element_t *bucket; | |
155 | ||
156 | if (!hash) | |
157 | return NULL; | |
158 | ||
159 | index = choose(keydata , hash->size); | |
160 | head = &hash->table[index]; | |
161 | ||
162 | hlist_for_each(walk, head) { | |
163 | bucket = hlist_entry(walk, struct element_t, hlist); | |
164 | if (compare(bucket->data, keydata)) | |
165 | return bucket->data; | |
166 | } | |
167 | ||
168 | return NULL; | |
169 | } | |
170 | ||
171 | #endif /* _NET_BATMAN_ADV_HASH_H_ */ |