Skip to content

Commit

Permalink
2003-08-26 Zoltan Varga <vargaz@freemail.hu>
Browse files Browse the repository at this point in the history
	* mono-hash.h mono-hash.c (mono_g_hash_table_remap): New function used
	to remap hashtable values during a copying collection. Not used at the
	moment.
	* mono-hash.c (g_hash_node_new): Restructured to avoid the global lock
	in the common case.
	* mono-hash.c (mono_g_hash_table_new_full): Enable the optimization of
	the key function even if the client supplied g_direct_equal instead of
	NULL.

svn path=/trunk/mono/; revision=17624
  • Loading branch information
vargaz committed Aug 26, 2003
1 parent 9d7bca1 commit 61c39af
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 35 deletions.
11 changes: 11 additions & 0 deletions mono/utils/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
2003-08-26 Zoltan Varga <vargaz@freemail.hu>

* mono-hash.h mono-hash.c (mono_g_hash_table_remap): New function used
to remap hashtable values during a copying collection. Not used at the
moment.
* mono-hash.c (g_hash_node_new): Restructured to avoid the global lock
in the common case.
* mono-hash.c (mono_g_hash_table_new_full): Enable the optimization of
the key function even if the client supplied g_direct_equal instead of
NULL.

2003-08-22 Duncan Mak <duncan@ximian.com>

* strtod.c: 64-bit related fixes from Laurent Morichetti
Expand Down
104 changes: 69 additions & 35 deletions mono/utils/mono-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mono_g_hash_table_new_full (GHashFunc hash_func,
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;
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
Expand Down Expand Up @@ -281,6 +281,50 @@ mono_g_hash_table_lookup_extended (MonoGHashTable *hash_table,
return FALSE;
}

static inline MonoGHashNode*
g_hash_node_new (gpointer key,
gpointer value)
{
MonoGHashNode *hash_node = NULL;

#if HAVE_BOEHM_GC
if (node_free_list) {
G_LOCK (g_hash_global);

if (node_free_list) {
hash_node = node_free_list;
node_free_list = node_free_list->next;
}
G_UNLOCK (g_hash_global);
}
if (!hash_node)
hash_node = GC_MALLOC (sizeof (MonoGHashNode));
#else
G_LOCK (g_hash_global);
if (node_free_list)
{
hash_node = node_free_list;
node_free_list = node_free_list->next;
}
else
{
if (!node_mem_chunk)
node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
sizeof (MonoGHashNode),
1024, G_ALLOC_ONLY);

hash_node = g_chunk_new (MonoGHashNode, node_mem_chunk);
}
G_UNLOCK (g_hash_global);
#endif

hash_node->key = key;
hash_node->value = value;
hash_node->next = NULL;

return hash_node;
}

/**
* g_hash_table_insert:
* @hash_table: a #GHashTable.
Expand Down Expand Up @@ -589,6 +633,30 @@ mono_g_hash_table_size (MonoGHashTable *hash_table)
return hash_table->nnodes;
}

/**
* mono_g_hash_table_remap:
*
* Calls the given function for each key-value pair in the hash table,
* and replaces the value stored in the hash table by the value returned by
* the function.
*
**/
void
mono_g_hash_table_remap (MonoGHashTable *hash_table,
MonoGRemapperFunc func,
gpointer user_data)
{
MonoGHashNode *node;
gint i;

g_return_if_fail (hash_table != NULL);
g_return_if_fail (func != NULL);

for (i = 0; i < hash_table->size; i++)
for (node = hash_table->nodes[i]; node; node = node->next)
node->value = (* func) (node->key, node->value, user_data);
}

static void
g_hash_table_resize (MonoGHashTable *hash_table)
{
Expand Down Expand Up @@ -627,40 +695,6 @@ g_hash_table_resize (MonoGHashTable *hash_table)
hash_table->size = new_size;
}

static MonoGHashNode*
g_hash_node_new (gpointer key,
gpointer value)
{
MonoGHashNode *hash_node;

G_LOCK (g_hash_global);
if (node_free_list)
{
hash_node = node_free_list;
node_free_list = node_free_list->next;
}
else
{
#if HAVE_BOEHM_GC
hash_node = GC_MALLOC (sizeof (MonoGHashNode));
#else
if (!node_mem_chunk)
node_mem_chunk = g_mem_chunk_new ("hash node mem chunk",
sizeof (MonoGHashNode),
1024, G_ALLOC_ONLY);

hash_node = g_chunk_new (MonoGHashNode, node_mem_chunk);
#endif
}
G_UNLOCK (g_hash_global);

hash_node->key = key;
hash_node->value = value;
hash_node->next = NULL;

return hash_node;
}

static void
g_hash_node_destroy (MonoGHashNode *hash_node,
GDestroyNotify key_destroy_func,
Expand Down
7 changes: 7 additions & 0 deletions mono/utils/mono-hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ G_BEGIN_DECLS

typedef struct _MonoGHashTable MonoGHashTable;

typedef gpointer (*MonoGRemapperFunc) (gpointer key, gpointer value,
gpointer user_data);

/* Hash tables
*/
MonoGHashTable* mono_g_hash_table_new (GHashFunc hash_func,
Expand Down Expand Up @@ -75,6 +78,10 @@ guint mono_g_hash_table_foreach_steal (MonoGHashTable *hash_table,
gpointer user_data);
guint mono_g_hash_table_size (MonoGHashTable *hash_table);

void mono_g_hash_table_remap (MonoGHashTable *hash_table,
MonoGRemapperFunc func,
gpointer user_data);

G_END_DECLS

#endif /* __MONO_G_HASH_H__ */
Expand Down

0 comments on commit 61c39af

Please sign in to comment.