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.0
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 static DBusHashEntry* find_two_strings_function (DBusHashTable *table,
236 dbus_bool_t create_if_not_found,
237 DBusHashEntry ***bucket,
238 DBusPreallocatedHash *preallocated);
239 static unsigned int string_hash (const char *str);
240 static unsigned int two_strings_hash (const char *str);
241 static void rebuild_table (DBusHashTable *table);
242 static DBusHashEntry* alloc_entry (DBusHashTable *table);
243 static void remove_entry (DBusHashTable *table,
244 DBusHashEntry **bucket,
245 DBusHashEntry *entry);
246 static void free_entry (DBusHashTable *table,
247 DBusHashEntry *entry);
248 static void free_entry_data (DBusHashTable *table,
249 DBusHashEntry *entry);
255 * @addtogroup DBusHashTable
260 * @typedef DBusHashIter
262 * Public opaque hash table iterator object.
266 * @typedef DBusHashTable
268 * Public opaque hash table object.
272 * @typedef DBusHashType
274 * Indicates the type of a key in the hash table.
278 * Constructs a new hash table. Should be freed with
279 * _dbus_hash_table_unref(). If memory cannot be
280 * allocated for the hash table, returns #NULL.
282 * @param type the type of hash key to use.
283 * @param key_free_function function to free hash keys.
284 * @param value_free_function function to free hash values.
285 * @returns a new DBusHashTable or #NULL if no memory.
288 _dbus_hash_table_new (DBusHashType type,
289 DBusFreeFunction key_free_function,
290 DBusFreeFunction value_free_function)
292 DBusHashTable *table;
293 DBusMemPool *entry_pool;
295 table = dbus_new0 (DBusHashTable, 1);
299 entry_pool = _dbus_mem_pool_new (sizeof (DBusHashEntry), TRUE);
300 if (entry_pool == NULL)
307 table->entry_pool = entry_pool;
309 _dbus_assert (DBUS_SMALL_HASH_TABLE == _DBUS_N_ELEMENTS (table->static_buckets));
311 table->buckets = table->static_buckets;
312 table->n_buckets = DBUS_SMALL_HASH_TABLE;
313 table->n_entries = 0;
314 table->hi_rebuild_size = DBUS_SMALL_HASH_TABLE * REBUILD_MULTIPLIER;
315 table->lo_rebuild_size = 0;
316 table->down_shift = 28;
318 table->key_type = type;
320 _dbus_assert (table->mask < table->n_buckets);
322 switch (table->key_type)
325 case DBUS_HASH_POINTER:
326 case DBUS_HASH_ULONG:
327 table->find_function = find_direct_function;
329 case DBUS_HASH_STRING:
330 table->find_function = find_string_function;
332 case DBUS_HASH_TWO_STRINGS:
333 table->find_function = find_two_strings_function;
336 _dbus_assert_not_reached ("Unknown hash table type");
340 table->free_key_function = key_free_function;
341 table->free_value_function = value_free_function;
348 * Increments the reference count for a hash table.
350 * @param table the hash table to add a reference to.
351 * @returns the hash table.
354 _dbus_hash_table_ref (DBusHashTable *table)
356 table->refcount += 1;
362 * Decrements the reference count for a hash table,
363 * freeing the hash table if the count reaches zero.
365 * @param table the hash table to remove a reference from.
368 _dbus_hash_table_unref (DBusHashTable *table)
370 table->refcount -= 1;
372 if (table->refcount == 0)
375 DBusHashEntry *entry;
379 /* Free the entries in the table. */
380 for (i = 0; i < table->n_buckets; i++)
382 entry = table->buckets[i];
383 while (entry != NULL)
387 free_entry (table, entry);
393 DBusHashEntry *entry;
396 /* Free the entries in the table. */
397 for (i = 0; i < table->n_buckets; i++)
399 entry = table->buckets[i];
400 while (entry != NULL)
402 free_entry_data (table, entry);
407 /* We can do this very quickly with memory pools ;-) */
408 _dbus_mem_pool_free (table->entry_pool);
411 /* Free the bucket array, if it was dynamically allocated. */
412 if (table->buckets != table->static_buckets)
413 dbus_free (table->buckets);
419 static DBusHashEntry*
420 alloc_entry (DBusHashTable *table)
422 DBusHashEntry *entry;
424 entry = _dbus_mem_pool_alloc (table->entry_pool);
430 free_entry_data (DBusHashTable *table,
431 DBusHashEntry *entry)
433 if (table->free_key_function)
434 (* table->free_key_function) (entry->key);
435 if (table->free_value_function)
436 (* table->free_value_function) (entry->value);
440 free_entry (DBusHashTable *table,
441 DBusHashEntry *entry)
443 free_entry_data (table, entry);
444 _dbus_mem_pool_dealloc (table->entry_pool, entry);
448 remove_entry (DBusHashTable *table,
449 DBusHashEntry **bucket,
450 DBusHashEntry *entry)
452 _dbus_assert (table != NULL);
453 _dbus_assert (bucket != NULL);
454 _dbus_assert (*bucket != NULL);
455 _dbus_assert (entry != NULL);
457 if (*bucket == entry)
458 *bucket = entry->next;
464 while (prev->next != entry)
467 _dbus_assert (prev != NULL);
469 prev->next = entry->next;
472 table->n_entries -= 1;
473 free_entry (table, entry);
477 * Initializes a hash table iterator. To iterate over all entries in a
478 * hash table, use the following code (the printf assumes a hash
479 * from strings to strings obviously):
484 * _dbus_hash_iter_init (table, &iter);
485 * while (_dbus_hash_iter_next (&iter))
487 * printf ("The first key is %s and value is %s\n",
488 * _dbus_hash_iter_get_string_key (&iter),
489 * _dbus_hash_iter_get_value (&iter));
495 * The iterator is initialized pointing "one before" the first hash
496 * entry. The first call to _dbus_hash_iter_next() moves it onto
497 * the first valid entry or returns #FALSE if the hash table is
498 * empty. Subsequent calls move to the next valid entry or return
499 * #FALSE if there are no more entries.
501 * Note that it is guaranteed to be safe to remove a hash entry during
502 * iteration, but it is not safe to add a hash entry.
504 * @param table the hash table to iterate over.
505 * @param iter the iterator to initialize.
508 _dbus_hash_iter_init (DBusHashTable *table,
511 DBusRealHashIter *real;
513 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
515 real = (DBusRealHashIter*) iter;
520 real->next_entry = NULL;
521 real->next_bucket = 0;
522 real->n_entries_on_init = table->n_entries;
526 * Move the hash iterator forward one step, to the next hash entry.
527 * The documentation for _dbus_hash_iter_init() explains in more
530 * @param iter the iterator to move forward.
531 * @returns #FALSE if there are no more entries to move to.
534 _dbus_hash_iter_next (DBusHashIter *iter)
536 DBusRealHashIter *real;
538 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
540 real = (DBusRealHashIter*) iter;
542 /* if this assertion failed someone probably added hash entries
543 * during iteration, which is bad.
545 _dbus_assert (real->n_entries_on_init >= real->table->n_entries);
547 /* Remember that real->entry may have been deleted */
549 while (real->next_entry == NULL)
551 if (real->next_bucket >= real->table->n_buckets)
553 /* invalidate iter and return false */
560 real->bucket = &(real->table->buckets[real->next_bucket]);
561 real->next_entry = *(real->bucket);
562 real->next_bucket += 1;
565 _dbus_assert (real->next_entry != NULL);
566 _dbus_assert (real->bucket != NULL);
568 real->entry = real->next_entry;
569 real->next_entry = real->entry->next;
575 * Removes the current entry from the hash table.
576 * If a key_free_function or value_free_function
577 * was provided to _dbus_hash_table_new(),
578 * frees the key and/or value for this entry.
580 * @param iter the hash table iterator.
583 _dbus_hash_iter_remove_entry (DBusHashIter *iter)
585 DBusRealHashIter *real;
587 real = (DBusRealHashIter*) iter;
589 _dbus_assert (real->table != NULL);
590 _dbus_assert (real->entry != NULL);
591 _dbus_assert (real->bucket != NULL);
593 remove_entry (real->table, real->bucket, real->entry);
595 real->entry = NULL; /* make it crash if you try to use this entry */
599 * Gets the value of the current entry.
601 * @param iter the hash table iterator.
604 _dbus_hash_iter_get_value (DBusHashIter *iter)
606 DBusRealHashIter *real;
608 real = (DBusRealHashIter*) iter;
610 _dbus_assert (real->table != NULL);
611 _dbus_assert (real->entry != NULL);
613 return real->entry->value;
617 * Sets the value of the current entry.
618 * If the hash table has a value_free_function
619 * it will be used to free the previous value.
620 * The hash table will own the passed-in value
621 * (it will not be copied).
623 * @param iter the hash table iterator.
624 * @param value the new value.
627 _dbus_hash_iter_set_value (DBusHashIter *iter,
630 DBusRealHashIter *real;
632 real = (DBusRealHashIter*) iter;
634 _dbus_assert (real->table != NULL);
635 _dbus_assert (real->entry != NULL);
637 if (real->table->free_value_function && value != real->entry->value)
638 (* real->table->free_value_function) (real->entry->value);
640 real->entry->value = value;
644 * Gets the key for the current entry.
645 * Only works for hash tables of type #DBUS_HASH_INT.
647 * @param iter the hash table iterator.
650 _dbus_hash_iter_get_int_key (DBusHashIter *iter)
652 DBusRealHashIter *real;
654 real = (DBusRealHashIter*) iter;
656 _dbus_assert (real->table != NULL);
657 _dbus_assert (real->entry != NULL);
659 return _DBUS_POINTER_TO_INT (real->entry->key);
663 * Gets the key for the current entry.
664 * Only works for hash tables of type #DBUS_HASH_ULONG.
666 * @param iter the hash table iterator.
669 _dbus_hash_iter_get_ulong_key (DBusHashIter *iter)
671 DBusRealHashIter *real;
673 real = (DBusRealHashIter*) iter;
675 _dbus_assert (real->table != NULL);
676 _dbus_assert (real->entry != NULL);
678 return (unsigned long) real->entry->key;
682 * Gets the key for the current entry.
683 * Only works for hash tables of type #DBUS_HASH_STRING
684 * @param iter the hash table iterator.
687 _dbus_hash_iter_get_string_key (DBusHashIter *iter)
689 DBusRealHashIter *real;
691 real = (DBusRealHashIter*) iter;
693 _dbus_assert (real->table != NULL);
694 _dbus_assert (real->entry != NULL);
696 return real->entry->key;
700 * Gets the key for the current entry.
701 * Only works for hash tables of type #DBUS_HASH_TWO_STRINGS
702 * @param iter the hash table iterator.
705 _dbus_hash_iter_get_two_strings_key (DBusHashIter *iter)
707 DBusRealHashIter *real;
709 real = (DBusRealHashIter*) iter;
711 _dbus_assert (real->table != NULL);
712 _dbus_assert (real->entry != NULL);
714 return real->entry->key;
718 * A low-level but efficient interface for manipulating the hash
719 * table. It's efficient because you can get, set, and optionally
720 * create the hash entry while only running the hash function one
723 * Note that while calling _dbus_hash_iter_next() on the iterator
724 * filled in by this function may work, it's completely
725 * undefined which entries are after this iter and which
726 * are before it. So it would be silly to iterate using this
729 * If the hash entry is created, its value will be initialized
732 * #FALSE may be returned due to memory allocation failure, or
733 * because create_if_not_found was #FALSE and the entry
736 * If create_if_not_found is #TRUE and the entry is created, the hash
737 * table takes ownership of the key that's passed in.
739 * For a hash table of type #DBUS_HASH_INT, cast the int
740 * key to the key parameter using #_DBUS_INT_TO_POINTER().
742 * @param table the hash table.
743 * @param key the hash key.
744 * @param create_if_not_found if #TRUE, create the entry if it didn't exist.
745 * @param iter the iterator to initialize.
746 * @returns #TRUE if the hash entry now exists (and the iterator is thus valid).
749 _dbus_hash_iter_lookup (DBusHashTable *table,
751 dbus_bool_t create_if_not_found,
754 DBusRealHashIter *real;
755 DBusHashEntry *entry;
756 DBusHashEntry **bucket;
758 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
760 real = (DBusRealHashIter*) iter;
762 entry = (* table->find_function) (table, key, create_if_not_found, &bucket, NULL);
768 real->bucket = bucket;
770 real->next_entry = entry->next;
771 real->next_bucket = (bucket - table->buckets) + 1;
772 real->n_entries_on_init = table->n_entries;
774 _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
780 add_allocated_entry (DBusHashTable *table,
781 DBusHashEntry *entry,
784 DBusHashEntry ***bucket)
790 b = &(table->buckets[idx]);
797 table->n_entries += 1;
799 /* note we ONLY rebuild when ADDING - because you can iterate over a
800 * table and remove entries safely.
802 if (table->n_entries >= table->hi_rebuild_size ||
803 table->n_entries < table->lo_rebuild_size)
804 rebuild_table (table);
807 static DBusHashEntry*
808 add_entry (DBusHashTable *table,
811 DBusHashEntry ***bucket,
812 DBusPreallocatedHash *preallocated)
814 DBusHashEntry *entry;
816 if (preallocated == NULL)
818 entry = alloc_entry (table);
828 entry = (DBusHashEntry*) preallocated;
831 add_allocated_entry (table, entry, idx, key, bucket);
836 /* This is g_str_hash from GLib which was
837 * extensively discussed/tested/profiled
840 string_hash (const char *str)
846 for (p += 1; *p != '\0'; p++)
847 h = (h << 5) - h + *p;
852 /* This hashes a memory block with two nul-terminated strings
853 * in it, used in dbus-object-registry.c at the moment.
856 two_strings_hash (const char *str)
862 for (p += 1; *p != '\0'; p++)
863 h = (h << 5) - h + *p;
865 for (p += 1; *p != '\0'; p++)
866 h = (h << 5) - h + *p;
871 /** Key comparison function */
872 typedef int (* KeyCompareFunc) (const void *key_a, const void *key_b);
874 static DBusHashEntry*
875 find_generic_function (DBusHashTable *table,
878 KeyCompareFunc compare_func,
879 dbus_bool_t create_if_not_found,
880 DBusHashEntry ***bucket,
881 DBusPreallocatedHash *preallocated)
883 DBusHashEntry *entry;
888 /* Search all of the entries in this bucket. */
889 entry = table->buckets[idx];
890 while (entry != NULL)
892 if ((compare_func == NULL && key == entry->key) ||
893 (compare_func != NULL && (* compare_func) (key, entry->key) == 0))
896 *bucket = &(table->buckets[idx]);
899 _dbus_hash_table_free_preallocated_entry (table, preallocated);
907 if (create_if_not_found)
908 entry = add_entry (table, idx, key, bucket, preallocated);
909 else if (preallocated)
910 _dbus_hash_table_free_preallocated_entry (table, preallocated);
915 static DBusHashEntry*
916 find_string_function (DBusHashTable *table,
918 dbus_bool_t create_if_not_found,
919 DBusHashEntry ***bucket,
920 DBusPreallocatedHash *preallocated)
924 idx = string_hash (key) & table->mask;
926 return find_generic_function (table, key, idx,
927 (KeyCompareFunc) strcmp, create_if_not_found, bucket,
932 two_strings_cmp (const char *a,
946 return strcmp (a + len_a + 1, b + len_b + 1);
949 static DBusHashEntry*
950 find_two_strings_function (DBusHashTable *table,
952 dbus_bool_t create_if_not_found,
953 DBusHashEntry ***bucket,
954 DBusPreallocatedHash *preallocated)
958 idx = two_strings_hash (key) & table->mask;
960 return find_generic_function (table, key, idx,
961 (KeyCompareFunc) two_strings_cmp, create_if_not_found, bucket,
965 static DBusHashEntry*
966 find_direct_function (DBusHashTable *table,
968 dbus_bool_t create_if_not_found,
969 DBusHashEntry ***bucket,
970 DBusPreallocatedHash *preallocated)
974 idx = RANDOM_INDEX (table, key) & table->mask;
977 return find_generic_function (table, key, idx,
978 NULL, create_if_not_found, bucket,
983 rebuild_table (DBusHashTable *table)
987 DBusHashEntry **old_buckets;
988 DBusHashEntry **old_chain;
989 DBusHashEntry *entry;
993 * Allocate and initialize the new bucket array, and set up
994 * hashing constants for new array size.
997 growing = table->n_entries >= table->hi_rebuild_size;
999 old_size = table->n_buckets;
1000 old_buckets = table->buckets;
1004 /* overflow paranoia */
1005 if (table->n_buckets < _DBUS_INT_MAX / 4 &&
1006 table->down_shift >= 0)
1007 new_buckets = table->n_buckets * 4;
1009 return; /* can't grow anymore */
1013 new_buckets = table->n_buckets / 4;
1014 if (new_buckets < DBUS_SMALL_HASH_TABLE)
1015 return; /* don't bother shrinking this far */
1018 table->buckets = dbus_new0 (DBusHashEntry*, new_buckets);
1019 if (table->buckets == NULL)
1021 /* out of memory, yay - just don't reallocate, the table will
1022 * still work, albeit more slowly.
1024 table->buckets = old_buckets;
1028 table->n_buckets = new_buckets;
1032 table->lo_rebuild_size = table->hi_rebuild_size;
1033 table->hi_rebuild_size *= 4;
1035 table->down_shift -= 2; /* keep 2 more high bits */
1036 table->mask = (table->mask << 2) + 3; /* keep 2 more high bits */
1040 table->hi_rebuild_size = table->lo_rebuild_size;
1041 table->lo_rebuild_size /= 4;
1043 table->down_shift += 2; /* keep 2 fewer high bits */
1044 table->mask = table->mask >> 2; /* keep 2 fewer high bits */
1048 printf ("%s table to lo = %d hi = %d downshift = %d mask = 0x%x\n",
1049 growing ? "GROW" : "SHRINK",
1050 table->lo_rebuild_size,
1051 table->hi_rebuild_size,
1056 _dbus_assert (table->lo_rebuild_size >= 0);
1057 _dbus_assert (table->hi_rebuild_size > table->lo_rebuild_size);
1058 _dbus_assert (table->mask != 0);
1059 /* the mask is essentially the max index */
1060 _dbus_assert (table->mask < table->n_buckets);
1063 * Rehash all of the existing entries into the new bucket array.
1066 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
1068 for (entry = *old_chain; entry != NULL; entry = *old_chain)
1071 DBusHashEntry **bucket;
1073 *old_chain = entry->next;
1074 switch (table->key_type)
1076 case DBUS_HASH_STRING:
1077 idx = string_hash (entry->key) & table->mask;
1079 case DBUS_HASH_TWO_STRINGS:
1080 idx = two_strings_hash (entry->key) & table->mask;
1083 case DBUS_HASH_ULONG:
1084 case DBUS_HASH_POINTER:
1085 idx = RANDOM_INDEX (table, entry->key);
1089 _dbus_assert_not_reached ("Unknown hash table type");
1093 bucket = &(table->buckets[idx]);
1094 entry->next = *bucket;
1099 /* Free the old bucket array, if it was dynamically allocated. */
1101 if (old_buckets != table->static_buckets)
1102 dbus_free (old_buckets);
1106 * Looks up the value for a given string in a hash table
1107 * of type #DBUS_HASH_STRING. Returns %NULL if the value
1108 * is not present. (A not-present entry is indistinguishable
1109 * from an entry with a value of %NULL.)
1110 * @param table the hash table.
1111 * @param key the string to look up.
1112 * @returns the value of the hash entry.
1115 _dbus_hash_table_lookup_string (DBusHashTable *table,
1118 DBusHashEntry *entry;
1120 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1122 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1125 return entry->value;
1131 * Looks up the value for a given string in a hash table
1132 * of type #DBUS_HASH_TWO_STRINGS. Returns %NULL if the value
1133 * is not present. (A not-present entry is indistinguishable
1134 * from an entry with a value of %NULL.)
1135 * @param table the hash table.
1136 * @param key the string to look up.
1137 * @returns the value of the hash entry.
1140 _dbus_hash_table_lookup_two_strings (DBusHashTable *table,
1143 DBusHashEntry *entry;
1145 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1147 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1150 return entry->value;
1156 * Looks up the value for a given integer in a hash table
1157 * of type #DBUS_HASH_INT. Returns %NULL if the value
1158 * is not present. (A not-present entry is indistinguishable
1159 * from an entry with a value of %NULL.)
1160 * @param table the hash table.
1161 * @param key the integer to look up.
1162 * @returns the value of the hash entry.
1165 _dbus_hash_table_lookup_int (DBusHashTable *table,
1168 DBusHashEntry *entry;
1170 _dbus_assert (table->key_type == DBUS_HASH_INT);
1172 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, NULL, NULL);
1175 return entry->value;
1180 #ifdef DBUS_BUILD_TESTS
1181 /* disabled since it's only used for testing */
1183 * Looks up the value for a given integer in a hash table
1184 * of type #DBUS_HASH_POINTER. Returns %NULL if the value
1185 * is not present. (A not-present entry is indistinguishable
1186 * from an entry with a value of %NULL.)
1187 * @param table the hash table.
1188 * @param key the integer to look up.
1189 * @returns the value of the hash entry.
1192 _dbus_hash_table_lookup_pointer (DBusHashTable *table,
1195 DBusHashEntry *entry;
1197 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1199 entry = (* table->find_function) (table, key, FALSE, NULL, NULL);
1202 return entry->value;
1206 #endif /* DBUS_BUILD_TESTS */
1209 * Looks up the value for a given integer in a hash table
1210 * of type #DBUS_HASH_ULONG. Returns %NULL if the value
1211 * is not present. (A not-present entry is indistinguishable
1212 * from an entry with a value of %NULL.)
1213 * @param table the hash table.
1214 * @param key the integer to look up.
1215 * @returns the value of the hash entry.
1218 _dbus_hash_table_lookup_ulong (DBusHashTable *table,
1221 DBusHashEntry *entry;
1223 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1225 entry = (* table->find_function) (table, (void*) key, FALSE, NULL, NULL);
1228 return entry->value;
1234 * Removes the hash entry for the given key. If no hash entry
1235 * for the key exists, does nothing.
1237 * @param table the hash table.
1238 * @param key the hash key.
1239 * @returns #TRUE if the entry existed
1242 _dbus_hash_table_remove_string (DBusHashTable *table,
1245 DBusHashEntry *entry;
1246 DBusHashEntry **bucket;
1248 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1250 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1254 remove_entry (table, bucket, entry);
1262 * Removes the hash entry for the given key. If no hash entry
1263 * for the key exists, does nothing.
1265 * @param table the hash table.
1266 * @param key the hash key.
1267 * @returns #TRUE if the entry existed
1270 _dbus_hash_table_remove_two_strings (DBusHashTable *table,
1273 DBusHashEntry *entry;
1274 DBusHashEntry **bucket;
1276 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1278 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1282 remove_entry (table, bucket, entry);
1290 * Removes the hash entry for the given key. If no hash entry
1291 * for the key exists, does nothing.
1293 * @param table the hash table.
1294 * @param key the hash key.
1295 * @returns #TRUE if the entry existed
1298 _dbus_hash_table_remove_int (DBusHashTable *table,
1301 DBusHashEntry *entry;
1302 DBusHashEntry **bucket;
1304 _dbus_assert (table->key_type == DBUS_HASH_INT);
1306 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, &bucket, NULL);
1310 remove_entry (table, bucket, entry);
1317 #ifdef DBUS_BUILD_TESTS
1318 /* disabled since it's only used for testing */
1320 * Removes the hash entry for the given key. If no hash entry
1321 * for the key exists, does nothing.
1323 * @param table the hash table.
1324 * @param key the hash key.
1325 * @returns #TRUE if the entry existed
1328 _dbus_hash_table_remove_pointer (DBusHashTable *table,
1331 DBusHashEntry *entry;
1332 DBusHashEntry **bucket;
1334 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1336 entry = (* table->find_function) (table, key, FALSE, &bucket, NULL);
1340 remove_entry (table, bucket, entry);
1346 #endif /* DBUS_BUILD_TESTS */
1349 * Removes the hash entry for the given key. If no hash entry
1350 * for the key exists, does nothing.
1352 * @param table the hash table.
1353 * @param key the hash key.
1354 * @returns #TRUE if the entry existed
1357 _dbus_hash_table_remove_ulong (DBusHashTable *table,
1360 DBusHashEntry *entry;
1361 DBusHashEntry **bucket;
1363 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1365 entry = (* table->find_function) (table, (void*) key, FALSE, &bucket, NULL);
1369 remove_entry (table, bucket, entry);
1377 * Creates a hash entry with the given key and value.
1378 * The key and value are not copied; they are stored
1379 * in the hash table by reference. If an entry with the
1380 * given key already exists, the previous key and value
1381 * are overwritten (and freed if the hash table has
1382 * a key_free_function and/or value_free_function).
1384 * Returns #FALSE if memory for the new hash entry
1385 * can't be allocated.
1387 * @param table the hash table.
1388 * @param key the hash entry key.
1389 * @param value the hash entry value.
1392 _dbus_hash_table_insert_string (DBusHashTable *table,
1396 DBusPreallocatedHash *preallocated;
1398 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1400 preallocated = _dbus_hash_table_preallocate_entry (table);
1401 if (preallocated == NULL)
1404 _dbus_hash_table_insert_string_preallocated (table, preallocated,
1411 * Creates a hash entry with the given key and value.
1412 * The key and value are not copied; they are stored
1413 * in the hash table by reference. If an entry with the
1414 * given key already exists, the previous key and value
1415 * are overwritten (and freed if the hash table has
1416 * a key_free_function and/or value_free_function).
1418 * Returns #FALSE if memory for the new hash entry
1419 * can't be allocated.
1421 * @param table the hash table.
1422 * @param key the hash entry key.
1423 * @param value the hash entry value.
1426 _dbus_hash_table_insert_two_strings (DBusHashTable *table,
1430 DBusHashEntry *entry;
1432 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1434 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1437 return FALSE; /* no memory */
1439 if (table->free_key_function && entry->key != key)
1440 (* table->free_key_function) (entry->key);
1442 if (table->free_value_function && entry->value != value)
1443 (* table->free_value_function) (entry->value);
1446 entry->value = value;
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_int (DBusHashTable *table,
1471 DBusHashEntry *entry;
1473 _dbus_assert (table->key_type == DBUS_HASH_INT);
1475 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), TRUE, NULL, NULL);
1478 return FALSE; /* no memory */
1480 if (table->free_key_function && entry->key != _DBUS_INT_TO_POINTER (key))
1481 (* table->free_key_function) (entry->key);
1483 if (table->free_value_function && entry->value != value)
1484 (* table->free_value_function) (entry->value);
1486 entry->key = _DBUS_INT_TO_POINTER (key);
1487 entry->value = value;
1492 #ifdef DBUS_BUILD_TESTS
1493 /* disabled since it's only used for testing */
1495 * Creates a hash entry with the given key and value.
1496 * The key and value are not copied; they are stored
1497 * in the hash table by reference. If an entry with the
1498 * given key already exists, the previous key and value
1499 * are overwritten (and freed if the hash table has
1500 * a key_free_function and/or value_free_function).
1502 * Returns #FALSE if memory for the new hash entry
1503 * can't be allocated.
1505 * @param table the hash table.
1506 * @param key the hash entry key.
1507 * @param value the hash entry value.
1510 _dbus_hash_table_insert_pointer (DBusHashTable *table,
1514 DBusHashEntry *entry;
1516 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1518 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1521 return FALSE; /* no memory */
1523 if (table->free_key_function && entry->key != key)
1524 (* table->free_key_function) (entry->key);
1526 if (table->free_value_function && entry->value != value)
1527 (* table->free_value_function) (entry->value);
1530 entry->value = value;
1534 #endif /* DBUS_BUILD_TESTS */
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_ulong (DBusHashTable *table,
1556 DBusHashEntry *entry;
1558 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1560 entry = (* table->find_function) (table, (void*) key, TRUE, NULL, NULL);
1563 return FALSE; /* no memory */
1565 if (table->free_key_function && entry->key != (void*) key)
1566 (* table->free_key_function) (entry->key);
1568 if (table->free_value_function && entry->value != value)
1569 (* table->free_value_function) (entry->value);
1571 entry->key = (void*) key;
1572 entry->value = value;
1578 * Preallocate an opaque data blob that allows us to insert into the
1579 * hash table at a later time without allocating any memory.
1581 * @param table the hash table
1582 * @returns the preallocated data, or #NULL if no memory
1584 DBusPreallocatedHash*
1585 _dbus_hash_table_preallocate_entry (DBusHashTable *table)
1587 DBusHashEntry *entry;
1589 entry = alloc_entry (table);
1591 return (DBusPreallocatedHash*) entry;
1595 * Frees an opaque DBusPreallocatedHash that was *not* used
1596 * in order to insert into the hash table.
1598 * @param table the hash table
1599 * @param preallocated the preallocated data
1602 _dbus_hash_table_free_preallocated_entry (DBusHashTable *table,
1603 DBusPreallocatedHash *preallocated)
1605 DBusHashEntry *entry;
1607 _dbus_assert (preallocated != NULL);
1609 entry = (DBusHashEntry*) preallocated;
1611 /* Don't use free_entry(), since this entry has no key/data */
1612 _dbus_mem_pool_dealloc (table->entry_pool, entry);
1616 * Inserts a string-keyed entry into the hash table, using a
1617 * preallocated data block from
1618 * _dbus_hash_table_preallocate_entry(). This function cannot fail due
1619 * to lack of memory. The DBusPreallocatedHash object is consumed and
1620 * should not be reused or freed. Otherwise this function works
1621 * just like _dbus_hash_table_insert_string().
1623 * @param table the hash table
1624 * @param preallocated the preallocated data
1625 * @param key the hash key
1626 * @param value the value
1629 _dbus_hash_table_insert_string_preallocated (DBusHashTable *table,
1630 DBusPreallocatedHash *preallocated,
1634 DBusHashEntry *entry;
1636 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1637 _dbus_assert (preallocated != NULL);
1639 entry = (* table->find_function) (table, key, TRUE, NULL, preallocated);
1641 _dbus_assert (entry != NULL);
1643 if (table->free_key_function && entry->key != key)
1644 (* table->free_key_function) (entry->key);
1646 if (table->free_value_function && entry->value != value)
1647 (* table->free_value_function) (entry->value);
1650 entry->value = value;
1654 * Gets the number of hash entries in a hash table.
1656 * @param table the hash table.
1657 * @returns the number of entries in the table.
1660 _dbus_hash_table_get_n_entries (DBusHashTable *table)
1662 return table->n_entries;
1667 #ifdef DBUS_BUILD_TESTS
1668 #include "dbus-test.h"
1671 /* If you're wondering why the hash table test takes
1672 * forever to run, it's because we call this function
1673 * in inner loops thus making things quadratic.
1676 count_entries (DBusHashTable *table)
1682 _dbus_hash_iter_init (table, &iter);
1683 while (_dbus_hash_iter_next (&iter))
1686 _dbus_assert (count == _dbus_hash_table_get_n_entries (table));
1691 /* Copy the foo\0bar\0 double string thing */
1693 _dbus_strdup2 (const char *str)
1702 len += strlen ((str + len + 1));
1704 copy = dbus_malloc (len + 2);
1708 memcpy (copy, str, len + 2);
1714 * @ingroup DBusHashTableInternals
1715 * Unit test for DBusHashTable
1716 * @returns #TRUE on success.
1719 _dbus_hash_test (void)
1722 DBusHashTable *table1;
1723 DBusHashTable *table2;
1724 DBusHashTable *table3;
1725 DBusHashTable *table4;
1727 #define N_HASH_KEYS 5000
1729 dbus_bool_t ret = FALSE;
1731 keys = dbus_new (char *, N_HASH_KEYS);
1733 _dbus_assert_not_reached ("no memory");
1735 for (i = 0; i < N_HASH_KEYS; i++)
1737 keys[i] = dbus_malloc (128);
1739 if (keys[i] == NULL)
1740 _dbus_assert_not_reached ("no memory");
1743 printf ("Computing test hash keys...\n");
1745 while (i < N_HASH_KEYS)
1749 /* all the hash keys are TWO_STRINGS, but
1750 * then we can also use those as regular strings.
1753 len = sprintf (keys[i], "Hash key %d", i);
1754 sprintf (keys[i] + len + 1, "Two string %d", i);
1755 _dbus_assert (*(keys[i] + len) == '\0');
1756 _dbus_assert (*(keys[i] + len + 1) != '\0');
1759 printf ("... done.\n");
1761 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1762 dbus_free, dbus_free);
1766 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1771 table3 = _dbus_hash_table_new (DBUS_HASH_ULONG,
1776 table4 = _dbus_hash_table_new (DBUS_HASH_TWO_STRINGS,
1777 dbus_free, dbus_free);
1782 /* Insert and remove a bunch of stuff, counting the table in between
1783 * to be sure it's not broken and that iteration works
1791 key = _dbus_strdup (keys[i]);
1794 value = _dbus_strdup ("Value!");
1798 if (!_dbus_hash_table_insert_string (table1,
1802 value = _dbus_strdup (keys[i]);
1806 if (!_dbus_hash_table_insert_int (table2,
1810 value = _dbus_strdup (keys[i]);
1814 if (!_dbus_hash_table_insert_ulong (table3,
1818 key = _dbus_strdup2 (keys[i]);
1821 value = _dbus_strdup ("Value!");
1825 if (!_dbus_hash_table_insert_two_strings (table4,
1829 _dbus_assert (count_entries (table1) == i + 1);
1830 _dbus_assert (count_entries (table2) == i + 1);
1831 _dbus_assert (count_entries (table3) == i + 1);
1832 _dbus_assert (count_entries (table4) == i + 1);
1834 value = _dbus_hash_table_lookup_string (table1, keys[i]);
1835 _dbus_assert (value != NULL);
1836 _dbus_assert (strcmp (value, "Value!") == 0);
1838 value = _dbus_hash_table_lookup_int (table2, i);
1839 _dbus_assert (value != NULL);
1840 _dbus_assert (strcmp (value, keys[i]) == 0);
1842 value = _dbus_hash_table_lookup_ulong (table3, i);
1843 _dbus_assert (value != NULL);
1844 _dbus_assert (strcmp (value, keys[i]) == 0);
1846 value = _dbus_hash_table_lookup_two_strings (table4, keys[i]);
1847 _dbus_assert (value != NULL);
1848 _dbus_assert (strcmp (value, "Value!") == 0);
1856 _dbus_hash_table_remove_string (table1,
1859 _dbus_hash_table_remove_int (table2, i);
1861 _dbus_hash_table_remove_ulong (table3, i);
1863 _dbus_hash_table_remove_two_strings (table4,
1866 _dbus_assert (count_entries (table1) == i);
1867 _dbus_assert (count_entries (table2) == i);
1868 _dbus_assert (count_entries (table3) == i);
1869 _dbus_assert (count_entries (table4) == i);
1874 _dbus_hash_table_ref (table1);
1875 _dbus_hash_table_ref (table2);
1876 _dbus_hash_table_ref (table3);
1877 _dbus_hash_table_ref (table4);
1878 _dbus_hash_table_unref (table1);
1879 _dbus_hash_table_unref (table2);
1880 _dbus_hash_table_unref (table3);
1881 _dbus_hash_table_unref (table4);
1882 _dbus_hash_table_unref (table1);
1883 _dbus_hash_table_unref (table2);
1884 _dbus_hash_table_unref (table3);
1885 _dbus_hash_table_unref (table4);
1888 /* Insert a bunch of stuff then check
1889 * that iteration works correctly (finds the right
1890 * values, iter_set_value works, etc.)
1892 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1893 dbus_free, dbus_free);
1897 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1908 key = _dbus_strdup (keys[i]);
1911 value = _dbus_strdup ("Value!");
1915 if (!_dbus_hash_table_insert_string (table1,
1919 value = _dbus_strdup (keys[i]);
1923 if (!_dbus_hash_table_insert_int (table2,
1927 _dbus_assert (count_entries (table1) == i + 1);
1928 _dbus_assert (count_entries (table2) == i + 1);
1933 _dbus_hash_iter_init (table1, &iter);
1934 while (_dbus_hash_iter_next (&iter))
1939 key = _dbus_hash_iter_get_string_key (&iter);
1940 value = _dbus_hash_iter_get_value (&iter);
1942 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1944 value = _dbus_strdup ("Different value!");
1948 _dbus_hash_iter_set_value (&iter, value);
1950 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1953 _dbus_hash_iter_init (table1, &iter);
1954 while (_dbus_hash_iter_next (&iter))
1956 _dbus_hash_iter_remove_entry (&iter);
1957 _dbus_assert (count_entries (table1) == i - 1);
1961 _dbus_hash_iter_init (table2, &iter);
1962 while (_dbus_hash_iter_next (&iter))
1967 key = _dbus_hash_iter_get_int_key (&iter);
1968 value = _dbus_hash_iter_get_value (&iter);
1970 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1972 value = _dbus_strdup ("Different value!");
1976 _dbus_hash_iter_set_value (&iter, value);
1978 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1981 i = count_entries (table2);
1982 _dbus_hash_iter_init (table2, &iter);
1983 while (_dbus_hash_iter_next (&iter))
1985 _dbus_hash_iter_remove_entry (&iter);
1986 _dbus_assert (count_entries (table2) + 1 == i);
1990 /* add/remove interleaved, to check that we grow/shrink the table
1999 key = _dbus_strdup (keys[i]);
2003 value = _dbus_strdup ("Value!");
2007 if (!_dbus_hash_table_insert_string (table1,
2020 key = _dbus_strdup (keys[i]);
2023 value = _dbus_strdup ("Value!");
2027 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2030 if (!_dbus_hash_table_insert_string (table1,
2034 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2037 _dbus_assert (_dbus_hash_table_get_n_entries (table1) == i);
2042 /* nuke these tables */
2043 _dbus_hash_table_unref (table1);
2044 _dbus_hash_table_unref (table2);
2047 /* Now do a bunch of things again using _dbus_hash_iter_lookup() to
2048 * be sure that interface works.
2050 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
2051 dbus_free, dbus_free);
2055 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
2066 key = _dbus_strdup (keys[i]);
2069 value = _dbus_strdup ("Value!");
2073 if (!_dbus_hash_iter_lookup (table1,
2076 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2077 _dbus_hash_iter_set_value (&iter, value);
2079 value = _dbus_strdup (keys[i]);
2083 if (!_dbus_hash_iter_lookup (table2,
2084 _DBUS_INT_TO_POINTER (i), TRUE, &iter))
2086 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2087 _dbus_hash_iter_set_value (&iter, value);
2089 _dbus_assert (count_entries (table1) == i + 1);
2090 _dbus_assert (count_entries (table2) == i + 1);
2092 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2095 value = _dbus_hash_iter_get_value (&iter);
2096 _dbus_assert (value != NULL);
2097 _dbus_assert (strcmp (value, "Value!") == 0);
2099 /* Iterate just to be sure it works, though
2100 * it's a stupid thing to do
2102 while (_dbus_hash_iter_next (&iter))
2105 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2108 value = _dbus_hash_iter_get_value (&iter);
2109 _dbus_assert (value != NULL);
2110 _dbus_assert (strcmp (value, keys[i]) == 0);
2112 /* Iterate just to be sure it works, though
2113 * it's a stupid thing to do
2115 while (_dbus_hash_iter_next (&iter))
2124 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2125 _dbus_assert_not_reached ("hash entry should have existed");
2126 _dbus_hash_iter_remove_entry (&iter);
2128 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2129 _dbus_assert_not_reached ("hash entry should have existed");
2130 _dbus_hash_iter_remove_entry (&iter);
2132 _dbus_assert (count_entries (table1) == i);
2133 _dbus_assert (count_entries (table2) == i);
2138 _dbus_hash_table_unref (table1);
2139 _dbus_hash_table_unref (table2);
2144 for (i = 0; i < N_HASH_KEYS; i++)
2145 dbus_free (keys[i]);
2152 #endif /* DBUS_BUILD_TESTS */