]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
NoNewGlobals for MemBlob::Stats (#1749)
authorFrancesco Chemolli <5175948+kinkie@users.noreply.github.com>
Sun, 7 Apr 2024 16:51:02 +0000 (16:51 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Mon, 8 Apr 2024 05:42:47 +0000 (05:42 +0000)
Detected by Coverity. CID 1554656: Initialization or destruction
ordering is unspecified (GLOBAL_INIT_ORDER).

Also update MemBlobStats initialization.

src/sbuf/MemBlob.cc
src/sbuf/MemBlob.h

index f611ff9dbf9b8343d206a79fa68b13d6ab6a3e36..3bb1af36b7b9e2264d6c4bbe222f656db5a8274b 100644 (file)
 
 #include <iostream>
 
-MemBlobStats MemBlob::Stats;
 InstanceIdDefinitions(MemBlob, "blob");
 
 /* MemBlobStats */
 
-MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0), liveBytes(0)
-{}
-
 MemBlobStats&
 MemBlobStats::operator += (const MemBlobStats& s)
 {
@@ -46,6 +42,19 @@ MemBlobStats::dump(std::ostream &os) const
     return os;
 }
 
+static auto &
+WriteableStats()
+{
+    static const auto stats = new MemBlobStats();
+    return *stats;
+}
+
+const MemBlobStats &
+MemBlob::GetStats()
+{
+    return WriteableStats();
+}
+
 /* MemBlob */
 
 MemBlob::MemBlob(const MemBlob::size_type reserveSize) :
@@ -72,8 +81,9 @@ MemBlob::~MemBlob()
 {
     if (mem || capacity)
         memFreeBuf(capacity, mem);
-    Stats.liveBytes -= capacity;
-    --Stats.live;
+    auto &stats = WriteableStats();
+    stats.liveBytes -= capacity;
+    --stats.live;
     SBufStats::RecordMemBlobSizeAtDestruct(capacity);
 
     debugs(MEMBLOB_DEBUGSECTION,9, "destructed, this="
@@ -99,9 +109,10 @@ MemBlob::memAlloc(const size_type minSize)
     debugs(MEMBLOB_DEBUGSECTION, 8,
            id << " memAlloc: requested=" << minSize <<
            ", received=" << capacity);
-    ++Stats.live;
-    ++Stats.alloc;
-    Stats.liveBytes += capacity;
+    auto &stats = WriteableStats();
+    ++stats.live;
+    ++stats.alloc;
+    stats.liveBytes += capacity;
 }
 
 void
@@ -109,7 +120,7 @@ MemBlob::appended(const size_type n)
 {
     Must(willFit(n));
     size += n;
-    ++Stats.append;
+    ++WriteableStats().append;
 }
 
 void
@@ -121,7 +132,7 @@ MemBlob::append(const char *source, const size_type n)
         memmove(mem + size, source, n);
         size += n;
     }
-    ++Stats.append;
+    ++WriteableStats().append;
 }
 
 void
@@ -145,12 +156,6 @@ MemBlob::consume(const size_type rawN)
     }
 }
 
-const MemBlobStats&
-MemBlob::GetStats()
-{
-    return Stats;
-}
-
 std::ostream&
 MemBlob::dump(std::ostream &os) const
 {
index 1932118f15ba2cb020929cad51df32b39cf9770d..e9304b9b5b9e91c09d0ba27b7f7669ab61f0e101 100644 (file)
 class MemBlobStats
 {
 public:
-    MemBlobStats();
-
     /// dumps class-wide statistics
     std::ostream& dump(std::ostream& os) const;
 
     MemBlobStats& operator += (const MemBlobStats&);
 
 public:
-    uint64_t alloc;     ///< number of MemBlob instances created so far
-    uint64_t live;      ///< number of MemBlob instances currently alive
-    uint64_t append;    ///< number of MemBlob::append() calls
-    uint64_t liveBytes; ///< the total size of currently allocated storage
+    uint64_t alloc = 0;     ///< number of MemBlob instances created so far
+    uint64_t live = 0;      ///< number of MemBlob instances currently alive
+    uint64_t append = 0;    ///< number of MemBlob::append() calls
+    uint64_t liveBytes = 0; ///< the total size of currently allocated storage
 };
 
 /** Refcountable, fixed-size, content-agnostic memory buffer.
@@ -115,8 +113,6 @@ public:
     const InstanceId<MemBlob> id; ///< blob identifier
 
 private:
-    static MemBlobStats Stats; ///< class-wide statistics
-
     void memAlloc(const size_type memSize);
 
     /// whether the offset points to the end of the used area