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
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
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 *);
40 void *data
; /* pointer to the data */
41 struct hlist_node hlist
; /* bucket list pointer */
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
;
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
);
77 free_cb(bucket
->data
, arg
);
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
)
93 struct hlist_head
*head
;
94 struct hlist_node
*walk
, *safe
;
95 struct element_t
*bucket
;
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
))
109 /* no duplicate found in list, add new element */
110 bucket
= kmalloc(sizeof(struct element_t
), GFP_ATOMIC
);
116 hlist_add_head(&bucket
->hlist
, head
);
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
125 static inline void *hash_remove(struct hashtable_t
*hash
,
126 hashdata_compare_cb compare
,
127 hashdata_choose_cb choose
, void *data
)
130 struct hlist_node
*walk
;
131 struct element_t
*bucket
;
132 struct hlist_head
*head
;
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
;
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
)
157 struct hlist_head
*head
;
158 struct hlist_node
*walk
;
159 struct element_t
*bucket
;
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
))
176 #endif /* _NET_BATMAN_ADV_HASH_H_ */