]>
Commit | Line | Data |
---|---|---|
528b2c61 | 1 | /* |
b8ae064d | 2 | * Copyright (C) 1996-2023 The Squid Software Foundation and contributors |
528b2c61 | 3 | * |
bbc27441 AJ |
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. | |
528b2c61 | 7 | */ |
8 | ||
ff9d9458 FC |
9 | #ifndef SQUID_SRC_MEMBUF_H |
10 | #define SQUID_SRC_MEMBUF_H | |
528b2c61 | 11 | |
10201568 | 12 | #include "base/Packable.h" |
aa839030 | 13 | #include "cbdata.h" |
6f5dc9e4 | 14 | #include "mem/forward.h" |
25b6a907 | 15 | |
ffec8f2e FC |
16 | /* in case we want to change it later */ |
17 | typedef ssize_t mb_size_t; | |
18 | ||
63be0a78 | 19 | /** |
10201568 | 20 | * Auto-growing memory-resident buffer with Packable interface |
0546971e | 21 | * \deprecated Use SBuf instead. |
63be0a78 | 22 | */ |
10201568 | 23 | class MemBuf : public Packable |
62e76326 | 24 | { |
5c2f68b7 AJ |
25 | CBDATA_CLASS(MemBuf); |
26 | ||
62e76326 | 27 | public: |
0546971e | 28 | MemBuf(): |
aee3523a | 29 | buf(nullptr), |
0546971e AJ |
30 | size(0), |
31 | max_capacity(0), | |
32 | capacity(0), | |
33 | stolen(0) | |
34 | {} | |
337b9aa4 | 35 | ~MemBuf() override { |
0546971e AJ |
36 | if (!stolen && buf) |
37 | clean(); | |
38 | } | |
032785bf | 39 | |
63be0a78 | 40 | /// start of the added data |
41 | char *content() { return buf; } | |
032785bf | 42 | |
63be0a78 | 43 | /// start of the added data |
44 | const char *content() const { return buf; } | |
9bcd2d11 | 45 | |
63be0a78 | 46 | /// available data size |
47 | mb_size_t contentSize() const { return size; } | |
032785bf | 48 | |
63be0a78 | 49 | /** |
50 | * Whether the buffer contains any data. | |
f53969cc SM |
51 | \retval true if data exists in the buffer |
52 | \retval false if data exists in the buffer | |
63be0a78 | 53 | */ |
032785bf | 54 | bool hasContent() const { return size > 0; } |
55 | ||
3aa3d385 AR |
56 | /// returns buffer after data; does not check space existence |
57 | char *space() { return buf + size; } ///< space to add data | |
58 | ||
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; } | |
032785bf | 64 | |
65 | mb_size_t spaceSize() const; | |
63be0a78 | 66 | |
67 | /** | |
68 | * Whether the buffer contains any data space available. | |
f53969cc SM |
69 | \retval true if data can be added to the buffer |
70 | \retval false if the buffer is full | |
63be0a78 | 71 | */ |
032785bf | 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 | ||
63be0a78 | 77 | /// \note there is currently no stretch() method to grow without appending |
032785bf | 78 | |
79 | void consume(mb_size_t sz); // removes sz bytes, moving content left | |
24eac830 | 80 | void consumeWhitespacePrefix(); ///< removes all prefix whitespace, moving content left |
1177ec50 | 81 | |
032785bf | 82 | void appended(mb_size_t sz); // updates content size after external append |
821beb5e | 83 | void truncate(mb_size_t sz); // removes sz last bytes |
032785bf | 84 | |
032785bf | 85 | void terminate(); // zero-terminates the buffer w/o increasing contentSize |
86 | ||
9bcd2d11 | 87 | bool wasStolen() const { return stolen; } |
88 | ||
63be0a78 | 89 | /** init with specific sizes */ |
2fe7eff9 | 90 | void init(mb_size_t szInit, mb_size_t szMax); |
91 | ||
63be0a78 | 92 | /** init with defaults */ |
2fe7eff9 | 93 | void init(); |
94 | ||
63be0a78 | 95 | /** cleans mb; last function to call if you do not give .buf away */ |
2fe7eff9 | 96 | void clean(); |
97 | ||
63be0a78 | 98 | /** resets mb preserving (or initializing if needed) memory buffer */ |
2fe7eff9 | 99 | void reset(); |
100 | ||
63be0a78 | 101 | /** unfirtunate hack to test if the buffer has been Init()ialized */ |
ddc77a2e | 102 | int isNull() const; |
2fe7eff9 | 103 | |
63be0a78 | 104 | /** |
105 | * freezes the object! and returns function to clear it up. | |
106 | * | |
107 | \retval free() function to be used. | |
108 | */ | |
2fe7eff9 | 109 | FREE *freeFunc(); |
110 | ||
10201568 | 111 | /* Packable API */ |
337b9aa4 AR |
112 | void append(const char *c, int sz) override; |
113 | void vappendf(const char *fmt, va_list ap) override; | |
10201568 | 114 | |
032785bf | 115 | private: |
63be0a78 | 116 | /** |
032785bf | 117 | * private copy constructor and assignment operator generates |
118 | * compiler errors if someone tries to copy/assign a MemBuf | |
119 | */ | |
ced8def3 | 120 | MemBuf(const MemBuf &) {assert(false);} |
032785bf | 121 | |
ced8def3 | 122 | MemBuf& operator= (const MemBuf &) {assert(false); return *this;} |
032785bf | 123 | |
2fe7eff9 | 124 | void grow(mb_size_t min_cap); |
125 | ||
032785bf | 126 | public: |
63be0a78 | 127 | /** |
128 | \deprecated use space*() and content*() methods to access safely instead. | |
129 | * public, read-only. | |
26ac0430 | 130 | * |
0546971e | 131 | * TODO: hide these members completely and remove 0-termination |
63be0a78 | 132 | * so that consume() does not need to memmove all the time |
133 | */ | |
032785bf | 134 | char *buf; // available content |
135 | mb_size_t size; // used space, does not count 0-terminator | |
528b2c61 | 136 | |
63be0a78 | 137 | /** |
138 | * when grows: assert(new_capacity <= max_capacity) | |
0546971e AJ |
139 | * \deprecated Use interface function instead |
140 | * TODO: make these private after converting memBuf*() functions to methods | |
63be0a78 | 141 | */ |
142 | mb_size_t max_capacity; | |
143 | ||
144 | /** | |
145 | * allocated space | |
0546971e AJ |
146 | * \deprecated Use interface function instead |
147 | * TODO: make these private after converting memBuf*() functions to methods | |
63be0a78 | 148 | */ |
26ac0430 | 149 | mb_size_t capacity; |
62e76326 | 150 | |
f53969cc | 151 | unsigned stolen:1; /* the buffer has been stolen for use by someone else */ |
9bcd2d11 | 152 | |
153 | #if 0 | |
154 | ||
f53969cc | 155 | unsigned valid:1; /* to be used for debugging only! */ |
9bcd2d11 | 156 | #endif |
528b2c61 | 157 | }; |
158 | ||
63be0a78 | 159 | /** returns free() function to be used, _freezes_ the object! */ |
d9c252f2 | 160 | void memBufReport(MemBuf * mb); |
528b2c61 | 161 | |
ff9d9458 | 162 | #endif /* SQUID_SRC_MEMBUF_H */ |
f53969cc | 163 |