create a common function for the many places where all nodes in the table

2007-12-03  Ryan Lortie  <desrt@desrt.ca>

	* glib/ghash.c: create a common function for the many places where all
	nodes in the table are removed (remove_all, steal_all, destroy, unref,
	etc...)



svn path=/trunk/; revision=6026
This commit is contained in:
Ryan Lortie 2007-12-03 21:21:28 +00:00 committed by Ryan Lortie
parent 18fc4b8f02
commit 72ed8191af
2 changed files with 34 additions and 68 deletions

View File

@ -1,4 +1,10 @@
2006-12-03 Ryan Lortie <desrt@desrt.ca>
2007-12-03 Ryan Lortie <desrt@desrt.ca>
* glib/ghash.c: create a common function for the many places where all
nodes in the table are removed (remove_all, steal_all, destroy, unref,
etc...)
2007-12-03 Ryan Lortie <desrt@desrt.ca>
* tests/hash-test.c (second_hash_test): fix memory leak, add a few
extra sanity tests.

View File

@ -76,16 +76,12 @@ static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
static GHashNode* g_hash_node_new (gpointer key,
gpointer value,
guint key_hash);
static void g_hash_node_destroy (GHashNode *hash_node,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func);
static void g_hash_nodes_destroy (GHashNode *hash_node,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func);
static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
GHRFunc func,
gpointer user_data,
gboolean notify);
static void g_hash_table_remove_all_nodes (GHashTable *hash_table,
gboolean notify);
/**
@ -193,12 +189,7 @@ g_hash_table_unref (GHashTable *hash_table)
if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
{
gint i;
for (i = 0; i < hash_table->size; i++)
g_hash_nodes_destroy (hash_table->nodes[i],
hash_table->key_destroy_func,
hash_table->value_destroy_func);
g_hash_table_remove_all_nodes (hash_table, FALSE);
g_free (hash_table->nodes);
g_slice_free (GHashTable, hash_table);
}
@ -418,13 +409,31 @@ g_hash_table_remove_node (GHashTable *hash_table,
*node_ptr = node->next;
g_hash_node_destroy (node,
notify ? hash_table->key_destroy_func : NULL,
notify ? hash_table->value_destroy_func : NULL);
if (notify && hash_table->key_destroy_func)
hash_table->key_destroy_func (node->key);
if (notify && hash_table->value_destroy_func)
hash_table->value_destroy_func (node->value);
g_slice_free (GHashNode, node);
hash_table->nnodes--;
}
static void
g_hash_table_remove_all_nodes (GHashTable *hash_table,
gboolean notify)
{
GHashNode **node_ptr;
int i;
for (i = 0; i < hash_table->size; i++)
for (node_ptr = &hash_table->nodes[i]; *node_ptr != NULL;)
g_hash_table_remove_node (hash_table, &node_ptr, notify);
hash_table->nnodes = 0;
}
static gboolean
g_hash_table_remove_internal (GHashTable *hash_table,
gconstpointer key,
@ -438,7 +447,7 @@ g_hash_table_remove_internal (GHashTable *hash_table,
if (*node_ptr == NULL)
return FALSE;
g_hash_table_remove_node (hash_table, &node_ptr, FALSE);
g_hash_table_remove_node (hash_table, &node_ptr, notify);
G_HASH_TABLE_RESIZE (hash_table);
return TRUE;
@ -481,19 +490,9 @@ g_hash_table_remove (GHashTable *hash_table,
void
g_hash_table_remove_all (GHashTable *hash_table)
{
guint i;
g_return_if_fail (hash_table != NULL);
for (i = 0; i < hash_table->size; i++)
{
g_hash_nodes_destroy (hash_table->nodes[i],
hash_table->key_destroy_func,
hash_table->value_destroy_func);
hash_table->nodes[i] = NULL;
}
hash_table->nnodes = 0;
g_hash_table_remove_all_nodes (hash_table, TRUE);
G_HASH_TABLE_RESIZE (hash_table);
}
@ -526,18 +525,9 @@ g_hash_table_steal (GHashTable *hash_table,
void
g_hash_table_steal_all (GHashTable *hash_table)
{
guint i;
g_return_if_fail (hash_table != NULL);
for (i = 0; i < hash_table->size; i++)
{
g_hash_nodes_destroy (hash_table->nodes[i], NULL, NULL);
hash_table->nodes[i] = NULL;
}
hash_table->nnodes = 0;
g_hash_table_remove_all_nodes (hash_table, FALSE);
G_HASH_TABLE_RESIZE (hash_table);
}
@ -814,35 +804,5 @@ g_hash_node_new (gpointer key,
return hash_node;
}
static void
g_hash_node_destroy (GHashNode *hash_node,
GDestroyNotify key_destroy_func,
GDestroyNotify value_destroy_func)
{
if (key_destroy_func)
key_destroy_func (hash_node->key);
if (value_destroy_func)
value_destroy_func (hash_node->value);
g_slice_free (GHashNode, hash_node);
}
static void
g_hash_nodes_destroy (GHashNode *hash_node,
GFreeFunc key_destroy_func,
GFreeFunc value_destroy_func)
{
while (hash_node)
{
GHashNode *next = hash_node->next;
if (key_destroy_func)
key_destroy_func (hash_node->key);
if (value_destroy_func)
value_destroy_func (hash_node->value);
g_slice_free (GHashNode, hash_node);
hash_node = next;
}
}
#define __G_HASH_C__
#include "galiasdef.c"