Squid Web Cache master
Loading...
Searching...
No Matches
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{
21public:
23 std::ostream& dump(std::ostream& os) const;
24
26
27public:
28 uint64_t alloc = 0;
29 uint64_t live = 0;
30 uint64_t append = 0;
31 uint64_t liveBytes = 0;
32};
33
43class MemBlob: public RefCountable
44{
46
47public:
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
111public:
112 /* MemBlob users should avoid these and must treat them as read-only */
113 value_type *mem = nullptr;
117
118private:
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 &);
130};
131
132#endif /* SQUID_SRC_SBUF_MEMBLOB_H */
133
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition Lock.h:66
Various MemBlob class-wide statistics.
Definition MemBlob.h:20
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
uint64_t alloc
number of MemBlob instances created so far
Definition MemBlob.h:28
uint64_t append
number of MemBlob::append() calls
Definition MemBlob.h:30
MemBlobStats & operator+=(const MemBlobStats &)
Definition MemBlob.cc:22
std::ostream & dump(std::ostream &os) const
dumps class-wide statistics
Definition MemBlob.cc:33
size_type spaceSize() const
the number unused bytes at the end of the allocated blob
Definition MemBlob.h:66
bool isAppendOffset(const size_type off) const
whether the offset points to the end of the used area
Definition MemBlob.h:122
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
value_type * mem
raw allocated memory block
Definition MemBlob.h:113
MemBlob(const MemBlob &)
size_type size
maximum allocated memory in use by callers
Definition MemBlob.h:115
void clear()
Definition MemBlob.h:109
static const MemBlobStats & GetStats()
obtain a const view of class-wide statistics
Definition MemBlob.cc:53
bool canAppend(const size_type off, const size_type n) const
Definition MemBlob.h:74
void memAlloc(const size_type memSize)
Definition MemBlob.cc:97
void appended(const size_type n)
Definition MemBlob.cc:117
char value_type
Definition MemBlob.h:51
const InstanceId< MemBlob > id
blob identifier
Definition MemBlob.h:116
RefCount< MemBlob > Pointer
Definition MemBlob.h:48
MemBlob & operator=(const MemBlob &)
void append(const_pointer source, const size_type n)
Definition MemBlob.cc:125
~MemBlob() override
Definition MemBlob.cc:78
std::ostream & dump(std::ostream &os) const
dump debugging information
Definition MemBlob.cc:158
uint32_t size_type
Definition MemBlob.h:52
void consume(const size_type n)
Definition MemBlob.cc:146
size_type capacity
size of the raw allocated memory block
Definition MemBlob.h:114
const value_type * const_pointer
Definition MemBlob.h:53
MEMPROXY_CLASS(MemBlob)