]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBlob.cc
2 * MemBlob.cc (C) 2009 Francesco Chemolli <kinkie@squid-cache.org>
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 #include "base/TextException.h"
44 MemBlobStats
MemBlob::Stats
;
45 InstanceIdDefinitions(MemBlob
, "blob");
50 MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0)
54 MemBlobStats::dump(std::ostream
&os
) const
57 "MemBlob created: " << alloc
<<
58 "\nMemBlob alive: " << live
<<
59 "\nMemBlob append calls: " << append
<<
60 "\nMemBlob currently allocated size: " << liveBytes
<<
61 "\nlive MemBlob mean current allocation size: " <<
62 (static_cast<double>(liveBytes
)/(live
?live
:1)) << std::endl
;
69 MemBlob::MemBlob(const MemBlob::size_type reserveSize
) :
70 mem(NULL
), capacity(0), size(0) // will be set by memAlloc
72 debugs(MEMBLOB_DEBUGSECTION
,9, HERE
<< "constructed, this="
73 << static_cast<void*>(this) << " id=" << id
74 << " reserveSize=" << reserveSize
);
75 memAlloc(reserveSize
);
78 MemBlob::MemBlob(const char *buffer
, const MemBlob::size_type bufSize
) :
79 mem(NULL
), capacity(0), size(0) // will be set by memAlloc
81 debugs(MEMBLOB_DEBUGSECTION
,9, HERE
<< "constructed, this="
82 << static_cast<void*>(this) << " id=" << id
83 << " buffer=" << static_cast<const void*>(buffer
)
84 << " bufSize=" << bufSize
);
86 append(buffer
, bufSize
);
92 memFreeString(capacity
,mem
);
93 Stats
.liveBytes
-= capacity
;
96 debugs(MEMBLOB_DEBUGSECTION
,9, HERE
<< "destructed, this="
97 << static_cast<void*>(this) << " id=" << id
98 << " capacity=" << capacity
102 /** Allocate an available space area of at least minSize bytes in size.
103 * Must be called by constructors and only by constructors.
106 MemBlob::memAlloc(const size_type minSize
)
108 size_t actualAlloc
= minSize
;
111 mem
= static_cast<char*>(memAllocString(actualAlloc
, &actualAlloc
));
114 capacity
= actualAlloc
;
116 debugs(MEMBLOB_DEBUGSECTION
, 8,
117 id
<< " memAlloc: requested=" << minSize
<<
118 ", received=" << capacity
);
121 Stats
.liveBytes
+= capacity
;
125 MemBlob::append(const char *source
, const size_type n
)
127 if (n
> 0) { // appending zero bytes is allowed but only affects the stats
130 /// \note memcpy() is safe because we copy to an unused area
131 memcpy(mem
+ size
, source
, n
);
145 MemBlob::dump(std::ostream
&os
) const
147 os
<< "id @" << (void *)this
148 << "mem:" << static_cast<void*>(mem
)
149 << ",capacity:" << capacity
151 << ",refs:" << RefCountCount() << "; ";