]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBlob.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / MemBlob.cc
1 /*
2 * MemBlob.cc (C) 2009 Francesco Chemolli <kinkie@squid-cache.org>
3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
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.
15 *
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.
20 *
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.
25 *
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.
29 */
30
31
32 #include "squid.h"
33 #include "base/TextException.h"
34 #include "Debug.h"
35 #include "Mem.h"
36 #include "MemBlob.h"
37 #include "protos.h"
38
39 #if HAVE_IOSTREAM
40 #include <iostream>
41 #endif
42
43
44 MemBlobStats MemBlob::Stats;
45 InstanceIdDefinitions(MemBlob, "blob");
46
47
48 /* MemBlobStats */
49
50 MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0)
51 {}
52
53 std::ostream&
54 MemBlobStats::dump(std::ostream &os) const
55 {
56 os <<
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;
63 return os;
64 }
65
66
67 /* MemBlob */
68
69 MemBlob::MemBlob(const MemBlob::size_type reserveSize) :
70 mem(NULL), capacity(0), size(0) // will be set by memAlloc
71 {
72 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this="
73 << static_cast<void*>(this) << " id=" << id
74 << " reserveSize=" << reserveSize);
75 memAlloc(reserveSize);
76 }
77
78 MemBlob::MemBlob(const char *buffer, const MemBlob::size_type bufSize) :
79 mem(NULL), capacity(0), size(0) // will be set by memAlloc
80 {
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);
85 memAlloc(bufSize);
86 append(buffer, bufSize);
87 }
88
89 MemBlob::~MemBlob()
90 {
91 if (mem || capacity)
92 memFreeString(capacity,mem);
93 Stats.liveBytes -= capacity;
94 --Stats.live;
95
96 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this="
97 << static_cast<void*>(this) << " id=" << id
98 << " capacity=" << capacity
99 << " size=" << size);
100 }
101
102 /** Allocate an available space area of at least minSize bytes in size.
103 * Must be called by constructors and only by constructors.
104 */
105 void
106 MemBlob::memAlloc(const size_type minSize)
107 {
108 size_t actualAlloc = minSize;
109
110 Must(!mem);
111 mem = static_cast<char*>(memAllocString(actualAlloc, &actualAlloc));
112 Must(mem);
113
114 capacity = actualAlloc;
115 size = 0;
116 debugs(MEMBLOB_DEBUGSECTION, 8,
117 id << " memAlloc: requested=" << minSize <<
118 ", received=" << capacity);
119 ++Stats.live;
120 ++Stats.alloc;
121 Stats.liveBytes += capacity;
122 }
123
124 void
125 MemBlob::append(const char *source, const size_type n)
126 {
127 if (n > 0) { // appending zero bytes is allowed but only affects the stats
128 Must(willFit(n));
129 Must(source);
130 /// \note memcpy() is safe because we copy to an unused area
131 memcpy(mem + size, source, n);
132 size += n;
133 }
134 ++Stats.append;
135 }
136
137
138 const MemBlobStats&
139 MemBlob::GetStats()
140 {
141 return Stats;
142 }
143
144 std::ostream&
145 MemBlob::dump(std::ostream &os) const
146 {
147 os << "id @" << (void *)this
148 << "mem:" << static_cast<void*>(mem)
149 << ",capacity:" << capacity
150 << ",size:" << size
151 << ",refs:" << RefCountCount() << "; ";
152 return os;
153 }