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;
118 GEqualFunc equal = hash->key_equal_func;
120 while (hash->keys [i] && !(*equal) (hash->keys [i], key)) {
122 if (i == hash->table_size)
130 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, const char *msg)
132 MonoGHashTable *hash;
135 hash_func = g_direct_hash;
137 key_equal_func = g_direct_equal;
140 hash = mg_new0 (MonoGHashTable, 1);
142 hash = mono_gc_alloc_fixed (sizeof (MonoGHashTable), MONO_GC_ROOT_DESCR_FOR_FIXED (sizeof (MonoGHashTable)), source, msg);
145 hash->hash_func = hash_func;
146 hash->key_equal_func = key_equal_func;
148 hash->table_size = g_spaced_primes_closest (1);
149 hash->keys = mg_new0 (MonoObject*, hash->table_size);
150 hash->values = mg_new0 (MonoObject*, hash->table_size);
152 hash->gc_type = type;
153 hash->source = source;
156 if (type > MONO_HASH_KEY_VALUE_GC)
157 g_error ("wrong type for gc hashtable");
160 if (hash->gc_type & MONO_HASH_KEY_GC)
161 mono_gc_register_root_wbarrier ((char*)hash->keys, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
162 if (hash->gc_type & MONO_HASH_VALUE_GC)
163 mono_gc_register_root_wbarrier ((char*)hash->values, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
170 MonoGHashTable *hash;
177 do_rehash (void *_data)
179 RehashData *data = (RehashData *)_data;
180 MonoGHashTable *hash = data->hash;
182 MonoObject **old_keys;
183 MonoObject **old_values;
185 current_size = hash->table_size;
186 hash->table_size = data->new_size;
187 old_keys = hash->keys;
188 old_values = hash->values;
189 hash->keys = data->keys;
190 hash->values = data->values;
192 for (i = 0; i < current_size; i++) {
194 int slot = mono_g_hash_table_find_slot (hash, old_keys [i]);
195 mono_g_hash_table_key_store (hash, slot, old_keys [i]);
196 mono_g_hash_table_value_store (hash, slot, old_values [i]);
203 rehash (MonoGHashTable *hash)
205 MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe
208 void *old_keys G_GNUC_UNUSED = hash->keys; /* unused on Boehm */
209 void *old_values G_GNUC_UNUSED = hash->values; /* unused on Boehm */
213 * Rehash to a size that can fit the current elements. Rehash relative to in_use
214 * to allow also for compaction.
216 data.new_size = g_spaced_primes_closest (hash->in_use / HASH_TABLE_MAX_LOAD_FACTOR * HASH_TABLE_RESIZE_RATIO);
217 data.keys = mg_new0 (MonoObject*, data.new_size);
218 data.values = mg_new0 (MonoObject*, data.new_size);
221 if (hash->gc_type & MONO_HASH_KEY_GC)
222 mono_gc_register_root_wbarrier ((char*)data.keys, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
223 if (hash->gc_type & MONO_HASH_VALUE_GC)
224 mono_gc_register_root_wbarrier ((char*)data.values, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
227 if (!mono_threads_is_coop_enabled ()) {
228 mono_gc_invoke_with_gc_lock (do_rehash, &data);
230 /* We cannot be preempted */
235 if (hash->gc_type & MONO_HASH_KEY_GC)
236 mono_gc_deregister_root ((char*)old_keys);
237 if (hash->gc_type & MONO_HASH_VALUE_GC)
238 mono_gc_deregister_root ((char*)old_values);
241 mg_free (old_values);
245 mono_g_hash_table_size (MonoGHashTable *hash)
247 g_return_val_if_fail (hash != NULL, 0);
253 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
255 gpointer orig_key, value;
257 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
264 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
268 g_return_val_if_fail (hash != NULL, FALSE);
270 slot = mono_g_hash_table_find_slot (hash, key);
272 if (hash->keys [slot]) {
273 *orig_key = hash->keys [slot];
274 *value = hash->values [slot];
282 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
286 g_return_if_fail (hash != NULL);
287 g_return_if_fail (func != NULL);
289 for (i = 0; i < hash->table_size; i++) {
291 (*func)(hash->keys [i], hash->values [i], user_data);
296 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
300 g_return_val_if_fail (hash != NULL, NULL);
301 g_return_val_if_fail (predicate != NULL, NULL);
303 for (i = 0; i < hash->table_size; i++) {
304 if (hash->keys [i] && (*predicate)(hash->keys [i], hash->values [i], user_data))
305 return hash->values [i];
311 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
313 int slot, last_clear_slot;
315 g_return_val_if_fail (hash != NULL, FALSE);
316 slot = mono_g_hash_table_find_slot (hash, key);
318 if (!hash->keys [slot])
321 if (hash->key_destroy_func)
322 (*hash->key_destroy_func)(hash->keys [slot]);
323 hash->keys [slot] = NULL;
324 if (hash->value_destroy_func)
325 (*hash->value_destroy_func)(hash->values [slot]);
326 hash->values [slot] = NULL;
330 * When we insert in the hashtable, if the required position is occupied we
331 * consecutively try out following positions. In order to be able to find
332 * if a key exists or not in the array (without traversing the entire hash)
333 * we maintain the constraint that there can be no free slots between two
334 * entries that are hashed to the same position. This means that, at search
335 * time, when we encounter a free slot we can stop looking for collissions.
336 * Similarly, at remove time, we need to shift all following slots to their
337 * normal slot, until we reach an empty slot.
339 last_clear_slot = slot;
340 slot = (slot + 1) % hash->table_size;
341 while (hash->keys [slot]) {
342 guint hashcode = ((*hash->hash_func)(hash->keys [slot])) % hash->table_size;
344 * We try to move the current element to last_clear_slot, but only if
345 * it brings it closer to its normal position (hashcode)
347 if ((last_clear_slot < slot && (hashcode > slot || hashcode <= last_clear_slot)) ||
348 (last_clear_slot > slot && (hashcode > slot && hashcode <= last_clear_slot))) {
349 mono_g_hash_table_key_store (hash, last_clear_slot, hash->keys [slot]);
350 mono_g_hash_table_value_store (hash, last_clear_slot, hash->values [slot]);
351 hash->keys [slot] = NULL;
352 hash->values [slot] = NULL;
353 last_clear_slot = slot;
356 if (slot == hash->table_size)
363 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
368 g_return_val_if_fail (hash != NULL, 0);
369 g_return_val_if_fail (func != NULL, 0);
371 for (i = 0; i < hash->table_size; i++) {
372 if (hash->keys [i] && (*func)(hash->keys [i], hash->values [i], user_data)) {
373 mono_g_hash_table_remove (hash, hash->keys [i]);
375 /* Retry current slot in case the removal shifted elements */
379 if (hash->in_use < hash->table_size * HASH_TABLE_MIN_LOAD_FACTOR)
385 mono_g_hash_table_destroy (MonoGHashTable *hash)
389 g_return_if_fail (hash != NULL);
392 if (hash->gc_type & MONO_HASH_KEY_GC)
393 mono_gc_deregister_root ((char*)hash->keys);
394 if (hash->gc_type & MONO_HASH_VALUE_GC)
395 mono_gc_deregister_root ((char*)hash->values);
398 for (i = 0; i < hash->table_size; i++) {
399 if (hash->keys [i]) {
400 if (hash->key_destroy_func)
401 (*hash->key_destroy_func)(hash->keys [i]);
402 if (hash->value_destroy_func)
403 (*hash->value_destroy_func)(hash->values [i]);
406 mg_free (hash->keys);
407 mg_free (hash->values);
411 mono_gc_free_fixed (hash);
416 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
419 g_return_if_fail (hash != NULL);
421 if (hash->in_use > (hash->table_size * HASH_TABLE_MAX_LOAD_FACTOR))
424 slot = mono_g_hash_table_find_slot (hash, key);
426 if (hash->keys [slot]) {
428 if (hash->key_destroy_func)
429 (*hash->key_destroy_func)(hash->keys [slot]);
430 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
432 if (hash->value_destroy_func)
433 (*hash->value_destroy_func) (hash->values [slot]);
434 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
436 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
437 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
443 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
445 mono_g_hash_table_insert_replace (h, k, v, FALSE);
449 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
451 mono_g_hash_table_insert_replace (h, k, v, TRUE);
455 mono_g_hash_table_print_stats (MonoGHashTable *hash)
457 int i = 0, chain_size = 0, max_chain_size = 0;
458 gboolean wrapped_around = FALSE;
461 if (hash->keys [i]) {
464 max_chain_size = MAX(max_chain_size, chain_size);
470 if (i == (hash->table_size - 1)) {
471 wrapped_around = TRUE;
477 /* Rehash to a size that can fit the current elements */
478 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", hash->in_use, hash->table_size, max_chain_size);