MemBlob.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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
19
20/* MemBlobStats */
21
22MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0), liveBytes(0)
23{}
24
27{
28 alloc+=s.alloc;
29 live+=s.live;
30 append+=s.append;
32
33 return *this;
34}
35
36std::ostream&
37MemBlobStats::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
52 mem(nullptr), capacity(0), size(0) // will be set by memAlloc
53{
54 debugs(MEMBLOB_DEBUGSECTION,9, "constructed, this="
55 << static_cast<void*>(this) << " id=" << id
56 << " reserveSize=" << reserveSize);
57 memAlloc(reserveSize);
58}
59
60MemBlob::MemBlob(const char *buffer, const MemBlob::size_type bufSize) :
61 mem(nullptr), capacity(0), size(0) // will be set by memAlloc
62{
63 debugs(MEMBLOB_DEBUGSECTION,9, "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
72{
73 if (mem || capacity)
76 --Stats.live;
78
79 debugs(MEMBLOB_DEBUGSECTION,9, "destructed, this="
80 << static_cast<void*>(this) << " id=" << id
81 << " capacity=" << capacity
82 << " size=" << size);
83}
84
88void
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;
100 id << " memAlloc: requested=" << minSize <<
101 ", received=" << capacity);
102 ++Stats.live;
103 ++Stats.alloc;
105}
106
107void
109{
110 Must(willFit(n));
111 size += n;
112 ++Stats.append;
113}
114
115void
116MemBlob::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
127void
129{
130 debugs(MEMBLOB_DEBUGSECTION, 7, n << " was: " << size);
131 Must(LockCount() <= 1);
132 Must(n <= size);
133 size = n;
134}
135
136void
138{
139 if (rawN && size) {
140 Must(LockCount() <= 1);
141 const auto n = std::min(rawN, size);
142 size -= n;
143 if (size)
144 memmove(mem, mem + n, size);
145 }
146}
147
148const MemBlobStats&
150{
151 return Stats;
152}
153
154std::ostream&
155MemBlob::dump(std::ostream &os) const
156{
157 os << "id @" << (void *)this
158 << "mem:" << static_cast<void*>(mem)
159 << ",capacity:" << capacity
160 << ",size:" << size
161 << ",refs:" << LockCount() << "; ";
162 return os;
163}
164
InstanceIdDefinitions(MemBlob, "blob")
#define MEMBLOB_DEBUGSECTION
Definition: MemBlob.h:12
int size
Definition: ModDevPoll.cc:75
#define Must(condition)
Definition: TextException.h:75
Various MemBlob class-wide statistics.
Definition: MemBlob.h:20
uint64_t live
number of MemBlob instances currently alive
Definition: MemBlob.h:31
MemBlobStats()
Definition: MemBlob.cc:22
uint64_t liveBytes
the total size of currently allocated storage
Definition: MemBlob.h:33
uint64_t alloc
number of MemBlob instances created so far
Definition: MemBlob.h:30
uint64_t append
number of MemBlob::append() calls
Definition: MemBlob.h:32
MemBlobStats & operator+=(const MemBlobStats &)
Definition: MemBlob.cc:26
std::ostream & dump(std::ostream &os) const
dumps class-wide statistics
Definition: MemBlob.cc:37
char * mem
raw allocated memory block
Definition: MemBlob.h:112
void append(const char *source, const size_type n)
Definition: MemBlob.cc:116
void syncSize(const size_type n)
Definition: MemBlob.cc:128
bool willFit(const size_type n) const
whether n more bytes can be appended
Definition: MemBlob.h:126
MemBlob(const size_type reserveSize)
create a new MemBlob with at least reserveSize capacity
Definition: MemBlob.cc:51
size_type size
maximum allocated memory in use by callers
Definition: MemBlob.h:114
static MemBlobStats Stats
class-wide statistics
Definition: MemBlob.h:118
static const MemBlobStats & GetStats()
obtain a const view of class-wide statistics
Definition: MemBlob.cc:149
void memAlloc(const size_type memSize)
Definition: MemBlob.cc:89
void appended(const size_type n)
Definition: MemBlob.cc:108
uint32_t size_type
Definition: MemBlob.h:51
~MemBlob() override
Definition: MemBlob.cc:71
std::ostream & dump(std::ostream &os) const
dump debugging information
Definition: MemBlob.cc:155
void consume(const size_type n)
Definition: MemBlob.cc:137
size_type capacity
size of the raw allocated memory block
Definition: MemBlob.h:113
static void RecordMemBlobSizeAtDestruct(size_t)
Record the size a MemBlob had when it was destructed.
Definition: Stats.cc:27
A const & min(A const &lhs, A const &rhs)
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
void memFreeString(size_t size, void *)
Definition: minimal.cc:72
void * memAllocString(size_t net_size, size_t *gross_size)
Definition: minimal.cc:66

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors