78 #include "dbus-hash.h" 79 #include "dbus-internals.h" 80 #include "dbus-mempool.h" 104 #define REBUILD_MULTIPLIER 3 122 #define RANDOM_INDEX(table, i) \ 123 (((((intptr_t) (i))*1103515245) >> (table)->down_shift) & (table)->mask) 130 #define DBUS_SMALL_HASH_TABLE 4 237 static unsigned int string_hash (
const char *str);
297 if (entry_pool ==
NULL)
376 while (entry !=
NULL)
380 free_entry (table, entry);
393 while (entry !=
NULL)
395 free_entry_data (table, entry);
452 free_entry_data (table, entry);
466 if (*bucket == entry)
467 *bucket = entry->
next;
473 while (prev->
next != entry)
482 free_entry (table, entry);
687 return (uintptr_t) real->
entry->
key;
754 entry = (* table->
find_function) (table, key, create_if_not_found, &bucket,
NULL);
759 if (create_if_not_found)
804 rebuild_table (table);
816 if (preallocated ==
NULL)
818 entry = alloc_entry (table);
831 add_allocated_entry (table, entry, idx, key, bucket);
840 string_hash (
const char *str)
846 for (p += 1; *p !=
'\0'; p++)
847 h = (h << 5) - h + *p;
871 while (entry !=
NULL)
873 if ((compare_func ==
NULL && key == entry->
key) ||
874 (compare_func !=
NULL && (* compare_func) (key, entry->
key) == 0))
877 *bucket = &(table->
buckets[idx]);
888 if (create_if_not_found)
889 entry = add_entry (table, idx, key, bucket, preallocated);
890 else if (preallocated)
905 idx = string_hash (key) & table->
mask;
907 return find_generic_function (table, key, idx,
924 return find_generic_function (table, key, idx,
925 NULL, create_if_not_found, bucket,
983 table->
mask = (table->
mask << 2) + 3;
995 printf (
"%s table to lo = %d hi = %d downshift = %d mask = 0x%x\n",
996 growing ?
"GROW" :
"SHRINK",
1014 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
1016 for (entry = *old_chain; entry !=
NULL; entry = *old_chain)
1021 *old_chain = entry->
next;
1025 idx = string_hash (entry->
key) & table->
mask;
1037 bucket = &(table->
buckets[idx]);
1038 entry->
next = *bucket;
1069 return entry->
value;
1094 return entry->
value;
1119 return entry->
value;
1145 remove_entry (table, bucket, entry);
1173 remove_entry (table, bucket, entry);
1201 remove_entry (table, bucket, entry);
1233 if (preallocated ==
NULL)
1278 entry->
value = value;
1318 entry->
key = (
void*) key;
1319 entry->
value = value;
1336 entry = alloc_entry (table);
1397 entry->
value = value;
1446 for (i = 0; array[i] !=
NULL; i++)
1453 char *hash_key, *hash_value;
1462 hash_key, hash_value))
1469 if (array[i] !=
NULL)
1517 const char *key, *value;
1543 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 1544 #include "dbus-test.h" 1567 static inline void *
1571 void **_ptr = (
void **) ptr;
1586 _dbus_hash_test (
void)
1593 #define N_HASH_KEYS 5000 1596 char *str_key =
NULL;
1597 char *str_value =
NULL;
1599 keys =
dbus_new (
char *, N_HASH_KEYS);
1603 for (i = 0; i < N_HASH_KEYS; i++)
1607 if (keys[i] ==
NULL)
1611 printf (
"Computing test hash keys...\n");
1613 while (i < N_HASH_KEYS)
1617 len = sprintf (keys[i],
"Hash key %d", i);
1621 printf (
"... done.\n");
1644 const void *out_value;
1647 if (str_key ==
NULL)
1650 if (str_value ==
NULL)
1655 steal (&str_value)))
1659 if (str_value ==
NULL)
1663 i, steal (&str_value)))
1667 if (str_value ==
NULL)
1671 i, steal (&str_value)))
1739 if (str_key ==
NULL)
1742 if (str_value ==
NULL)
1747 steal (&str_value)))
1751 if (str_value ==
NULL)
1755 i, steal (&str_value)))
1776 if (str_value ==
NULL)
1804 if (str_value ==
NULL)
1812 i = count_entries (table2);
1828 if (str_key ==
NULL)
1832 if (str_value ==
NULL)
1837 steal (&str_value)))
1847 if (str_key ==
NULL)
1850 if (str_value ==
NULL)
1858 steal (&str_value)))
1890 const void *out_value;
1893 if (str_key ==
NULL)
1896 if (str_value ==
NULL)
1900 steal (&str_key),
TRUE, &iter))
1906 if (str_value ==
NULL)
1970 for (i = 0; i < N_HASH_KEYS; i++)
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
void * _dbus_hash_table_lookup_uintptr(DBusHashTable *table, uintptr_t key)
Looks up the value for a given integer in a hash table of type DBUS_HASH_UINTPTR. ...
DBusHashEntry *(* DBusFindEntryFunction)(DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashEntry ***bucket, DBusPreallocatedHash *preallocated)
Function used to find and optionally create a hash entry.
struct DBusPreallocatedHash DBusPreallocatedHash
A preallocated hash entry.
#define NULL
A null pointer, defined appropriately for C or C++.
void * _dbus_mem_pool_alloc(DBusMemPool *pool)
Allocates an object from the memory pool.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
DBusPreallocatedHash * _dbus_hash_table_preallocate_entry(DBusHashTable *table)
Preallocate an opaque data blob that allows us to insert into the hash table at a later time without ...
int down_shift
Shift count used in hashing function.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
dbus_bool_t _dbus_hash_table_insert_int(DBusHashTable *table, int key, void *value)
Creates a hash entry with the given key and value.
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
const char * _dbus_hash_iter_get_string_key(DBusHashIter *iter)
Gets the key for the current entry.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_INT_TO_POINTER(integer)
Safely stuffs an integer into a pointer, to be extracted later with _DBUS_POINTER_TO_INT.
dbus_bool_t _dbus_hash_table_insert_uintptr(DBusHashTable *table, uintptr_t key, void *value)
Creates a hash entry with the given key and value.
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero...
DBusHashType key_type
Type of keys used in this table.
int n_buckets
Total number of buckets allocated at **buckets.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
DBusHashTable * _dbus_hash_table_ref(DBusHashTable *table)
Increments the reference count for a hash table.
dbus_bool_t _dbus_hash_iter_next(DBusHashIter *iter)
Move the hash iterator forward one step, to the next hash entry.
dbus_bool_t _dbus_mem_pool_dealloc(DBusMemPool *pool, void *element)
Deallocates an object previously created with _dbus_mem_pool_alloc().
Hash keys are integer capable to hold a pointer.
void _dbus_hash_table_remove_all(DBusHashTable *table)
Removed all entries from a hash table.
dbus_bool_t _dbus_hash_table_from_array(DBusHashTable *table, char **array, char delimiter)
Imports a string array into a hash table The hash table needs to be initialized with string keys...
#define _DBUS_INT_MAX
Maximum value of type "int".
void _dbus_hash_table_insert_string_preallocated(DBusHashTable *table, DBusPreallocatedHash *preallocated, char *key, void *value)
Inserts a string-keyed entry into the hash table, using a preallocated data block from _dbus_hash_tab...
dbus_bool_t _dbus_hash_table_remove_int(DBusHashTable *table, int key)
Removes the hash entry for the given key.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
int _dbus_hash_table_get_n_entries(DBusHashTable *table)
Gets the number of hash entries in a hash table.
void * _dbus_hash_iter_get_value(DBusHashIter *iter)
Gets the value of the current entry.
dbus_bool_t _dbus_hash_iter_lookup(DBusHashTable *table, void *key, dbus_bool_t create_if_not_found, DBusHashIter *iter)
A low-level but efficient interface for manipulating the hash table.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
int next_bucket
index of next bucket
uintptr_t _dbus_hash_iter_get_uintptr_key(DBusHashIter *iter)
Gets the key for the current entry.
dbus_bool_t _dbus_hash_table_insert_string(DBusHashTable *table, char *key, void *value)
Creates a hash entry with the given key and value.
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
DBusHashEntry * entry
Current hash entry.
int(* KeyCompareFunc)(const void *key_a, const void *key_b)
Key comparison function.
void _dbus_hash_iter_remove_entry(DBusHashIter *iter)
Removes the current entry from the hash table.
Internals of DBusHashIter.
void _dbus_mem_pool_free(DBusMemPool *pool)
Frees a memory pool (and all elements allocated from it).
int lo_rebuild_size
Shrink table when n_entries gets below this.
#define _DBUS_POINTER_TO_INT(pointer)
Safely casts a void* to an integer; should only be used on void* that actually contain integers...
Internal representation of a hash entry.
Internals fields of DBusMemPool.
DBusHashEntry * next
Pointer to next entry in this hash bucket, or NULL for end of chain.
int n_entries_on_init
used to detect table resize since initialization
int refcount
Reference count.
DBusHashEntry ** buckets
Pointer to bucket array.
DBusHashTable * table
Pointer to table containing entry.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
DBusHashEntry * static_buckets[DBUS_SMALL_HASH_TABLE]
Bucket array used for small tables (to avoid mallocs and frees).
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
int hi_rebuild_size
Enlarge table when n_entries gets to be this large.
DBusFreeFunction free_value_function
Function to free values.
DBusHashEntry ** bucket
Pointer to bucket that points to first entry in this entry's chain: used for deleting the entry...
void _dbus_hash_iter_set_value(DBusHashIter *iter, void *value)
Sets the value of the current entry.
DBusHashType
Indicates the type of a key in the hash table.
DBusFreeFunction free_key_function
Function to free keys.
dbus_bool_t _dbus_hash_table_remove_string(DBusHashTable *table, const char *key)
Removes the hash entry for the given key.
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
char ** _dbus_hash_table_to_array(DBusHashTable *table, char delimiter)
Creates a string array from a hash table.
void * _dbus_hash_table_lookup_string(DBusHashTable *table, const char *key)
Looks up the value for a given string in a hash table of type DBUS_HASH_STRING.
void * _dbus_hash_table_lookup_int(DBusHashTable *table, int key)
Looks up the value for a given integer in a hash table of type DBUS_HASH_INT.
int _dbus_hash_iter_get_int_key(DBusHashIter *iter)
Gets the key for the current entry.
void _dbus_hash_table_free_preallocated_entry(DBusHashTable *table, DBusPreallocatedHash *preallocated)
Frees an opaque DBusPreallocatedHash that was not used in order to insert into the hash table...
#define FALSE
Expands to "0".
#define DBUS_SMALL_HASH_TABLE
Initial number of buckets in hash table (hash table statically allocates its buckets for this size an...
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Internals of DBusHashTable.
char * _dbus_strdup(const char *str)
Duplicates a string.
DBusHashEntry * next_entry
Next entry to be iterated onto in current bucket.
#define REBUILD_MULTIPLIER
When there are this many entries per bucket, on average, rebuild the hash table to make it larger...
int mask
Mask value used in hashing function.
dbus_bool_t _dbus_hash_table_remove_uintptr(DBusHashTable *table, uintptr_t key)
Removes the hash entry for the given key.
DBusFindEntryFunction find_function
Function for finding entries.
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
dbus_bool_t _dbus_string_split_on_byte(DBusString *source, unsigned char byte, DBusString *tail)
Looks for the first occurance of a byte, deletes that byte, and moves everything after the byte to th...
DBusMemPool * _dbus_mem_pool_new(int element_size, dbus_bool_t zero_elements)
Creates a new memory pool, or returns NULL on failure.
#define RANDOM_INDEX(table, i)
Takes a preliminary integer hash value and produces an index into a hash tables bucket list...
int n_entries
Total number of entries present in table.
DBusMemPool * entry_pool
Memory pool for hash entries.