4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <mono/metadata/sgen-gc.h>
31 rehash (SgenHashTable *hash_table)
33 SgenHashTableEntry **old_hash = hash_table->table;
34 guint old_hash_size = hash_table->size;
35 guint i, hash, new_size;
36 SgenHashTableEntry **new_hash;
37 SgenHashTableEntry *entry, *next;
40 mono_sgen_register_fixed_internal_mem_type (hash_table->entry_mem_type,
41 sizeof (SgenHashTableEntry*) + sizeof (gpointer) + hash_table->data_size);
44 new_size = g_spaced_primes_closest (hash_table->num_entries);
47 new_hash = mono_sgen_alloc_internal_dynamic (new_size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type);
48 for (i = 0; i < old_hash_size; ++i) {
49 for (entry = old_hash [i]; entry; entry = next) {
50 hash = hash_table->hash_func (entry->key) % new_size;
52 entry->next = new_hash [hash];
53 new_hash [hash] = entry;
56 mono_sgen_free_internal_dynamic (old_hash, old_hash_size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type);
57 hash_table->table = new_hash;
58 hash_table->size = new_size;
62 rehash_if_necessary (SgenHashTable *hash_table)
64 if (hash_table->num_entries >= hash_table->size * 2)
68 static SgenHashTableEntry*
69 lookup (SgenHashTable *hash_table, gpointer key, guint *_hash)
71 SgenHashTableEntry *entry;
73 GEqualFunc equal = hash_table->equal_func;
75 if (!hash_table->size)
78 hash = hash_table->hash_func (key) % hash_table->size;
82 for (entry = hash_table->table [hash]; entry; entry = entry->next) {
83 if ((equal && equal (entry->key, key)) || (!equal && entry->key == key))
90 mono_sgen_hash_table_lookup (SgenHashTable *hash_table, gpointer key)
92 SgenHashTableEntry *entry = lookup (hash_table, key, NULL);
99 mono_sgen_hash_table_replace (SgenHashTable *hash_table, gpointer key, gpointer data)
102 SgenHashTableEntry *entry;
104 rehash_if_necessary (hash_table);
105 entry = lookup (hash_table, key, &hash);
108 memcpy (entry->data, data, hash_table->data_size);
112 entry = mono_sgen_alloc_internal (hash_table->entry_mem_type);
114 memcpy (entry->data, data, hash_table->data_size);
116 entry->next = hash_table->table [hash];
117 hash_table->table [hash] = entry;
119 hash_table->num_entries++;
125 mono_sgen_hash_table_set_value (SgenHashTable *hash_table, gpointer key, gpointer data)
128 SgenHashTableEntry *entry;
130 entry = lookup (hash_table, key, &hash);
133 memcpy (entry->data, data, hash_table->data_size);
141 mono_sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key)
144 SgenHashTableEntry *entry;
146 entry = lookup (hash_table, old_key, &hash);
149 entry->key = new_key;
157 mono_sgen_hash_table_remove (SgenHashTable *hash_table, gpointer key, gpointer data_return)
159 SgenHashTableEntry *entry, *prev;
161 GEqualFunc equal = hash_table->equal_func;
163 rehash_if_necessary (hash_table);
164 hash = hash_table->hash_func (key) % hash_table->size;
167 for (entry = hash_table->table [hash]; entry; entry = entry->next) {
168 if ((equal && equal (entry->key, key)) || (!equal && entry->key == key)) {
170 prev->next = entry->next;
172 hash_table->table [hash] = entry->next;
174 hash_table->num_entries--;
177 memcpy (data_return, entry->data, hash_table->data_size);
179 mono_sgen_free_internal (entry, hash_table->entry_mem_type);
190 mono_sgen_hash_table_clean (SgenHashTable *hash_table)
194 if (!hash_table->size) {
195 g_assert (!hash_table->table);
196 g_assert (!hash_table->num_entries);
200 for (i = 0; i < hash_table->size; ++i) {
201 SgenHashTableEntry *entry = hash_table->table [i];
203 SgenHashTableEntry *next = entry->next;
204 mono_sgen_free_internal (entry, hash_table->entry_mem_type);
209 mono_sgen_free_internal_dynamic (hash_table->table, hash_table->size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type);
211 hash_table->table = NULL;
212 hash_table->size = 0;
213 hash_table->num_entries = 0;