]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/MemBuf.cc
4 * DEBUG: section 59 auto-growing Memory Buffer with printf
5 * AUTHOR: Alex Rousskov
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 \todo use memory pools for .buf recycling @?@ @?@
44 * Here is how one would Comm::Write an object without MemBuffer:
48 * buf = malloc(big_enough);
51 * snprintf object(s) piece-by-piece constantly checking for overflows
52 * and maintaining (buf+offset);
56 * Comm::Write(buf, free, ...);
59 * The whole "packing" idea is quite messy: We are given a buffer of fixed
60 * size and we have to check all the time that we still fit. Sounds logical.
62 * However, what happens if we have more data? If we are lucky to stop before
63 * we overrun any buffers, we still may have garbage (e.g. half of ETag) in
69 * MemBuffer is a memory-resident buffer with printf()-like interface. It
70 * hides all offest handling and overflow checking. Moreover, it has a
71 * build-in control that no partial data has been written.
73 * MemBuffer is designed to handle relatively small data. It starts with a
74 * small buffer of configurable size to avoid allocating huge buffers all the
75 * time. MemBuffer doubles the buffer when needed. It assert()s that it will
76 * not grow larger than a configurable limit. MemBuffer has virtually no
77 * overhead (and can even reduce memory consumption) compared to old
80 * MemBuffer eliminates both "packing" mess and truncated data:
86 * -- required init with optional size tuning (see #defines for defaults)
87 * buf.init(initial-size, absolute-maximum);
89 * -- "pack" (no need to handle offsets or check for overflows)
94 * Comm::Write(fd, buf, callback);
96 * -- *iff* you did not give the buffer away, free it yourself
105 #include "profiler/Profiler.h"
111 #if defined HAVE_VA_COPY
112 #define VA_COPY va_copy
113 #elif defined HAVE___VA_COPY
114 #define VA_COPY __va_copy
117 /* local constants */
119 /* default values for buffer sizes, used by memBufDefInit */
120 #define MEM_BUF_INIT_SIZE (2*1024)
121 #define MEM_BUF_MAX_SIZE (2*1000*1024*1024)
123 CBDATA_CLASS_INIT(MemBuf
);
125 /** init with defaults */
129 init(MEM_BUF_INIT_SIZE
, MEM_BUF_MAX_SIZE
);
133 /** init with specific sizes */
135 MemBuf::init(mb_size_t szInit
, mb_size_t szMax
)
137 assert(szInit
> 0 && szMax
> 0);
140 max_capacity
= szMax
;
147 * cleans the mb; last function to call if you do not give .buf away with
157 assert(!stolen
); /* not frozen */
159 memFreeBuf(capacity
, buf
);
161 size
= capacity
= max_capacity
= 0;
166 * Cleans the buffer without changing its capacity
167 * if called with a Null buffer, calls memBufDefInit()
175 assert(!stolen
); /* not frozen */
177 memset(buf
, 0, capacity
);
183 * Unfortunate hack to test if the buffer has been Init()ialized
188 if (!buf
&& !max_capacity
&& !capacity
&& !size
)
189 return 1; /* is null (not initialized) */
191 assert(buf
&& max_capacity
&& capacity
); /* paranoid */
196 mb_size_t
MemBuf::spaceSize() const
198 const mb_size_t terminatedSize
= size
+ 1;
199 return (terminatedSize
< capacity
) ? capacity
- terminatedSize
: 0;
202 mb_size_t
MemBuf::potentialSpaceSize() const
204 const mb_size_t terminatedSize
= size
+ 1;
205 return (terminatedSize
< max_capacity
) ? max_capacity
- terminatedSize
: 0;
208 /// removes sz bytes and "packs" by moving content left
209 void MemBuf::consume(mb_size_t shiftSize
)
211 const mb_size_t cSize
= contentSize();
212 assert(0 <= shiftSize
&& shiftSize
<= cSize
);
213 assert(!stolen
); /* not frozen */
215 PROF_start(MemBuf_consume
);
217 if (shiftSize
< cSize
)
218 memmove(buf
, buf
+ shiftSize
, cSize
- shiftSize
);
224 PROF_stop(MemBuf_consume
);
227 // removes last tailSize bytes
228 void MemBuf::truncate(mb_size_t tailSize
)
230 const mb_size_t cSize
= contentSize();
231 assert(0 <= tailSize
&& tailSize
<= cSize
);
232 assert(!stolen
); /* not frozen */
237 * calls memcpy, appends exactly size bytes,
238 * extends buffer or creates buffer if needed.
240 void MemBuf::append(const char *newContent
, mb_size_t sz
)
243 assert(buf
|| (0==capacity
&& 0==size
));
244 assert(!stolen
); /* not frozen */
246 PROF_start(MemBuf_append
);
248 if (size
+ sz
+ 1 > capacity
)
251 assert(size
+ sz
<= capacity
); /* paranoid */
252 memcpy(space(), newContent
, sz
);
255 PROF_stop(MemBuf_append
);
258 /// updates content size after external append
259 void MemBuf::appended(mb_size_t sz
)
261 assert(size
+ sz
<= capacity
);
267 * Null-terminate in case we are used as a string.
268 * Extra octet is not counted in the content size (or space size)
270 \note XXX: but the extra octet is counted when growth decisions are made!
271 * This will cause the buffer to grow when spaceSize() == 1 on append,
272 * which will assert() if the buffer cannot grow any more.
274 void MemBuf::terminate()
276 assert(size
< capacity
);
280 /* calls memBufVPrintf */
282 MemBuf::Printf(const char *fmt
,...)
292 * vPrintf for other printf()'s to use; calls vsnprintf, extends buf if needed
295 MemBuf::vPrintf(const char *fmt
, va_list vargs
)
304 assert(!stolen
); /* not frozen */
305 /* assert in Grow should quit first, but we do not want to have a scary infinite loop */
307 while (capacity
<= max_capacity
) {
308 mb_size_t free_space
= capacity
- size
;
309 /* put as much as we can */
312 /* Fix of bug 753r. The value of vargs is undefined
313 * after vsnprintf() returns. Make a copy of vargs
314 * incase we loop around and call vsnprintf() again.
317 sz
= vsnprintf(buf
+ size
, free_space
, fmt
, ap
);
321 sz
= vsnprintf(buf
+ size
, free_space
, fmt
, vargs
);
323 /* check for possible overflow */
324 /* snprintf on Linuz returns -1 on overflows */
325 /* snprintf on FreeBSD returns at least free_space on overflows */
327 if (sz
< 0 || sz
>= free_space
)
334 /* on Linux and FreeBSD, '\0' is not counted in return value */
335 /* on XXX it might be counted */
336 /* check that '\0' is appended and not counted */
338 if (!size
|| buf
[size
- 1]) {
347 * calling this function "freezes" mb,
348 * do not _update_ mb after that in any way
349 * (you still can read-access .buf and .size)
351 \retval free() function to be used.
358 assert(!stolen
); /* not frozen */
360 ff
= memFreeBufFunc((size_t) capacity
);
361 stolen
= 1; /* freeze */
366 * Grows (doubles) internal buffer to satisfy required minimal capacity
369 MemBuf::grow(mb_size_t min_cap
)
375 assert(capacity
< min_cap
);
377 PROF_start(MemBuf_grow
);
379 /* determine next capacity */
381 if (min_cap
> 64 * 1024) {
384 while (new_cap
< (size_t) min_cap
)
385 new_cap
+= 64 * 1024; /* increase in reasonable steps */
387 new_cap
= (size_t) min_cap
;
390 /* last chance to fit before we assert(!overflow) */
391 if (new_cap
> (size_t) max_capacity
)
392 new_cap
= (size_t) max_capacity
;
394 assert(new_cap
<= (size_t) max_capacity
); /* no overflow */
396 assert(new_cap
> (size_t) capacity
); /* progress */
398 buf_cap
= (size_t) capacity
;
400 buf
= (char *)memReallocBuf(buf
, new_cap
, &buf_cap
);
403 capacity
= (mb_size_t
) buf_cap
;
404 PROF_stop(MemBuf_grow
);
411 * Puts report on MemBuf _module_ usage into mb
414 memBufReport(MemBuf
* mb
)
417 mb
->Printf("memBufReport is not yet implemented @?@\n");
421 #include "MemBuf.cci"