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_MEMBLOB_H_
10#define SQUID_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
25 std::ostream& dump(std::ostream& os) const;
26
28
29public:
30 uint64_t alloc;
31 uint64_t live;
32 uint64_t append;
33 uint64_t liveBytes;
34};
35
45class MemBlob: public RefCountable
46{
48
49public:
51 typedef uint32_t size_type;
52
54 static const MemBlobStats& GetStats();
55
57 explicit MemBlob(const size_type reserveSize);
58
60 MemBlob(const char *buffer, const size_type bufferSize);
61
62 ~MemBlob() override;
63
65 size_type spaceSize() const { return capacity - size; }
66
73 bool canAppend(const size_type off, const size_type n) const {
74 // TODO: ignore offset (and adjust size) when the blob is not shared?
75 return (isAppendOffset(off) && willFit(n)) || !n;
76 }
77
83 void appended(const size_type n);
84
92 void append(const char *source, const size_type n);
93
94 /* non-const methods below require exclusive object ownership */
95
97 void clear() { size = 0; }
98
101 void syncSize(const size_type n);
102
105 void consume(const size_type n);
106
108 std::ostream & dump(std::ostream &os) const;
109
110public:
111 /* MemBlob users should avoid these and must treat them as read-only */
112 char *mem;
116
117private:
119
120 void memAlloc(const size_type memSize);
121
123 bool isAppendOffset(const size_type off) const { return off == size; }
124
126 bool willFit(const size_type n) const { return n <= spaceSize(); }
127
128 /* copying is not implemented */
129 MemBlob(const MemBlob &);
131};
132
133#endif /* SQUID_MEMBLOB_H_ */
134
#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: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
size_type spaceSize() const
the number unused bytes at the end of the allocated blob
Definition: MemBlob.h:65
char * mem
raw allocated memory block
Definition: MemBlob.h:112
bool isAppendOffset(const size_type off) const
whether the offset points to the end of the used area
Definition: MemBlob.h:123
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
MemBlob(const MemBlob &)
size_type size
maximum allocated memory in use by callers
Definition: MemBlob.h:114
static MemBlobStats Stats
class-wide statistics
Definition: MemBlob.h:118
void clear()
extends the available space to the entire allocated blob
Definition: MemBlob.h:97
static const MemBlobStats & GetStats()
obtain a const view of class-wide statistics
Definition: MemBlob.cc:149
bool canAppend(const size_type off, const size_type n) const
Definition: MemBlob.h:73
void memAlloc(const size_type memSize)
Definition: MemBlob.cc:89
void appended(const size_type n)
Definition: MemBlob.cc:108
const InstanceId< MemBlob > id
blob identifier
Definition: MemBlob.h:115
RefCount< MemBlob > Pointer
Definition: MemBlob.h:50
MemBlob & operator=(const MemBlob &)
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
MEMPROXY_CLASS(MemBlob)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors