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-internals.h"
34 #include <mono/utils/checked-build.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;
67 MonoGCRootSource source;
71 static MonoGHashTable *
72 mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func);
75 static MonoGCDescriptor table_hash_descr = MONO_GC_DESCRIPTOR_NULL;
77 static void mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func, void *gc_data);
81 new_slot (MonoGHashTable *hash)
83 return mg_new (Slot, 1);
87 free_slot (MonoGHashTable *hash, Slot *slot)
98 for (n = 3; n< (int)sqrt (x); n += 2) {
104 // There is only one even prime - 2.
113 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
122 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, const char *msg)
124 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
126 hash->gc_type = type;
127 hash->source = source;
130 if (type > MONO_HASH_KEY_VALUE_GC)
131 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 mono_gc_register_root_wbarrier ((char*)hash, sizeof (MonoGHashTable), table_hash_descr, source, msg);
146 static MonoGHashTable *
147 mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
149 MonoGHashTable *hash;
151 if (hash_func == NULL)
152 hash_func = g_direct_hash;
153 if (key_equal_func == NULL)
154 key_equal_func = g_direct_equal;
155 hash = mg_new0 (MonoGHashTable, 1);
157 hash->hash_func = hash_func;
158 hash->key_equal_func = key_equal_func;
160 hash->table_size = g_spaced_primes_closest (1);
161 hash->table = mg_new0 (Slot *, hash->table_size);
162 hash->last_rehash = hash->table_size;
168 MonoGHashTable *hash;
174 do_rehash (void *_data)
176 RehashData *data = (RehashData *)_data;
177 MonoGHashTable *hash = data->hash;
181 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
182 hash->last_rehash = hash->table_size;
183 current_size = hash->table_size;
184 hash->table_size = data->new_size;
185 /* printf ("New size: %d\n", hash->table_size); */
187 hash->table = data->table;
189 for (i = 0; i < current_size; i++){
192 for (s = table [i]; s != NULL; s = next){
193 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
196 s->next = hash->table [hashcode];
197 hash->table [hashcode] = s;
204 rehash (MonoGHashTable *hash)
206 MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe
208 int diff = ABS (hash->last_rehash - hash->in_use);
210 void *old_table G_GNUC_UNUSED; /* unused on Boehm */
212 /* These are the factors to play with to change the rehashing strategy */
213 /* I played with them with a large range, and could not really get */
214 /* something that was too good, maybe the tests are not that great */
215 if (!(diff * 0.75 > hash->table_size * 2))
219 data.new_size = g_spaced_primes_closest (hash->in_use);
220 data.table = mg_new0 (Slot *, data.new_size);
222 if (!mono_threads_is_coop_enabled ()) {
223 old_table = mono_gc_invoke_with_gc_lock (do_rehash, &data);
225 /* We cannot be preempted */
226 old_table = do_rehash (&data);
233 mono_g_hash_table_size (MonoGHashTable *hash)
235 g_return_val_if_fail (hash != NULL, 0);
241 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
243 gpointer orig_key, value;
245 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
252 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
258 g_return_val_if_fail (hash != NULL, FALSE);
259 equal = hash->key_equal_func;
261 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
263 for (s = hash->table [hashcode]; s != NULL; s = s->next){
264 if ((*equal)(s->key, key)){
274 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
278 g_return_if_fail (hash != NULL);
279 g_return_if_fail (func != NULL);
281 for (i = 0; i < hash->table_size; i++){
284 for (s = hash->table [i]; s != NULL; s = s->next)
285 (*func)(s->key, s->value, user_data);
290 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
294 g_return_val_if_fail (hash != NULL, NULL);
295 g_return_val_if_fail (predicate != NULL, NULL);
297 for (i = 0; i < hash->table_size; i++){
300 for (s = hash->table [i]; s != NULL; s = s->next)
301 if ((*predicate)(s->key, s->value, user_data))
308 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
314 g_return_val_if_fail (hash != NULL, FALSE);
315 equal = hash->key_equal_func;
317 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
319 for (s = hash->table [hashcode]; s != NULL; s = s->next){
320 if ((*equal)(s->key, key)){
321 if (hash->key_destroy_func != NULL)
322 (*hash->key_destroy_func)(s->key);
323 if (hash->value_destroy_func != NULL)
324 (*hash->value_destroy_func)(s->value);
326 hash->table [hashcode] = s->next;
328 last->next = s->next;
339 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
344 g_return_val_if_fail (hash != NULL, 0);
345 g_return_val_if_fail (func != NULL, 0);
347 for (i = 0; i < hash->table_size; i++){
351 for (s = hash->table [i]; s != NULL; ){
352 if ((*func)(s->key, s->value, user_data)){
355 if (hash->key_destroy_func != NULL)
356 (*hash->key_destroy_func)(s->key);
357 if (hash->value_destroy_func != NULL)
358 (*hash->value_destroy_func)(s->value);
360 hash->table [i] = s->next;
363 last->next = s->next;
382 mono_g_hash_table_destroy (MonoGHashTable *hash)
386 g_return_if_fail (hash != NULL);
389 mono_gc_deregister_root ((char*)hash);
392 for (i = 0; i < hash->table_size; i++){
395 for (s = hash->table [i]; s != NULL; s = next){
398 if (hash->key_destroy_func != NULL)
399 (*hash->key_destroy_func)(s->key);
400 if (hash->value_destroy_func != NULL)
401 (*hash->value_destroy_func)(s->value);
405 mg_free (hash->table);
410 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
416 g_return_if_fail (hash != NULL);
418 equal = hash->key_equal_func;
419 if (hash->in_use >= hash->threshold)
422 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
423 for (s = hash->table [hashcode]; s != NULL; s = s->next){
424 if ((*equal) (s->key, key)){
426 if (hash->key_destroy_func != NULL)
427 (*hash->key_destroy_func)(s->key);
428 s->key = (MonoObject *)key;
430 if (hash->value_destroy_func != NULL)
431 (*hash->value_destroy_func) (s->value);
432 s->value = (MonoObject *)value;
437 s->key = (MonoObject *)key;
438 s->value = (MonoObject *)value;
439 s->next = hash->table [hashcode];
440 hash->table [hashcode] = s;
445 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
447 mono_g_hash_table_insert_replace (h, k, v, FALSE);
451 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
453 mono_g_hash_table_insert_replace (h, k, v, TRUE);
457 mono_g_hash_table_print_stats (MonoGHashTable *table)
459 int i, chain_size, max_chain_size;
463 for (i = 0; i < table->table_size; i++) {
465 for (node = table->table [i]; node; node = node->next)
467 max_chain_size = MAX(max_chain_size, chain_size);
470 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", table->in_use, table->table_size, max_chain_size);
475 /* GC marker function */
477 mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func, void *gc_data)
479 MonoGHashTable *table = (MonoGHashTable*)addr;
483 if (table->gc_type == MONO_HASH_KEY_GC) {
484 for (i = 0; i < table->table_size; i++) {
485 for (node = table->table [i]; node; node = node->next) {
487 mark_func (&node->key, gc_data);
490 } else if (table->gc_type == MONO_HASH_VALUE_GC) {
491 for (i = 0; i < table->table_size; i++) {
492 for (node = table->table [i]; node; node = node->next) {
494 mark_func (&node->value, gc_data);
497 } else if (table->gc_type == MONO_HASH_KEY_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->key, gc_data);
503 mark_func (&node->value, gc_data);