MemBlob.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2025 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/Stream.h"
12 #include "sbuf/MemBlob.h"
13 #include "sbuf/Stats.h"
14 
15 #include <iostream>
16 
18 
19 /* MemBlobStats */
20 
23 {
24  alloc+=s.alloc;
25  live+=s.live;
26  append+=s.append;
28 
29  return *this;
30 }
31 
32 std::ostream&
33 MemBlobStats::dump(std::ostream &os) const
34 {
35  os <<
36  "MemBlob created: " << alloc <<
37  "\nMemBlob alive: " << live <<
38  "\nMemBlob append calls: " << append <<
39  "\nMemBlob currently allocated size: " << liveBytes <<
40  "\nlive MemBlob mean current allocation size: " <<
41  (static_cast<double>(liveBytes)/(live?live:1)) << std::endl;
42  return os;
43 }
44 
45 static auto &
47 {
48  static const auto stats = new MemBlobStats();
49  return *stats;
50 }
51 
52 const MemBlobStats &
54 {
55  return WriteableStats();
56 }
57 
58 /* MemBlob */
59 
60 MemBlob::MemBlob(const size_type reserveSize)
61 {
62  debugs(MEMBLOB_DEBUGSECTION,9, "constructed, this="
63  << static_cast<void*>(this) << " id=" << id
64  << " reserveSize=" << reserveSize);
65  memAlloc(reserveSize);
66 }
67 
69 {
70  debugs(MEMBLOB_DEBUGSECTION,9, "constructed, this="
71  << static_cast<void*>(this) << " id=" << id
72  << " buffer=" << static_cast<const void*>(buffer)
73  << " bufSize=" << bufSize);
74  memAlloc(bufSize);
75  append(buffer, bufSize);
76 }
77 
79 {
80  if (mem || capacity)
82  auto &stats = WriteableStats();
83  stats.liveBytes -= capacity;
84  --stats.live;
86 
87  debugs(MEMBLOB_DEBUGSECTION,9, "destructed, this="
88  << static_cast<void*>(this) << " id=" << id
89  << " capacity=" << capacity
90  << " size=" << size);
91 }
92 
96 void
98 {
99  size_t actualAlloc = minSize;
100 
101  Must(!mem);
102  mem = static_cast<value_type *>(memAllocBuf(actualAlloc, &actualAlloc));
103  Must(mem);
104 
105  capacity = actualAlloc;
106  size = 0;
108  id << " memAlloc: requested=" << minSize <<
109  ", received=" << capacity);
110  auto &stats = WriteableStats();
111  ++stats.live;
112  ++stats.alloc;
113  stats.liveBytes += capacity;
114 }
115 
116 void
118 {
119  Must(willFit(n));
120  size += n;
121  ++WriteableStats().append;
122 }
123 
124 void
126 {
127  if (n > 0) { // appending zero bytes is allowed but only affects the stats
128  Must(willFit(n));
129  Must(source);
130  memmove(mem + size, source, n);
131  size += n;
132  }
133  ++WriteableStats().append;
134 }
135 
136 void
138 {
139  debugs(MEMBLOB_DEBUGSECTION, 7, n << " was: " << size);
140  Must(LockCount() <= 1);
141  Must(n <= size);
142  size = n;
143 }
144 
145 void
147 {
148  if (rawN && size) {
149  Must(LockCount() <= 1);
150  const auto n = std::min(rawN, size);
151  size -= n;
152  if (size)
153  memmove(mem, mem + n, size);
154  }
155 }
156 
157 std::ostream&
158 MemBlob::dump(std::ostream &os) const
159 {
160  os << "id @" << (void *)this
161  << "mem:" << static_cast<void*>(mem)
162  << ",capacity:" << capacity
163  << ",size:" << size
164  << ",refs:" << LockCount() << "; ";
165  return os;
166 }
167 
#define MEMBLOB_DEBUGSECTION
Definition: MemBlob.h:12
void syncSize(const size_type n)
Definition: MemBlob.cc:137
bool willFit(const size_type n) const
whether n more bytes can be appended
Definition: MemBlob.h:125
static auto & WriteableStats()
Definition: MemBlob.cc:46
void * memAllocBuf(size_t net_size, size_t *gross_size)
Definition: minimal.cc:46
void append(const_pointer source, const size_type n)
Definition: MemBlob.cc:125
size_type size
maximum allocated memory in use by callers
Definition: MemBlob.h:115
uint64_t append
number of MemBlob::append() calls
Definition: MemBlob.h:30
~MemBlob() override
Definition: MemBlob.cc:78
MemBlob(const size_type reserveSize)
create a new MemBlob with at least reserveSize capacity
Definition: MemBlob.cc:60
value_type * mem
raw allocated memory block
Definition: MemBlob.h:113
uint64_t alloc
number of MemBlob instances created so far
Definition: MemBlob.h:28
std::ostream & dump(std::ostream &os) const
dumps class-wide statistics
Definition: MemBlob.cc:33
const value_type * const_pointer
Definition: MemBlob.h:53
InstanceIdDefinitions(MemBlob, "blob")
static const MemBlobStats & GetStats()
obtain a const view of class-wide statistics
Definition: MemBlob.cc:53
void appended(const size_type n)
Definition: MemBlob.cc:117
uint64_t live
number of MemBlob instances currently alive
Definition: MemBlob.h:29
uint64_t liveBytes
the total size of currently allocated storage
Definition: MemBlob.h:31
uint32_t size_type
Definition: MemBlob.h:52
char value_type
Definition: MemBlob.h:51
MemBlobStats & operator+=(const MemBlobStats &)
Definition: MemBlob.cc:22
size_type capacity
size of the raw allocated memory block
Definition: MemBlob.h:114
static void RecordMemBlobSizeAtDestruct(size_t)
Record the size a MemBlob had when it was destructed.
Definition: Stats.cc:27
#define Must(condition)
Definition: TextException.h:75
std::ostream & dump(std::ostream &os) const
dump debugging information
Definition: MemBlob.cc:158
Various MemBlob class-wide statistics.
Definition: MemBlob.h:19
void consume(const size_type n)
Definition: MemBlob.cc:146
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
const A & min(A const &lhs, A const &rhs)
void memAlloc(const size_type memSize)
Definition: MemBlob.cc:97
void memFreeBuf(size_t size, void *)
Definition: minimal.cc:67

 

Introduction

Documentation

Support

Miscellaneous