]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/sbuf/MemBlob.cc
5020b97099b3facced4b478d2cd6e1c6c7aa856b
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "base/TextException.h"
11 #include "debug/Stream.h"
12 #include "sbuf/MemBlob.h"
13 #include "sbuf/Stats.h"
17 InstanceIdDefinitions(MemBlob
, "blob");
22 MemBlobStats::operator += (const MemBlobStats
& s
)
27 liveBytes
+=s
.liveBytes
;
33 MemBlobStats::dump(std::ostream
&os
) const
36 "MemBlob created: " << alloc
<<
37 "\nMemBlob alive: " << live
<<
38 "\nMemBlob append calls: " << append
<<
39 "\nMemBlob currently allocated size: " << liveBytes
<<
40 "\nlive MemBlob mean current allocation size: " <<
41 (static_cast<double>(liveBytes
)/(live
?live
:1)) << std::endl
;
48 static const auto stats
= new MemBlobStats();
55 return WriteableStats();
60 MemBlob::MemBlob(const MemBlob::size_type reserveSize
) :
61 mem(nullptr), capacity(0), size(0) // will be set by memAlloc
63 debugs(MEMBLOB_DEBUGSECTION
,9, "constructed, this="
64 << static_cast<void*>(this) << " id=" << id
65 << " reserveSize=" << reserveSize
);
66 memAlloc(reserveSize
);
69 MemBlob::MemBlob(const char *buffer
, const MemBlob::size_type bufSize
) :
70 mem(nullptr), capacity(0), size(0) // will be set by memAlloc
72 debugs(MEMBLOB_DEBUGSECTION
,9, "constructed, this="
73 << static_cast<void*>(this) << " id=" << id
74 << " buffer=" << static_cast<const void*>(buffer
)
75 << " bufSize=" << bufSize
);
77 append(buffer
, bufSize
);
83 memFreeBuf(capacity
, mem
);
84 auto &stats
= WriteableStats();
85 stats
.liveBytes
-= capacity
;
87 SBufStats::RecordMemBlobSizeAtDestruct(capacity
);
89 debugs(MEMBLOB_DEBUGSECTION
,9, "destructed, this="
90 << static_cast<void*>(this) << " id=" << id
91 << " capacity=" << capacity
95 /** Allocate an available space area of at least minSize bytes in size.
96 * Must be called by constructors and only by constructors.
99 MemBlob::memAlloc(const size_type minSize
)
101 size_t actualAlloc
= minSize
;
104 mem
= static_cast<char*>(memAllocBuf(actualAlloc
, &actualAlloc
));
107 capacity
= actualAlloc
;
109 debugs(MEMBLOB_DEBUGSECTION
, 8,
110 id
<< " memAlloc: requested=" << minSize
<<
111 ", received=" << capacity
);
112 auto &stats
= WriteableStats();
115 stats
.liveBytes
+= capacity
;
119 MemBlob::appended(const size_type n
)
123 ++WriteableStats().append
;
127 MemBlob::append(const char *source
, const size_type n
)
129 if (n
> 0) { // appending zero bytes is allowed but only affects the stats
132 memmove(mem
+ size
, source
, n
);
135 ++WriteableStats().append
;
139 MemBlob::syncSize(const size_type n
)
141 debugs(MEMBLOB_DEBUGSECTION
, 7, n
<< " was: " << size
);
142 Must(LockCount() <= 1);
148 MemBlob::consume(const size_type rawN
)
151 Must(LockCount() <= 1);
152 const auto n
= std::min(rawN
, size
);
155 memmove(mem
, mem
+ n
, size
);
160 MemBlob::dump(std::ostream
&os
) const
162 os
<< "id @" << (void *)this
163 << "mem:" << static_cast<void*>(mem
)
164 << ",capacity:" << capacity
166 << ",refs:" << LockCount() << "; ";