]> git.ipfire.org Git - thirdparty/squid.git/blob - src/sbuf/MemBlob.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / sbuf / MemBlob.cc
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "base/TextException.h"
11 #include "Debug.h"
12 #include "sbuf/DetailedStats.h"
13 #include "sbuf/MemBlob.h"
14
15 #include <iostream>
16
17 MemBlobStats MemBlob::Stats;
18 InstanceIdDefinitions(MemBlob, "blob");
19
20 /* MemBlobStats */
21
22 MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0), liveBytes(0)
23 {}
24
25 MemBlobStats&
26 MemBlobStats::operator += (const MemBlobStats& s)
27 {
28 alloc+=s.alloc;
29 live+=s.live;
30 append+=s.append;
31 liveBytes+=s.liveBytes;
32
33 return *this;
34 }
35
36 std::ostream&
37 MemBlobStats::dump(std::ostream &os) const
38 {
39 os <<
40 "MemBlob created: " << alloc <<
41 "\nMemBlob alive: " << live <<
42 "\nMemBlob append calls: " << append <<
43 "\nMemBlob currently allocated size: " << liveBytes <<
44 "\nlive MemBlob mean current allocation size: " <<
45 (static_cast<double>(liveBytes)/(live?live:1)) << std::endl;
46 return os;
47 }
48
49 /* MemBlob */
50
51 MemBlob::MemBlob(const MemBlob::size_type reserveSize) :
52 mem(NULL), capacity(0), size(0) // will be set by memAlloc
53 {
54 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this="
55 << static_cast<void*>(this) << " id=" << id
56 << " reserveSize=" << reserveSize);
57 memAlloc(reserveSize);
58 }
59
60 MemBlob::MemBlob(const char *buffer, const MemBlob::size_type bufSize) :
61 mem(NULL), capacity(0), size(0) // will be set by memAlloc
62 {
63 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this="
64 << static_cast<void*>(this) << " id=" << id
65 << " buffer=" << static_cast<const void*>(buffer)
66 << " bufSize=" << bufSize);
67 memAlloc(bufSize);
68 append(buffer, bufSize);
69 }
70
71 MemBlob::~MemBlob()
72 {
73 if (mem || capacity)
74 memFreeString(capacity,mem);
75 Stats.liveBytes -= capacity;
76 --Stats.live;
77 recordMemBlobSizeAtDestruct(capacity);
78
79 debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this="
80 << static_cast<void*>(this) << " id=" << id
81 << " capacity=" << capacity
82 << " size=" << size);
83 }
84
85 /** Allocate an available space area of at least minSize bytes in size.
86 * Must be called by constructors and only by constructors.
87 */
88 void
89 MemBlob::memAlloc(const size_type minSize)
90 {
91 size_t actualAlloc = minSize;
92
93 Must(!mem);
94 mem = static_cast<char*>(memAllocString(actualAlloc, &actualAlloc));
95 Must(mem);
96
97 capacity = actualAlloc;
98 size = 0;
99 debugs(MEMBLOB_DEBUGSECTION, 8,
100 id << " memAlloc: requested=" << minSize <<
101 ", received=" << capacity);
102 ++Stats.live;
103 ++Stats.alloc;
104 Stats.liveBytes += capacity;
105 }
106
107 void
108 MemBlob::appended(const size_type n)
109 {
110 Must(willFit(n));
111 size += n;
112 ++Stats.append;
113 }
114
115 void
116 MemBlob::append(const char *source, const size_type n)
117 {
118 if (n > 0) { // appending zero bytes is allowed but only affects the stats
119 Must(willFit(n));
120 Must(source);
121 memmove(mem + size, source, n);
122 size += n;
123 }
124 ++Stats.append;
125 }
126
127 const MemBlobStats&
128 MemBlob::GetStats()
129 {
130 return Stats;
131 }
132
133 std::ostream&
134 MemBlob::dump(std::ostream &os) const
135 {
136 os << "id @" << (void *)this
137 << "mem:" << static_cast<void*>(mem)
138 << ",capacity:" << capacity
139 << ",size:" << size
140 << ",refs:" << LockCount() << "; ";
141 return os;
142 }
143