From 96a9f80300b7794475a5451a60a07555ea3526be Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 6 Jan 2003 01:08:14 +0000 Subject: 2003-01-05 Havoc Pennington * bus/connection.c: implement routines for handling connections, first thing is keeping a list of owned services on each connection and setting up watches etc. * bus/services.c: implement a mapping from service names to lists of connections * dbus/dbus-hash.c: add DBUS_HASH_POINTER * dbus/dbus-threads.c (dbus_static_mutex_lock): add functions to use static mutexes for global data * dbus/dbus-connection.c (dbus_connection_set_data): add new collection of functions to set/get application-specific data on the DBusConnection. --- dbus/dbus-hash.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 6 deletions(-) (limited to 'dbus/dbus-hash.c') diff --git a/dbus/dbus-hash.c b/dbus/dbus-hash.c index 9cec1c1d..8bac61a8 100644 --- a/dbus/dbus-hash.c +++ b/dbus/dbus-hash.c @@ -220,7 +220,7 @@ typedef struct int n_entries_on_init; /**< used to detect table resize since initialization */ } DBusRealHashIter; -static DBusHashEntry* find_int_function (DBusHashTable *table, +static DBusHashEntry* find_direct_function (DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashEntry ***bucket); @@ -310,7 +310,8 @@ _dbus_hash_table_new (DBusHashType type, switch (table->key_type) { case DBUS_HASH_INT: - table->find_function = find_int_function; + case DBUS_HASH_POINTER: + table->find_function = find_direct_function; break; case DBUS_HASH_STRING: table->find_function = find_string_function; @@ -808,10 +809,10 @@ find_string_function (DBusHashTable *table, } static DBusHashEntry* -find_int_function (DBusHashTable *table, - void *key, - dbus_bool_t create_if_not_found, - DBusHashEntry ***bucket) +find_direct_function (DBusHashTable *table, + void *key, + dbus_bool_t create_if_not_found, + DBusHashEntry ***bucket) { DBusHashEntry *entry; unsigned int idx; @@ -940,6 +941,7 @@ rebuild_table (DBusHashTable *table) idx = string_hash (entry->key) & table->mask; break; case DBUS_HASH_INT: + case DBUS_HASH_POINTER: idx = RANDOM_INDEX (table, entry->key); break; default: @@ -1010,6 +1012,31 @@ _dbus_hash_table_lookup_int (DBusHashTable *table, return NULL; } +/** + * Looks up the value for a given integer in a hash table + * of type #DBUS_HASH_POINTER. Returns %NULL if the value + * is not present. (A not-present entry is indistinguishable + * from an entry with a value of %NULL.) + * @param table the hash table. + * @param key the integer to look up. + * @returns the value of the hash entry. + */ +void* +_dbus_hash_table_lookup_pointer (DBusHashTable *table, + void *key) +{ + DBusHashEntry *entry; + + _dbus_assert (table->key_type == DBUS_HASH_POINTER); + + entry = (* table->find_function) (table, key, FALSE, NULL); + + if (entry) + return entry->value; + else + return NULL; +} + /** * Removes the hash entry for the given key. If no hash entry * for the key exists, does nothing. @@ -1066,6 +1093,35 @@ _dbus_hash_table_remove_int (DBusHashTable *table, return FALSE; } +/** + * Removes the hash entry for the given key. If no hash entry + * for the key exists, does nothing. + * + * @param table the hash table. + * @param key the hash key. + * @returns #TRUE if the entry existed + */ +dbus_bool_t +_dbus_hash_table_remove_pointer (DBusHashTable *table, + void *key) +{ + DBusHashEntry *entry; + DBusHashEntry **bucket; + + _dbus_assert (table->key_type == DBUS_HASH_POINTER); + + entry = (* table->find_function) (table, key, FALSE, &bucket); + + if (entry) + { + remove_entry (table, bucket, entry); + return TRUE; + } + else + return FALSE; +} + + /** * Creates a hash entry with the given key and value. * The key and value are not copied; they are stored @@ -1148,6 +1204,47 @@ _dbus_hash_table_insert_int (DBusHashTable *table, return TRUE; } +/** + * Creates a hash entry with the given key and value. + * The key and value are not copied; they are stored + * in the hash table by reference. If an entry with the + * given key already exists, the previous key and value + * are overwritten (and freed if the hash table has + * a key_free_function and/or value_free_function). + * + * Returns #FALSE if memory for the new hash entry + * can't be allocated. + * + * @param table the hash table. + * @param key the hash entry key. + * @param value the hash entry value. + */ +dbus_bool_t +_dbus_hash_table_insert_pointer (DBusHashTable *table, + void *key, + void *value) +{ + DBusHashEntry *entry; + + _dbus_assert (table->key_type == DBUS_HASH_POINTER); + + entry = (* table->find_function) (table, key, TRUE, NULL); + + if (entry == NULL) + return FALSE; /* no memory */ + + if (table->free_key_function && entry->key != key) + (* table->free_key_function) (entry->key); + + if (table->free_value_function && entry->value != value) + (* table->free_value_function) (entry->value); + + entry->key = key; + entry->value = value; + + return TRUE; +} + /** * Gets the number of hash entries in a hash table. * -- cgit