1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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.
78 #include "dbus-hash.h"
79 #include "dbus-internals.h"
80 #include "dbus-mempool.h"
83 * @defgroup DBusHashTable Hash table
84 * @ingroup DBusInternals
85 * @brief DBusHashTable data structure
87 * Types and functions related to DBusHashTable.
91 * @defgroup DBusHashTableInternals Hash table implementation details
92 * @ingroup DBusInternals
93 * @brief DBusHashTable implementation details
95 * The guts of DBusHashTable.
101 * When there are this many entries per bucket, on average, rebuild
102 * the hash table to make it larger.
104 #define REBUILD_MULTIPLIER 3
107 * Takes a preliminary integer hash value and produces an index into a
108 * hash tables bucket list. The idea is to make it so that
109 * preliminary values that are arbitrarily similar will end up in
110 * different buckets. The hash function was taken from a
111 * random-number generator. (This is used to hash integers.)
113 * The down_shift drops off the high bits of the hash index, and
114 * decreases as we increase the number of hash buckets (to keep more
115 * range in the hash index). The mask also strips high bits and strips
116 * fewer high bits as the number of hash buckets increases.
117 * I don't understand two things: why is the initial downshift 28
118 * to keep 4 bits when the initial mask is 011 to keep 2 bits,
119 * and why do we have both a mask and a downshift?
122 #define RANDOM_INDEX(table, i) \
123 (((((intptr_t) (i))*1103515245) >> (table)->down_shift) & (table)->mask)
126 * Initial number of buckets in hash table (hash table statically
127 * allocates its buckets for this size and below).
128 * The initial mask has to be synced to this.
130 #define DBUS_SMALL_HASH_TABLE 4
133 * Typedef for DBusHashEntry
135 typedef struct DBusHashEntry DBusHashEntry;
138 * @brief Internal representation of a hash entry.
140 * A single entry (key-value pair) in the hash table.
141 * Internal to hash table implementation.
145 DBusHashEntry *next; /**< Pointer to next entry in this
146 * hash bucket, or #NULL for end of
149 void *key; /**< Hash key */
150 void *value; /**< Hash value */
154 * Function used to find and optionally create a hash entry.
156 typedef DBusHashEntry* (* DBusFindEntryFunction) (DBusHashTable *table,
158 dbus_bool_t create_if_not_found,
159 DBusHashEntry ***bucket,
160 DBusPreallocatedHash *preallocated);
163 * @brief Internals of DBusHashTable.
165 * Hash table internals. Hash tables are opaque objects, they must be
166 * used via accessor functions.
168 struct DBusHashTable {
169 int refcount; /**< Reference count */
171 DBusHashEntry **buckets; /**< Pointer to bucket array. Each
172 * element points to first entry in
173 * bucket's hash chain, or #NULL.
175 DBusHashEntry *static_buckets[DBUS_SMALL_HASH_TABLE];
176 /**< Bucket array used for small tables
177 * (to avoid mallocs and frees).
179 int n_buckets; /**< Total number of buckets allocated
182 int n_entries; /**< Total number of entries present
185 int hi_rebuild_size; /**< Enlarge table when n_entries gets
188 int lo_rebuild_size; /**< Shrink table when n_entries gets
191 int down_shift; /**< Shift count used in hashing
192 * function. Designed to use high-
193 * order bits of randomized keys.
195 int mask; /**< Mask value used in hashing
198 DBusHashType key_type; /**< Type of keys used in this table */
201 DBusFindEntryFunction find_function; /**< Function for finding entries */
203 DBusFreeFunction free_key_function; /**< Function to free keys */
204 DBusFreeFunction free_value_function; /**< Function to free values */
206 DBusMemPool *entry_pool; /**< Memory pool for hash entries */
210 * @brief Internals of DBusHashIter.
214 DBusHashTable *table; /**< Pointer to table containing entry. */
215 DBusHashEntry **bucket; /**< Pointer to bucket that points to
216 * first entry in this entry's chain:
217 * used for deleting the entry.
219 DBusHashEntry *entry; /**< Current hash entry */
220 DBusHashEntry *next_entry; /**< Next entry to be iterated onto in current bucket */
221 int next_bucket; /**< index of next bucket */
222 int n_entries_on_init; /**< used to detect table resize since initialization */
225 static DBusHashEntry* find_direct_function (DBusHashTable *table,
227 dbus_bool_t create_if_not_found,
228 DBusHashEntry ***bucket,
229 DBusPreallocatedHash *preallocated);
230 static DBusHashEntry* find_string_function (DBusHashTable *table,
232 dbus_bool_t create_if_not_found,
233 DBusHashEntry ***bucket,
234 DBusPreallocatedHash *preallocated);
235 #ifdef DBUS_BUILD_TESTS
236 static DBusHashEntry* find_two_strings_function (DBusHashTable *table,
238 dbus_bool_t create_if_not_found,
239 DBusHashEntry ***bucket,
240 DBusPreallocatedHash *preallocated);
242 static unsigned int string_hash (const char *str);
243 #ifdef DBUS_BUILD_TESTS
244 static unsigned int two_strings_hash (const char *str);
246 static void rebuild_table (DBusHashTable *table);
247 static DBusHashEntry* alloc_entry (DBusHashTable *table);
248 static void remove_entry (DBusHashTable *table,
249 DBusHashEntry **bucket,
250 DBusHashEntry *entry);
251 static void free_entry (DBusHashTable *table,
252 DBusHashEntry *entry);
253 static void free_entry_data (DBusHashTable *table,
254 DBusHashEntry *entry);
260 * @addtogroup DBusHashTable
265 * @typedef DBusHashIter
267 * Public opaque hash table iterator object.
271 * @typedef DBusHashTable
273 * Public opaque hash table object.
277 * @typedef DBusHashType
279 * Indicates the type of a key in the hash table.
283 * Constructs a new hash table. Should be freed with
284 * _dbus_hash_table_unref(). If memory cannot be
285 * allocated for the hash table, returns #NULL.
287 * @param type the type of hash key to use.
288 * @param key_free_function function to free hash keys.
289 * @param value_free_function function to free hash values.
290 * @returns a new DBusHashTable or #NULL if no memory.
293 _dbus_hash_table_new (DBusHashType type,
294 DBusFreeFunction key_free_function,
295 DBusFreeFunction value_free_function)
297 DBusHashTable *table;
298 DBusMemPool *entry_pool;
300 table = dbus_new0 (DBusHashTable, 1);
304 entry_pool = _dbus_mem_pool_new (sizeof (DBusHashEntry), TRUE);
305 if (entry_pool == NULL)
312 table->entry_pool = entry_pool;
314 _dbus_assert (DBUS_SMALL_HASH_TABLE == _DBUS_N_ELEMENTS (table->static_buckets));
316 table->buckets = table->static_buckets;
317 table->n_buckets = DBUS_SMALL_HASH_TABLE;
318 table->n_entries = 0;
319 table->hi_rebuild_size = DBUS_SMALL_HASH_TABLE * REBUILD_MULTIPLIER;
320 table->lo_rebuild_size = 0;
321 table->down_shift = 28;
323 table->key_type = type;
325 _dbus_assert (table->mask < table->n_buckets);
327 switch (table->key_type)
330 case DBUS_HASH_POINTER:
331 case DBUS_HASH_UINTPTR:
332 table->find_function = find_direct_function;
334 case DBUS_HASH_STRING:
335 table->find_function = find_string_function;
337 case DBUS_HASH_TWO_STRINGS:
338 #ifdef DBUS_BUILD_TESTS
339 table->find_function = find_two_strings_function;
343 _dbus_assert_not_reached ("Unknown hash table type");
347 table->free_key_function = key_free_function;
348 table->free_value_function = value_free_function;
355 * Increments the reference count for a hash table.
357 * @param table the hash table to add a reference to.
358 * @returns the hash table.
361 _dbus_hash_table_ref (DBusHashTable *table)
363 table->refcount += 1;
369 * Decrements the reference count for a hash table,
370 * freeing the hash table if the count reaches zero.
372 * @param table the hash table to remove a reference from.
375 _dbus_hash_table_unref (DBusHashTable *table)
377 table->refcount -= 1;
379 if (table->refcount == 0)
382 DBusHashEntry *entry;
386 /* Free the entries in the table. */
387 for (i = 0; i < table->n_buckets; i++)
389 entry = table->buckets[i];
390 while (entry != NULL)
394 free_entry (table, entry);
400 DBusHashEntry *entry;
403 /* Free the entries in the table. */
404 for (i = 0; i < table->n_buckets; i++)
406 entry = table->buckets[i];
407 while (entry != NULL)
409 free_entry_data (table, entry);
414 /* We can do this very quickly with memory pools ;-) */
415 _dbus_mem_pool_free (table->entry_pool);
418 /* Free the bucket array, if it was dynamically allocated. */
419 if (table->buckets != table->static_buckets)
420 dbus_free (table->buckets);
427 * Removed all entries from a hash table.
429 * @param table the hash table to remove all entries from.
432 _dbus_hash_table_remove_all (DBusHashTable *table)
435 _dbus_hash_iter_init (table, &iter);
436 while (_dbus_hash_iter_next (&iter))
438 _dbus_hash_iter_remove_entry(&iter);
442 static DBusHashEntry*
443 alloc_entry (DBusHashTable *table)
445 DBusHashEntry *entry;
447 entry = _dbus_mem_pool_alloc (table->entry_pool);
453 free_entry_data (DBusHashTable *table,
454 DBusHashEntry *entry)
456 if (table->free_key_function)
457 (* table->free_key_function) (entry->key);
458 if (table->free_value_function)
459 (* table->free_value_function) (entry->value);
463 free_entry (DBusHashTable *table,
464 DBusHashEntry *entry)
466 free_entry_data (table, entry);
467 _dbus_mem_pool_dealloc (table->entry_pool, entry);
471 remove_entry (DBusHashTable *table,
472 DBusHashEntry **bucket,
473 DBusHashEntry *entry)
475 _dbus_assert (table != NULL);
476 _dbus_assert (bucket != NULL);
477 _dbus_assert (*bucket != NULL);
478 _dbus_assert (entry != NULL);
480 if (*bucket == entry)
481 *bucket = entry->next;
487 while (prev->next != entry)
490 _dbus_assert (prev != NULL);
492 prev->next = entry->next;
495 table->n_entries -= 1;
496 free_entry (table, entry);
500 * Initializes a hash table iterator. To iterate over all entries in a
501 * hash table, use the following code (the printf assumes a hash
502 * from strings to strings obviously):
507 * _dbus_hash_iter_init (table, &iter);
508 * while (_dbus_hash_iter_next (&iter))
510 * printf ("The first key is %s and value is %s\n",
511 * _dbus_hash_iter_get_string_key (&iter),
512 * _dbus_hash_iter_get_value (&iter));
518 * The iterator is initialized pointing "one before" the first hash
519 * entry. The first call to _dbus_hash_iter_next() moves it onto
520 * the first valid entry or returns #FALSE if the hash table is
521 * empty. Subsequent calls move to the next valid entry or return
522 * #FALSE if there are no more entries.
524 * Note that it is guaranteed to be safe to remove a hash entry during
525 * iteration, but it is not safe to add a hash entry.
527 * @param table the hash table to iterate over.
528 * @param iter the iterator to initialize.
531 _dbus_hash_iter_init (DBusHashTable *table,
534 DBusRealHashIter *real;
536 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
538 real = (DBusRealHashIter*) iter;
543 real->next_entry = NULL;
544 real->next_bucket = 0;
545 real->n_entries_on_init = table->n_entries;
549 * Move the hash iterator forward one step, to the next hash entry.
550 * The documentation for _dbus_hash_iter_init() explains in more
553 * @param iter the iterator to move forward.
554 * @returns #FALSE if there are no more entries to move to.
557 _dbus_hash_iter_next (DBusHashIter *iter)
559 DBusRealHashIter *real;
561 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
563 real = (DBusRealHashIter*) iter;
565 /* if this assertion failed someone probably added hash entries
566 * during iteration, which is bad.
568 _dbus_assert (real->n_entries_on_init >= real->table->n_entries);
570 /* Remember that real->entry may have been deleted */
572 while (real->next_entry == NULL)
574 if (real->next_bucket >= real->table->n_buckets)
576 /* invalidate iter and return false */
583 real->bucket = &(real->table->buckets[real->next_bucket]);
584 real->next_entry = *(real->bucket);
585 real->next_bucket += 1;
588 _dbus_assert (real->next_entry != NULL);
589 _dbus_assert (real->bucket != NULL);
591 real->entry = real->next_entry;
592 real->next_entry = real->entry->next;
598 * Removes the current entry from the hash table.
599 * If a key_free_function or value_free_function
600 * was provided to _dbus_hash_table_new(),
601 * frees the key and/or value for this entry.
603 * @param iter the hash table iterator.
606 _dbus_hash_iter_remove_entry (DBusHashIter *iter)
608 DBusRealHashIter *real;
610 real = (DBusRealHashIter*) iter;
612 _dbus_assert (real->table != NULL);
613 _dbus_assert (real->entry != NULL);
614 _dbus_assert (real->bucket != NULL);
616 remove_entry (real->table, real->bucket, real->entry);
618 real->entry = NULL; /* make it crash if you try to use this entry */
622 * Gets the value of the current entry.
624 * @param iter the hash table iterator.
627 _dbus_hash_iter_get_value (DBusHashIter *iter)
629 DBusRealHashIter *real;
631 real = (DBusRealHashIter*) iter;
633 _dbus_assert (real->table != NULL);
634 _dbus_assert (real->entry != NULL);
636 return real->entry->value;
640 * Sets the value of the current entry.
641 * If the hash table has a value_free_function
642 * it will be used to free the previous value.
643 * The hash table will own the passed-in value
644 * (it will not be copied).
646 * @param iter the hash table iterator.
647 * @param value the new value.
650 _dbus_hash_iter_set_value (DBusHashIter *iter,
653 DBusRealHashIter *real;
655 real = (DBusRealHashIter*) iter;
657 _dbus_assert (real->table != NULL);
658 _dbus_assert (real->entry != NULL);
660 if (real->table->free_value_function && value != real->entry->value)
661 (* real->table->free_value_function) (real->entry->value);
663 real->entry->value = value;
667 * Gets the key for the current entry.
668 * Only works for hash tables of type #DBUS_HASH_INT.
670 * @param iter the hash table iterator.
673 _dbus_hash_iter_get_int_key (DBusHashIter *iter)
675 DBusRealHashIter *real;
677 real = (DBusRealHashIter*) iter;
679 _dbus_assert (real->table != NULL);
680 _dbus_assert (real->entry != NULL);
682 return _DBUS_POINTER_TO_INT (real->entry->key);
686 * Gets the key for the current entry.
687 * Only works for hash tables of type #DBUS_HASH_UINTPTR.
689 * @param iter the hash table iterator.
692 _dbus_hash_iter_get_uintptr_key (DBusHashIter *iter)
694 DBusRealHashIter *real;
696 real = (DBusRealHashIter*) iter;
698 _dbus_assert (real->table != NULL);
699 _dbus_assert (real->entry != NULL);
701 return (uintptr_t) real->entry->key;
705 * Gets the key for the current entry.
706 * Only works for hash tables of type #DBUS_HASH_STRING
707 * @param iter the hash table iterator.
710 _dbus_hash_iter_get_string_key (DBusHashIter *iter)
712 DBusRealHashIter *real;
714 real = (DBusRealHashIter*) iter;
716 _dbus_assert (real->table != NULL);
717 _dbus_assert (real->entry != NULL);
719 return real->entry->key;
722 #ifdef DBUS_BUILD_TESTS
724 * Gets the key for the current entry.
725 * Only works for hash tables of type #DBUS_HASH_TWO_STRINGS
726 * @param iter the hash table iterator.
729 _dbus_hash_iter_get_two_strings_key (DBusHashIter *iter)
731 DBusRealHashIter *real;
733 real = (DBusRealHashIter*) iter;
735 _dbus_assert (real->table != NULL);
736 _dbus_assert (real->entry != NULL);
738 return real->entry->key;
740 #endif /* DBUS_BUILD_TESTS */
743 * A low-level but efficient interface for manipulating the hash
744 * table. It's efficient because you can get, set, and optionally
745 * create the hash entry while only running the hash function one
748 * Note that while calling _dbus_hash_iter_next() on the iterator
749 * filled in by this function may work, it's completely
750 * undefined which entries are after this iter and which
751 * are before it. So it would be silly to iterate using this
754 * If the hash entry is created, its value will be initialized
757 * #FALSE may be returned due to memory allocation failure, or
758 * because create_if_not_found was #FALSE and the entry
761 * If create_if_not_found is #TRUE and the entry is created, the hash
762 * table takes ownership of the key that's passed in.
764 * For a hash table of type #DBUS_HASH_INT, cast the int
765 * key to the key parameter using #_DBUS_INT_TO_POINTER().
767 * @param table the hash table.
768 * @param key the hash key.
769 * @param create_if_not_found if #TRUE, create the entry if it didn't exist.
770 * @param iter the iterator to initialize.
771 * @returns #TRUE if the hash entry now exists (and the iterator is thus valid).
774 _dbus_hash_iter_lookup (DBusHashTable *table,
776 dbus_bool_t create_if_not_found,
779 DBusRealHashIter *real;
780 DBusHashEntry *entry;
781 DBusHashEntry **bucket;
783 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
785 real = (DBusRealHashIter*) iter;
787 entry = (* table->find_function) (table, key, create_if_not_found, &bucket, NULL);
793 real->bucket = bucket;
795 real->next_entry = entry->next;
796 real->next_bucket = (bucket - table->buckets) + 1;
797 real->n_entries_on_init = table->n_entries;
799 _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
805 add_allocated_entry (DBusHashTable *table,
806 DBusHashEntry *entry,
809 DBusHashEntry ***bucket)
815 b = &(table->buckets[idx]);
822 table->n_entries += 1;
824 /* note we ONLY rebuild when ADDING - because you can iterate over a
825 * table and remove entries safely.
827 if (table->n_entries >= table->hi_rebuild_size ||
828 table->n_entries < table->lo_rebuild_size)
829 rebuild_table (table);
832 static DBusHashEntry*
833 add_entry (DBusHashTable *table,
836 DBusHashEntry ***bucket,
837 DBusPreallocatedHash *preallocated)
839 DBusHashEntry *entry;
841 if (preallocated == NULL)
843 entry = alloc_entry (table);
853 entry = (DBusHashEntry*) preallocated;
856 add_allocated_entry (table, entry, idx, key, bucket);
861 /* This is g_str_hash from GLib which was
862 * extensively discussed/tested/profiled
865 string_hash (const char *str)
871 for (p += 1; *p != '\0'; p++)
872 h = (h << 5) - h + *p;
877 #ifdef DBUS_BUILD_TESTS
878 /* This hashes a memory block with two nul-terminated strings
879 * in it, used in dbus-object-registry.c at the moment.
882 two_strings_hash (const char *str)
888 for (p += 1; *p != '\0'; p++)
889 h = (h << 5) - h + *p;
891 for (p += 1; *p != '\0'; p++)
892 h = (h << 5) - h + *p;
896 #endif /* DBUS_BUILD_TESTS */
898 /** Key comparison function */
899 typedef int (* KeyCompareFunc) (const void *key_a, const void *key_b);
901 static DBusHashEntry*
902 find_generic_function (DBusHashTable *table,
905 KeyCompareFunc compare_func,
906 dbus_bool_t create_if_not_found,
907 DBusHashEntry ***bucket,
908 DBusPreallocatedHash *preallocated)
910 DBusHashEntry *entry;
915 /* Search all of the entries in this bucket. */
916 entry = table->buckets[idx];
917 while (entry != NULL)
919 if ((compare_func == NULL && key == entry->key) ||
920 (compare_func != NULL && (* compare_func) (key, entry->key) == 0))
923 *bucket = &(table->buckets[idx]);
926 _dbus_hash_table_free_preallocated_entry (table, preallocated);
934 if (create_if_not_found)
935 entry = add_entry (table, idx, key, bucket, preallocated);
936 else if (preallocated)
937 _dbus_hash_table_free_preallocated_entry (table, preallocated);
942 static DBusHashEntry*
943 find_string_function (DBusHashTable *table,
945 dbus_bool_t create_if_not_found,
946 DBusHashEntry ***bucket,
947 DBusPreallocatedHash *preallocated)
951 idx = string_hash (key) & table->mask;
953 return find_generic_function (table, key, idx,
954 (KeyCompareFunc) strcmp, create_if_not_found, bucket,
958 #ifdef DBUS_BUILD_TESTS
960 two_strings_cmp (const char *a,
974 return strcmp (a + len_a + 1, b + len_b + 1);
978 #ifdef DBUS_BUILD_TESTS
979 static DBusHashEntry*
980 find_two_strings_function (DBusHashTable *table,
982 dbus_bool_t create_if_not_found,
983 DBusHashEntry ***bucket,
984 DBusPreallocatedHash *preallocated)
988 idx = two_strings_hash (key) & table->mask;
990 return find_generic_function (table, key, idx,
991 (KeyCompareFunc) two_strings_cmp, create_if_not_found, bucket,
994 #endif /* DBUS_BUILD_TESTS */
996 static DBusHashEntry*
997 find_direct_function (DBusHashTable *table,
999 dbus_bool_t create_if_not_found,
1000 DBusHashEntry ***bucket,
1001 DBusPreallocatedHash *preallocated)
1005 idx = RANDOM_INDEX (table, key) & table->mask;
1008 return find_generic_function (table, key, idx,
1009 NULL, create_if_not_found, bucket,
1014 rebuild_table (DBusHashTable *table)
1018 DBusHashEntry **old_buckets;
1019 DBusHashEntry **old_chain;
1020 DBusHashEntry *entry;
1021 dbus_bool_t growing;
1024 * Allocate and initialize the new bucket array, and set up
1025 * hashing constants for new array size.
1028 growing = table->n_entries >= table->hi_rebuild_size;
1030 old_size = table->n_buckets;
1031 old_buckets = table->buckets;
1035 /* overflow paranoia */
1036 if (table->n_buckets < _DBUS_INT_MAX / 4 &&
1037 table->down_shift >= 0)
1038 new_buckets = table->n_buckets * 4;
1040 return; /* can't grow anymore */
1044 new_buckets = table->n_buckets / 4;
1045 if (new_buckets < DBUS_SMALL_HASH_TABLE)
1046 return; /* don't bother shrinking this far */
1049 table->buckets = dbus_new0 (DBusHashEntry*, new_buckets);
1050 if (table->buckets == NULL)
1052 /* out of memory, yay - just don't reallocate, the table will
1053 * still work, albeit more slowly.
1055 table->buckets = old_buckets;
1059 table->n_buckets = new_buckets;
1063 table->lo_rebuild_size = table->hi_rebuild_size;
1064 table->hi_rebuild_size *= 4;
1066 table->down_shift -= 2; /* keep 2 more high bits */
1067 table->mask = (table->mask << 2) + 3; /* keep 2 more high bits */
1071 table->hi_rebuild_size = table->lo_rebuild_size;
1072 table->lo_rebuild_size /= 4;
1074 table->down_shift += 2; /* keep 2 fewer high bits */
1075 table->mask = table->mask >> 2; /* keep 2 fewer high bits */
1079 printf ("%s table to lo = %d hi = %d downshift = %d mask = 0x%x\n",
1080 growing ? "GROW" : "SHRINK",
1081 table->lo_rebuild_size,
1082 table->hi_rebuild_size,
1087 _dbus_assert (table->lo_rebuild_size >= 0);
1088 _dbus_assert (table->hi_rebuild_size > table->lo_rebuild_size);
1089 _dbus_assert (table->mask != 0);
1090 /* the mask is essentially the max index */
1091 _dbus_assert (table->mask < table->n_buckets);
1094 * Rehash all of the existing entries into the new bucket array.
1097 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
1099 for (entry = *old_chain; entry != NULL; entry = *old_chain)
1102 DBusHashEntry **bucket;
1104 *old_chain = entry->next;
1105 switch (table->key_type)
1107 case DBUS_HASH_STRING:
1108 idx = string_hash (entry->key) & table->mask;
1110 case DBUS_HASH_TWO_STRINGS:
1111 #ifdef DBUS_BUILD_TESTS
1112 idx = two_strings_hash (entry->key) & table->mask;
1115 _dbus_assert_not_reached ("two-strings is not enabled");
1119 case DBUS_HASH_UINTPTR:
1120 case DBUS_HASH_POINTER:
1121 idx = RANDOM_INDEX (table, entry->key);
1125 _dbus_assert_not_reached ("Unknown hash table type");
1129 bucket = &(table->buckets[idx]);
1130 entry->next = *bucket;
1135 /* Free the old bucket array, if it was dynamically allocated. */
1137 if (old_buckets != table->static_buckets)
1138 dbus_free (old_buckets);
1142 * Looks up the value for a given string in a hash table
1143 * of type #DBUS_HASH_STRING. Returns %NULL if the value
1144 * is not present. (A not-present entry is indistinguishable
1145 * from an entry with a value of %NULL.)
1146 * @param table the hash table.
1147 * @param key the string to look up.
1148 * @returns the value of the hash entry.
1151 _dbus_hash_table_lookup_string (DBusHashTable *table,
1154 DBusHashEntry *entry;
1156 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1158 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1161 return entry->value;
1166 #ifdef DBUS_BUILD_TESTS
1168 * Looks up the value for a given string in a hash table
1169 * of type #DBUS_HASH_TWO_STRINGS. Returns %NULL if the value
1170 * is not present. (A not-present entry is indistinguishable
1171 * from an entry with a value of %NULL.)
1172 * @param table the hash table.
1173 * @param key the string to look up.
1174 * @returns the value of the hash entry.
1177 _dbus_hash_table_lookup_two_strings (DBusHashTable *table,
1180 DBusHashEntry *entry;
1182 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1184 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1187 return entry->value;
1191 #endif /* DBUS_BUILD_TESTS */
1194 * Looks up the value for a given integer in a hash table
1195 * of type #DBUS_HASH_INT. Returns %NULL if the value
1196 * is not present. (A not-present entry is indistinguishable
1197 * from an entry with a value of %NULL.)
1198 * @param table the hash table.
1199 * @param key the integer to look up.
1200 * @returns the value of the hash entry.
1203 _dbus_hash_table_lookup_int (DBusHashTable *table,
1206 DBusHashEntry *entry;
1208 _dbus_assert (table->key_type == DBUS_HASH_INT);
1210 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, NULL, NULL);
1213 return entry->value;
1218 #ifdef DBUS_BUILD_TESTS
1219 /* disabled since it's only used for testing */
1221 * Looks up the value for a given integer in a hash table
1222 * of type #DBUS_HASH_POINTER. Returns %NULL if the value
1223 * is not present. (A not-present entry is indistinguishable
1224 * from an entry with a value of %NULL.)
1225 * @param table the hash table.
1226 * @param key the integer to look up.
1227 * @returns the value of the hash entry.
1230 _dbus_hash_table_lookup_pointer (DBusHashTable *table,
1233 DBusHashEntry *entry;
1235 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1237 entry = (* table->find_function) (table, key, FALSE, NULL, NULL);
1240 return entry->value;
1244 #endif /* DBUS_BUILD_TESTS */
1247 * Looks up the value for a given integer in a hash table
1248 * of type #DBUS_HASH_UINTPTR. Returns %NULL if the value
1249 * is not present. (A not-present entry is indistinguishable
1250 * from an entry with a value of %NULL.)
1251 * @param table the hash table.
1252 * @param key the integer to look up.
1253 * @returns the value of the hash entry.
1256 _dbus_hash_table_lookup_uintptr (DBusHashTable *table,
1259 DBusHashEntry *entry;
1261 _dbus_assert (table->key_type == DBUS_HASH_UINTPTR);
1263 entry = (* table->find_function) (table, (void*) key, FALSE, NULL, NULL);
1266 return entry->value;
1272 * Removes the hash entry for the given key. If no hash entry
1273 * for the key exists, does nothing.
1275 * @param table the hash table.
1276 * @param key the hash key.
1277 * @returns #TRUE if the entry existed
1280 _dbus_hash_table_remove_string (DBusHashTable *table,
1283 DBusHashEntry *entry;
1284 DBusHashEntry **bucket;
1286 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1288 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1292 remove_entry (table, bucket, entry);
1299 #ifdef DBUS_BUILD_TESTS
1301 * Removes the hash entry for the given key. If no hash entry
1302 * for the key exists, does nothing.
1304 * @param table the hash table.
1305 * @param key the hash key.
1306 * @returns #TRUE if the entry existed
1309 _dbus_hash_table_remove_two_strings (DBusHashTable *table,
1312 DBusHashEntry *entry;
1313 DBusHashEntry **bucket;
1315 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1317 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1321 remove_entry (table, bucket, entry);
1327 #endif /* DBUS_BUILD_TESTS */
1330 * Removes the hash entry for the given key. If no hash entry
1331 * for the key exists, does nothing.
1333 * @param table the hash table.
1334 * @param key the hash key.
1335 * @returns #TRUE if the entry existed
1338 _dbus_hash_table_remove_int (DBusHashTable *table,
1341 DBusHashEntry *entry;
1342 DBusHashEntry **bucket;
1344 _dbus_assert (table->key_type == DBUS_HASH_INT);
1346 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, &bucket, NULL);
1350 remove_entry (table, bucket, entry);
1357 #ifdef DBUS_BUILD_TESTS
1358 /* disabled since it's only used for testing */
1360 * Removes the hash entry for the given key. If no hash entry
1361 * for the key exists, does nothing.
1363 * @param table the hash table.
1364 * @param key the hash key.
1365 * @returns #TRUE if the entry existed
1368 _dbus_hash_table_remove_pointer (DBusHashTable *table,
1371 DBusHashEntry *entry;
1372 DBusHashEntry **bucket;
1374 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1376 entry = (* table->find_function) (table, key, FALSE, &bucket, NULL);
1380 remove_entry (table, bucket, entry);
1386 #endif /* DBUS_BUILD_TESTS */
1389 * Removes the hash entry for the given key. If no hash entry
1390 * for the key exists, does nothing.
1392 * @param table the hash table.
1393 * @param key the hash key.
1394 * @returns #TRUE if the entry existed
1397 _dbus_hash_table_remove_uintptr (DBusHashTable *table,
1400 DBusHashEntry *entry;
1401 DBusHashEntry **bucket;
1403 _dbus_assert (table->key_type == DBUS_HASH_UINTPTR);
1405 entry = (* table->find_function) (table, (void*) key, FALSE, &bucket, NULL);
1409 remove_entry (table, bucket, entry);
1417 * Creates a hash entry with the given key and value.
1418 * The key and value are not copied; they are stored
1419 * in the hash table by reference. If an entry with the
1420 * given key already exists, the previous key and value
1421 * are overwritten (and freed if the hash table has
1422 * a key_free_function and/or value_free_function).
1424 * Returns #FALSE if memory for the new hash entry
1425 * can't be allocated.
1427 * @param table the hash table.
1428 * @param key the hash entry key.
1429 * @param value the hash entry value.
1432 _dbus_hash_table_insert_string (DBusHashTable *table,
1436 DBusPreallocatedHash *preallocated;
1438 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1440 preallocated = _dbus_hash_table_preallocate_entry (table);
1441 if (preallocated == NULL)
1444 _dbus_hash_table_insert_string_preallocated (table, preallocated,
1450 #ifdef DBUS_BUILD_TESTS
1452 * Creates a hash entry with the given key and value.
1453 * The key and value are not copied; they are stored
1454 * in the hash table by reference. If an entry with the
1455 * given key already exists, the previous key and value
1456 * are overwritten (and freed if the hash table has
1457 * a key_free_function and/or value_free_function).
1459 * Returns #FALSE if memory for the new hash entry
1460 * can't be allocated.
1462 * @param table the hash table.
1463 * @param key the hash entry key.
1464 * @param value the hash entry value.
1467 _dbus_hash_table_insert_two_strings (DBusHashTable *table,
1471 DBusHashEntry *entry;
1473 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1475 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1478 return FALSE; /* no memory */
1480 if (table->free_key_function && entry->key != key)
1481 (* table->free_key_function) (entry->key);
1483 if (table->free_value_function && entry->value != value)
1484 (* table->free_value_function) (entry->value);
1487 entry->value = value;
1491 #endif /* DBUS_BUILD_TESTS */
1494 * Creates a hash entry with the given key and value.
1495 * The key and value are not copied; they are stored
1496 * in the hash table by reference. If an entry with the
1497 * given key already exists, the previous key and value
1498 * are overwritten (and freed if the hash table has
1499 * a key_free_function and/or value_free_function).
1501 * Returns #FALSE if memory for the new hash entry
1502 * can't be allocated.
1504 * @param table the hash table.
1505 * @param key the hash entry key.
1506 * @param value the hash entry value.
1509 _dbus_hash_table_insert_int (DBusHashTable *table,
1513 DBusHashEntry *entry;
1515 _dbus_assert (table->key_type == DBUS_HASH_INT);
1517 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), TRUE, NULL, NULL);
1520 return FALSE; /* no memory */
1522 if (table->free_key_function && entry->key != _DBUS_INT_TO_POINTER (key))
1523 (* table->free_key_function) (entry->key);
1525 if (table->free_value_function && entry->value != value)
1526 (* table->free_value_function) (entry->value);
1528 entry->key = _DBUS_INT_TO_POINTER (key);
1529 entry->value = value;
1534 #ifdef DBUS_BUILD_TESTS
1535 /* disabled since it's only used for testing */
1537 * Creates a hash entry with the given key and value.
1538 * The key and value are not copied; they are stored
1539 * in the hash table by reference. If an entry with the
1540 * given key already exists, the previous key and value
1541 * are overwritten (and freed if the hash table has
1542 * a key_free_function and/or value_free_function).
1544 * Returns #FALSE if memory for the new hash entry
1545 * can't be allocated.
1547 * @param table the hash table.
1548 * @param key the hash entry key.
1549 * @param value the hash entry value.
1552 _dbus_hash_table_insert_pointer (DBusHashTable *table,
1556 DBusHashEntry *entry;
1558 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1560 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1563 return FALSE; /* no memory */
1565 if (table->free_key_function && entry->key != key)
1566 (* table->free_key_function) (entry->key);
1568 if (table->free_value_function && entry->value != value)
1569 (* table->free_value_function) (entry->value);
1572 entry->value = value;
1576 #endif /* DBUS_BUILD_TESTS */
1579 * Creates a hash entry with the given key and value.
1580 * The key and value are not copied; they are stored
1581 * in the hash table by reference. If an entry with the
1582 * given key already exists, the previous key and value
1583 * are overwritten (and freed if the hash table has
1584 * a key_free_function and/or value_free_function).
1586 * Returns #FALSE if memory for the new hash entry
1587 * can't be allocated.
1589 * @param table the hash table.
1590 * @param key the hash entry key.
1591 * @param value the hash entry value.
1594 _dbus_hash_table_insert_uintptr (DBusHashTable *table,
1598 DBusHashEntry *entry;
1600 _dbus_assert (table->key_type == DBUS_HASH_UINTPTR);
1602 entry = (* table->find_function) (table, (void*) key, TRUE, NULL, NULL);
1605 return FALSE; /* no memory */
1607 if (table->free_key_function && entry->key != (void*) key)
1608 (* table->free_key_function) (entry->key);
1610 if (table->free_value_function && entry->value != value)
1611 (* table->free_value_function) (entry->value);
1613 entry->key = (void*) key;
1614 entry->value = value;
1620 * Preallocate an opaque data blob that allows us to insert into the
1621 * hash table at a later time without allocating any memory.
1623 * @param table the hash table
1624 * @returns the preallocated data, or #NULL if no memory
1626 DBusPreallocatedHash*
1627 _dbus_hash_table_preallocate_entry (DBusHashTable *table)
1629 DBusHashEntry *entry;
1631 entry = alloc_entry (table);
1633 return (DBusPreallocatedHash*) entry;
1637 * Frees an opaque DBusPreallocatedHash that was *not* used
1638 * in order to insert into the hash table.
1640 * @param table the hash table
1641 * @param preallocated the preallocated data
1644 _dbus_hash_table_free_preallocated_entry (DBusHashTable *table,
1645 DBusPreallocatedHash *preallocated)
1647 DBusHashEntry *entry;
1649 _dbus_assert (preallocated != NULL);
1651 entry = (DBusHashEntry*) preallocated;
1653 /* Don't use free_entry(), since this entry has no key/data */
1654 _dbus_mem_pool_dealloc (table->entry_pool, entry);
1658 * Inserts a string-keyed entry into the hash table, using a
1659 * preallocated data block from
1660 * _dbus_hash_table_preallocate_entry(). This function cannot fail due
1661 * to lack of memory. The DBusPreallocatedHash object is consumed and
1662 * should not be reused or freed. Otherwise this function works
1663 * just like _dbus_hash_table_insert_string().
1665 * @param table the hash table
1666 * @param preallocated the preallocated data
1667 * @param key the hash key
1668 * @param value the value
1671 _dbus_hash_table_insert_string_preallocated (DBusHashTable *table,
1672 DBusPreallocatedHash *preallocated,
1676 DBusHashEntry *entry;
1678 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1679 _dbus_assert (preallocated != NULL);
1681 entry = (* table->find_function) (table, key, TRUE, NULL, preallocated);
1683 _dbus_assert (entry != NULL);
1685 if (table->free_key_function && entry->key != key)
1686 (* table->free_key_function) (entry->key);
1688 if (table->free_value_function && entry->value != value)
1689 (* table->free_value_function) (entry->value);
1692 entry->value = value;
1696 * Gets the number of hash entries in a hash table.
1698 * @param table the hash table.
1699 * @returns the number of entries in the table.
1702 _dbus_hash_table_get_n_entries (DBusHashTable *table)
1704 return table->n_entries;
1709 #ifdef DBUS_BUILD_TESTS
1710 #include "dbus-test.h"
1713 /* If you're wondering why the hash table test takes
1714 * forever to run, it's because we call this function
1715 * in inner loops thus making things quadratic.
1718 count_entries (DBusHashTable *table)
1724 _dbus_hash_iter_init (table, &iter);
1725 while (_dbus_hash_iter_next (&iter))
1728 _dbus_assert (count == _dbus_hash_table_get_n_entries (table));
1733 /* Copy the foo\0bar\0 double string thing */
1735 _dbus_strdup2 (const char *str)
1744 len += strlen ((str + len + 1));
1746 copy = dbus_malloc (len + 2);
1750 memcpy (copy, str, len + 2);
1756 * @ingroup DBusHashTableInternals
1757 * Unit test for DBusHashTable
1758 * @returns #TRUE on success.
1761 _dbus_hash_test (void)
1764 DBusHashTable *table1;
1765 DBusHashTable *table2;
1766 DBusHashTable *table3;
1767 DBusHashTable *table4;
1769 #define N_HASH_KEYS 5000
1771 dbus_bool_t ret = FALSE;
1773 keys = dbus_new (char *, N_HASH_KEYS);
1775 _dbus_assert_not_reached ("no memory");
1777 for (i = 0; i < N_HASH_KEYS; i++)
1779 keys[i] = dbus_malloc (128);
1781 if (keys[i] == NULL)
1782 _dbus_assert_not_reached ("no memory");
1785 printf ("Computing test hash keys...\n");
1787 while (i < N_HASH_KEYS)
1791 /* all the hash keys are TWO_STRINGS, but
1792 * then we can also use those as regular strings.
1795 len = sprintf (keys[i], "Hash key %d", i);
1796 sprintf (keys[i] + len + 1, "Two string %d", i);
1797 _dbus_assert (*(keys[i] + len) == '\0');
1798 _dbus_assert (*(keys[i] + len + 1) != '\0');
1801 printf ("... done.\n");
1803 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1804 dbus_free, dbus_free);
1808 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1813 table3 = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
1818 table4 = _dbus_hash_table_new (DBUS_HASH_TWO_STRINGS,
1819 dbus_free, dbus_free);
1824 /* Insert and remove a bunch of stuff, counting the table in between
1825 * to be sure it's not broken and that iteration works
1833 key = _dbus_strdup (keys[i]);
1836 value = _dbus_strdup ("Value!");
1840 if (!_dbus_hash_table_insert_string (table1,
1844 value = _dbus_strdup (keys[i]);
1848 if (!_dbus_hash_table_insert_int (table2,
1852 value = _dbus_strdup (keys[i]);
1856 if (!_dbus_hash_table_insert_uintptr (table3,
1860 key = _dbus_strdup2 (keys[i]);
1863 value = _dbus_strdup ("Value!");
1867 if (!_dbus_hash_table_insert_two_strings (table4,
1871 _dbus_assert (count_entries (table1) == i + 1);
1872 _dbus_assert (count_entries (table2) == i + 1);
1873 _dbus_assert (count_entries (table3) == i + 1);
1874 _dbus_assert (count_entries (table4) == i + 1);
1876 value = _dbus_hash_table_lookup_string (table1, keys[i]);
1877 _dbus_assert (value != NULL);
1878 _dbus_assert (strcmp (value, "Value!") == 0);
1880 value = _dbus_hash_table_lookup_int (table2, i);
1881 _dbus_assert (value != NULL);
1882 _dbus_assert (strcmp (value, keys[i]) == 0);
1884 value = _dbus_hash_table_lookup_uintptr (table3, i);
1885 _dbus_assert (value != NULL);
1886 _dbus_assert (strcmp (value, keys[i]) == 0);
1888 value = _dbus_hash_table_lookup_two_strings (table4, keys[i]);
1889 _dbus_assert (value != NULL);
1890 _dbus_assert (strcmp (value, "Value!") == 0);
1898 _dbus_hash_table_remove_string (table1,
1901 _dbus_hash_table_remove_int (table2, i);
1903 _dbus_hash_table_remove_uintptr (table3, i);
1905 _dbus_hash_table_remove_two_strings (table4,
1908 _dbus_assert (count_entries (table1) == i);
1909 _dbus_assert (count_entries (table2) == i);
1910 _dbus_assert (count_entries (table3) == i);
1911 _dbus_assert (count_entries (table4) == i);
1916 _dbus_hash_table_ref (table1);
1917 _dbus_hash_table_ref (table2);
1918 _dbus_hash_table_ref (table3);
1919 _dbus_hash_table_ref (table4);
1920 _dbus_hash_table_unref (table1);
1921 _dbus_hash_table_unref (table2);
1922 _dbus_hash_table_unref (table3);
1923 _dbus_hash_table_unref (table4);
1924 _dbus_hash_table_unref (table1);
1925 _dbus_hash_table_unref (table2);
1926 _dbus_hash_table_unref (table3);
1927 _dbus_hash_table_unref (table4);
1930 /* Insert a bunch of stuff then check
1931 * that iteration works correctly (finds the right
1932 * values, iter_set_value works, etc.)
1934 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1935 dbus_free, dbus_free);
1939 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1950 key = _dbus_strdup (keys[i]);
1953 value = _dbus_strdup ("Value!");
1957 if (!_dbus_hash_table_insert_string (table1,
1961 value = _dbus_strdup (keys[i]);
1965 if (!_dbus_hash_table_insert_int (table2,
1969 _dbus_assert (count_entries (table1) == i + 1);
1970 _dbus_assert (count_entries (table2) == i + 1);
1975 _dbus_hash_iter_init (table1, &iter);
1976 while (_dbus_hash_iter_next (&iter))
1981 key = _dbus_hash_iter_get_string_key (&iter);
1982 value = _dbus_hash_iter_get_value (&iter);
1984 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1986 value = _dbus_strdup ("Different value!");
1990 _dbus_hash_iter_set_value (&iter, value);
1992 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1995 _dbus_hash_iter_init (table1, &iter);
1996 while (_dbus_hash_iter_next (&iter))
1998 _dbus_hash_iter_remove_entry (&iter);
1999 _dbus_assert (count_entries (table1) == i - 1);
2003 _dbus_hash_iter_init (table2, &iter);
2004 while (_dbus_hash_iter_next (&iter))
2009 key = _dbus_hash_iter_get_int_key (&iter);
2010 value = _dbus_hash_iter_get_value (&iter);
2012 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
2014 value = _dbus_strdup ("Different value!");
2018 _dbus_hash_iter_set_value (&iter, value);
2020 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
2023 i = count_entries (table2);
2024 _dbus_hash_iter_init (table2, &iter);
2025 while (_dbus_hash_iter_next (&iter))
2027 _dbus_hash_iter_remove_entry (&iter);
2028 _dbus_assert (count_entries (table2) + 1 == i);
2032 /* add/remove interleaved, to check that we grow/shrink the table
2041 key = _dbus_strdup (keys[i]);
2045 value = _dbus_strdup ("Value!");
2049 if (!_dbus_hash_table_insert_string (table1,
2062 key = _dbus_strdup (keys[i]);
2065 value = _dbus_strdup ("Value!");
2069 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2072 if (!_dbus_hash_table_insert_string (table1,
2076 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2079 _dbus_assert (_dbus_hash_table_get_n_entries (table1) == i);
2084 /* nuke these tables */
2085 _dbus_hash_table_unref (table1);
2086 _dbus_hash_table_unref (table2);
2089 /* Now do a bunch of things again using _dbus_hash_iter_lookup() to
2090 * be sure that interface works.
2092 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
2093 dbus_free, dbus_free);
2097 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
2108 key = _dbus_strdup (keys[i]);
2111 value = _dbus_strdup ("Value!");
2115 if (!_dbus_hash_iter_lookup (table1,
2118 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2119 _dbus_hash_iter_set_value (&iter, value);
2121 value = _dbus_strdup (keys[i]);
2125 if (!_dbus_hash_iter_lookup (table2,
2126 _DBUS_INT_TO_POINTER (i), TRUE, &iter))
2128 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2129 _dbus_hash_iter_set_value (&iter, value);
2131 _dbus_assert (count_entries (table1) == i + 1);
2132 _dbus_assert (count_entries (table2) == i + 1);
2134 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2137 value = _dbus_hash_iter_get_value (&iter);
2138 _dbus_assert (value != NULL);
2139 _dbus_assert (strcmp (value, "Value!") == 0);
2141 /* Iterate just to be sure it works, though
2142 * it's a stupid thing to do
2144 while (_dbus_hash_iter_next (&iter))
2147 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2150 value = _dbus_hash_iter_get_value (&iter);
2151 _dbus_assert (value != NULL);
2152 _dbus_assert (strcmp (value, keys[i]) == 0);
2154 /* Iterate just to be sure it works, though
2155 * it's a stupid thing to do
2157 while (_dbus_hash_iter_next (&iter))
2166 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2167 _dbus_assert_not_reached ("hash entry should have existed");
2168 _dbus_hash_iter_remove_entry (&iter);
2170 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2171 _dbus_assert_not_reached ("hash entry should have existed");
2172 _dbus_hash_iter_remove_entry (&iter);
2174 _dbus_assert (count_entries (table1) == i);
2175 _dbus_assert (count_entries (table2) == i);
2180 _dbus_hash_table_unref (table1);
2181 _dbus_hash_table_unref (table2);
2186 for (i = 0; i < N_HASH_KEYS; i++)
2187 dbus_free (keys[i]);
2194 #endif /* DBUS_BUILD_TESTS */