* MT safe
*/
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "config.h"
+#include "galias.h"
#include "glib.h"
gboolean notify);
+#ifndef DISABLE_MEM_POOLS
G_LOCK_DEFINE_STATIC (g_hash_global);
static GMemChunk *node_mem_chunk = NULL;
static GHashNode *node_free_list = NULL;
+#endif
/**
* g_hash_table_new:
* Hash values are used to determine where keys are stored within the
* #GHashTable data structure. The g_direct_hash(), g_int_hash() and
* g_str_hash() functions are provided for some common types of keys.
- * If hash_func is NULL, g_direct_hash() is used.
+ * If hash_func is %NULL, g_direct_hash() is used.
* @key_equal_func: a function to check two keys for equality. This is
* used when looking up keys in the #GHashTable. The g_direct_equal(),
* g_int_equal() and g_str_equal() functions are provided for the most
- * common types of keys. If @key_equal_func is NULL, keys are compared
+ * common types of keys. If @key_equal_func is %NULL, keys are compared
* directly in a similar fashion to g_direct_equal(), but without the
* overhead of a function call.
*
* @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
+ * 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
+ * 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
* @hash_table: a #GHashTable.
* @key: the key to look up.
*
- * Looks up a key in a #GHashTable.
+ * Looks up a key in a #GHashTable. Note that this function cannot
+ * distinguish between a key that is not present and one which is present
+ * and has the value %NULL. If you need this distinction, use
+ * g_hash_table_lookup_extended().
*
- * Return value: the associated value, or NULL if the key is not found.
+ * Return value: the associated value, or %NULL if the key is not found.
**/
gpointer
g_hash_table_lookup (GHashTable *hash_table,
* @value: returns the value associated with the key.
*
* Looks up a key in the #GHashTable, returning the original key and the
- * associated value and a gboolean which is TRUE if the key was found. This
+ * associated value and a #gboolean which is %TRUE if the key was found. This
* is useful if you need to free the memory allocated for the original key,
* for example before calling g_hash_table_remove().
*
- * Return value: #TRUE if the key was found in the #GHashTable.
+ * Return value: %TRUE if the key was found in the #GHashTable.
**/
gboolean
g_hash_table_lookup_extended (GHashTable *hash_table,
* Inserts a new key and value into a #GHashTable.
*
* If the key already exists in the #GHashTable its current value is replaced
- * with the new value. If you supplied a value_destroy_func when creating the
+ * with the new value. If you supplied a @value_destroy_func when creating the
* #GHashTable, the old value is freed using that function. If you supplied
- * a key_destroy_func when creating the #GHashTable, the passed key is freed
+ * a @key_destroy_func when creating the #GHashTable, the passed key is freed
* using that function.
**/
void
* Inserts a new key and value into a #GHashTable similar to
* g_hash_table_insert(). The difference is that if the key already exists
* in the #GHashTable, it gets replaced by the new key. If you supplied a
- * value_destroy_func when creating the #GHashTable, the old value is freed
- * using that function. If you supplied a key_destroy_func when creating the
+ * @value_destroy_func when creating the #GHashTable, the old value is freed
+ * using that function. If you supplied a @key_destroy_func when creating the
* #GHashTable, the old key is freed using that function.
**/
void
* Removes a key and its associated value from a #GHashTable.
*
* If the #GHashTable was created using g_hash_table_new_full(), the
- * key and value are freed using the supplied destroy_functions, otherwise
+ * key and value are freed using the supplied destroy functions, otherwise
* you have to make sure that any dynamically allocated values are freed
* yourself.
*
- * Return value: #TRUE if the key was found and removed from the #GHashTable.
+ * Return value: %TRUE if the key was found and removed from the #GHashTable.
**/
gboolean
g_hash_table_remove (GHashTable *hash_table,
* Removes a key and its associated value from a #GHashTable without
* calling the key and value destroy functions.
*
- * Return value: #TRUE if the key was found and removed from the #GHashTable.
+ * Return value: %TRUE if the key was found and removed from the #GHashTable.
**/
gboolean
g_hash_table_steal (GHashTable *hash_table,
* @user_data: user data to pass to the function.
*
* Calls the given function for each key/value pair in the #GHashTable.
- * If the function returns TRUE, then the key/value pair is removed from the
+ * If the function returns %TRUE, then the key/value pair is removed from the
* #GHashTable. If you supplied key or value destroy functions when creating
* the #GHashTable, they are used to free the memory allocated for the removed
* keys and values.
* @user_data: user data to pass to the function.
*
* Calls the given function for each key/value pair in the #GHashTable.
- * If the function returns TRUE, then the key/value pair is removed from the
+ * If the function returns %TRUE, then the key/value pair is removed from the
* #GHashTable, but no key or value destroy functions are called.
*
* Return value: the number of key/value pairs removed.
* @func: the function to call for each key/value pair.
* @user_data: user data to pass to the function.
*
- * Calls the given function for each of the key/value pairs in the #GHashTable.
- * The function is passed the key and value of each pair, and the given
- * @user_data parameter.
+ * Calls the given function for each of the key/value pairs in the
+ * #GHashTable. The function is passed the key and value of each
+ * pair, and the given @user_data parameter. The hash table may not
+ * be modified while iterating over it (you can't add/remove
+ * items). To remove all items matching a predicate, use
+ * g_hash_table_remove().
**/
void
g_hash_table_foreach (GHashTable *hash_table,
}
/**
+ * g_hash_table_find:
+ * @hash_table: a #GHashTable.
+ * @predicate: function to test the key/value pairs for a certain property.
+ * @user_data: user data to pass to the function.
+ *
+ * Calls the given function for key/value pairs in the #GHashTable until
+ * @predicate returns %TRUE. The function is passed the key and value of
+ * each pair, and the given @user_data parameter. The hash table may not
+ * be modified while iterating over it (you can't add/remove items).
+ *
+ * Return value: The value of the first key/value pair is returned, for which
+ * func evaluates to %TRUE. If no pair with the requested property is found,
+ * %NULL is returned.
+ *
+ * Since: 2.4
+ **/
+gpointer
+g_hash_table_find (GHashTable *hash_table,
+ GHRFunc predicate,
+ gpointer user_data)
+{
+ GHashNode *node;
+ gint i;
+
+ g_return_val_if_fail (hash_table != NULL, NULL);
+ g_return_val_if_fail (predicate != NULL, NULL);
+
+ for (i = 0; i < hash_table->size; i++)
+ for (node = hash_table->nodes[i]; node; node = node->next)
+ if (predicate (node->key, node->value, user_data))
+ return node->value;
+ return NULL;
+}
+
+/**
* g_hash_table_size:
* @hash_table: a #GHashTable.
*
gint new_size;
gint i;
- new_size = CLAMP(g_spaced_primes_closest (hash_table->nnodes),
- HASH_TABLE_MIN_SIZE,
- HASH_TABLE_MAX_SIZE);
+ new_size = g_spaced_primes_closest (hash_table->nnodes);
+ new_size = CLAMP (new_size, HASH_TABLE_MIN_SIZE, HASH_TABLE_MAX_SIZE);
+
new_nodes = g_new0 (GHashNode*, new_size);
for (i = 0; i < hash_table->size; i++)
{
GHashNode *hash_node;
+#ifdef DISABLE_MEM_POOLS
+ hash_node = g_new (GHashNode, 1);
+#else
G_LOCK (g_hash_global);
if (node_free_list)
{
hash_node = g_chunk_new (GHashNode, node_mem_chunk);
}
G_UNLOCK (g_hash_global);
+#endif
hash_node->key = key;
hash_node->value = value;
hash_node->value = NULL;
#endif /* ENABLE_GC_FRIENDLY */
+#ifdef DISABLE_MEM_POOLS
+ g_free (hash_node);
+#else
G_LOCK (g_hash_global);
hash_node->next = node_free_list;
node_free_list = hash_node;
G_UNLOCK (g_hash_global);
+#endif
}
static void
GFreeFunc key_destroy_func,
GFreeFunc value_destroy_func)
{
+#ifdef DISABLE_MEM_POOLS
+ 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_free (hash_node);
+ hash_node = next;
+ }
+#else
if (hash_node)
{
GHashNode *node = hash_node;
node = node->next;
}
- if (key_destroy_func)
- key_destroy_func (node->key);
- if (value_destroy_func)
- value_destroy_func (node->value);
+ if (key_destroy_func)
+ key_destroy_func (node->key);
+ if (value_destroy_func)
+ value_destroy_func (node->value);
#ifdef ENABLE_GC_FRIENDLY
node->key = NULL;
node_free_list = hash_node;
G_UNLOCK (g_hash_global);
}
+#endif
}