Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 250 lines (206 sloc) 6.815 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/*
* sgen-hash-table.c
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "config.h"

#ifdef HAVE_SGEN_GC

#include <mono/metadata/sgen-gc.h>
#include <mono/metadata/sgen-hash-table.h>
#include <mono/utils/mono-counters.h>

#ifdef HEAVY_STATISTICS
static long long stat_lookups;
static long long stat_lookup_iterations;
static long long stat_lookup_max_iterations;
#endif

static void
rehash (SgenHashTable *hash_table)
{
SgenHashTableEntry **old_hash = hash_table->table;
guint old_hash_size = hash_table->size;
guint i, hash, new_size;
SgenHashTableEntry **new_hash;
SgenHashTableEntry *entry, *next;

if (!old_hash) {
sgen_register_fixed_internal_mem_type (hash_table->entry_mem_type,
sizeof (SgenHashTableEntry*) + sizeof (gpointer) + hash_table->data_size);
new_size = 13;
} else {
new_size = g_spaced_primes_closest (hash_table->num_entries);
}

new_hash = sgen_alloc_internal_dynamic (new_size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type, TRUE);
for (i = 0; i < old_hash_size; ++i) {
for (entry = old_hash [i]; entry; entry = next) {
hash = hash_table->hash_func (entry->key) % new_size;
next = entry->next;
entry->next = new_hash [hash];
new_hash [hash] = entry;
}
}
sgen_free_internal_dynamic (old_hash, old_hash_size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type);
hash_table->table = new_hash;
hash_table->size = new_size;
}

static void
rehash_if_necessary (SgenHashTable *hash_table)
{
if (hash_table->num_entries >= hash_table->size * 2)
rehash (hash_table);

SGEN_ASSERT (1, hash_table->size, "rehash guarantees size > 0");
}

static SgenHashTableEntry*
lookup (SgenHashTable *hash_table, gpointer key, guint *_hash)
{
SgenHashTableEntry *entry;
guint hash;
GEqualFunc equal = hash_table->equal_func;
#ifdef HEAVY_STATISTICS
long long iterations = 0;
++stat_lookups;
#endif

if (!hash_table->size)
return NULL;

hash = hash_table->hash_func (key) % hash_table->size;
if (_hash)
*_hash = hash;

for (entry = hash_table->table [hash]; entry; entry = entry->next) {
#ifdef HEAVY_STATISTICS
++stat_lookup_iterations;
++iterations;
if (iterations > stat_lookup_max_iterations)
stat_lookup_max_iterations = iterations;
#endif
if ((equal && equal (entry->key, key)) || (!equal && entry->key == key))
return entry;
}
return NULL;
}

gpointer
sgen_hash_table_lookup (SgenHashTable *hash_table, gpointer key)
{
SgenHashTableEntry *entry = lookup (hash_table, key, NULL);
if (!entry)
return NULL;
return entry->data;
}

gboolean
sgen_hash_table_replace (SgenHashTable *hash_table, gpointer key, gpointer new_value, gpointer old_value)
{
guint hash;
SgenHashTableEntry *entry;

rehash_if_necessary (hash_table);
entry = lookup (hash_table, key, &hash);

if (entry) {
if (old_value)
memcpy (old_value, entry->data, hash_table->data_size);
memcpy (entry->data, new_value, hash_table->data_size);
return FALSE;
}

entry = sgen_alloc_internal (hash_table->entry_mem_type);
entry->key = key;
memcpy (entry->data, new_value, hash_table->data_size);

entry->next = hash_table->table [hash];
hash_table->table [hash] = entry;

hash_table->num_entries++;

return TRUE;
}

gboolean
sgen_hash_table_set_value (SgenHashTable *hash_table, gpointer key, gpointer new_value, gpointer old_value)
{
guint hash;
SgenHashTableEntry *entry;

entry = lookup (hash_table, key, &hash);

if (entry) {
if (old_value)
memcpy (old_value, entry->data, hash_table->data_size);
memcpy (entry->data, new_value, hash_table->data_size);
return TRUE;
}

return FALSE;
}

gboolean
sgen_hash_table_set_key (SgenHashTable *hash_table, gpointer old_key, gpointer new_key)
{
guint hash;
SgenHashTableEntry *entry;

entry = lookup (hash_table, old_key, &hash);

if (entry) {
entry->key = new_key;
return TRUE;
}

return FALSE;
}

gboolean
sgen_hash_table_remove (SgenHashTable *hash_table, gpointer key, gpointer data_return)
{
SgenHashTableEntry *entry, *prev;
guint hash;
GEqualFunc equal = hash_table->equal_func;

rehash_if_necessary (hash_table);
hash = hash_table->hash_func (key) % hash_table->size;

prev = NULL;
for (entry = hash_table->table [hash]; entry; entry = entry->next) {
if ((equal && equal (entry->key, key)) || (!equal && entry->key == key)) {
if (prev)
prev->next = entry->next;
else
hash_table->table [hash] = entry->next;

hash_table->num_entries--;

if (data_return)
memcpy (data_return, entry->data, hash_table->data_size);

sgen_free_internal (entry, hash_table->entry_mem_type);

return TRUE;
}
prev = entry;
}

return FALSE;
}

void
sgen_hash_table_clean (SgenHashTable *hash_table)
{
guint i;

if (!hash_table->size) {
SGEN_ASSERT (1, !hash_table->table, "clean should reset hash_table->table");
SGEN_ASSERT (1, !hash_table->num_entries, "clean should reset hash_table->num_entries");
return;
}

for (i = 0; i < hash_table->size; ++i) {
SgenHashTableEntry *entry = hash_table->table [i];
while (entry) {
SgenHashTableEntry *next = entry->next;
sgen_free_internal (entry, hash_table->entry_mem_type);
entry = next;
}
}

sgen_free_internal_dynamic (hash_table->table, hash_table->size * sizeof (SgenHashTableEntry*), hash_table->table_mem_type);

hash_table->table = NULL;
hash_table->size = 0;
hash_table->num_entries = 0;
}

void
sgen_init_hash_table (void)
{
#ifdef HEAVY_STATISTICS
mono_counters_register ("Hash table lookups", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_lookups);
mono_counters_register ("Hash table lookup iterations", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_lookup_iterations);
mono_counters_register ("Hash table lookup max iterations", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_lookup_max_iterations);
#endif
}

#endif
Something went wrong with that request. Please try again.