]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
NoNewGlobals for cbdata_htable (#1991)
authorEduard Bagdasaryan <eduard.bagdasaryan@measurement-factory.com>
Wed, 5 Feb 2025 12:58:59 +0000 (12:58 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Wed, 5 Feb 2025 13:52:00 +0000 (13:52 +0000)
This fix also reduces memory leak false positives
reported by Valgrind.

src/cbdata.cc

index 58ade16eeb31b0e6a7936d63fc132ee5193d8038..3fa44b319c35324100ff44ce6f20162a9e523238 100644 (file)
@@ -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()
@@ -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));
@@ -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