/** * g_hash_table_new_full: * @hash_func: a function to create a hash value from a key. * @key_equal_func: a function to check two keys for equality. * @key_destroy_func: a function to free the memory allocated for the key * used when removing the entry from the #GHashTable or %NULL if you * don't want to supply such a function. * @value_destroy_func: a function to free the memory allocated for the * value used when removing the entry from the #GHashTable or %NULL if * you don't want to supply such a function. * * Creates a new #GHashTable like g_hash_table_new() and allows to specify * functions to free the memory allocated for the key and value that get * called when removing the entry from the #GHashTable. * * Return value: a new #GHashTable. **/ MonoGHashTable* mono_g_hash_table_new_full (GHashFunc hash_func, GEqualFunc key_equal_func, GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func) { MonoGHashTable *hash_table; #if HAVE_BOEHM_GC static gboolean inited = FALSE; if (!inited) { MONO_GC_REGISTER_ROOT (node_free_lists [0]); inited = TRUE; } hash_table = GC_MALLOC (sizeof (MonoGHashTable)); #else hash_table = g_new (MonoGHashTable, 1); #endif hash_table->size = HASH_TABLE_MIN_SIZE; hash_table->nnodes = 0; hash_table->hash_func = hash_func ? hash_func : g_direct_hash; hash_table->key_equal_func = key_equal_func == g_direct_equal ? NULL : key_equal_func; hash_table->key_destroy_func = key_destroy_func; hash_table->value_destroy_func = value_destroy_func; #if HAVE_BOEHM_GC hash_table->nodes = GC_MALLOC (sizeof (MonoGHashNode*) * hash_table->size); #else hash_table->nodes = g_new0 (MonoGHashNode*, hash_table->size); #endif hash_table->gc_type = 0; return hash_table; }
static void add_thread_to_finalize (MonoInternalThread *thread) { mono_finalizer_lock (); if (!threads_to_finalize) MONO_GC_REGISTER_ROOT (threads_to_finalize); threads_to_finalize = mono_mlist_append (threads_to_finalize, (MonoObject*)thread); mono_finalizer_unlock (); }
MonoGHashTable* mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type) { MonoGHashTable *table = mono_g_hash_table_new_full (hash_func, key_equal_func, NULL, NULL); if (type == MONO_HASH_KEY_GC || type == MONO_HASH_KEY_VALUE_GC) g_assert (hash_func); table->gc_type = type; #if defined(HAVE_SGEN_GC) if (type < 0 || type > MONO_HASH_KEY_VALUE_GC) g_error ("wrong type for gc hashtable"); /* * We use a user defined marking function to avoid having to register a GC root for * each hash node. */ if (!hash_descr) hash_descr = mono_gc_make_root_descr_user (mono_g_hash_mark); if (type != MONO_HASH_CONSERVATIVE_GC) mono_gc_register_root_wbarrier ((char*)table, sizeof (MonoGHashTable), hash_descr); #elif defined(HAVE_BOEHM_GC) if (type < 0 || type > MONO_HASH_KEY_VALUE_GC) g_error ("wrong type for gc hashtable"); if (!node_gc_descs [type] && type > MONO_HASH_CONSERVATIVE_GC) { gsize bmap = 0; if (type & MONO_HASH_KEY_GC) bmap |= 1; /* the first field in the node is the key */ if (type & MONO_HASH_VALUE_GC) bmap |= 2; /* the second is the value */ bmap |= 4; /* next */ node_gc_descs [type] = mono_gc_make_descr_from_bitmap (&bmap, 3); MONO_GC_REGISTER_ROOT (node_free_lists [type]); } #endif return table; }