]> git.ipfire.org Git - thirdparty/squid.git/blame - src/MemBlob.cc
cachemgr.cgi: Memory Leaks and DoS Vulnerability
[thirdparty/squid.git] / src / MemBlob.cc
CommitLineData
43d1bbe4
FC
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
f7f3304a 31#include "squid.h"
43d1bbe4
FC
32#include "base/TextException.h"
33#include "Debug.h"
835f22bb 34#include "Mem.h"
43d1bbe4 35#include "MemBlob.h"
835f22bb 36
43d1bbe4
FC
37#if HAVE_IOSTREAM
38#include <iostream>
39#endif
40
43d1bbe4
FC
41MemBlobStats MemBlob::Stats;
42InstanceIdDefinitions(MemBlob, "blob");
43
43d1bbe4
FC
44/* MemBlobStats */
45
46MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0)
47{}
48
49std::ostream&
50MemBlobStats::dump(std::ostream &os) const
51{
52 os <<
53 "MemBlob created: " << alloc <<
54 "\nMemBlob alive: " << live <<
55 "\nMemBlob append calls: " << append <<
56 "\nMemBlob currently allocated size: " << liveBytes <<
57 "\nlive MemBlob mean current allocation size: " <<
58 (static_cast<double>(liveBytes)/(live?live:1)) << std::endl;
59 return os;
60}
61
43d1bbe4
FC
62/* MemBlob */
63
64MemBlob::MemBlob(const MemBlob::size_type reserveSize) :
65 mem(NULL), capacity(0), size(0) // will be set by memAlloc
66{
67 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this="
68 << static_cast<void*>(this) << " id=" << id
69 << " reserveSize=" << reserveSize);
70 memAlloc(reserveSize);
71}
72
73MemBlob::MemBlob(const char *buffer, const MemBlob::size_type bufSize) :
74 mem(NULL), capacity(0), size(0) // will be set by memAlloc
75{
76 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this="
77 << static_cast<void*>(this) << " id=" << id
78 << " buffer=" << static_cast<const void*>(buffer)
79 << " bufSize=" << bufSize);
80 memAlloc(bufSize);
81 append(buffer, bufSize);
82}
83
84MemBlob::~MemBlob()
85{
835f22bb
FC
86 if (mem || capacity)
87 memFreeString(capacity,mem);
43d1bbe4
FC
88 Stats.liveBytes -= capacity;
89 --Stats.live;
90
91 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this="
92 << static_cast<void*>(this) << " id=" << id
b59e6847
A
93 << " capacity=" << capacity
94 << " size=" << size);
43d1bbe4
FC
95}
96
43d1bbe4
FC
97/** Allocate an available space area of at least minSize bytes in size.
98 * Must be called by constructors and only by constructors.
99 */
100void
101MemBlob::memAlloc(const size_type minSize)
102{
835f22bb 103 size_t actualAlloc = minSize;
43d1bbe4
FC
104
105 Must(!mem);
835f22bb 106 mem = static_cast<char*>(memAllocString(actualAlloc, &actualAlloc));
43d1bbe4
FC
107 Must(mem);
108
109 capacity = actualAlloc;
110 size = 0;
111 debugs(MEMBLOB_DEBUGSECTION, 8,
112 id << " memAlloc: requested=" << minSize <<
113 ", received=" << capacity);
114 ++Stats.live;
115 ++Stats.alloc;
116 Stats.liveBytes += capacity;
117}
118
119void
120MemBlob::append(const char *source, const size_type n)
b59e6847 121{
43d1bbe4
FC
122 if (n > 0) { // appending zero bytes is allowed but only affects the stats
123 Must(willFit(n));
124 Must(source);
125 /// \note memcpy() is safe because we copy to an unused area
126 memcpy(mem + size, source, n);
127 size += n;
128 }
129 ++Stats.append;
130}
131
43d1bbe4
FC
132const MemBlobStats&
133MemBlob::GetStats()
134{
135 return Stats;
136}
137
138std::ostream&
139MemBlob::dump(std::ostream &os) const
140{
141 os << "id @" << (void *)this
142 << "mem:" << static_cast<void*>(mem)
143 << ",capacity:" << capacity
144 << ",size:" << size
8bf217bd 145 << ",refs:" << LockCount() << "; ";
43d1bbe4
FC
146 return os;
147}