25efb00586a0b620be57f7f555682b204967dfba
[mono.git] / mono / metadata / mono-hash.c
1 /**
2  * \file
3  * Hashtable implementation
4  *
5  * Author:
6  *   Miguel de Icaza (miguel@novell.com)
7  *
8  * (C) 2006 Novell, Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29 #include <config.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <glib.h>
33 #include "mono-hash.h"
34 #include "metadata/gc-internals.h"
35 #include <mono/utils/checked-build.h>
36 #include <mono/utils/mono-threads-coop.h>
37
38 int mono_g_hash_table_max_chain_length;
39
40 #ifdef HAVE_BOEHM_GC
41 #define mg_new0(type,n)  ((type *) GC_MALLOC(sizeof(type) * (n)))
42 #define mg_new(type,n)   ((type *) GC_MALLOC(sizeof(type) * (n)))
43 #define mg_free(x)       do { } while (0)
44 #else
45 #define mg_new0(x,n)     g_new0(x,n)
46 #define mg_new(type,n)   g_new(type,n)
47 #define mg_free(x)       g_free(x)
48 #endif
49
50 struct _MonoGHashTable {
51         GHashFunc      hash_func;
52         GEqualFunc     key_equal_func;
53
54         MonoObject **keys;
55         MonoObject **values;
56         int   table_size;
57         int   in_use;
58         GDestroyNotify value_destroy_func, key_destroy_func;
59         MonoGHashGCType gc_type;
60         MonoGCRootSource source;
61         const char *msg;
62 };
63
64 #if UNUSED
65 static gboolean
66 test_prime (int x)
67 {
68         if ((x & 1) != 0) {
69                 int n;
70                 for (n = 3; n< (int)sqrt (x); n += 2) {
71                         if ((x % n) == 0)
72                                 return FALSE;
73                 }
74                 return TRUE;
75         }
76         // There is only one even prime - 2.
77         return (x == 2);
78 }
79
80 static int
81 calc_prime (int x)
82 {
83         int i;
84
85         for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
86                 if (test_prime (i))
87                         return i;
88         }
89         return x;
90 }
91 #endif
92
93 #define HASH_TABLE_MAX_LOAD_FACTOR 0.7f
94 /* We didn't really do compaction before, keep it lenient for now */
95 #define HASH_TABLE_MIN_LOAD_FACTOR 0.05f
96 /* We triple the table size at rehash time, similar with previous implementation */
97 #define HASH_TABLE_RESIZE_RATIO 3
98
99 static inline void mono_g_hash_table_key_store (MonoGHashTable *hash, int slot, MonoObject* key)
100 {
101         MonoObject **key_addr = &hash->keys [slot];
102         if (hash->gc_type & MONO_HASH_KEY_GC)
103                 mono_gc_wbarrier_generic_store (key_addr, key);
104         else
105                 *key_addr = key;
106 }
107
108 static inline void mono_g_hash_table_value_store (MonoGHashTable *hash, int slot, MonoObject* value)
109 {
110         MonoObject **value_addr = &hash->values [slot];
111         if (hash->gc_type & MONO_HASH_VALUE_GC)
112                 mono_gc_wbarrier_generic_store (value_addr, value);
113         else
114                 *value_addr = value;
115 }
116
117 /* Returns position of key or of an empty slot for it */
118 static inline int mono_g_hash_table_find_slot (MonoGHashTable *hash, const MonoObject *key)
119 {
120         guint start = ((*hash->hash_func) (key)) % hash->table_size;
121         guint i = start;
122
123         if (hash->key_equal_func) {
124                 GEqualFunc equal = hash->key_equal_func;
125
126                 while (hash->keys [i] && !(*equal) (hash->keys [i], key)) {
127                         i++;
128                         if (i == hash->table_size)
129                                 i = 0;
130                 }
131         } else {
132                 while (hash->keys [i] && hash->keys [i] != key) {
133                         i++;
134                         if (i == hash->table_size)
135                                 i = 0;
136                 }
137         }
138
139         if (i > start && (i - start) > mono_g_hash_table_max_chain_length)
140                 mono_g_hash_table_max_chain_length = i - start;
141         else if (i < start && (hash->table_size - (start - i)) > mono_g_hash_table_max_chain_length)
142                 mono_g_hash_table_max_chain_length = hash->table_size - (start - i);
143         return i;
144 }
145
146
147 MonoGHashTable *
148 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, const char *msg)
149 {
150         MonoGHashTable *hash;
151
152         if (!hash_func)
153                 hash_func = g_direct_hash;
154
155 #ifdef HAVE_SGEN_GC
156         hash = mg_new0 (MonoGHashTable, 1);
157 #else
158         hash = mono_gc_alloc_fixed (sizeof (MonoGHashTable), MONO_GC_ROOT_DESCR_FOR_FIXED (sizeof (MonoGHashTable)), source, msg);
159 #endif
160
161         hash->hash_func = hash_func;
162         hash->key_equal_func = key_equal_func;
163
164         hash->table_size = g_spaced_primes_closest (1);
165         hash->keys = mg_new0 (MonoObject*, hash->table_size);
166         hash->values = mg_new0 (MonoObject*, hash->table_size);
167
168         hash->gc_type = type;
169         hash->source = source;
170         hash->msg = msg;
171
172         if (type > MONO_HASH_KEY_VALUE_GC)
173                 g_error ("wrong type for gc hashtable");
174
175 #ifdef HAVE_SGEN_GC
176         if (hash->gc_type & MONO_HASH_KEY_GC)
177                 mono_gc_register_root_wbarrier ((char*)hash->keys, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
178         if (hash->gc_type & MONO_HASH_VALUE_GC)
179                 mono_gc_register_root_wbarrier ((char*)hash->values, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
180 #endif
181
182         return hash;
183 }
184
185 typedef struct {
186         MonoGHashTable *hash;
187         int new_size;
188         MonoObject **keys;
189         MonoObject **values;
190 } RehashData;
191
192 static void*
193 do_rehash (void *_data)
194 {
195         RehashData *data = (RehashData *)_data;
196         MonoGHashTable *hash = data->hash;
197         int current_size, i;
198         MonoObject **old_keys;
199         MonoObject **old_values;
200
201         current_size = hash->table_size;
202         hash->table_size = data->new_size;
203         old_keys = hash->keys;
204         old_values = hash->values;
205         hash->keys = data->keys;
206         hash->values = data->values;
207
208         for (i = 0; i < current_size; i++) {
209                 if (old_keys [i]) {
210                         int slot = mono_g_hash_table_find_slot (hash, old_keys [i]);
211                         mono_g_hash_table_key_store (hash, slot, old_keys [i]);
212                         mono_g_hash_table_value_store (hash, slot, old_values [i]);
213                 }
214         }
215         return NULL;
216 }
217
218 static void
219 rehash (MonoGHashTable *hash)
220 {
221         MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe
222
223         RehashData data;
224         void *old_keys G_GNUC_UNUSED = hash->keys; /* unused on Boehm */
225         void *old_values G_GNUC_UNUSED = hash->values; /* unused on Boehm */
226
227         data.hash = hash;
228         /*
229          * Rehash to a size that can fit the current elements. Rehash relative to in_use
230          * to allow also for compaction.
231          */
232         data.new_size = g_spaced_primes_closest (hash->in_use / HASH_TABLE_MAX_LOAD_FACTOR * HASH_TABLE_RESIZE_RATIO);
233         data.keys = mg_new0 (MonoObject*, data.new_size);
234         data.values = mg_new0 (MonoObject*, data.new_size);
235
236 #ifdef HAVE_SGEN_GC
237         if (hash->gc_type & MONO_HASH_KEY_GC)
238                 mono_gc_register_root_wbarrier ((char*)data.keys, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
239         if (hash->gc_type & MONO_HASH_VALUE_GC)
240                 mono_gc_register_root_wbarrier ((char*)data.values, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
241 #endif
242
243         if (!mono_threads_is_coop_enabled ()) {
244                 mono_gc_invoke_with_gc_lock (do_rehash, &data);
245         } else {
246                 /* We cannot be preempted */
247                 do_rehash (&data);
248         }
249
250 #ifdef HAVE_SGEN_GC
251         if (hash->gc_type & MONO_HASH_KEY_GC)
252                 mono_gc_deregister_root ((char*)old_keys);
253         if (hash->gc_type & MONO_HASH_VALUE_GC)
254                 mono_gc_deregister_root ((char*)old_values);
255 #endif
256         mg_free (old_keys);
257         mg_free (old_values);
258 }
259
260 guint
261 mono_g_hash_table_size (MonoGHashTable *hash)
262 {
263         g_return_val_if_fail (hash != NULL, 0);
264
265         return hash->in_use;
266 }
267
268 gpointer
269 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
270 {
271         gpointer orig_key, value;
272
273         if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
274                 return value;
275         else
276                 return NULL;
277 }
278
279 gboolean
280 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
281 {
282         int slot;
283
284         g_return_val_if_fail (hash != NULL, FALSE);
285
286         slot = mono_g_hash_table_find_slot (hash, key);
287
288         if (hash->keys [slot]) {
289                 *orig_key = hash->keys [slot];
290                 *value = hash->values [slot];
291                 return TRUE;
292         }
293
294         return FALSE;
295 }
296
297 void
298 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
299 {
300         int i;
301
302         g_return_if_fail (hash != NULL);
303         g_return_if_fail (func != NULL);
304
305         for (i = 0; i < hash->table_size; i++) {
306                 if (hash->keys [i])
307                         (*func)(hash->keys [i], hash->values [i], user_data);
308         }
309 }
310
311 gpointer
312 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
313 {
314         int i;
315
316         g_return_val_if_fail (hash != NULL, NULL);
317         g_return_val_if_fail (predicate != NULL, NULL);
318
319         for (i = 0; i < hash->table_size; i++) {
320                 if (hash->keys [i] && (*predicate)(hash->keys [i], hash->values [i], user_data))
321                         return hash->values [i];
322         }
323         return NULL;
324 }
325
326 gboolean
327 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
328 {
329         int slot, last_clear_slot;
330
331         g_return_val_if_fail (hash != NULL, FALSE);
332         slot = mono_g_hash_table_find_slot (hash, key);
333
334         if (!hash->keys [slot])
335                 return FALSE;
336
337         if (hash->key_destroy_func)
338                 (*hash->key_destroy_func)(hash->keys [slot]);
339         hash->keys [slot] = NULL;
340         if (hash->value_destroy_func)
341                 (*hash->value_destroy_func)(hash->values [slot]);
342         hash->values [slot] = NULL;
343         hash->in_use--;
344
345         /*
346          * When we insert in the hashtable, if the required position is occupied we
347          * consecutively try out following positions. In order to be able to find
348          * if a key exists or not in the array (without traversing the entire hash)
349          * we maintain the constraint that there can be no free slots between two
350          * entries that are hashed to the same position. This means that, at search
351          * time, when we encounter a free slot we can stop looking for collissions.
352          * Similarly, at remove time, we need to shift all following slots to their
353          * normal slot, until we reach an empty slot.
354          */
355         last_clear_slot = slot;
356         slot = (slot + 1) % hash->table_size;
357         while (hash->keys [slot]) {
358                 guint hashcode = ((*hash->hash_func)(hash->keys [slot])) % hash->table_size;
359                 /*
360                  * We try to move the current element to last_clear_slot, but only if
361                  * it brings it closer to its normal position (hashcode)
362                  */
363                 if ((last_clear_slot < slot && (hashcode > slot || hashcode <= last_clear_slot)) ||
364                                 (last_clear_slot > slot && (hashcode > slot && hashcode <= last_clear_slot))) {
365                         mono_g_hash_table_key_store (hash, last_clear_slot, hash->keys [slot]);
366                         mono_g_hash_table_value_store (hash, last_clear_slot, hash->values [slot]);
367                         hash->keys [slot] = NULL;
368                         hash->values [slot] = NULL;
369                         last_clear_slot = slot;
370                 }
371                 slot++;
372                 if (slot == hash->table_size)
373                         slot = 0;
374         }
375         return TRUE;
376 }
377
378 guint
379 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
380 {
381         int i;
382         int count = 0;
383
384         g_return_val_if_fail (hash != NULL, 0);
385         g_return_val_if_fail (func != NULL, 0);
386
387         for (i = 0; i < hash->table_size; i++) {
388                 if (hash->keys [i] && (*func)(hash->keys [i], hash->values [i], user_data)) {
389                         mono_g_hash_table_remove (hash, hash->keys [i]);
390                         count++;
391                         /* Retry current slot in case the removal shifted elements */
392                         i--;
393                 }
394         }
395         if (hash->in_use < hash->table_size * HASH_TABLE_MIN_LOAD_FACTOR)
396                 rehash (hash);
397         return count;
398 }
399
400 void
401 mono_g_hash_table_destroy (MonoGHashTable *hash)
402 {
403         int i;
404
405         g_return_if_fail (hash != NULL);
406
407 #ifdef HAVE_SGEN_GC
408         if (hash->gc_type & MONO_HASH_KEY_GC)
409                 mono_gc_deregister_root ((char*)hash->keys);
410         if (hash->gc_type & MONO_HASH_VALUE_GC)
411                 mono_gc_deregister_root ((char*)hash->values);
412 #endif
413
414         for (i = 0; i < hash->table_size; i++) {
415                 if (hash->keys [i]) {
416                         if (hash->key_destroy_func)
417                                 (*hash->key_destroy_func)(hash->keys [i]);
418                         if (hash->value_destroy_func)
419                                 (*hash->value_destroy_func)(hash->values [i]);
420                 }
421         }
422         mg_free (hash->keys);
423         mg_free (hash->values);
424 #ifdef HAVE_SGEN_GC
425         mg_free (hash);
426 #else
427         mono_gc_free_fixed (hash);
428 #endif
429 }
430
431 static void
432 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
433 {
434         int slot;
435         g_return_if_fail (hash != NULL);
436
437         if (hash->in_use > (hash->table_size * HASH_TABLE_MAX_LOAD_FACTOR))
438                 rehash (hash);
439
440         slot = mono_g_hash_table_find_slot (hash, key);
441
442         if (hash->keys [slot]) {
443                 if (replace) {
444                         if (hash->key_destroy_func)
445                                 (*hash->key_destroy_func)(hash->keys [slot]);
446                         mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
447                 }
448                 if (hash->value_destroy_func)
449                         (*hash->value_destroy_func) (hash->values [slot]);
450                 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
451         } else {
452                 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
453                 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
454                 hash->in_use++;
455         }
456 }
457
458 void
459 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
460 {
461         mono_g_hash_table_insert_replace (h, k, v, FALSE);
462 }
463
464 void
465 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
466 {
467         mono_g_hash_table_insert_replace (h, k, v, TRUE);
468 }
469
470 void
471 mono_g_hash_table_print_stats (MonoGHashTable *hash)
472 {
473         int i = 0, chain_size = 0, max_chain_size = 0;
474         gboolean wrapped_around = FALSE;
475
476         while (TRUE) {
477                 if (hash->keys [i]) {
478                         chain_size++;
479                 } else {
480                         max_chain_size = MAX(max_chain_size, chain_size);
481                         chain_size = 0;
482                         if (wrapped_around)
483                                 break;
484                 }
485
486                 if (i == (hash->table_size - 1)) {
487                         wrapped_around = TRUE;
488                         i = 0;
489                 } else {
490                         i++;
491                 }
492         }
493         /* Rehash to a size that can fit the current elements */
494         printf ("Size: %d Table Size: %d Max Chain Length: %d\n", hash->in_use, hash->table_size, max_chain_size);
495 }