MemBlob.h
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 #ifndef SQUID_SRC_SBUF_MEMBLOB_H
10 #define SQUID_SRC_SBUF_MEMBLOB_H
11 
12 #define MEMBLOB_DEBUGSECTION 24
13 
14 #include "base/InstanceId.h"
15 #include "base/RefCount.h"
16 #include "mem/forward.h"
17 
20 {
21 public:
23  std::ostream& dump(std::ostream& os) const;
24 
26 
27 public:
28  uint64_t alloc = 0;
29  uint64_t live = 0;
30  uint64_t append = 0;
31  uint64_t liveBytes = 0;
32 };
33 
43 class MemBlob: public RefCountable
44 {
46 
47 public:
49  typedef uint32_t size_type;
50 
52  static const MemBlobStats& GetStats();
53 
55  explicit MemBlob(const size_type reserveSize);
56 
58  MemBlob(const char *buffer, const size_type bufferSize);
59 
60  ~MemBlob() override;
61 
63  size_type spaceSize() const { return capacity - size; }
64 
71  bool canAppend(const size_type off, const size_type n) const {
72  // TODO: ignore offset (and adjust size) when the blob is not shared?
73  return (isAppendOffset(off) && willFit(n)) || !n;
74  }
75 
81  void appended(const size_type n);
82 
90  void append(const char *source, const size_type n);
91 
92  /* non-const methods below require exclusive object ownership */
93 
95  void clear() { size = 0; }
96 
99  void syncSize(const size_type n);
100 
103  void consume(const size_type n);
104 
106  std::ostream & dump(std::ostream &os) const;
107 
108 public:
109  /* MemBlob users should avoid these and must treat them as read-only */
110  char *mem;
114 
115 private:
116  void memAlloc(const size_type memSize);
117 
119  bool isAppendOffset(const size_type off) const { return off == size; }
120 
122  bool willFit(const size_type n) const { return n <= spaceSize(); }
123 
124  /* copying is not implemented */
125  MemBlob(const MemBlob &);
126  MemBlob& operator =(const MemBlob &);
127 };
128 
129 #endif /* SQUID_SRC_SBUF_MEMBLOB_H */
130 
void syncSize(const size_type n)
Definition: MemBlob.cc:139
RefCount< MemBlob > Pointer
Definition: MemBlob.h:48
bool willFit(const size_type n) const
whether n more bytes can be appended
Definition: MemBlob.h:122
void clear()
extends the available space to the entire allocated blob
Definition: MemBlob.h:95
size_type spaceSize() const
the number unused bytes at the end of the allocated blob
Definition: MemBlob.h:63
size_type size
maximum allocated memory in use by callers
Definition: MemBlob.h:112
uint64_t append
number of MemBlob::append() calls
Definition: MemBlob.h:30
~MemBlob() override
Definition: MemBlob.cc:80
bool canAppend(const size_type off, const size_type n) const
Definition: MemBlob.h:71
MemBlob(const size_type reserveSize)
create a new MemBlob with at least reserveSize capacity
Definition: MemBlob.cc:60
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
MemBlob & operator=(const MemBlob &)
bool isAppendOffset(const size_type off) const
whether the offset points to the end of the used area
Definition: MemBlob.h:119
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:119
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
MemBlobStats & operator+=(const MemBlobStats &)
Definition: MemBlob.cc:22
uint32_t size_type
Definition: MemBlob.h:49
size_type capacity
size of the raw allocated memory block
Definition: MemBlob.h:111
std::ostream & dump(std::ostream &os) const
dump debugging information
Definition: MemBlob.cc:160
Various MemBlob class-wide statistics.
Definition: MemBlob.h:19
void append(const char *source, const size_type n)
Definition: MemBlob.cc:127
const InstanceId< MemBlob > id
blob identifier
Definition: MemBlob.h:113
void consume(const size_type n)
Definition: MemBlob.cc:148
MEMPROXY_CLASS(MemBlob)
char * mem
raw allocated memory block
Definition: MemBlob.h:110
void memAlloc(const size_type memSize)
Definition: MemBlob.cc:99

 

Introduction

Documentation

Support

Miscellaneous