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;
68 static const int prime_tbl[] = {
69 11, 19, 37, 73, 109, 163, 251, 367, 557, 823, 1237,
70 1861, 2777, 4177, 6247, 9371, 14057, 21089, 31627,
71 47431, 71143, 106721, 160073, 240101, 360163,
72 540217, 810343, 1215497, 1823231, 2734867, 4102283,
73 6153409, 9230113, 13845163
77 static void *table_hash_descr = NULL;
79 static void mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func);
82 new_slot (MonoGHashTable *hash)
84 if (hash->gc_type == MONO_HASH_CONSERVATIVE_GC)
85 return mono_gc_alloc_fixed (sizeof (Slot), NULL);
87 return mg_new (Slot, 1);
91 free_slot (MonoGHashTable *hash, Slot *slot)
93 if (hash->gc_type == MONO_HASH_CONSERVATIVE_GC)
94 mono_gc_free_fixed (slot);
99 #define new_slot(h) mg_new(Slot,1)
100 #define free_slot(h,s) mg_free((s))
109 for (n = 3; n< (int)sqrt (x); n += 2) {
115 // There is only one even prime - 2.
124 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
133 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type)
135 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
137 hash->gc_type = type;
140 if (type < 0 || type > MONO_HASH_KEY_VALUE_GC)
141 g_error ("wrong type for gc hashtable");
143 * We use a user defined marking function to avoid having to register a GC root for
146 if (!table_hash_descr)
147 table_hash_descr = mono_gc_make_root_descr_user (mono_g_hash_mark);
148 if (type != MONO_HASH_CONSERVATIVE_GC)
149 mono_gc_register_root_wbarrier ((char*)hash, sizeof (MonoGHashTable), table_hash_descr);
156 mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
158 MonoGHashTable *hash;
160 if (hash_func == NULL)
161 hash_func = g_direct_hash;
162 if (key_equal_func == NULL)
163 key_equal_func = g_direct_equal;
164 hash = mg_new0 (MonoGHashTable, 1);
166 hash->hash_func = hash_func;
167 hash->key_equal_func = key_equal_func;
169 hash->table_size = g_spaced_primes_closest (1);
170 hash->table = mg_new0 (Slot *, hash->table_size);
171 hash->last_rehash = hash->table_size;
177 mono_g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
178 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
180 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
184 hash->key_destroy_func = key_destroy_func;
185 hash->value_destroy_func = value_destroy_func;
191 MonoGHashTable *hash;
197 do_rehash (void *_data)
199 RehashData *data = _data;
200 MonoGHashTable *hash = data->hash;
204 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
205 hash->last_rehash = hash->table_size;
206 current_size = hash->table_size;
207 hash->table_size = data->new_size;
208 /* printf ("New size: %d\n", hash->table_size); */
210 hash->table = data->table;
212 for (i = 0; i < current_size; i++){
215 for (s = table [i]; s != NULL; s = next){
216 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
219 s->next = hash->table [hashcode];
220 hash->table [hashcode] = s;
227 rehash (MonoGHashTable *hash)
229 int diff = ABS (hash->last_rehash - hash->in_use);
233 /* These are the factors to play with to change the rehashing strategy */
234 /* I played with them with a large range, and could not really get */
235 /* something that was too good, maybe the tests are not that great */
236 if (!(diff * 0.75 > hash->table_size * 2))
240 data.new_size = g_spaced_primes_closest (hash->in_use);
241 data.table = mg_new0 (Slot *, data.new_size);
243 old_table = mono_gc_invoke_with_gc_lock (do_rehash, &data);
248 mono_g_hash_table_size (MonoGHashTable *hash)
250 g_return_val_if_fail (hash != NULL, 0);
256 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
258 gpointer orig_key, value;
260 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
267 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
273 g_return_val_if_fail (hash != NULL, FALSE);
274 equal = hash->key_equal_func;
276 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
278 for (s = hash->table [hashcode]; s != NULL; s = s->next){
279 if ((*equal)(s->key, key)){
289 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
293 g_return_if_fail (hash != NULL);
294 g_return_if_fail (func != NULL);
296 for (i = 0; i < hash->table_size; i++){
299 for (s = hash->table [i]; s != NULL; s = s->next)
300 (*func)(s->key, s->value, user_data);
305 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
309 g_return_val_if_fail (hash != NULL, NULL);
310 g_return_val_if_fail (predicate != NULL, NULL);
312 for (i = 0; i < hash->table_size; i++){
315 for (s = hash->table [i]; s != NULL; s = s->next)
316 if ((*predicate)(s->key, s->value, user_data))
323 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
329 g_return_val_if_fail (hash != NULL, FALSE);
330 equal = hash->key_equal_func;
332 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
334 for (s = hash->table [hashcode]; s != NULL; s = s->next){
335 if ((*equal)(s->key, key)){
336 if (hash->key_destroy_func != NULL)
337 (*hash->key_destroy_func)(s->key);
338 if (hash->value_destroy_func != NULL)
339 (*hash->value_destroy_func)(s->value);
341 hash->table [hashcode] = s->next;
343 last->next = s->next;
354 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
359 g_return_val_if_fail (hash != NULL, 0);
360 g_return_val_if_fail (func != NULL, 0);
362 for (i = 0; i < hash->table_size; i++){
366 for (s = hash->table [i]; s != NULL; ){
367 if ((*func)(s->key, s->value, user_data)){
370 if (hash->key_destroy_func != NULL)
371 (*hash->key_destroy_func)(s->key);
372 if (hash->value_destroy_func != NULL)
373 (*hash->value_destroy_func)(s->value);
375 hash->table [i] = s->next;
378 last->next = s->next;
397 mono_g_hash_table_destroy (MonoGHashTable *hash)
401 g_return_if_fail (hash != NULL);
404 mono_gc_deregister_root ((char*)hash);
407 for (i = 0; i < hash->table_size; i++){
410 for (s = hash->table [i]; s != NULL; s = next){
413 if (hash->key_destroy_func != NULL)
414 (*hash->key_destroy_func)(s->key);
415 if (hash->value_destroy_func != NULL)
416 (*hash->value_destroy_func)(s->value);
420 mg_free (hash->table);
425 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
431 g_return_if_fail (hash != NULL);
433 equal = hash->key_equal_func;
434 if (hash->in_use >= hash->threshold)
437 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
438 for (s = hash->table [hashcode]; s != NULL; s = s->next){
439 if ((*equal) (s->key, key)){
441 if (hash->key_destroy_func != NULL)
442 (*hash->key_destroy_func)(s->key);
445 if (hash->value_destroy_func != NULL)
446 (*hash->value_destroy_func) (s->value);
454 s->next = hash->table [hashcode];
455 hash->table [hashcode] = s;
460 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
462 mono_g_hash_table_insert_replace (h, k, v, FALSE);
466 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
468 mono_g_hash_table_insert_replace (h, k, v, TRUE);
472 mono_g_hash_table_print_stats (MonoGHashTable *table)
474 int i, chain_size, max_chain_size;
478 for (i = 0; i < table->table_size; i++) {
480 for (node = table->table [i]; node; node = node->next)
482 max_chain_size = MAX(max_chain_size, chain_size);
485 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", table->in_use, table->table_size, max_chain_size);
490 /* GC marker function */
492 mono_g_hash_mark (void *addr, MonoGCMarkFunc mark_func)
494 MonoGHashTable *table = (MonoGHashTable*)addr;
498 if (table->gc_type == MONO_HASH_KEY_GC) {
499 for (i = 0; i < table->table_size; i++) {
500 for (node = table->table [i]; node; node = node->next) {
502 mark_func (&node->key);
505 } else if (table->gc_type == MONO_HASH_VALUE_GC) {
506 for (i = 0; i < table->table_size; i++) {
507 for (node = table->table [i]; node; node = node->next) {
509 mark_func (&node->value);
512 } else if (table->gc_type == MONO_HASH_KEY_VALUE_GC) {
513 for (i = 0; i < table->table_size; i++) {
514 for (node = table->table [i]; node; node = node->next) {
516 mark_func (&node->key);
518 mark_func (&node->value);