]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBuf.h
2 * Copyright (C) 1996-2014 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
16 * Auto-growing memory-resident buffer with printf interface
17 * \deprecated Use SBuf instead.
36 /// start of the added data
37 char *content() { return buf
; }
39 /// start of the added data
40 const char *content() const { return buf
; }
42 /// available data size
43 mb_size_t
contentSize() const { return size
; }
46 * Whether the buffer contains any data.
47 \retval true if data exists in the buffer
48 \retval false if data exists in the buffer
50 bool hasContent() const { return size
> 0; }
52 /// returns buffer after data; does not check space existence
53 char *space() { return buf
+ size
; } ///< space to add data
55 /// Returns buffer following data, after possibly growing the buffer to
56 /// accommodate addition of the required bytes PLUS a 0-terminator char.
57 /// The caller is not required to terminate the buffer, but MemBuf does
58 /// terminate internally, trading termination for size calculation bugs.
59 char *space(mb_size_t required
) { if (size
+ required
>= capacity
) grow(size
+ required
+ 1); return buf
+ size
; }
61 mb_size_t
spaceSize() const;
64 * Whether the buffer contains any data space available.
65 \retval true if data can be added to the buffer
66 \retval false if the buffer is full
68 bool hasSpace() const { return size
+1 < capacity
; }
70 mb_size_t
potentialSpaceSize() const; // accounts for possible growth
71 bool hasPotentialSpace() const { return potentialSpaceSize() > 0; }
73 /// \note there is currently no stretch() method to grow without appending
75 void consume(mb_size_t sz
); // removes sz bytes, moving content left
76 void consumeWhitespacePrefix(); ///< removes all prefix whitespace, moving content left
78 void append(const char *c
, mb_size_t sz
); // grows if needed and possible
79 void appended(mb_size_t sz
); // updates content size after external append
80 void truncate(mb_size_t sz
); // removes sz last bytes
82 void terminate(); // zero-terminates the buffer w/o increasing contentSize
84 bool wasStolen() const { return stolen
; }
86 /** init with specific sizes */
87 void init(mb_size_t szInit
, mb_size_t szMax
);
89 /** init with defaults */
92 /** cleans mb; last function to call if you do not give .buf away */
95 /** resets mb preserving (or initializing if needed) memory buffer */
98 /** unfirtunate hack to test if the buffer has been Init()ialized */
102 * calls snprintf, extends buffer if needed
103 \note we use Printf instead of printf so the compiler won't
104 * think we're calling the libc printf()
106 void Printf(const char *fmt
,...) PRINTF_FORMAT_ARG2
;
108 /** vPrintf for other printf()'s to use */
109 void vPrintf(const char *fmt
, va_list ap
);
112 * freezes the object! and returns function to clear it up.
114 \retval free() function to be used.
120 * private copy constructor and assignment operator generates
121 * compiler errors if someone tries to copy/assign a MemBuf
123 MemBuf(const MemBuf
& m
) {assert(false);};
125 MemBuf
& operator= (const MemBuf
& m
) {assert(false); return *this;};
127 void grow(mb_size_t min_cap
);
131 \deprecated use space*() and content*() methods to access safely instead.
134 * TODO: hide these members completely and remove 0-termination
135 * so that consume() does not need to memmove all the time
137 char *buf
; // available content
138 mb_size_t size
; // used space, does not count 0-terminator
141 * when grows: assert(new_capacity <= max_capacity)
142 * \deprecated Use interface function instead
143 * TODO: make these private after converting memBuf*() functions to methods
145 mb_size_t max_capacity
;
149 * \deprecated Use interface function instead
150 * TODO: make these private after converting memBuf*() functions to methods
154 unsigned stolen
:1; /* the buffer has been stolen for use by someone else */
158 unsigned valid
:1; /* to be used for debugging only! */
162 /** returns free() function to be used, _freezes_ the object! */
163 void memBufReport(MemBuf
* mb
);
164 /** pack content into a mem buf. */
165 void packerToMemInit(Packer
* p
, MemBuf
* mb
);
167 #endif /* SQUID_MEMBUF_H */