MemBlob.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 
19 
20 /* MemBlobStats */
21 
22 MemBlobStats::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 
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 
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 
72 {
73  if (mem || capacity)
76  --Stats.live;
78 
79  debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this="
80  << static_cast<void*>(this) << " id=" << id
81  << " capacity=" << capacity
82  << " size=" << size);
83 }
84 
88 void
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 
107 void
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&
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 
uint64_t alloc
number of MemBlob instances created so far
Definition: MemBlob.h:30
uint32_t size_type
Definition: MemBlob.h:51
uint64_t liveBytes
the total size of currently allocated storage
Definition: MemBlob.h:33
void appended(const size_type n)
Definition: MemBlob.cc:108
size_type capacity
size of the raw allocated memory block
Definition: MemBlob.h:103
static MemBlobStats Stats
class-wide statistics
Definition: MemBlob.h:108
uint64_t live
number of MemBlob instances currently alive
Definition: MemBlob.h:31
MemBlobStats & operator+=(const MemBlobStats &)
Definition: MemBlob.cc:26
static const MemBlobStats & GetStats()
obtain a const view of class-wide statistics
Definition: MemBlob.cc:128
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define MEMBLOB_DEBUGSECTION
Definition: MemBlob.h:12
bool willFit(const size_type n) const
whether n more bytes can be appended
Definition: MemBlob.h:116
MemBlob(const size_type reserveSize)
create a new MemBlob with at least reserveSize capacity
Definition: MemBlob.cc:51
std::ostream & dump(std::ostream &os) const
dump debugging information
Definition: MemBlob.cc:134
char * mem
raw allocated memory block
Definition: MemBlob.h:102
void memAlloc(const size_type memSize)
Definition: MemBlob.cc:89
std::ostream & dump(std::ostream &os) const
dumps class-wide statistics
Definition: MemBlob.cc:37
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
void * memAllocString(size_t net_size, size_t *gross_size)
Definition: old_api.cc:235
void append(const char *source, const size_type n)
Definition: MemBlob.cc:116
#define Must(cond)
Definition: TextException.h:89
InstanceIdDefinitions(MemBlob,"blob")
Various MemBlob class-wide statistics.
Definition: MemBlob.h:19
void memFreeString(size_t size, void *)
Definition: old_api.cc:268
void recordMemBlobSizeAtDestruct(SBuf::size_type sz)
Record the size a MemBlob had when it was destructed.
virtual ~MemBlob()
Definition: MemBlob.cc:71
uint64_t append
number of MemBlob::append() calls
Definition: MemBlob.h:32
size_type size
maximum allocated memory in use by callers
Definition: MemBlob.h:104
MemBlobStats()
Definition: MemBlob.cc:22
#define NULL
Definition: types.h:166
int size
Definition: ModDevPoll.cc:77

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors