MemBuf.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 */
17 typedef ssize_t mb_size_t;
18 
23 class MemBuf : public Packable
24 {
26 
27 public:
28  MemBuf():
29  buf(NULL),
30  size(0),
31  max_capacity(0),
32  capacity(0),
33  stolen(0)
34  {}
35  virtual ~MemBuf() {
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  virtual void append(const char *c, int sz);
113  virtual void vappendf(const char *fmt, va_list ap);
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_MEMBUF_H */
163 
FREE * freeFunc()
Definition: MemBuf.cc:328
void grow(mb_size_t min_cap)
Definition: MemBuf.cc:343
bool hasSpace() const
Definition: MemBuf.h:72
#define assert(EX)
Definition: assert.h:17
mb_size_t spaceSize() const
Definition: MemBuf.cc:167
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:225
bool wasStolen() const
Definition: MemBuf.h:87
void consumeWhitespacePrefix()
removes all prefix whitespace, moving content left
Definition: MemBuf.cc:199
mb_size_t potentialSpaceSize() const
Definition: MemBuf.cc:173
void consume(mb_size_t sz)
removes sz bytes and "packs" by moving content left
Definition: MemBuf.cc:180
MemBuf(const MemBuf &)
Definition: MemBuf.h:120
void FREE(void *)
Definition: forward.h:36
mb_size_t capacity
Definition: MemBuf.h:149
virtual void vappendf(const char *fmt, va_list ap)
Definition: MemBuf.cc:269
bool hasContent() const
Definition: MemBuf.h:54
char * space()
returns buffer after data; does not check space existence
Definition: MemBuf.h:57
const char * content() const
start of the added data
Definition: MemBuf.h:44
MemBuf & operator=(const MemBuf &)
Definition: MemBuf.h:122
void reset()
Definition: MemBuf.cc:141
mb_size_t size
Definition: MemBuf.h:135
#define CBDATA_CLASS(type)
Definition: cbdata.h:302
char * content()
start of the added data
Definition: MemBuf.h:41
void appended(mb_size_t sz)
updates content size after external append
Definition: MemBuf.cc:244
char * space(mb_size_t required)
Definition: MemBuf.h:63
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
void memBufReport(MemBuf *mb)
Definition: MemBuf.cc:387
void clean()
Definition: MemBuf.cc:122
unsigned stolen
Definition: MemBuf.h:151
char * buf
Definition: MemBuf.h:134
int isNull() const
Definition: MemBuf.cc:157
bool hasPotentialSpace() const
Definition: MemBuf.h:75
mb_size_t max_capacity
Definition: MemBuf.h:142
ssize_t mb_size_t
Definition: MemBuf.h:17
Definition: MemBuf.h:23
void init()
Definition: MemBuf.cc:98
void truncate(mb_size_t sz)
Definition: MemBuf.cc:213
virtual ~MemBuf()
Definition: MemBuf.h:35
#define NULL
Definition: types.h:166
MemBuf()
Definition: MemBuf.h:28
void terminate()
Definition: MemBuf.cc:259

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors