]>
Commit | Line | Data |
---|---|---|
43d1bbe4 | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 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" | |
835f22bb | 12 | #include "Mem.h" |
43d1bbe4 | 13 | #include "MemBlob.h" |
ff1eb053 | 14 | #include "SBufDetailedStats.h" |
835f22bb | 15 | |
43d1bbe4 | 16 | #include <iostream> |
43d1bbe4 | 17 | |
43d1bbe4 FC |
18 | MemBlobStats MemBlob::Stats; |
19 | InstanceIdDefinitions(MemBlob, "blob"); | |
20 | ||
43d1bbe4 FC |
21 | /* MemBlobStats */ |
22 | ||
cb38f5f5 | 23 | MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0), liveBytes(0) |
43d1bbe4 FC |
24 | {} |
25 | ||
412da427 FC |
26 | MemBlobStats& |
27 | MemBlobStats::operator += (const MemBlobStats& s) | |
28 | { | |
29 | alloc+=s.alloc; | |
30 | live+=s.live; | |
31 | append+=s.append; | |
32 | liveBytes+=s.liveBytes; | |
33 | ||
34 | return *this; | |
35 | } | |
36 | ||
43d1bbe4 FC |
37 | std::ostream& |
38 | MemBlobStats::dump(std::ostream &os) const | |
39 | { | |
40 | os << | |
41 | "MemBlob created: " << alloc << | |
42 | "\nMemBlob alive: " << live << | |
43 | "\nMemBlob append calls: " << append << | |
44 | "\nMemBlob currently allocated size: " << liveBytes << | |
45 | "\nlive MemBlob mean current allocation size: " << | |
46 | (static_cast<double>(liveBytes)/(live?live:1)) << std::endl; | |
47 | return os; | |
48 | } | |
49 | ||
43d1bbe4 FC |
50 | /* MemBlob */ |
51 | ||
52 | MemBlob::MemBlob(const MemBlob::size_type reserveSize) : | |
53 | mem(NULL), capacity(0), size(0) // will be set by memAlloc | |
54 | { | |
55 | debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this=" | |
56 | << static_cast<void*>(this) << " id=" << id | |
57 | << " reserveSize=" << reserveSize); | |
58 | memAlloc(reserveSize); | |
59 | } | |
60 | ||
61 | MemBlob::MemBlob(const char *buffer, const MemBlob::size_type bufSize) : | |
62 | mem(NULL), capacity(0), size(0) // will be set by memAlloc | |
63 | { | |
64 | debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this=" | |
65 | << static_cast<void*>(this) << " id=" << id | |
66 | << " buffer=" << static_cast<const void*>(buffer) | |
67 | << " bufSize=" << bufSize); | |
68 | memAlloc(bufSize); | |
69 | append(buffer, bufSize); | |
70 | } | |
71 | ||
72 | MemBlob::~MemBlob() | |
73 | { | |
835f22bb FC |
74 | if (mem || capacity) |
75 | memFreeString(capacity,mem); | |
43d1bbe4 FC |
76 | Stats.liveBytes -= capacity; |
77 | --Stats.live; | |
21a8baf1 | 78 | recordMemBlobSizeAtDestruct(capacity); |
43d1bbe4 FC |
79 | |
80 | debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this=" | |
81 | << static_cast<void*>(this) << " id=" << id | |
b59e6847 A |
82 | << " capacity=" << capacity |
83 | << " size=" << size); | |
43d1bbe4 FC |
84 | } |
85 | ||
43d1bbe4 FC |
86 | /** Allocate an available space area of at least minSize bytes in size. |
87 | * Must be called by constructors and only by constructors. | |
88 | */ | |
89 | void | |
90 | MemBlob::memAlloc(const size_type minSize) | |
91 | { | |
835f22bb | 92 | size_t actualAlloc = minSize; |
43d1bbe4 FC |
93 | |
94 | Must(!mem); | |
835f22bb | 95 | mem = static_cast<char*>(memAllocString(actualAlloc, &actualAlloc)); |
43d1bbe4 FC |
96 | Must(mem); |
97 | ||
98 | capacity = actualAlloc; | |
99 | size = 0; | |
100 | debugs(MEMBLOB_DEBUGSECTION, 8, | |
101 | id << " memAlloc: requested=" << minSize << | |
102 | ", received=" << capacity); | |
103 | ++Stats.live; | |
104 | ++Stats.alloc; | |
105 | Stats.liveBytes += capacity; | |
106 | } | |
107 | ||
6bba9bf1 AR |
108 | void |
109 | MemBlob::appended(const size_type n) | |
110 | { | |
111 | Must(willFit(n)); | |
112 | size += n; | |
113 | ++Stats.append; | |
114 | } | |
115 | ||
43d1bbe4 FC |
116 | void |
117 | MemBlob::append(const char *source, const size_type n) | |
b59e6847 | 118 | { |
43d1bbe4 FC |
119 | if (n > 0) { // appending zero bytes is allowed but only affects the stats |
120 | Must(willFit(n)); | |
121 | Must(source); | |
07bdd852 | 122 | memmove(mem + size, source, n); |
43d1bbe4 FC |
123 | size += n; |
124 | } | |
125 | ++Stats.append; | |
126 | } | |
127 | ||
43d1bbe4 FC |
128 | const MemBlobStats& |
129 | MemBlob::GetStats() | |
130 | { | |
131 | return Stats; | |
132 | } | |
133 | ||
134 | std::ostream& | |
135 | MemBlob::dump(std::ostream &os) const | |
136 | { | |
137 | os << "id @" << (void *)this | |
138 | << "mem:" << static_cast<void*>(mem) | |
139 | << ",capacity:" << capacity | |
140 | << ",size:" << size | |
8bf217bd | 141 | << ",refs:" << LockCount() << "; "; |
43d1bbe4 FC |
142 | return os; |
143 | } |