2 Copyright (c) 2002, 2004, Christopher Clark
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 * Neither the name of the original author; nor the names of any contributors
17 may be used to endorse or promote products derived from this software
18 without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* Modifications made by Jack Lange <jarusl@cs.northwestern.edu> */
37 #ifndef __VMM_HASHTABLE_H__
38 #define __VMM_HASHTABLE_H__
46 * struct hashtable *h;
48 * struct some_value *v;
50 * static uint_t hash_from_key_fn( void *k );
51 * static int keys_equal_fn ( void *key1, void *key2 );
53 * h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
54 * k = (struct some_key *) malloc(sizeof(struct some_key));
55 * v = (struct some_value *) malloc(sizeof(struct some_value));
57 * (initialise k and v to suitable values)
59 * if (! hashtable_insert(h,k,v) )
62 * if (NULL == (found = hashtable_search(h,k) ))
63 * { printf("not found!"); }
65 * if (NULL == (found = hashtable_remove(h,k) ))
66 * { printf("Not found\n"); }
70 /* Macros may be used to define type-safe(r) hashtable access functions, with
71 * methods specialized to take known key and value types as parameters.
75 * Insert this at the start of your file:
77 * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
78 * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
79 * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
81 * This defines the functions 'insert_some', 'search_some' and 'remove_some'.
82 * These operate just like hashtable_insert etc., with the same parameters,
83 * but their function signatures have 'struct some_key *' rather than
84 * 'void *', and hence can generate compile time errors if your program is
85 * supplying incorrect data as a key (and similarly for value).
87 * Note that the hash and key equality functions passed to create_hashtable
88 * still take 'void *' parameters instead of 'some key *'. This shouldn't be
89 * a difficult issue as they're only defined and passed once, and the other
90 * functions will ensure that only valid keys are supplied to them.
92 * The cost for this checking is increased code size and runtime overhead
93 * - if performance is important, it may be worth switching back to the
94 * unsafe methods once your program has been debugged with the safe methods.
95 * This just requires switching to some simple alternative defines - eg:
96 * #define insert_some hashtable_insert
100 #define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
101 static int fnname (struct hashtable * htable, keytype key, valuetype value) { \
102 return v3_htable_insert(htable, (addr_t)key, (addr_t)value); \
105 #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
106 static valuetype * fnname (struct hashtable * htable, keytype key) { \
107 return (valuetype *) (v3_htable_search(htable, (addr_t)key)); \
110 #define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype, free_key) \
111 static valuetype * fnname (struct hashtable * htable, keytype key) { \
112 return (valuetype *) (v3_htable_remove(htable, (addr_t)key, free_key)); \
119 /* These cannot be inlined because they are referenced as fn ptrs */
120 ulong_t v3_hash_long(ulong_t val, uint_t bits);
121 ulong_t v3_hash_buffer(uchar_t * msg, uint_t length);
125 struct hashtable * v3_create_htable(uint_t min_size,
126 uint_t (*hashfunction) (addr_t key),
127 int (*key_eq_fn) (addr_t key1, addr_t key2));
129 void v3_free_htable(struct hashtable * htable, int free_values, int free_keys);
132 * returns non-zero for successful insertion
134 * This function will cause the table to expand if the insertion would take
135 * the ratio of entries to table size over the maximum load factor.
137 * This function does not check for repeated insertions with a duplicate key.
138 * The value returned when using a duplicate key is undefined -- when
139 * the hashtable changes size, the order of retrieval of duplicate key
140 * entries is reversed.
141 * If in doubt, remove before insert.
143 int v3_htable_insert(struct hashtable * htable, addr_t key, addr_t value);
145 int v3_htable_change(struct hashtable * htable, addr_t key, addr_t value, int free_value);
148 // returns the value associated with the key, or NULL if none found
149 addr_t v3_htable_search(struct hashtable * htable, addr_t key);
151 // returns the value associated with the key, or NULL if none found
152 addr_t v3_htable_remove(struct hashtable * htable, addr_t key, int free_key);
154 uint_t v3_htable_count(struct hashtable * htable);
156 // Specialty functions for a counting hashtable
157 int v3_htable_inc(struct hashtable * htable, addr_t key, addr_t value);
158 int v3_htable_dec(struct hashtable * htable, addr_t key, addr_t value);
165 #define DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \
166 static int fnname (struct hashtable_itr * iter, struct hashtable * htable, keytype * key) { \
167 return (v3_htable_iter_search(iter, htable, key)); \
172 /*****************************************************************************/
173 /* This struct is only concrete here to allow the inlining of two of the
174 * accessor functions. */
175 struct hashtable_iter {
176 struct hashtable * htable;
177 struct hash_entry * entry;
178 struct hash_entry * parent;
183 struct hashtable_iter * v3_create_htable_iter(struct hashtable * htable);
185 /* - return the value of the (key,value) pair at the current position */
187 addr_t v3_htable_get_iter_key(struct hashtable_iter * iter);
189 return iter->entry->key;
194 /* value - return the value of the (key,value) pair at the current position */
196 addr_t v3_htable_get_iter_value(struct hashtable_iter * iter);
198 return iter->entry->value;
204 /* returns zero if advanced to end of table */
205 int v3_htable_iter_advance(struct hashtable_iter * iter);
207 /* remove current element and advance the iterator to the next element
208 * NB: if you need the value to free it, read it before
209 * removing. ie: beware memory leaks!
210 * returns zero if advanced to end of table
212 int v3_htable_iter_remove(struct hashtable_iter * iter, int free_key);
215 /* search - overwrite the supplied iterator, to point to the entry
216 * matching the supplied key.
217 * returns zero if not found. */
218 int v3_htable_iter_search(struct hashtable_iter * iter, struct hashtable * htable, addr_t key);
223 #endif // ! __V3VEE__
226 #endif /* __VMM_HASHTABLE_H__ */
266 * Copyright (c) 2002, Christopher Clark
267 * All rights reserved.
269 * Redistribution and use in source and binary forms, with or without
270 * modification, are permitted provided that the following conditions
273 * * Redistributions of source code must retain the above copyright
274 * notice, this list of conditions and the following disclaimer.
276 * * Redistributions in binary form must reproduce the above copyright
277 * notice, this list of conditions and the following disclaimer in the
278 * documentation and/or other materials provided with the distribution.
280 * * Neither the name of the original author; nor the names of any contributors
281 * may be used to endorse or promote products derived from this software
282 * without specific prior written permission.
285 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
286 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
287 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
288 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
289 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
290 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
291 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
292 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
293 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
294 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
295 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.