Completely replace mono_error_ok with is_ok and make first external_only. (#16217)
[mono-project.git] / mono / metadata / mono-hash.c
blob0323f012c468ab38a7b46ffd4a95a5d5e8219957
1 /**
2 * \file
3 * Hashtable implementation
5 * Author:
6 * Miguel de Icaza (miguel@novell.com)
8 * (C) 2006 Novell, Inc.
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * a copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sublicense, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include <config.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <glib.h>
34 #include "mono-hash.h"
35 #include "mono-hash-internals.h"
36 #include "metadata/gc-internals.h"
38 #include <mono/utils/checked-build.h>
39 #include <mono/utils/mono-threads-coop.h>
40 #include <mono/utils/unlocked.h>
42 gint32 mono_g_hash_table_max_chain_length;
44 struct _MonoGHashTable {
45 GHashFunc hash_func;
46 GEqualFunc key_equal_func;
48 MonoObject **keys;
49 MonoObject **values;
50 int table_size;
51 int in_use;
52 GDestroyNotify value_destroy_func, key_destroy_func;
53 MonoGHashGCType gc_type;
54 MonoGCRootSource source;
55 void *key;
56 const char *msg;
59 #if UNUSED
60 static gboolean
61 test_prime (int x)
63 if ((x & 1) != 0) {
64 int n;
65 for (n = 3; n< (int)sqrt (x); n += 2) {
66 if ((x % n) == 0)
67 return FALSE;
69 return TRUE;
71 // There is only one even prime - 2.
72 return (x == 2);
75 static int
76 calc_prime (int x)
78 int i;
80 for (i = (x & (~1))-1; i< G_MAXINT32; i += 2) {
81 if (test_prime (i))
82 return i;
84 return x;
86 #endif
88 #define HASH_TABLE_MAX_LOAD_FACTOR 0.7f
89 /* We didn't really do compaction before, keep it lenient for now */
90 #define HASH_TABLE_MIN_LOAD_FACTOR 0.05f
91 /* We triple the table size at rehash time, similar with previous implementation */
92 #define HASH_TABLE_RESIZE_RATIO 3
94 static inline void mono_g_hash_table_key_store (MonoGHashTable *hash, int slot, MonoObject* key)
96 MonoObject **key_addr = &hash->keys [slot];
97 if (hash->gc_type & MONO_HASH_KEY_GC)
98 mono_gc_wbarrier_generic_store_internal (key_addr, key);
99 else
100 *key_addr = key;
103 static inline void mono_g_hash_table_value_store (MonoGHashTable *hash, int slot, MonoObject* value)
105 MonoObject **value_addr = &hash->values [slot];
106 if (hash->gc_type & MONO_HASH_VALUE_GC)
107 mono_gc_wbarrier_generic_store_internal (value_addr, value);
108 else
109 *value_addr = value;
112 /* Returns position of key or of an empty slot for it */
113 static inline int mono_g_hash_table_find_slot (MonoGHashTable *hash, const MonoObject *key)
115 guint start = ((*hash->hash_func) (key)) % hash->table_size;
116 guint i = start;
118 if (hash->key_equal_func) {
119 GEqualFunc equal = hash->key_equal_func;
121 while (hash->keys [i] && !(*equal) (hash->keys [i], key)) {
122 i++;
123 if (i == hash->table_size)
124 i = 0;
126 } else {
127 while (hash->keys [i] && hash->keys [i] != key) {
128 i++;
129 if (i == hash->table_size)
130 i = 0;
134 gint32 max_length = UnlockedRead (&mono_g_hash_table_max_chain_length);
135 if (i > start && (i - start) > max_length)
136 UnlockedWrite (&mono_g_hash_table_max_chain_length, i - start);
137 else if (i < start && (hash->table_size - (start - i)) > max_length)
138 UnlockedWrite (&mono_g_hash_table_max_chain_length, hash->table_size - (start - i));
140 return i;
143 MonoGHashTable *
144 mono_g_hash_table_new_type_internal (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type, MonoGCRootSource source, void *key, const char *msg)
146 MONO_REQ_GC_UNSAFE_MODE;
147 MonoGHashTable *hash;
149 if (!hash_func)
150 hash_func = g_direct_hash;
152 hash = g_new0 (MonoGHashTable, 1);
154 hash->hash_func = hash_func;
155 hash->key_equal_func = key_equal_func;
157 hash->table_size = g_spaced_primes_closest (1);
158 hash->keys = g_new0 (MonoObject*, hash->table_size);
159 hash->values = g_new0 (MonoObject*, hash->table_size);
161 hash->gc_type = type;
162 hash->source = source;
163 hash->key = key;
164 hash->msg = msg;
166 if (type > MONO_HASH_KEY_VALUE_GC)
167 g_error ("wrong type for gc hashtable");
169 if (hash->gc_type & MONO_HASH_KEY_GC)
170 mono_gc_register_root_wbarrier ((char*)hash->keys, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->key, hash->msg);
171 if (hash->gc_type & MONO_HASH_VALUE_GC)
172 mono_gc_register_root_wbarrier ((char*)hash->values, sizeof (MonoObject*) * hash->table_size, mono_gc_make_vector_descr (), hash->source, hash->key, hash->msg);
174 return hash;
177 typedef struct {
178 MonoGHashTable *hash;
179 int new_size;
180 MonoObject **keys;
181 MonoObject **values;
182 } RehashData;
184 static void*
185 do_rehash (void *_data)
187 RehashData *data = (RehashData *)_data;
188 MonoGHashTable *hash = data->hash;
189 int current_size, i;
190 MonoObject **old_keys;
191 MonoObject **old_values;
193 current_size = hash->table_size;
194 hash->table_size = data->new_size;
195 old_keys = hash->keys;
196 old_values = hash->values;
197 hash->keys = data->keys;
198 hash->values = data->values;
200 for (i = 0; i < current_size; i++) {
201 if (old_keys [i]) {
202 int slot = mono_g_hash_table_find_slot (hash, old_keys [i]);
203 mono_g_hash_table_key_store (hash, slot, old_keys [i]);
204 mono_g_hash_table_value_store (hash, slot, old_values [i]);
207 return NULL;
210 static void
211 rehash (MonoGHashTable *hash)
213 MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe
215 RehashData data;
216 void *old_keys = hash->keys;
217 void *old_values = hash->values;
219 data.hash = hash;
221 * Rehash to a size that can fit the current elements. Rehash relative to in_use
222 * to allow also for compaction.
224 data.new_size = g_spaced_primes_closest (hash->in_use / HASH_TABLE_MAX_LOAD_FACTOR * HASH_TABLE_RESIZE_RATIO);
225 data.keys = g_new0 (MonoObject*, data.new_size);
226 data.values = g_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->key, 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->key, hash->msg);
233 if (!mono_threads_are_safepoints_enabled ()) {
234 mono_gc_invoke_with_gc_lock (do_rehash, &data);
235 } else {
236 /* We cannot be preempted */
237 do_rehash (&data);
240 if (hash->gc_type & MONO_HASH_KEY_GC)
241 mono_gc_deregister_root ((char*)old_keys);
242 if (hash->gc_type & MONO_HASH_VALUE_GC)
243 mono_gc_deregister_root ((char*)old_values);
245 g_free (old_keys);
246 g_free (old_values);
250 * mono_g_hash_table_size:
252 guint
253 mono_g_hash_table_size (MonoGHashTable *hash)
255 g_return_val_if_fail (hash != NULL, 0);
257 return hash->in_use;
261 * mono_g_hash_table_lookup:
263 gpointer
264 mono_g_hash_table_lookup (MonoGHashTable *hash, gconstpointer key)
266 gpointer orig_key, value;
268 if (mono_g_hash_table_lookup_extended (hash, key, &orig_key, &value))
269 return value;
270 else
271 return NULL;
275 * mono_g_hash_table_lookup_extended:
277 gboolean
278 mono_g_hash_table_lookup_extended (MonoGHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value)
280 int slot;
282 g_return_val_if_fail (hash != NULL, FALSE);
284 slot = mono_g_hash_table_find_slot (hash, (MonoObject*)key);
286 if (hash->keys [slot]) {
287 if (orig_key)
288 *orig_key = hash->keys [slot];
289 if (value)
290 *value = hash->values [slot];
291 return TRUE;
294 return FALSE;
298 * mono_g_hash_table_foreach:
300 void
301 mono_g_hash_table_foreach (MonoGHashTable *hash, GHFunc func, gpointer user_data)
303 int i;
305 g_return_if_fail (hash != NULL);
306 g_return_if_fail (func != NULL);
308 for (i = 0; i < hash->table_size; i++) {
309 if (hash->keys [i])
310 (*func)(hash->keys [i], hash->values [i], user_data);
314 gpointer
315 mono_g_hash_table_find (MonoGHashTable *hash, GHRFunc predicate, gpointer user_data)
317 int i;
319 g_return_val_if_fail (hash != NULL, NULL);
320 g_return_val_if_fail (predicate != NULL, NULL);
322 for (i = 0; i < hash->table_size; i++) {
323 if (hash->keys [i] && (*predicate)(hash->keys [i], hash->values [i], user_data))
324 return hash->values [i];
326 return NULL;
330 * mono_g_hash_table_remove:
332 gboolean
333 mono_g_hash_table_remove (MonoGHashTable *hash, gconstpointer key)
335 int slot, last_clear_slot;
337 g_return_val_if_fail (hash != NULL, FALSE);
338 slot = mono_g_hash_table_find_slot (hash, (MonoObject*)key);
340 if (!hash->keys [slot])
341 return FALSE;
343 if (hash->key_destroy_func)
344 (*hash->key_destroy_func)(hash->keys [slot]);
345 hash->keys [slot] = NULL;
346 if (hash->value_destroy_func)
347 (*hash->value_destroy_func)(hash->values [slot]);
348 hash->values [slot] = NULL;
349 hash->in_use--;
352 * When we insert in the hashtable, if the required position is occupied we
353 * consecutively try out following positions. In order to be able to find
354 * if a key exists or not in the array (without traversing the entire hash)
355 * we maintain the constraint that there can be no free slots between two
356 * entries that are hashed to the same position. This means that, at search
357 * time, when we encounter a free slot we can stop looking for collissions.
358 * Similarly, at remove time, we need to shift all following slots to their
359 * normal slot, until we reach an empty slot.
361 last_clear_slot = slot;
362 slot = (slot + 1) % hash->table_size;
363 while (hash->keys [slot]) {
364 guint hashcode = ((*hash->hash_func)(hash->keys [slot])) % hash->table_size;
366 * We try to move the current element to last_clear_slot, but only if
367 * it brings it closer to its normal position (hashcode)
369 if ((last_clear_slot < slot && (hashcode > slot || hashcode <= last_clear_slot)) ||
370 (last_clear_slot > slot && (hashcode > slot && hashcode <= last_clear_slot))) {
371 mono_g_hash_table_key_store (hash, last_clear_slot, hash->keys [slot]);
372 mono_g_hash_table_value_store (hash, last_clear_slot, hash->values [slot]);
373 hash->keys [slot] = NULL;
374 hash->values [slot] = NULL;
375 last_clear_slot = slot;
377 slot++;
378 if (slot == hash->table_size)
379 slot = 0;
381 return TRUE;
385 * mono_g_hash_table_foreach_remove:
387 guint
388 mono_g_hash_table_foreach_remove (MonoGHashTable *hash, GHRFunc func, gpointer user_data)
390 int i;
391 int count = 0;
393 g_return_val_if_fail (hash != NULL, 0);
394 g_return_val_if_fail (func != NULL, 0);
396 for (i = 0; i < hash->table_size; i++) {
397 if (hash->keys [i] && (*func)(hash->keys [i], hash->values [i], user_data)) {
398 mono_g_hash_table_remove (hash, hash->keys [i]);
399 count++;
400 /* Retry current slot in case the removal shifted elements */
401 i--;
404 if (hash->in_use < hash->table_size * HASH_TABLE_MIN_LOAD_FACTOR)
405 rehash (hash);
406 return count;
410 * mono_g_hash_table_destroy:
412 void
413 mono_g_hash_table_destroy (MonoGHashTable *hash)
415 int i;
417 g_return_if_fail (hash != NULL);
419 if (hash->gc_type & MONO_HASH_KEY_GC)
420 mono_gc_deregister_root ((char*)hash->keys);
421 if (hash->gc_type & MONO_HASH_VALUE_GC)
422 mono_gc_deregister_root ((char*)hash->values);
424 for (i = 0; i < hash->table_size; i++) {
425 if (hash->keys [i]) {
426 if (hash->key_destroy_func)
427 (*hash->key_destroy_func)(hash->keys [i]);
428 if (hash->value_destroy_func)
429 (*hash->value_destroy_func)(hash->values [i]);
432 g_free (hash->keys);
433 g_free (hash->values);
434 g_free (hash);
437 static void
438 mono_g_hash_table_insert_replace (MonoGHashTable *hash, gpointer key, gpointer value, gboolean replace)
440 MONO_REQ_GC_UNSAFE_MODE;
441 int slot;
442 g_return_if_fail (hash != NULL);
444 if (hash->in_use > (hash->table_size * HASH_TABLE_MAX_LOAD_FACTOR))
445 rehash (hash);
447 slot = mono_g_hash_table_find_slot (hash, (MonoObject*)key);
449 if (hash->keys [slot]) {
450 if (replace) {
451 if (hash->key_destroy_func)
452 (*hash->key_destroy_func)(hash->keys [slot]);
453 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
455 if (hash->value_destroy_func)
456 (*hash->value_destroy_func) (hash->values [slot]);
457 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
458 } else {
459 mono_g_hash_table_key_store (hash, slot, (MonoObject*)key);
460 mono_g_hash_table_value_store (hash, slot, (MonoObject*)value);
461 hash->in_use++;
466 * mono_g_hash_table_insert:
468 void
469 mono_g_hash_table_insert (MonoGHashTable *h, gpointer k, gpointer v)
471 MONO_ENTER_GC_UNSAFE;
472 mono_g_hash_table_insert_internal (h, k, v);
473 MONO_EXIT_GC_UNSAFE;
476 void
477 mono_g_hash_table_insert_internal (MonoGHashTable *h, gpointer k, gpointer v)
479 MONO_REQ_GC_UNSAFE_MODE;
480 mono_g_hash_table_insert_replace (h, k, v, FALSE);
484 * mono_g_hash_table_replace:
486 void
487 mono_g_hash_table_replace(MonoGHashTable *h, gpointer k, gpointer v)
489 mono_g_hash_table_insert_replace (h, k, v, TRUE);
492 void
493 mono_g_hash_table_print_stats (MonoGHashTable *hash)
495 int i = 0, chain_size = 0, max_chain_size = 0;
496 gboolean wrapped_around = FALSE;
498 while (TRUE) {
499 if (hash->keys [i]) {
500 chain_size++;
501 } else {
502 max_chain_size = MAX(max_chain_size, chain_size);
503 chain_size = 0;
504 if (wrapped_around)
505 break;
508 if (i == (hash->table_size - 1)) {
509 wrapped_around = TRUE;
510 i = 0;
511 } else {
512 i++;
515 /* Rehash to a size that can fit the current elements */
516 printf ("Size: %d Table Size: %d Max Chain Length: %d\n", hash->in_use, hash->table_size, max_chain_size);