Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ht->table = &ht->perfect_bit;
}
ht->table = &ht->perfect_bit;
}
-/* We've changed ht->bits, update ht->max and ht->max_with_deleted */
-static void htable_adjust_capacity(struct htable *ht)
+static inline size_t ht_max(const struct htable *ht)
- ht->max = ((size_t)3 << ht->bits) / 4;
- ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
+ return ((size_t)3 << ht->bits) / 4;
+}
+
+static inline size_t ht_max_with_deleted(const struct htable *ht)
+{
+ return ((size_t)9 << ht->bits) / 10;
}
bool htable_init_sized(struct htable *ht,
}
bool htable_init_sized(struct htable *ht,
ht->table = &ht->perfect_bit;
return false;
}
ht->table = &ht->perfect_bit;
return false;
}
- htable_adjust_capacity(ht);
(void)htable_debug(ht, HTABLE_LOC);
return true;
}
(void)htable_debug(ht, HTABLE_LOC);
return true;
}
return false;
}
ht->bits++;
return false;
}
ht->bits++;
- htable_adjust_capacity(ht);
/* If we lost our "perfect bit", get it back now. */
if (!ht->perfect_bit && ht->common_mask) {
/* If we lost our "perfect bit", get it back now. */
if (!ht->perfect_bit && ht->common_mask) {
bool htable_add_(struct htable *ht, size_t hash, const void *p)
{
bool htable_add_(struct htable *ht, size_t hash, const void *p)
{
- if (ht->elems+1 > ht->max && !double_table(ht))
+ if (ht->elems+1 > ht_max(ht) && !double_table(ht))
- if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
+ if (ht->elems+1 + ht->deleted > ht_max_with_deleted(ht))
rehash_table(ht);
assert(p);
if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
rehash_table(ht);
assert(p);
if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
size_t (*rehash)(const void *elem, void *priv);
void *priv;
unsigned int bits;
size_t (*rehash)(const void *elem, void *priv);
void *priv;
unsigned int bits;
- size_t elems, deleted, max, max_with_deleted;
/* These are the bits which are the same in all pointers. */
uintptr_t common_mask, common_bits;
uintptr_t perfect_bit;
/* These are the bits which are the same in all pointers. */
uintptr_t common_mask, common_bits;
uintptr_t perfect_bit;
* static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
*/
#define HTABLE_INITIALIZER(name, rehash, priv) \
* static struct htable ht = HTABLE_INITIALIZER(ht, rehash, NULL);
*/
#define HTABLE_INITIALIZER(name, rehash, priv) \
- { rehash, priv, 0, 0, 0, 0, 0, -1, 0, 0, &name.perfect_bit }
+ { rehash, priv, 0, 0, 0, -1, 0, 0, &name.perfect_bit }
/**
* htable_init - initialize an empty hash table.
/**
* htable_init - initialize an empty hash table.
htable_init(&ht, hash, NULL);
for (i = 0; i < NUM_VALS; i++) {
htable_init(&ht, hash, NULL);
for (i = 0; i < NUM_VALS; i++) {
- ok1(ht.max >= i);
- ok1(ht.max <= i * 2);
+ ok1(ht_max(&ht) >= i);
+ ok1(ht_max(&ht) <= i * 2);
htable_add(&ht, hash(&val[i], NULL), &val[i]);
}
htable_add(&ht, hash(&val[i], NULL), &val[i]);
}
dne = i;
htable_init(&ht, hash, NULL);
dne = i;
htable_init(&ht, hash, NULL);
ok1(ht.bits == 0);
/* We cannot find an entry which doesn't exist. */
ok1(ht.bits == 0);
/* We cannot find an entry which doesn't exist. */
/* This should increase it once. */
add_vals(&ht, val, 0, 1);
ok1(ht.bits == 1);
/* This should increase it once. */
add_vals(&ht, val, 0, 1);
ok1(ht.bits == 1);
weight = 0;
for (i = 0; i < sizeof(ht.common_mask) * CHAR_BIT; i++) {
if (ht.common_mask & ((uintptr_t)1 << i)) {
weight = 0;
for (i = 0; i < sizeof(ht.common_mask) * CHAR_BIT; i++) {
if (ht.common_mask & ((uintptr_t)1 << i)) {
/* This should increase it again. */
add_vals(&ht, val, 1, 1);
ok1(ht.bits == 2);
/* This should increase it again. */
add_vals(&ht, val, 1, 1);
ok1(ht.bits == 2);
/* Mask should be set. */
ok1(ht.common_mask != 0);
/* Mask should be set. */
ok1(ht.common_mask != 0);
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1024));
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1024));
+ ok1(ht_max(&ht) >= 1024);
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1023));
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1023));
+ ok1(ht_max(&ht) >= 1023);
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1025));
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1025));
+ ok1(ht_max(&ht) >= 1025);
htable_clear(&ht);
return exit_status();
htable_clear(&ht);
return exit_status();
htable_init(&ht, hash, NULL);
for (i = 0; i < NUM_VALS; i++) {
htable_init(&ht, hash, NULL);
for (i = 0; i < NUM_VALS; i++) {
- ok1(ht.max >= i);
- ok1(ht.max <= i * 2);
+ ok1(ht_max(&ht) >= i);
+ ok1(ht_max(&ht) <= i * 2);
htable_add(&ht, hash(&val[i], NULL), &val[i]);
}
htable_clear(&ht);
htable_add(&ht, hash(&val[i], NULL), &val[i]);
}
htable_clear(&ht);
dne = i;
htable_obj_init(&ht);
dne = i;
htable_obj_init(&ht);
+ ok1(ht_max(&ht.raw) == 0);
ok1(ht.raw.bits == 0);
/* We cannot find an entry which doesn't exist. */
ok1(ht.raw.bits == 0);
/* We cannot find an entry which doesn't exist. */
/* Fill it, it should increase in size. */
add_vals(&ht, val, NUM_VALS);
ok1(ht.raw.bits == NUM_BITS + 1);
/* Fill it, it should increase in size. */
add_vals(&ht, val, NUM_VALS);
ok1(ht.raw.bits == NUM_BITS + 1);
- ok1(ht.raw.max < (1 << ht.raw.bits));
+ ok1(ht_max(&ht.raw) < (1 << ht.raw.bits));
/* Mask should be set. */
ok1(ht.raw.common_mask != 0);
/* Mask should be set. */
ok1(ht.raw.common_mask != 0);
dne = i;
htable_obj_init(&ht);
dne = i;
htable_obj_init(&ht);
+ ok1(ht_max(&ht.raw) == 0);
ok1(ht.raw.bits == 0);
/* We cannot find an entry which doesn't exist. */
ok1(ht.raw.bits == 0);
/* We cannot find an entry which doesn't exist. */
/* Fill it, it should increase in size. */
add_vals(&ht, val, NUM_VALS);
ok1(ht.raw.bits == NUM_BITS + 1);
/* Fill it, it should increase in size. */
add_vals(&ht, val, NUM_VALS);
ok1(ht.raw.bits == NUM_BITS + 1);
- ok1(ht.raw.max < (1 << ht.raw.bits));
+ ok1(ht_max(&ht.raw) < (1 << ht.raw.bits));
/* Mask should be set. */
ok1(ht.raw.common_mask != 0);
/* Mask should be set. */
ok1(ht.raw.common_mask != 0);
dne = i;
htable_init(&ht, hash, NULL);
dne = i;
htable_init(&ht, hash, NULL);
ok1(ht.bits == 0);
/* We cannot find an entry which doesn't exist. */
ok1(ht.bits == 0);
/* We cannot find an entry which doesn't exist. */
/* This should increase it once. */
add_vals(&ht, val, 0, 1);
ok1(ht.bits == 1);
/* This should increase it once. */
add_vals(&ht, val, 0, 1);
ok1(ht.bits == 1);
weight = 0;
for (i = 0; i < sizeof(ht.common_mask) * CHAR_BIT; i++) {
if (ht.common_mask & ((uintptr_t)1 << i)) {
weight = 0;
for (i = 0; i < sizeof(ht.common_mask) * CHAR_BIT; i++) {
if (ht.common_mask & ((uintptr_t)1 << i)) {
/* This should increase it again. */
add_vals(&ht, val, 1, 1);
ok1(ht.bits == 2);
/* This should increase it again. */
add_vals(&ht, val, 1, 1);
ok1(ht.bits == 2);
/* Mask should be set. */
ok1(ht.common_mask != 0);
/* Mask should be set. */
ok1(ht.common_mask != 0);
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1024));
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1024));
+ ok1(ht_max(&ht) >= 1024);
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1023));
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1023));
+ ok1(ht_max(&ht) >= 1023);
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1025));
htable_clear(&ht);
ok1(htable_init_sized(&ht, hash, NULL, 1025));
+ ok1(ht_max(&ht) >= 1025);
htable_clear(&ht);
return exit_status();
htable_clear(&ht);
return exit_status();