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_ENABLE_EMBEDDED_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++)