-/* -*- mode: C; c-file-style: "gnu" -*- */
-/* dbus-hash.c Generic hash table utility (internal to D-BUS implementation)
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/* dbus-hash.c Generic hash table utility (internal to D-Bus implementation)
*
* Copyright (C) 2002 Red Hat, Inc.
* Copyright (c) 1991-1993 The Regents of the University of California.
*
* Hash table implementation based on generic/tclHash.c from the Tcl
* source code. The original Tcl license applies to portions of the
- * code from tclHash.c; the Tcl license follows this standad D-BUS
+ * code from tclHash.c; the Tcl license follows this standad D-Bus
* license information.
*
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
/*
* accordance with the terms specified in this license.
*/
+#include <config.h>
#include "dbus-hash.h"
#include "dbus-internals.h"
#include "dbus-mempool.h"
dbus_bool_t create_if_not_found,
DBusHashEntry ***bucket,
DBusPreallocatedHash *preallocated);
+#ifdef DBUS_BUILD_TESTS
static DBusHashEntry* find_two_strings_function (DBusHashTable *table,
void *key,
dbus_bool_t create_if_not_found,
DBusHashEntry ***bucket,
DBusPreallocatedHash *preallocated);
+#endif
static unsigned int string_hash (const char *str);
+#ifdef DBUS_BUILD_TESTS
static unsigned int two_strings_hash (const char *str);
+#endif
static void rebuild_table (DBusHashTable *table);
static DBusHashEntry* alloc_entry (DBusHashTable *table);
static void remove_entry (DBusHashTable *table,
table->find_function = find_string_function;
break;
case DBUS_HASH_TWO_STRINGS:
+#ifdef DBUS_BUILD_TESTS
table->find_function = find_two_strings_function;
+#endif
break;
default:
_dbus_assert_not_reached ("Unknown hash table type");
* Increments the reference count for a hash table.
*
* @param table the hash table to add a reference to.
+ * @returns the hash table.
*/
-void
+DBusHashTable *
_dbus_hash_table_ref (DBusHashTable *table)
{
table->refcount += 1;
+
+ return table;
}
/**
}
}
+/**
+ * Removed all entries from a hash table.
+ *
+ * @param table the hash table to remove all entries from.
+ */
+void
+_dbus_hash_table_remove_all (DBusHashTable *table)
+{
+ DBusHashIter iter;
+ _dbus_hash_iter_init (table, &iter);
+ while (_dbus_hash_iter_next (&iter))
+ {
+ _dbus_hash_iter_remove_entry(&iter);
+ }
+}
+
static DBusHashEntry*
alloc_entry (DBusHashTable *table)
{
return real->entry->key;
}
+#ifdef DBUS_BUILD_TESTS
/**
* Gets the key for the current entry.
* Only works for hash tables of type #DBUS_HASH_TWO_STRINGS
return real->entry->key;
}
+#endif /* DBUS_BUILD_TESTS */
/**
* A low-level but efficient interface for manipulating the hash
return h;
}
+#ifdef DBUS_BUILD_TESTS
/* This hashes a memory block with two nul-terminated strings
* in it, used in dbus-object-registry.c at the moment.
*/
return h;
}
+#endif /* DBUS_BUILD_TESTS */
+/** Key comparison function */
typedef int (* KeyCompareFunc) (const void *key_a, const void *key_b);
static DBusHashEntry*
preallocated);
}
+#ifdef DBUS_BUILD_TESTS
static int
two_strings_cmp (const char *a,
const char *b)
return strcmp (a + len_a + 1, b + len_b + 1);
}
+#endif
+#ifdef DBUS_BUILD_TESTS
static DBusHashEntry*
find_two_strings_function (DBusHashTable *table,
void *key,
(KeyCompareFunc) two_strings_cmp, create_if_not_found, bucket,
preallocated);
}
+#endif /* DBUS_BUILD_TESTS */
static DBusHashEntry*
find_direct_function (DBusHashTable *table,
idx = string_hash (entry->key) & table->mask;
break;
case DBUS_HASH_TWO_STRINGS:
+#ifdef DBUS_BUILD_TESTS
idx = two_strings_hash (entry->key) & table->mask;
+#else
+ idx = 0;
+ _dbus_assert_not_reached ("two-strings is not enabled");
+#endif
break;
case DBUS_HASH_INT:
case DBUS_HASH_ULONG:
return NULL;
}
+#ifdef DBUS_BUILD_TESTS
/**
* Looks up the value for a given string in a hash table
* of type #DBUS_HASH_TWO_STRINGS. Returns %NULL if the value
else
return NULL;
}
+#endif /* DBUS_BUILD_TESTS */
/**
* Looks up the value for a given integer in a hash table
return FALSE;
}
+#ifdef DBUS_BUILD_TESTS
/**
* Removes the hash entry for the given key. If no hash entry
* for the key exists, does nothing.
else
return FALSE;
}
+#endif /* DBUS_BUILD_TESTS */
/**
* Removes the hash entry for the given key. If no hash entry
return TRUE;
}
+#ifdef DBUS_BUILD_TESTS
/**
* Creates a hash entry with the given key and value.
* The key and value are not copied; they are stored
char *key,
void *value)
{
- DBusPreallocatedHash *preallocated;
-
+ DBusHashEntry *entry;
+
_dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
+
+ entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
- preallocated = _dbus_hash_table_preallocate_entry (table);
- if (preallocated == NULL)
- return FALSE;
+ if (entry == NULL)
+ return FALSE; /* no memory */
- _dbus_hash_table_insert_string_preallocated (table, preallocated,
- key, value);
+ 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;
}
+#endif /* DBUS_BUILD_TESTS */
/**
* Creates a hash entry with the given key and value.
if (value == NULL)
goto out;
- if (!_dbus_hash_table_insert_string (table4,
- key, value))
+ if (!_dbus_hash_table_insert_two_strings (table4,
+ key, value))
goto out;
_dbus_assert (count_entries (table1) == i + 1);
_dbus_assert (value != NULL);
_dbus_assert (strcmp (value, keys[i]) == 0);
- value = _dbus_hash_table_lookup_ulong (table4, i);
+ value = _dbus_hash_table_lookup_two_strings (table4, keys[i]);
_dbus_assert (value != NULL);
- _dbus_assert (strcmp (value, keys[i]) == 0);
+ _dbus_assert (strcmp (value, "Value!") == 0);
++i;
}