* Makefile.am (version.h): Fix issues when built out of tree.
[mono.git] / mono / utils / mono-ehash.c
index 78fd08eafa594eaca0d7cefa439b9de4635eb5eb..e5ece3a699f00fbbbde1545aea948a9a6fb7abd8 100644 (file)
@@ -29,7 +29,6 @@
 #include <stdio.h>
 #include <math.h>
 #include <glib.h>
-#include <mono/os/gc_wrapper.h>
 #include "mono-hash.h"
 #include "metadata/gc-internal.h"
 
@@ -177,7 +176,7 @@ do_rehash (MonoGHashTable *hash)
 static void
 rehash (MonoGHashTable *hash)
 {
-       int diff = ABS (hash->last_rehash, hash->in_use);
+       int diff = ABS (hash->last_rehash - hash->in_use);
 
        /* These are the factors to play with to change the rehashing strategy */
        /* I played with them with a large range, and could not really get */
@@ -338,9 +337,43 @@ mono_g_hash_table_destroy (MonoGHashTable *hash)
                        mg_free (s);
                }
        }
-       g_free (hash->table);
+       mg_free (hash->table);
+       mg_free (hash);
+}
+
+void
+mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
+{
+       guint hashcode;
+       Slot *s;
+       GEqualFunc equal;
        
-       g_free (hash);
+       g_return_if_fail (hash != NULL);
+
+       equal = hash->key_equal_func;
+       if (hash->in_use >= hash->threshold)
+               rehash (hash);
+
+       hashcode = ((*hash->hash_func) (key)) % hash->table_size;
+       for (s = hash->table [hashcode]; s != NULL; s = s->next){
+               if ((*equal) (s->key, key)){
+                       if (replace){
+                               if (hash->key_destroy_func != NULL)
+                                       (*hash->key_destroy_func)(s->key);
+                               s->key = key;
+                       }
+                       if (hash->value_destroy_func != NULL)
+                               (*hash->value_destroy_func) (s->value);
+                       s->value = value;
+                       return;
+               }
+       }
+       s = mg_new (Slot, 1);
+       s->key = key;
+       s->value = value;
+       s->next = hash->table [hashcode];
+       hash->table [hashcode] = s;
+       hash->in_use++;
 }
 
 void