staging: batman-adv meshing protocol
[safe/jmp/linux-2.6] / drivers / staging / batman-adv / hash.h
1 /*
2  * Copyright (C) 2006-2009 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 _BATMAN_HASH_H
23 #define _BATMAN_HASH_H
24
25 typedef int (*hashdata_compare_cb)(void *, void *);
26 typedef int (*hashdata_choose_cb)(void *, int);
27 typedef void (*hashdata_free_cb)(void *);
28
29 struct element_t {
30         void *data;             /* pointer to the data */
31         struct element_t *next; /* overflow bucket pointer */
32 };
33
34 struct hash_it_t {
35         int index;
36         struct element_t *bucket;
37         struct element_t *prev_bucket;
38         struct element_t **first_bucket;
39 };
40
41 struct hashtable_t {
42         struct element_t **table;   /* the hashtable itself, with the buckets */
43         int elements;               /* number of elements registered */
44         int size;                   /* size of hashtable */
45         hashdata_compare_cb compare;/* callback to a compare function.  should
46                                      * compare 2 element datas for their keys,
47                                      * return 0 if same and not 0 if not
48                                      * same */
49         hashdata_choose_cb choose;  /* the hashfunction, should return an index
50                                      * based on the key in the data of the first
51                                      * argument and the size the second */
52 };
53
54 /* clears the hash */
55 void hash_init(struct hashtable_t *hash);
56
57 /* allocates and clears the hash */
58 struct hashtable_t *hash_new(int size, hashdata_compare_cb compare,
59                              hashdata_choose_cb choose);
60
61 /* remove bucket (this might be used in hash_iterate() if you already found the
62  * bucket you want to delete and don't need the overhead to find it again with
63  * hash_remove().  But usually, you don't want to use this function, as it
64  * fiddles with hash-internals. */
65 void *hash_remove_bucket(struct hashtable_t *hash, struct hash_it_t *hash_it_t);
66
67 /* remove the hash structure. if hashdata_free_cb != NULL, this function will be
68  * called to remove the elements inside of the hash.  if you don't remove the
69  * elements, memory might be leaked. */
70 void hash_delete(struct hashtable_t *hash, hashdata_free_cb free_cb);
71
72 /* free only the hashtable and the hash itself. */
73 void hash_destroy(struct hashtable_t *hash);
74
75 /* adds data to the hashtable. returns 0 on success, -1 on error */
76 int hash_add(struct hashtable_t *hash, void *data);
77
78 /* removes data from hash, if found. returns pointer do data on success, so you
79  * can remove the used structure yourself, or NULL on error .  data could be the
80  * structure you use with just the key filled, we just need the key for
81  * comparing. */
82 void *hash_remove(struct hashtable_t *hash, void *data);
83
84 /* finds data, based on the key in keydata. returns the found data on success,
85  * or NULL on error */
86 void *hash_find(struct hashtable_t *hash, void *keydata);
87
88 /* resize the hash, returns the pointer to the new hash or NULL on
89  * error. removes the old hash on success */
90 struct hashtable_t *hash_resize(struct hashtable_t *hash, int size);
91
92 /* iterate though the hash. first element is selected with iter_in NULL.  use
93  * the returned iterator to access the elements until hash_it_t returns NULL. */
94 struct hash_it_t *hash_iterate(struct hashtable_t *hash,
95                                struct hash_it_t *iter_in);
96
97 /* print the hash table for debugging */
98 void hash_debug(struct hashtable_t *hash);
99 #endif