From bbcef1c5cc499808dd7836c7fa9a721f8244ea11 Mon Sep 17 00:00:00 2001 From: Eduard Bagdasaryan Date: Wed, 5 Feb 2025 12:58:59 +0000 Subject: [PATCH] NoNewGlobals for cbdata_htable (#1991) This fix also reduces memory leak false positives reported by Valgrind. --- src/cbdata.cc | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cbdata.cc b/src/cbdata.cc index 58ade16eeb3..3fa44b319c3 100644 --- a/src/cbdata.cc +++ b/src/cbdata.cc @@ -90,7 +90,12 @@ struct CBDataIndex { int cbdata_types = 0; #if WITH_VALGRIND -static std::map cbdata_htable; +static auto & +CbdataTable() +{ + static const auto htable = new std::map(); + return *htable; +} #endif cbdata::~cbdata() @@ -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(p) - offsetof(cbdata, data); return reinterpret_cast(const_cast(t)); @@ -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; @@ -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