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.
34 /* Modifications made by Lei Xia */
36 #ifndef __PALACIOS_HASHTABLE_H__
37 #define __PALACIOS_HASHTABLE_H__
45 * struct hashtable *h;
47 * struct some_value *v;
49 * static uint_t hash_from_key_fn( void *k );
50 * static int keys_equal_fn ( void *key1, void *key2 );
52 * h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
53 * k = (struct some_key *) malloc(sizeof(struct some_key));
54 * v = (struct some_value *) malloc(sizeof(struct some_value));
56 * (initialise k and v to suitable values)
58 * if (! hashtable_insert(h,k,v) )
61 * if (NULL == (found = hashtable_search(h,k) ))
62 * { printf("not found!"); }
64 * if (NULL == (found = hashtable_remove(h,k) ))
65 * { printf("Not found\n"); }
69 /* Macros may be used to define type-safe(r) hashtable access functions, with
70 * methods specialized to take known key and value types as parameters.
74 * Insert this at the start of your file:
76 * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
77 * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
78 * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
80 * This defines the functions 'insert_some', 'search_some' and 'remove_some'.
81 * These operate just like hashtable_insert etc., with the same parameters,
82 * but their function signatures have 'struct some_key *' rather than
83 * 'void *', and hence can generate compile time errors if your program is
84 * supplying incorrect data as a key (and similarly for value).
86 * Note that the hash and key equality functions passed to create_hashtable
87 * still take 'void *' parameters instead of 'some key *'. This shouldn't be
88 * a difficult issue as they're only defined and passed once, and the other
89 * functions will ensure that only valid keys are supplied to them.
91 * The cost for this checking is increased code size and runtime overhead
92 * - if performance is important, it may be worth switching back to the
93 * unsafe methods once your program has been debugged with the safe methods.
94 * This just requires switching to some simple alternative defines - eg:
95 * #define insert_some hashtable_insert
99 typedef unsigned char uchar_t;
100 typedef unsigned int uint_t;
101 typedef unsigned long long ullong_t;
102 typedef unsigned long ulong_t;
103 typedef ulong_t addr_t;
106 #define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
107 static int fnname (struct hashtable * htable, keytype key, valuetype value) { \
108 return v3_htable_insert(htable, (addr_t)key, (addr_t)value); \
111 #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
112 static valuetype * fnname (struct hashtable * htable, keytype key) { \
113 return (valuetype *) (v3_htable_search(htable, (addr_t)key)); \
116 #define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype, free_key) \
117 static valuetype * fnname (struct hashtable * htable, keytype key) { \
118 return (valuetype *) (v3_htable_remove(htable, (addr_t)key, free_key)); \
125 /* These cannot be inlined because they are referenced as fn ptrs */
126 ulong_t palacios_hash_long(ulong_t val, uint_t bits);
127 ulong_t palacios_hash_buffer(uchar_t * msg, uint_t length);
131 struct hashtable * palacios_create_htable(uint_t min_size,
132 uint_t (*hashfunction) (addr_t key),
133 int (*key_eq_fn) (addr_t key1, addr_t key2));
135 void palacios_free_htable(struct hashtable * htable, int free_values, int free_keys);
138 * returns non-zero for successful insertion
140 * This function will cause the table to expand if the insertion would take
141 * the ratio of entries to table size over the maximum load factor.
143 * This function does not check for repeated insertions with a duplicate key.
144 * The value returned when using a duplicate key is undefined -- when
145 * the hashtable changes size, the order of retrieval of duplicate key
146 * entries is reversed.
147 * If in doubt, remove before insert.
149 int palacios_htable_insert(struct hashtable * htable, addr_t key, addr_t value);
151 int palacios_htable_change(struct hashtable * htable, addr_t key, addr_t value, int free_value);
154 // returns the value associated with the key, or NULL if none found
155 addr_t palacios_htable_search(struct hashtable * htable, addr_t key);
157 // returns the value associated with the key, or NULL if none found
158 addr_t palacios_htable_remove(struct hashtable * htable, addr_t key, int free_key);
160 uint_t palacios_htable_count(struct hashtable * htable);
162 // Specialty functions for a counting hashtable
163 int palacios_htable_inc(struct hashtable * htable, addr_t key, addr_t value);
164 int palacios_htable_dec(struct hashtable * htable, addr_t key, addr_t value);