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>
35 #include <mono/utils/mono-threads-coop.h>
38 #define mg_new0(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
39 #define mg_new(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
40 #define mg_free(x) do { } while (0)
42 #define mg_new0(x,n) g_new0(x,n)
43 #define mg_new(type,n) g_new(type,n)
44 #define mg_free(x) g_free(x)
47 typedef struct _Slot Slot;
55 static gpointer KEYMARKER_REMOVED = &KEYMARKER_REMOVED;
57 struct _MonoGHashTable {
59 GEqualFunc key_equal_func;
66 GDestroyNotify value_destroy_func, key_destroy_func;
67 MonoGHashGCType gc_type;
68 MonoGCRootSource source;
73 static MonoGCDescriptor table_hash_descr = MONO_GC_DESCRIPTOR_NULL;
75 static void mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func, void *gc_data);
79 new_slot (MonoGHashTable *hash)
81 return mg_new (Slot, 1);
85 free_slot (MonoGHashTable *hash, Slot *slot)
96 for (n = 3; n< (int)sqrt (x); n += 2) {
102 // There is only one even prime - 2.
111 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
120 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, const char *msg)
122 MonoGHashTable *hash;
124 if (hash_func == NULL)
125 hash_func = g_direct_hash;
126 if (key_equal_func == NULL)
127 key_equal_func = g_direct_equal;
130 hash = mg_new0 (MonoGHashTable, 1);
132 hash = mono_gc_alloc_fixed (sizeof (MonoGHashTable), MONO_GC_ROOT_DESCR_FOR_FIXED (sizeof (MonoGHashTable)), source, msg);
135 hash->hash_func = hash_func;
136 hash->key_equal_func = key_equal_func;
138 hash->table_size = g_spaced_primes_closest (1);
139 hash->table = mg_new0 (Slot *, hash->table_size);
140 hash->last_rehash = hash->table_size;
142 hash->gc_type = type;
143 hash->source = source;
146 if (type > MONO_HASH_KEY_VALUE_GC)
147 g_error ("wrong type for gc hashtable");
151 * We use a user defined marking function to avoid having to register a GC root for
154 if (!table_hash_descr)
155 table_hash_descr = mono_gc_make_root_descr_user (mono_g_hash_mark);
156 mono_gc_register_root_wbarrier ((char*)hash, sizeof (MonoGHashTable), table_hash_descr, source, msg);
163 MonoGHashTable *hash;
169 do_rehash (void *_data)
171 RehashData *data = (RehashData *)_data;
172 MonoGHashTable *hash = data->hash;
176 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
177 hash->last_rehash = hash->table_size;
178 current_size = hash->table_size;
179 hash->table_size = data->new_size;
180 /* printf ("New size: %d\n", hash->table_size); */
182 hash->table = data->table;
184 for (i = 0; i < current_size; i++){
187 for (s = table [i]; s != NULL; s = next){
188 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
191 s->next = hash->table [hashcode];
192 hash->table [hashcode] = s;
199 rehash (MonoGHashTable *hash)
201 MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe
203 int diff = ABS (hash->last_rehash - hash->in_use);
205 void *old_table G_GNUC_UNUSED; /* unused on Boehm */
207 /* These are the factors to play with to change the rehashing strategy */
208 /* I played with them with a large range, and could not really get */
209 /* something that was too good, maybe the tests are not that great */
210 if (!(diff * 0.75 > hash->table_size * 2))
214 data.new_size = g_spaced_primes_closest (hash->in_use);
215 data.table = mg_new0 (Slot *, data.new_size);
217 if (!mono_threads_is_coop_enabled ()) {
218 old_table = mono_gc_invoke_with_gc_lock (do_rehash, &data);
220 /* We cannot be preempted */
221 old_table = do_rehash (&data);
228 mono_g_hash_table_size (MonoGHashTable *hash)
230 g_return_val_if_fail (hash != NULL, 0);
236 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
238 gpointer orig_key, value;
240 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
247 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
253 g_return_val_if_fail (hash != NULL, FALSE);
254 equal = hash->key_equal_func;
256 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
258 for (s = hash->table [hashcode]; s != NULL; s = s->next){
259 if ((*equal)(s->key, key)){
269 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
273 g_return_if_fail (hash != NULL);
274 g_return_if_fail (func != NULL);
276 for (i = 0; i < hash->table_size; i++){
279 for (s = hash->table [i]; s != NULL; s = s->next)
280 (*func)(s->key, s->value, user_data);
285 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
289 g_return_val_if_fail (hash != NULL, NULL);
290 g_return_val_if_fail (predicate != NULL, NULL);
292 for (i = 0; i < hash->table_size; i++){
295 for (s = hash->table [i]; s != NULL; s = s->next)
296 if ((*predicate)(s->key, s->value, user_data))
303 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
309 g_return_val_if_fail (hash != NULL, FALSE);
310 equal = hash->key_equal_func;
312 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
314 for (s = hash->table [hashcode]; s != NULL; s = s->next){
315 if ((*equal)(s->key, key)){
316 if (hash->key_destroy_func != NULL)
317 (*hash->key_destroy_func)(s->key);
318 if (hash->value_destroy_func != NULL)
319 (*hash->value_destroy_func)(s->value);
321 hash->table [hashcode] = s->next;
323 last->next = s->next;
334 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
339 g_return_val_if_fail (hash != NULL, 0);
340 g_return_val_if_fail (func != NULL, 0);
342 for (i = 0; i < hash->table_size; i++){
346 for (s = hash->table [i]; s != NULL; ){
347 if ((*func)(s->key, s->value, user_data)){
350 if (hash->key_destroy_func != NULL)
351 (*hash->key_destroy_func)(s->key);
352 if (hash->value_destroy_func != NULL)
353 (*hash->value_destroy_func)(s->value);
355 hash->table [i] = s->next;
358 last->next = s->next;
377 mono_g_hash_table_destroy (MonoGHashTable *hash)
381 g_return_if_fail (hash != NULL);
384 mono_gc_deregister_root ((char*)hash);
387 for (i = 0; i < hash->table_size; i++){
390 for (s = hash->table [i]; s != NULL; s = next){
393 if (hash->key_destroy_func != NULL)
394 (*hash->key_destroy_func)(s->key);
395 if (hash->value_destroy_func != NULL)
396 (*hash->value_destroy_func)(s->value);
400 mg_free (hash->table);
404 mono_gc_free_fixed (hash);
409 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
415 g_return_if_fail (hash != NULL);
417 equal = hash->key_equal_func;
418 if (hash->in_use >= hash->threshold)
421 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
422 for (s = hash->table [hashcode]; s != NULL; s = s->next){
423 if ((*equal) (s->key, key)){
425 if (hash->key_destroy_func != NULL)
426 (*hash->key_destroy_func)(s->key);
427 s->key = (MonoObject *)key;
429 if (hash->value_destroy_func != NULL)
430 (*hash->value_destroy_func) (s->value);
431 s->value = (MonoObject *)value;
436 s->key = (MonoObject *)key;
437 s->value = (MonoObject *)value;
438 s->next = hash->table [hashcode];
439 hash->table [hashcode] = s;
444 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
446 mono_g_hash_table_insert_replace (h, k, v, FALSE);
450 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
452 mono_g_hash_table_insert_replace (h, k, v, TRUE);
456 mono_g_hash_table_print_stats (MonoGHashTable *table)
458 int i, chain_size, max_chain_size;
462 for (i = 0; i < table->table_size; i++) {
464 for (node = table->table [i]; node; node = node->next)
466 max_chain_size = MAX(max_chain_size, chain_size);
469 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", table->in_use, table->table_size, max_chain_size);
474 /* GC marker function */
476 mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func, void *gc_data)
478 MonoGHashTable *table = (MonoGHashTable*)addr;
482 if (table->gc_type == MONO_HASH_KEY_GC) {
483 for (i = 0; i < table->table_size; i++) {
484 for (node = table->table [i]; node; node = node->next) {
486 mark_func (&node->key, gc_data);
489 } else if (table->gc_type == MONO_HASH_VALUE_GC) {
490 for (i = 0; i < table->table_size; i++) {
491 for (node = table->table [i]; node; node = node->next) {
493 mark_func (&node->value, gc_data);
496 } else if (table->gc_type == MONO_HASH_KEY_VALUE_GC) {
497 for (i = 0; i < table->table_size; i++) {
498 for (node = table->table [i]; node; node = node->next) {
500 mark_func (&node->key, gc_data);
502 mark_func (&node->value, gc_data);