Revert "[runtime] Get rid of ResumeThread(), use mono_thread_info_resume () instead."
[mono.git] / mono / metadata / sgen-hash-table.c
1 /*
2  * sgen-hash-table.c
3  *
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:
11  * 
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  * 
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.
22  */
23
24 #include "config.h"
25
26 #ifdef HAVE_SGEN_GC
27
28 #include <mono/metadata/sgen-gc.h>
29 #include <mono/metadata/sgen-hash-table.h>
30
31 static void
32 rehash (SgenHashTable *hash_table)
33 {
34         SgenHashTableEntry **old_hash = hash_table->table;
35         guint old_hash_size = hash_table->size;
36         guint i, hash, new_size;
37         SgenHashTableEntry **new_hash;
38         SgenHashTableEntry *entry, *next;
39
40         if (!old_hash) {
41                 sgen_register_fixed_internal_mem_type (hash_table->entry_mem_type,
42                                 sizeof (SgenHashTableEntry*) + sizeof (gpointer) + hash_table->data_size);
43                 new_size = 13;
44         } else {
45                 new_size = g_spaced_primes_closest (hash_table->num_entries);
46         }
47
48         new_hash = sgen_alloc_internal_dynamic (new_size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type, TRUE);
49         for (i = 0; i < old_hash_size; ++i) {
50                 for (entry = old_hash [i]; entry; entry = next) {
51                         hash = hash_table->hash_func (entry->key) % new_size;
52                         next = entry->next;
53                         entry->next = new_hash [hash];
54                         new_hash [hash] = entry;
55                 }
56         }
57         sgen_free_internal_dynamic (old_hash, old_hash_size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type);
58         hash_table->table = new_hash;
59         hash_table->size = new_size;
60 }
61
62 static void
63 rehash_if_necessary (SgenHashTable *hash_table)
64 {
65         if (hash_table->num_entries >= hash_table->size * 2)
66                 rehash (hash_table);
67 }
68
69 static SgenHashTableEntry*
70 lookup (SgenHashTable *hash_table, gpointer key, guint *_hash)
71 {
72         SgenHashTableEntry *entry;
73         guint hash;
74         GEqualFunc equal = hash_table->equal_func;
75
76         if (!hash_table->size)
77                 return NULL;
78
79         hash = hash_table->hash_func (key) % hash_table->size;
80         if (_hash)
81                 *_hash = hash;
82
83         for (entry = hash_table->table [hash]; entry; entry = entry->next) {
84                 if ((equal && equal (entry->key, key)) || (!equal && entry->key == key))
85                         return entry;
86         }
87         return NULL;
88 }
89
90 gpointer
91 sgen_hash_table_lookup (SgenHashTable *hash_table, gpointer key)
92 {
93         SgenHashTableEntry *entry = lookup (hash_table, key, NULL);
94         if (!entry)
95                 return NULL;
96         return entry->data;
97 }
98
99 gboolean
100 sgen_hash_table_replace (SgenHashTable *hash_table, gpointer key, gpointer new_value, gpointer old_value)
101 {
102         guint hash;
103         SgenHashTableEntry *entry;
104
105         rehash_if_necessary (hash_table);
106         entry = lookup (hash_table, key, &hash);
107
108         if (entry) {
109                 if (old_value)
110                         memcpy (old_value, entry->data, hash_table->data_size); 
111                 memcpy (entry->data, new_value, hash_table->data_size);
112                 return FALSE;
113         }
114
115         entry = sgen_alloc_internal (hash_table->entry_mem_type);
116         entry->key = key;
117         memcpy (entry->data, new_value, hash_table->data_size);
118
119         entry->next = hash_table->table [hash];
120         hash_table->table [hash] = entry;
121
122         hash_table->num_entries++;
123
124         return TRUE;
125 }
126
127 gboolean
128 sgen_hash_table_set_value (SgenHashTable *hash_table, gpointer key, gpointer new_value, gpointer old_value)
129 {
130         guint hash;
131         SgenHashTableEntry *entry;
132
133         entry = lookup (hash_table, key, &hash);
134
135         if (entry) {
136                 if (old_value)
137                         memcpy (old_value, entry->data, hash_table->data_size);
138                 memcpy (entry->data, new_value, hash_table->data_size);
139                 return TRUE;
140         }
141
142         return FALSE;
143 }
144
145 gboolean
146 sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key)
147 {
148         guint hash;
149         SgenHashTableEntry *entry;
150
151         entry = lookup (hash_table, old_key, &hash);
152
153         if (entry) {
154                 entry->key = new_key;
155                 return TRUE;
156         }
157
158         return FALSE;
159 }
160
161 gboolean
162 sgen_hash_table_remove (SgenHashTable *hash_table, gpointer key, gpointer data_return)
163 {
164         SgenHashTableEntry *entry, *prev;
165         guint hash;
166         GEqualFunc equal = hash_table->equal_func;
167
168         rehash_if_necessary (hash_table);
169         hash = hash_table->hash_func (key) % hash_table->size;
170
171         prev = NULL;
172         for (entry = hash_table->table [hash]; entry; entry = entry->next) {
173                 if ((equal && equal (entry->key, key)) || (!equal && entry->key == key)) {
174                         if (prev)
175                                 prev->next = entry->next;
176                         else
177                                 hash_table->table [hash] = entry->next;
178
179                         hash_table->num_entries--;
180
181                         if (data_return)
182                                 memcpy (data_return, entry->data, hash_table->data_size);
183
184                         sgen_free_internal (entry, hash_table->entry_mem_type);
185
186                         return TRUE;
187                 }
188                 prev = entry;
189         }
190
191         return FALSE;
192 }
193
194 void
195 sgen_hash_table_clean (SgenHashTable *hash_table)
196 {
197         guint i;
198
199         if (!hash_table->size) {
200                 g_assert (!hash_table->table);
201                 g_assert (!hash_table->num_entries);
202                 return;
203         }
204
205         for (i = 0; i < hash_table->size; ++i) {
206                 SgenHashTableEntry *entry = hash_table->table [i];
207                 while (entry) {
208                         SgenHashTableEntry *next = entry->next;
209                         sgen_free_internal (entry, hash_table->entry_mem_type);
210                         entry = next;
211                 }
212         }
213
214         sgen_free_internal_dynamic (hash_table->table, hash_table->size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type);
215
216         hash_table->table = NULL;
217         hash_table->size = 0;
218         hash_table->num_entries = 0;
219 }
220
221 #endif