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 1.2
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.
353 _dbus_hash_table_ref (DBusHashTable *table)
355 table->refcount += 1;
359 * Decrements the reference count for a hash table,
360 * freeing the hash table if the count reaches zero.
362 * @param table the hash table to remove a reference from.
365 _dbus_hash_table_unref (DBusHashTable *table)
367 table->refcount -= 1;
369 if (table->refcount == 0)
372 DBusHashEntry *entry;
376 /* Free the entries in the table. */
377 for (i = 0; i < table->n_buckets; i++)
379 entry = table->buckets[i];
380 while (entry != NULL)
384 free_entry (table, entry);
390 DBusHashEntry *entry;
393 /* Free the entries in the table. */
394 for (i = 0; i < table->n_buckets; i++)
396 entry = table->buckets[i];
397 while (entry != NULL)
399 free_entry_data (table, entry);
404 /* We can do this very quickly with memory pools ;-) */
405 _dbus_mem_pool_free (table->entry_pool);
408 /* Free the bucket array, if it was dynamically allocated. */
409 if (table->buckets != table->static_buckets)
410 dbus_free (table->buckets);
416 static DBusHashEntry*
417 alloc_entry (DBusHashTable *table)
419 DBusHashEntry *entry;
421 entry = _dbus_mem_pool_alloc (table->entry_pool);
427 free_entry_data (DBusHashTable *table,
428 DBusHashEntry *entry)
430 if (table->free_key_function)
431 (* table->free_key_function) (entry->key);
432 if (table->free_value_function)
433 (* table->free_value_function) (entry->value);
437 free_entry (DBusHashTable *table,
438 DBusHashEntry *entry)
440 free_entry_data (table, entry);
441 _dbus_mem_pool_dealloc (table->entry_pool, entry);
445 remove_entry (DBusHashTable *table,
446 DBusHashEntry **bucket,
447 DBusHashEntry *entry)
449 _dbus_assert (table != NULL);
450 _dbus_assert (bucket != NULL);
451 _dbus_assert (*bucket != NULL);
452 _dbus_assert (entry != NULL);
454 if (*bucket == entry)
455 *bucket = entry->next;
461 while (prev->next != entry)
464 _dbus_assert (prev != NULL);
466 prev->next = entry->next;
469 table->n_entries -= 1;
470 free_entry (table, entry);
474 * Initializes a hash table iterator. To iterate over all entries in a
475 * hash table, use the following code (the printf assumes a hash
476 * from strings to strings obviously):
481 * _dbus_hash_iter_init (table, &iter);
482 * while (_dbus_hash_iter_next (&iter))
484 * printf ("The first key is %s and value is %s\n",
485 * _dbus_hash_iter_get_string_key (&iter),
486 * _dbus_hash_iter_get_value (&iter));
492 * The iterator is initialized pointing "one before" the first hash
493 * entry. The first call to _dbus_hash_iter_next() moves it onto
494 * the first valid entry or returns #FALSE if the hash table is
495 * empty. Subsequent calls move to the next valid entry or return
496 * #FALSE if there are no more entries.
498 * Note that it is guaranteed to be safe to remove a hash entry during
499 * iteration, but it is not safe to add a hash entry.
501 * @param table the hash table to iterate over.
502 * @param iter the iterator to initialize.
505 _dbus_hash_iter_init (DBusHashTable *table,
508 DBusRealHashIter *real;
510 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
512 real = (DBusRealHashIter*) iter;
517 real->next_entry = NULL;
518 real->next_bucket = 0;
519 real->n_entries_on_init = table->n_entries;
523 * Move the hash iterator forward one step, to the next hash entry.
524 * The documentation for _dbus_hash_iter_init() explains in more
527 * @param iter the iterator to move forward.
528 * @returns #FALSE if there are no more entries to move to.
531 _dbus_hash_iter_next (DBusHashIter *iter)
533 DBusRealHashIter *real;
535 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
537 real = (DBusRealHashIter*) iter;
539 /* if this assertion failed someone probably added hash entries
540 * during iteration, which is bad.
542 _dbus_assert (real->n_entries_on_init >= real->table->n_entries);
544 /* Remember that real->entry may have been deleted */
546 while (real->next_entry == NULL)
548 if (real->next_bucket >= real->table->n_buckets)
550 /* invalidate iter and return false */
557 real->bucket = &(real->table->buckets[real->next_bucket]);
558 real->next_entry = *(real->bucket);
559 real->next_bucket += 1;
562 _dbus_assert (real->next_entry != NULL);
563 _dbus_assert (real->bucket != NULL);
565 real->entry = real->next_entry;
566 real->next_entry = real->entry->next;
572 * Removes the current entry from the hash table.
573 * If a key_free_function or value_free_function
574 * was provided to _dbus_hash_table_new(),
575 * frees the key and/or value for this entry.
577 * @param iter the hash table iterator.
580 _dbus_hash_iter_remove_entry (DBusHashIter *iter)
582 DBusRealHashIter *real;
584 real = (DBusRealHashIter*) iter;
586 _dbus_assert (real->table != NULL);
587 _dbus_assert (real->entry != NULL);
588 _dbus_assert (real->bucket != NULL);
590 remove_entry (real->table, real->bucket, real->entry);
592 real->entry = NULL; /* make it crash if you try to use this entry */
596 * Gets the value of the current entry.
598 * @param iter the hash table iterator.
601 _dbus_hash_iter_get_value (DBusHashIter *iter)
603 DBusRealHashIter *real;
605 real = (DBusRealHashIter*) iter;
607 _dbus_assert (real->table != NULL);
608 _dbus_assert (real->entry != NULL);
610 return real->entry->value;
614 * Sets the value of the current entry.
615 * If the hash table has a value_free_function
616 * it will be used to free the previous value.
617 * The hash table will own the passed-in value
618 * (it will not be copied).
620 * @param iter the hash table iterator.
621 * @param value the new value.
624 _dbus_hash_iter_set_value (DBusHashIter *iter,
627 DBusRealHashIter *real;
629 real = (DBusRealHashIter*) iter;
631 _dbus_assert (real->table != NULL);
632 _dbus_assert (real->entry != NULL);
634 if (real->table->free_value_function && value != real->entry->value)
635 (* real->table->free_value_function) (real->entry->value);
637 real->entry->value = value;
641 * Gets the key for the current entry.
642 * Only works for hash tables of type #DBUS_HASH_INT.
644 * @param iter the hash table iterator.
647 _dbus_hash_iter_get_int_key (DBusHashIter *iter)
649 DBusRealHashIter *real;
651 real = (DBusRealHashIter*) iter;
653 _dbus_assert (real->table != NULL);
654 _dbus_assert (real->entry != NULL);
656 return _DBUS_POINTER_TO_INT (real->entry->key);
660 * Gets the key for the current entry.
661 * Only works for hash tables of type #DBUS_HASH_ULONG.
663 * @param iter the hash table iterator.
666 _dbus_hash_iter_get_ulong_key (DBusHashIter *iter)
668 DBusRealHashIter *real;
670 real = (DBusRealHashIter*) iter;
672 _dbus_assert (real->table != NULL);
673 _dbus_assert (real->entry != NULL);
675 return (unsigned long) real->entry->key;
679 * Gets the key for the current entry.
680 * Only works for hash tables of type #DBUS_HASH_STRING
681 * @param iter the hash table iterator.
684 _dbus_hash_iter_get_string_key (DBusHashIter *iter)
686 DBusRealHashIter *real;
688 real = (DBusRealHashIter*) iter;
690 _dbus_assert (real->table != NULL);
691 _dbus_assert (real->entry != NULL);
693 return real->entry->key;
697 * Gets the key for the current entry.
698 * Only works for hash tables of type #DBUS_HASH_TWO_STRINGS
699 * @param iter the hash table iterator.
702 _dbus_hash_iter_get_two_strings_key (DBusHashIter *iter)
704 DBusRealHashIter *real;
706 real = (DBusRealHashIter*) iter;
708 _dbus_assert (real->table != NULL);
709 _dbus_assert (real->entry != NULL);
711 return real->entry->key;
715 * A low-level but efficient interface for manipulating the hash
716 * table. It's efficient because you can get, set, and optionally
717 * create the hash entry while only running the hash function one
720 * Note that while calling _dbus_hash_iter_next() on the iterator
721 * filled in by this function may work, it's completely
722 * undefined which entries are after this iter and which
723 * are before it. So it would be silly to iterate using this
726 * If the hash entry is created, its value will be initialized
729 * #FALSE may be returned due to memory allocation failure, or
730 * because create_if_not_found was #FALSE and the entry
733 * If create_if_not_found is #TRUE and the entry is created, the hash
734 * table takes ownership of the key that's passed in.
736 * For a hash table of type #DBUS_HASH_INT, cast the int
737 * key to the key parameter using #_DBUS_INT_TO_POINTER().
739 * @param table the hash table.
740 * @param key the hash key.
741 * @param create_if_not_found if #TRUE, create the entry if it didn't exist.
742 * @param iter the iterator to initialize.
743 * @returns #TRUE if the hash entry now exists (and the iterator is thus valid).
746 _dbus_hash_iter_lookup (DBusHashTable *table,
748 dbus_bool_t create_if_not_found,
751 DBusRealHashIter *real;
752 DBusHashEntry *entry;
753 DBusHashEntry **bucket;
755 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
757 real = (DBusRealHashIter*) iter;
759 entry = (* table->find_function) (table, key, create_if_not_found, &bucket, NULL);
765 real->bucket = bucket;
767 real->next_entry = entry->next;
768 real->next_bucket = (bucket - table->buckets) + 1;
769 real->n_entries_on_init = table->n_entries;
771 _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
777 add_allocated_entry (DBusHashTable *table,
778 DBusHashEntry *entry,
781 DBusHashEntry ***bucket)
787 b = &(table->buckets[idx]);
794 table->n_entries += 1;
796 /* note we ONLY rebuild when ADDING - because you can iterate over a
797 * table and remove entries safely.
799 if (table->n_entries >= table->hi_rebuild_size ||
800 table->n_entries < table->lo_rebuild_size)
801 rebuild_table (table);
804 static DBusHashEntry*
805 add_entry (DBusHashTable *table,
808 DBusHashEntry ***bucket,
809 DBusPreallocatedHash *preallocated)
811 DBusHashEntry *entry;
813 if (preallocated == NULL)
815 entry = alloc_entry (table);
825 entry = (DBusHashEntry*) preallocated;
828 add_allocated_entry (table, entry, idx, key, bucket);
833 /* This is g_str_hash from GLib which was
834 * extensively discussed/tested/profiled
837 string_hash (const char *str)
843 for (p += 1; *p != '\0'; p++)
844 h = (h << 5) - h + *p;
849 /* This hashes a memory block with two nul-terminated strings
850 * in it, used in dbus-object-registry.c at the moment.
853 two_strings_hash (const char *str)
859 for (p += 1; *p != '\0'; p++)
860 h = (h << 5) - h + *p;
862 for (p += 1; *p != '\0'; p++)
863 h = (h << 5) - h + *p;
868 /** Key comparison function */
869 typedef int (* KeyCompareFunc) (const void *key_a, const void *key_b);
871 static DBusHashEntry*
872 find_generic_function (DBusHashTable *table,
875 KeyCompareFunc compare_func,
876 dbus_bool_t create_if_not_found,
877 DBusHashEntry ***bucket,
878 DBusPreallocatedHash *preallocated)
880 DBusHashEntry *entry;
885 /* Search all of the entries in this bucket. */
886 entry = table->buckets[idx];
887 while (entry != NULL)
889 if ((compare_func == NULL && key == entry->key) ||
890 (compare_func != NULL && (* compare_func) (key, entry->key) == 0))
893 *bucket = &(table->buckets[idx]);
896 _dbus_hash_table_free_preallocated_entry (table, preallocated);
904 if (create_if_not_found)
905 entry = add_entry (table, idx, key, bucket, preallocated);
906 else if (preallocated)
907 _dbus_hash_table_free_preallocated_entry (table, preallocated);
912 static DBusHashEntry*
913 find_string_function (DBusHashTable *table,
915 dbus_bool_t create_if_not_found,
916 DBusHashEntry ***bucket,
917 DBusPreallocatedHash *preallocated)
921 idx = string_hash (key) & table->mask;
923 return find_generic_function (table, key, idx,
924 (KeyCompareFunc) strcmp, create_if_not_found, bucket,
929 two_strings_cmp (const char *a,
943 return strcmp (a + len_a + 1, b + len_b + 1);
946 static DBusHashEntry*
947 find_two_strings_function (DBusHashTable *table,
949 dbus_bool_t create_if_not_found,
950 DBusHashEntry ***bucket,
951 DBusPreallocatedHash *preallocated)
955 idx = two_strings_hash (key) & table->mask;
957 return find_generic_function (table, key, idx,
958 (KeyCompareFunc) two_strings_cmp, create_if_not_found, bucket,
962 static DBusHashEntry*
963 find_direct_function (DBusHashTable *table,
965 dbus_bool_t create_if_not_found,
966 DBusHashEntry ***bucket,
967 DBusPreallocatedHash *preallocated)
971 idx = RANDOM_INDEX (table, key) & table->mask;
974 return find_generic_function (table, key, idx,
975 NULL, create_if_not_found, bucket,
980 rebuild_table (DBusHashTable *table)
984 DBusHashEntry **old_buckets;
985 DBusHashEntry **old_chain;
986 DBusHashEntry *entry;
990 * Allocate and initialize the new bucket array, and set up
991 * hashing constants for new array size.
994 growing = table->n_entries >= table->hi_rebuild_size;
996 old_size = table->n_buckets;
997 old_buckets = table->buckets;
1001 /* overflow paranoia */
1002 if (table->n_buckets < _DBUS_INT_MAX / 4 &&
1003 table->down_shift >= 0)
1004 new_buckets = table->n_buckets * 4;
1006 return; /* can't grow anymore */
1010 new_buckets = table->n_buckets / 4;
1011 if (new_buckets < DBUS_SMALL_HASH_TABLE)
1012 return; /* don't bother shrinking this far */
1015 table->buckets = dbus_new0 (DBusHashEntry*, new_buckets);
1016 if (table->buckets == NULL)
1018 /* out of memory, yay - just don't reallocate, the table will
1019 * still work, albeit more slowly.
1021 table->buckets = old_buckets;
1025 table->n_buckets = new_buckets;
1029 table->lo_rebuild_size = table->hi_rebuild_size;
1030 table->hi_rebuild_size *= 4;
1032 table->down_shift -= 2; /* keep 2 more high bits */
1033 table->mask = (table->mask << 2) + 3; /* keep 2 more high bits */
1037 table->hi_rebuild_size = table->lo_rebuild_size;
1038 table->lo_rebuild_size /= 4;
1040 table->down_shift += 2; /* keep 2 fewer high bits */
1041 table->mask = table->mask >> 2; /* keep 2 fewer high bits */
1045 printf ("%s table to lo = %d hi = %d downshift = %d mask = 0x%x\n",
1046 growing ? "GROW" : "SHRINK",
1047 table->lo_rebuild_size,
1048 table->hi_rebuild_size,
1053 _dbus_assert (table->lo_rebuild_size >= 0);
1054 _dbus_assert (table->hi_rebuild_size > table->lo_rebuild_size);
1055 _dbus_assert (table->mask != 0);
1056 /* the mask is essentially the max index */
1057 _dbus_assert (table->mask < table->n_buckets);
1060 * Rehash all of the existing entries into the new bucket array.
1063 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
1065 for (entry = *old_chain; entry != NULL; entry = *old_chain)
1068 DBusHashEntry **bucket;
1070 *old_chain = entry->next;
1071 switch (table->key_type)
1073 case DBUS_HASH_STRING:
1074 idx = string_hash (entry->key) & table->mask;
1076 case DBUS_HASH_TWO_STRINGS:
1077 idx = two_strings_hash (entry->key) & table->mask;
1080 case DBUS_HASH_ULONG:
1081 case DBUS_HASH_POINTER:
1082 idx = RANDOM_INDEX (table, entry->key);
1086 _dbus_assert_not_reached ("Unknown hash table type");
1090 bucket = &(table->buckets[idx]);
1091 entry->next = *bucket;
1096 /* Free the old bucket array, if it was dynamically allocated. */
1098 if (old_buckets != table->static_buckets)
1099 dbus_free (old_buckets);
1103 * Looks up the value for a given string in a hash table
1104 * of type #DBUS_HASH_STRING. Returns %NULL if the value
1105 * is not present. (A not-present entry is indistinguishable
1106 * from an entry with a value of %NULL.)
1107 * @param table the hash table.
1108 * @param key the string to look up.
1109 * @returns the value of the hash entry.
1112 _dbus_hash_table_lookup_string (DBusHashTable *table,
1115 DBusHashEntry *entry;
1117 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1119 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1122 return entry->value;
1128 * Looks up the value for a given string in a hash table
1129 * of type #DBUS_HASH_TWO_STRINGS. Returns %NULL if the value
1130 * is not present. (A not-present entry is indistinguishable
1131 * from an entry with a value of %NULL.)
1132 * @param table the hash table.
1133 * @param key the string to look up.
1134 * @returns the value of the hash entry.
1137 _dbus_hash_table_lookup_two_strings (DBusHashTable *table,
1140 DBusHashEntry *entry;
1142 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1144 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1147 return entry->value;
1153 * Looks up the value for a given integer in a hash table
1154 * of type #DBUS_HASH_INT. Returns %NULL if the value
1155 * is not present. (A not-present entry is indistinguishable
1156 * from an entry with a value of %NULL.)
1157 * @param table the hash table.
1158 * @param key the integer to look up.
1159 * @returns the value of the hash entry.
1162 _dbus_hash_table_lookup_int (DBusHashTable *table,
1165 DBusHashEntry *entry;
1167 _dbus_assert (table->key_type == DBUS_HASH_INT);
1169 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, NULL, NULL);
1172 return entry->value;
1177 #ifdef DBUS_BUILD_TESTS
1178 /* disabled since it's only used for testing */
1180 * Looks up the value for a given integer in a hash table
1181 * of type #DBUS_HASH_POINTER. Returns %NULL if the value
1182 * is not present. (A not-present entry is indistinguishable
1183 * from an entry with a value of %NULL.)
1184 * @param table the hash table.
1185 * @param key the integer to look up.
1186 * @returns the value of the hash entry.
1189 _dbus_hash_table_lookup_pointer (DBusHashTable *table,
1192 DBusHashEntry *entry;
1194 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1196 entry = (* table->find_function) (table, key, FALSE, NULL, NULL);
1199 return entry->value;
1203 #endif /* DBUS_BUILD_TESTS */
1206 * Looks up the value for a given integer in a hash table
1207 * of type #DBUS_HASH_ULONG. Returns %NULL if the value
1208 * is not present. (A not-present entry is indistinguishable
1209 * from an entry with a value of %NULL.)
1210 * @param table the hash table.
1211 * @param key the integer to look up.
1212 * @returns the value of the hash entry.
1215 _dbus_hash_table_lookup_ulong (DBusHashTable *table,
1218 DBusHashEntry *entry;
1220 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1222 entry = (* table->find_function) (table, (void*) key, FALSE, NULL, NULL);
1225 return entry->value;
1231 * Removes the hash entry for the given key. If no hash entry
1232 * for the key exists, does nothing.
1234 * @param table the hash table.
1235 * @param key the hash key.
1236 * @returns #TRUE if the entry existed
1239 _dbus_hash_table_remove_string (DBusHashTable *table,
1242 DBusHashEntry *entry;
1243 DBusHashEntry **bucket;
1245 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1247 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1251 remove_entry (table, bucket, entry);
1259 * Removes the hash entry for the given key. If no hash entry
1260 * for the key exists, does nothing.
1262 * @param table the hash table.
1263 * @param key the hash key.
1264 * @returns #TRUE if the entry existed
1267 _dbus_hash_table_remove_two_strings (DBusHashTable *table,
1270 DBusHashEntry *entry;
1271 DBusHashEntry **bucket;
1273 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1275 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1279 remove_entry (table, bucket, entry);
1287 * Removes the hash entry for the given key. If no hash entry
1288 * for the key exists, does nothing.
1290 * @param table the hash table.
1291 * @param key the hash key.
1292 * @returns #TRUE if the entry existed
1295 _dbus_hash_table_remove_int (DBusHashTable *table,
1298 DBusHashEntry *entry;
1299 DBusHashEntry **bucket;
1301 _dbus_assert (table->key_type == DBUS_HASH_INT);
1303 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, &bucket, NULL);
1307 remove_entry (table, bucket, entry);
1314 #ifdef DBUS_BUILD_TESTS
1315 /* disabled since it's only used for testing */
1317 * Removes the hash entry for the given key. If no hash entry
1318 * for the key exists, does nothing.
1320 * @param table the hash table.
1321 * @param key the hash key.
1322 * @returns #TRUE if the entry existed
1325 _dbus_hash_table_remove_pointer (DBusHashTable *table,
1328 DBusHashEntry *entry;
1329 DBusHashEntry **bucket;
1331 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1333 entry = (* table->find_function) (table, key, FALSE, &bucket, NULL);
1337 remove_entry (table, bucket, entry);
1343 #endif /* DBUS_BUILD_TESTS */
1346 * Removes the hash entry for the given key. If no hash entry
1347 * for the key exists, does nothing.
1349 * @param table the hash table.
1350 * @param key the hash key.
1351 * @returns #TRUE if the entry existed
1354 _dbus_hash_table_remove_ulong (DBusHashTable *table,
1357 DBusHashEntry *entry;
1358 DBusHashEntry **bucket;
1360 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1362 entry = (* table->find_function) (table, (void*) key, FALSE, &bucket, NULL);
1366 remove_entry (table, bucket, entry);
1374 * Creates a hash entry with the given key and value.
1375 * The key and value are not copied; they are stored
1376 * in the hash table by reference. If an entry with the
1377 * given key already exists, the previous key and value
1378 * are overwritten (and freed if the hash table has
1379 * a key_free_function and/or value_free_function).
1381 * Returns #FALSE if memory for the new hash entry
1382 * can't be allocated.
1384 * @param table the hash table.
1385 * @param key the hash entry key.
1386 * @param value the hash entry value.
1389 _dbus_hash_table_insert_string (DBusHashTable *table,
1393 DBusPreallocatedHash *preallocated;
1395 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1397 preallocated = _dbus_hash_table_preallocate_entry (table);
1398 if (preallocated == NULL)
1401 _dbus_hash_table_insert_string_preallocated (table, preallocated,
1408 * Creates a hash entry with the given key and value.
1409 * The key and value are not copied; they are stored
1410 * in the hash table by reference. If an entry with the
1411 * given key already exists, the previous key and value
1412 * are overwritten (and freed if the hash table has
1413 * a key_free_function and/or value_free_function).
1415 * Returns #FALSE if memory for the new hash entry
1416 * can't be allocated.
1418 * @param table the hash table.
1419 * @param key the hash entry key.
1420 * @param value the hash entry value.
1423 _dbus_hash_table_insert_two_strings (DBusHashTable *table,
1427 DBusHashEntry *entry;
1429 _dbus_assert (table->key_type == DBUS_HASH_TWO_STRINGS);
1431 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1434 return FALSE; /* no memory */
1436 if (table->free_key_function && entry->key != key)
1437 (* table->free_key_function) (entry->key);
1439 if (table->free_value_function && entry->value != value)
1440 (* table->free_value_function) (entry->value);
1443 entry->value = value;
1449 * Creates a hash entry with the given key and value.
1450 * The key and value are not copied; they are stored
1451 * in the hash table by reference. If an entry with the
1452 * given key already exists, the previous key and value
1453 * are overwritten (and freed if the hash table has
1454 * a key_free_function and/or value_free_function).
1456 * Returns #FALSE if memory for the new hash entry
1457 * can't be allocated.
1459 * @param table the hash table.
1460 * @param key the hash entry key.
1461 * @param value the hash entry value.
1464 _dbus_hash_table_insert_int (DBusHashTable *table,
1468 DBusHashEntry *entry;
1470 _dbus_assert (table->key_type == DBUS_HASH_INT);
1472 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), TRUE, NULL, NULL);
1475 return FALSE; /* no memory */
1477 if (table->free_key_function && entry->key != _DBUS_INT_TO_POINTER (key))
1478 (* table->free_key_function) (entry->key);
1480 if (table->free_value_function && entry->value != value)
1481 (* table->free_value_function) (entry->value);
1483 entry->key = _DBUS_INT_TO_POINTER (key);
1484 entry->value = value;
1489 #ifdef DBUS_BUILD_TESTS
1490 /* disabled since it's only used for testing */
1492 * Creates a hash entry with the given key and value.
1493 * The key and value are not copied; they are stored
1494 * in the hash table by reference. If an entry with the
1495 * given key already exists, the previous key and value
1496 * are overwritten (and freed if the hash table has
1497 * a key_free_function and/or value_free_function).
1499 * Returns #FALSE if memory for the new hash entry
1500 * can't be allocated.
1502 * @param table the hash table.
1503 * @param key the hash entry key.
1504 * @param value the hash entry value.
1507 _dbus_hash_table_insert_pointer (DBusHashTable *table,
1511 DBusHashEntry *entry;
1513 _dbus_assert (table->key_type == DBUS_HASH_POINTER);
1515 entry = (* table->find_function) (table, key, TRUE, NULL, NULL);
1518 return FALSE; /* no memory */
1520 if (table->free_key_function && entry->key != key)
1521 (* table->free_key_function) (entry->key);
1523 if (table->free_value_function && entry->value != value)
1524 (* table->free_value_function) (entry->value);
1527 entry->value = value;
1531 #endif /* DBUS_BUILD_TESTS */
1534 * Creates a hash entry with the given key and value.
1535 * The key and value are not copied; they are stored
1536 * in the hash table by reference. If an entry with the
1537 * given key already exists, the previous key and value
1538 * are overwritten (and freed if the hash table has
1539 * a key_free_function and/or value_free_function).
1541 * Returns #FALSE if memory for the new hash entry
1542 * can't be allocated.
1544 * @param table the hash table.
1545 * @param key the hash entry key.
1546 * @param value the hash entry value.
1549 _dbus_hash_table_insert_ulong (DBusHashTable *table,
1553 DBusHashEntry *entry;
1555 _dbus_assert (table->key_type == DBUS_HASH_ULONG);
1557 entry = (* table->find_function) (table, (void*) key, TRUE, NULL, NULL);
1560 return FALSE; /* no memory */
1562 if (table->free_key_function && entry->key != (void*) key)
1563 (* table->free_key_function) (entry->key);
1565 if (table->free_value_function && entry->value != value)
1566 (* table->free_value_function) (entry->value);
1568 entry->key = (void*) key;
1569 entry->value = value;
1575 * Preallocate an opaque data blob that allows us to insert into the
1576 * hash table at a later time without allocating any memory.
1578 * @param table the hash table
1579 * @returns the preallocated data, or #NULL if no memory
1581 DBusPreallocatedHash*
1582 _dbus_hash_table_preallocate_entry (DBusHashTable *table)
1584 DBusHashEntry *entry;
1586 entry = alloc_entry (table);
1588 return (DBusPreallocatedHash*) entry;
1592 * Frees an opaque DBusPreallocatedHash that was *not* used
1593 * in order to insert into the hash table.
1595 * @param table the hash table
1596 * @param preallocated the preallocated data
1599 _dbus_hash_table_free_preallocated_entry (DBusHashTable *table,
1600 DBusPreallocatedHash *preallocated)
1602 DBusHashEntry *entry;
1604 _dbus_assert (preallocated != NULL);
1606 entry = (DBusHashEntry*) preallocated;
1608 /* Don't use free_entry(), since this entry has no key/data */
1609 _dbus_mem_pool_dealloc (table->entry_pool, entry);
1613 * Inserts a string-keyed entry into the hash table, using a
1614 * preallocated data block from
1615 * _dbus_hash_table_preallocate_entry(). This function cannot fail due
1616 * to lack of memory. The DBusPreallocatedHash object is consumed and
1617 * should not be reused or freed. Otherwise this function works
1618 * just like _dbus_hash_table_insert_string().
1620 * @param table the hash table
1621 * @param preallocated the preallocated data
1622 * @param key the hash key
1623 * @param value the value
1626 _dbus_hash_table_insert_string_preallocated (DBusHashTable *table,
1627 DBusPreallocatedHash *preallocated,
1631 DBusHashEntry *entry;
1633 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1634 _dbus_assert (preallocated != NULL);
1636 entry = (* table->find_function) (table, key, TRUE, NULL, preallocated);
1638 _dbus_assert (entry != NULL);
1640 if (table->free_key_function && entry->key != key)
1641 (* table->free_key_function) (entry->key);
1643 if (table->free_value_function && entry->value != value)
1644 (* table->free_value_function) (entry->value);
1647 entry->value = value;
1651 * Gets the number of hash entries in a hash table.
1653 * @param table the hash table.
1654 * @returns the number of entries in the table.
1657 _dbus_hash_table_get_n_entries (DBusHashTable *table)
1659 return table->n_entries;
1664 #ifdef DBUS_BUILD_TESTS
1665 #include "dbus-test.h"
1668 /* If you're wondering why the hash table test takes
1669 * forever to run, it's because we call this function
1670 * in inner loops thus making things quadratic.
1673 count_entries (DBusHashTable *table)
1679 _dbus_hash_iter_init (table, &iter);
1680 while (_dbus_hash_iter_next (&iter))
1683 _dbus_assert (count == _dbus_hash_table_get_n_entries (table));
1688 /* Copy the foo\0bar\0 double string thing */
1690 _dbus_strdup2 (const char *str)
1699 len += strlen ((str + len + 1));
1701 copy = dbus_malloc (len + 2);
1705 memcpy (copy, str, len + 2);
1711 * @ingroup DBusHashTableInternals
1712 * Unit test for DBusHashTable
1713 * @returns #TRUE on success.
1716 _dbus_hash_test (void)
1719 DBusHashTable *table1;
1720 DBusHashTable *table2;
1721 DBusHashTable *table3;
1722 DBusHashTable *table4;
1724 #define N_HASH_KEYS 5000
1726 dbus_bool_t ret = FALSE;
1728 keys = dbus_new (char *, N_HASH_KEYS);
1730 _dbus_assert_not_reached ("no memory");
1732 for (i = 0; i < N_HASH_KEYS; i++)
1734 keys[i] = dbus_malloc (128);
1736 if (keys[i] == NULL)
1737 _dbus_assert_not_reached ("no memory");
1740 printf ("Computing test hash keys...\n");
1742 while (i < N_HASH_KEYS)
1746 /* all the hash keys are TWO_STRINGS, but
1747 * then we can also use those as regular strings.
1750 len = sprintf (keys[i], "Hash key %d", i);
1751 sprintf (keys[i] + len + 1, "Two string %d", i);
1752 _dbus_assert (*(keys[i] + len) == '\0');
1753 _dbus_assert (*(keys[i] + len + 1) != '\0');
1756 printf ("... done.\n");
1758 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1759 dbus_free, dbus_free);
1763 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1768 table3 = _dbus_hash_table_new (DBUS_HASH_ULONG,
1773 table4 = _dbus_hash_table_new (DBUS_HASH_TWO_STRINGS,
1774 dbus_free, dbus_free);
1779 /* Insert and remove a bunch of stuff, counting the table in between
1780 * to be sure it's not broken and that iteration works
1788 key = _dbus_strdup (keys[i]);
1791 value = _dbus_strdup ("Value!");
1795 if (!_dbus_hash_table_insert_string (table1,
1799 value = _dbus_strdup (keys[i]);
1803 if (!_dbus_hash_table_insert_int (table2,
1807 value = _dbus_strdup (keys[i]);
1811 if (!_dbus_hash_table_insert_ulong (table3,
1815 key = _dbus_strdup2 (keys[i]);
1818 value = _dbus_strdup ("Value!");
1822 if (!_dbus_hash_table_insert_two_strings (table4,
1826 _dbus_assert (count_entries (table1) == i + 1);
1827 _dbus_assert (count_entries (table2) == i + 1);
1828 _dbus_assert (count_entries (table3) == i + 1);
1829 _dbus_assert (count_entries (table4) == i + 1);
1831 value = _dbus_hash_table_lookup_string (table1, keys[i]);
1832 _dbus_assert (value != NULL);
1833 _dbus_assert (strcmp (value, "Value!") == 0);
1835 value = _dbus_hash_table_lookup_int (table2, i);
1836 _dbus_assert (value != NULL);
1837 _dbus_assert (strcmp (value, keys[i]) == 0);
1839 value = _dbus_hash_table_lookup_ulong (table3, i);
1840 _dbus_assert (value != NULL);
1841 _dbus_assert (strcmp (value, keys[i]) == 0);
1843 value = _dbus_hash_table_lookup_two_strings (table4, keys[i]);
1844 _dbus_assert (value != NULL);
1845 _dbus_assert (strcmp (value, "Value!") == 0);
1853 _dbus_hash_table_remove_string (table1,
1856 _dbus_hash_table_remove_int (table2, i);
1858 _dbus_hash_table_remove_ulong (table3, i);
1860 _dbus_hash_table_remove_two_strings (table4,
1863 _dbus_assert (count_entries (table1) == i);
1864 _dbus_assert (count_entries (table2) == i);
1865 _dbus_assert (count_entries (table3) == i);
1866 _dbus_assert (count_entries (table4) == i);
1871 _dbus_hash_table_ref (table1);
1872 _dbus_hash_table_ref (table2);
1873 _dbus_hash_table_ref (table3);
1874 _dbus_hash_table_ref (table4);
1875 _dbus_hash_table_unref (table1);
1876 _dbus_hash_table_unref (table2);
1877 _dbus_hash_table_unref (table3);
1878 _dbus_hash_table_unref (table4);
1879 _dbus_hash_table_unref (table1);
1880 _dbus_hash_table_unref (table2);
1881 _dbus_hash_table_unref (table3);
1882 _dbus_hash_table_unref (table4);
1885 /* Insert a bunch of stuff then check
1886 * that iteration works correctly (finds the right
1887 * values, iter_set_value works, etc.)
1889 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1890 dbus_free, dbus_free);
1894 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1905 key = _dbus_strdup (keys[i]);
1908 value = _dbus_strdup ("Value!");
1912 if (!_dbus_hash_table_insert_string (table1,
1916 value = _dbus_strdup (keys[i]);
1920 if (!_dbus_hash_table_insert_int (table2,
1924 _dbus_assert (count_entries (table1) == i + 1);
1925 _dbus_assert (count_entries (table2) == i + 1);
1930 _dbus_hash_iter_init (table1, &iter);
1931 while (_dbus_hash_iter_next (&iter))
1936 key = _dbus_hash_iter_get_string_key (&iter);
1937 value = _dbus_hash_iter_get_value (&iter);
1939 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1941 value = _dbus_strdup ("Different value!");
1945 _dbus_hash_iter_set_value (&iter, value);
1947 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1950 _dbus_hash_iter_init (table1, &iter);
1951 while (_dbus_hash_iter_next (&iter))
1953 _dbus_hash_iter_remove_entry (&iter);
1954 _dbus_assert (count_entries (table1) == i - 1);
1958 _dbus_hash_iter_init (table2, &iter);
1959 while (_dbus_hash_iter_next (&iter))
1964 key = _dbus_hash_iter_get_int_key (&iter);
1965 value = _dbus_hash_iter_get_value (&iter);
1967 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1969 value = _dbus_strdup ("Different value!");
1973 _dbus_hash_iter_set_value (&iter, value);
1975 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1978 i = count_entries (table2);
1979 _dbus_hash_iter_init (table2, &iter);
1980 while (_dbus_hash_iter_next (&iter))
1982 _dbus_hash_iter_remove_entry (&iter);
1983 _dbus_assert (count_entries (table2) + 1 == i);
1987 /* add/remove interleaved, to check that we grow/shrink the table
1996 key = _dbus_strdup (keys[i]);
2000 value = _dbus_strdup ("Value!");
2004 if (!_dbus_hash_table_insert_string (table1,
2017 key = _dbus_strdup (keys[i]);
2020 value = _dbus_strdup ("Value!");
2024 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2027 if (!_dbus_hash_table_insert_string (table1,
2031 if (!_dbus_hash_table_remove_string (table1, keys[i]))
2034 _dbus_assert (_dbus_hash_table_get_n_entries (table1) == i);
2039 /* nuke these tables */
2040 _dbus_hash_table_unref (table1);
2041 _dbus_hash_table_unref (table2);
2044 /* Now do a bunch of things again using _dbus_hash_iter_lookup() to
2045 * be sure that interface works.
2047 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
2048 dbus_free, dbus_free);
2052 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
2063 key = _dbus_strdup (keys[i]);
2066 value = _dbus_strdup ("Value!");
2070 if (!_dbus_hash_iter_lookup (table1,
2073 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2074 _dbus_hash_iter_set_value (&iter, value);
2076 value = _dbus_strdup (keys[i]);
2080 if (!_dbus_hash_iter_lookup (table2,
2081 _DBUS_INT_TO_POINTER (i), TRUE, &iter))
2083 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
2084 _dbus_hash_iter_set_value (&iter, value);
2086 _dbus_assert (count_entries (table1) == i + 1);
2087 _dbus_assert (count_entries (table2) == i + 1);
2089 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2092 value = _dbus_hash_iter_get_value (&iter);
2093 _dbus_assert (value != NULL);
2094 _dbus_assert (strcmp (value, "Value!") == 0);
2096 /* Iterate just to be sure it works, though
2097 * it's a stupid thing to do
2099 while (_dbus_hash_iter_next (&iter))
2102 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2105 value = _dbus_hash_iter_get_value (&iter);
2106 _dbus_assert (value != NULL);
2107 _dbus_assert (strcmp (value, keys[i]) == 0);
2109 /* Iterate just to be sure it works, though
2110 * it's a stupid thing to do
2112 while (_dbus_hash_iter_next (&iter))
2121 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
2122 _dbus_assert_not_reached ("hash entry should have existed");
2123 _dbus_hash_iter_remove_entry (&iter);
2125 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
2126 _dbus_assert_not_reached ("hash entry should have existed");
2127 _dbus_hash_iter_remove_entry (&iter);
2129 _dbus_assert (count_entries (table1) == i);
2130 _dbus_assert (count_entries (table2) == i);
2135 _dbus_hash_table_unref (table1);
2136 _dbus_hash_table_unref (table2);
2141 for (i = 0; i < N_HASH_KEYS; i++)
2142 dbus_free (keys[i]);
2149 #endif /* DBUS_BUILD_TESTS */