Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMemoCache refactor + double assign race condition fix #321

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/encoding.hpp
Expand Up @@ -35,14 +35,13 @@ class TMemoCache {
public:
~TMemoCache()
{
std::lock_guard<std::mutex> l(memoMutex);
// Clean up global entries on destruction
std::map<double, FSE_CTable *>::iterator itc;
for (itc = CT_MEMO.begin(); itc != CT_MEMO.end(); itc++) {
FSE_freeCTable(itc->second);
for (const auto& entry : CT_MEMO) {
FSE_freeCTable(entry.second);
}
std::map<double, FSE_DTable *>::iterator itd;
for (itd = DT_MEMO.begin(); itd != DT_MEMO.end(); itd++) {
FSE_freeDTable(itd->second);
for (const auto& entry : DT_MEMO) {
FSE_freeDTable(entry.second);
}
}

Expand All @@ -61,13 +60,21 @@ class TMemoCache {
void CTAssign(double R, FSE_CTable *ct)
{
std::lock_guard<std::mutex> l(memoMutex);
CT_MEMO[R] = ct;
auto& table = CT_MEMO[R];
if(table) {
FSE_freeCTable(table);
}
table = ct;
}

void DTAssign(double R, FSE_DTable *dt)
{
std::lock_guard<std::mutex> l(memoMutex);
DT_MEMO[R] = dt;
auto& table = DT_MEMO[R];
if(table) {
FSE_freeDTable(table);
}
table = dt;
}

FSE_CTable *CTGet(double R)
Expand Down