~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to src/MemBuf.cc

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2009-09-24 14:51:06 UTC
  • mfrom: (1.1.12 upstream)
  • mto: (20.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20090924145106-38jgrzmj0d73pha5
Tags: 3.1.0.13-1
* Upload to experimental

* New upstream release
  - Fixes Follow-X-Forwarded-For support (Closes: #523943)
  - Adds IPv6 support (Closes: #432351)

* debian/rules
  - Removed obsolete configuration options
  - Enable db and radius basic authentication modules

* debian/patches/01-cf.data.debian
  - Adapted to new upstream version

* debian/patches/02-makefile-defaults
  - Adapted to new upstream version

* debian/{squid.postinst,squid.rc,README.Debian,watch}
  - Updated references to squid 3.1

* debian/squid3.install
  - Install CSS file for error pages
  - Install manual pages for new authentication modules

* debian/squid3-common.install
  - Install documented version of configuration file in /usr/share/doc/squid3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
1
/*
3
 
 * $Id: MemBuf.cc,v 1.42 2006/09/20 08:13:38 adrian Exp $
 
2
 * $Id$
4
3
 *
5
4
 * DEBUG: section 59    auto-growing Memory Buffer with printf
6
5
 * AUTHOR: Alex Rousskov
21
20
 *  it under the terms of the GNU General Public License as published by
22
21
 *  the Free Software Foundation; either version 2 of the License, or
23
22
 *  (at your option) any later version.
24
 
 *  
 
23
 *
25
24
 *  This program is distributed in the hope that it will be useful,
26
25
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27
26
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
27
 *  GNU General Public License for more details.
29
 
 *  
 
28
 *
30
29
 *  You should have received a copy of the GNU General Public License
31
30
 *  along with this program; if not, write to the Free Software
32
31
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33
32
 *
34
33
 */
35
34
 
36
 
/*
37
 
 * To-Do: use memory pools for .buf recycling @?@ @?@
 
35
/**
 
36
 \todo use memory pools for .buf recycling @?@ @?@
38
37
 */
39
38
 
40
 
/*
 
39
/**
 
40
 \verbatim
41
41
 * Rationale:
42
42
 * ----------
43
 
 * 
 
43
 *
44
44
 * Here is how one would comm_write an object without MemBuffer:
45
 
 * 
 
45
 *
46
46
 * {
47
47
 * -- allocate:
48
48
 * buf = malloc(big_enough);
49
 
 * 
 
49
 *
50
50
 * -- "pack":
51
51
 * snprintf object(s) piece-by-piece constantly checking for overflows
52
52
 * and maintaining (buf+offset);
53
53
 * ...
54
 
 * 
 
54
 *
55
55
 * -- write
56
56
 * comm_write(buf, free, ...);
57
57
 * }
58
 
 * 
 
58
 *
59
59
 * The whole "packing" idea is quite messy: We are given a buffer of fixed
60
60
 * size and we have to check all the time that we still fit. Sounds logical.
61
61
 *
62
62
 * However, what happens if we have more data? If we are lucky to stop before
63
63
 * we overrun any buffers, we still may have garbage (e.g. half of ETag) in
64
64
 * the buffer.
65
 
 * 
 
65
 *
66
66
 * MemBuffer:
67
67
 * ----------
68
 
 * 
 
68
 *
69
69
 * MemBuffer is a memory-resident buffer with printf()-like interface. It
70
70
 * hides all offest handling and overflow checking. Moreover, it has a
71
71
 * build-in control that no partial data has been written.
72
 
 * 
 
72
 *
73
73
 * MemBuffer is designed to handle relatively small data. It starts with a
74
74
 * small buffer of configurable size to avoid allocating huge buffers all the
75
75
 * time.  MemBuffer doubles the buffer when needed. It assert()s that it will
76
76
 * not grow larger than a configurable limit. MemBuffer has virtually no
77
77
 * overhead (and can even reduce memory consumption) compared to old
78
78
 * "packing" approach.
79
 
 * 
 
79
 *
80
80
 * MemBuffer eliminates both "packing" mess and truncated data:
81
 
 * 
 
81
 *
82
82
 * {
83
83
 * -- setup
84
84
 * MemBuf buf;
85
 
 * 
 
85
 *
86
86
 * -- required init with optional size tuning (see #defines for defaults)
87
87
 * buf.init(initial-size, absolute-maximum);
88
 
 * 
 
88
 *
89
89
 * -- "pack" (no need to handle offsets or check for overflows)
90
90
 * buf.Printf(...);
91
91
 * ...
92
 
 * 
 
92
 *
93
93
 * -- write
94
94
 * comm_write_mbuf(fd, buf, handler, data);
95
95
 *
96
96
 * -- *iff* you did not give the buffer away, free it yourself
97
97
 * -- buf.clean();
98
98
 * }
 
99
 \endverbatim
99
100
 */
 
101
 
100
102
/* if you have configure you can use this */
101
103
#if defined(HAVE_CONFIG_H)
102
104
#include "config.h"
122
124
 
123
125
CBDATA_CLASS_INIT(MemBuf);
124
126
 
125
 
/* init with defaults */
 
127
/** init with defaults */
126
128
void
127
129
MemBuf::init()
128
130
{
130
132
}
131
133
 
132
134
 
133
 
/* init with specific sizes */
 
135
/** init with specific sizes */
134
136
void
135
137
MemBuf::init(mb_size_t szInit, mb_size_t szMax)
136
138
{
143
145
    grow(szInit);
144
146
}
145
147
 
146
 
/*
 
148
/**
147
149
 * cleans the mb; last function to call if you do not give .buf away with
148
150
 * memBufFreeFunc
149
151
 */
162
164
    }
163
165
}
164
166
 
165
 
/* cleans the buffer without changing its capacity
166
 
 * if called with a Null buffer, calls memBufDefInit() */
 
167
/**
 
168
 * Cleans the buffer without changing its capacity
 
169
 * if called with a Null buffer, calls memBufDefInit()
 
170
 */
167
171
void
168
172
MemBuf::reset()
169
173
{
177
181
    }
178
182
}
179
183
 
180
 
/* unfortunate hack to test if the buffer has been Init()ialized */
 
184
/**
 
185
 * Unfortunate hack to test if the buffer has been Init()ialized
 
186
 */
181
187
int
182
188
MemBuf::isNull()
183
189
{
201
207
    return (terminatedSize < max_capacity) ? max_capacity - terminatedSize : 0;
202
208
}
203
209
 
204
 
// removes sz bytes and "packs" by moving content left
 
210
/// removes sz bytes and "packs" by moving content left
205
211
void MemBuf::consume(mb_size_t shiftSize)
206
212
{
207
213
    const mb_size_t cSize = contentSize();
220
226
    PROF_stop(MemBuf_consume);
221
227
}
222
228
 
223
 
// calls memcpy, appends exactly size bytes, extends buffer if needed
 
229
// removes last tailSize bytes
 
230
void MemBuf::truncate(mb_size_t tailSize)
 
231
{
 
232
    const mb_size_t cSize = contentSize();
 
233
    assert(0 <= tailSize && tailSize <= cSize);
 
234
    assert(!stolen); /* not frozen */
 
235
    size -= tailSize;
 
236
}
 
237
 
 
238
/**
 
239
 * calls memcpy, appends exactly size bytes,
 
240
 * extends buffer or creates buffer if needed.
 
241
 */
224
242
void MemBuf::append(const char *newContent, mb_size_t sz)
225
243
{
226
244
    assert(sz >= 0);
227
 
    assert(buf);
 
245
    assert(buf || (0==capacity && 0==size));
228
246
    assert(!stolen); /* not frozen */
229
247
 
230
248
    PROF_start(MemBuf_append);
241
259
    PROF_stop(MemBuf_append);
242
260
}
243
261
 
244
 
// updates content size after external append
 
262
/// updates content size after external append
245
263
void MemBuf::appended(mb_size_t sz)
246
264
{
247
265
    assert(size + sz <= capacity);
249
267
    terminate();
250
268
}
251
269
 
252
 
// 0-terminate in case we are used as a string.
253
 
// Extra octet is not counted in the content size (or space size)
254
 
// XXX: but the extra octet is counted when growth decisions are made!
255
 
// This will cause the buffer to grow when spaceSize() == 1 on append,
256
 
// which will assert() if the buffer cannot grow any more.
 
270
/**
 
271
 * Null-terminate in case we are used as a string.
 
272
 * Extra octet is not counted in the content size (or space size)
 
273
 *
 
274
 \note XXX: but the extra octet is counted when growth decisions are made!
 
275
 *     This will cause the buffer to grow when spaceSize() == 1 on append,
 
276
 *     which will assert() if the buffer cannot grow any more.
 
277
 */
257
278
void MemBuf::terminate()
258
279
{
259
280
    assert(size < capacity);
261
282
}
262
283
 
263
284
/* calls memBufVPrintf */
264
 
#if STDC_HEADERS
265
285
void
266
286
MemBuf::Printf(const char *fmt,...)
267
287
{
268
288
    va_list args;
269
289
    va_start(args, fmt);
270
 
#else
271
 
void
272
 
MemBuf::Printf(va_alist)
273
 
va_dcl
274
 
{
275
 
    va_list args;
276
 
    mb_size_t sz = 0;
277
 
    va_start(args);
278
 
    const char *fmt = va_arg(args, char *);
279
 
#endif
280
 
 
281
290
    vPrintf(fmt, args);
282
291
    va_end(args);
283
292
}
284
293
 
285
294
 
286
 
/* vPrintf for other printf()'s to use; calls vsnprintf, extends buf if needed */
 
295
/**
 
296
 * vPrintf for other printf()'s to use; calls vsnprintf, extends buf if needed
 
297
 */
287
298
void
288
 
MemBuf::vPrintf(const char *fmt, va_list vargs) {
 
299
MemBuf::vPrintf(const char *fmt, va_list vargs)
 
300
{
289
301
#ifdef VA_COPY
290
302
    va_list ap;
291
303
#endif
334
346
    }
335
347
}
336
348
 
337
 
/*
338
 
 * returns free() function to be used.
 
349
/**
339
350
 * Important:
340
351
 *   calling this function "freezes" mb,
341
352
 *   do not _update_ mb after that in any way
342
353
 *   (you still can read-access .buf and .size)
 
354
 *
 
355
 \retval free() function to be used.
343
356
 */
344
357
FREE *
345
 
MemBuf::freeFunc() {
 
358
MemBuf::freeFunc()
 
359
{
346
360
    FREE *ff;
347
361
    assert(buf);
348
362
    assert(!stolen);    /* not frozen */
352
366
    return ff;
353
367
}
354
368
 
355
 
/* grows (doubles) internal buffer to satisfy required minimal capacity */
 
369
/**
 
370
 * Grows (doubles) internal buffer to satisfy required minimal capacity
 
371
 */
356
372
void
357
 
MemBuf::grow(mb_size_t min_cap) {
 
373
MemBuf::grow(mb_size_t min_cap)
 
374
{
358
375
    size_t new_cap;
359
376
    size_t buf_cap;
360
377
 
394
411
 
395
412
/* Reports */
396
413
 
397
 
/* puts report on MemBuf _module_ usage into mb */
 
414
/**
 
415
 * Puts report on MemBuf _module_ usage into mb
 
416
 */
398
417
void
399
 
memBufReport(MemBuf * mb) {
 
418
memBufReport(MemBuf * mb)
 
419
{
400
420
    assert(mb);
401
421
    mb->Printf("memBufReport is not yet implemented @?@\n");
402
422
}