mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-09 12:25:48 +01:00
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:
parent
18fc4b8f02
commit
72ed8191af
@ -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
|
* tests/hash-test.c (second_hash_test): fix memory leak, add a few
|
||||||
extra sanity tests.
|
extra sanity tests.
|
||||||
|
94
glib/ghash.c
94
glib/ghash.c
@ -76,16 +76,12 @@ static GHashNode** g_hash_table_lookup_node (GHashTable *hash_table,
|
|||||||
static GHashNode* g_hash_node_new (gpointer key,
|
static GHashNode* g_hash_node_new (gpointer key,
|
||||||
gpointer value,
|
gpointer value,
|
||||||
guint key_hash);
|
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,
|
static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
|
||||||
GHRFunc func,
|
GHRFunc func,
|
||||||
gpointer user_data,
|
gpointer user_data,
|
||||||
gboolean notify);
|
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)
|
if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
|
||||||
{
|
{
|
||||||
gint i;
|
g_hash_table_remove_all_nodes (hash_table, FALSE);
|
||||||
|
|
||||||
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_free (hash_table->nodes);
|
g_free (hash_table->nodes);
|
||||||
g_slice_free (GHashTable, hash_table);
|
g_slice_free (GHashTable, hash_table);
|
||||||
}
|
}
|
||||||
@ -418,13 +409,31 @@ g_hash_table_remove_node (GHashTable *hash_table,
|
|||||||
|
|
||||||
*node_ptr = node->next;
|
*node_ptr = node->next;
|
||||||
|
|
||||||
g_hash_node_destroy (node,
|
if (notify && hash_table->key_destroy_func)
|
||||||
notify ? hash_table->key_destroy_func : NULL,
|
hash_table->key_destroy_func (node->key);
|
||||||
notify ? hash_table->value_destroy_func : NULL);
|
|
||||||
|
if (notify && hash_table->value_destroy_func)
|
||||||
|
hash_table->value_destroy_func (node->value);
|
||||||
|
|
||||||
|
g_slice_free (GHashNode, node);
|
||||||
|
|
||||||
hash_table->nnodes--;
|
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
|
static gboolean
|
||||||
g_hash_table_remove_internal (GHashTable *hash_table,
|
g_hash_table_remove_internal (GHashTable *hash_table,
|
||||||
gconstpointer key,
|
gconstpointer key,
|
||||||
@ -438,7 +447,7 @@ g_hash_table_remove_internal (GHashTable *hash_table,
|
|||||||
if (*node_ptr == NULL)
|
if (*node_ptr == NULL)
|
||||||
return FALSE;
|
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);
|
G_HASH_TABLE_RESIZE (hash_table);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@ -481,19 +490,9 @@ g_hash_table_remove (GHashTable *hash_table,
|
|||||||
void
|
void
|
||||||
g_hash_table_remove_all (GHashTable *hash_table)
|
g_hash_table_remove_all (GHashTable *hash_table)
|
||||||
{
|
{
|
||||||
guint i;
|
|
||||||
|
|
||||||
g_return_if_fail (hash_table != NULL);
|
g_return_if_fail (hash_table != NULL);
|
||||||
|
|
||||||
for (i = 0; i < hash_table->size; i++)
|
g_hash_table_remove_all_nodes (hash_table, TRUE);
|
||||||
{
|
|
||||||
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_RESIZE (hash_table);
|
G_HASH_TABLE_RESIZE (hash_table);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -526,18 +525,9 @@ g_hash_table_steal (GHashTable *hash_table,
|
|||||||
void
|
void
|
||||||
g_hash_table_steal_all (GHashTable *hash_table)
|
g_hash_table_steal_all (GHashTable *hash_table)
|
||||||
{
|
{
|
||||||
guint i;
|
|
||||||
|
|
||||||
g_return_if_fail (hash_table != NULL);
|
g_return_if_fail (hash_table != NULL);
|
||||||
|
|
||||||
for (i = 0; i < hash_table->size; i++)
|
g_hash_table_remove_all_nodes (hash_table, FALSE);
|
||||||
{
|
|
||||||
g_hash_nodes_destroy (hash_table->nodes[i], NULL, NULL);
|
|
||||||
hash_table->nodes[i] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
hash_table->nnodes = 0;
|
|
||||||
|
|
||||||
G_HASH_TABLE_RESIZE (hash_table);
|
G_HASH_TABLE_RESIZE (hash_table);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -814,35 +804,5 @@ g_hash_node_new (gpointer key,
|
|||||||
return hash_node;
|
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__
|
#define __G_HASH_C__
|
||||||
#include "galiasdef.c"
|
#include "galiasdef.c"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user