]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBlob.cc
2 * Copyright (C) 1996-2014 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"
13 #include "SBufDetailedStats.h"
17 MemBlobStats
MemBlob::Stats
;
18 InstanceIdDefinitions(MemBlob
, "blob");
22 MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0), liveBytes(0)
26 MemBlobStats::operator += (const MemBlobStats
& s
)
31 liveBytes
+=s
.liveBytes
;
37 MemBlobStats::dump(std::ostream
&os
) const
40 "MemBlob created: " << alloc
<<
41 "\nMemBlob alive: " << live
<<
42 "\nMemBlob append calls: " << append
<<
43 "\nMemBlob currently allocated size: " << liveBytes
<<
44 "\nlive MemBlob mean current allocation size: " <<
45 (static_cast<double>(liveBytes
)/(live
?live
:1)) << std::endl
;
51 MemBlob::MemBlob(const MemBlob::size_type reserveSize
) :
52 mem(NULL
), capacity(0), size(0) // will be set by memAlloc
54 debugs(MEMBLOB_DEBUGSECTION
,9, HERE
<< "constructed, this="
55 << static_cast<void*>(this) << " id=" << id
56 << " reserveSize=" << reserveSize
);
57 memAlloc(reserveSize
);
60 MemBlob::MemBlob(const char *buffer
, const MemBlob::size_type bufSize
) :
61 mem(NULL
), capacity(0), size(0) // will be set by memAlloc
63 debugs(MEMBLOB_DEBUGSECTION
,9, HERE
<< "constructed, this="
64 << static_cast<void*>(this) << " id=" << id
65 << " buffer=" << static_cast<const void*>(buffer
)
66 << " bufSize=" << bufSize
);
68 append(buffer
, bufSize
);
74 memFreeString(capacity
,mem
);
75 Stats
.liveBytes
-= capacity
;
77 recordMemBlobSizeAtDestruct(capacity
);
79 debugs(MEMBLOB_DEBUGSECTION
,9, HERE
<< "destructed, this="
80 << static_cast<void*>(this) << " id=" << id
81 << " capacity=" << capacity
85 /** Allocate an available space area of at least minSize bytes in size.
86 * Must be called by constructors and only by constructors.
89 MemBlob::memAlloc(const size_type minSize
)
91 size_t actualAlloc
= minSize
;
94 mem
= static_cast<char*>(memAllocString(actualAlloc
, &actualAlloc
));
97 capacity
= actualAlloc
;
99 debugs(MEMBLOB_DEBUGSECTION
, 8,
100 id
<< " memAlloc: requested=" << minSize
<<
101 ", received=" << capacity
);
104 Stats
.liveBytes
+= capacity
;
108 MemBlob::appended(const size_type n
)
116 MemBlob::append(const char *source
, const size_type n
)
118 if (n
> 0) { // appending zero bytes is allowed but only affects the stats
121 memmove(mem
+ size
, source
, n
);
134 MemBlob::dump(std::ostream
&os
) const
136 os
<< "id @" << (void *)this
137 << "mem:" << static_cast<void*>(mem
)
138 << ",capacity:" << capacity
140 << ",refs:" << LockCount() << "; ";