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_SRC_MEMBUF_H
10 #define SQUID_SRC_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 */
17 typedef ssize_t mb_size_t;
18 
23 class MemBuf : public Packable
24 {
26 
27 public:
28  MemBuf():
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 
115 private:
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 
126 public:
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 
160 void memBufReport(MemBuf * mb);
161 
162 #endif /* SQUID_SRC_MEMBUF_H */
163 
char * buf
Definition: MemBuf.h:134
void terminate()
Definition: MemBuf.cc:241
FREE * freeFunc()
Definition: MemBuf.cc:303
void consumeWhitespacePrefix()
removes all prefix whitespace, moving content left
Definition: MemBuf.cc:185
void FREE(void *)
Definition: forward.h:37
mb_size_t size
Definition: MemBuf.h:135
bool hasPotentialSpace() const
Definition: MemBuf.h:75
bool hasContent() const
Definition: MemBuf.h:54
#define CBDATA_CLASS(type)
Definition: cbdata.h:289
bool wasStolen() const
Definition: MemBuf.h:87
MemBuf()
Definition: MemBuf.h:28
mb_size_t max_capacity
Definition: MemBuf.h:142
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
int isNull() const
Definition: MemBuf.cc:145
void append(const char *c, int sz) override
Definition: MemBuf.cc:209
mb_size_t capacity
Definition: MemBuf.h:149
Definition: MemBuf.h:23
void clean()
Definition: MemBuf.cc:110
MemBuf(const MemBuf &)
Definition: MemBuf.h:120
const char * content() const
start of the added data
Definition: MemBuf.h:44
unsigned stolen
Definition: MemBuf.h:151
#define assert(EX)
Definition: assert.h:17
~MemBuf() override
Definition: MemBuf.h:35
void appended(mb_size_t sz)
updates content size after external append
Definition: MemBuf.cc:226
MemBuf & operator=(const MemBuf &)
Definition: MemBuf.h:122
void vappendf(const char *fmt, va_list ap) override
Definition: MemBuf.cc:251
void init()
Definition: MemBuf.cc:86
mb_size_t potentialSpaceSize() const
Definition: MemBuf.cc:161
void truncate(mb_size_t sz)
Definition: MemBuf.cc:197
char * content()
start of the added data
Definition: MemBuf.h:41
void memBufReport(MemBuf *mb)
Definition: MemBuf.cc:359
mb_size_t spaceSize() const
Definition: MemBuf.cc:155
bool hasSpace() const
Definition: MemBuf.h:72
void reset()
Definition: MemBuf.cc:129
ssize_t mb_size_t
Definition: MemBuf.h:17
void consume(mb_size_t sz)
removes sz bytes and "packs" by moving content left
Definition: MemBuf.cc:168
void grow(mb_size_t min_cap)
Definition: MemBuf.cc:318
char * space(mb_size_t required)
Definition: MemBuf.h:63

 

Introduction

Documentation

Support

Miscellaneous