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, MonoGCCopyFunc 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))
108 for (n = 3; n< (int)sqrt (x); n += 2) {
114 // There is only one even prime - 2.
123 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
131 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type)
133 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
135 hash->gc_type = type;
138 if (type < 0 || type > MONO_HASH_KEY_VALUE_GC)
139 g_error ("wrong type for gc hashtable");
141 * We use a user defined marking function to avoid having to register a GC root for
144 if (!table_hash_descr)
145 table_hash_descr = mono_gc_make_root_descr_user (mono_g_hash_mark);
146 if (type != MONO_HASH_CONSERVATIVE_GC)
147 mono_gc_register_root_wbarrier ((char*)hash, sizeof (MonoGHashTable), table_hash_descr);
154 mono_g_hash_table_new (GHashFunc hash_func, GEqualFunc key_equal_func)
156 MonoGHashTable *hash;
158 if (hash_func == NULL)
159 hash_func = g_direct_hash;
160 if (key_equal_func == NULL)
161 key_equal_func = g_direct_equal;
162 hash = mg_new0 (MonoGHashTable, 1);
164 hash->hash_func = hash_func;
165 hash->key_equal_func = key_equal_func;
167 hash->table_size = g_spaced_primes_closest (1);
168 hash->table = mg_new0 (Slot *, hash->table_size);
169 hash->last_rehash = hash->table_size;
175 mono_g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func,
176 GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func)
178 MonoGHashTable *hash = mono_g_hash_table_new (hash_func, key_equal_func);
182 hash->key_destroy_func = key_destroy_func;
183 hash->value_destroy_func = value_destroy_func;
189 MonoGHashTable *hash;
195 do_rehash (void *_data)
197 RehashData *data = _data;
198 MonoGHashTable *hash = data->hash;
202 /* printf ("Resizing diff=%d slots=%d\n", hash->in_use - hash->last_rehash, hash->table_size); */
203 hash->last_rehash = hash->table_size;
204 current_size = hash->table_size;
205 hash->table_size = data->new_size;
206 /* printf ("New size: %d\n", hash->table_size); */
208 hash->table = data->table;
210 for (i = 0; i < current_size; i++){
213 for (s = table [i]; s != NULL; s = next){
214 guint hashcode = ((*hash->hash_func) (s->key)) % hash->table_size;
217 s->next = hash->table [hashcode];
218 hash->table [hashcode] = s;
225 rehash (MonoGHashTable *hash)
227 int diff = ABS (hash->last_rehash - hash->in_use);
230 /* These are the factors to play with to change the rehashing strategy */
231 /* I played with them with a large range, and could not really get */
232 /* something that was too good, maybe the tests are not that great */
233 if (!(diff * 0.75 > hash->table_size * 2))
237 data.new_size = g_spaced_primes_closest (hash->in_use);
238 data.table = mg_new0 (Slot *, data.new_size);
240 mg_free (mono_gc_invoke_with_gc_lock (do_rehash, &data));
244 mono_g_hash_table_size (MonoGHashTable *hash)
246 g_return_val_if_fail (hash != NULL, 0);
252 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
254 gpointer orig_key, value;
256 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
263 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
269 g_return_val_if_fail (hash != NULL, FALSE);
270 equal = hash->key_equal_func;
272 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
274 for (s = hash->table [hashcode]; s != NULL; s = s->next){
275 if ((*equal)(s->key, key)){
285 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
289 g_return_if_fail (hash != NULL);
290 g_return_if_fail (func != NULL);
292 for (i = 0; i < hash->table_size; i++){
295 for (s = hash->table [i]; s != NULL; s = s->next)
296 (*func)(s->key, s->value, user_data);
301 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
305 g_return_val_if_fail (hash != NULL, NULL);
306 g_return_val_if_fail (predicate != NULL, NULL);
308 for (i = 0; i < hash->table_size; i++){
311 for (s = hash->table [i]; s != NULL; s = s->next)
312 if ((*predicate)(s->key, s->value, user_data))
319 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
325 g_return_val_if_fail (hash != NULL, FALSE);
326 equal = hash->key_equal_func;
328 hashcode = ((*hash->hash_func)(key)) % hash->table_size;
330 for (s = hash->table [hashcode]; s != NULL; s = s->next){
331 if ((*equal)(s->key, key)){
332 if (hash->key_destroy_func != NULL)
333 (*hash->key_destroy_func)(s->key);
334 if (hash->value_destroy_func != NULL)
335 (*hash->value_destroy_func)(s->value);
337 hash->table [hashcode] = s->next;
339 last->next = s->next;
350 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
355 g_return_val_if_fail (hash != NULL, 0);
356 g_return_val_if_fail (func != NULL, 0);
358 for (i = 0; i < hash->table_size; i++){
362 for (s = hash->table [i]; s != NULL; ){
363 if ((*func)(s->key, s->value, user_data)){
366 if (hash->key_destroy_func != NULL)
367 (*hash->key_destroy_func)(s->key);
368 if (hash->value_destroy_func != NULL)
369 (*hash->value_destroy_func)(s->value);
371 hash->table [i] = s->next;
374 last->next = s->next;
393 mono_g_hash_table_destroy (MonoGHashTable *hash)
397 g_return_if_fail (hash != NULL);
400 mono_gc_deregister_root ((char*)hash);
403 for (i = 0; i < hash->table_size; i++){
406 for (s = hash->table [i]; s != NULL; s = next){
409 if (hash->key_destroy_func != NULL)
410 (*hash->key_destroy_func)(s->key);
411 if (hash->value_destroy_func != NULL)
412 (*hash->value_destroy_func)(s->value);
416 mg_free (hash->table);
421 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
427 g_return_if_fail (hash != NULL);
429 equal = hash->key_equal_func;
430 if (hash->in_use >= hash->threshold)
433 hashcode = ((*hash->hash_func) (key)) % hash->table_size;
434 for (s = hash->table [hashcode]; s != NULL; s = s->next){
435 if ((*equal) (s->key, key)){
437 if (hash->key_destroy_func != NULL)
438 (*hash->key_destroy_func)(s->key);
441 if (hash->value_destroy_func != NULL)
442 (*hash->value_destroy_func) (s->value);
450 s->next = hash->table [hashcode];
451 hash->table [hashcode] = s;
456 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
458 mono_g_hash_table_insert_replace (h, k, v, FALSE);
462 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
464 mono_g_hash_table_insert_replace (h, k, v, TRUE);
469 /* GC marker function */
471 mono_g_hash_mark (void *addr, MonoGCCopyFunc mark_func)
473 MonoGHashTable *table = (MonoGHashTable*)addr;
477 if (table->gc_type == MONO_HASH_KEY_GC) {
478 for (i = 0; i < table->table_size; i++) {
479 for (node = table->table [i]; node; node = node->next) {
481 node->key = mark_func (node->key);
484 } else if (table->gc_type == MONO_HASH_VALUE_GC) {
485 for (i = 0; i < table->table_size; i++) {
486 for (node = table->table [i]; node; node = node->next) {
488 node->value = mark_func (node->value);
491 } else if (table->gc_type == MONO_HASH_KEY_VALUE_GC) {
492 for (i = 0; i < table->table_size; i++) {
493 for (node = table->table [i]; node; node = node->next) {
495 node->key = mark_func (node->key);
497 node->value = mark_func (node->value);