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-hash.h"
33 #include "metadata/gc-internal.h"
36 #define mg_new0(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
37 #define mg_new(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
38 #define mg_free(x) do { } while (0)
40 #define mg_new0(x,n) g_new0(x,n)
41 #define mg_new(type,n) g_new(type,n)
42 #define mg_free(x) g_free(x)
45 typedef struct _Slot Slot;
53 static gpointer KEYMARKER_REMOVED = &KEYMARKER_REMOVED;
55 struct _MonoGHashTable {
57 GEqualFunc key_equal_func;
64 GDestroyNotify value_destroy_func, key_destroy_func;
65 MonoGHashGCType gc_type;
69 static void *table_hash_descr = NULL;
71 static void mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func, void *gc_data);
74 new_slot (MonoGHashTable *hash)
76 if (hash->gc_type == MONO_HASH_CONSERVATIVE_GC)
77 return mono_gc_alloc_fixed (sizeof (Slot), NULL);
79 return mg_new (Slot, 1);
83 free_slot (MonoGHashTable *hash, Slot *slot)
85 if (hash->gc_type == MONO_HASH_CONSERVATIVE_GC)
86 mono_gc_free_fixed (slot);
91 #define new_slot(h) mg_new(Slot,1)
92 #define free_slot(h,s) mg_free((s))
101 for (n = 3; n< (int)sqrt (x); n += 2) {
107 // There is only one even prime - 2.
116 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
125 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type)
127 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
129 hash->gc_type = type;
132 if (type > MONO_HASH_KEY_VALUE_GC)
133 g_error ("wrong type for gc hashtable");
135 * We use a user defined marking function to avoid having to register a GC root for
138 if (!table_hash_descr)
139 table_hash_descr = mono_gc_make_root_descr_user (mono_g_hash_mark);
140 if (type != MONO_HASH_CONSERVATIVE_GC)
141 mono_gc_register_root_wbarrier ((char*)hash, sizeof (MonoGHashTable), table_hash_descr);
148 mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
150 MonoGHashTable *hash;
152 if (hash_func == NULL)
153 hash_func = g_direct_hash;
154 if (key_equal_func == NULL)
155 key_equal_func = g_direct_equal;
156 hash = mg_new0 (MonoGHashTable, 1);
158 hash->hash_func = hash_func;
159 hash->key_equal_func = key_equal_func;
161 hash->table_size = g_spaced_primes_closest (1);
162 hash->table = mg_new0 (Slot *, hash->table_size);
163 hash->last_rehash = hash->table_size;
169 mono_g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
170 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
172 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
176 hash->key_destroy_func = key_destroy_func;
177 hash->value_destroy_func = value_destroy_func;
183 MonoGHashTable *hash;
189 do_rehash (void *_data)
191 RehashData *data = _data;
192 MonoGHashTable *hash = data->hash;
196 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
197 hash->last_rehash = hash->table_size;
198 current_size = hash->table_size;
199 hash->table_size = data->new_size;
200 /* printf ("New size: %d\n", hash->table_size); */
202 hash->table = data->table;
204 for (i = 0; i < current_size; i++){
207 for (s = table [i]; s != NULL; s = next){
208 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
211 s->next = hash->table [hashcode];
212 hash->table [hashcode] = s;
219 rehash (MonoGHashTable *hash)
221 int diff = ABS (hash->last_rehash - hash->in_use);
225 /* These are the factors to play with to change the rehashing strategy */
226 /* I played with them with a large range, and could not really get */
227 /* something that was too good, maybe the tests are not that great */
228 if (!(diff * 0.75 > hash->table_size * 2))
232 data.new_size = g_spaced_primes_closest (hash->in_use);
233 data.table = mg_new0 (Slot *, data.new_size);
235 old_table = mono_gc_invoke_with_gc_lock (do_rehash, &data);
240 mono_g_hash_table_size (MonoGHashTable *hash)
242 g_return_val_if_fail (hash != NULL, 0);
248 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
250 gpointer orig_key, value;
252 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
259 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
265 g_return_val_if_fail (hash != NULL, FALSE);
266 equal = hash->key_equal_func;
268 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
270 for (s = hash->table [hashcode]; s != NULL; s = s->next){
271 if ((*equal)(s->key, key)){
281 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
285 g_return_if_fail (hash != NULL);
286 g_return_if_fail (func != NULL);
288 for (i = 0; i < hash->table_size; i++){
291 for (s = hash->table [i]; s != NULL; s = s->next)
292 (*func)(s->key, s->value, user_data);
297 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
301 g_return_val_if_fail (hash != NULL, NULL);
302 g_return_val_if_fail (predicate != NULL, NULL);
304 for (i = 0; i < hash->table_size; i++){
307 for (s = hash->table [i]; s != NULL; s = s->next)
308 if ((*predicate)(s->key, s->value, user_data))
315 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
321 g_return_val_if_fail (hash != NULL, FALSE);
322 equal = hash->key_equal_func;
324 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
326 for (s = hash->table [hashcode]; s != NULL; s = s->next){
327 if ((*equal)(s->key, key)){
328 if (hash->key_destroy_func != NULL)
329 (*hash->key_destroy_func)(s->key);
330 if (hash->value_destroy_func != NULL)
331 (*hash->value_destroy_func)(s->value);
333 hash->table [hashcode] = s->next;
335 last->next = s->next;
346 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
351 g_return_val_if_fail (hash != NULL, 0);
352 g_return_val_if_fail (func != NULL, 0);
354 for (i = 0; i < hash->table_size; i++){
358 for (s = hash->table [i]; s != NULL; ){
359 if ((*func)(s->key, s->value, user_data)){
362 if (hash->key_destroy_func != NULL)
363 (*hash->key_destroy_func)(s->key);
364 if (hash->value_destroy_func != NULL)
365 (*hash->value_destroy_func)(s->value);
367 hash->table [i] = s->next;
370 last->next = s->next;
389 mono_g_hash_table_destroy (MonoGHashTable *hash)
393 g_return_if_fail (hash != NULL);
396 mono_gc_deregister_root ((char*)hash);
399 for (i = 0; i < hash->table_size; i++){
402 for (s = hash->table [i]; s != NULL; s = next){
405 if (hash->key_destroy_func != NULL)
406 (*hash->key_destroy_func)(s->key);
407 if (hash->value_destroy_func != NULL)
408 (*hash->value_destroy_func)(s->value);
412 mg_free (hash->table);
417 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
423 g_return_if_fail (hash != NULL);
425 equal = hash->key_equal_func;
426 if (hash->in_use >= hash->threshold)
429 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
430 for (s = hash->table [hashcode]; s != NULL; s = s->next){
431 if ((*equal) (s->key, key)){
433 if (hash->key_destroy_func != NULL)
434 (*hash->key_destroy_func)(s->key);
437 if (hash->value_destroy_func != NULL)
438 (*hash->value_destroy_func) (s->value);
446 s->next = hash->table [hashcode];
447 hash->table [hashcode] = s;
452 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
454 mono_g_hash_table_insert_replace (h, k, v, FALSE);
458 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
460 mono_g_hash_table_insert_replace (h, k, v, TRUE);
464 mono_g_hash_table_print_stats (MonoGHashTable *table)
466 int i, chain_size, max_chain_size;
470 for (i = 0; i < table->table_size; i++) {
472 for (node = table->table [i]; node; node = node->next)
474 max_chain_size = MAX(max_chain_size, chain_size);
477 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", table->in_use, table->table_size, max_chain_size);
482 /* GC marker function */
484 mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func, void *gc_data)
486 MonoGHashTable *table = (MonoGHashTable*)addr;
490 if (table->gc_type == MONO_HASH_KEY_GC) {
491 for (i = 0; i < table->table_size; i++) {
492 for (node = table->table [i]; node; node = node->next) {
494 mark_func (&node->key, gc_data);
497 } else if (table->gc_type == MONO_HASH_VALUE_GC) {
498 for (i = 0; i < table->table_size; i++) {
499 for (node = table->table [i]; node; node = node->next) {
501 mark_func (&node->value, gc_data);
504 } else if (table->gc_type == MONO_HASH_KEY_VALUE_GC) {
505 for (i = 0; i < table->table_size; i++) {
506 for (node = table->table [i]; node; node = node->next) {
508 mark_func (&node->key, gc_data);
510 mark_func (&node->value, gc_data);