2 * ghashtable.c: Hashtable implementation
5 * Miguel de Icaza (miguel@novell.com)
7 * (C) 2006 Novell, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #include <mono/os/gc_wrapper.h>
33 #include "mono-hash.h"
34 #include "metadata/gc-internal.h"
37 #define mg_new0(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
38 #define mg_new(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
39 #define mg_free(x) do { } while (0)
41 #define mg_new0(x,n) g_new0(x,n)
42 #define mg_new(type,n) g_new(type,n)
43 #define mg_free(x) g_free(x)
46 typedef struct _Slot Slot;
54 static gpointer KEYMARKER_REMOVED = &KEYMARKER_REMOVED;
56 struct _MonoGHashTable {
58 GEqualFunc key_equal_func;
65 GDestroyNotify value_destroy_func, key_destroy_func;
66 MonoGHashGCType gc_type;
69 static const int prime_tbl[] = {
70 11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
71 1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
72 47431, 71143, 106721, 160073, 240101, 360163,
73 540217, 810343, 1215497, 1823231, 2734867, 4102283,
74 6153409, 9230113, 13845163
82 for (n = 3; n< (int)sqrt (x); n += 2) {
88 // There is only one even prime - 2.
97 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
105 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type)
107 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
109 hash->gc_type = type;
115 mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
117 MonoGHashTable *hash;
119 if (hash_func == NULL)
120 hash_func = g_direct_hash;
121 if (key_equal_func == NULL)
122 key_equal_func = g_direct_equal;
123 hash = mg_new0 (MonoGHashTable, 1);
125 hash->hash_func = hash_func;
126 hash->key_equal_func = key_equal_func;
128 hash->table_size = g_spaced_primes_closest (1);
129 hash->table = mg_new0 (Slot *, hash->table_size);
130 hash->last_rehash = hash->table_size;
136 mono_g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
137 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
139 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
143 hash->key_destroy_func = key_destroy_func;
144 hash->value_destroy_func = value_destroy_func;
150 do_rehash (MonoGHashTable *hash)
155 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
156 hash->last_rehash = hash->table_size;
157 current_size = hash->table_size;
158 hash->table_size = g_spaced_primes_closest (hash->in_use);
159 /* printf ("New size: %d\n", hash->table_size); */
161 hash->table = mg_new0 (Slot *, hash->table_size);
163 for (i = 0; i < current_size; i++){
166 for (s = table [i]; s != NULL; s = next){
167 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
170 s->next = hash->table [hashcode];
171 hash->table [hashcode] = s;
178 rehash (MonoGHashTable *hash)
180 int diff = ABS (hash->last_rehash, hash->in_use);
182 /* These are the factors to play with to change the rehashing strategy */
183 /* I played with them with a large range, and could not really get */
184 /* something that was too good, maybe the tests are not that great */
185 if (!(diff * 0.75 > hash->table_size * 2))
191 mono_g_hash_table_size (MonoGHashTable *hash)
193 g_return_val_if_fail (hash != NULL, 0);
199 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
201 gpointer orig_key, value;
203 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
210 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
216 g_return_val_if_fail (hash != NULL, FALSE);
217 equal = hash->key_equal_func;
219 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
221 for (s = hash->table [hashcode]; s != NULL; s = s->next){
222 if ((*equal)(s->key, key)){
232 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
236 g_return_if_fail (hash != NULL);
237 g_return_if_fail (func != NULL);
239 for (i = 0; i < hash->table_size; i++){
242 for (s = hash->table [i]; s != NULL; s = s->next)
243 (*func)(s->key, s->value, user_data);
248 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
254 g_return_val_if_fail (hash != NULL, FALSE);
255 equal = hash->key_equal_func;
257 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
259 for (s = hash->table [hashcode]; s != NULL; s = s->next){
260 if ((*equal)(s->key, key)){
261 if (hash->key_destroy_func != NULL)
262 (*hash->key_destroy_func)(s->key);
263 if (hash->value_destroy_func != NULL)
264 (*hash->value_destroy_func)(s->value);
266 hash->table [hashcode] = s->next;
268 last->next = s->next;
279 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
284 g_return_val_if_fail (hash != NULL, 0);
285 g_return_val_if_fail (func != NULL, 0);
287 for (i = 0; i < hash->table_size; i++){
291 for (s = hash->table [i]; s != NULL; ){
292 if ((*func)(s->key, s->value, user_data)){
295 if (hash->key_destroy_func != NULL)
296 (*hash->key_destroy_func)(s->key);
297 if (hash->value_destroy_func != NULL)
298 (*hash->value_destroy_func)(s->value);
300 hash->table [i] = s->next;
303 last->next = s->next;
322 mono_g_hash_table_destroy (MonoGHashTable *hash)
326 g_return_if_fail (hash != NULL);
328 for (i = 0; i < hash->table_size; i++){
331 for (s = hash->table [i]; s != NULL; s = next){
334 if (hash->key_destroy_func != NULL)
335 (*hash->key_destroy_func)(s->key);
336 if (hash->value_destroy_func != NULL)
337 (*hash->value_destroy_func)(s->value);
341 mg_free (hash->table);
346 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
352 g_return_if_fail (hash != NULL);
354 equal = hash->key_equal_func;
355 if (hash->in_use >= hash->threshold)
358 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
359 for (s = hash->table [hashcode]; s != NULL; s = s->next){
360 if ((*equal) (s->key, key)){
362 if (hash->key_destroy_func != NULL)
363 (*hash->key_destroy_func)(s->key);
366 if (hash->value_destroy_func != NULL)
367 (*hash->value_destroy_func) (s->value);
372 s = mg_new (Slot, 1);
375 s->next = hash->table [hashcode];
376 hash->table [hashcode] = s;
381 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
383 mono_g_hash_table_insert_replace (h, k, v, FALSE);
387 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
389 mono_g_hash_table_insert_replace (h, k, v, TRUE);