Consistently include <config.h> in all C source files and never in header files.
[platform/upstream/dbus.git] / dbus / dbus-hash.c
index f454781..80a494a 100644 (file)
@@ -1,5 +1,5 @@
-/* -*- 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.
@@ -7,10 +7,10 @@
  * 
  * 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
@@ -24,7 +24,7 @@
  * 
  * 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
  *
  */
 /* 
@@ -74,6 +74,7 @@
  * accordance with the terms specified in this license.
  */
 
+#include <config.h>
 #include "dbus-hash.h"
 #include "dbus-internals.h"
 #include "dbus-mempool.h"
@@ -231,13 +232,17 @@ static DBusHashEntry* find_string_function      (DBusHashTable          *table,
                                                  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,
@@ -330,7 +335,9 @@ _dbus_hash_table_new (DBusHashType     type,
       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");
@@ -348,11 +355,14 @@ _dbus_hash_table_new (DBusHashType     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;
 }
 
 /**
@@ -413,6 +423,22 @@ _dbus_hash_table_unref (DBusHashTable *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)
 {
@@ -693,6 +719,7 @@ _dbus_hash_iter_get_string_key (DBusHashIter *iter)
   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
@@ -710,6 +737,7 @@ _dbus_hash_iter_get_two_strings_key (DBusHashIter *iter)
 
   return real->entry->key;
 }
+#endif /* DBUS_BUILD_TESTS */
 
 /**
  * A low-level but efficient interface for manipulating the hash
@@ -846,6 +874,7 @@ string_hash (const char *str)
   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.
  */
@@ -864,7 +893,9 @@ two_strings_hash (const char *str)
   
   return h;
 }
+#endif /* DBUS_BUILD_TESTS */
 
+/** Key comparison function */
 typedef int (* KeyCompareFunc) (const void *key_a, const void *key_b);
 
 static DBusHashEntry*
@@ -924,6 +955,7 @@ find_string_function (DBusHashTable        *table,
                                 preallocated);
 }
 
+#ifdef DBUS_BUILD_TESTS
 static int
 two_strings_cmp (const char *a,
                  const char *b)
@@ -941,7 +973,9 @@ two_strings_cmp (const char *a,
 
   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,
@@ -957,6 +991,7 @@ find_two_strings_function (DBusHashTable        *table,
                                 (KeyCompareFunc) two_strings_cmp, create_if_not_found, bucket,
                                 preallocated);
 }
+#endif /* DBUS_BUILD_TESTS */
 
 static DBusHashEntry*
 find_direct_function (DBusHashTable        *table,
@@ -1073,7 +1108,12 @@ rebuild_table (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:
@@ -1123,6 +1163,7 @@ _dbus_hash_table_lookup_string (DBusHashTable *table,
     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
@@ -1147,6 +1188,7 @@ _dbus_hash_table_lookup_two_strings (DBusHashTable *table,
   else
     return NULL;
 }
+#endif /* DBUS_BUILD_TESTS */
 
 /**
  * Looks up the value for a given integer in a hash table
@@ -1254,6 +1296,7 @@ _dbus_hash_table_remove_string (DBusHashTable *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.
@@ -1281,6 +1324,7 @@ _dbus_hash_table_remove_two_strings (DBusHashTable *table,
   else
     return FALSE;
 }
+#endif /* DBUS_BUILD_TESTS */
 
 /**
  * Removes the hash entry for the given key. If no hash entry
@@ -1403,6 +1447,7 @@ _dbus_hash_table_insert_string (DBusHashTable *table,
   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
@@ -1423,19 +1468,27 @@ _dbus_hash_table_insert_two_strings (DBusHashTable *table,
                                      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.
@@ -1811,8 +1864,8 @@ _dbus_hash_test (void)
       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);
@@ -1832,9 +1885,9 @@ _dbus_hash_test (void)
       _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;
     }