1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-hash.c Generic hash table utility (internal to D-Bus implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
5 * Copyright (c) 1991-1993 The Regents of the University of California.
6 * Copyright (c) 1994 Sun Microsystems, Inc.
8 * Hash table implementation based on generic/tclHash.c from the Tcl
9 * source code. The original Tcl license applies to portions of the
10 * code from tclHash.c; the Tcl license follows this standad D-Bus
11 * license information.
13 * Licensed under the Academic Free License version 2.1
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 * The following copyright applies to code from the Tcl distribution.
33 * Copyright (c) 1991-1993 The Regents of the University of California.
34 * Copyright (c) 1994 Sun Microsystems, Inc.
36 * This software is copyrighted by the Regents of the University of
37 * California, Sun Microsystems, Inc., Scriptics Corporation, and
38 * other parties. The following terms apply to all files associated
39 * with the software unless explicitly disclaimed in individual files.
41 * The authors hereby grant permission to use, copy, modify,
42 * distribute, and license this software and its documentation for any
43 * purpose, provided that existing copyright notices are retained in
44 * all copies and that this notice is included verbatim in any
45 * distributions. No written agreement, license, or royalty fee is
46 * required for any of the authorized uses. Modifications to this
47 * software may be copyrighted by their authors and need not follow
48 * the licensing terms described here, provided that the new terms are
49 * clearly indicated on the first page of each file where they apply.
51 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
52 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
53 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
54 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
55 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
58 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
59 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
60 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
61 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
62 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
64 * GOVERNMENT USE: If you are acquiring this software on behalf of the
65 * U.S. government, the Government shall have only "Restricted Rights"
66 * in the software and related documentation as defined in the Federal
67 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
68 * are acquiring the software on behalf of the Department of Defense,
69 * the software shall be classified as "Commercial Computer Software"
70 * and the Government shall have only "Restricted Rights" as defined
71 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
72 * foregoing, the authors grant the U.S. Government and others acting
73 * in its behalf permission to use and distribute the software in
74 * accordance with the terms specified in this license.
78 #include "dbus-hash.h"
79 #include "dbus-internals.h"
80 #include "dbus-mempool.h"
83 * @defgroup DBusHashTable Hash table
84 * @ingroup DBusInternals
85 * @brief DBusHashTable data structure
87 * Types and functions related to DBusHashTable.
91 * @defgroup DBusHashTableInternals Hash table implementation details
92 * @ingroup DBusInternals
93 * @brief DBusHashTable implementation details
95 * The guts of DBusHashTable.
101 * When there are this many entries per bucket, on average, rebuild
102 * the hash table to make it larger.
104 #define REBUILD_MULTIPLIER 3
107 * Takes a preliminary integer hash value and produces an index into a
108 * hash tables bucket list. The idea is to make it so that
109 * preliminary values that are arbitrarily similar will end up in
110 * different buckets. The hash function was taken from a
111 * random-number generator. (This is used to hash integers.)
113 * The down_shift drops off the high bits of the hash index, and
114 * decreases as we increase the number of hash buckets (to keep more
115 * range in the hash index). The mask also strips high bits and strips
116 * fewer high bits as the number of hash buckets increases.
117 * I don't understand two things: why is the initial downshift 28
118 * to keep 4 bits when the initial mask is 011 to keep 2 bits,
119 * and why do we have both a mask and a downshift?
122 #define RANDOM_INDEX(table, i) \
123 (((((intptr_t) (i))*1103515245) >> (table)->down_shift) & (table)->mask)
126 * Initial number of buckets in hash table (hash table statically
127 * allocates its buckets for this size and below).
128 * The initial mask has to be synced to this.
130 #define DBUS_SMALL_HASH_TABLE 4
133 * Typedef for DBusHashEntry
135 typedef struct DBusHashEntry DBusHashEntry;
138 * @brief Internal representation of a hash entry.
140 * A single entry (key-value pair) in the hash table.
141 * Internal to hash table implementation.
145 DBusHashEntry *next; /**< Pointer to next entry in this
146 * hash bucket, or #NULL for end of
149 void *key; /**< Hash key */
150 void *value; /**< Hash value */
154 * Function used to find and optionally create a hash entry.
156 typedef DBusHashEntry* (* DBusFindEntryFunction) (DBusHashTable *table,
158 dbus_bool_t create_if_not_found,
159 DBusHashEntry ***bucket,
160 DBusPreallocatedHash *preallocated);
163 * @brief Internals of DBusHashTable.
165 * Hash table internals. Hash tables are opaque objects, they must be
166 * used via accessor functions.
168 struct DBusHashTable {
169 int refcount; /**< Reference count */
171 DBusHashEntry **buckets; /**< Pointer to bucket array. Each
172 * element points to first entry in
173 * bucket's hash chain, or #NULL.
175 DBusHashEntry *static_buckets[DBUS_SMALL_HASH_TABLE];
176 /**< Bucket array used for small tables
177 * (to avoid mallocs and frees).
179 int n_buckets; /**< Total number of buckets allocated
182 int n_entries; /**< Total number of entries present
185 int hi_rebuild_size; /**< Enlarge table when n_entries gets
188 int lo_rebuild_size; /**< Shrink table when n_entries gets
191 int down_shift; /**< Shift count used in hashing
192 * function. Designed to use high-
193 * order bits of randomized keys.
195 int mask; /**< Mask value used in hashing
198 DBusHashType key_type; /**< Type of keys used in this table */
201 DBusFindEntryFunction find_function; /**< Function for finding entries */
203 DBusFreeFunction free_key_function; /**< Function to free keys */
204 DBusFreeFunction free_value_function; /**< Function to free values */
206 DBusMemPool *entry_pool; /**< Memory pool for hash entries */
210 * @brief Internals of DBusHashIter.
214 DBusHashTable *table; /**< Pointer to table containing entry. */
215 DBusHashEntry **bucket; /**< Pointer to bucket that points to
216 * first entry in this entry's chain:
217 * used for deleting the entry.
219 DBusHashEntry *entry; /**< Current hash entry */
220 DBusHashEntry *next_entry; /**< Next entry to be iterated onto in current bucket */
221 int next_bucket; /**< index of next bucket */
222 int n_entries_on_init; /**< used to detect table resize since initialization */
225 _DBUS_STATIC_ASSERT (sizeof (DBusRealHashIter) == sizeof (DBusHashIter));
227 static DBusHashEntry* find_direct_function (DBusHashTable *table,
229 dbus_bool_t create_if_not_found,
230 DBusHashEntry ***bucket,
231 DBusPreallocatedHash *preallocated);
232 static DBusHashEntry* find_string_function (DBusHashTable *table,
234 dbus_bool_t create_if_not_found,
235 DBusHashEntry ***bucket,
236 DBusPreallocatedHash *preallocated);
237 static unsigned int string_hash (const char *str);
238 static void rebuild_table (DBusHashTable *table);
239 static DBusHashEntry* alloc_entry (DBusHashTable *table);
240 static void remove_entry (DBusHashTable *table,
241 DBusHashEntry **bucket,
242 DBusHashEntry *entry);
243 static void free_entry (DBusHashTable *table,
244 DBusHashEntry *entry);
245 static void free_entry_data (DBusHashTable *table,
246 DBusHashEntry *entry);
252 * @addtogroup DBusHashTable
257 * @typedef DBusHashIter
259 * Public opaque hash table iterator object.
263 * @typedef DBusHashTable
265 * Public opaque hash table object.
269 * @typedef DBusHashType
271 * Indicates the type of a key in the hash table.
275 * Constructs a new hash table. Should be freed with
276 * _dbus_hash_table_unref(). If memory cannot be
277 * allocated for the hash table, returns #NULL.
279 * @param type the type of hash key to use.
280 * @param key_free_function function to free hash keys.
281 * @param value_free_function function to free hash values.
282 * @returns a new DBusHashTable or #NULL if no memory.
285 _dbus_hash_table_new (DBusHashType type,
286 DBusFreeFunction key_free_function,
287 DBusFreeFunction value_free_function)
289 DBusHashTable *table;
290 DBusMemPool *entry_pool;
292 table = dbus_new0 (DBusHashTable, 1);
296 entry_pool = _dbus_mem_pool_new (sizeof (DBusHashEntry), TRUE);
297 if (entry_pool == NULL)
304 table->entry_pool = entry_pool;
306 _dbus_assert (DBUS_SMALL_HASH_TABLE == _DBUS_N_ELEMENTS (table->static_buckets));
308 table->buckets = table->static_buckets;
309 table->n_buckets = DBUS_SMALL_HASH_TABLE;
310 table->n_entries = 0;
311 table->hi_rebuild_size = DBUS_SMALL_HASH_TABLE * REBUILD_MULTIPLIER;
312 table->lo_rebuild_size = 0;
313 table->down_shift = 28;
315 table->key_type = type;
317 _dbus_assert (table->mask < table->n_buckets);
319 switch (table->key_type)
322 case DBUS_HASH_UINTPTR:
323 table->find_function = find_direct_function;
325 case DBUS_HASH_STRING:
326 table->find_function = find_string_function;
329 _dbus_assert_not_reached ("Unknown hash table type");
333 table->free_key_function = key_free_function;
334 table->free_value_function = value_free_function;
341 * Increments the reference count for a hash table.
343 * @param table the hash table to add a reference to.
344 * @returns the hash table.
347 _dbus_hash_table_ref (DBusHashTable *table)
349 table->refcount += 1;
355 * Decrements the reference count for a hash table,
356 * freeing the hash table if the count reaches zero.
358 * @param table the hash table to remove a reference from.
361 _dbus_hash_table_unref (DBusHashTable *table)
363 table->refcount -= 1;
365 if (table->refcount == 0)
368 DBusHashEntry *entry;
372 /* Free the entries in the table. */
373 for (i = 0; i < table->n_buckets; i++)
375 entry = table->buckets[i];
376 while (entry != NULL)
380 free_entry (table, entry);
386 DBusHashEntry *entry;
389 /* Free the entries in the table. */
390 for (i = 0; i < table->n_buckets; i++)
392 entry = table->buckets[i];
393 while (entry != NULL)
395 free_entry_data (table, entry);
400 /* We can do this very quickly with memory pools ;-) */
401 _dbus_mem_pool_free (table->entry_pool);
404 /* Free the bucket array, if it was dynamically allocated. */
405 if (table->buckets != table->static_buckets)
406 dbus_free (table->buckets);
413 * Removed all entries from a hash table.
415 * @param table the hash table to remove all entries from.
418 _dbus_hash_table_remove_all (DBusHashTable *table)
421 _dbus_hash_iter_init (table, &iter);
422 while (_dbus_hash_iter_next (&iter))
424 _dbus_hash_iter_remove_entry(&iter);
428 static DBusHashEntry*
429 alloc_entry (DBusHashTable *table)
431 DBusHashEntry *entry;
433 entry = _dbus_mem_pool_alloc (table->entry_pool);
439 free_entry_data (DBusHashTable *table,
440 DBusHashEntry *entry)
442 if (table->free_key_function)
443 (* table->free_key_function) (entry->key);
444 if (table->free_value_function)
445 (* table->free_value_function) (entry->value);
449 free_entry (DBusHashTable *table,
450 DBusHashEntry *entry)
452 free_entry_data (table, entry);
453 _dbus_mem_pool_dealloc (table->entry_pool, entry);
457 remove_entry (DBusHashTable *table,
458 DBusHashEntry **bucket,
459 DBusHashEntry *entry)
461 _dbus_assert (table != NULL);
462 _dbus_assert (bucket != NULL);
463 _dbus_assert (*bucket != NULL);
464 _dbus_assert (entry != NULL);
466 if (*bucket == entry)
467 *bucket = entry->next;
473 while (prev->next != entry)
476 _dbus_assert (prev != NULL);
478 prev->next = entry->next;
481 table->n_entries -= 1;
482 free_entry (table, entry);
486 * Initializes a hash table iterator. To iterate over all entries in a
487 * hash table, use the following code (the printf assumes a hash
488 * from strings to strings obviously):
493 * _dbus_hash_iter_init (table, &iter);
494 * while (_dbus_hash_iter_next (&iter))
496 * printf ("The first key is %s and value is %s\n",
497 * _dbus_hash_iter_get_string_key (&iter),
498 * _dbus_hash_iter_get_value (&iter));
504 * The iterator is initialized pointing "one before" the first hash
505 * entry. The first call to _dbus_hash_iter_next() moves it onto
506 * the first valid entry or returns #FALSE if the hash table is
507 * empty. Subsequent calls move to the next valid entry or return
508 * #FALSE if there are no more entries.
510 * Note that it is guaranteed to be safe to remove a hash entry during
511 * iteration, but it is not safe to add a hash entry.
513 * @param table the hash table to iterate over.
514 * @param iter the iterator to initialize.
517 _dbus_hash_iter_init (DBusHashTable *table,
520 DBusRealHashIter *real;
522 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
524 real = (DBusRealHashIter*) iter;
529 real->next_entry = NULL;
530 real->next_bucket = 0;
531 real->n_entries_on_init = table->n_entries;
535 * Move the hash iterator forward one step, to the next hash entry.
536 * The documentation for _dbus_hash_iter_init() explains in more
539 * @param iter the iterator to move forward.
540 * @returns #FALSE if there are no more entries to move to.
543 _dbus_hash_iter_next (DBusHashIter *iter)
545 DBusRealHashIter *real;
547 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
549 real = (DBusRealHashIter*) iter;
551 /* if this assertion failed someone probably added hash entries
552 * during iteration, which is bad.
554 _dbus_assert (real->n_entries_on_init >= real->table->n_entries);
556 /* Remember that real->entry may have been deleted */
558 while (real->next_entry == NULL)
560 if (real->next_bucket >= real->table->n_buckets)
562 /* invalidate iter and return false */
569 real->bucket = &(real->table->buckets[real->next_bucket]);
570 real->next_entry = *(real->bucket);
571 real->next_bucket += 1;
574 _dbus_assert (real->next_entry != NULL);
575 _dbus_assert (real->bucket != NULL);
577 real->entry = real->next_entry;
578 real->next_entry = real->entry->next;
584 * Removes the current entry from the hash table.
585 * If a key_free_function or value_free_function
586 * was provided to _dbus_hash_table_new(),
587 * frees the key and/or value for this entry.
589 * @param iter the hash table iterator.
592 _dbus_hash_iter_remove_entry (DBusHashIter *iter)
594 DBusRealHashIter *real;
596 real = (DBusRealHashIter*) iter;
598 _dbus_assert (real->table != NULL);
599 _dbus_assert (real->entry != NULL);
600 _dbus_assert (real->bucket != NULL);
602 remove_entry (real->table, real->bucket, real->entry);
604 real->entry = NULL; /* make it crash if you try to use this entry */
608 * Gets the value of the current entry.
610 * @param iter the hash table iterator.
613 _dbus_hash_iter_get_value (DBusHashIter *iter)
615 DBusRealHashIter *real;
617 real = (DBusRealHashIter*) iter;
619 _dbus_assert (real->table != NULL);
620 _dbus_assert (real->entry != NULL);
622 return real->entry->value;
626 * Sets the value of the current entry.
627 * If the hash table has a value_free_function
628 * it will be used to free the previous value.
629 * The hash table will own the passed-in value
630 * (it will not be copied).
632 * @param iter the hash table iterator.
633 * @param value the new value.
636 _dbus_hash_iter_set_value (DBusHashIter *iter,
639 DBusRealHashIter *real;
641 real = (DBusRealHashIter*) iter;
643 _dbus_assert (real->table != NULL);
644 _dbus_assert (real->entry != NULL);
646 if (real->table->free_value_function && value != real->entry->value)
647 (* real->table->free_value_function) (real->entry->value);
649 real->entry->value = value;
653 * Gets the key for the current entry.
654 * Only works for hash tables of type #DBUS_HASH_INT.
656 * @param iter the hash table iterator.
659 _dbus_hash_iter_get_int_key (DBusHashIter *iter)
661 DBusRealHashIter *real;
663 real = (DBusRealHashIter*) iter;
665 _dbus_assert (real->table != NULL);
666 _dbus_assert (real->entry != NULL);
668 return _DBUS_POINTER_TO_INT (real->entry->key);
672 * Gets the key for the current entry.
673 * Only works for hash tables of type #DBUS_HASH_UINTPTR.
675 * @param iter the hash table iterator.
678 _dbus_hash_iter_get_uintptr_key (DBusHashIter *iter)
680 DBusRealHashIter *real;
682 real = (DBusRealHashIter*) iter;
684 _dbus_assert (real->table != NULL);
685 _dbus_assert (real->entry != NULL);
687 return (uintptr_t) real->entry->key;
691 * Gets the key for the current entry.
692 * Only works for hash tables of type #DBUS_HASH_STRING
693 * @param iter the hash table iterator.
696 _dbus_hash_iter_get_string_key (DBusHashIter *iter)
698 DBusRealHashIter *real;
700 real = (DBusRealHashIter*) iter;
702 _dbus_assert (real->table != NULL);
703 _dbus_assert (real->entry != NULL);
705 return real->entry->key;
709 * A low-level but efficient interface for manipulating the hash
710 * table. It's efficient because you can get, set, and optionally
711 * create the hash entry while only running the hash function one
714 * Note that while calling _dbus_hash_iter_next() on the iterator
715 * filled in by this function may work, it's completely
716 * undefined which entries are after this iter and which
717 * are before it. So it would be silly to iterate using this
720 * If the hash entry is created, its value will be initialized
723 * #FALSE may be returned due to memory allocation failure, or
724 * because create_if_not_found was #FALSE and the entry
727 * If create_if_not_found is #TRUE and the entry is created, the hash
728 * table takes ownership of the key that's passed in.
730 * For a hash table of type #DBUS_HASH_INT, cast the int
731 * key to the key parameter using #_DBUS_INT_TO_POINTER().
733 * @param table the hash table.
734 * @param key the hash key.
735 * @param create_if_not_found if #TRUE, create the entry if it didn't exist.
736 * @param iter the iterator to initialize.
737 * @returns #TRUE if the hash entry now exists (and the iterator is thus valid).
740 _dbus_hash_iter_lookup (DBusHashTable *table,
742 dbus_bool_t create_if_not_found,
745 DBusRealHashIter *real;
746 DBusHashEntry *entry;
747 DBusHashEntry **bucket;
749 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
751 real = (DBusRealHashIter*) iter;
753 entry = (* table->find_function) (table, key, create_if_not_found, &bucket, NULL);
759 real->bucket = bucket;
761 real->next_entry = entry->next;
762 real->next_bucket = (bucket - table->buckets) + 1;
763 real->n_entries_on_init = table->n_entries;
765 _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
771 add_allocated_entry (DBusHashTable *table,
772 DBusHashEntry *entry,
775 DBusHashEntry ***bucket)
781 b = &(table->buckets[idx]);
788 table->n_entries += 1;
790 /* note we ONLY rebuild when ADDING - because you can iterate over a
791 * table and remove entries safely.
793 if (table->n_entries >= table->hi_rebuild_size ||
794 table->n_entries < table->lo_rebuild_size)
795 rebuild_table (table);
798 static DBusHashEntry*
799 add_entry (DBusHashTable *table,
802 DBusHashEntry ***bucket,
803 DBusPreallocatedHash *preallocated)
805 DBusHashEntry *entry;
807 if (preallocated == NULL)
809 entry = alloc_entry (table);
819 entry = (DBusHashEntry*) preallocated;
822 add_allocated_entry (table, entry, idx, key, bucket);
827 /* This is g_str_hash from GLib which was
828 * extensively discussed/tested/profiled
831 string_hash (const char *str)
837 for (p += 1; *p != '\0'; p++)
838 h = (h << 5) - h + *p;
843 /** Key comparison function */
844 typedef int (* KeyCompareFunc) (const void *key_a, const void *key_b);
846 static DBusHashEntry*
847 find_generic_function (DBusHashTable *table,
850 KeyCompareFunc compare_func,
851 dbus_bool_t create_if_not_found,
852 DBusHashEntry ***bucket,
853 DBusPreallocatedHash *preallocated)
855 DBusHashEntry *entry;
860 /* Search all of the entries in this bucket. */
861 entry = table->buckets[idx];
862 while (entry != NULL)
864 if ((compare_func == NULL && key == entry->key) ||
865 (compare_func != NULL && (* compare_func) (key, entry->key) == 0))
868 *bucket = &(table->buckets[idx]);
871 _dbus_hash_table_free_preallocated_entry (table, preallocated);
879 if (create_if_not_found)
880 entry = add_entry (table, idx, key, bucket, preallocated);
881 else if (preallocated)
882 _dbus_hash_table_free_preallocated_entry (table, preallocated);
887 static DBusHashEntry*
888 find_string_function (DBusHashTable *table,
890 dbus_bool_t create_if_not_found,
891 DBusHashEntry ***bucket,
892 DBusPreallocatedHash *preallocated)
896 idx = string_hash (key) & table->mask;
898 return find_generic_function (table, key, idx,
899 (KeyCompareFunc) strcmp, create_if_not_found, bucket,
903 static DBusHashEntry*
904 find_direct_function (DBusHashTable *table,
906 dbus_bool_t create_if_not_found,
907 DBusHashEntry ***bucket,
908 DBusPreallocatedHash *preallocated)
912 idx = RANDOM_INDEX (table, key) & table->mask;
915 return find_generic_function (table, key, idx,
916 NULL, create_if_not_found, bucket,
921 rebuild_table (DBusHashTable *table)
925 DBusHashEntry **old_buckets;
926 DBusHashEntry **old_chain;
927 DBusHashEntry *entry;
931 * Allocate and initialize the new bucket array, and set up
932 * hashing constants for new array size.
935 growing = table->n_entries >= table->hi_rebuild_size;
937 old_size = table->n_buckets;
938 old_buckets = table->buckets;
942 /* overflow paranoia */
943 if (table->n_buckets < _DBUS_INT_MAX / 4 &&
944 table->down_shift >= 0)
945 new_buckets = table->n_buckets * 4;
947 return; /* can't grow anymore */
951 new_buckets = table->n_buckets / 4;
952 if (new_buckets < DBUS_SMALL_HASH_TABLE)
953 return; /* don't bother shrinking this far */
956 table->buckets = dbus_new0 (DBusHashEntry*, new_buckets);
957 if (table->buckets == NULL)
959 /* out of memory, yay - just don't reallocate, the table will
960 * still work, albeit more slowly.
962 table->buckets = old_buckets;
966 table->n_buckets = new_buckets;
970 table->lo_rebuild_size = table->hi_rebuild_size;
971 table->hi_rebuild_size *= 4;
973 table->down_shift -= 2; /* keep 2 more high bits */
974 table->mask = (table->mask << 2) + 3; /* keep 2 more high bits */
978 table->hi_rebuild_size = table->lo_rebuild_size;
979 table->lo_rebuild_size /= 4;
981 table->down_shift += 2; /* keep 2 fewer high bits */
982 table->mask = table->mask >> 2; /* keep 2 fewer high bits */
986 printf ("%s table to lo = %d hi = %d downshift = %d mask = 0x%x\n",
987 growing ? "GROW" : "SHRINK",
988 table->lo_rebuild_size,
989 table->hi_rebuild_size,
994 _dbus_assert (table->lo_rebuild_size >= 0);
995 _dbus_assert (table->hi_rebuild_size > table->lo_rebuild_size);
996 _dbus_assert (table->mask != 0);
997 /* the mask is essentially the max index */
998 _dbus_assert (table->mask < table->n_buckets);
1001 * Rehash all of the existing entries into the new bucket array.
1004 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
1006 for (entry = *old_chain; entry != NULL; entry = *old_chain)
1009 DBusHashEntry **bucket;
1011 *old_chain = entry->next;
1012 switch (table->key_type)
1014 case DBUS_HASH_STRING:
1015 idx = string_hash (entry->key) & table->mask;
1018 case DBUS_HASH_UINTPTR:
1019 idx = RANDOM_INDEX (table, entry->key);
1023 _dbus_assert_not_reached ("Unknown hash table type");
1027 bucket = &(table->buckets[idx]);
1028 entry->next = *bucket;
1033 /* Free the old bucket array, if it was dynamically allocated. */
1035 if (old_buckets != table->static_buckets)
1036 dbus_free (old_buckets);
1040 * Looks up the value for a given string in a hash table
1041 * of type #DBUS_HASH_STRING. Returns %NULL if the value
1042 * is not present. (A not-present entry is indistinguishable
1043 * from an entry with a value of %NULL.)
1044 * @param table the hash table.
1045 * @param key the string to look up.
1046 * @returns the value of the hash entry.
1049 _dbus_hash_table_lookup_string (DBusHashTable *table,
1052 DBusHashEntry *entry;
1054 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1056 entry = (* table->find_function) (table, (char*) key, FALSE, NULL, NULL);
1059 return entry->value;
1065 * Looks up the value for a given integer in a hash table
1066 * of type #DBUS_HASH_INT. Returns %NULL if the value
1067 * is not present. (A not-present entry is indistinguishable
1068 * from an entry with a value of %NULL.)
1069 * @param table the hash table.
1070 * @param key the integer to look up.
1071 * @returns the value of the hash entry.
1074 _dbus_hash_table_lookup_int (DBusHashTable *table,
1077 DBusHashEntry *entry;
1079 _dbus_assert (table->key_type == DBUS_HASH_INT);
1081 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, NULL, NULL);
1084 return entry->value;
1090 * Looks up the value for a given integer in a hash table
1091 * of type #DBUS_HASH_UINTPTR. Returns %NULL if the value
1092 * is not present. (A not-present entry is indistinguishable
1093 * from an entry with a value of %NULL.)
1094 * @param table the hash table.
1095 * @param key the integer to look up.
1096 * @returns the value of the hash entry.
1099 _dbus_hash_table_lookup_uintptr (DBusHashTable *table,
1102 DBusHashEntry *entry;
1104 _dbus_assert (table->key_type == DBUS_HASH_UINTPTR);
1106 entry = (* table->find_function) (table, (void*) key, FALSE, NULL, NULL);
1109 return entry->value;
1115 * Removes the hash entry for the given key. If no hash entry
1116 * for the key exists, does nothing.
1118 * @param table the hash table.
1119 * @param key the hash key.
1120 * @returns #TRUE if the entry existed
1123 _dbus_hash_table_remove_string (DBusHashTable *table,
1126 DBusHashEntry *entry;
1127 DBusHashEntry **bucket;
1129 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1131 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket, NULL);
1135 remove_entry (table, bucket, entry);
1143 * Removes the hash entry for the given key. If no hash entry
1144 * for the key exists, does nothing.
1146 * @param table the hash table.
1147 * @param key the hash key.
1148 * @returns #TRUE if the entry existed
1151 _dbus_hash_table_remove_int (DBusHashTable *table,
1154 DBusHashEntry *entry;
1155 DBusHashEntry **bucket;
1157 _dbus_assert (table->key_type == DBUS_HASH_INT);
1159 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, &bucket, NULL);
1163 remove_entry (table, bucket, entry);
1171 * Removes the hash entry for the given key. If no hash entry
1172 * for the key exists, does nothing.
1174 * @param table the hash table.
1175 * @param key the hash key.
1176 * @returns #TRUE if the entry existed
1179 _dbus_hash_table_remove_uintptr (DBusHashTable *table,
1182 DBusHashEntry *entry;
1183 DBusHashEntry **bucket;
1185 _dbus_assert (table->key_type == DBUS_HASH_UINTPTR);
1187 entry = (* table->find_function) (table, (void*) key, FALSE, &bucket, NULL);
1191 remove_entry (table, bucket, entry);
1199 * Creates a hash entry with the given key and value.
1200 * The key and value are not copied; they are stored
1201 * in the hash table by reference. If an entry with the
1202 * given key already exists, the previous key and value
1203 * are overwritten (and freed if the hash table has
1204 * a key_free_function and/or value_free_function).
1206 * Returns #FALSE if memory for the new hash entry
1207 * can't be allocated.
1209 * @param table the hash table.
1210 * @param key the hash entry key.
1211 * @param value the hash entry value.
1214 _dbus_hash_table_insert_string (DBusHashTable *table,
1218 DBusPreallocatedHash *preallocated;
1220 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1222 preallocated = _dbus_hash_table_preallocate_entry (table);
1223 if (preallocated == NULL)
1226 _dbus_hash_table_insert_string_preallocated (table, preallocated,
1233 * Creates a hash entry with the given key and value.
1234 * The key and value are not copied; they are stored
1235 * in the hash table by reference. If an entry with the
1236 * given key already exists, the previous key and value
1237 * are overwritten (and freed if the hash table has
1238 * a key_free_function and/or value_free_function).
1240 * Returns #FALSE if memory for the new hash entry
1241 * can't be allocated.
1243 * @param table the hash table.
1244 * @param key the hash entry key.
1245 * @param value the hash entry value.
1248 _dbus_hash_table_insert_int (DBusHashTable *table,
1252 DBusHashEntry *entry;
1254 _dbus_assert (table->key_type == DBUS_HASH_INT);
1256 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), TRUE, NULL, NULL);
1259 return FALSE; /* no memory */
1261 if (table->free_key_function && entry->key != _DBUS_INT_TO_POINTER (key))
1262 (* table->free_key_function) (entry->key);
1264 if (table->free_value_function && entry->value != value)
1265 (* table->free_value_function) (entry->value);
1267 entry->key = _DBUS_INT_TO_POINTER (key);
1268 entry->value = value;
1274 * Creates a hash entry with the given key and value.
1275 * The key and value are not copied; they are stored
1276 * in the hash table by reference. If an entry with the
1277 * given key already exists, the previous key and value
1278 * are overwritten (and freed if the hash table has
1279 * a key_free_function and/or value_free_function).
1281 * Returns #FALSE if memory for the new hash entry
1282 * can't be allocated.
1284 * @param table the hash table.
1285 * @param key the hash entry key.
1286 * @param value the hash entry value.
1289 _dbus_hash_table_insert_uintptr (DBusHashTable *table,
1293 DBusHashEntry *entry;
1295 _dbus_assert (table->key_type == DBUS_HASH_UINTPTR);
1297 entry = (* table->find_function) (table, (void*) key, TRUE, NULL, NULL);
1300 return FALSE; /* no memory */
1302 if (table->free_key_function && entry->key != (void*) key)
1303 (* table->free_key_function) (entry->key);
1305 if (table->free_value_function && entry->value != value)
1306 (* table->free_value_function) (entry->value);
1308 entry->key = (void*) key;
1309 entry->value = value;
1315 * Preallocate an opaque data blob that allows us to insert into the
1316 * hash table at a later time without allocating any memory.
1318 * @param table the hash table
1319 * @returns the preallocated data, or #NULL if no memory
1321 DBusPreallocatedHash*
1322 _dbus_hash_table_preallocate_entry (DBusHashTable *table)
1324 DBusHashEntry *entry;
1326 entry = alloc_entry (table);
1328 return (DBusPreallocatedHash*) entry;
1332 * Frees an opaque DBusPreallocatedHash that was *not* used
1333 * in order to insert into the hash table.
1335 * @param table the hash table
1336 * @param preallocated the preallocated data
1339 _dbus_hash_table_free_preallocated_entry (DBusHashTable *table,
1340 DBusPreallocatedHash *preallocated)
1342 DBusHashEntry *entry;
1344 _dbus_assert (preallocated != NULL);
1346 entry = (DBusHashEntry*) preallocated;
1348 /* Don't use free_entry(), since this entry has no key/data */
1349 _dbus_mem_pool_dealloc (table->entry_pool, entry);
1353 * Inserts a string-keyed entry into the hash table, using a
1354 * preallocated data block from
1355 * _dbus_hash_table_preallocate_entry(). This function cannot fail due
1356 * to lack of memory. The DBusPreallocatedHash object is consumed and
1357 * should not be reused or freed. Otherwise this function works
1358 * just like _dbus_hash_table_insert_string().
1360 * @param table the hash table
1361 * @param preallocated the preallocated data
1362 * @param key the hash key
1363 * @param value the value
1366 _dbus_hash_table_insert_string_preallocated (DBusHashTable *table,
1367 DBusPreallocatedHash *preallocated,
1371 DBusHashEntry *entry;
1373 _dbus_assert (table->key_type == DBUS_HASH_STRING);
1374 _dbus_assert (preallocated != NULL);
1376 entry = (* table->find_function) (table, key, TRUE, NULL, preallocated);
1378 _dbus_assert (entry != NULL);
1380 if (table->free_key_function && entry->key != key)
1381 (* table->free_key_function) (entry->key);
1383 if (table->free_value_function && entry->value != value)
1384 (* table->free_value_function) (entry->value);
1387 entry->value = value;
1391 * Gets the number of hash entries in a hash table.
1393 * @param table the hash table.
1394 * @returns the number of entries in the table.
1397 _dbus_hash_table_get_n_entries (DBusHashTable *table)
1399 return table->n_entries;
1404 #ifdef DBUS_BUILD_TESTS
1405 #include "dbus-test.h"
1408 /* If you're wondering why the hash table test takes
1409 * forever to run, it's because we call this function
1410 * in inner loops thus making things quadratic.
1413 count_entries (DBusHashTable *table)
1419 _dbus_hash_iter_init (table, &iter);
1420 while (_dbus_hash_iter_next (&iter))
1423 _dbus_assert (count == _dbus_hash_table_get_n_entries (table));
1429 * @ingroup DBusHashTableInternals
1430 * Unit test for DBusHashTable
1431 * @returns #TRUE on success.
1434 _dbus_hash_test (void)
1437 DBusHashTable *table1;
1438 DBusHashTable *table2;
1439 DBusHashTable *table3;
1441 #define N_HASH_KEYS 5000
1443 dbus_bool_t ret = FALSE;
1445 keys = dbus_new (char *, N_HASH_KEYS);
1447 _dbus_assert_not_reached ("no memory");
1449 for (i = 0; i < N_HASH_KEYS; i++)
1451 keys[i] = dbus_malloc (128);
1453 if (keys[i] == NULL)
1454 _dbus_assert_not_reached ("no memory");
1457 printf ("Computing test hash keys...\n");
1459 while (i < N_HASH_KEYS)
1463 len = sprintf (keys[i], "Hash key %d", i);
1464 _dbus_assert (*(keys[i] + len) == '\0');
1467 printf ("... done.\n");
1469 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1470 dbus_free, dbus_free);
1474 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1479 table3 = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
1484 /* Insert and remove a bunch of stuff, counting the table in between
1485 * to be sure it's not broken and that iteration works
1493 key = _dbus_strdup (keys[i]);
1496 value = _dbus_strdup ("Value!");
1500 if (!_dbus_hash_table_insert_string (table1,
1504 value = _dbus_strdup (keys[i]);
1508 if (!_dbus_hash_table_insert_int (table2,
1512 value = _dbus_strdup (keys[i]);
1516 if (!_dbus_hash_table_insert_uintptr (table3,
1520 _dbus_assert (count_entries (table1) == i + 1);
1521 _dbus_assert (count_entries (table2) == i + 1);
1522 _dbus_assert (count_entries (table3) == i + 1);
1524 value = _dbus_hash_table_lookup_string (table1, keys[i]);
1525 _dbus_assert (value != NULL);
1526 _dbus_assert (strcmp (value, "Value!") == 0);
1528 value = _dbus_hash_table_lookup_int (table2, i);
1529 _dbus_assert (value != NULL);
1530 _dbus_assert (strcmp (value, keys[i]) == 0);
1532 value = _dbus_hash_table_lookup_uintptr (table3, i);
1533 _dbus_assert (value != NULL);
1534 _dbus_assert (strcmp (value, keys[i]) == 0);
1542 _dbus_hash_table_remove_string (table1,
1545 _dbus_hash_table_remove_int (table2, i);
1547 _dbus_hash_table_remove_uintptr (table3, i);
1549 _dbus_assert (count_entries (table1) == i);
1550 _dbus_assert (count_entries (table2) == i);
1551 _dbus_assert (count_entries (table3) == i);
1556 _dbus_hash_table_ref (table1);
1557 _dbus_hash_table_ref (table2);
1558 _dbus_hash_table_ref (table3);
1559 _dbus_hash_table_unref (table1);
1560 _dbus_hash_table_unref (table2);
1561 _dbus_hash_table_unref (table3);
1562 _dbus_hash_table_unref (table1);
1563 _dbus_hash_table_unref (table2);
1564 _dbus_hash_table_unref (table3);
1567 /* Insert a bunch of stuff then check
1568 * that iteration works correctly (finds the right
1569 * values, iter_set_value works, etc.)
1571 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1572 dbus_free, dbus_free);
1576 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1587 key = _dbus_strdup (keys[i]);
1590 value = _dbus_strdup ("Value!");
1594 if (!_dbus_hash_table_insert_string (table1,
1598 value = _dbus_strdup (keys[i]);
1602 if (!_dbus_hash_table_insert_int (table2,
1606 _dbus_assert (count_entries (table1) == i + 1);
1607 _dbus_assert (count_entries (table2) == i + 1);
1612 _dbus_hash_iter_init (table1, &iter);
1613 while (_dbus_hash_iter_next (&iter))
1618 key = _dbus_hash_iter_get_string_key (&iter);
1619 value = _dbus_hash_iter_get_value (&iter);
1621 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1623 value = _dbus_strdup ("Different value!");
1627 _dbus_hash_iter_set_value (&iter, value);
1629 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1632 _dbus_hash_iter_init (table1, &iter);
1633 while (_dbus_hash_iter_next (&iter))
1635 _dbus_hash_iter_remove_entry (&iter);
1636 _dbus_assert (count_entries (table1) == i - 1);
1640 _dbus_hash_iter_init (table2, &iter);
1641 while (_dbus_hash_iter_next (&iter))
1646 key = _dbus_hash_iter_get_int_key (&iter);
1647 value = _dbus_hash_iter_get_value (&iter);
1649 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1651 value = _dbus_strdup ("Different value!");
1655 _dbus_hash_iter_set_value (&iter, value);
1657 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1660 i = count_entries (table2);
1661 _dbus_hash_iter_init (table2, &iter);
1662 while (_dbus_hash_iter_next (&iter))
1664 _dbus_hash_iter_remove_entry (&iter);
1665 _dbus_assert (count_entries (table2) + 1 == i);
1669 /* add/remove interleaved, to check that we grow/shrink the table
1678 key = _dbus_strdup (keys[i]);
1682 value = _dbus_strdup ("Value!");
1686 if (!_dbus_hash_table_insert_string (table1,
1699 key = _dbus_strdup (keys[i]);
1702 value = _dbus_strdup ("Value!");
1706 if (!_dbus_hash_table_remove_string (table1, keys[i]))
1709 if (!_dbus_hash_table_insert_string (table1,
1713 if (!_dbus_hash_table_remove_string (table1, keys[i]))
1716 _dbus_assert (_dbus_hash_table_get_n_entries (table1) == i);
1721 /* nuke these tables */
1722 _dbus_hash_table_unref (table1);
1723 _dbus_hash_table_unref (table2);
1726 /* Now do a bunch of things again using _dbus_hash_iter_lookup() to
1727 * be sure that interface works.
1729 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1730 dbus_free, dbus_free);
1734 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1745 key = _dbus_strdup (keys[i]);
1748 value = _dbus_strdup ("Value!");
1752 if (!_dbus_hash_iter_lookup (table1,
1755 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
1756 _dbus_hash_iter_set_value (&iter, value);
1758 value = _dbus_strdup (keys[i]);
1762 if (!_dbus_hash_iter_lookup (table2,
1763 _DBUS_INT_TO_POINTER (i), TRUE, &iter))
1765 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
1766 _dbus_hash_iter_set_value (&iter, value);
1768 _dbus_assert (count_entries (table1) == i + 1);
1769 _dbus_assert (count_entries (table2) == i + 1);
1771 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
1774 value = _dbus_hash_iter_get_value (&iter);
1775 _dbus_assert (value != NULL);
1776 _dbus_assert (strcmp (value, "Value!") == 0);
1778 /* Iterate just to be sure it works, though
1779 * it's a stupid thing to do
1781 while (_dbus_hash_iter_next (&iter))
1784 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
1787 value = _dbus_hash_iter_get_value (&iter);
1788 _dbus_assert (value != NULL);
1789 _dbus_assert (strcmp (value, keys[i]) == 0);
1791 /* Iterate just to be sure it works, though
1792 * it's a stupid thing to do
1794 while (_dbus_hash_iter_next (&iter))
1803 if (!_dbus_hash_iter_lookup (table1, keys[i], FALSE, &iter))
1804 _dbus_assert_not_reached ("hash entry should have existed");
1805 _dbus_hash_iter_remove_entry (&iter);
1807 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
1808 _dbus_assert_not_reached ("hash entry should have existed");
1809 _dbus_hash_iter_remove_entry (&iter);
1811 _dbus_assert (count_entries (table1) == i);
1812 _dbus_assert (count_entries (table2) == i);
1817 _dbus_hash_table_unref (table1);
1818 _dbus_hash_table_unref (table2);
1823 for (i = 0; i < N_HASH_KEYS; i++)
1824 dbus_free (keys[i]);
1831 #endif /* DBUS_BUILD_TESTS */