]> git.ipfire.org Git - thirdparty/squid.git/blame - src/SBuf.h
Various audit updates
[thirdparty/squid.git] / src / SBuf.h
CommitLineData
412da427 1/*
412da427
FC
2 * SQUID Web Proxy Cache http://www.squid-cache.org/
3 * ----------------------------------------------------------
4 *
5 * Squid is the result of efforts by numerous individuals from
6 * the Internet community; see the CONTRIBUTORS file for full
7 * details. Many organizations have provided support for Squid's
8 * development; see the SPONSORS file for full details. Squid is
9 * Copyrighted (C) 2001 by the Regents of the University of
10 * California; see the COPYRIGHT file for full details. Squid
11 * incorporates software developed and/or copyrighted by other
12 * sources; see the CREDITS file for full details.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 */
28
29#ifndef SQUID_SBUF_H
30#define SQUID_SBUF_H
31
32#include "base/InstanceId.h"
412da427 33#include "MemBlob.h"
62842d40 34#include "SBufExceptions.h"
412da427
FC
35#include "SquidString.h"
36
62842d40 37#include <climits>
074d6a40
AJ
38#include <cstdarg>
39#include <iosfwd>
412da427
FC
40#if HAVE_UNISTD_H
41#include <unistd.h>
42#endif
412da427 43
496a9e97 44/* SBuf placeholder for printf */
412da427
FC
45#ifndef SQUIDSBUFPH
46#define SQUIDSBUFPH "%.*s"
47#define SQUIDSBUFPRINT(s) (s).plength(),(s).rawContent()
48#endif /* SQUIDSBUFPH */
49
6a17a36d 50// TODO: move within SBuf and rename
412da427
FC
51typedef enum {
52 caseSensitive,
53 caseInsensitive
54} SBufCaseSensitive;
55
56/**
57 * Container for various SBuf class-wide statistics.
58 *
59 * The stats are not completely accurate; they're mostly meant to
60 * understand whether Squid is leaking resources
61 * and whether SBuf is paying off the expected gains.
62 */
63class SBufStats
64{
65public:
7cbd3256
FC
66 uint64_t alloc; ///<number of calls to SBuf constructors
67 uint64_t allocCopy; ///<number of calls to SBuf copy-constructor
68 uint64_t allocFromString; ///<number of copy-allocations from Strings
69 uint64_t allocFromCString; ///<number of copy-allocations from c-strings
70 uint64_t assignFast; ///<number of no-copy assignment operations
71 uint64_t clear; ///<number of clear operations
72 uint64_t append; ///<number of append operations
73 uint64_t toStream; ///<number of write operations to ostreams
74 uint64_t setChar; ///<number of calls to setAt
75 uint64_t getChar; ///<number of calls to at() and operator[]
76 uint64_t compareSlow; ///<number of comparison operations requiring data scan
77 uint64_t compareFast; ///<number of comparison operations not requiring data scan
78 uint64_t copyOut; ///<number of data-copies to other forms of buffers
79 uint64_t rawAccess; ///<number of accesses to raw contents
7f175a76 80 uint64_t nulTerminate; ///<number of c_str() terminations
7cbd3256
FC
81 uint64_t chop; ///<number of chop operations
82 uint64_t trim; ///<number of trim operations
83 uint64_t find; ///<number of find operations
84 uint64_t scanf; ///<number of scanf operations
85 uint64_t caseChange; ///<number of toUpper and toLower operations
86 uint64_t cowFast; ///<number of cow operations not actually requiring a copy
87 uint64_t cowSlow; ///<number of cow operations requiring a copy
88 uint64_t live; ///<number of currently-allocated SBuf
412da427 89
496a9e97 90 ///Dump statistics to an ostream.
412da427
FC
91 std::ostream& dump(std::ostream &os) const;
92 SBufStats();
93
94 SBufStats& operator +=(const SBufStats&);
95};
96
4685b0c4
FC
97class CharacterSet;
98
412da427
FC
99/**
100 * A String or Buffer.
101 * Features: refcounted backing store, cheap copy and sub-stringing
102 * operations, copy-on-write to isolate change operations to each instance.
103 * Where possible, we're trying to mimic std::string's interface.
104 */
105class SBuf
106{
107public:
7cbd3256 108 typedef MemBlob::size_type size_type;
50827187 109 static const size_type npos = 0xffffffff; // max(uint32_t)
412da427
FC
110
111 /// Maximum size of a SBuf. By design it MUST be < MAX(size_type)/2. Currently 256Mb.
112 static const size_type maxSize = 0xfffffff;
113
114 /// create an empty (zero-size) SBuf
115 SBuf();
116 SBuf(const SBuf &S);
117
118 /** Constructor: import c-style string
119 *
120 * Create a new SBuf containing a COPY of the contents of the
121 * c-string
122 * \param S the c string to be copied
7cbd3256 123 * \param n how many bytes to import into the SBuf. If it is npos
412da427
FC
124 * or unspecified, imports to end-of-cstring
125 * \note it is the caller's responsibility not to go out of bounds
50827187 126 * \note bounds is 0 <= pos < length(); caller must pay attention to signedness
412da427 127 */
7cbd3256 128 explicit SBuf(const char *S, size_type n = npos);
412da427
FC
129
130 /** Constructor: import SquidString, copying contents.
131 *
132 * This method will be removed once SquidString has gone.
133 */
496a9e97
FC
134 explicit SBuf(const String &S);
135
136 /// Constructor: import std::string. Contents are copied.
137 explicit SBuf(const std::string &s);
412da427
FC
138
139 ~SBuf();
62842d40 140
412da427
FC
141 /** Explicit assignment.
142 *
143 * Current SBuf will share backing store with the assigned one.
144 */
145 SBuf& assign(const SBuf &S);
62842d40 146
412da427
FC
147 /** Assignment operator.
148 *
149 * Current SBuf will share backing store with the assigned one.
150 */
62842d40 151 SBuf& operator =(const SBuf & S) {return assign(S);}
412da427
FC
152
153 /** Import a c-string into a SBuf, copying the data.
154 *
155 * It is the caller's duty to free the imported string, if needed.
156 * \param S the c string to be copied
7cbd3256 157 * \param n how many bytes to import into the SBuf. If it is npos
412da427
FC
158 * or unspecified, imports to end-of-cstring
159 * \note it is the caller's responsibility not to go out of bounds
7cbd3256
FC
160 * \note to assign a std::string use the pattern:
161 * assign(stdstr.data(), stdstd.length())
412da427 162 */
7cbd3256 163 SBuf& assign(const char *S, size_type n = npos);
412da427
FC
164
165 /** Assignment operator. Copy a NULL-terminated c-style string into a SBuf.
166 *
167 * Copy a c-style string into a SBuf. Shortcut for SBuf.assign(S)
168 * It is the caller's duty to free the imported string, if needed.
7cbd3256 169 * \note not \0-clean
412da427 170 */
62842d40 171 SBuf& operator =(const char *S) {return assign(S);}
412da427 172
412da427
FC
173 /** reset the SBuf as if it was just created.
174 *
175 * Resets the SBuf to empty, memory is freed lazily.
176 */
177 void clear();
178
179 /** Append operation
180 *
181 * Append the supplied SBuf to the current one; extend storage as needed.
182 */
183 SBuf& append(const SBuf & S);
184
185 /** Append operation for C-style strings.
186 *
187 * Append the supplied c-string to the SBuf; extend storage
188 * as needed.
189 *
190 * \param S the c string to be copied. Can be NULL.
a452bc8b
FC
191 * \param Ssize how many bytes to import into the SBuf. If it is npos
192 * or unspecified, imports to end-of-cstring. If S is NULL,
193 * Ssize is ignored.
7cbd3256
FC
194 * \note to append a std::string use the pattern
195 * cstr_append(stdstr.data(), stdstd.length())
412da427 196 */
7cbd3256 197 SBuf& append(const char * S, size_type Ssize = npos);
412da427
FC
198
199 /** Assignment operation with printf(3)-style definition
200 * \note arguments may be evaluated more than once, be careful
201 * of side-effects
202 */
203 SBuf& Printf(const char *fmt, ...);
204
205 /** Append operation with printf-style arguments
206 * \note arguments may be evaluated more than once, be careful
207 * of side-effects
208 */
209 SBuf& appendf(const char *fmt, ...);
496a9e97 210
412da427
FC
211 /** Append operation, with vsprintf(3)-style arguments.
212 * \note arguments may be evaluated more than once, be careful
213 * of side-effects
214 */
215 SBuf& vappendf(const char *fmt, va_list vargs);
216
496a9e97 217 /// print the SBuf contents to the supplied ostream
412da427
FC
218 std::ostream& print(std::ostream &os) const;
219
496a9e97 220 /** print SBuf contents and debug information about the SBuf to an ostream
412da427
FC
221 *
222 * Debug function, dumps to a stream informations on the current SBuf,
223 * including low-level details and statistics.
224 */
225 std::ostream& dump(std::ostream &os) const;
226
227 /** random-access read to any char within the SBuf
228 *
229 * does not check access bounds. If you need that, use at()
230 */
fdd1982c 231 char operator [](size_type pos) const {++stats.getChar; return store_->mem[off_+pos];}
412da427
FC
232
233 /** random-access read to any char within the SBuf.
234 *
235 * \throw OutOfBoundsException when access is out of bounds
50827187 236 * \note bounds is 0 <= pos < length(); caller must pay attention to signedness
412da427 237 */
fdd1982c 238 char at(size_type pos) const {checkAccessBounds(pos); return operator[](pos);}
412da427
FC
239
240 /** direct-access set a byte at a specified operation.
241 *
242 * \param pos the position to be overwritten
243 * \param toset the value to be written
244 * \throw OutOfBoundsException when pos is of bounds
50827187 245 * \note bounds is 0 <= pos < length(); caller must pay attention to signedness
412da427
FC
246 * \note performs a copy-on-write if needed.
247 */
248 void setAt(size_type pos, char toset);
249
250 /** compare to other SBuf, str(case)cmp-style
251 *
252 * \param isCaseSensitive one of caseSensitive or caseInsensitive
7cbd3256 253 * \param n compare up to this many bytes. if npos (default), compare whole SBufs
412da427
FC
254 * \retval >0 argument of the call is greater than called SBuf
255 * \retval <0 argument of the call is smaller than called SBuf
256 * \retval 0 argument of the call has the same contents of called SBuf
257 */
1284f77c 258 int compare(const SBuf &S, const SBufCaseSensitive isCaseSensitive, const size_type n = npos) const;
7cbd3256 259
1284f77c
AJ
260 /// shorthand version for compare()
261 inline int cmp(const SBuf &S, const size_type n = npos) const {
7cbd3256
FC
262 return compare(S,caseSensitive,n);
263 }
264
1284f77c
AJ
265 /// shorthand version for case-insensitive compare()
266 inline int caseCmp(const SBuf &S, const size_type n = npos) const {
267 return compare(S,caseInsensitive,n);
268 }
269
270 /// Comparison with a C-string.
271 int compare(const char *s, const SBufCaseSensitive isCaseSensitive, const size_type n = npos) const;
272
273 /// Shorthand version for C-string compare().
274 inline int cmp(const char *S, const size_type n = npos) const {
275 return compare(S,caseSensitive,n);
276 }
277
278 /// Shorthand version for case-insensitive C-string compare().
279 inline int caseCmp(const char *S, const size_type n = npos) const {
7cbd3256
FC
280 return compare(S,caseInsensitive,n);
281 }
412da427
FC
282
283 /** check whether the entire supplied argument is a prefix of the SBuf.
284 * \param S the prefix to match against
285 * \param isCaseSensitive one of caseSensitive or caseInsensitive
286 * \retval true argument is a prefix of the SBuf
287 */
1284f77c 288 bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive = caseSensitive) const;
412da427 289
412da427
FC
290 bool operator ==(const SBuf & S) const;
291 bool operator !=(const SBuf & S) const;
7cbd3256
FC
292 bool operator <(const SBuf &S) const {return (cmp(S) < 0);}
293 bool operator >(const SBuf &S) const {return (cmp(S) > 0);}
294 bool operator <=(const SBuf &S) const {return (cmp(S) <= 0);}
295 bool operator >=(const SBuf &S) const {return (cmp(S) >= 0);}
412da427
FC
296
297 /** Consume bytes at the head of the SBuf
298 *
299 * Consume N chars at SBuf head, or to SBuf's end,
300 * whichever is shorter. If more bytes are consumed than available,
301 * the SBuf is emptied
302 * \param n how many bytes to remove; could be zero.
7cbd3256 303 * npos (or no argument) means 'to the end of SBuf'
412da427
FC
304 * \return a new SBuf containing the consumed bytes.
305 */
306 SBuf consume(size_type n = npos);
307
496a9e97 308 /// gets global statistic informations
412da427
FC
309 static const SBufStats& GetStats();
310
311 /** Copy SBuf contents into user-supplied C buffer.
312 *
313 * Export a copy of the SBuf's contents into the user-supplied
314 * buffer, up to the user-supplied-length. No zero-termination is performed
315 * \return num the number of actually-copied chars.
316 */
317 size_type copy(char *dest, size_type n) const;
318
319 /** exports a pointer to the SBuf internal storage.
320 * \warning ACCESSING RAW STORAGE IS DANGEROUS!
321 *
496a9e97
FC
322 * Returns a ead-only pointer to SBuf's content. No terminating null
323 * character is appended (use c_str() for that).
412da427
FC
324 * The returned value points to an internal location whose contents
325 * are guaranteed to remain unchanged only until the next call
326 * to a non-constant member function of the SBuf object. Such a
327 * call may be implicit (e.g., when SBuf is destroyed
328 * upon leaving the current context).
329 * This is a very UNSAFE way of accessing the data.
330 * This call never returns NULL.
331 * \see c_str
332 * \note the memory management system guarantees that the exported region
333 * of memory will remain valid if the caller keeps holding
334 * a valid reference to the SBuf object and does not write or append to
335 * it. For example:
336 * \code
337 * SBuf foo("some string");
338 * const char *bar = foo.rawContent();
339 * doSomething(bar); //safe
340 * foo.append(" other string");
341 * doSomething(bar); //unsafe
342 * \endcode
343 */
344 const char* rawContent() const;
345
346 /** Exports a writable pointer to the SBuf internal storage.
347 * \warning Use with EXTREME caution, this is a dangerous operation.
348 *
349 * Returns a pointer to the first unused byte in the SBuf's storage,
ac7947fd
FC
350 * which can be be used for appending. At least minSize bytes will
351 * be available for writing.
352 * The returned pointer must not be stored by the caller, as it will
496a9e97 353 * be invalidated by the first call to a non-const method call
412da427 354 * on the SBuf.
ac7947fd 355 * This call guarantees to never return NULL.
8aa34dad
FC
356 * \see reserveSpace
357 * \note Unlike reserveSpace(), this method does not guarantee exclusive
358 * buffer ownership. It is instead optimized for a one writer
359 * (appender), many readers scenario by avoiding unnecessary
360 * copying and allocations.
412da427
FC
361 * \throw SBufTooBigException if the user tries to allocate too big a SBuf
362 */
ac7947fd 363 char *rawSpace(size_type minSize);
412da427 364
50827187 365 /** Obtain how much free space is available in the backing store.
8aa34dad 366 *
50827187
FC
367 * \note: unless the client just cow()ed, it is not guaranteed that
368 * the free space can be used.
8aa34dad
FC
369 */
370 size_type spaceSize() const { return store_->spaceSize(); }
371
412da427
FC
372 /** Force a SBuf's size
373 * \warning use with EXTREME caution, this is a dangerous operation
374 *
375 * Adapt the SBuf internal state after external interference
376 * such as writing into it via rawSpace.
7cbd3256
FC
377 * \throw TextException if SBuf doesn't have exclusive ownership of store
378 * \throw SBufTooBigException if new size is bigger than available store space
412da427
FC
379 */
380 void forceSize(size_type newSize);
381
382 /** exports a null-terminated reference to the SBuf internal storage.
383 * \warning ACCESSING RAW STORAGE IS DANGEROUS! DO NOT EVER USE
384 * THE RETURNED POINTER FOR WRITING
385 *
386 * The returned value points to an internal location whose contents
387 * are guaranteed to remain unchanged only until the next call
388 * to a non-constant member function of the SBuf object. Such a
389 * call may be implicit (e.g., when SBuf is destroyed
390 * upon leaving the current context).
391 * This is a very UNSAFE way of accessing the data.
392 * This call never returns NULL.
393 * \see rawContent
394 * \note the memory management system guarantees that the exported region
496a9e97
FC
395 * of memory will remain valid will remain valid only if the
396 * caller keeps holding a valid reference to the SBuf object and
397 * does not write or append to it
412da427
FC
398 */
399 const char* c_str();
400
496a9e97 401 /// Returns the number of bytes stored in SBuf.
62842d40 402 size_type length() const {return len_;}
412da427
FC
403
404 /** Get the length of the SBuf, as a signed integer
405 *
406 * Compatibility function for printf(3) which requires a signed int
407 * \throw SBufTooBigException if the SBuf is too big for a signed integer
408 */
62842d40
FC
409 int plength() const {
410 if (length()>INT_MAX)
411 throw SBufTooBigException(__FILE__, __LINE__);
412 return static_cast<int>(length());
413 }
412da427
FC
414
415 /** Check whether the SBuf is empty
416 *
417 * \return true if length() == 0
418 */
62842d40 419 bool isEmpty() const {return (len_==0);}
412da427 420
8aa34dad 421 /** Request to guarantee the SBuf's free store space.
412da427
FC
422 *
423 * After the reserveSpace request, the SBuf is guaranteed to have at
7cbd3256 424 * least minSpace bytes of unused backing store following the currently
ac7947fd 425 * used portion and single ownership of the backing store.
412da427
FC
426 * \throw SBufTooBigException if the user tries to allocate too big a SBuf
427 */
e842ecce 428 void reserveSpace(size_type minSpace) {
6fd0701a 429 Must(minSpace <= maxSize);
a44b8b80 430 Must(length() <= maxSize - minSpace);
e842ecce
FC
431 reserveCapacity(length()+minSpace);
432 }
412da427 433
8aa34dad 434 /** Request to guarantee the SBuf's store capacity
412da427
FC
435 *
436 * After this method is called, the SBuf is guaranteed to have at least
7cbd3256 437 * minCapacity bytes of total buffer size, including the currently-used
577bcebc
FC
438 * portion; it is also guaranteed that after this call this SBuf
439 * has unique ownership of the underlying memory store.
412da427
FC
440 * \throw SBufTooBigException if the user tries to allocate too big a SBuf
441 */
442 void reserveCapacity(size_type minCapacity);
443
444 /** slicing method
445 *
496a9e97
FC
446 * Removes SBuf prefix and suffix, leaving a sequence of 'n'
447 * bytes starting from position 'pos', first byte is at pos 0.
448 * It is an in-place-modifying version of substr.
412da427 449 * \param pos start sub-stringing from this byte. If it is
496a9e97 450 * npos or it is greater than the SBuf length, the SBuf is cleared and
50827187 451 * an empty SBuf is returned.
412da427 452 * \param n maximum number of bytes of the resulting SBuf.
7cbd3256 453 * npos means "to end of SBuf".
496a9e97 454 * if it is 0, the SBuf is cleared and an empty SBuf is returned.
496a9e97
FC
455 * if it overflows the end of the SBuf, it is capped to the end of SBuf
456 * \see substr, trim
412da427
FC
457 */
458 SBuf& chop(size_type pos, size_type n = npos);
459
460 /** Remove characters in the toremove set at the beginning, end or both
461 *
462 * \param toremove characters to be removed. Stops chomping at the first
463 * found char not in the set
464 * \param atBeginning if true (default), strips at the beginning of the SBuf
465 * \param atEnd if true (default), strips at the end of the SBuf
466 */
467 SBuf& trim(const SBuf &toRemove, bool atBeginning = true, bool atEnd = true);
468
469 /** Extract a part of the current SBuf.
470 *
496a9e97
FC
471 * Return a fresh a fresh copy of a portion the current SBuf, which is
472 * left untouched. The same parameter convetions apply as for chop.
473 * \see trim, chop
412da427
FC
474 */
475 SBuf substr(size_type pos, size_type n = npos) const;
476
477 /** Find first occurrence of character in SBuf
478 *
479 * Returns the index in the SBuf of the first occurrence of char c.
7cbd3256 480 * \return npos if the char was not found
412da427 481 * \param startPos if specified, ignore any occurrences before that position
496a9e97 482 * if startPos is npos or greater than length() npos is always returned
7cbd3256 483 * if startPos is less than zero, it is ignored
412da427
FC
484 */
485 size_type find(char c, size_type startPos = 0) const;
486
487 /** Find first occurrence of SBuf in SBuf.
488 *
489 * Returns the index in the SBuf of the first occurrence of the
490 * sequence contained in the str argument.
491 * \param startPos if specified, ignore any occurrences before that position
496a9e97 492 * if startPos is npos or greater than length() npos is always returned
7cbd3256 493 * \return npos if the SBuf was not found
412da427
FC
494 */
495 size_type find(const SBuf & str, size_type startPos = 0) const;
496
497 /** Find last occurrence of character in SBuf
498 *
499 * Returns the index in the SBuf of the last occurrence of char c.
7cbd3256 500 * \return npos if the char was not found
412da427 501 * \param endPos if specified, ignore any occurrences after that position.
496a9e97 502 * if npos or greater than length(), the whole SBuf is considered
412da427
FC
503 */
504 size_type rfind(char c, size_type endPos = npos) const;
505
506 /** Find last occurrence of SBuf in SBuf
507 *
508 * Returns the index in the SBuf of the last occurrence of the
509 * sequence contained in the str argument.
7cbd3256 510 * \return npos if the sequence was not found
412da427 511 * \param endPos if specified, ignore any occurrences after that position
496a9e97 512 * if npos or greater than length(), the whole SBuf is considered
412da427
FC
513 */
514 size_type rfind(const SBuf &str, size_type endPos = npos) const;
515
516 /** Find first occurrence of character of set in SBuf
517 *
518 * Finds the first occurrence of ANY of the characters in the supplied set in
519 * the SBuf.
7cbd3256 520 * \return npos if no character in the set could be found
412da427 521 * \param startPos if specified, ignore any occurrences before that position
7cbd3256 522 * if npos, then npos is always returned
c8046ec7
FC
523 *
524 * TODO: rename to camelCase
412da427 525 */
810a5427 526 size_type findFirstOf(const CharacterSet &set, size_type startPos = 0) const;
3c1106a0
FC
527
528 /** Find first occurrence character NOT in character set
529 *
c8046ec7 530 * \return npos if all characters in the SBuf are from set
3c1106a0
FC
531 * \param startPos if specified, ignore any occurrences before that position
532 * if npos, then npos is always returned
c8046ec7
FC
533 *
534 * TODO: rename to camelCase
3c1106a0 535 */
810a5427 536 size_type findFirstNotOf(const CharacterSet &set, size_type startPos = 0) const;
412da427
FC
537
538 /** sscanf-alike
539 *
540 * sscanf re-implementation. Non-const, and not \0-clean.
541 * \return same as sscanf
542 * \see man sscanf(3)
543 */
544 int scanf(const char *format, ...);
545
546 /** Lower-case SBuf
547 *
548 * Returns a lower-cased COPY of the SBuf
549 * \see man tolower(3)
550 */
551 SBuf toLower() const;
552
553 /** Upper-case SBuf
554 *
555 * Returns an upper-cased COPY of the SBuf
556 * \see man toupper(3)
557 */
558 SBuf toUpper() const;
559
560 /** String export function
496a9e97
FC
561 * converts the SBuf to a legacy String, by copy.
562 * \deprecated
412da427
FC
563 */
564 String toString() const;
565
7cbd3256
FC
566 /// std::string export function
567 std::string toStdString() const { return std::string(buf(),length()); }
568
496a9e97
FC
569 // TODO: possibly implement erase() similar to std::string's erase
570 // TODO: possibly implement a replace() call
412da427
FC
571private:
572
573 MemBlob::Pointer store_; ///< memory block, possibly shared with other SBufs
574 size_type off_; ///< our content start offset from the beginning of shared store_
575 size_type len_; ///< number of our content bytes in shared store_
576 static SBufStats stats; ///< class-wide statistics
577
7cbd3256
FC
578 /// SBuf object identifier; does not change when contents do,
579 /// including during assignment
580 const InstanceId<SBuf> id;
412da427 581
62842d40
FC
582 /** obtain prototype store
583 *
584 * Just-created SBufs all share to the same MemBlob.
585 * This call instantiates and returns it.
586 */
587 static MemBlob::Pointer GetStorePrototype();
588
589 /**
590 * obtains a char* to the beginning of this SBuf in memory.
591 * \note the obtained string is NOT null-terminated.
592 */
593 char * buf() const {return (store_->mem+off_);}
594
595 /** returns the pointer to the first char after this SBuf end
596 *
597 * No checks are made that the space returned is safe, checking that is
598 * up to the caller.
599 */
600 char * bufEnd() const {return (store_->mem+off_+len_);}
601
602 /**
603 * Try to guesstimate how big a MemBlob to allocate.
604 * The result is guarranteed to be to be at least the desired size.
605 */
fdd1982c 606 size_type estimateCapacity(size_type desired) const {return (2*desired);}
412da427 607
412da427
FC
608 void reAlloc(size_type newsize);
609
a452bc8b 610 void cow(size_type minsize = npos);
412da427
FC
611
612 void checkAccessBounds(size_type pos) const;
62842d40 613
7cbd3256
FC
614 /** Low-level append operation
615 *
616 * Takes as input a contiguous area of memory and appends its contents
617 * to the SBuf, taking care of memory management. Does no bounds checking
618 * on the supplied memory buffer, it is the duty of the caller to ensure
619 * that the supplied area is valid.
620 */
621 SBuf& lowAppend(const char * memArea, size_type areaSize);
412da427
FC
622};
623
496a9e97 624/// ostream output operator
62842d40
FC
625inline std::ostream &
626operator <<(std::ostream& os, const SBuf& S)
627{
628 return S.print(os);
629}
412da427
FC
630
631#endif /* SQUID_SBUF_H */