]>
Commit | Line | Data |
---|---|---|
43d1bbe4 | 1 | /* |
5b74111a | 2 | * Copyright (C) 1996-2018 The Squid Software Foundation and contributors |
43d1bbe4 | 3 | * |
bbc27441 AJ |
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. | |
43d1bbe4 FC |
7 | */ |
8 | ||
f7f3304a | 9 | #include "squid.h" |
43d1bbe4 FC |
10 | #include "base/TextException.h" |
11 | #include "Debug.h" | |
dfe4488f | 12 | #include "sbuf/DetailedStats.h" |
a471b119 | 13 | #include "sbuf/MemBlob.h" |
835f22bb | 14 | |
43d1bbe4 | 15 | #include <iostream> |
43d1bbe4 | 16 | |
43d1bbe4 FC |
17 | MemBlobStats MemBlob::Stats; |
18 | InstanceIdDefinitions(MemBlob, "blob"); | |
19 | ||
43d1bbe4 FC |
20 | /* MemBlobStats */ |
21 | ||
cb38f5f5 | 22 | MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0), liveBytes(0) |
43d1bbe4 FC |
23 | {} |
24 | ||
412da427 FC |
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 | ||
43d1bbe4 FC |
36 | std::ostream& |
37 | MemBlobStats::dump(std::ostream &os) const | |
38 | { | |
39 | os << | |
f53969cc SM |
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; | |
43d1bbe4 FC |
46 | return os; |
47 | } | |
48 | ||
43d1bbe4 FC |
49 | /* MemBlob */ |
50 | ||
51 | MemBlob::MemBlob(const MemBlob::size_type reserveSize) : | |
f53969cc | 52 | mem(NULL), capacity(0), size(0) // will be set by memAlloc |
43d1bbe4 FC |
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) : | |
f53969cc | 61 | mem(NULL), capacity(0), size(0) // will be set by memAlloc |
43d1bbe4 FC |
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 | { | |
835f22bb FC |
73 | if (mem || capacity) |
74 | memFreeString(capacity,mem); | |
43d1bbe4 FC |
75 | Stats.liveBytes -= capacity; |
76 | --Stats.live; | |
21a8baf1 | 77 | recordMemBlobSizeAtDestruct(capacity); |
43d1bbe4 FC |
78 | |
79 | debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this=" | |
80 | << static_cast<void*>(this) << " id=" << id | |
b59e6847 A |
81 | << " capacity=" << capacity |
82 | << " size=" << size); | |
43d1bbe4 FC |
83 | } |
84 | ||
43d1bbe4 FC |
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 | { | |
835f22bb | 91 | size_t actualAlloc = minSize; |
43d1bbe4 FC |
92 | |
93 | Must(!mem); | |
835f22bb | 94 | mem = static_cast<char*>(memAllocString(actualAlloc, &actualAlloc)); |
43d1bbe4 FC |
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 | ||
6bba9bf1 AR |
107 | void |
108 | MemBlob::appended(const size_type n) | |
109 | { | |
110 | Must(willFit(n)); | |
111 | size += n; | |
112 | ++Stats.append; | |
113 | } | |
114 | ||
43d1bbe4 FC |
115 | void |
116 | MemBlob::append(const char *source, const size_type n) | |
b59e6847 | 117 | { |
43d1bbe4 FC |
118 | if (n > 0) { // appending zero bytes is allowed but only affects the stats |
119 | Must(willFit(n)); | |
120 | Must(source); | |
07bdd852 | 121 | memmove(mem + size, source, n); |
43d1bbe4 FC |
122 | size += n; |
123 | } | |
124 | ++Stats.append; | |
125 | } | |
126 | ||
43d1bbe4 FC |
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 | |
f53969cc SM |
137 | << "mem:" << static_cast<void*>(mem) |
138 | << ",capacity:" << capacity | |
139 | << ",size:" << size | |
140 | << ",refs:" << LockCount() << "; "; | |
43d1bbe4 FC |
141 | return os; |
142 | } | |
f53969cc | 143 |