Skip to content

Commit

Permalink
NoNewGlobals for cbdata_htable (squid-cache#1991)
Browse files Browse the repository at this point in the history
This fix also reduces memory leak false positives
reported by Valgrind.
  • Loading branch information
eduard-bagdasaryan authored and squid-anubis committed Feb 5, 2025
1 parent 3352fa2 commit bbcef1c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/cbdata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ struct CBDataIndex {
int cbdata_types = 0;

#if WITH_VALGRIND
static std::map<const void *, cbdata *> cbdata_htable;
static auto &
CbdataTable()
{
static const auto htable = new std::map<const void *, cbdata *>();
return *htable;
}
#endif

cbdata::~cbdata()
Expand All @@ -106,7 +111,7 @@ cbdata::~cbdata()
cbdata *
cbdata::FromUserData(const void *p) {
#if WITH_VALGRIND
return cbdata_htable.at(p);
return CbdataTable().at(p);
#else
const auto t = static_cast<const char *>(p) - offsetof(cbdata, data);
return reinterpret_cast<cbdata *>(const_cast<char *>(t));
Expand Down Expand Up @@ -154,7 +159,7 @@ cbdataInternalAlloc(cbdata_type type)
c = new cbdata;
p = cbdata_index[type].pool->alloc();
c->data = p;
cbdata_htable.emplace(p,c);
CbdataTable().emplace(p, c);
#else
c = new (cbdata_index[type].pool->alloc()) cbdata;
p = (void *)&c->data;
Expand Down Expand Up @@ -182,7 +187,7 @@ cbdataRealFree(cbdata *c)
debugs(45, 9, "Freeing " << p);

#if WITH_VALGRIND
cbdata_htable.erase(p);
CbdataTable().erase(p);
delete c;
#else
/* This is ugly. But: operator delete doesn't get
Expand Down

0 comments on commit bbcef1c

Please sign in to comment.