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