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