Skip to content

Commit

Permalink
fix: allocate default table
Browse files Browse the repository at this point in the history
Avoids problems with dynamic library situations:
skarupke/flat_hash_map#26
  • Loading branch information
william-silversmith committed May 25, 2022
1 parent 36deed6 commit d30a152
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions ska_flat_hash_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,6 @@ struct sherwood_v3_entry
~sherwood_v3_entry()
{
}
static sherwood_v3_entry * empty_default_table()
{
static sherwood_v3_entry result[min_lookups] = { {}, {}, {}, {special_end_value} };
return result;
}

bool has_value() const
{
Expand Down Expand Up @@ -849,13 +844,23 @@ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal
}

private:
EntryPointer entries = Entry::empty_default_table();
EntryPointer entries = empty_default_table();
size_t num_slots_minus_one = 0;
typename HashPolicySelector<ArgumentHash>::type hash_policy;
int8_t max_lookups = detailv3::min_lookups - 1;
float _max_load_factor = 0.5f;
size_t num_elements = 0;

EntryPointer empty_default_table()
{
EntryPointer result = AllocatorTraits::allocate(*this, detailv3::min_lookups);
EntryPointer special_end_item = result + static_cast<ptrdiff_t>(detailv3::min_lookups - 1);
for (EntryPointer it = result; it != special_end_item; ++it)
it->distance_from_desired = -1;
special_end_item->distance_from_desired = Entry::special_end_value;
return result;
}

static int8_t compute_max_lookups(size_t num_buckets)
{
int8_t desired = detailv3::log2(num_buckets);
Expand Down Expand Up @@ -935,16 +940,13 @@ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal

void deallocate_data(EntryPointer begin, size_t num_slots_minus_one, int8_t max_lookups)
{
if (begin != Entry::empty_default_table())
{
AllocatorTraits::deallocate(*this, begin, num_slots_minus_one + max_lookups + 1);
}
AllocatorTraits::deallocate(*this, begin, num_slots_minus_one + max_lookups + 1);
}

void reset_to_empty_state()
{
deallocate_data(entries, num_slots_minus_one, max_lookups);
entries = Entry::empty_default_table();
entries = empty_default_table();
num_slots_minus_one = 0;
hash_policy.reset();
max_lookups = detailv3::min_lookups - 1;
Expand Down

0 comments on commit d30a152

Please sign in to comment.