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 struct _MonoGHashTable {
49 GEqualFunc key_equal_func;
55 GDestroyNotify value_destroy_func, key_destroy_func;
56 MonoGHashGCType gc_type;
57 MonoGCRootSource source;
67 for (n = 3; n< (int)sqrt (x); n += 2) {
73 // There is only one even prime - 2.
82 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
90 #define HASH_TABLE_MAX_LOAD_FACTOR 0.7f
91 /* We didn't really do compaction before, keep it lenient for now */
92 #define HASH_TABLE_MIN_LOAD_FACTOR 0.05f
93 /* We triple the table size at rehash time, similar with previous implementation */
94 #define HASH_TABLE_RESIZE_RATIO 3
96 static inline void mono_g_hash_table_key_store (MonoGHashTable *hash, int slot, MonoObject* key)
98 MonoObject **key_addr = &hash->keys [slot];
99 if (hash->gc_type & MONO_HASH_KEY_GC)
100 mono_gc_wbarrier_generic_store (key_addr, key);
105 static inline void mono_g_hash_table_value_store (MonoGHashTable *hash, int slot, MonoObject* value)
107 MonoObject **value_addr = &hash->values [slot];
108 if (hash->gc_type & MONO_HASH_VALUE_GC)
109 mono_gc_wbarrier_generic_store (value_addr, value);
114 /* Returns position of key or of an empty slot for it */
115 static inline int mono_g_hash_table_find_slot (MonoGHashTable *hash, const MonoObject *key)
117 guint i = ((*hash->hash_func) (key)) % hash->table_size;
119 if (hash->key_equal_func) {
120 GEqualFunc equal = hash->key_equal_func;
122 while (hash->keys [i] && !(*equal) (hash->keys [i], key)) {
124 if (i == hash->table_size)
128 while (hash->keys [i] && hash->keys [i] != key) {
130 if (i == hash->table_size)
139 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, const char *msg)
141 MonoGHashTable *hash;
144 hash_func = g_direct_hash;
147 hash = mg_new0 (MonoGHashTable, 1);
149 hash = mono_gc_alloc_fixed (sizeof (MonoGHashTable), MONO_GC_ROOT_DESCR_FOR_FIXED (sizeof (MonoGHashTable)), source, msg);
152 hash->hash_func = hash_func;
153 hash->key_equal_func = key_equal_func;
155 hash->table_size = g_spaced_primes_closest (1);
156 hash->keys = mg_new0 (MonoObject*, hash->table_size);
157 hash->values = mg_new0 (MonoObject*, hash->table_size);
159 hash->gc_type = type;
160 hash->source = source;
163 if (type > MONO_HASH_KEY_VALUE_GC)
164 g_error ("wrong type for gc hashtable");
167 if (hash->gc_type & MONO_HASH_KEY_GC)
168 mono_gc_register_root_wbarrier ((char*)hash->keys, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
169 if (hash->gc_type & MONO_HASH_VALUE_GC)
170 mono_gc_register_root_wbarrier ((char*)hash->values, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
177 MonoGHashTable *hash;
184 do_rehash (void *_data)
186 RehashData *data = (RehashData *)_data;
187 MonoGHashTable *hash = data->hash;
189 MonoObject **old_keys;
190 MonoObject **old_values;
192 current_size = hash->table_size;
193 hash->table_size = data->new_size;
194 old_keys = hash->keys;
195 old_values = hash->values;
196 hash->keys = data->keys;
197 hash->values = data->values;
199 for (i = 0; i < current_size; i++) {
201 int slot = mono_g_hash_table_find_slot (hash, old_keys [i]);
202 mono_g_hash_table_key_store (hash, slot, old_keys [i]);
203 mono_g_hash_table_value_store (hash, slot, old_values [i]);
210 rehash (MonoGHashTable *hash)
212 MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe
215 void *old_keys G_GNUC_UNUSED = hash->keys; /* unused on Boehm */
216 void *old_values G_GNUC_UNUSED = hash->values; /* unused on Boehm */
220 * Rehash to a size that can fit the current elements. Rehash relative to in_use
221 * to allow also for compaction.
223 data.new_size = g_spaced_primes_closest (hash->in_use / HASH_TABLE_MAX_LOAD_FACTOR * HASH_TABLE_RESIZE_RATIO);
224 data.keys = mg_new0 (MonoObject*, data.new_size);
225 data.values = mg_new0 (MonoObject*, data.new_size);
228 if (hash->gc_type & MONO_HASH_KEY_GC)
229 mono_gc_register_root_wbarrier ((char*)data.keys, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
230 if (hash->gc_type & MONO_HASH_VALUE_GC)
231 mono_gc_register_root_wbarrier ((char*)data.values, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
234 if (!mono_threads_is_coop_enabled ()) {
235 mono_gc_invoke_with_gc_lock (do_rehash, &data);
237 /* We cannot be preempted */
242 if (hash->gc_type & MONO_HASH_KEY_GC)
243 mono_gc_deregister_root ((char*)old_keys);
244 if (hash->gc_type & MONO_HASH_VALUE_GC)
245 mono_gc_deregister_root ((char*)old_values);
248 mg_free (old_values);
252 mono_g_hash_table_size (MonoGHashTable *hash)
254 g_return_val_if_fail (hash != NULL, 0);
260 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
262 gpointer orig_key, value;
264 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
271 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
275 g_return_val_if_fail (hash != NULL, FALSE);
277 slot = mono_g_hash_table_find_slot (hash, key);
279 if (hash->keys [slot]) {
280 *orig_key = hash->keys [slot];
281 *value = hash->values [slot];
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++) {
298 (*func)(hash->keys [i], hash->values [i], user_data);
303 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
307 g_return_val_if_fail (hash != NULL, NULL);
308 g_return_val_if_fail (predicate != NULL, NULL);
310 for (i = 0; i < hash->table_size; i++) {
311 if (hash->keys [i] && (*predicate)(hash->keys [i], hash->values [i], user_data))
312 return hash->values [i];
318 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
320 int slot, last_clear_slot;
322 g_return_val_if_fail (hash != NULL, FALSE);
323 slot = mono_g_hash_table_find_slot (hash, key);
325 if (!hash->keys [slot])
328 if (hash->key_destroy_func)
329 (*hash->key_destroy_func)(hash->keys [slot]);
330 hash->keys [slot] = NULL;
331 if (hash->value_destroy_func)
332 (*hash->value_destroy_func)(hash->values [slot]);
333 hash->values [slot] = NULL;
337 * When we insert in the hashtable, if the required position is occupied we
338 * consecutively try out following positions. In order to be able to find
339 * if a key exists or not in the array (without traversing the entire hash)
340 * we maintain the constraint that there can be no free slots between two
341 * entries that are hashed to the same position. This means that, at search
342 * time, when we encounter a free slot we can stop looking for collissions.
343 * Similarly, at remove time, we need to shift all following slots to their
344 * normal slot, until we reach an empty slot.
346 last_clear_slot = slot;
347 slot = (slot + 1) % hash->table_size;
348 while (hash->keys [slot]) {
349 guint hashcode = ((*hash->hash_func)(hash->keys [slot])) % hash->table_size;
351 * We try to move the current element to last_clear_slot, but only if
352 * it brings it closer to its normal position (hashcode)
354 if ((last_clear_slot < slot && (hashcode > slot || hashcode <= last_clear_slot)) ||
355 (last_clear_slot > slot && (hashcode > slot && hashcode <= last_clear_slot))) {
356 mono_g_hash_table_key_store (hash, last_clear_slot, hash->keys [slot]);
357 mono_g_hash_table_value_store (hash, last_clear_slot, hash->values [slot]);
358 hash->keys [slot] = NULL;
359 hash->values [slot] = NULL;
360 last_clear_slot = slot;
363 if (slot == hash->table_size)
370 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
375 g_return_val_if_fail (hash != NULL, 0);
376 g_return_val_if_fail (func != NULL, 0);
378 for (i = 0; i < hash->table_size; i++) {
379 if (hash->keys [i] && (*func)(hash->keys [i], hash->values [i], user_data)) {
380 mono_g_hash_table_remove (hash, hash->keys [i]);
382 /* Retry current slot in case the removal shifted elements */
386 if (hash->in_use < hash->table_size * HASH_TABLE_MIN_LOAD_FACTOR)
392 mono_g_hash_table_destroy (MonoGHashTable *hash)
396 g_return_if_fail (hash != NULL);
399 if (hash->gc_type & MONO_HASH_KEY_GC)
400 mono_gc_deregister_root ((char*)hash->keys);
401 if (hash->gc_type & MONO_HASH_VALUE_GC)
402 mono_gc_deregister_root ((char*)hash->values);
405 for (i = 0; i < hash->table_size; i++) {
406 if (hash->keys [i]) {
407 if (hash->key_destroy_func)
408 (*hash->key_destroy_func)(hash->keys [i]);
409 if (hash->value_destroy_func)
410 (*hash->value_destroy_func)(hash->values [i]);
413 mg_free (hash->keys);
414 mg_free (hash->values);
418 mono_gc_free_fixed (hash);
423 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
426 g_return_if_fail (hash != NULL);
428 if (hash->in_use > (hash->table_size * HASH_TABLE_MAX_LOAD_FACTOR))
431 slot = mono_g_hash_table_find_slot (hash, key);
433 if (hash->keys [slot]) {
435 if (hash->key_destroy_func)
436 (*hash->key_destroy_func)(hash->keys [slot]);
437 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
439 if (hash->value_destroy_func)
440 (*hash->value_destroy_func) (hash->values [slot]);
441 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
443 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
444 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
450 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
452 mono_g_hash_table_insert_replace (h, k, v, FALSE);
456 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
458 mono_g_hash_table_insert_replace (h, k, v, TRUE);
462 mono_g_hash_table_print_stats (MonoGHashTable *hash)
464 int i = 0, chain_size = 0, max_chain_size = 0;
465 gboolean wrapped_around = FALSE;
468 if (hash->keys [i]) {
471 max_chain_size = MAX(max_chain_size, chain_size);
477 if (i == (hash->table_size - 1)) {
478 wrapped_around = TRUE;
484 /* Rehash to a size that can fit the current elements */
485 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", hash->in_use, hash->table_size, max_chain_size);