]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBuf.cc
2 * DEBUG: section 59 auto-growing Memory Buffer with printf
3 * AUTHOR: Alex Rousskov
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 \todo use memory pools for .buf recycling @?@ @?@
42 * Here is how one would Comm::Write an object without MemBuffer:
46 * buf = malloc(big_enough);
49 * snprintf object(s) piece-by-piece constantly checking for overflows
50 * and maintaining (buf+offset);
54 * Comm::Write(buf, free, ...);
57 * The whole "packing" idea is quite messy: We are given a buffer of fixed
58 * size and we have to check all the time that we still fit. Sounds logical.
60 * However, what happens if we have more data? If we are lucky to stop before
61 * we overrun any buffers, we still may have garbage (e.g. half of ETag) in
67 * MemBuffer is a memory-resident buffer with printf()-like interface. It
68 * hides all offest handling and overflow checking. Moreover, it has a
69 * build-in control that no partial data has been written.
71 * MemBuffer is designed to handle relatively small data. It starts with a
72 * small buffer of configurable size to avoid allocating huge buffers all the
73 * time. MemBuffer doubles the buffer when needed. It assert()s that it will
74 * not grow larger than a configurable limit. MemBuffer has virtually no
75 * overhead (and can even reduce memory consumption) compared to old
78 * MemBuffer eliminates both "packing" mess and truncated data:
84 * -- required init with optional size tuning (see #defines for defaults)
85 * buf.init(initial-size, absolute-maximum);
87 * -- "pack" (no need to handle offsets or check for overflows)
92 * Comm::Write(fd, buf, callback);
94 * -- *iff* you did not give the buffer away, free it yourself
103 #include "profiler/Profiler.h"
108 #if defined HAVE_VA_COPY
109 #define VA_COPY va_copy
110 #elif defined HAVE___VA_COPY
111 #define VA_COPY __va_copy
114 /* local constants */
116 /* default values for buffer sizes, used by memBufDefInit */
117 #define MEM_BUF_INIT_SIZE (2*1024)
118 #define MEM_BUF_MAX_SIZE (2*1000*1024*1024)
120 CBDATA_CLASS_INIT(MemBuf
);
122 /** init with defaults */
126 init(MEM_BUF_INIT_SIZE
, MEM_BUF_MAX_SIZE
);
129 /** init with specific sizes */
131 MemBuf::init(mb_size_t szInit
, mb_size_t szMax
)
133 assert(szInit
> 0 && szMax
> 0);
136 max_capacity
= szMax
;
143 * cleans the mb; last function to call if you do not give .buf away with
153 assert(!stolen
); /* not frozen */
155 memFreeBuf(capacity
, buf
);
157 size
= capacity
= max_capacity
= 0;
162 * Cleans the buffer without changing its capacity
163 * if called with a Null buffer, calls memBufDefInit()
171 assert(!stolen
); /* not frozen */
173 memset(buf
, 0, capacity
);
179 * Unfortunate hack to test if the buffer has been Init()ialized
184 if (!buf
&& !max_capacity
&& !capacity
&& !size
)
185 return 1; /* is null (not initialized) */
187 assert(buf
&& max_capacity
&& capacity
); /* paranoid */
192 mb_size_t
MemBuf::spaceSize() const
194 const mb_size_t terminatedSize
= size
+ 1;
195 return (terminatedSize
< capacity
) ? capacity
- terminatedSize
: 0;
198 mb_size_t
MemBuf::potentialSpaceSize() const
200 const mb_size_t terminatedSize
= size
+ 1;
201 return (terminatedSize
< max_capacity
) ? max_capacity
- terminatedSize
: 0;
204 /// removes sz bytes and "packs" by moving content left
205 void MemBuf::consume(mb_size_t shiftSize
)
207 const mb_size_t cSize
= contentSize();
208 assert(0 <= shiftSize
&& shiftSize
<= cSize
);
209 assert(!stolen
); /* not frozen */
211 PROF_start(MemBuf_consume
);
213 if (shiftSize
< cSize
)
214 memmove(buf
, buf
+ shiftSize
, cSize
- shiftSize
);
220 PROF_stop(MemBuf_consume
);
223 // removes last tailSize bytes
224 void MemBuf::truncate(mb_size_t tailSize
)
226 const mb_size_t cSize
= contentSize();
227 assert(0 <= tailSize
&& tailSize
<= cSize
);
228 assert(!stolen
); /* not frozen */
233 * calls memcpy, appends exactly size bytes,
234 * extends buffer or creates buffer if needed.
236 void MemBuf::append(const char *newContent
, mb_size_t sz
)
239 assert(buf
|| (0==capacity
&& 0==size
));
240 assert(!stolen
); /* not frozen */
242 PROF_start(MemBuf_append
);
244 if (size
+ sz
+ 1 > capacity
)
247 assert(size
+ sz
<= capacity
); /* paranoid */
248 memcpy(space(), newContent
, sz
);
251 PROF_stop(MemBuf_append
);
254 /// updates content size after external append
255 void MemBuf::appended(mb_size_t sz
)
257 assert(size
+ sz
<= capacity
);
263 * Null-terminate in case we are used as a string.
264 * Extra octet is not counted in the content size (or space size)
266 \note XXX: but the extra octet is counted when growth decisions are made!
267 * This will cause the buffer to grow when spaceSize() == 1 on append,
268 * which will assert() if the buffer cannot grow any more.
270 void MemBuf::terminate()
272 assert(size
< capacity
);
276 /* calls memBufVPrintf */
278 MemBuf::Printf(const char *fmt
,...)
287 * vPrintf for other printf()'s to use; calls vsnprintf, extends buf if needed
290 MemBuf::vPrintf(const char *fmt
, va_list vargs
)
299 assert(!stolen
); /* not frozen */
300 /* assert in Grow should quit first, but we do not want to have a scary infinite loop */
302 while (capacity
<= max_capacity
) {
303 mb_size_t free_space
= capacity
- size
;
304 /* put as much as we can */
307 /* Fix of bug 753r. The value of vargs is undefined
308 * after vsnprintf() returns. Make a copy of vargs
309 * incase we loop around and call vsnprintf() again.
312 sz
= vsnprintf(buf
+ size
, free_space
, fmt
, ap
);
316 sz
= vsnprintf(buf
+ size
, free_space
, fmt
, vargs
);
318 /* check for possible overflow */
319 /* snprintf on Linuz returns -1 on overflows */
320 /* snprintf on FreeBSD returns at least free_space on overflows */
322 if (sz
< 0 || sz
>= free_space
)
329 /* on Linux and FreeBSD, '\0' is not counted in return value */
330 /* on XXX it might be counted */
331 /* check that '\0' is appended and not counted */
333 if (!size
|| buf
[size
- 1]) {
342 * calling this function "freezes" mb,
343 * do not _update_ mb after that in any way
344 * (you still can read-access .buf and .size)
346 \retval free() function to be used.
353 assert(!stolen
); /* not frozen */
355 ff
= memFreeBufFunc((size_t) capacity
);
356 stolen
= 1; /* freeze */
361 * Grows (doubles) internal buffer to satisfy required minimal capacity
364 MemBuf::grow(mb_size_t min_cap
)
370 assert(capacity
< min_cap
);
372 PROF_start(MemBuf_grow
);
374 /* determine next capacity */
376 if (min_cap
> 64 * 1024) {
379 while (new_cap
< (size_t) min_cap
)
380 new_cap
+= 64 * 1024; /* increase in reasonable steps */
382 new_cap
= (size_t) min_cap
;
385 /* last chance to fit before we assert(!overflow) */
386 if (new_cap
> (size_t) max_capacity
)
387 new_cap
= (size_t) max_capacity
;
389 assert(new_cap
<= (size_t) max_capacity
); /* no overflow */
391 assert(new_cap
> (size_t) capacity
); /* progress */
393 buf_cap
= (size_t) capacity
;
395 buf
= (char *)memReallocBuf(buf
, new_cap
, &buf_cap
);
398 capacity
= (mb_size_t
) buf_cap
;
399 PROF_stop(MemBuf_grow
);
405 * Puts report on MemBuf _module_ usage into mb
408 memBufReport(MemBuf
* mb
)
411 mb
->Printf("memBufReport is not yet implemented @?@\n");
415 #include "MemBuf.cci"