From: Francesco Chemolli <5175948+kinkie@users.noreply.github.com> Date: Sun, 7 Apr 2024 16:51:02 +0000 (+0000) Subject: NoNewGlobals for MemBlob::Stats (#1749) X-Git-Tag: SQUID_7_0_1~148 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9e6c696c80946964c7b6fb3aee38d253493cb603;p=thirdparty%2Fsquid.git NoNewGlobals for MemBlob::Stats (#1749) Detected by Coverity. CID 1554656: Initialization or destruction ordering is unspecified (GLOBAL_INIT_ORDER). Also update MemBlobStats initialization. --- diff --git a/src/sbuf/MemBlob.cc b/src/sbuf/MemBlob.cc index f611ff9dbf..3bb1af36b7 100644 --- a/src/sbuf/MemBlob.cc +++ b/src/sbuf/MemBlob.cc @@ -14,14 +14,10 @@ #include -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 { diff --git a/src/sbuf/MemBlob.h b/src/sbuf/MemBlob.h index 1932118f15..e9304b9b5b 100644 --- a/src/sbuf/MemBlob.h +++ b/src/sbuf/MemBlob.h @@ -19,18 +19,16 @@ 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 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