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>
37 int mono_g_hash_table_max_chain_length;
40 #define mg_new0(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
41 #define mg_new(type,n) ((type *) GC_MALLOC(sizeof(type) * (n)))
42 #define mg_free(x) do { } while (0)
44 #define mg_new0(x,n) g_new0(x,n)
45 #define mg_new(type,n) g_new(type,n)
46 #define mg_free(x) g_free(x)
49 struct _MonoGHashTable {
51 GEqualFunc key_equal_func;
57 GDestroyNotify value_destroy_func, key_destroy_func;
58 MonoGHashGCType gc_type;
59 MonoGCRootSource source;
69 for (n = 3; n< (int)sqrt (x); n += 2) {
75 // There is only one even prime - 2.
84 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
92 #define HASH_TABLE_MAX_LOAD_FACTOR 0.7f
93 /* We didn't really do compaction before, keep it lenient for now */
94 #define HASH_TABLE_MIN_LOAD_FACTOR 0.05f
95 /* We triple the table size at rehash time, similar with previous implementation */
96 #define HASH_TABLE_RESIZE_RATIO 3
98 static inline void mono_g_hash_table_key_store (MonoGHashTable *hash, int slot, MonoObject* key)
100 MonoObject **key_addr = &hash->keys [slot];
101 if (hash->gc_type & MONO_HASH_KEY_GC)
102 mono_gc_wbarrier_generic_store (key_addr, key);
107 static inline void mono_g_hash_table_value_store (MonoGHashTable *hash, int slot, MonoObject* value)
109 MonoObject **value_addr = &hash->values [slot];
110 if (hash->gc_type & MONO_HASH_VALUE_GC)
111 mono_gc_wbarrier_generic_store (value_addr, value);
116 /* Returns position of key or of an empty slot for it */
117 static inline int mono_g_hash_table_find_slot (MonoGHashTable *hash, const MonoObject *key)
119 guint start = ((*hash->hash_func) (key)) % hash->table_size;
122 if (hash->key_equal_func) {
123 GEqualFunc equal = hash->key_equal_func;
125 while (hash->keys [i] && !(*equal) (hash->keys [i], key)) {
127 if (i == hash->table_size)
131 while (hash->keys [i] && hash->keys [i] != key) {
133 if (i == hash->table_size)
138 if (i > start && (i - start) > mono_g_hash_table_max_chain_length)
139 mono_g_hash_table_max_chain_length = i - start;
140 else if (i < start && (hash->table_size - (start - i)) > mono_g_hash_table_max_chain_length)
141 mono_g_hash_table_max_chain_length = hash->table_size - (start - i);
147 mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, const char *msg)
149 MonoGHashTable *hash;
152 hash_func = g_direct_hash;
155 hash = mg_new0 (MonoGHashTable, 1);
157 hash = mono_gc_alloc_fixed (sizeof (MonoGHashTable), MONO_GC_ROOT_DESCR_FOR_FIXED (sizeof (MonoGHashTable)), source, msg);
160 hash->hash_func = hash_func;
161 hash->key_equal_func = key_equal_func;
163 hash->table_size = g_spaced_primes_closest (1);
164 hash->keys = mg_new0 (MonoObject*, hash->table_size);
165 hash->values = mg_new0 (MonoObject*, hash->table_size);
167 hash->gc_type = type;
168 hash->source = source;
171 if (type > MONO_HASH_KEY_VALUE_GC)
172 g_error ("wrong type for gc hashtable");
175 if (hash->gc_type & MONO_HASH_KEY_GC)
176 mono_gc_register_root_wbarrier ((char*)hash->keys, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
177 if (hash->gc_type & MONO_HASH_VALUE_GC)
178 mono_gc_register_root_wbarrier ((char*)hash->values, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
185 MonoGHashTable *hash;
192 do_rehash (void *_data)
194 RehashData *data = (RehashData *)_data;
195 MonoGHashTable *hash = data->hash;
197 MonoObject **old_keys;
198 MonoObject **old_values;
200 current_size = hash->table_size;
201 hash->table_size = data->new_size;
202 old_keys = hash->keys;
203 old_values = hash->values;
204 hash->keys = data->keys;
205 hash->values = data->values;
207 for (i = 0; i < current_size; i++) {
209 int slot = mono_g_hash_table_find_slot (hash, old_keys [i]);
210 mono_g_hash_table_key_store (hash, slot, old_keys [i]);
211 mono_g_hash_table_value_store (hash, slot, old_values [i]);
218 rehash (MonoGHashTable *hash)
220 MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe
223 void *old_keys G_GNUC_UNUSED = hash->keys; /* unused on Boehm */
224 void *old_values G_GNUC_UNUSED = hash->values; /* unused on Boehm */
228 * Rehash to a size that can fit the current elements. Rehash relative to in_use
229 * to allow also for compaction.
231 data.new_size = g_spaced_primes_closest (hash->in_use / HASH_TABLE_MAX_LOAD_FACTOR * HASH_TABLE_RESIZE_RATIO);
232 data.keys = mg_new0 (MonoObject*, data.new_size);
233 data.values = mg_new0 (MonoObject*, data.new_size);
236 if (hash->gc_type & MONO_HASH_KEY_GC)
237 mono_gc_register_root_wbarrier ((char*)data.keys, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
238 if (hash->gc_type & MONO_HASH_VALUE_GC)
239 mono_gc_register_root_wbarrier ((char*)data.values, sizeof (MonoObject*) * data.new_size, mono_gc_make_vector_descr (), hash->source, hash->msg);
242 if (!mono_threads_is_coop_enabled ()) {
243 mono_gc_invoke_with_gc_lock (do_rehash, &data);
245 /* We cannot be preempted */
250 if (hash->gc_type & MONO_HASH_KEY_GC)
251 mono_gc_deregister_root ((char*)old_keys);
252 if (hash->gc_type & MONO_HASH_VALUE_GC)
253 mono_gc_deregister_root ((char*)old_values);
256 mg_free (old_values);
260 mono_g_hash_table_size (MonoGHashTable *hash)
262 g_return_val_if_fail (hash != NULL, 0);
268 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
270 gpointer orig_key, value;
272 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
279 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
283 g_return_val_if_fail (hash != NULL, FALSE);
285 slot = mono_g_hash_table_find_slot (hash, key);
287 if (hash->keys [slot]) {
288 *orig_key = hash->keys [slot];
289 *value = hash->values [slot];
297 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
301 g_return_if_fail (hash != NULL);
302 g_return_if_fail (func != NULL);
304 for (i = 0; i < hash->table_size; i++) {
306 (*func)(hash->keys [i], hash->values [i], user_data);
311 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
315 g_return_val_if_fail (hash != NULL, NULL);
316 g_return_val_if_fail (predicate != NULL, NULL);
318 for (i = 0; i < hash->table_size; i++) {
319 if (hash->keys [i] && (*predicate)(hash->keys [i], hash->values [i], user_data))
320 return hash->values [i];
326 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
328 int slot, last_clear_slot;
330 g_return_val_if_fail (hash != NULL, FALSE);
331 slot = mono_g_hash_table_find_slot (hash, key);
333 if (!hash->keys [slot])
336 if (hash->key_destroy_func)
337 (*hash->key_destroy_func)(hash->keys [slot]);
338 hash->keys [slot] = NULL;
339 if (hash->value_destroy_func)
340 (*hash->value_destroy_func)(hash->values [slot]);
341 hash->values [slot] = NULL;
345 * When we insert in the hashtable, if the required position is occupied we
346 * consecutively try out following positions. In order to be able to find
347 * if a key exists or not in the array (without traversing the entire hash)
348 * we maintain the constraint that there can be no free slots between two
349 * entries that are hashed to the same position. This means that, at search
350 * time, when we encounter a free slot we can stop looking for collissions.
351 * Similarly, at remove time, we need to shift all following slots to their
352 * normal slot, until we reach an empty slot.
354 last_clear_slot = slot;
355 slot = (slot + 1) % hash->table_size;
356 while (hash->keys [slot]) {
357 guint hashcode = ((*hash->hash_func)(hash->keys [slot])) % hash->table_size;
359 * We try to move the current element to last_clear_slot, but only if
360 * it brings it closer to its normal position (hashcode)
362 if ((last_clear_slot < slot && (hashcode > slot || hashcode <= last_clear_slot)) ||
363 (last_clear_slot > slot && (hashcode > slot && hashcode <= last_clear_slot))) {
364 mono_g_hash_table_key_store (hash, last_clear_slot, hash->keys [slot]);
365 mono_g_hash_table_value_store (hash, last_clear_slot, hash->values [slot]);
366 hash->keys [slot] = NULL;
367 hash->values [slot] = NULL;
368 last_clear_slot = slot;
371 if (slot == hash->table_size)
378 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
383 g_return_val_if_fail (hash != NULL, 0);
384 g_return_val_if_fail (func != NULL, 0);
386 for (i = 0; i < hash->table_size; i++) {
387 if (hash->keys [i] && (*func)(hash->keys [i], hash->values [i], user_data)) {
388 mono_g_hash_table_remove (hash, hash->keys [i]);
390 /* Retry current slot in case the removal shifted elements */
394 if (hash->in_use < hash->table_size * HASH_TABLE_MIN_LOAD_FACTOR)
400 mono_g_hash_table_destroy (MonoGHashTable *hash)
404 g_return_if_fail (hash != NULL);
407 if (hash->gc_type & MONO_HASH_KEY_GC)
408 mono_gc_deregister_root ((char*)hash->keys);
409 if (hash->gc_type & MONO_HASH_VALUE_GC)
410 mono_gc_deregister_root ((char*)hash->values);
413 for (i = 0; i < hash->table_size; i++) {
414 if (hash->keys [i]) {
415 if (hash->key_destroy_func)
416 (*hash->key_destroy_func)(hash->keys [i]);
417 if (hash->value_destroy_func)
418 (*hash->value_destroy_func)(hash->values [i]);
421 mg_free (hash->keys);
422 mg_free (hash->values);
426 mono_gc_free_fixed (hash);
431 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
434 g_return_if_fail (hash != NULL);
436 if (hash->in_use > (hash->table_size * HASH_TABLE_MAX_LOAD_FACTOR))
439 slot = mono_g_hash_table_find_slot (hash, key);
441 if (hash->keys [slot]) {
443 if (hash->key_destroy_func)
444 (*hash->key_destroy_func)(hash->keys [slot]);
445 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
447 if (hash->value_destroy_func)
448 (*hash->value_destroy_func) (hash->values [slot]);
449 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
451 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
452 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
458 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
460 mono_g_hash_table_insert_replace (h, k, v, FALSE);
464 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
466 mono_g_hash_table_insert_replace (h, k, v, TRUE);
470 mono_g_hash_table_print_stats (MonoGHashTable *hash)
472 int i = 0, chain_size = 0, max_chain_size = 0;
473 gboolean wrapped_around = FALSE;
476 if (hash->keys [i]) {
479 max_chain_size = MAX(max_chain_size, chain_size);
485 if (i == (hash->table_size - 1)) {
486 wrapped_around = TRUE;
492 /* Rehash to a size that can fit the current elements */
493 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", hash->in_use, hash->table_size, max_chain_size);