Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'Release-1.2' of ssh://palacios@newskysaw.cs.northwestern.edu//home...
[palacios.git] / palacios / include / palacios / vmm_hashtable.h
1 /*
2   Copyright (c) 2002, 2004, Christopher Clark
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8   
9   * Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11   
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.
15   
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.
19   
20   
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.
32 */
33 /* Modifications made by Jack Lange <jarusl@cs.northwestern.edu> */
34
35
36
37 #ifndef __VMM_HASHTABLE_H__
38 #define __VMM_HASHTABLE_H__
39
40 #ifdef __V3VEE__
41
42 struct hashtable;
43
44 /* Example of use:
45  *
46  *      struct hashtable  *h;
47  *      struct some_key   *k;
48  *      struct some_value *v;
49  *
50  *      static uint_t         hash_from_key_fn( void *k );
51  *      static int                  keys_equal_fn ( void *key1, void *key2 );
52  *
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));
56  *
57  *      (initialise k and v to suitable values)
58  * 
59  *      if (! hashtable_insert(h,k,v) )
60  *      {     exit(-1);               }
61  *
62  *      if (NULL == (found = hashtable_search(h,k) ))
63  *      {    printf("not found!");                  }
64  *
65  *      if (NULL == (found = hashtable_remove(h,k) ))
66  *      {    printf("Not found\n");                 }
67  *
68  */
69
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.
72  * 
73  * Example:
74  *
75  * Insert this at the start of your file:
76  *
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);
80  *
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).
86  *
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.
91  *
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
97  *
98  */
99
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);    \
103     }
104
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));   \
108     }
109
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)); \
113     }
114
115
116
117
118
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);
122
123
124
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));
128
129 void v3_free_htable(struct hashtable * htable, int free_values, int free_keys);
130
131 /*
132  * returns non-zero for successful insertion
133  *
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.
136  *
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.
142  */
143 int v3_htable_insert(struct hashtable * htable, addr_t key, addr_t value);
144
145 int v3_htable_change(struct hashtable * htable, addr_t key, addr_t value, int free_value);
146
147
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);
150
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);
153
154 uint_t v3_htable_count(struct hashtable * htable);
155
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);
159
160
161 /* ************ */
162 /* ITERATOR API */
163 /* ************ */
164
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));              \
168     }
169
170
171
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;
179     uint_t index;
180 };
181
182
183 struct hashtable_iter * v3_create_htable_iter(struct hashtable * htable);
184
185 /* - return the value of the (key,value) pair at the current position */
186 //extern inline 
187 addr_t v3_htable_get_iter_key(struct hashtable_iter * iter);
188 /* {
189    return iter->entry->key;
190    }
191 */
192
193
194 /* value - return the value of the (key,value) pair at the current position */
195 //extern inline 
196 addr_t v3_htable_get_iter_value(struct hashtable_iter * iter);
197 /* {
198    return iter->entry->value;
199    }
200 */
201
202
203
204 /* returns zero if advanced to end of table */
205 int v3_htable_iter_advance(struct hashtable_iter * iter);
206
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 
211  */
212 int v3_htable_iter_remove(struct hashtable_iter * iter, int free_key);
213
214
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);
219
220
221
222
223 #endif // ! __V3VEE__
224
225
226 #endif /* __VMM_HASHTABLE_H__ */
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 /*
266  * Copyright (c) 2002, Christopher Clark
267  * All rights reserved.
268  * 
269  * Redistribution and use in source and binary forms, with or without
270  * modification, are permitted provided that the following conditions
271  * are met:
272  * 
273  * * Redistributions of source code must retain the above copyright
274  * notice, this list of conditions and the following disclaimer.
275  * 
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.
279  * 
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.
283  * 
284  * 
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.
296  */