]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBuf.cc
d4b25923da8a405eac57c18ac9d5c427e1ec9a66
2 * Copyright (C) 1996-2015 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.
9 /* DEBUG: section 59 auto-growing Memory Buffer with printf */
16 * Here is how one would Comm::Write an object without MemBuffer:
20 * buf = malloc(big_enough);
23 * snprintf object(s) piece-by-piece constantly checking for overflows
24 * and maintaining (buf+offset);
28 * Comm::Write(buf, free, ...);
31 * The whole "packing" idea is quite messy: We are given a buffer of fixed
32 * size and we have to check all the time that we still fit. Sounds logical.
34 * However, what happens if we have more data? If we are lucky to stop before
35 * we overrun any buffers, we still may have garbage (e.g. half of ETag) in
41 * MemBuffer is a memory-resident buffer with printf()-like interface. It
42 * hides all offest handling and overflow checking. Moreover, it has a
43 * build-in control that no partial data has been written.
45 * MemBuffer is designed to handle relatively small data. It starts with a
46 * small buffer of configurable size to avoid allocating huge buffers all the
47 * time. MemBuffer doubles the buffer when needed. It assert()s that it will
48 * not grow larger than a configurable limit. MemBuffer has virtually no
49 * overhead (and can even reduce memory consumption) compared to old
52 * MemBuffer eliminates both "packing" mess and truncated data:
58 * -- required init with optional size tuning (see #defines for defaults)
59 * buf.init(initial-size, absolute-maximum);
61 * -- "pack" (no need to handle offsets or check for overflows)
66 * Comm::Write(fd, buf, callback);
68 * -- *iff* you did not give the buffer away, free it yourself
75 #include "mem/forward.h"
77 #include "profiler/Profiler.h"
82 #if defined HAVE_VA_COPY
83 #define VA_COPY va_copy
84 #elif defined HAVE___VA_COPY
85 #define VA_COPY __va_copy
90 /* default values for buffer sizes, used by memBufDefInit */
91 #define MEM_BUF_INIT_SIZE (2*1024)
92 #define MEM_BUF_MAX_SIZE (2*1000*1024*1024)
94 CBDATA_CLASS_INIT(MemBuf
);
96 /** init with defaults */
100 init(MEM_BUF_INIT_SIZE
, MEM_BUF_MAX_SIZE
);
103 /** init with specific sizes */
105 MemBuf::init(mb_size_t szInit
, mb_size_t szMax
)
107 assert(szInit
> 0 && szMax
> 0);
110 max_capacity
= szMax
;
118 * cleans the mb; last function to call if you do not give .buf away with
128 assert(!stolen
); /* not frozen */
130 memFreeBuf(capacity
, buf
);
132 size
= capacity
= max_capacity
= 0;
137 * Cleans the buffer without changing its capacity
138 * if called with a Null buffer, calls memBufDefInit()
146 assert(!stolen
); /* not frozen */
148 memset(buf
, 0, capacity
);
154 * Unfortunate hack to test if the buffer has been Init()ialized
159 if (!buf
&& !max_capacity
&& !capacity
&& !size
)
160 return 1; /* is null (not initialized) */
162 assert(buf
&& max_capacity
&& capacity
); /* paranoid */
167 mb_size_t
MemBuf::spaceSize() const
169 const mb_size_t terminatedSize
= size
+ 1;
170 return (terminatedSize
< capacity
) ? capacity
- terminatedSize
: 0;
173 mb_size_t
MemBuf::potentialSpaceSize() const
175 const mb_size_t terminatedSize
= size
+ 1;
176 return (terminatedSize
< max_capacity
) ? max_capacity
- terminatedSize
: 0;
179 /// removes sz bytes and "packs" by moving content left
180 void MemBuf::consume(mb_size_t shiftSize
)
182 const mb_size_t cSize
= contentSize();
183 assert(0 <= shiftSize
&& shiftSize
<= cSize
);
184 assert(!stolen
); /* not frozen */
186 PROF_start(MemBuf_consume
);
188 if (shiftSize
< cSize
)
189 memmove(buf
, buf
+ shiftSize
, cSize
- shiftSize
);
195 PROF_stop(MemBuf_consume
);
198 /// removes all whitespace prefix bytes and "packs" by moving content left
199 void MemBuf::consumeWhitespacePrefix()
201 PROF_start(MemBuf_consumeWhitespace
);
202 if (contentSize() > 0) {
203 const char *end
= buf
+ contentSize();
205 for (; p
<end
&& xisspace(*p
); ++p
);
209 PROF_stop(MemBuf_consumeWhitespace
);
212 // removes last tailSize bytes
213 void MemBuf::truncate(mb_size_t tailSize
)
215 const mb_size_t cSize
= contentSize();
216 assert(0 <= tailSize
&& tailSize
<= cSize
);
217 assert(!stolen
); /* not frozen */
222 * calls memcpy, appends exactly size bytes,
223 * extends buffer or creates buffer if needed.
225 void MemBuf::append(const char *newContent
, mb_size_t sz
)
228 assert(buf
|| (0==capacity
&& 0==size
));
229 assert(!stolen
); /* not frozen */
231 PROF_start(MemBuf_append
);
233 if (size
+ sz
+ 1 > capacity
)
236 assert(size
+ sz
<= capacity
); /* paranoid */
237 memcpy(space(), newContent
, sz
);
240 PROF_stop(MemBuf_append
);
243 /// updates content size after external append
244 void MemBuf::appended(mb_size_t sz
)
246 assert(size
+ sz
<= capacity
);
252 * Null-terminate in case we are used as a string.
253 * Extra octet is not counted in the content size (or space size)
255 \note XXX: but the extra octet is counted when growth decisions are made!
256 * This will cause the buffer to grow when spaceSize() == 1 on append,
257 * which will assert() if the buffer cannot grow any more.
259 void MemBuf::terminate()
261 assert(size
< capacity
);
265 /* calls memBufVPrintf */
267 MemBuf::Printf(const char *fmt
,...)
276 * vPrintf for other printf()'s to use; calls vsnprintf, extends buf if needed
279 MemBuf::vPrintf(const char *fmt
, va_list vargs
)
288 assert(!stolen
); /* not frozen */
289 /* assert in Grow should quit first, but we do not want to have a scary infinite loop */
291 while (capacity
<= max_capacity
) {
292 mb_size_t free_space
= capacity
- size
;
293 /* put as much as we can */
296 /* Fix of bug 753r. The value of vargs is undefined
297 * after vsnprintf() returns. Make a copy of vargs
298 * incase we loop around and call vsnprintf() again.
301 sz
= vsnprintf(buf
+ size
, free_space
, fmt
, ap
);
305 sz
= vsnprintf(buf
+ size
, free_space
, fmt
, vargs
);
307 /* check for possible overflow */
308 /* snprintf on Linuz returns -1 on overflows */
309 /* snprintf on FreeBSD returns at least free_space on overflows */
311 if (sz
< 0 || sz
>= free_space
)
318 /* on Linux and FreeBSD, '\0' is not counted in return value */
319 /* on XXX it might be counted */
320 /* check that '\0' is appended and not counted */
322 if (!size
|| buf
[size
- 1]) {
331 * calling this function "freezes" mb,
332 * do not _update_ mb after that in any way
333 * (you still can read-access .buf and .size)
335 \retval free() function to be used.
342 assert(!stolen
); /* not frozen */
344 ff
= memFreeBufFunc((size_t) capacity
);
345 stolen
= 1; /* freeze */
350 * Grows (doubles) internal buffer to satisfy required minimal capacity
353 MemBuf::grow(mb_size_t min_cap
)
359 assert(capacity
< min_cap
);
361 PROF_start(MemBuf_grow
);
363 /* determine next capacity */
365 if (min_cap
> 64 * 1024) {
368 while (new_cap
< (size_t) min_cap
)
369 new_cap
+= 64 * 1024; /* increase in reasonable steps */
371 new_cap
= (size_t) min_cap
;
374 /* last chance to fit before we assert(!overflow) */
375 if (new_cap
> (size_t) max_capacity
)
376 new_cap
= (size_t) max_capacity
;
378 assert(new_cap
<= (size_t) max_capacity
); /* no overflow */
380 assert(new_cap
> (size_t) capacity
); /* progress */
382 buf_cap
= (size_t) capacity
;
384 buf
= (char *)memReallocBuf(buf
, new_cap
, &buf_cap
);
387 capacity
= (mb_size_t
) buf_cap
;
388 PROF_stop(MemBuf_grow
);
394 * Puts report on MemBuf _module_ usage into mb
397 memBufReport(MemBuf
* mb
)
400 mb
->Printf("memBufReport is not yet implemented @?@\n");