1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-hash.c Generic hash table utility (internal to D-Bus implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
5 * Copyright (c) 1991-1993 The Regents of the University of California.
6 * Copyright (c) 1994 Sun Microsystems, Inc.
8 * Hash table implementation based on generic/tclHash.c from the Tcl
9 * source code. The original Tcl license applies to portions of the
10 * code from tclHash.c; the Tcl license follows this standad D-Bus
11 * license information.
13 * Licensed under the Academic Free License version 2.1
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * The following copyright applies to code from the Tcl distribution.
33 * Copyright (c) 1991-1993 The Regents of the University of California.
34 * Copyright (c) 1994 Sun Microsystems, Inc.
36 * This software is copyrighted by the Regents of the University of
37 * California, Sun Microsystems, Inc., Scriptics Corporation, and
38 * other parties. The following terms apply to all files associated
39 * with the software unless explicitly disclaimed in individual files.
41 * The authors hereby grant permission to use, copy, modify,
42 * distribute, and license this software and its documentation for any
43 * purpose, provided that existing copyright notices are retained in
44 * all copies and that this notice is included verbatim in any
45 * distributions. No written agreement, license, or royalty fee is
46 * required for any of the authorized uses. Modifications to this
47 * software may be copyrighted by their authors and need not follow
48 * the licensing terms described here, provided that the new terms are
49 * clearly indicated on the first page of each file where they apply.
51 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
52 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
53 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
54 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
55 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
58 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
59 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
60 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
61 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
62 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
64 * GOVERNMENT USE: If you are acquiring this software on behalf of the
65 * U.S. government, the Government shall have only "Restricted Rights"
66 * in the software and related documentation as defined in the Federal
67 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
68 * are acquiring the software on behalf of the Department of Defense,
69 * the software shall be classified as "Commercial Computer Software"
70 * and the Government shall have only "Restricted Rights" as defined
71 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
72 * foregoing, the authors grant the U.S. Government and others acting
73 * in its behalf permission to use and distribute the software in
74 * accordance with the terms specified in this license.
77 #include "dbus-hash.h"
78 #include "dbus-internals.h"
79 #include "dbus-mempool.h"
82 * @defgroup DBusHashTable Hash table
83 * @ingroup DBusInternals
84 * @brief DBusHashTable data structure
86 * Types and functions related to DBusHashTable.
90 * @defgroup DBusHashTableInternals Hash table implementation details
91 * @ingroup DBusInternals
92 * @brief DBusHashTable implementation details
94 * The guts of DBusHashTable.
100 * When there are this many entries per bucket, on average, rebuild
101 * the hash table to make it larger.
103 #define REBUILD_MULTIPLIER 3
106 * Takes a preliminary integer hash value and produces an index into a
107 * hash tables bucket list. The idea is to make it so that
108 * preliminary values that are arbitrarily similar will end up in
109 * different buckets. The hash function was taken from a
110 * random-number generator. (This is used to hash integers.)
112 * The down_shift drops off the high bits of the hash index, and
113 * decreases as we increase the number of hash buckets (to keep more
114 * range in the hash index). The mask also strips high bits and strips
115 * fewer high bits as the number of hash buckets increases.
116 * I don't understand two things: why is the initial downshift 28
117 * to keep 4 bits when the initial mask is 011 to keep 2 bits,
118 * and why do we have both a mask and a downshift?
121 #define RANDOM_INDEX(table, i) \
122 (((((long) (i))*1103515245) >> (table)->down_shift) & (table)->mask)
125 * Initial number of buckets in hash table (hash table statically
126 * allocates its buckets for this size and below).
127 * The initial mask has to be synced to this.
129 #define DBUS_SMALL_HASH_TABLE 4
132 * Typedef for DBusHashEntry
134 typedef struct DBusHashEntry DBusHashEntry;
137 * @brief Internal representation of a hash entry.
139 * A single entry (key-value pair) in the hash table.
140 * Internal to hash table implementation.
144 DBusHashEntry *next; /**< Pointer to next entry in this
145 * hash bucket, or #NULL for end of
148 void *key; /**< Hash key */
149 void *value; /**< Hash value */
153 * Function used to find and optionally create a hash entry.
155 typedef DBusHashEntry* (* DBusFindEntryFunction) (DBusHashTable *table,
157 dbus_bool_t create_if_not_found,
158 DBusHashEntry ***bucket,
159 DBusPreallocatedHash *preallocated);
162 * @brief Internals of DBusHashTable.
164 * Hash table internals. Hash tables are opaque objects, they must be
165 * used via accessor functions.
167 struct DBusHashTable {
168 int refcount; /**< Reference count */
170 DBusHashEntry **buckets; /**< Pointer to bucket array. Each
171 * element points to first entry in
172 * bucket's hash chain, or #NULL.
174 DBusHashEntry *static_buckets[DBUS_SMALL_HASH_TABLE];
175 /**< Bucket array used for small tables
176 * (to avoid mallocs and frees).
178 int n_buckets; /**< Total number of buckets allocated
181 int n_entries; /**< Total number of entries present
184 int hi_rebuild_size; /**< Enlarge table when n_entries gets
187 int lo_rebuild_size; /**< Shrink table when n_entries gets
190 int down_shift; /**< Shift count used in hashing
191 * function. Designed to use high-
192 * order bits of randomized keys.
194 int mask; /**< Mask value used in hashing
197 DBusHashType key_type; /**< Type of keys used in this table */
200 DBusFindEntryFunction find_function; /**< Function for finding entries */
202 DBusFreeFunction free_key_function; /**< Function to free keys */
203 DBusFreeFunction free_value_function; /**< Function to free values */
205 DBusMemPool *entry_pool; /**< Memory pool for hash entries */
209 * @brief Internals of DBusHashIter.
213 DBusHashTable *table; /**< Pointer to table containing entry. */
214 DBusHashEntry **bucket; /**< Pointer to bucket that points to
215 * first entry in this entry's chain:
216 * used for deleting the entry.
218 DBusHashEntry *entry; /**< Current hash entry */
219 DBusHashEntry *next_entry; /**< Next entry to be iterated onto in current bucket */
220 int next_bucket; /**< index of next bucket */
221 int n_entries_on_init; /**< used to detect table resize since initialization */
224 static DBusHashEntry* find_direct_function (DBusHashTable *table,
226 dbus_bool_t create_if_not_found,
227 DBusHashEntry ***bucket,
228 DBusPreallocatedHash *preallocated);
229 static DBusHashEntry* find_string_function (DBusHashTable *table,
231 dbus_bool_t create_if_not_found,
232 DBusHashEntry ***bucket,
233 DBusPreallocatedHash *preallocated);
234 #ifdef DBUS_BUILD_TESTS
235 static DBusHashEntry* find_two_strings_function (DBusHashTable *table,
237 dbus_bool_t create_if_not_found,
238 DBusHashEntry ***bucket,
239 DBusPreallocatedHash *preallocated);
241 static unsigned int string_hash (const char *str);
242 #ifdef DBUS_BUILD_TESTS
243 static unsigned int two_strings_hash (const char *str);
245 static void rebuild_table (DBusHashTable *table);
246 static DBusHashEntry* alloc_entry (DBusHashTable *table);
247 static void remove_entry (DBusHashTable *table,
248 DBusHashEntry **bucket,
249 DBusHashEntry *entry);
250 static void free_entry (DBusHashTable *table,
251 DBusHashEntry *entry);
252 static void free_entry_data (DBusHashTable *table,
253 DBusHashEntry *entry);
259 * @addtogroup DBusHashTable
264 * @typedef DBusHashIter
266 * Public opaque hash table iterator object.
270 * @typedef DBusHashTable
272 * Public opaque hash table object.
276 * @typedef DBusHashType
278 * Indicates the type of a key in the hash table.
282 * Constructs a new hash table. Should be freed with
283 * _dbus_hash_table_unref(). If memory cannot be
284 * allocated for the hash table, returns #NULL.
286 * @param type the type of hash key to use.
287 * @param key_free_function function to free hash keys.
288 * @param value_free_function function to free hash values.
289 * @returns a new DBusHashTable or #NULL if no memory.
292 _dbus_hash_table_new (DBusHashType type,
293 DBusFreeFunction key_free_function,
294 DBusFreeFunction value_free_function)
296 DBusHashTable *table;
297 DBusMemPool *entry_pool;
299 table = dbus_new0 (DBusHashTable, 1);
303 entry_pool = _dbus_mem_pool_new (sizeof (DBusHashEntry), TRUE);
304 if (entry_pool == NULL)
311 table->entry_pool = entry_pool;
313 _dbus_assert (DBUS_SMALL_HASH_TABLE == _DBUS_N_ELEMENTS (table->static_buckets));
315 table->buckets = table->static_buckets;
316 table->n_buckets = DBUS_SMALL_HASH_TABLE;
317 table->n_entries = 0;
318 table->hi_rebuild_size = DBUS_SMALL_HASH_TABLE * REBUILD_MULTIPLIER;
319 table->lo_rebuild_size = 0;
320 table->down_shift = 28;
322 table->key_type = type;
324 _dbus_assert (table->mask < table->n_buckets);
326 switch (table->key_type)
329 case DBUS_HASH_POINTER:
330 case DBUS_HASH_ULONG:
331 table->find_function = find_direct_function;
333 case DBUS_HASH_STRING:
334 table->find_function = find_string_function;
336 case DBUS_HASH_TWO_STRINGS:
337 #ifdef DBUS_BUILD_TESTS
338 table->find_function = find_two_strings_function;
342 _dbus_assert_not_reached ("Unknown hash table type");
346 table->free_key_function = key_free_function;
347 table->free_value_function = value_free_function;
354 * Increments the reference count for a hash table.
356 * @param table the hash table to add a reference to.
357 * @returns the hash table.
360 _dbus_hash_table_ref (DBusHashTable *table)
362 table->refcount += 1;
368 * Decrements the reference count for a hash table,
369 * freeing the hash table if the count reaches zero.
371 * @param table the hash table to remove a reference from.
374 _dbus_hash_table_unref (DBusHashTable *table)
376 table->refcount -= 1;
378 if (table->refcount == 0)
381 DBusHashEntry *entry;
385 /* Free the entries in the table. */
386 for (i = 0; i < table->n_buckets; i++)
388 entry = table->buckets[i];
389 while (entry != NULL)
393 free_entry (table, entry);
399 DBusHashEntry *entry;
402 /* Free the entries in the table. */
403 for (i = 0; i < table->n_buckets; i++)
405 entry = table->buckets[i];
406 while (entry != NULL)
408 free_entry_data (table, entry);
413 /* We can do this very quickly with memory pools ;-) */
414 _dbus_mem_pool_free (table->entry_pool);
417 /* Free the bucket array, if it was dynamically allocated. */
418 if (table->buckets != table->static_buckets)
419 dbus_free (table->buckets);
426 * Removed all entries from a hash table.
428 * @param table the hash table to remove all entries from.
431 _dbus_hash_table_remove_all (DBusHashTable *table)
434 _dbus_hash_iter_init (table, &iter);
435 while (_dbus_hash_iter_next (&iter))
437 _dbus_hash_iter_remove_entry(&iter);
441 static DBusHashEntry*
442 alloc_entry (DBusHashTable *table)
444 DBusHashEntry *entry;
446 entry = _dbus_mem_pool_alloc (table->entry_pool);
452 free_entry_data (DBusHashTable *table,
453 DBusHashEntry *entry)
455 if (table->free_key_function)
456 (* table->free_key_function) (entry->key);
457 if (table->free_value_function)
458 (* table->free_value_function) (entry->value);
462 free_entry (DBusHashTable *table,
463 DBusHashEntry *entry)
465 free_entry_data (table, entry);
466 _dbus_mem_pool_dealloc (table->entry_pool, entry);
470 remove_entry (DBusHashTable *table,
471 DBusHashEntry **bucket,
472 DBusHashEntry *entry)
474 _dbus_assert (table != NULL);
475 _dbus_assert (bucket != NULL);
476 _dbus_assert (*bucket != NULL);
477 _dbus_assert (entry != NULL);
479 if (*bucket == entry)
480 *bucket = entry->next;
486 while (prev->next != entry)
489 _dbus_assert (prev != NULL);
491 prev->next = entry->next;
494 table->n_entries -= 1;
495 free_entry (table, entry);
499 * Initializes a hash table iterator. To iterate over all entries in a
500 * hash table, use the following code (the printf assumes a hash
501 * from strings to strings obviously):
506 * _dbus_hash_iter_init (table, &iter);
507 * while (_dbus_hash_iter_next (&iter))
509 * printf ("The first key is %s and value is %s\n",
510 * _dbus_hash_iter_get_string_key (&iter),
511 * _dbus_hash_iter_get_value (&iter));
517 * The iterator is initialized pointing "one before" the first hash
518 * entry. The first call to _dbus_hash_iter_next() moves it onto
519 * the first valid entry or returns #FALSE if the hash table is
520 * empty. Subsequent calls move to the next valid entry or return
521 * #FALSE if there are no more entries.
523 * Note that it is guaranteed to be safe to remove a hash entry during
524 * iteration, but it is not safe to add a hash entry.
526 * @param table the hash table to iterate over.
527 * @param iter the iterator to initialize.
530 _dbus_hash_iter_init (DBusHashTable *table,
533 DBusRealHashIter *real;
535 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
537 real = (DBusRealHashIter*) iter;
542 real->next_entry = NULL;
543 real->next_bucket = 0;
544 real->n_entries_on_init = table->n_entries;
548 * Move the hash iterator forward one step, to the next hash entry.
549 * The documentation for _dbus_hash_iter_init() explains in more
552 * @param iter the iterator to move forward.
553 * @returns #FALSE if there are no more entries to move to.
556 _dbus_hash_iter_next (DBusHashIter *iter)
558 DBusRealHashIter *real;
560 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
562 real = (DBusRealHashIter*) iter;
564 /* if this assertion failed someone probably added hash entries
565 * during iteration, which is bad.
567 _dbus_assert (real->n_entries_on_init >= real->table->n_entries);
569 /* Remember that real->entry may have been deleted */
571 while (real->next_entry == NULL)
573 if (real->next_bucket >= real->table->n_buckets)
575 /* invalidate iter and return false */
582 real->bucket = &(real->table->buckets[real->next_bucket]);
583 real->next_entry = *(real->bucket);
584 real->next_bucket += 1;
587 _dbus_assert (real->next_entry != NULL);
588 _dbus_assert (real->bucket != NULL);
590 real->entry = real->next_entry;
591 real->next_entry = real->entry->next;
597 * Removes the current entry from the hash table.
598 * If a key_free_function or value_free_function
599 * was provided to _dbus_hash_table_new(),
600 * frees the key and/or value for this entry.
602 * @param iter the hash table iterator.
605 _dbus_hash_iter_remove_entry (DBusHashIter *iter)
607 DBusRealHashIter *real;
609 real = (DBusRealHashIter*) iter;
611 _dbus_assert (real->table != NULL);
612 _dbus_assert (real->entry != NULL);
613 _dbus_assert (real->bucket != NULL);
615 remove_entry (real->table, real->bucket, real->entry);
617 real->entry = NULL; /* make it crash if you try to use this entry */
621 * Gets the value of the current entry.
623 * @param iter the hash table iterator.
626 _dbus_hash_iter_get_value (DBusHashIter *iter)
628 DBusRealHashIter *real;
630 real = (DBusRealHashIter*) iter;
632 _dbus_assert (real->table != NULL);
633 _dbus_assert (real->entry != NULL);
635 return real->entry->value;
639 * Sets the value of the current entry.
640 * If the hash table has a value_free_function
641 * it will be used to free the previous value.
642 * The hash table will own the passed-in value
643 * (it will not be copied).
645 * @param iter the hash table iterator.
646 * @param value the new value.
649 _dbus_hash_iter_set_value (DBusHashIter *iter,
652 DBusRealHashIter *real;
654 real = (DBusRealHashIter*) iter;
656 _dbus_assert (real->table != NULL);
657 _dbus_assert (real->entry != NULL);
659 if (real->table->free_value_function && value != real->entry->value)
660 (* real->table->free_value_function) (real->entry->value);
662 real->entry->value = value;
666 * Gets the key for the current entry.
667 * Only works for hash tables of type #DBUS_HASH_INT.
669 * @param iter the hash table iterator.
672 _dbus_hash_iter_get_int_key (DBusHashIter *iter)
674 DBusRealHashIter *real;
676 real = (DBusRealHashIter*) iter;
678 _dbus_assert (real->table != NULL);
679 _dbus_assert (real->entry != NULL);
681 return _DBUS_POINTER_TO_INT (real->entry->key);
685 * Gets the key for the current entry.
686 * Only works for hash tables of type #DBUS_HASH_ULONG.
688 * @param iter the hash table iterator.
691 _dbus_hash_iter_get_ulong_key (DBusHashIter *iter)
693 DBusRealHashIter *real;
695 real = (DBusRealHashIter*) iter;
697 _dbus_assert (real->table != NULL);
698 _dbus_assert (real->entry != NULL);
700 return (unsigned long) real->entry->key;
704 * Gets the key for the current entry.
705 * Only works for hash tables of type #DBUS_HASH_STRING
706 * @param iter the hash table iterator.
709 _dbus_hash_iter_get_string_key (DBusHashIter *iter)
711 DBusRealHashIter *real;
713 real = (DBusRealHashIter*) iter;
715 _dbus_assert (real->table != NULL);
716 _dbus_assert (real->entry != NULL);
718 return real->entry->key;
721 #ifdef DBUS_BUILD_TESTS
723 * Gets the key for the current entry.
724 * Only works for hash tables of type #DBUS_HASH_TWO_STRINGS
725 * @param iter the hash table iterator.
728 _dbus_hash_iter_get_two_strings_key (DBusHashIter *iter)
730 DBusRealHashIter *real;
732 real = (DBusRealHashIter*) iter;
734 _dbus_assert (real->table != NULL);
735 _dbus_assert (real->entry != NULL);
737 return real->entry->key;
739 #endif /* DBUS_BUILD_TESTS */
742 * A low-level but efficient interface for manipulating the hash
743 * table. It's efficient because you can get, set, and optionally
744 * create the hash entry while only running the hash function one
747 * Note that while calling _dbus_hash_iter_next() on the iterator
748 * filled in by this function may work, it's completely
749 * undefined which entries are after this iter and which
750 * are before it. So it would be silly to iterate using this
753 * If the hash entry is created, its value will be initialized
756 * #FALSE may be returned due to memory allocation failure, or
757 * because create_if_not_found was #FALSE and the entry
760 * If create_if_not_found is #TRUE and the entry is created, the hash
761 * table takes ownership of the key that's passed in.
763 * For a hash table of type #DBUS_HASH_INT, cast the int
764 * key to the key parameter using #_DBUS_INT_TO_POINTER().
766 * @param table the hash table.
767 * @param key the hash key.
768 * @param create_if_not_found if #TRUE, create the entry if it didn't exist.
769 * @param iter the iterator to initialize.
770 * @returns #TRUE if the hash entry now exists (and the iterator is thus valid).
773 _dbus_hash_iter_lookup (DBusHashTable *table,
775 dbus_bool_t create_if_not_found,
778 DBusRealHashIter *real;
779 DBusHashEntry *entry;
780 DBusHashEntry **bucket;
782 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
784 real = (DBusRealHashIter*) iter;
786 entry = (* table->find_function) (table, key, create_if_not_found, &bucket, NULL);
792 real->bucket = bucket;
794 real->next_entry = entry->next;
795 real->next_bucket = (bucket - table->buckets) + 1;
796 real->n_entries_on_init = table->n_entries;
798 _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
804 add_allocated_entry (DBusHashTable *table,
805 DBusHashEntry *entry,
808 DBusHashEntry ***bucket)
814 b = &(table->buckets[idx]);
821 table->n_entries += 1;
823 /* note we ONLY rebuild when ADDING - because you can iterate over a
824 * table and remove entries safely.
826 if (table->n_entries >= table->hi_rebuild_size ||
827 table->n_entries < table->lo_rebuild_size)
828 rebuild_table (table);
831 static DBusHashEntry*
832 add_entry (DBusHashTable *table,
835 DBusHashEntry ***bucket,
836 DBusPreallocatedHash *preallocated)
838 DBusHashEntry *entry;
840 if (preallocated == NULL)
842 entry = alloc_entry (table);
852 entry = (DBusHashEntry*) preallocated;
855 add_allocated_entry (table, entry, idx, key, bucket);
860 /* This is g_str_hash from GLib which was
861 * extensively discussed/tested/profiled
864 string_hash (const char *str)
870 for (p += 1; *p != '\0'; p++)
871 h = (h << 5) - h + *p;
876 #ifdef DBUS_BUILD_TESTS
877 /* This hashes a memory block with two nul-terminated strings
878 * in it, used in dbus-object-registry.c at the moment.
881 two_strings_hash (const char *str)
887 for (p += 1; *p != '\0'; p++)
888 h = (h << 5) - h + *p;
890 for (p += 1; *p != '\0'; p++)
891 h = (h << 5) - h + *p;
895 #endif /* DBUS_BUILD_TESTS */
897 /** Key comparison function */
898 typedef int (* KeyCompareFunc) (const void *key_a, const void *key_b);
900 static DBusHashEntry*
901 find_generic_function (DBusHashTable *table,
904 KeyCompareFunc compare_func,
905 dbus_bool_t create_if_not_found,
906 DBusHashEntry ***bucket,
907 DBusPreallocatedHash *preallocated)
909 DBusHashEntry *entry;
914 /* Search all of the entries in this bucket. */
915 entry = table->buckets[idx];
916 while (entry != NULL)
918 if ((compare_func == NULL && key == entry->key) ||
919 (compare_func != NULL && (* compare_func) (key, entry->key) == 0))
922 *bucket = &(table->buckets[idx]);
925 _dbus_hash_table_free_preallocated_entry (table, preallocated);
933 if (create_if_not_found)
934 entry = add_entry (table, idx, key, bucket, preallocated);
935 else if (preallocated)
936 _dbus_hash_table_free_preallocated_entry (table, preallocated);
941 static DBusHashEntry*
942 find_string_function (DBusHashTable *table,
944 dbus_bool_t create_if_not_found,
945 DBusHashEntry ***bucket,
946 DBusPreallocatedHash *preallocated)
950 idx = string_hash (key) & table->mask;
952 return find_generic_function (table, key, idx,
953 (KeyCompareFunc) strcmp, create_if_not_found, bucket,
957 #ifdef DBUS_BUILD_TESTS
959 two_strings_cmp (const char *a,
973 return strcmp (a + len_a + 1, b + len_b + 1);
977 #ifdef DBUS_BUILD_TESTS
978 static DBusHashEntry*
979 find_two_strings_function (DBusHashTable *table,
981 dbus_bool_t create_if_not_found,
982 DBusHashEntry ***bucket,
983 DBusPreallocatedHash *preallocated)
987 idx = two_strings_hash (key) & table->mask;
989 return find_generic_function (table, key, idx,
990 (KeyCompareFunc) two_strings_cmp, create_if_not_found, bucket,
993 #endif /* DBUS_BUILD_TESTS */
995 static DBusHashEntry*
996 find_direct_function (DBusHashTable *table,
998 dbus_bool_t create_if_not_found,
999 DBusHashEntry ***bucket,
1000 DBusPreallocatedHash *preallocated)
1004 idx = RANDOM_INDEX (table, key) & table->mask;
1007 return find_generic_function (table, key, idx,
1008 NULL, create_if_not_found, bucket,
1013 rebuild_table (DBusHashTable *table)
1017 DBusHashEntry **old_buckets;
1018 DBusHashEntry **old_chain;
1019 DBusHashEntry *entry;
1020 dbus_bool_t growing;
1023 * Allocate and initialize the new bucket array, and set up
1024 * hashing constants for new array size.
1027 growing = table->n_entries >= table->hi_rebuild_size;
1029 old_size = table->n_buckets;
1030 old_buckets = table->buckets;
1034 /* overflow paranoia */
1035 if (table->n_buckets < _DBUS_INT_MAX / 4 &&
1036 table->down_shift >= 0)
1037 new_buckets = table->n_buckets * 4;
1039 return; /* can't grow anymore */
1043 new_buckets = table->n_buckets / 4;
1044 if (new_buckets < DBUS_SMALL_HASH_TABLE)
1045 return; /* don't bother shrinking this far */
1048 table->buckets = dbus_new0 (DBusHashEntry*, new_buckets);
1049 if (table->buckets == NULL)
1051 /* out of memory, yay - just don't reallocate, the table will
1052 * still work, albeit more slowly.
1054 table->buckets = old_buckets;
1058 table->n_buckets = new_buckets;
1062 table->lo_rebuild_size = table->hi_rebuild_size;
1063 table->hi_rebuild_size *= 4;
1065 table->down_shift -= 2; /* keep 2 more high bits */
1066 table->mask = (table->mask << 2) + 3; /* keep 2 more high bits */
1070 table->hi_rebuild_size = table->lo_rebuild_size;
1071 table->lo_rebuild_size /= 4;
1073 table->down_shift += 2; /* keep 2 fewer high bits */
1074 table->mask = table->mask >> 2; /* keep 2 fewer high bits */
1078 printf ("%s table to lo = %d hi = %d downshift = %d mask = 0x%x\n",
1079 growing ? "GROW" : "SHRINK",
1080 table->lo_rebuild_size,
1081 table->hi_rebuild_size,
1086 _dbus_assert (table->lo_rebuild_size >= 0);
1087 _dbus_assert (table->hi_rebuild_size > table->lo_rebuild_size);
1088 _dbus_assert (table->mask != 0);
1089 /* the mask is essentially the max index */
1090 _dbus_assert (table->mask < table->n_buckets);
1093 * Rehash all of the existing entries into the new bucket array.
1096 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
1098 for (entry = *old_chain; entry != NULL; entry = *old_chain)
1101 DBusHashEntry **bucket;
1103 *old_chain = entry->next;
1104 switch (table->key_type)
1106 case DBUS_HASH_STRING:
1107 idx = string_hash (entry->key) & table->mask;
1109 case DBUS_HASH_TWO_STRINGS:
1110 #ifdef DBUS_BUILD_TESTS
1111 idx = two_strings_hash (entry->key) & table->mask;
1114 _dbus_assert_not_reached ("two-strings is not enabled");
1118 case DBUS_HASH_ULONG:
1119 case DBUS_HASH_POINTER:
1120 idx = RANDOM_INDEX (table, entry->key);
1124 _dbus_assert_not_reached ("Unknown hash table type");
1128 bucket = &(table->buckets[idx]);
1129 entry->next = *bucket;
1134 /* Free the old bucket array, if it was dynamically allocated. */
1136 if (old_buckets != table->static_buckets)
1137 dbus_free (old_buckets);
1141 * Looks up the value for a given string in a hash table
1142 * of type #DBUS_HASH_STRING. Returns %NULL if the value
1143 * is not present. (A not-present entry is indistinguishable
1144 * from an entry with a value of %NULL.)
1145 * @param table the hash table.
1146 * @param key the string to look up.
1147 * @returns the value of the hash entry.
1150 _dbus_hash_table_lookup_string (DBusHashTable *table,
1153 DBusHashEntry *entry;
1155 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1157 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1160 return entry->value;
1165 #ifdef DBUS_BUILD_TESTS
1167 * Looks up the value for a given string in a hash table
1168 * of type #DBUS_HASH_TWO_STRINGS. Returns %NULL if the value
1169 * is not present. (A not-present entry is indistinguishable
1170 * from an entry with a value of %NULL.)
1171 * @param table the hash table.
1172 * @param key the string to look up.
1173 * @returns the value of the hash entry.
1176 _dbus_hash_table_lookup_two_strings (DBusHashTable *table,
1179 DBusHashEntry *entry;
1181 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1183 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1186 return entry->value;
1190 #endif /* DBUS_BUILD_TESTS */
1193 * Looks up the value for a given integer in a hash table
1194 * of type #DBUS_HASH_INT. Returns %NULL if the value
1195 * is not present. (A not-present entry is indistinguishable
1196 * from an entry with a value of %NULL.)
1197 * @param table the hash table.
1198 * @param key the integer to look up.
1199 * @returns the value of the hash entry.
1202 _dbus_hash_table_lookup_int (DBusHashTable *table,
1205 DBusHashEntry *entry;
1207 _dbus_assert (table->key_type == DBUS_HASH_INT);
1209 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, NULL, NULL);
1212 return entry->value;
1217 #ifdef DBUS_BUILD_TESTS
1218 /* disabled since it's only used for testing */
1220 * Looks up the value for a given integer in a hash table
1221 * of type #DBUS_HASH_POINTER. Returns %NULL if the value
1222 * is not present. (A not-present entry is indistinguishable
1223 * from an entry with a value of %NULL.)
1224 * @param table the hash table.
1225 * @param key the integer to look up.
1226 * @returns the value of the hash entry.
1229 _dbus_hash_table_lookup_pointer (DBusHashTable *table,
1232 DBusHashEntry *entry;
1234 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1236 entry = (* table->find_function) (table, key, FALSE, NULL, NULL);
1239 return entry->value;
1243 #endif /* DBUS_BUILD_TESTS */
1246 * Looks up the value for a given integer in a hash table
1247 * of type #DBUS_HASH_ULONG. Returns %NULL if the value
1248 * is not present. (A not-present entry is indistinguishable
1249 * from an entry with a value of %NULL.)
1250 * @param table the hash table.
1251 * @param key the integer to look up.
1252 * @returns the value of the hash entry.
1255 _dbus_hash_table_lookup_ulong (DBusHashTable *table,
1258 DBusHashEntry *entry;
1260 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1262 entry = (* table->find_function) (table, (void*) key, FALSE, NULL, NULL);
1265 return entry->value;
1271 * Removes the hash entry for the given key. If no hash entry
1272 * for the key exists, does nothing.
1274 * @param table the hash table.
1275 * @param key the hash key.
1276 * @returns #TRUE if the entry existed
1279 _dbus_hash_table_remove_string (DBusHashTable *table,
1282 DBusHashEntry *entry;
1283 DBusHashEntry **bucket;
1285 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1287 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1291 remove_entry (table, bucket, entry);
1298 #ifdef DBUS_BUILD_TESTS
1300 * Removes the hash entry for the given key. If no hash entry
1301 * for the key exists, does nothing.
1303 * @param table the hash table.
1304 * @param key the hash key.
1305 * @returns #TRUE if the entry existed
1308 _dbus_hash_table_remove_two_strings (DBusHashTable *table,
1311 DBusHashEntry *entry;
1312 DBusHashEntry **bucket;
1314 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1316 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1320 remove_entry (table, bucket, entry);
1326 #endif /* DBUS_BUILD_TESTS */
1329 * Removes the hash entry for the given key. If no hash entry
1330 * for the key exists, does nothing.
1332 * @param table the hash table.
1333 * @param key the hash key.
1334 * @returns #TRUE if the entry existed
1337 _dbus_hash_table_remove_int (DBusHashTable *table,
1340 DBusHashEntry *entry;
1341 DBusHashEntry **bucket;
1343 _dbus_assert (table->key_type == DBUS_HASH_INT);
1345 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, &bucket, NULL);
1349 remove_entry (table, bucket, entry);
1356 #ifdef DBUS_BUILD_TESTS
1357 /* disabled since it's only used for testing */
1359 * Removes the hash entry for the given key. If no hash entry
1360 * for the key exists, does nothing.
1362 * @param table the hash table.
1363 * @param key the hash key.
1364 * @returns #TRUE if the entry existed
1367 _dbus_hash_table_remove_pointer (DBusHashTable *table,
1370 DBusHashEntry *entry;
1371 DBusHashEntry **bucket;
1373 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1375 entry = (* table->find_function) (table, key, FALSE, &bucket, NULL);
1379 remove_entry (table, bucket, entry);
1385 #endif /* DBUS_BUILD_TESTS */
1388 * Removes the hash entry for the given key. If no hash entry
1389 * for the key exists, does nothing.
1391 * @param table the hash table.
1392 * @param key the hash key.
1393 * @returns #TRUE if the entry existed
1396 _dbus_hash_table_remove_ulong (DBusHashTable *table,
1399 DBusHashEntry *entry;
1400 DBusHashEntry **bucket;
1402 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1404 entry = (* table->find_function) (table, (void*) key, FALSE, &bucket, NULL);
1408 remove_entry (table, bucket, entry);
1416 * Creates a hash entry with the given key and value.
1417 * The key and value are not copied; they are stored
1418 * in the hash table by reference. If an entry with the
1419 * given key already exists, the previous key and value
1420 * are overwritten (and freed if the hash table has
1421 * a key_free_function and/or value_free_function).
1423 * Returns #FALSE if memory for the new hash entry
1424 * can't be allocated.
1426 * @param table the hash table.
1427 * @param key the hash entry key.
1428 * @param value the hash entry value.
1431 _dbus_hash_table_insert_string (DBusHashTable *table,
1435 DBusPreallocatedHash *preallocated;
1437 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1439 preallocated = _dbus_hash_table_preallocate_entry (table);
1440 if (preallocated == NULL)
1443 _dbus_hash_table_insert_string_preallocated (table, preallocated,
1449 #ifdef DBUS_BUILD_TESTS
1451 * Creates a hash entry with the given key and value.
1452 * The key and value are not copied; they are stored
1453 * in the hash table by reference. If an entry with the
1454 * given key already exists, the previous key and value
1455 * are overwritten (and freed if the hash table has
1456 * a key_free_function and/or value_free_function).
1458 * Returns #FALSE if memory for the new hash entry
1459 * can't be allocated.
1461 * @param table the hash table.
1462 * @param key the hash entry key.
1463 * @param value the hash entry value.
1466 _dbus_hash_table_insert_two_strings (DBusHashTable *table,
1470 DBusHashEntry *entry;
1472 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1474 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1477 return FALSE; /* no memory */
1479 if (table->free_key_function && entry->key != key)
1480 (* table->free_key_function) (entry->key);
1482 if (table->free_value_function && entry->value != value)
1483 (* table->free_value_function) (entry->value);
1486 entry->value = value;
1490 #endif /* DBUS_BUILD_TESTS */
1493 * Creates a hash entry with the given key and value.
1494 * The key and value are not copied; they are stored
1495 * in the hash table by reference. If an entry with the
1496 * given key already exists, the previous key and value
1497 * are overwritten (and freed if the hash table has
1498 * a key_free_function and/or value_free_function).
1500 * Returns #FALSE if memory for the new hash entry
1501 * can't be allocated.
1503 * @param table the hash table.
1504 * @param key the hash entry key.
1505 * @param value the hash entry value.
1508 _dbus_hash_table_insert_int (DBusHashTable *table,
1512 DBusHashEntry *entry;
1514 _dbus_assert (table->key_type == DBUS_HASH_INT);
1516 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), TRUE, NULL, NULL);
1519 return FALSE; /* no memory */
1521 if (table->free_key_function && entry->key != _DBUS_INT_TO_POINTER (key))
1522 (* table->free_key_function) (entry->key);
1524 if (table->free_value_function && entry->value != value)
1525 (* table->free_value_function) (entry->value);
1527 entry->key = _DBUS_INT_TO_POINTER (key);
1528 entry->value = value;
1533 #ifdef DBUS_BUILD_TESTS
1534 /* disabled since it's only used for testing */
1536 * Creates a hash entry with the given key and value.
1537 * The key and value are not copied; they are stored
1538 * in the hash table by reference. If an entry with the
1539 * given key already exists, the previous key and value
1540 * are overwritten (and freed if the hash table has
1541 * a key_free_function and/or value_free_function).
1543 * Returns #FALSE if memory for the new hash entry
1544 * can't be allocated.
1546 * @param table the hash table.
1547 * @param key the hash entry key.
1548 * @param value the hash entry value.
1551 _dbus_hash_table_insert_pointer (DBusHashTable *table,
1555 DBusHashEntry *entry;
1557 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1559 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1562 return FALSE; /* no memory */
1564 if (table->free_key_function && entry->key != key)
1565 (* table->free_key_function) (entry->key);
1567 if (table->free_value_function && entry->value != value)
1568 (* table->free_value_function) (entry->value);
1571 entry->value = value;
1575 #endif /* DBUS_BUILD_TESTS */
1578 * Creates a hash entry with the given key and value.
1579 * The key and value are not copied; they are stored
1580 * in the hash table by reference. If an entry with the
1581 * given key already exists, the previous key and value
1582 * are overwritten (and freed if the hash table has
1583 * a key_free_function and/or value_free_function).
1585 * Returns #FALSE if memory for the new hash entry
1586 * can't be allocated.
1588 * @param table the hash table.
1589 * @param key the hash entry key.
1590 * @param value the hash entry value.
1593 _dbus_hash_table_insert_ulong (DBusHashTable *table,
1597 DBusHashEntry *entry;
1599 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1601 entry = (* table->find_function) (table, (void*) key, TRUE, NULL, NULL);
1604 return FALSE; /* no memory */
1606 if (table->free_key_function && entry->key != (void*) key)
1607 (* table->free_key_function) (entry->key);
1609 if (table->free_value_function && entry->value != value)
1610 (* table->free_value_function) (entry->value);
1612 entry->key = (void*) key;
1613 entry->value = value;
1619 * Preallocate an opaque data blob that allows us to insert into the
1620 * hash table at a later time without allocating any memory.
1622 * @param table the hash table
1623 * @returns the preallocated data, or #NULL if no memory
1625 DBusPreallocatedHash*
1626 _dbus_hash_table_preallocate_entry (DBusHashTable *table)
1628 DBusHashEntry *entry;
1630 entry = alloc_entry (table);
1632 return (DBusPreallocatedHash*) entry;
1636 * Frees an opaque DBusPreallocatedHash that was *not* used
1637 * in order to insert into the hash table.
1639 * @param table the hash table
1640 * @param preallocated the preallocated data
1643 _dbus_hash_table_free_preallocated_entry (DBusHashTable *table,
1644 DBusPreallocatedHash *preallocated)
1646 DBusHashEntry *entry;
1648 _dbus_assert (preallocated != NULL);
1650 entry = (DBusHashEntry*) preallocated;
1652 /* Don't use free_entry(), since this entry has no key/data */
1653 _dbus_mem_pool_dealloc (table->entry_pool, entry);
1657 * Inserts a string-keyed entry into the hash table, using a
1658 * preallocated data block from
1659 * _dbus_hash_table_preallocate_entry(). This function cannot fail due
1660 * to lack of memory. The DBusPreallocatedHash object is consumed and
1661 * should not be reused or freed. Otherwise this function works
1662 * just like _dbus_hash_table_insert_string().
1664 * @param table the hash table
1665 * @param preallocated the preallocated data
1666 * @param key the hash key
1667 * @param value the value
1670 _dbus_hash_table_insert_string_preallocated (DBusHashTable *table,
1671 DBusPreallocatedHash *preallocated,
1675 DBusHashEntry *entry;
1677 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1678 _dbus_assert (preallocated != NULL);
1680 entry = (* table->find_function) (table, key, TRUE, NULL, preallocated);
1682 _dbus_assert (entry != NULL);
1684 if (table->free_key_function && entry->key != key)
1685 (* table->free_key_function) (entry->key);
1687 if (table->free_value_function && entry->value != value)
1688 (* table->free_value_function) (entry->value);
1691 entry->value = value;
1695 * Gets the number of hash entries in a hash table.
1697 * @param table the hash table.
1698 * @returns the number of entries in the table.
1701 _dbus_hash_table_get_n_entries (DBusHashTable *table)
1703 return table->n_entries;
1708 #ifdef DBUS_BUILD_TESTS
1709 #include "dbus-test.h"
1712 /* If you're wondering why the hash table test takes
1713 * forever to run, it's because we call this function
1714 * in inner loops thus making things quadratic.
1717 count_entries (DBusHashTable *table)
1723 _dbus_hash_iter_init (table, &iter);
1724 while (_dbus_hash_iter_next (&iter))
1727 _dbus_assert (count == _dbus_hash_table_get_n_entries (table));
1732 /* Copy the foo\0bar\0 double string thing */
1734 _dbus_strdup2 (const char *str)
1743 len += strlen ((str + len + 1));
1745 copy = dbus_malloc (len + 2);
1749 memcpy (copy, str, len + 2);
1755 * @ingroup DBusHashTableInternals
1756 * Unit test for DBusHashTable
1757 * @returns #TRUE on success.
1760 _dbus_hash_test (void)
1763 DBusHashTable *table1;
1764 DBusHashTable *table2;
1765 DBusHashTable *table3;
1766 DBusHashTable *table4;
1768 #define N_HASH_KEYS 5000
1770 dbus_bool_t ret = FALSE;
1772 keys = dbus_new (char *, N_HASH_KEYS);
1774 _dbus_assert_not_reached ("no memory");
1776 for (i = 0; i < N_HASH_KEYS; i++)
1778 keys[i] = dbus_malloc (128);
1780 if (keys[i] == NULL)
1781 _dbus_assert_not_reached ("no memory");
1784 printf ("Computing test hash keys...\n");
1786 while (i < N_HASH_KEYS)
1790 /* all the hash keys are TWO_STRINGS, but
1791 * then we can also use those as regular strings.
1794 len = sprintf (keys[i], "Hash key %d", i);
1795 sprintf (keys[i] + len + 1, "Two string %d", i);
1796 _dbus_assert (*(keys[i] + len) == '\0');
1797 _dbus_assert (*(keys[i] + len + 1) != '\0');
1800 printf ("... done.\n");
1802 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1803 dbus_free, dbus_free);
1807 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1812 table3 = _dbus_hash_table_new (DBUS_HASH_ULONG,
1817 table4 = _dbus_hash_table_new (DBUS_HASH_TWO_STRINGS,
1818 dbus_free, dbus_free);
1823 /* Insert and remove a bunch of stuff, counting the table in between
1824 * to be sure it's not broken and that iteration works
1832 key = _dbus_strdup (keys[i]);
1835 value = _dbus_strdup ("Value!");
1839 if (!_dbus_hash_table_insert_string (table1,
1843 value = _dbus_strdup (keys[i]);
1847 if (!_dbus_hash_table_insert_int (table2,
1851 value = _dbus_strdup (keys[i]);
1855 if (!_dbus_hash_table_insert_ulong (table3,
1859 key = _dbus_strdup2 (keys[i]);
1862 value = _dbus_strdup ("Value!");
1866 if (!_dbus_hash_table_insert_two_strings (table4,
1870 _dbus_assert (count_entries (table1) == i + 1);
1871 _dbus_assert (count_entries (table2) == i + 1);
1872 _dbus_assert (count_entries (table3) == i + 1);
1873 _dbus_assert (count_entries (table4) == i + 1);
1875 value = _dbus_hash_table_lookup_string (table1, keys[i]);
1876 _dbus_assert (value != NULL);
1877 _dbus_assert (strcmp (value, "Value!") == 0);
1879 value = _dbus_hash_table_lookup_int (table2, i);
1880 _dbus_assert (value != NULL);
1881 _dbus_assert (strcmp (value, keys[i]) == 0);
1883 value = _dbus_hash_table_lookup_ulong (table3, i);
1884 _dbus_assert (value != NULL);
1885 _dbus_assert (strcmp (value, keys[i]) == 0);
1887 value = _dbus_hash_table_lookup_two_strings (table4, keys[i]);
1888 _dbus_assert (value != NULL);
1889 _dbus_assert (strcmp (value, "Value!") == 0);
1897 _dbus_hash_table_remove_string (table1,
1900 _dbus_hash_table_remove_int (table2, i);
1902 _dbus_hash_table_remove_ulong (table3, i);
1904 _dbus_hash_table_remove_two_strings (table4,
1907 _dbus_assert (count_entries (table1) == i);
1908 _dbus_assert (count_entries (table2) == i);
1909 _dbus_assert (count_entries (table3) == i);
1910 _dbus_assert (count_entries (table4) == i);
1915 _dbus_hash_table_ref (table1);
1916 _dbus_hash_table_ref (table2);
1917 _dbus_hash_table_ref (table3);
1918 _dbus_hash_table_ref (table4);
1919 _dbus_hash_table_unref (table1);
1920 _dbus_hash_table_unref (table2);
1921 _dbus_hash_table_unref (table3);
1922 _dbus_hash_table_unref (table4);
1923 _dbus_hash_table_unref (table1);
1924 _dbus_hash_table_unref (table2);
1925 _dbus_hash_table_unref (table3);
1926 _dbus_hash_table_unref (table4);
1929 /* Insert a bunch of stuff then check
1930 * that iteration works correctly (finds the right
1931 * values, iter_set_value works, etc.)
1933 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1934 dbus_free, dbus_free);
1938 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1949 key = _dbus_strdup (keys[i]);
1952 value = _dbus_strdup ("Value!");
1956 if (!_dbus_hash_table_insert_string (table1,
1960 value = _dbus_strdup (keys[i]);
1964 if (!_dbus_hash_table_insert_int (table2,
1968 _dbus_assert (count_entries (table1) == i + 1);
1969 _dbus_assert (count_entries (table2) == i + 1);
1974 _dbus_hash_iter_init (table1, &iter);
1975 while (_dbus_hash_iter_next (&iter))
1980 key = _dbus_hash_iter_get_string_key (&iter);
1981 value = _dbus_hash_iter_get_value (&iter);
1983 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1985 value = _dbus_strdup ("Different value!");
1989 _dbus_hash_iter_set_value (&iter, value);
1991 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1994 _dbus_hash_iter_init (table1, &iter);
1995 while (_dbus_hash_iter_next (&iter))
1997 _dbus_hash_iter_remove_entry (&iter);
1998 _dbus_assert (count_entries (table1) == i - 1);
2002 _dbus_hash_iter_init (table2, &iter);
2003 while (_dbus_hash_iter_next (&iter))
2008 key = _dbus_hash_iter_get_int_key (&iter);
2009 value = _dbus_hash_iter_get_value (&iter);
2011 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
2013 value = _dbus_strdup ("Different value!");
2017 _dbus_hash_iter_set_value (&iter, value);
2019 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
2022 i = count_entries (table2);
2023 _dbus_hash_iter_init (table2, &iter);
2024 while (_dbus_hash_iter_next (&iter))
2026 _dbus_hash_iter_remove_entry (&iter);
2027 _dbus_assert (count_entries (table2) + 1 == i);
2031 /* add/remove interleaved, to check that we grow/shrink the table
2040 key = _dbus_strdup (keys[i]);
2044 value = _dbus_strdup ("Value!");
2048 if (!_dbus_hash_table_insert_string (table1,
2061 key = _dbus_strdup (keys[i]);
2064 value = _dbus_strdup ("Value!");
2068 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2071 if (!_dbus_hash_table_insert_string (table1,
2075 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2078 _dbus_assert (_dbus_hash_table_get_n_entries (table1) == i);
2083 /* nuke these tables */
2084 _dbus_hash_table_unref (table1);
2085 _dbus_hash_table_unref (table2);
2088 /* Now do a bunch of things again using _dbus_hash_iter_lookup() to
2089 * be sure that interface works.
2091 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
2092 dbus_free, dbus_free);
2096 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
2107 key = _dbus_strdup (keys[i]);
2110 value = _dbus_strdup ("Value!");
2114 if (!_dbus_hash_iter_lookup (table1,
2117 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2118 _dbus_hash_iter_set_value (&iter, value);
2120 value = _dbus_strdup (keys[i]);
2124 if (!_dbus_hash_iter_lookup (table2,
2125 _DBUS_INT_TO_POINTER (i), TRUE, &iter))
2127 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2128 _dbus_hash_iter_set_value (&iter, value);
2130 _dbus_assert (count_entries (table1) == i + 1);
2131 _dbus_assert (count_entries (table2) == i + 1);
2133 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2136 value = _dbus_hash_iter_get_value (&iter);
2137 _dbus_assert (value != NULL);
2138 _dbus_assert (strcmp (value, "Value!") == 0);
2140 /* Iterate just to be sure it works, though
2141 * it's a stupid thing to do
2143 while (_dbus_hash_iter_next (&iter))
2146 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2149 value = _dbus_hash_iter_get_value (&iter);
2150 _dbus_assert (value != NULL);
2151 _dbus_assert (strcmp (value, keys[i]) == 0);
2153 /* Iterate just to be sure it works, though
2154 * it's a stupid thing to do
2156 while (_dbus_hash_iter_next (&iter))
2165 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2166 _dbus_assert_not_reached ("hash entry should have existed");
2167 _dbus_hash_iter_remove_entry (&iter);
2169 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2170 _dbus_assert_not_reached ("hash entry should have existed");
2171 _dbus_hash_iter_remove_entry (&iter);
2173 _dbus_assert (count_entries (table1) == i);
2174 _dbus_assert (count_entries (table2) == i);
2179 _dbus_hash_table_unref (table1);
2180 _dbus_hash_table_unref (table2);
2185 for (i = 0; i < N_HASH_KEYS; i++)
2186 dbus_free (keys[i]);
2193 #endif /* DBUS_BUILD_TESTS */