Skip to content

Commit

Permalink
Do not use std::aligned_alloc
Browse files Browse the repository at this point in the history
Fixes #1152
  • Loading branch information
rui314 committed Nov 22, 2023
1 parent b3984c9 commit 8415252
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ class ConcurrentMap {
}

~ConcurrentMap() {
free(entries);
free(entries_buf);
}

// In order to avoid unnecessary cache-line false sharing, we want
Expand All @@ -579,19 +579,15 @@ class ConcurrentMap {

void resize(i64 nbuckets) {
this->nbuckets = std::max<i64>(MIN_NBUCKETS, bit_ceil(nbuckets));
free(entries_buf);

i64 sz = sizeof(Entry) * this->nbuckets;
free(entries);

#if _WIN32
// Even though std::aligned_alloc is defined in C++17, MSVC doesn't
// seem to provide that function.
entries = (Entry *)_aligned_malloc(sz, alignof(Entry));
#else
entries = (Entry *)std::aligned_alloc(alignof(Entry), sz);
#endif

memset(entries, 0, sz);
// seem to provide that function. C11's aligned_alloc may not be always
// avialalbe. Therefore, we'll align the buffer ourselves.
i64 size = sizeof(Entry) * this->nbuckets;
entries_buf = malloc(size + alignof(Entry) - 1);
entries = (Entry *)align_to((u64)entries_buf, alignof(Entry));
memset(entries, 0, size);
}

std::pair<T *, bool> insert(std::string_view key, u64 hash, const T &val) {
Expand Down Expand Up @@ -699,6 +695,7 @@ class ConcurrentMap {
static constexpr i64 NUM_SHARDS = 16;
static constexpr i64 MAX_RETRY = 128;

void *entries_buf = nullptr;
Entry *entries = nullptr;
i64 nbuckets = 0;

Expand Down

0 comments on commit 8415252

Please sign in to comment.