/* -*- mode: C; c-file-style: "gnu" -*- */ /* dbus-hash.c Generic hash table utility (internal to D-BUS implementation) * * Copyright (C) 2002 Red Hat, Inc. * Copyright (c) 1991-1993 The Regents of the University of California. * Copyright (c) 1994 Sun Microsystems, Inc. * * Hash table implementation based on generic/tclHash.c from the Tcl * source code. The original Tcl license applies to portions of the * code from tclHash.c; the Tcl license follows this standad D-BUS * license information. * * Licensed under the Academic Free License version 1.2 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /* * The following copyright applies to code from the Tcl distribution. * * Copyright (c) 1991-1993 The Regents of the University of California. * Copyright (c) 1994 Sun Microsystems, Inc. * * This software is copyrighted by the Regents of the University of * California, Sun Microsystems, Inc., Scriptics Corporation, and * other parties. The following terms apply to all files associated * with the software unless explicitly disclaimed in individual files. * * The authors hereby grant permission to use, copy, modify, * distribute, and license this software and its documentation for any * purpose, provided that existing copyright notices are retained in * all copies and that this notice is included verbatim in any * distributions. No written agreement, license, or royalty fee is * required for any of the authorized uses. Modifications to this * software may be copyrighted by their authors and need not follow * the licensing terms described here, provided that the new terms are * clearly indicated on the first page of each file where they apply. * * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * * GOVERNMENT USE: If you are acquiring this software on behalf of the * U.S. government, the Government shall have only "Restricted Rights" * in the software and related documentation as defined in the Federal * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you * are acquiring the software on behalf of the Department of Defense, * the software shall be classified as "Commercial Computer Software" * and the Government shall have only "Restricted Rights" as defined * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the * foregoing, the authors grant the U.S. Government and others acting * in its behalf permission to use and distribute the software in * accordance with the terms specified in this license. */ #include "dbus-hash.h" #include "dbus-internals.h" /** * @defgroup DBusHashTable Hash table * @ingroup DBusInternals * @brief DBusHashTable data structure * * Types and functions related to DBusHashTable. */ /** * @defgroup DBusHashTableInternals Hash table implementation details * @ingroup DBusInternals * @brief DBusHashTable implementation details * * The guts of DBusHashTable. * * @todo rebuild_table() should be modified to also shrink the hash bucket * array when appropriate; otherwise if a hash table has been * very large but is now small, iteration becomes inefficient. * We should still only shrink when adding hash entries though, not * when removing them, so that you can still iterate over the hash * removing entries. So if you added 5000, removed 4000, the * shrinking would happen next time an entry was added. * @{ */ /** * When there are this many entries per bucket, on average, rebuild * the hash table to make it larger. */ #define REBUILD_MULTIPLIER 3 /** * Takes a preliminary integer hash value and produces an index into a * hash tables bucket list. The idea is to make it so that * preliminary values that are arbitrarily similar will end up in * different buckets. The hash function was taken from a * random-number generator. (This is used to hash integers.) */ #define RANDOM_INDEX(table, i) \ (((((long) (i))*1103515245) >> (table)->down_shift) & (table)->mask) /** * Initial number of buckets in hash table (hash table statically * allocates its buckets for this size and below). */ #define DBUS_SMALL_HASH_TABLE 4 /** * Typedef for DBusHashEntry */ typedef struct DBusHashEntry DBusHashEntry; /** * @brief Internal representation of a hash entry. * * A single entry (key-value pair) in the hash table. * Internal to hash table implementation. */ struct DBusHashEntry { DBusHashEntry *next; /**< Pointer to next entry in this * hash bucket, or #NULL for end of * chain. */ void *key; /**< Hash key */ void *value; /**< Hash value */ }; /** * Function used to find and optionally create a hash entry. */ typedef DBusHashEntry* (* DBusFindEntryFunction) (DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashEntry ***bucket); /** * @brief Internals of DBusHashTable. * * Hash table internals. Hash tables are opaque objects, they must be * used via accessor functions. */ struct DBusHashTable { int refcount; /**< Reference count */ DBusHashEntry **buckets; /**< Pointer to bucket array. Each * element points to first entry in * bucket's hash chain, or #NULL. */ DBusHashEntry *static_buckets[DBUS_SMALL_HASH_TABLE]; /**< Bucket array used for small tables * (to avoid mallocs and frees). */ int n_buckets; /**< Total number of buckets allocated * at **buckets. */ int n_entries; /**< Total number of entries present * in table. */ int rebuild_size; /**< Enlarge table when numEntries gets * to be this large. */ int down_shift; /**< Shift count used in hashing * function. Designed to use high- * order bits of randomized keys. */ int mask; /**< Mask value used in hashing * function. */ DBusHashType key_type; /**< Type of keys used in this table */ DBusFindEntryFunction find_function; /**< Function for finding entries */ DBusFreeFunction free_key_function; /**< Function to free keys */ DBusFreeFunction free_value_function; /**< Function to free values */ }; /** * @brief Internals of DBusHashIter. */ typedef struct { DBusHashTable *table; /**< Pointer to table containing entry. */ DBusHashEntry **bucket; /**< Pointer to bucket that points to * first entry in this entry's chain: * used for deleting the entry. */ DBusHashEntry *entry; /**< Current hash entry */ DBusHashEntry *next_entry; /**< Next entry to be iterated onto in current bucket */ int next_bucket; /**< index of next bucket */ int n_entries_on_init; /**< used to detect table resize since initialization */ } DBusRealHashIter; static DBusHashEntry* find_int_function (DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashEntry ***bucket); static DBusHashEntry* find_string_function (DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashEntry ***bucket); static unsigned int string_hash (const char *str); static void rebuild_table (DBusHashTable *table); static DBusHashEntry* alloc_entry (DBusHashTable *table); static void remove_entry (DBusHashTable *table, DBusHashEntry **bucket, DBusHashEntry *entry); static void free_entry (DBusHashTable *table, DBusHashEntry *entry); /** @} */ /** * @addtogroup DBusHashTable * @{ */ /** * @typedef DBusHashIter * * Public opaque hash table iterator object. */ /** * @typedef DBusHashTable * * Public opaque hash table object. */ /** * @typedef DBusHashType * * Indicates the type of a key in the hash table. */ /** * Constructs a new hash table. Should be freed with * _dbus_hash_table_unref(). If memory cannot be * allocated for the hash table, returns #NULL. * * @param type the type of hash key to use. * @param key_free_function function to free hash keys. * @param value_free_function function to free hash values. * @returns a new DBusHashTable or #NULL if no memory. */ DBusHashTable* _dbus_hash_table_new (DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function) { DBusHashTable *table; table = dbus_new0 (DBusHashTable, 1); if (table == NULL) return NULL; table->refcount = 1; _dbus_assert (DBUS_SMALL_HASH_TABLE == _DBUS_N_ELEMENTS (table->static_buckets)); table->buckets = table->static_buckets; table->n_buckets = DBUS_SMALL_HASH_TABLE; table->n_entries = 0; table->rebuild_size = DBUS_SMALL_HASH_TABLE * REBUILD_MULTIPLIER; table->down_shift = 28; table->mask = 3; table->key_type = type; switch (table->key_type) { case DBUS_HASH_INT: table->find_function = find_int_function; break; case DBUS_HASH_STRING: table->find_function = find_string_function; break; default: _dbus_assert_not_reached ("Unknown hash table type"); break; } table->free_key_function = key_free_function; table->free_value_function = value_free_function; return table; } /** * Increments the reference count for a hash table. * * @param table the hash table to add a reference to. */ void _dbus_hash_table_ref (DBusHashTable *table) { table->refcount += 1; } /** * Decrements the reference count for a hash table, * freeing the hash table if the count reaches zero. * * @param table the hash table to remove a reference from. */ void _dbus_hash_table_unref (DBusHashTable *table) { table->refcount -= 1; if (table->refcount == 0) { DBusHashEntry *entry; DBusHashEntry *next; int i; /* Free the entries in the table. */ for (i = 0; i < table->n_buckets; i++) { entry = table->buckets[i]; while (entry != NULL) { next = entry->next; free_entry (table, entry); entry = next; } } /* Free the bucket array, if it was dynamically allocated. */ if (table->buckets != table->static_buckets) dbus_free (table->buckets); dbus_free (table); } } static DBusHashEntry* alloc_entry (DBusHashTable *table) { DBusHashEntry *entry; entry = dbus_new0 (DBusHashEntry, 1); return entry; } static void free_entry (DBusHashTable *table, DBusHashEntry *entry) { if (table->free_key_function) (* table->free_key_function) (entry->key); if (table->free_value_function) (* table->free_value_function) (entry->value); dbus_free (entry); } static void remove_entry (DBusHashTable *table, DBusHashEntry **bucket, DBusHashEntry *entry) { _dbus_assert (table != NULL); _dbus_assert (bucket != NULL); _dbus_assert (*bucket != NULL); _dbus_assert (entry != NULL); if (*bucket == entry) *bucket = entry->next; else { DBusHashEntry *prev; prev = *bucket; while (prev->next != entry) prev = prev->next; _dbus_assert (prev != NULL); prev->next = entry->next; } table->n_entries -= 1; free_entry (table, entry); } /** * Initializes a hash table iterator. To iterate over all entries in a * hash table, use the following code (the printf assumes a hash * from strings to strings obviously): * * @code * DBusHashIter iter; * * _dbus_hash_iter_init (table, &iter); * while (_dbus_hash_iter_next (&iter)) * { * printf ("The first key is %s and value is %s\n", * _dbus_hash_iter_get_string_key (&iter), * _dbus_hash_iter_get_value (&iter)); * } * * * @endcode * * The iterator is initialized pointing "one before" the first hash * entry. The first call to _dbus_hash_iter_next() moves it onto * the first valid entry or returns #FALSE if the hash table is * empty. Subsequent calls move to the next valid entry or return * #FALSE if there are no more entries. * * Note that it is guaranteed to be safe to remove a hash entry during * iteration, but it is not safe to add a hash entry. * * @param table the hash table to iterate over. * @param iter the iterator to initialize. */ void _dbus_hash_iter_init (DBusHashTable *table, DBusHashIter *iter) { DBusRealHashIter *real; _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); real = (DBusRealHashIter*) iter; real->table = table; real->bucket = NULL; real->entry = NULL; real->next_entry = NULL; real->next_bucket = 0; real->n_entries_on_init = table->n_entries; } /** * Move the hash iterator forward one step, to the next hash entry. * The documentation for _dbus_hash_iter_init() explains in more * detail. * * @param iter the iterator to move forward. * @returns #FALSE if there are no more entries to move to. */ dbus_bool_t _dbus_hash_iter_next (DBusHashIter *iter) { DBusRealHashIter *real; _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); real = (DBusRealHashIter*) iter; /* if this assertion failed someone probably added hash entries * during iteration, which is bad. */ _dbus_assert (real->n_entries_on_init >= real->table->n_entries); /* Remember that real->entry may have been deleted */ while (real->next_entry == NULL) { if (real->next_bucket >= real->table->n_buckets) { /* invalidate iter and return false */ real->entry = NULL; real->table = NULL; real->bucket = NULL; return FALSE; } real->bucket = &(real->table->buckets[real->next_bucket]); real->next_entry = *(real->bucket); real->next_bucket += 1; } _dbus_assert (real->next_entry != NULL); _dbus_assert (real->bucket != NULL); real->entry = real->next_entry; real->next_entry = real->entry->next; return TRUE; } /** * Removes the current entry from the hash table. * If a key_free_function or value_free_function * was provided to _dbus_hash_table_new(), * frees the key and/or value for this entry. * * @param iter the hash table iterator. */ void _dbus_hash_iter_remove_entry (DBusHashIter *iter) { DBusRealHashIter *real; real = (DBusRealHashIter*) iter; _dbus_assert (real->table != NULL); _dbus_assert (real->entry != NULL); _dbus_assert (real->bucket != NULL); remove_entry (real->table, real->bucket, real->entry); real->entry = NULL; /* make it crash if you try to use this entry */ } /** * Gets the value of the current entry. * * @param iter the hash table iterator. */ void* _dbus_hash_iter_get_value (DBusHashIter *iter) { DBusRealHashIter *real; real = (DBusRealHashIter*) iter; _dbus_assert (real->table != NULL); _dbus_assert (real->entry != NULL); return real->entry->value; } /** * Sets the value of the current entry. * If the hash table has a value_free_function * it will be used to free the previous value. * The hash table will own the passed-in value * (it will not be copied). * * @param iter the hash table iterator. * @param value the new value. */ void _dbus_hash_iter_set_value (DBusHashIter *iter, void *value) { DBusRealHashIter *real; real = (DBusRealHashIter*) iter; _dbus_assert (real->table != NULL); _dbus_assert (real->entry != NULL); if (real->table->free_value_function && value != real->entry->value) (* real->table->free_value_function) (real->entry->value); real->entry->value = value; } /** * Gets the key for the current entry. * Only works for hash tables of type #DBUS_HASH_INT. * * @param iter the hash table iterator. */ int _dbus_hash_iter_get_int_key (DBusHashIter *iter) { DBusRealHashIter *real; real = (DBusRealHashIter*) iter; _dbus_assert (real->table != NULL); _dbus_assert (real->entry != NULL); return _DBUS_POINTER_TO_INT (real->entry->key); } /** * Gets the key for the current entry. * Only works for hash tables of type #DBUS_HASH_STRING * @param iter the hash table iterator. */ const char* _dbus_hash_iter_get_string_key (DBusHashIter *iter) { DBusRealHashIter *real; real = (DBusRealHashIter*) iter; _dbus_assert (real->table != NULL); _dbus_assert (real->entry != NULL); return real->entry->key; } /** * A low-level but efficient interface for manipulating the hash * table. It's efficient because you can get, set, and optionally * create the hash entry while only running the hash function one * time. * * Note that while calling _dbus_hash_iter_next() on the iterator * filled in by this function may work, it's completely * undefined which entries are after this iter and which * are before it. So it would be silly to iterate using this * iterator. * * If the hash entry is created, its value will be initialized * to all bits zero. * * #FALSE may be returned due to memory allocation failure, or * because create_if_not_found was #FALSE and the entry * did not exist. * * For a hash table of type #DBUS_HASH_INT, cast the int * key to the key parameter using #_DBUS_INT_TO_POINTER(). * * @param table the hash table. * @param key the hash key. * @param create_if_not_found if #TRUE, create the entry if it didn't exist. * @param iter the iterator to initialize. * @returns #TRUE if the hash entry now exists (and the iterator is thus valid). */ dbus_bool_t _dbus_hash_iter_lookup (DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashIter *iter) { DBusRealHashIter *real; DBusHashEntry *entry; DBusHashEntry **bucket; _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); real = (DBusRealHashIter*) iter; entry = (* table->find_function) (table, key, create_if_not_found, &bucket); if (entry == NULL) return FALSE; real->table = table; real->bucket = bucket; real->entry = entry; real->next_entry = entry->next; real->next_bucket = (bucket - table->buckets) + 1; real->n_entries_on_init = table->n_entries; _dbus_assert (&(table->buckets[real->next_bucket-1]) == real->bucket); return TRUE; } static DBusHashEntry* add_entry (DBusHashTable *table, unsigned int idx, void *key, DBusHashEntry ***bucket) { DBusHashEntry *entry; DBusHashEntry **b; entry = alloc_entry (table); if (entry == NULL) { if (bucket) *bucket = NULL; return NULL; } entry->key = key; b = &(table->buckets[idx]); entry->next = *b; *b = entry; if (bucket) *bucket = b; table->n_entries += 1; if (table->n_entries >= table->rebuild_size) rebuild_table (table); return entry; } static unsigned int string_hash (const char *str) { register unsigned int result; register int c; /* * I tried a zillion different hash functions and asked many other * people for advice. Many people had their own favorite functions, * all different, but no-one had much idea why they were good ones. * I chose the one below (multiply by 9 and add new character) * because of the following reasons: * * 1. Multiplying by 10 is perfect for keys that are decimal strings, * and multiplying by 9 is just about as good. * 2. Times-9 is (shift-left-3) plus (old). This means that each * character's bits hang around in the low-order bits of the * hash value for ever, plus they spread fairly rapidly up to * the high-order bits to fill out the hash value. This seems * works well both for decimal and non-decimal strings. */ result = 0; while (TRUE) { c = *str; str++; if (c == 0) break; result += (result << 3) + c; } return result; } static DBusHashEntry* find_string_function (DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashEntry ***bucket) { DBusHashEntry *entry; unsigned int idx; if (bucket) *bucket = NULL; idx = string_hash (key) & table->mask; /* Search all of the entries in this bucket. */ entry = table->buckets[idx]; while (entry != NULL) { if (strcmp (key, entry->key) == 0) { if (bucket) *bucket = &(table->buckets[idx]); return entry; } entry = entry->next; } if (create_if_not_found) entry = add_entry (table, idx, key, bucket); return entry; } static DBusHashEntry* find_int_function (DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashEntry ***bucket) { DBusHashEntry *entry; unsigned int idx; if (bucket) *bucket = NULL; idx = RANDOM_INDEX (table, key) & table->mask; /* Search all of the entries in this bucket. */ entry = table->buckets[idx]; while (entry != NULL) { if (key == entry->key) { if (bucket) *bucket = &(table->buckets[idx]); return entry; } entry = entry->next; } /* Entry not found. Add a new one to the bucket. */ if (create_if_not_found) entry = add_entry (table, idx, key, bucket); return entry; } static void rebuild_table (DBusHashTable *table) { int old_size; DBusHashEntry **old_buckets; DBusHashEntry **old_chain; DBusHashEntry *entry; /* * Allocate and initialize the new bucket array, and set up * hashing constants for new array size. */ old_size = table->n_buckets; old_buckets = table->buckets; table->n_buckets *= 4; table->buckets = dbus_new0 (DBusHashEntry*, table->n_buckets); if (table->buckets == NULL) { /* out of memory, yay - just don't reallocate, the table will * still work, albeit more slowly. */ table->n_buckets /= 4; table->buckets = old_buckets; return; } table->rebuild_size *= 4; table->down_shift -= 2; table->mask = (table->mask << 2) + 3; /* * Rehash all of the existing entries into the new bucket array. */ for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++) { for (entry = *old_chain; entry != NULL; entry = *old_chain) { unsigned int idx; DBusHashEntry **bucket; *old_chain = entry->next; switch (table->key_type) { case DBUS_HASH_STRING: idx = string_hash (entry->key) & table->mask; break; case DBUS_HASH_INT: idx = RANDOM_INDEX (table, entry->key); break; default: idx = 0; _dbus_assert_not_reached ("Unknown hash table type"); break; } bucket = &(table->buckets[idx]); entry->next = *bucket; *bucket = entry; } } /* Free the old bucket array, if it was dynamically allocated. */ if (old_buckets != table->static_buckets) dbus_free (old_buckets); } /** * Looks up the value for a given string in a hash table * of type #DBUS_HASH_STRING. Returns %NULL if the value * is not present. (A not-present entry is indistinguishable * from an entry with a value of %NULL.) * @param table the hash table. * @param key the string to look up. * @returns the value of the hash entry. */ void* _dbus_hash_table_lookup_string (DBusHashTable *table, const char *key) { DBusHashEntry *entry; _dbus_assert (table->key_type == DBUS_HASH_STRING); entry = (* table->find_function) (table, (char*) key, FALSE, NULL); if (entry) return entry->value; else return NULL; } /** * Looks up the value for a given integer in a hash table * of type #DBUS_HASH_INT. Returns %NULL if the value * is not present. (A not-present entry is indistinguishable * from an entry with a value of %NULL.) * @param table the hash table. * @param key the integer to look up. * @returns the value of the hash entry. */ void* _dbus_hash_table_lookup_int (DBusHashTable *table, int key) { DBusHashEntry *entry; _dbus_assert (table->key_type == DBUS_HASH_INT); entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, NULL); if (entry) return entry->value; else return NULL; } /** * Removes the hash entry for the given key. If no hash entry * for the key exists, does nothing. * * @param table the hash table. * @param key the hash key. * @returns #TRUE if the entry existed */ dbus_bool_t _dbus_hash_table_remove_string (DBusHashTable *table, const char *key) { DBusHashEntry *entry; DBusHashEntry **bucket; _dbus_assert (table->key_type == DBUS_HASH_STRING); entry = (* table->find_function) (table, (char*) key, FALSE, &bucket); if (entry) { remove_entry (table, bucket, entry); return TRUE; } else return FALSE; } /** * Removes the hash entry for the given key. If no hash entry * for the key exists, does nothing. * * @param table the hash table. * @param key the hash key. * @returns #TRUE if the entry existed */ dbus_bool_t _dbus_hash_table_remove_int (DBusHashTable *table, int key) { DBusHashEntry *entry; DBusHashEntry **bucket; _dbus_assert (table->key_type == DBUS_HASH_INT); entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), FALSE, &bucket); if (entry) { remove_entry (table, bucket, entry); return TRUE; } else return FALSE; } /** * Creates a hash entry with the given key and value. * The key and value are not copied; they are stored * in the hash table by reference. If an entry with the * given key already exists, the previous key and value * are overwritten (and freed if the hash table has * a key_free_function and/or value_free_function). * * Returns #FALSE if memory for the new hash entry * can't be allocated. * * @param table the hash table. * @param key the hash entry key. * @param value the hash entry value. */ dbus_bool_t _dbus_hash_table_insert_string (DBusHashTable *table, char *key, void *value) { DBusHashEntry *entry; _dbus_assert (table->key_type == DBUS_HASH_STRING); entry = (* table->find_function) (table, key, TRUE, NULL); if (entry == NULL) return FALSE; /* no memory */ if (table->free_key_function && entry->key != key) (* table->free_key_function) (entry->key); if (table->free_value_function && entry->value != value) (* table->free_value_function) (entry->value); entry->key = key; entry->value = value; return TRUE; } /** * Creates a hash entry with the given key and value. * The key and value are not copied; they are stored * in the hash table by reference. If an entry with the * given key already exists, the previous key and value * are overwritten (and freed if the hash table has * a key_free_function and/or value_free_function). * * Returns #FALSE if memory for the new hash entry * can't be allocated. * * @param table the hash table. * @param key the hash entry key. * @param value the hash entry value. */ dbus_bool_t _dbus_hash_table_insert_int (DBusHashTable *table, int key, void *value) { DBusHashEntry *entry; _dbus_assert (table->key_type == DBUS_HASH_INT); entry = (* table->find_function) (table, _DBUS_INT_TO_POINTER (key), TRUE, NULL); if (entry == NULL) return FALSE; /* no memory */ if (table->free_key_function && entry->key != _DBUS_INT_TO_POINTER (key)) (* table->free_key_function) (entry->key); if (table->free_value_function && entry->value != value) (* table->free_value_function) (entry->value); entry->key = _DBUS_INT_TO_POINTER (key); entry->value = value; return TRUE; } /** * Gets the number of hash entries in a hash table. * * @param table the hash table. * @returns the number of entries in the table. */ int _dbus_hash_table_get_n_entries (DBusHashTable *table) { return table->n_entries; } /** @} */ #ifdef DBUS_BUILD_TESTS #include "dbus-test.h" #include static int count_entries (DBusHashTable *table) { DBusHashIter iter; int count; count = 0; _dbus_hash_iter_init (table, &iter); while (_dbus_hash_iter_next (&iter)) ++count; _dbus_assert (count == _dbus_hash_table_get_n_entries (table)); return count; } /** * @ingroup DBusHashTableInternals * Unit test for DBusHashTable * @returns #TRUE on success. */ dbus_bool_t _dbus_hash_test (void) { int i; DBusHashTable *table1; DBusHashTable *table2; DBusHashIter iter; table1 = _dbus_hash_table_new (DBUS_HASH_STRING, dbus_free, dbus_free); if (table1 == NULL) return FALSE; table2 = _dbus_hash_table_new (DBUS_HASH_INT, NULL, dbus_free); if (table2 == NULL) return FALSE; /* Insert and remove a bunch of stuff, counting the table in between * to be sure it's not broken and that iteration works */ i = 0; while (i < 3000) { char buf[256]; sprintf (buf, "Hash key %d", i); void *value; char *key; key = _dbus_strdup (buf); if (key == NULL) return FALSE; value = _dbus_strdup ("Value!"); if (value == NULL) return FALSE; if (!_dbus_hash_table_insert_string (table1, key, value)) return FALSE; value = _dbus_strdup (buf); if (value == NULL) return FALSE; if (!_dbus_hash_table_insert_int (table2, i, value)) return FALSE; _dbus_assert (count_entries (table1) == i + 1); _dbus_assert (count_entries (table2) == i + 1); value = _dbus_hash_table_lookup_string (table1, buf); _dbus_assert (value != NULL); _dbus_assert (strcmp (value, "Value!") == 0); value = _dbus_hash_table_lookup_int (table2, i); _dbus_assert (value != NULL); _dbus_assert (strcmp (value, buf) == 0); ++i; } --i; while (i >= 0) { char buf[256]; sprintf (buf, "Hash key %d", i); _dbus_hash_table_remove_string (table1, buf); _dbus_hash_table_remove_int (table2, i); _dbus_assert (count_entries (table1) == i); _dbus_assert (count_entries (table2) == i); --i; } _dbus_hash_table_ref (table1); _dbus_hash_table_ref (table2); _dbus_hash_table_unref (table1); _dbus_hash_table_unref (table2); _dbus_hash_table_unref (table1); _dbus_hash_table_unref (table2); /* Insert a bunch of stuff then check * that iteration works correctly (finds the right * values, iter_set_value works, etc.) */ table1 = _dbus_hash_table_new (DBUS_HASH_STRING, dbus_free, dbus_free); if (table1 == NULL) return FALSE; table2 = _dbus_hash_table_new (DBUS_HASH_INT, NULL, dbus_free); if (table2 == NULL) return FALSE; i = 0; while (i < 5000) { char buf[256]; sprintf (buf, "Hash key %d", i); char *key; void *value; key = _dbus_strdup (buf); if (key == NULL) return FALSE; value = _dbus_strdup ("Value!"); if (value == NULL) return FALSE; if (!_dbus_hash_table_insert_string (table1, key, value)) return FALSE; value = _dbus_strdup (buf); if (value == NULL) return FALSE; if (!_dbus_hash_table_insert_int (table2, i, value)) return FALSE; _dbus_assert (count_entries (table1) == i + 1); _dbus_assert (count_entries (table2) == i + 1); ++i; } _dbus_hash_iter_init (table1, &iter); while (_dbus_hash_iter_next (&iter)) { const char *key; void *value; key = _dbus_hash_iter_get_string_key (&iter); value = _dbus_hash_iter_get_value (&iter); _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value); value = _dbus_strdup ("Different value!"); if (value == NULL) return FALSE; _dbus_hash_iter_set_value (&iter, value); _dbus_assert (_dbus_hash_table_lookup_string (table1, key) == value); } _dbus_hash_iter_init (table1, &iter); while (_dbus_hash_iter_next (&iter)) { _dbus_hash_iter_remove_entry (&iter); _dbus_assert (count_entries (table1) == i - 1); --i; } _dbus_hash_iter_init (table2, &iter); while (_dbus_hash_iter_next (&iter)) { int key; void *value; key = _dbus_hash_iter_get_int_key (&iter); value = _dbus_hash_iter_get_value (&iter); _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value); value = _dbus_strdup ("Different value!"); if (value == NULL) return FALSE; _dbus_hash_iter_set_value (&iter, value); _dbus_assert (_dbus_hash_table_lookup_int (table2, key) == value); } i = count_entries (table2); _dbus_hash_iter_init (table2, &iter); while (_dbus_hash_iter_next (&iter)) { _dbus_hash_iter_remove_entry (&iter); _dbus_assert (count_entries (table2) + 1 == i); --i; } _dbus_hash_table_unref (table1); _dbus_hash_table_unref (table2); /* Now do a bunch of things again using _dbus_hash_iter_lookup() to * be sure that interface works. */ table1 = _dbus_hash_table_new (DBUS_HASH_STRING, dbus_free, dbus_free); if (table1 == NULL) return FALSE; table2 = _dbus_hash_table_new (DBUS_HASH_INT, NULL, dbus_free); if (table2 == NULL) return FALSE; i = 0; while (i < 3000) { char buf[256]; sprintf (buf, "Hash key %d", i); void *value; char *key; key = _dbus_strdup (buf); if (key == NULL) return FALSE; value = _dbus_strdup ("Value!"); if (value == NULL) return FALSE; if (!_dbus_hash_iter_lookup (table1, key, TRUE, &iter)) return FALSE; _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL); _dbus_hash_iter_set_value (&iter, value); value = _dbus_strdup (buf); if (value == NULL) return FALSE; if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), TRUE, &iter)) return FALSE; _dbus_assert (_dbus_hash_iter_get_value (&iter) == NULL); _dbus_hash_iter_set_value (&iter, value); _dbus_assert (count_entries (table1) == i + 1); _dbus_assert (count_entries (table2) == i + 1); if (!_dbus_hash_iter_lookup (table1, buf, FALSE, &iter)) return FALSE; value = _dbus_hash_iter_get_value (&iter); _dbus_assert (value != NULL); _dbus_assert (strcmp (value, "Value!") == 0); /* Iterate just to be sure it works, though * it's a stupid thing to do */ while (_dbus_hash_iter_next (&iter)) ; if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter)) return FALSE; value = _dbus_hash_iter_get_value (&iter); _dbus_assert (value != NULL); _dbus_assert (strcmp (value, buf) == 0); /* Iterate just to be sure it works, though * it's a stupid thing to do */ while (_dbus_hash_iter_next (&iter)) ; ++i; } --i; while (i >= 0) { char buf[256]; sprintf (buf, "Hash key %d", i); if (!_dbus_hash_iter_lookup (table1, buf, FALSE, &iter)) _dbus_assert_not_reached ("hash entry should have existed"); _dbus_hash_iter_remove_entry (&iter); if (!_dbus_hash_iter_lookup (table2, _DBUS_INT_TO_POINTER (i), FALSE, &iter)) _dbus_assert_not_reached ("hash entry should have existed"); _dbus_hash_iter_remove_entry (&iter); _dbus_assert (count_entries (table1) == i); _dbus_assert (count_entries (table2) == i); --i; } _dbus_hash_table_unref (table1); _dbus_hash_table_unref (table2); return TRUE; } #endif /* DBUS_BUILD_TESTS */