]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBuf.h
00716f05d147443dd0d9ff8774a3890bd913519e
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
10 #define SQUID_MEMBUF_H
12 #include "base/Packable.h"
14 #include "mem/forward.h"
16 /* in case we want to change it later */
17 typedef ssize_t mb_size_t
;
20 * Auto-growing memory-resident buffer with Packable interface
21 * \deprecated Use SBuf instead.
23 class MemBuf
: public Packable
40 /// start of the added data
41 char *content() { return buf
; }
43 /// start of the added data
44 const char *content() const { return buf
; }
46 /// available data size
47 mb_size_t
contentSize() const { return size
; }
50 * Whether the buffer contains any data.
51 \retval true if data exists in the buffer
52 \retval false if data exists in the buffer
54 bool hasContent() const { return size
> 0; }
56 /// returns buffer after data; does not check space existence
57 char *space() { return buf
+ size
; } ///< space to add data
59 /// Returns buffer following data, after possibly growing the buffer to
60 /// accommodate addition of the required bytes PLUS a 0-terminator char.
61 /// The caller is not required to terminate the buffer, but MemBuf does
62 /// terminate internally, trading termination for size calculation bugs.
63 char *space(mb_size_t required
) { if (size
+ required
>= capacity
) grow(size
+ required
+ 1); return buf
+ size
; }
65 mb_size_t
spaceSize() const;
68 * Whether the buffer contains any data space available.
69 \retval true if data can be added to the buffer
70 \retval false if the buffer is full
72 bool hasSpace() const { return size
+1 < capacity
; }
74 mb_size_t
potentialSpaceSize() const; // accounts for possible growth
75 bool hasPotentialSpace() const { return potentialSpaceSize() > 0; }
77 /// \note there is currently no stretch() method to grow without appending
79 void consume(mb_size_t sz
); // removes sz bytes, moving content left
80 void consumeWhitespacePrefix(); ///< removes all prefix whitespace, moving content left
82 void appended(mb_size_t sz
); // updates content size after external append
83 void truncate(mb_size_t sz
); // removes sz last bytes
85 void terminate(); // zero-terminates the buffer w/o increasing contentSize
87 bool wasStolen() const { return stolen
; }
89 /** init with specific sizes */
90 void init(mb_size_t szInit
, mb_size_t szMax
);
92 /** init with defaults */
95 /** cleans mb; last function to call if you do not give .buf away */
98 /** resets mb preserving (or initializing if needed) memory buffer */
101 /** unfirtunate hack to test if the buffer has been Init()ialized */
105 * freezes the object! and returns function to clear it up.
107 \retval free() function to be used.
112 virtual void append(const char *c
, int sz
);
113 virtual void vappendf(const char *fmt
, va_list ap
);
117 * private copy constructor and assignment operator generates
118 * compiler errors if someone tries to copy/assign a MemBuf
120 MemBuf(const MemBuf
&) {assert(false);}
122 MemBuf
& operator= (const MemBuf
&) {assert(false); return *this;}
124 void grow(mb_size_t min_cap
);
128 \deprecated use space*() and content*() methods to access safely instead.
131 * TODO: hide these members completely and remove 0-termination
132 * so that consume() does not need to memmove all the time
134 char *buf
; // available content
135 mb_size_t size
; // used space, does not count 0-terminator
138 * when grows: assert(new_capacity <= max_capacity)
139 * \deprecated Use interface function instead
140 * TODO: make these private after converting memBuf*() functions to methods
142 mb_size_t max_capacity
;
146 * \deprecated Use interface function instead
147 * TODO: make these private after converting memBuf*() functions to methods
151 unsigned stolen
:1; /* the buffer has been stolen for use by someone else */
155 unsigned valid
:1; /* to be used for debugging only! */
159 /** returns free() function to be used, _freezes_ the object! */
160 void memBufReport(MemBuf
* mb
);
162 #endif /* SQUID_MEMBUF_H */