MemBuf.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_MEMBUF_H
10#define SQUID_MEMBUF_H
11
12#include "base/Packable.h"
13#include "cbdata.h"
14#include "mem/forward.h"
15
16/* in case we want to change it later */
17typedef ssize_t mb_size_t;
18
23class MemBuf : public Packable
24{
26
27public:
29 buf(nullptr),
30 size(0),
31 max_capacity(0),
32 capacity(0),
33 stolen(0)
34 {}
35 ~MemBuf() override {
36 if (!stolen && buf)
37 clean();
38 }
39
41 char *content() { return buf; }
42
44 const char *content() const { return buf; }
45
47 mb_size_t contentSize() const { return size; }
48
54 bool hasContent() const { return size > 0; }
55
57 char *space() { return buf + size; }
58
63 char *space(mb_size_t required) { if (size + required >= capacity) grow(size + required + 1); return buf + size; }
64
65 mb_size_t spaceSize() const;
66
72 bool hasSpace() const { return size+1 < capacity; }
73
74 mb_size_t potentialSpaceSize() const; // accounts for possible growth
75 bool hasPotentialSpace() const { return potentialSpaceSize() > 0; }
76
78
79 void consume(mb_size_t sz); // removes sz bytes, moving content left
81
82 void appended(mb_size_t sz); // updates content size after external append
83 void truncate(mb_size_t sz); // removes sz last bytes
84
85 void terminate(); // zero-terminates the buffer w/o increasing contentSize
86
87 bool wasStolen() const { return stolen; }
88
90 void init(mb_size_t szInit, mb_size_t szMax);
91
93 void init();
94
96 void clean();
97
99 void reset();
100
102 int isNull() const;
103
109 FREE *freeFunc();
110
111 /* Packable API */
112 void append(const char *c, int sz) override;
113 void vappendf(const char *fmt, va_list ap) override;
114
115private:
120 MemBuf(const MemBuf &) {assert(false);}
121
122 MemBuf& operator= (const MemBuf &) {assert(false); return *this;}
123
124 void grow(mb_size_t min_cap);
125
126public:
134 char *buf; // available content
135 mb_size_t size; // used space, does not count 0-terminator
136
143
150
151 unsigned stolen:1; /* the buffer has been stolen for use by someone else */
152
153#if 0
154
155 unsigned valid:1; /* to be used for debugging only! */
156#endif
157};
158
160void memBufReport(MemBuf * mb);
161
162#endif /* SQUID_MEMBUF_H */
163
ssize_t mb_size_t
Definition: MemBuf.h:17
void memBufReport(MemBuf *mb)
Definition: MemBuf.cc:359
#define assert(EX)
Definition: assert.h:17
#define CBDATA_CLASS(type)
Definition: cbdata.h:289
Definition: MemBuf.h:24
mb_size_t spaceSize() const
Definition: MemBuf.cc:155
void init()
Definition: MemBuf.cc:86
void clean()
Definition: MemBuf.cc:110
MemBuf(const MemBuf &)
Definition: MemBuf.h:120
void truncate(mb_size_t sz)
Definition: MemBuf.cc:197
void append(const char *c, int sz) override
Definition: MemBuf.cc:209
char * space()
returns buffer after data; does not check space existence
Definition: MemBuf.h:57
FREE * freeFunc()
Definition: MemBuf.cc:303
bool wasStolen() const
Definition: MemBuf.h:87
mb_size_t size
Definition: MemBuf.h:135
MemBuf()
Definition: MemBuf.h:28
char * buf
Definition: MemBuf.h:134
bool hasSpace() const
Definition: MemBuf.h:72
char * content()
start of the added data
Definition: MemBuf.h:41
char * space(mb_size_t required)
Definition: MemBuf.h:63
void grow(mb_size_t min_cap)
Definition: MemBuf.cc:318
MemBuf & operator=(const MemBuf &)
Definition: MemBuf.h:122
~MemBuf() override
Definition: MemBuf.h:35
mb_size_t max_capacity
Definition: MemBuf.h:142
const char * content() const
start of the added data
Definition: MemBuf.h:44
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
mb_size_t potentialSpaceSize() const
Definition: MemBuf.cc:161
void consume(mb_size_t sz)
removes sz bytes and "packs" by moving content left
Definition: MemBuf.cc:168
int isNull() const
Definition: MemBuf.cc:145
void reset()
Definition: MemBuf.cc:129
bool hasContent() const
Definition: MemBuf.h:54
mb_size_t capacity
Definition: MemBuf.h:149
void appended(mb_size_t sz)
updates content size after external append
Definition: MemBuf.cc:226
bool hasPotentialSpace() const
Definition: MemBuf.h:75
void terminate()
Definition: MemBuf.cc:241
unsigned stolen
Definition: MemBuf.h:151
void vappendf(const char *fmt, va_list ap) override
Definition: MemBuf.cc:251
void consumeWhitespacePrefix()
removes all prefix whitespace, moving content left
Definition: MemBuf.cc:185
void FREE(void *)
Definition: forward.h:37

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors