MemBlob.h
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 #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 
50  /* emulate std::basic_string API */
51  using value_type = char;
52  using size_type = uint32_t;
53  using const_pointer = const value_type *;
54 
56  static const MemBlobStats& GetStats();
57 
59  explicit MemBlob(const size_type reserveSize);
60 
61  /* emulate std::basic_string API */
63  ~MemBlob() override;
64 
66  size_type spaceSize() const { return capacity - size; }
67 
74  bool canAppend(const size_type off, const size_type n) const {
75  // TODO: ignore offset (and adjust size) when the blob is not shared?
76  return (isAppendOffset(off) && willFit(n)) || !n;
77  }
78 
84  void appended(const size_type n);
85 
93  void append(const_pointer source, const size_type n);
94 
95  /* non-const methods below require exclusive object ownership */
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  /* emulate std::basic_string API */
109  void clear() { size = 0; }
110 
111 public:
112  /* MemBlob users should avoid these and must treat them as read-only */
113  value_type *mem = nullptr;
117 
118 private:
119  void memAlloc(const size_type memSize);
120 
122  bool isAppendOffset(const size_type off) const { return off == size; }
123 
125  bool willFit(const size_type n) const { return n <= spaceSize(); }
126 
127  /* copying is not implemented */
128  MemBlob(const MemBlob &);
129  MemBlob& operator =(const MemBlob &);
130 };
131 
132 #endif /* SQUID_SRC_SBUF_MEMBLOB_H */
133 
void syncSize(const size_type n)
Definition: MemBlob.cc:137
RefCount< MemBlob > Pointer
Definition: MemBlob.h:48
bool willFit(const size_type n) const
whether n more bytes can be appended
Definition: MemBlob.h:125
void clear()
Definition: MemBlob.h:109
size_type spaceSize() const
the number unused bytes at the end of the allocated blob
Definition: MemBlob.h:66
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
bool canAppend(const size_type off, const size_type n) const
Definition: MemBlob.h:74
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
MemBlob & operator=(const MemBlob &)
const value_type * const_pointer
Definition: MemBlob.h:53
bool isAppendOffset(const size_type off) const
whether the offset points to the end of the used area
Definition: MemBlob.h:122
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
std::ostream & dump(std::ostream &os) const
dump debugging information
Definition: MemBlob.cc:158
Various MemBlob class-wide statistics.
Definition: MemBlob.h:19
const InstanceId< MemBlob > id
blob identifier
Definition: MemBlob.h:116
void consume(const size_type n)
Definition: MemBlob.cc:146
MEMPROXY_CLASS(MemBlob)
void memAlloc(const size_type memSize)
Definition: MemBlob.cc:97

 

Introduction

Documentation

Support

Miscellaneous