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++)
 
  
DBusFreeFunction free_value_function
Function to free values.
int _dbus_hash_table_get_n_entries(DBusHashTable *table)
Gets the number of hash entries in a hash table.
#define _DBUS_N_ELEMENTS(array)
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.
char ** _dbus_hash_table_to_array(DBusHashTable *table, char delimiter)
Creates a string array from a hash table.
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero.
DBusHashEntry * next_entry
Next entry to be iterated onto in current bucket.
DBusMemPool * _dbus_mem_pool_new(int element_size, dbus_bool_t zero_elements)
Creates a new memory pool, or returns NULL on failure.
dbus_bool_t _dbus_hash_table_remove_int(DBusHashTable *table, int key)
Removes the hash entry for the given key.
int n_buckets
Total number of buckets allocated at **buckets.
#define REBUILD_MULTIPLIER
When there are this many entries per bucket, on average, rebuild the hash table to make it larger.
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).
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.
@ DBUS_HASH_INT
Hash keys are integers.
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.
DBusHashEntry * next
Pointer to next entry in this hash bucket, or NULL for end of chain.
void(* DBusFreeFunction)(void *memory)
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.
Internals fields of DBusMemPool.
uintptr_t _dbus_hash_iter_get_uintptr_key(DBusHashIter *iter)
Gets the key for the current entry.
int mask
Mask value used in hashing function.
DBusHashEntry ** buckets
Pointer to bucket array.
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 ...
void _dbus_hash_iter_set_value(DBusHashIter *iter, void *value)
Sets the value of the current entry.
void _dbus_hash_iter_remove_entry(DBusHashIter *iter)
Removes the current entry from the 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...
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
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.
DBusHashEntry * entry
Current hash entry.
@ DBUS_HASH_STRING
Hash keys are strings.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
int n_entries_on_init
used to detect table resize since initialization
#define RANDOM_INDEX(table, i)
Takes a preliminary integer hash value and produces an index into a hash tables bucket list.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
#define _DBUS_INT_TO_POINTER(integer)
Internal representation of a hash entry.
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_hash_table_insert_int(DBusHashTable *table, int 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.
#define DBUS_SMALL_HASH_TABLE
Initial number of buckets in hash table (hash table statically allocates its buckets for this size an...
DBusFreeFunction free_key_function
Function to free keys.
DBusMemPool * entry_pool
Memory pool for hash entries.
int n_entries
Total number of entries present in table.
int _dbus_hash_iter_get_int_key(DBusHashIter *iter)
Gets the key for the current entry.
void _dbus_hash_table_remove_all(DBusHashTable *table)
Removed all entries from a hash table.
dbus_bool_t _dbus_mem_pool_dealloc(DBusMemPool *pool, void *element)
Deallocates an object previously created with _dbus_mem_pool_alloc().
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.
int next_bucket
index of next bucket
void _dbus_mem_pool_free(DBusMemPool *pool)
Frees a memory pool (and all elements allocated from it).
@ DBUS_HASH_UINTPTR
Hash keys are integer capable to hold a pointer.
int down_shift
Shift count used in hashing function.
Internals of DBusHashTable.
#define _dbus_assert_not_reached(explanation)
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Internals of DBusHashIter.
DBusHashType key_type
Type of keys used in this table.
#define dbus_new(type, count)
#define _dbus_assert(condition)
#define _DBUS_POINTER_TO_INT(pointer)
char * _dbus_strdup(const char *str)
Duplicates a string.
DBusHashTable * table
Pointer to table containing entry.
const char * _dbus_hash_iter_get_string_key(DBusHashIter *iter)
Gets the key for the current entry.
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,...
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_bool_t _dbus_hash_table_remove_uintptr(DBusHashTable *table, uintptr_t key)
Removes the hash entry for the given key.
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...
int(* KeyCompareFunc)(const void *key_a, const void *key_b)
Key comparison function.
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
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.
int refcount
Reference count.
DBusHashEntry ** bucket
Pointer to bucket that points to first entry in this entry's chain: used for deleting the entry.
void * _dbus_mem_pool_alloc(DBusMemPool *pool)
Allocates an object from the memory pool.
struct DBusPreallocatedHash DBusPreallocatedHash
A preallocated hash entry.
int lo_rebuild_size
Shrink table when n_entries gets below this.
#define dbus_new0(type, count)
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
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(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
int hi_rebuild_size
Enlarge table when n_entries gets to be this large.
DBusHashTable * _dbus_hash_table_ref(DBusHashTable *table)
Increments the reference count for a hash table.
dbus_bool_t _dbus_hash_table_remove_string(DBusHashTable *table, const char *key)
Removes the hash entry for the given key.
dbus_uint32_t dbus_bool_t