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;
753 entry = (* table->
find_function) (table, key, create_if_not_found, &bucket,
NULL);
795 rebuild_table (table);
807 if (preallocated ==
NULL)
809 entry = alloc_entry (table);
822 add_allocated_entry (table, entry, idx, key, bucket);
831 string_hash (
const char *str)
837 for (p += 1; *p !=
'\0'; p++)
838 h = (h << 5) - h + *p;
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]);
879 if (create_if_not_found)
880 entry = add_entry (table, idx, key, bucket, preallocated);
881 else if (preallocated)
896 idx = string_hash (key) & table->
mask;
898 return find_generic_function (table, key, idx,
915 return find_generic_function (table, key, idx,
916 NULL, create_if_not_found, bucket,
974 table->
mask = (table->
mask << 2) + 3;
986 printf (
"%s table to lo = %d hi = %d downshift = %d mask = 0x%x\n",
987 growing ?
"GROW" :
"SHRINK",
1004 for (old_chain = old_buckets; old_size > 0; old_size--, old_chain++)
1006 for (entry = *old_chain; entry !=
NULL; entry = *old_chain)
1011 *old_chain = entry->
next;
1015 idx = string_hash (entry->
key) & table->
mask;
1027 bucket = &(table->
buckets[idx]);
1028 entry->
next = *bucket;
1059 return entry->
value;
1084 return entry->
value;
1109 return entry->
value;
1135 remove_entry (table, bucket, entry);
1163 remove_entry (table, bucket, entry);
1191 remove_entry (table, bucket, entry);
1223 if (preallocated ==
NULL)
1268 entry->
value = value;
1308 entry->
key = (
void*) key;
1309 entry->
value = value;
1326 entry = alloc_entry (table);
1387 entry->
value = value;
1404 #ifdef DBUS_BUILD_TESTS
1405 #include "dbus-test.h"
1434 _dbus_hash_test (
void)
1441 #define N_HASH_KEYS 5000
1445 keys =
dbus_new (
char *, N_HASH_KEYS);
1449 for (i = 0; i < N_HASH_KEYS; i++)
1453 if (keys[i] ==
NULL)
1457 printf (
"Computing test hash keys...\n");
1459 while (i < N_HASH_KEYS)
1463 len = sprintf (keys[i],
"Hash key %d", i);
1467 printf (
"... done.\n");
1660 i = count_entries (table2);
1823 for (i = 0; i < N_HASH_KEYS; i++)
int _dbus_hash_table_get_n_entries(DBusHashTable *table)
Gets the number of hash entries in a hash table.
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().
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
#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.
DBusHashType key_type
Type of keys used in this table.
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...
int n_buckets
Total number of buckets allocated at **buckets.
DBusHashTable * _dbus_hash_table_ref(DBusHashTable *table)
Increments the reference count for a hash table.
dbus_bool_t _dbus_hash_table_remove_int(DBusHashTable *table, int key)
Removes the hash entry for the given key.
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero...
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.
#define _DBUS_INT_MAX
Maximum value of type "int".
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_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().
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
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
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 * entry
Current hash entry.
uintptr_t _dbus_hash_iter_get_uintptr_key(DBusHashIter *iter)
Gets the key for the current entry.
int(* KeyCompareFunc)(const void *key_a, const void *key_b)
Key comparison function.
dbus_bool_t _dbus_hash_iter_next(DBusHashIter *iter)
Move the hash iterator forward one step, to the next hash entry.
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.
dbus_bool_t _dbus_hash_table_insert_string(DBusHashTable *table, char *key, void *value)
Creates a hash entry with the given key and value.
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().
dbus_bool_t _dbus_hash_table_insert_int(DBusHashTable *table, int key, void *value)
Creates a hash entry with the given key and value.
DBusHashEntry * static_buckets[DBUS_SMALL_HASH_TABLE]
Bucket array used for small tables (to avoid mallocs and frees).
#define TRUE
Expands to "1".
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.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_hash_table_remove_string(DBusHashTable *table, const char *key)
Removes the hash entry for the given key.
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_remove_entry(DBusHashIter *iter)
Removes the current entry from the hash table.
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.
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
void * _dbus_hash_iter_get_value(DBusHashIter *iter)
Gets the value of the current entry.
#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...
const char * _dbus_hash_iter_get_string_key(DBusHashIter *iter)
Gets the key for the current entry.
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...
Internals of DBusHashTable.
char * _dbus_strdup(const char *str)
Duplicates a string.
DBusHashEntry * next_entry
Next entry to be iterated onto in current bucket.
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.
#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.
DBusFindEntryFunction find_function
Function for finding entries.
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.
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.