g_hash_table_new_full(): create hash tables with a ref count of 1.

Tue Nov 22 14:04:26 2005  Tim Janik  <timj@imendio.com>

        * glib/ghash.h:
        * glib/ghash.c:
        g_hash_table_new_full(): create hash tables with a ref count of 1.
        g_hash_table_ref(): atomically ref_count+=1
        g_hash_table_unref(): atomically ref_count-=1, destroys hash table
        when refcount reaches 0.
        g_hash_table_destroy(): just destroy keys and values, unref by 1.
        g_hash_table_insert():
        g_hash_table_replace(): assert ref_count>0.

        * glib/gatomic.h:
        * glib/gatomic.c: added 'volatile' qualifier to all atomic pointer and
        integer pointers.
This commit is contained in:
Tim Janik 2005-11-22 13:16:58 +00:00 committed by Tim Janik
parent 79209e5d56
commit 3e847a090c
8 changed files with 243 additions and 135 deletions

View File

@ -1,3 +1,19 @@
Tue Nov 22 14:04:26 2005 Tim Janik <timj@imendio.com>
* glib/ghash.h:
* glib/ghash.c:
g_hash_table_new_full(): create hash tables with a ref count of 1.
g_hash_table_ref(): atomically ref_count+=1
g_hash_table_unref(): atomically ref_count-=1, destroys hash table
when refcount reaches 0.
g_hash_table_destroy(): just destroy keys and values, unref by 1.
g_hash_table_insert():
g_hash_table_replace(): assert ref_count>0.
* glib/gatomic.h:
* glib/gatomic.c: added 'volatile' qualifier to all atomic pointer and
integer pointers.
2005-11-20 Behdad Esfahbod <behdad@gnome.org> 2005-11-20 Behdad Esfahbod <behdad@gnome.org>
* glib/guniprop.c (g_unichar_get_mirror_char): Remove unused * glib/guniprop.c (g_unichar_get_mirror_char): Remove unused

View File

@ -1,3 +1,19 @@
Tue Nov 22 14:04:26 2005 Tim Janik <timj@imendio.com>
* glib/ghash.h:
* glib/ghash.c:
g_hash_table_new_full(): create hash tables with a ref count of 1.
g_hash_table_ref(): atomically ref_count+=1
g_hash_table_unref(): atomically ref_count-=1, destroys hash table
when refcount reaches 0.
g_hash_table_destroy(): just destroy keys and values, unref by 1.
g_hash_table_insert():
g_hash_table_replace(): assert ref_count>0.
* glib/gatomic.h:
* glib/gatomic.c: added 'volatile' qualifier to all atomic pointer and
integer pointers.
2005-11-20 Behdad Esfahbod <behdad@gnome.org> 2005-11-20 Behdad Esfahbod <behdad@gnome.org>
* glib/guniprop.c (g_unichar_get_mirror_char): Remove unused * glib/guniprop.c (g_unichar_get_mirror_char): Remove unused

View File

@ -1,3 +1,19 @@
Tue Nov 22 14:04:26 2005 Tim Janik <timj@imendio.com>
* glib/ghash.h:
* glib/ghash.c:
g_hash_table_new_full(): create hash tables with a ref count of 1.
g_hash_table_ref(): atomically ref_count+=1
g_hash_table_unref(): atomically ref_count-=1, destroys hash table
when refcount reaches 0.
g_hash_table_destroy(): just destroy keys and values, unref by 1.
g_hash_table_insert():
g_hash_table_replace(): assert ref_count>0.
* glib/gatomic.h:
* glib/gatomic.c: added 'volatile' qualifier to all atomic pointer and
integer pointers.
2005-11-20 Behdad Esfahbod <behdad@gnome.org> 2005-11-20 Behdad Esfahbod <behdad@gnome.org>
* glib/guniprop.c (g_unichar_get_mirror_char): Remove unused * glib/guniprop.c (g_unichar_get_mirror_char): Remove unused

View File

@ -31,7 +31,7 @@
/* Adapted from CVS version 1.10 of glibc's sysdeps/i386/i486/bits/atomic.h /* Adapted from CVS version 1.10 of glibc's sysdeps/i386/i486/bits/atomic.h
*/ */
gint gint
g_atomic_int_exchange_and_add (gint *atomic, g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result; gint result;
@ -43,7 +43,7 @@ g_atomic_int_exchange_and_add (gint *atomic,
} }
void void
g_atomic_int_add (gint *atomic, g_atomic_int_add (volatile gint *atomic,
gint val) gint val)
{ {
__asm__ __volatile__ ("lock; addl %1,%0" __asm__ __volatile__ ("lock; addl %1,%0"
@ -52,7 +52,7 @@ g_atomic_int_add (gint *atomic,
} }
gboolean gboolean
g_atomic_int_compare_and_exchange (gint *atomic, g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval) gint newval)
{ {
@ -70,7 +70,7 @@ g_atomic_int_compare_and_exchange (gint *atomic,
* arguments and calling the former function */ * arguments and calling the former function */
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -98,7 +98,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
# if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */ # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -111,7 +111,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
} }
# elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */ # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -157,7 +157,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
}) })
# if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */ # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -183,7 +183,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
} }
# elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */ # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -215,7 +215,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
/* Adapted from CVS version 1.9 of glibc's sysdeps/x86_64/bits/atomic.h /* Adapted from CVS version 1.9 of glibc's sysdeps/x86_64/bits/atomic.h
*/ */
gint gint
g_atomic_int_exchange_and_add (gint *atomic, g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result; gint result;
@ -227,7 +227,7 @@ g_atomic_int_exchange_and_add (gint *atomic,
} }
void void
g_atomic_int_add (gint *atomic, g_atomic_int_add (volatile gint *atomic,
gint val) gint val)
{ {
__asm__ __volatile__ ("lock; addl %1,%0" __asm__ __volatile__ ("lock; addl %1,%0"
@ -236,7 +236,7 @@ g_atomic_int_add (gint *atomic,
} }
gboolean gboolean
g_atomic_int_compare_and_exchange (gint *atomic, g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval) gint newval)
{ {
@ -250,7 +250,7 @@ g_atomic_int_compare_and_exchange (gint *atomic,
} }
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -272,7 +272,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
/* Non-optimizing compile bails on the following two asm statements /* Non-optimizing compile bails on the following two asm statements
* for reasons unknown to the author */ * for reasons unknown to the author */
gint gint
g_atomic_int_exchange_and_add (gint *atomic, g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result, temp; gint result, temp;
@ -288,7 +288,7 @@ g_atomic_int_exchange_and_add (gint *atomic,
/* The same as above, to save a function call repeated here */ /* The same as above, to save a function call repeated here */
void void
g_atomic_int_add (gint *atomic, g_atomic_int_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result, temp; gint result, temp;
@ -302,7 +302,7 @@ g_atomic_int_add (gint *atomic,
} }
# else /* !__OPTIMIZE__ */ # else /* !__OPTIMIZE__ */
gint gint
g_atomic_int_exchange_and_add (gint *atomic, g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result; gint result;
@ -314,7 +314,7 @@ g_atomic_int_exchange_and_add (gint *atomic,
} }
void void
g_atomic_int_add (gint *atomic, g_atomic_int_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result; gint result;
@ -326,7 +326,7 @@ g_atomic_int_add (gint *atomic,
# if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */ # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
gboolean gboolean
g_atomic_int_compare_and_exchange (gint *atomic, g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval) gint newval)
{ {
@ -345,7 +345,7 @@ g_atomic_int_compare_and_exchange (gint *atomic,
} }
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -364,7 +364,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
} }
# elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */ # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean gboolean
g_atomic_int_compare_and_exchange (gint *atomic, g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval) gint newval)
{ {
@ -384,7 +384,7 @@ g_atomic_int_compare_and_exchange (gint *atomic,
} }
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -411,7 +411,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
/* Adapted from CVS version 1.8 of glibc's sysdeps/ia64/bits/atomic.h /* Adapted from CVS version 1.8 of glibc's sysdeps/ia64/bits/atomic.h
*/ */
gint gint
g_atomic_int_exchange_and_add (gint *atomic, g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val) gint val)
{ {
return __sync_fetch_and_add_si (atomic, val); return __sync_fetch_and_add_si (atomic, val);
@ -425,7 +425,7 @@ g_atomic_int_add (gint *atomic,
} }
gboolean gboolean
g_atomic_int_compare_and_exchange (gint *atomic, g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval) gint newval)
{ {
@ -433,7 +433,7 @@ g_atomic_int_compare_and_exchange (gint *atomic,
} }
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -456,7 +456,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
# if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */ # if GLIB_SIZEOF_VOID_P == 4 /* 32-bit system */
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -468,7 +468,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
} }
# elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */ # elif GLIB_SIZEOF_VOID_P == 8 /* 64-bit system */
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -496,31 +496,31 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
#ifdef DEFINE_WITH_WIN32_INTERLOCKED #ifdef DEFINE_WITH_WIN32_INTERLOCKED
# include <windows.h> # include <windows.h>
gint32 gint32
g_atomic_int_exchange_and_add (gint32 *atomic, g_atomic_int_exchange_and_add (volatile gint32 *atomic,
gint32 val) gint32 val)
{ {
return InterlockedExchangeAdd (atomic, val); return InterlockedExchangeAdd (atomic, val);
} }
void void
g_atomic_int_add (gint32 *atomic, g_atomic_int_add (volatile gint32 *atomic,
gint32 val) gint32 val)
{ {
InterlockedExchangeAdd (atomic, val); InterlockedExchangeAdd (atomic, val);
} }
gboolean gboolean
g_atomic_int_compare_and_exchange (gint32 *atomic, g_atomic_int_compare_and_exchange (volatile gint32 *atomic,
gint32 oldval, gint32 oldval,
gint32 newval) gint32 newval)
{ {
return (guint32)InterlockedCompareExchange ((PVOID*)atomic, return (guint32) InterlockedCompareExchange ((PVOID*)atomic,
(PVOID)newval, (PVOID)newval,
(PVOID)oldval) == oldval; (PVOID)oldval) == oldval;
} }
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -537,7 +537,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
static GMutex *g_atomic_mutex; static GMutex *g_atomic_mutex;
gint gint
g_atomic_int_exchange_and_add (gint *atomic, g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result; gint result;
@ -552,7 +552,7 @@ g_atomic_int_exchange_and_add (gint *atomic,
void void
g_atomic_int_add (gint *atomic, g_atomic_int_add (volatile gint *atomic,
gint val) gint val)
{ {
g_mutex_lock (g_atomic_mutex); g_mutex_lock (g_atomic_mutex);
@ -561,7 +561,7 @@ g_atomic_int_add (gint *atomic,
} }
gboolean gboolean
g_atomic_int_compare_and_exchange (gint *atomic, g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval) gint newval)
{ {
@ -581,7 +581,7 @@ g_atomic_int_compare_and_exchange (gint *atomic,
} }
gboolean gboolean
g_atomic_pointer_compare_and_exchange (gpointer *atomic, g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval) gpointer newval)
{ {
@ -602,7 +602,7 @@ g_atomic_pointer_compare_and_exchange (gpointer *atomic,
#ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
gint gint
g_atomic_int_get (gint *atomic) g_atomic_int_get (volatile gint *atomic)
{ {
gint result; gint result;
@ -614,7 +614,7 @@ g_atomic_int_get (gint *atomic)
} }
gpointer gpointer
g_atomic_pointer_get (gpointer *atomic) g_atomic_pointer_get (volatile gpointer *atomic)
{ {
gpointer result; gpointer result;
@ -627,7 +627,7 @@ g_atomic_pointer_get (gpointer *atomic)
#endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */ #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
#elif defined (G_ATOMIC_OP_MEMORY_BARRIER_NEEDED) #elif defined (G_ATOMIC_OP_MEMORY_BARRIER_NEEDED)
gint gint
g_atomic_int_get (gint *atomic) g_atomic_int_get (volatile gint *atomic)
{ {
gint result = *atomic; gint result = *atomic;
@ -637,7 +637,7 @@ g_atomic_int_get (gint *atomic)
} }
gpointer gpointer
g_atomic_pointer_get (gpointer *atomic) g_atomic_pointer_get (volatile gpointer *atomic)
{ {
gpointer result = *atomic; gpointer result = *atomic;
@ -649,7 +649,7 @@ g_atomic_pointer_get (gpointer *atomic)
#ifdef ATOMIC_INT_CMP_XCHG #ifdef ATOMIC_INT_CMP_XCHG
gboolean gboolean
g_atomic_int_compare_and_exchange (gint *atomic, g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval) gint newval)
{ {
@ -657,7 +657,7 @@ g_atomic_int_compare_and_exchange (gint *atomic,
} }
gint gint
g_atomic_int_exchange_and_add (gint *atomic, g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result; gint result;
@ -669,7 +669,7 @@ g_atomic_int_exchange_and_add (gint *atomic,
} }
void void
g_atomic_int_add (gint *atomic, g_atomic_int_add (volatile gint *atomic,
gint val) gint val)
{ {
gint result; gint result;
@ -689,13 +689,13 @@ _g_atomic_thread_init (void)
#ifndef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED #ifndef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
gint gint
(g_atomic_int_get) (gint *atomic) (g_atomic_int_get) (volatile gint *atomic)
{ {
return g_atomic_int_get (atomic); return g_atomic_int_get (atomic);
} }
gpointer gpointer
(g_atomic_pointer_get) (gpointer *atomic) (g_atomic_pointer_get) (volatile gpointer *atomic)
{ {
return g_atomic_pointer_get (atomic); return g_atomic_pointer_get (atomic);
} }

View File

@ -34,19 +34,19 @@
G_BEGIN_DECLS G_BEGIN_DECLS
gint g_atomic_int_exchange_and_add (gint *atomic, gint g_atomic_int_exchange_and_add (volatile gint *atomic,
gint val); gint val);
void g_atomic_int_add (gint *atomic, void g_atomic_int_add (volatile gint *atomic,
gint val); gint val);
gboolean g_atomic_int_compare_and_exchange (gint *atomic, gboolean g_atomic_int_compare_and_exchange (volatile gint *atomic,
gint oldval, gint oldval,
gint newval); gint newval);
gboolean g_atomic_pointer_compare_and_exchange (gpointer *atomic, gboolean g_atomic_pointer_compare_and_exchange (volatile gpointer *atomic,
gpointer oldval, gpointer oldval,
gpointer newval); gpointer newval);
gint g_atomic_int_get (gint *atomic); gint g_atomic_int_get (volatile gint *atomic);
gpointer g_atomic_pointer_get (gpointer *atomic); gpointer g_atomic_pointer_get (volatile gpointer *atomic);
#ifndef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED #ifndef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
# define g_atomic_int_get(atomic) (*(atomic)) # define g_atomic_int_get(atomic) (*(atomic))

View File

@ -54,6 +54,7 @@ struct _GHashTable
GHashNode **nodes; GHashNode **nodes;
GHashFunc hash_func; GHashFunc hash_func;
GEqualFunc key_equal_func; GEqualFunc key_equal_func;
volatile guint ref_count;
GDestroyNotify key_destroy_func; GDestroyNotify key_destroy_func;
GDestroyNotify value_destroy_func; GDestroyNotify value_destroy_func;
}; };
@ -98,7 +99,7 @@ static guint g_hash_table_foreach_remove_or_steal (GHashTable *hash_table,
* directly in a similar fashion to g_direct_equal(), but without the * directly in a similar fashion to g_direct_equal(), but without the
* overhead of a function call. * overhead of a function call.
* *
* Creates a new #GHashTable. * Creates a new #GHashTable with a reference count of 1.
* *
* Return value: a new #GHashTable. * Return value: a new #GHashTable.
**/ **/
@ -121,9 +122,9 @@ g_hash_table_new (GHashFunc hash_func,
* value used when removing the entry from the #GHashTable or %NULL if * value used when removing the entry from the #GHashTable or %NULL if
* you don't want to supply such a function. * you don't want to supply such a function.
* *
* Creates a new #GHashTable like g_hash_table_new() and allows to specify * Creates a new #GHashTable like g_hash_table_new() with a reference count
* functions to free the memory allocated for the key and value that get * of 1 and allows to specify functions to free the memory allocated for the
* called when removing the entry from the #GHashTable. * key and value that get called when removing the entry from the #GHashTable.
* *
* Return value: a new #GHashTable. * Return value: a new #GHashTable.
**/ **/
@ -134,32 +135,77 @@ g_hash_table_new_full (GHashFunc hash_func,
GDestroyNotify value_destroy_func) GDestroyNotify value_destroy_func)
{ {
GHashTable *hash_table; GHashTable *hash_table;
guint i;
hash_table = g_slice_new (GHashTable); hash_table = g_slice_new (GHashTable);
hash_table->size = HASH_TABLE_MIN_SIZE; hash_table->size = HASH_TABLE_MIN_SIZE;
hash_table->nnodes = 0; hash_table->nnodes = 0;
hash_table->hash_func = hash_func ? hash_func : g_direct_hash; 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;
hash_table->ref_count = 1;
hash_table->key_destroy_func = key_destroy_func; hash_table->key_destroy_func = key_destroy_func;
hash_table->value_destroy_func = value_destroy_func; hash_table->value_destroy_func = value_destroy_func;
hash_table->nodes = g_new (GHashNode*, hash_table->size); hash_table->nodes = g_new0 (GHashNode*, hash_table->size);
for (i = 0; i < hash_table->size; i++)
hash_table->nodes[i] = NULL;
return hash_table; return hash_table;
} }
/**
* g_hash_table_ref:
* @hash_table: a valid #GHashTable.
*
* Atomically increments the reference count of @hash_table by one.
* This function is MT-safe and may be called from any thread.
*
* Return value: the passed in #GHashTable.
**/
GHashTable*
g_hash_table_ref (GHashTable *hash_table)
{
g_return_val_if_fail (hash_table != NULL, NULL);
g_return_val_if_fail (hash_table->ref_count > 0, hash_table);
g_atomic_int_add (&hash_table->ref_count, 1);
return hash_table;
}
/**
* g_hash_table_unref:
* @hash_table: a valid #GHashTable.
*
* Atomically decrements the reference count of @hash_table by one.
* If the reference count drops to 0, all keys and values will be
* destroyed, and all memory allocated by the hash table is released.
* This function is MT-safe and may be called from any thread.
**/
void
g_hash_table_unref (GHashTable *hash_table)
{
g_return_if_fail (hash_table != NULL);
g_return_if_fail (hash_table->ref_count > 0);
if (g_atomic_int_exchange_and_add (&hash_table->ref_count, -1) - 1 == 0)
{
guint 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_free (hash_table->nodes);
g_slice_free (GHashTable, hash_table);
}
}
/** /**
* g_hash_table_destroy: * g_hash_table_destroy:
* @hash_table: a #GHashTable. * @hash_table: a #GHashTable.
* *
* Destroys the #GHashTable. If keys and/or values are dynamically * Destroys all keys and values in the #GHashTable and decrements it's
* allocated, you should either free them first or create the #GHashTable * reference count by 1. If keys and/or values are dynamically allocated,
* using g_hash_table_new_full(). In the latter case the destroy functions * you should either free them first or create the #GHashTable with destroy
* you supplied will be called on all keys and values before destroying * notifiers using g_hash_table_new_full(). In the latter case the destroy
* the #GHashTable. * functions you supplied will be called on all keys and values during the
* destruction phase.
**/ **/
void void
g_hash_table_destroy (GHashTable *hash_table) g_hash_table_destroy (GHashTable *hash_table)
@ -167,14 +213,19 @@ g_hash_table_destroy (GHashTable *hash_table)
guint i; guint i;
g_return_if_fail (hash_table != NULL); g_return_if_fail (hash_table != NULL);
g_return_if_fail (hash_table->ref_count > 0);
for (i = 0; i < hash_table->size; i++) for (i = 0; i < hash_table->size; i++)
{
g_hash_nodes_destroy (hash_table->nodes[i], g_hash_nodes_destroy (hash_table->nodes[i],
hash_table->key_destroy_func, hash_table->key_destroy_func,
hash_table->value_destroy_func); hash_table->value_destroy_func);
hash_table->nodes[i] = NULL;
}
hash_table->nnodes = 0;
hash_table->size = HASH_TABLE_MIN_SIZE;
g_free (hash_table->nodes); g_hash_table_unref (hash_table);
g_slice_free (GHashTable, hash_table);
} }
static inline GHashNode** static inline GHashNode**
@ -286,6 +337,7 @@ g_hash_table_insert (GHashTable *hash_table,
GHashNode **node; GHashNode **node;
g_return_if_fail (hash_table != NULL); g_return_if_fail (hash_table != NULL);
g_return_if_fail (hash_table->ref_count > 0);
node = g_hash_table_lookup_node (hash_table, key); node = g_hash_table_lookup_node (hash_table, key);
@ -334,6 +386,7 @@ g_hash_table_replace (GHashTable *hash_table,
GHashNode **node; GHashNode **node;
g_return_if_fail (hash_table != NULL); g_return_if_fail (hash_table != NULL);
g_return_if_fail (hash_table->ref_count > 0);
node = g_hash_table_lookup_node (hash_table, key); node = g_hash_table_lookup_node (hash_table, key);
@ -686,5 +739,6 @@ g_hash_nodes_destroy (GHashNode *hash_node,
} }
} }
#define __G_HASH_C__ #define __G_HASH_C__
#include "galiasdef.c" #include "galiasdef.c"

View File

@ -76,6 +76,10 @@ guint g_hash_table_foreach_steal (GHashTable *hash_table,
gpointer user_data); gpointer user_data);
guint g_hash_table_size (GHashTable *hash_table); guint g_hash_table_size (GHashTable *hash_table);
/* keeping hash tables alive */
GHashTable* g_hash_table_ref (GHashTable *hash_table);
void g_hash_table_unref (GHashTable *hash_table);
#ifndef G_DISABLE_DEPRECATED #ifndef G_DISABLE_DEPRECATED
/* The following two functions are deprecated and will be removed in /* The following two functions are deprecated and will be removed in

View File

@ -287,6 +287,8 @@ g_mkstemp_utf8
#if IN_HEADER(__G_HASH_H__) #if IN_HEADER(__G_HASH_H__)
#if IN_FILE(__G_HASH_C__) #if IN_FILE(__G_HASH_C__)
g_hash_table_destroy g_hash_table_destroy
g_hash_table_unref
g_hash_table_ref
g_hash_table_find g_hash_table_find
g_hash_table_foreach g_hash_table_foreach
g_hash_table_foreach_remove g_hash_table_foreach_remove