]>
Commit | Line | Data |
---|---|---|
43d1bbe4 | 1 | /* |
43d1bbe4 FC |
2 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
3 | * ---------------------------------------------------------- | |
4 | * | |
5 | * Squid is the result of efforts by numerous individuals from | |
6 | * the Internet community; see the CONTRIBUTORS file for full | |
7 | * details. Many organizations have provided support for Squid's | |
8 | * development; see the SPONSORS file for full details. Squid is | |
9 | * Copyrighted (C) 2001 by the Regents of the University of | |
10 | * California; see the COPYRIGHT file for full details. Squid | |
11 | * incorporates software developed and/or copyrighted by other | |
12 | * sources; see the CREDITS file for full details. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. | |
27 | */ | |
28 | ||
f7f3304a | 29 | #include "squid.h" |
43d1bbe4 FC |
30 | #include "base/TextException.h" |
31 | #include "Debug.h" | |
835f22bb | 32 | #include "Mem.h" |
43d1bbe4 | 33 | #include "MemBlob.h" |
ff1eb053 | 34 | #include "SBufDetailedStats.h" |
835f22bb | 35 | |
43d1bbe4 | 36 | #include <iostream> |
43d1bbe4 | 37 | |
43d1bbe4 FC |
38 | MemBlobStats MemBlob::Stats; |
39 | InstanceIdDefinitions(MemBlob, "blob"); | |
40 | ||
43d1bbe4 FC |
41 | /* MemBlobStats */ |
42 | ||
cb38f5f5 | 43 | MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0), liveBytes(0) |
43d1bbe4 FC |
44 | {} |
45 | ||
412da427 FC |
46 | MemBlobStats& |
47 | MemBlobStats::operator += (const MemBlobStats& s) | |
48 | { | |
49 | alloc+=s.alloc; | |
50 | live+=s.live; | |
51 | append+=s.append; | |
52 | liveBytes+=s.liveBytes; | |
53 | ||
54 | return *this; | |
55 | } | |
56 | ||
43d1bbe4 FC |
57 | std::ostream& |
58 | MemBlobStats::dump(std::ostream &os) const | |
59 | { | |
60 | os << | |
61 | "MemBlob created: " << alloc << | |
62 | "\nMemBlob alive: " << live << | |
63 | "\nMemBlob append calls: " << append << | |
64 | "\nMemBlob currently allocated size: " << liveBytes << | |
65 | "\nlive MemBlob mean current allocation size: " << | |
66 | (static_cast<double>(liveBytes)/(live?live:1)) << std::endl; | |
67 | return os; | |
68 | } | |
69 | ||
43d1bbe4 FC |
70 | /* MemBlob */ |
71 | ||
72 | MemBlob::MemBlob(const MemBlob::size_type reserveSize) : | |
73 | mem(NULL), capacity(0), size(0) // will be set by memAlloc | |
74 | { | |
75 | debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this=" | |
76 | << static_cast<void*>(this) << " id=" << id | |
77 | << " reserveSize=" << reserveSize); | |
78 | memAlloc(reserveSize); | |
79 | } | |
80 | ||
81 | MemBlob::MemBlob(const char *buffer, const MemBlob::size_type bufSize) : | |
82 | mem(NULL), capacity(0), size(0) // will be set by memAlloc | |
83 | { | |
84 | debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this=" | |
85 | << static_cast<void*>(this) << " id=" << id | |
86 | << " buffer=" << static_cast<const void*>(buffer) | |
87 | << " bufSize=" << bufSize); | |
88 | memAlloc(bufSize); | |
89 | append(buffer, bufSize); | |
90 | } | |
91 | ||
92 | MemBlob::~MemBlob() | |
93 | { | |
835f22bb FC |
94 | if (mem || capacity) |
95 | memFreeString(capacity,mem); | |
43d1bbe4 FC |
96 | Stats.liveBytes -= capacity; |
97 | --Stats.live; | |
21a8baf1 | 98 | recordMemBlobSizeAtDestruct(capacity); |
43d1bbe4 FC |
99 | |
100 | debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this=" | |
101 | << static_cast<void*>(this) << " id=" << id | |
b59e6847 A |
102 | << " capacity=" << capacity |
103 | << " size=" << size); | |
43d1bbe4 FC |
104 | } |
105 | ||
43d1bbe4 FC |
106 | /** Allocate an available space area of at least minSize bytes in size. |
107 | * Must be called by constructors and only by constructors. | |
108 | */ | |
109 | void | |
110 | MemBlob::memAlloc(const size_type minSize) | |
111 | { | |
835f22bb | 112 | size_t actualAlloc = minSize; |
43d1bbe4 FC |
113 | |
114 | Must(!mem); | |
835f22bb | 115 | mem = static_cast<char*>(memAllocString(actualAlloc, &actualAlloc)); |
43d1bbe4 FC |
116 | Must(mem); |
117 | ||
118 | capacity = actualAlloc; | |
119 | size = 0; | |
120 | debugs(MEMBLOB_DEBUGSECTION, 8, | |
121 | id << " memAlloc: requested=" << minSize << | |
122 | ", received=" << capacity); | |
123 | ++Stats.live; | |
124 | ++Stats.alloc; | |
125 | Stats.liveBytes += capacity; | |
126 | } | |
127 | ||
6bba9bf1 AR |
128 | void |
129 | MemBlob::appended(const size_type n) | |
130 | { | |
131 | Must(willFit(n)); | |
132 | size += n; | |
133 | ++Stats.append; | |
134 | } | |
135 | ||
43d1bbe4 FC |
136 | void |
137 | MemBlob::append(const char *source, const size_type n) | |
b59e6847 | 138 | { |
43d1bbe4 FC |
139 | if (n > 0) { // appending zero bytes is allowed but only affects the stats |
140 | Must(willFit(n)); | |
141 | Must(source); | |
07bdd852 | 142 | memmove(mem + size, source, n); |
43d1bbe4 FC |
143 | size += n; |
144 | } | |
145 | ++Stats.append; | |
146 | } | |
147 | ||
43d1bbe4 FC |
148 | const MemBlobStats& |
149 | MemBlob::GetStats() | |
150 | { | |
151 | return Stats; | |
152 | } | |
153 | ||
154 | std::ostream& | |
155 | MemBlob::dump(std::ostream &os) const | |
156 | { | |
157 | os << "id @" << (void *)this | |
158 | << "mem:" << static_cast<void*>(mem) | |
159 | << ",capacity:" << capacity | |
160 | << ",size:" << size | |
8bf217bd | 161 | << ",refs:" << LockCount() << "; "; |
43d1bbe4 FC |
162 | return os; |
163 | } |