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"
81 * @defgroup DBusHashTable Hash table
82 * @ingroup DBusInternals
83 * @brief DBusHashTable data structure
85 * Types and functions related to DBusHashTable.
89 * @defgroup DBusHashTableInternals Hash table implementation details
90 * @ingroup DBusInternals
91 * @brief DBusHashTable implementation details
93 * The guts of DBusHashTable.
99 * When there are this many entries per bucket, on average, rebuild
100 * the hash table to make it larger.
102 #define REBUILD_MULTIPLIER 3
105 * Takes a preliminary integer hash value and produces an index into a
106 * hash tables bucket list. The idea is to make it so that
107 * preliminary values that are arbitrarily similar will end up in
108 * different buckets. The hash function was taken from a
109 * random-number generator. (This is used to hash integers.)
111 #define RANDOM_INDEX(table, i) \
112 (((((long) (i))*1103515245) >> (table)->down_shift) & (table)->mask)
115 * Initial number of buckets in hash table (hash table statically
116 * allocates its buckets for this size and below).
118 #define DBUS_SMALL_HASH_TABLE 4
121 * Typedef for DBusHashEntry
123 typedef struct DBusHashEntry DBusHashEntry;
126 * A single entry (key-value pair) in the hash table.
127 * Internal to hash table implementation.
131 DBusHashEntry *next; /**< Pointer to next entry in this
132 * hash bucket, or #NULL for end of
135 void *key; /**< Hash key */
136 void *value; /**< Hash value */
140 * Function used to find and optionally create a hash entry.
142 typedef DBusHashEntry* (* DBusFindEntryFunction) (DBusHashTable *table,
144 dbus_bool_t create_if_not_found,
145 DBusHashEntry ***bucket);
148 * Hash table internal members. Hash tables are opaque objects,
149 * they must be used via accessor functions.
151 struct DBusHashTable {
152 int refcount; /**< Reference count */
154 DBusHashEntry **buckets; /**< Pointer to bucket array. Each
155 * element points to first entry in
156 * bucket's hash chain, or #NULL.
158 DBusHashEntry *static_buckets[DBUS_SMALL_HASH_TABLE];
159 /**< Bucket array used for small tables
160 * (to avoid mallocs and frees).
162 int n_buckets; /**< Total number of buckets allocated
165 int n_entries; /**< Total number of entries present
168 int rebuild_size; /**< Enlarge table when numEntries gets
171 int down_shift; /**< Shift count used in hashing
172 * function. Designed to use high-
173 * order bits of randomized keys.
175 int mask; /**< Mask value used in hashing
178 DBusHashType key_type; /**< Type of keys used in this table */
181 DBusFindEntryFunction find_function; /**< Function for finding entries */
183 DBusFreeFunction free_key_function; /**< Function to free keys */
184 DBusFreeFunction free_value_function; /**< Function to free values */
188 * Internals of DBusHashIter.
192 DBusHashTable *table; /**< Pointer to table containing entry. */
193 DBusHashEntry **bucket; /**< Pointer to bucket that points to
194 * first entry in this entry's chain:
195 * used for deleting the entry.
197 DBusHashEntry *entry; /**< Current hash entry */
198 DBusHashEntry *next_entry; /**< Next entry to be iterated onto in current bucket */
199 int next_bucket; /**< index of next bucket */
200 int n_entries_on_init; /**< used to detect table resize since initialization */
203 static DBusHashEntry* find_int_function (DBusHashTable *table,
205 dbus_bool_t create_if_not_found,
206 DBusHashEntry ***bucket);
207 static DBusHashEntry* find_string_function (DBusHashTable *table,
209 dbus_bool_t create_if_not_found,
210 DBusHashEntry ***bucket);
211 static unsigned int string_hash (const char *str);
212 static void rebuild_table (DBusHashTable *table);
213 static DBusHashEntry* alloc_entry (DBusHashTable *table);
214 static void remove_entry (DBusHashTable *table,
215 DBusHashEntry **bucket,
216 DBusHashEntry *entry);
217 static void free_entry (DBusHashTable *table,
218 DBusHashEntry *entry);
223 * @addtogroup DBusHashTable
228 * @typedef DBusHashIter
230 * Public opaque hash table iterator object.
234 * @typedef DBusHashTable
236 * Public opaque hash table object.
240 * @typedef DBusHashType
242 * Indicates the type of a key in the hash table.
246 * Constructs a new hash table. Should be freed with
247 * _dbus_hash_table_unref(). If memory cannot be
248 * allocated for the hash table, returns #NULL.
250 * @param type the type of hash key to use.
251 * @param key_free_function function to free hash keys.
252 * @param value_free_function function to free hash values.
253 * @returns a new DBusHashTable or #NULL if no memory.
256 _dbus_hash_table_new (DBusHashType type,
257 DBusFreeFunction key_free_function,
258 DBusFreeFunction value_free_function)
260 DBusHashTable *table;
262 table = dbus_new0 (DBusHashTable, 1);
268 _dbus_assert (DBUS_SMALL_HASH_TABLE == _DBUS_N_ELEMENTS (table->static_buckets));
270 table->buckets = table->static_buckets;
271 table->n_buckets = DBUS_SMALL_HASH_TABLE;
272 table->n_entries = 0;
273 table->rebuild_size = DBUS_SMALL_HASH_TABLE * REBUILD_MULTIPLIER;
274 table->down_shift = 28;
276 table->key_type = type;
278 switch (table->key_type)
281 table->find_function = find_int_function;
283 case DBUS_HASH_STRING:
284 table->find_function = find_string_function;
287 _dbus_assert_not_reached ("Unknown hash table type");
291 table->free_key_function = key_free_function;
292 table->free_value_function = value_free_function;
299 * Increments the reference count for a hash table.
301 * @param table the hash table to add a reference to.
304 _dbus_hash_table_ref (DBusHashTable *table)
306 table->refcount += 1;
310 * Decrements the reference count for a hash table,
311 * freeing the hash table if the count reaches zero.
313 * @param table the hash table to remove a reference from.
316 _dbus_hash_table_unref (DBusHashTable *table)
318 table->refcount -= 1;
320 if (table->refcount == 0)
322 DBusHashEntry *entry;
326 /* Free the entries in the table. */
328 for (i = 0; i < table->n_buckets; i++)
330 entry = table->buckets[i];
331 while (entry != NULL)
335 free_entry (table, entry);
341 /* Free the bucket array, if it was dynamically allocated. */
342 if (table->buckets != table->static_buckets)
343 dbus_free (table->buckets);
349 static DBusHashEntry*
350 alloc_entry (DBusHashTable *table)
352 DBusHashEntry *entry;
354 entry = dbus_new0 (DBusHashEntry, 1);
360 free_entry (DBusHashTable *table,
361 DBusHashEntry *entry)
363 if (table->free_key_function)
364 (* table->free_key_function) (entry->key);
365 if (table->free_value_function)
366 (* table->free_value_function) (entry->value);
372 remove_entry (DBusHashTable *table,
373 DBusHashEntry **bucket,
374 DBusHashEntry *entry)
376 _dbus_assert (table != NULL);
377 _dbus_assert (bucket != NULL);
378 _dbus_assert (*bucket != NULL);
379 _dbus_assert (entry != NULL);
381 if (*bucket == entry)
382 *bucket = entry->next;
388 while (prev->next != entry)
391 _dbus_assert (prev != NULL);
393 prev->next = entry->next;
396 table->n_entries -= 1;
397 free_entry (table, entry);
401 * Initializes a hash table iterator. To iterate over all entries in a
402 * hash table, use the following code (the printf assumes a hash
403 * from strings to strings obviously):
408 * _dbus_hash_iter_init (table, &iter);
409 * while (_dbus_hash_iter_next (&iter))
411 * printf ("The first key is %s and value is %s\n",
412 * _dbus_hash_iter_get_string_key (&iter),
413 * _dbus_hash_iter_get_value (&iter));
419 * The iterator is initialized pointing "one before" the first hash
420 * entry. The first call to _dbus_hash_iter_next() moves it onto
421 * the first valid entry or returns #FALSE if the hash table is
422 * empty. Subsequent calls move to the next valid entry or return
423 * #FALSE if there are no more entries.
425 * Note that it is guaranteed to be safe to remove a hash entry during
426 * iteration, but it is not safe to add a hash entry.
428 * @param table the hash table to iterate over.
429 * @param iter the iterator to initialize.
432 _dbus_hash_iter_init (DBusHashTable *table,
435 DBusRealHashIter *real;
437 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
439 real = (DBusRealHashIter*) iter;
444 real->next_entry = NULL;
445 real->next_bucket = 0;
446 real->n_entries_on_init = table->n_entries;
450 * Move the hash iterator forward one step, to the next hash entry.
451 * The documentation for _dbus_hash_iter_init() explains in more
454 * @param iter the iterator to move forward.
455 * @returns #FALSE if there are no more entries to move to.
458 _dbus_hash_iter_next (DBusHashIter *iter)
460 DBusRealHashIter *real;
462 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
464 real = (DBusRealHashIter*) iter;
466 /* if this assertion failed someone probably added hash entries
467 * during iteration, which is bad.
469 _dbus_assert (real->n_entries_on_init >= real->table->n_entries);
471 /* Remember that real->entry may have been deleted */
473 while (real->next_entry == NULL)
475 if (real->next_bucket >= real->table->n_buckets)
477 /* invalidate iter and return false */
484 real->bucket = &(real->table->buckets[real->next_bucket]);
485 real->next_entry = *(real->bucket);
486 real->next_bucket += 1;
489 _dbus_assert (real->next_entry != NULL);
490 _dbus_assert (real->bucket != NULL);
492 real->entry = real->next_entry;
493 real->next_entry = real->entry->next;
499 * Removes the current entry from the hash table.
500 * If a key_free_function or value_free_function
501 * was provided to _dbus_hash_table_new(),
502 * frees the key and/or value for this entry.
504 * @param iter the hash table iterator.
507 _dbus_hash_iter_remove_entry (DBusHashIter *iter)
509 DBusRealHashIter *real;
511 real = (DBusRealHashIter*) iter;
513 _dbus_assert (real->table != NULL);
514 _dbus_assert (real->entry != NULL);
515 _dbus_assert (real->bucket != NULL);
517 remove_entry (real->table, real->bucket, real->entry);
519 real->entry = NULL; /* make it crash if you try to use this entry */
523 * Gets the value of the current entry.
525 * @param iter the hash table iterator.
528 _dbus_hash_iter_get_value (DBusHashIter *iter)
530 DBusRealHashIter *real;
532 real = (DBusRealHashIter*) iter;
534 _dbus_assert (real->table != NULL);
535 _dbus_assert (real->entry != NULL);
537 return real->entry->value;
541 * Sets the value of the current entry.
542 * If the hash table has a value_free_function
543 * it will be used to free the previous value.
544 * The hash table will own the passed-in value
545 * (it will not be copied).
547 * @param iter the hash table iterator.
548 * @param value the new value.
551 _dbus_hash_iter_set_value (DBusHashIter *iter,
554 DBusRealHashIter *real;
556 real = (DBusRealHashIter*) iter;
558 _dbus_assert (real->table != NULL);
559 _dbus_assert (real->entry != NULL);
561 if (real->table->free_value_function && value != real->entry->value)
562 (* real->table->free_value_function) (real->entry->value);
564 real->entry->value = value;
568 * Gets the key for the current entry.
569 * Only works for hash tables of type #DBUS_HASH_INT.
571 * @param iter the hash table iterator.
574 _dbus_hash_iter_get_int_key (DBusHashIter *iter)
576 DBusRealHashIter *real;
578 real = (DBusRealHashIter*) iter;
580 _dbus_assert (real->table != NULL);
581 _dbus_assert (real->entry != NULL);
583 return _DBUS_POINTER_TO_INT (real->entry->key);
587 * Gets the key for the current entry.
588 * Only works for hash tables of type #DBUS_HASH_STRING
589 * @param iter the hash table iterator.
592 _dbus_hash_iter_get_string_key (DBusHashIter *iter)
594 DBusRealHashIter *real;
596 real = (DBusRealHashIter*) iter;
598 _dbus_assert (real->table != NULL);
599 _dbus_assert (real->entry != NULL);
601 return real->entry->key;
605 * A low-level but efficient interface for manipulating the hash
606 * table. It's efficient because you can get, set, and optionally
607 * create the hash entry while only running the hash function one
610 * Note that while calling _dbus_hash_iter_next() on the iterator
611 * filled in by this function may work, it's completely
612 * undefined which entries are after this iter and which
613 * are before it. So it would be silly to iterate using this
616 * If the hash entry is created, its value will be initialized
619 * #FALSE may be returned due to memory allocation failure, or
620 * because create_if_not_found was #FALSE and the entry
623 * For a hash table of type #DBUS_HASH_INT, cast the int
624 * key to the key parameter using #_DBUS_INT_TO_POINTER().
626 * @param table the hash table.
627 * @param key the hash key.
628 * @param create_if_not_found if #TRUE, create the entry if it didn't exist.
629 * @param iter the iterator to initialize.
630 * @returns #TRUE if the hash entry now exists (and the iterator is thus valid).
633 _dbus_hash_iter_lookup (DBusHashTable *table,
635 dbus_bool_t create_if_not_found,
638 DBusRealHashIter *real;
639 DBusHashEntry *entry;
640 DBusHashEntry **bucket;
642 _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter));
644 real = (DBusRealHashIter*) iter;
646 entry = (* table->find_function) (table, key, create_if_not_found, &bucket);
652 real->bucket = bucket;
654 real->next_entry = entry->next;
655 real->next_bucket = (bucket - table->buckets) + 1;
656 real->n_entries_on_init = table->n_entries;
658 _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket);
663 static DBusHashEntry*
664 add_entry (DBusHashTable *table,
667 DBusHashEntry ***bucket)
669 DBusHashEntry *entry;
672 entry = alloc_entry (table);
682 b = &(table->buckets[idx]);
689 table->n_entries += 1;
691 if (table->n_entries >= table->rebuild_size)
692 rebuild_table (table);
698 string_hash (const char *str)
700 register unsigned int result;
704 * I tried a zillion different hash functions and asked many other
705 * people for advice. Many people had their own favorite functions,
706 * all different, but no-one had much idea why they were good ones.
707 * I chose the one below (multiply by 9 and add new character)
708 * because of the following reasons:
710 * 1. Multiplying by 10 is perfect for keys that are decimal strings,
711 * and multiplying by 9 is just about as good.
712 * 2. Times-9 is (shift-left-3) plus (old). This means that each
713 * character's bits hang around in the low-order bits of the
714 * hash value for ever, plus they spread fairly rapidly up to
715 * the high-order bits to fill out the hash value. This seems
716 * works well both for decimal and non-decimal strings.
727 result += (result << 3) + c;
733 static DBusHashEntry*
734 find_string_function (DBusHashTable *table,
736 dbus_bool_t create_if_not_found,
737 DBusHashEntry ***bucket)
739 DBusHashEntry *entry;
745 idx = string_hash (key) & table->mask;
747 /* Search all of the entries in this bucket. */
748 entry = table->buckets[idx];
749 while (entry != NULL)
751 if (strcmp (key, entry->key) == 0)
754 *bucket = &(table->buckets[idx]);
761 if (create_if_not_found)
762 entry = add_entry (table, idx, key, bucket);
767 static DBusHashEntry*
768 find_int_function (DBusHashTable *table,
770 dbus_bool_t create_if_not_found,
771 DBusHashEntry ***bucket)
773 DBusHashEntry *entry;
779 idx = RANDOM_INDEX (table, key) & table->mask;
781 /* Search all of the entries in this bucket. */
782 entry = table->buckets[idx];
783 while (entry != NULL)
785 if (key == entry->key)
788 *bucket = &(table->buckets[idx]);
795 /* Entry not found. Add a new one to the bucket. */
796 if (create_if_not_found)
797 entry = add_entry (table, idx, key, bucket);
803 rebuild_table (DBusHashTable *table)
806 DBusHashEntry **old_buckets;
807 DBusHashEntry **old_chain;
808 DBusHashEntry *entry;
811 * Allocate and initialize the new bucket array, and set up
812 * hashing constants for new array size.
815 old_size = table->n_buckets;
816 old_buckets = table->buckets;
818 table->n_buckets *= 4;
819 table->buckets = dbus_new0 (DBusHashEntry*, table->n_buckets);
820 if (table->buckets == NULL)
822 /* out of memory, yay - just don't reallocate, the table will
823 * still work, albeit more slowly.
825 table->n_buckets /= 4;
826 table->buckets = old_buckets;
830 table->rebuild_size *= 4;
831 table->down_shift -= 2;
832 table->mask = (table->mask << 2) + 3;
835 * Rehash all of the existing entries into the new bucket array.
838 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
840 for (entry = *old_chain; entry != NULL; entry = *old_chain)
843 DBusHashEntry **bucket;
845 *old_chain = entry->next;
846 switch (table->key_type)
848 case DBUS_HASH_STRING:
849 idx = string_hash (entry->key) & table->mask;
852 idx = RANDOM_INDEX (table, entry->key);
856 _dbus_assert_not_reached ("Unknown hash table type");
860 bucket = &(table->buckets[idx]);
861 entry->next = *bucket;
866 /* Free the old bucket array, if it was dynamically allocated. */
868 if (old_buckets != table->static_buckets)
869 dbus_free (old_buckets);
873 * Looks up the value for a given string in a hash table
874 * of type #DBUS_HASH_STRING. Returns %NULL if the value
875 * is not present. (A not-present entry is indistinguishable
876 * from an entry with a value of %NULL.)
877 * @param table the hash table.
878 * @param key the string to look up.
879 * @returns the value of the hash entry.
882 _dbus_hash_table_lookup_string (DBusHashTable *table,
885 DBusHashEntry *entry;
887 _dbus_assert (table->key_type == DBUS_HASH_STRING);
889 entry = (* table->find_function) (table, (char*) key, FALSE, NULL);
898 * Looks up the value for a given integer in a hash table
899 * of type #DBUS_HASH_INT. Returns %NULL if the value
900 * is not present. (A not-present entry is indistinguishable
901 * from an entry with a value of %NULL.)
902 * @param table the hash table.
903 * @param key the integer to look up.
904 * @returns the value of the hash entry.
907 _dbus_hash_table_lookup_int (DBusHashTable *table,
910 DBusHashEntry *entry;
912 _dbus_assert (table->key_type == DBUS_HASH_INT);
914 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, NULL);
923 * Removes the hash entry for the given key. If no hash entry
924 * for the key exists, does nothing.
926 * @param table the hash table.
927 * @param key the hash key.
930 _dbus_hash_table_remove_string (DBusHashTable *table,
933 DBusHashEntry *entry;
934 DBusHashEntry **bucket;
936 _dbus_assert (table->key_type == DBUS_HASH_STRING);
938 entry = (* table->find_function) (table, (char*) key, FALSE, &bucket);
941 remove_entry (table, bucket, entry);
945 * Removes the hash entry for the given key. If no hash entry
946 * for the key exists, does nothing.
948 * @param table the hash table.
949 * @param key the hash key.
952 _dbus_hash_table_remove_int (DBusHashTable *table,
955 DBusHashEntry *entry;
956 DBusHashEntry **bucket;
958 _dbus_assert (table->key_type == DBUS_HASH_INT);
960 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, &bucket);
963 remove_entry (table, bucket, entry);
967 * Creates a hash entry with the given key and value.
968 * The key and value are not copied; they are stored
969 * in the hash table by reference. If an entry with the
970 * given key already exists, the previous key and value
971 * are overwritten (and freed if the hash table has
972 * a key_free_function and/or value_free_function).
974 * Returns #FALSE if memory for the new hash entry
975 * can't be allocated.
977 * @param table the hash table.
978 * @param key the hash entry key.
979 * @param value the hash entry value.
982 _dbus_hash_table_insert_string (DBusHashTable *table,
986 DBusHashEntry *entry;
988 _dbus_assert (table->key_type == DBUS_HASH_STRING);
990 entry = (* table->find_function) (table, key, TRUE, NULL);
993 return FALSE; /* no memory */
995 if (table->free_key_function && entry->key != key)
996 (* table->free_key_function) (entry->key);
998 if (table->free_value_function && entry->value != value)
999 (* table->free_value_function) (entry->value);
1002 entry->value = value;
1008 * Creates a hash entry with the given key and value.
1009 * The key and value are not copied; they are stored
1010 * in the hash table by reference. If an entry with the
1011 * given key already exists, the previous key and value
1012 * are overwritten (and freed if the hash table has
1013 * a key_free_function and/or value_free_function).
1015 * Returns #FALSE if memory for the new hash entry
1016 * can't be allocated.
1018 * @param table the hash table.
1019 * @param key the hash entry key.
1020 * @param value the hash entry value.
1023 _dbus_hash_table_insert_int (DBusHashTable *table,
1027 DBusHashEntry *entry;
1029 _dbus_assert (table->key_type == DBUS_HASH_INT);
1031 entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), TRUE, NULL);
1034 return FALSE; /* no memory */
1036 if (table->free_key_function && entry->key != _DBUS_INT_TO_POINTER (key))
1037 (* table->free_key_function) (entry->key);
1039 if (table->free_value_function && entry->value != value)
1040 (* table->free_value_function) (entry->value);
1042 entry->key = _DBUS_INT_TO_POINTER (key);
1043 entry->value = value;
1049 * Gets the number of hash entries in a hash table.
1051 * @param table the hash table.
1052 * @returns the number of entries in the table.
1055 _dbus_hash_table_get_n_entries (DBusHashTable *table)
1057 return table->n_entries;
1062 #ifdef DBUS_BUILD_TESTS
1063 #include "dbus-test.h"
1067 count_entries (DBusHashTable *table)
1073 _dbus_hash_iter_init (table, &iter);
1074 while (_dbus_hash_iter_next (&iter))
1077 _dbus_assert (count == _dbus_hash_table_get_n_entries (table));
1083 * @ingroup DBusHashTableInternals
1084 * Unit test for DBusHashTable
1085 * @returns #TRUE on success.
1088 _dbus_hash_test (void)
1091 DBusHashTable *table1;
1092 DBusHashTable *table2;
1095 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1096 dbus_free, dbus_free);
1100 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1105 /* Insert and remove a bunch of stuff, counting the table in between
1106 * to be sure it's not broken and that iteration works
1112 sprintf (buf, "Hash key %d", i);
1116 key = _dbus_strdup (buf);
1119 value = _dbus_strdup ("Value!");
1123 if (!_dbus_hash_table_insert_string (table1,
1127 value = _dbus_strdup (buf);
1131 if (!_dbus_hash_table_insert_int (table2,
1135 _dbus_assert (count_entries (table1) == i + 1);
1136 _dbus_assert (count_entries (table2) == i + 1);
1138 value = _dbus_hash_table_lookup_string (table1, buf);
1139 _dbus_assert (value != NULL);
1140 _dbus_assert (strcmp (value, "Value!") == 0);
1142 value = _dbus_hash_table_lookup_int (table2, i);
1143 _dbus_assert (value != NULL);
1144 _dbus_assert (strcmp (value, buf) == 0);
1153 sprintf (buf, "Hash key %d", i);
1155 _dbus_hash_table_remove_string (table1,
1158 _dbus_hash_table_remove_int (table2, i);
1160 _dbus_assert (count_entries (table1) == i);
1161 _dbus_assert (count_entries (table2) == i);
1166 _dbus_hash_table_ref (table1);
1167 _dbus_hash_table_ref (table2);
1168 _dbus_hash_table_unref (table1);
1169 _dbus_hash_table_unref (table2);
1170 _dbus_hash_table_unref (table1);
1171 _dbus_hash_table_unref (table2);
1174 /* Insert a bunch of stuff then check
1175 * that iteration works correctly (finds the right
1176 * values, iter_set_value works, etc.)
1178 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1179 dbus_free, dbus_free);
1183 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1192 sprintf (buf, "Hash key %d", i);
1196 key = _dbus_strdup (buf);
1199 value = _dbus_strdup ("Value!");
1203 if (!_dbus_hash_table_insert_string (table1,
1207 value = _dbus_strdup (buf);
1211 if (!_dbus_hash_table_insert_int (table2,
1215 _dbus_assert (count_entries (table1) == i + 1);
1216 _dbus_assert (count_entries (table2) == i + 1);
1221 _dbus_hash_iter_init (table1, &iter);
1222 while (_dbus_hash_iter_next (&iter))
1227 key = _dbus_hash_iter_get_string_key (&iter);
1228 value = _dbus_hash_iter_get_value (&iter);
1230 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1232 value = _dbus_strdup ("Different value!");
1236 _dbus_hash_iter_set_value (&iter, value);
1238 _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value);
1241 _dbus_hash_iter_init (table1, &iter);
1242 while (_dbus_hash_iter_next (&iter))
1244 _dbus_hash_iter_remove_entry (&iter);
1245 _dbus_assert (count_entries (table1) == i - 1);
1249 _dbus_hash_iter_init (table2, &iter);
1250 while (_dbus_hash_iter_next (&iter))
1255 key = _dbus_hash_iter_get_int_key (&iter);
1256 value = _dbus_hash_iter_get_value (&iter);
1258 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1260 value = _dbus_strdup ("Different value!");
1264 _dbus_hash_iter_set_value (&iter, value);
1266 _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value);
1269 i = count_entries (table2);
1270 _dbus_hash_iter_init (table2, &iter);
1271 while (_dbus_hash_iter_next (&iter))
1273 _dbus_hash_iter_remove_entry (&iter);
1274 _dbus_assert (count_entries (table2) + 1 == i);
1278 _dbus_hash_table_unref (table1);
1279 _dbus_hash_table_unref (table2);
1282 /* Now do a bunch of things again using _dbus_hash_iter_lookup() to
1283 * be sure that interface works.
1285 table1 = _dbus_hash_table_new (DBUS_HASH_STRING,
1286 dbus_free, dbus_free);
1290 table2 = _dbus_hash_table_new (DBUS_HASH_INT,
1299 sprintf (buf, "Hash key %d", i);
1303 key = _dbus_strdup (buf);
1306 value = _dbus_strdup ("Value!");
1310 if (!_dbus_hash_iter_lookup (table1,
1313 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
1314 _dbus_hash_iter_set_value (&iter, value);
1316 value = _dbus_strdup (buf);
1320 if (!_dbus_hash_iter_lookup (table2,
1321 _DBUS_INT_TO_POINTER (i), TRUE, &iter))
1323 _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL);
1324 _dbus_hash_iter_set_value (&iter, value);
1326 _dbus_assert (count_entries (table1) == i + 1);
1327 _dbus_assert (count_entries (table2) == i + 1);
1329 if (!_dbus_hash_iter_lookup (table1, buf, FALSE, &iter))
1332 value = _dbus_hash_iter_get_value (&iter);
1333 _dbus_assert (value != NULL);
1334 _dbus_assert (strcmp (value, "Value!") == 0);
1336 /* Iterate just to be sure it works, though
1337 * it's a stupid thing to do
1339 while (_dbus_hash_iter_next (&iter))
1342 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
1345 value = _dbus_hash_iter_get_value (&iter);
1346 _dbus_assert (value != NULL);
1347 _dbus_assert (strcmp (value, buf) == 0);
1349 /* Iterate just to be sure it works, though
1350 * it's a stupid thing to do
1352 while (_dbus_hash_iter_next (&iter))
1362 sprintf (buf, "Hash key %d", i);
1364 if (!_dbus_hash_iter_lookup (table1, buf, FALSE, &iter))
1365 _dbus_assert_not_reached ("hash entry should have existed");
1366 _dbus_hash_iter_remove_entry (&iter);
1368 if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter))
1369 _dbus_assert_not_reached ("hash entry should have existed");
1370 _dbus_hash_iter_remove_entry (&iter);
1372 _dbus_assert (count_entries (table1) == i);
1373 _dbus_assert (count_entries (table2) == i);
1378 _dbus_hash_table_unref (table1);
1379 _dbus_hash_table_unref (table2);