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 Lei Xia <lxia@northwestern.edu> */
36 #ifndef __VNET_HASHTABLE_H__
37 #define __VNET_HASHTABLE_H__
43 * struct hashtable *h;
45 * struct some_value *v;
47 * static uint_t hash_from_key_fn( void *k );
48 * static int keys_equal_fn ( void *key1, void *key2 );
50 * h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
51 * k = (struct some_key *) malloc(sizeof(struct some_key));
52 * v = (struct some_value *) malloc(sizeof(struct some_value));
54 * (initialise k and v to suitable values)
56 * if (! hashtable_insert(h,k,v) )
59 * if (NULL == (found = hashtable_search(h,k) ))
60 * { printf("not found!"); }
62 * if (NULL == (found = hashtable_remove(h,k) ))
63 * { printf("Not found\n"); }
67 /* Macros may be used to define type-safe(r) hashtable access functions, with
68 * methods specialized to take known key and value types as parameters.
72 * Insert this at the start of your file:
74 * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
75 * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
76 * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
78 * This defines the functions 'insert_some', 'search_some' and 'remove_some'.
79 * These operate just like hashtable_insert etc., with the same parameters,
80 * but their function signatures have 'struct some_key *' rather than
81 * 'void *', and hence can generate compile time errors if your program is
82 * supplying incorrect data as a key (and similarly for value).
84 * Note that the hash and key equality functions passed to create_hashtable
85 * still take 'void *' parameters instead of 'some key *'. This shouldn't be
86 * a difficult issue as they're only defined and passed once, and the other
87 * functions will ensure that only valid keys are supplied to them.
89 * The cost for this checking is increased code size and runtime overhead
90 * - if performance is important, it may be worth switching back to the
91 * unsafe methods once your program has been debugged with the safe methods.
92 * This just requires switching to some simple alternative defines - eg:
93 * #define insert_some hashtable_insert
97 /* These cannot be inlined because they are referenced as fn ptrs */
98 ulong_t vnet_hash_long(ulong_t val, uint_t bits);
99 ulong_t vnet_hash_buffer(uchar_t * msg, uint_t length);
101 struct hashtable * vnet_create_htable(uint_t min_size,
102 uint_t (*hashfunction) (addr_t key),
103 int (*key_eq_fn) (addr_t key1, addr_t key2));
105 void vnet_free_htable(struct hashtable * htable, int free_values, int free_keys);
108 * returns non-zero for successful insertion
110 * This function will cause the table to expand if the insertion would take
111 * the ratio of entries to table size over the maximum load factor.
113 * This function does not check for repeated insertions with a duplicate key.
114 * The value returned when using a duplicate key is undefined -- when
115 * the hashtable changes size, the order of retrieval of duplicate key
116 * entries is reversed.
117 * If in doubt, remove before insert.
119 int vnet_htable_insert(struct hashtable * htable, addr_t key, addr_t value);
121 // returns the value associated with the key, or NULL if none found
122 addr_t vnet_htable_search(struct hashtable * htable, addr_t key);
124 // returns the value associated with the key, or NULL if none found
125 addr_t vnet_htable_remove(struct hashtable * htable, addr_t key, int free_key);
127 uint_t vnet_htable_count(struct hashtable * htable);
170 * Copyright (c) 2002, Christopher Clark
171 * All rights reserved.
173 * Redistribution and use in source and binary forms, with or without
174 * modification, are permitted provided that the following conditions
177 * * Redistributions of source code must retain the above copyright
178 * notice, this list of conditions and the following disclaimer.
180 * * Redistributions in binary form must reproduce the above copyright
181 * notice, this list of conditions and the following disclaimer in the
182 * documentation and/or other materials provided with the distribution.
184 * * Neither the name of the original author; nor the names of any contributors
185 * may be used to endorse or promote products derived from this software
186 * without specific prior written permission.
189 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
190 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
193 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
194 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
195 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
196 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
197 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
198 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
199 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.