From: wessels <> Date: Thu, 1 Sep 2005 01:15:35 +0000 (+0000) Subject: Some changes and new features for MemBuf to make it more class-like X-Git-Tag: SQUID_3_0_PRE4~658 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=032785bf;p=thirdparty%2Fsquid.git Some changes and new features for MemBuf to make it more class-like - made sure that cleaning a buffer that has not been initialized yet does not lead to coredumps (this is similar to deleting a null pointer in C++). - Added consume() and append*() methods to allow the buffer to be used in a consumer/producer pipe-like environment. - Added content() and space() methods as the first step to hide buf and size members that require consume() method to always shift content to keep a copyf of buf member valid (in case somebody made a copy of it). - Noted that spaceSize() logic assumes the buffer does not expand and is 0-terminated. This means that the following does not hold: max_capacity == contentSize() + spaceSize() Fortunately, max_capacity is a private member that nobody should be using outside of MemBuf.cc 0-termination of a MemBuf? Can we make it explicit like string::c_str()? - added hasContent() method - Added public terminate() method because some HTTP parsing routines need it, unfortunately. - Added potentialSpaceSize() to report how much space can be available after the buffer is grown to the max. Useful for callers that decide whether they _will_ have the space to store new data that they can get from somewhere. - Use private copy and operator= methods to prevent us from creating a copy of a MemBuf. MemBuf copies are bad because then two ->buf pointers point to the same location and its not clear who should free the memory. - Added a destructor that asserts if someone forgot to free ->buf. Good for finding memory leaks. Restriction on not copying MemBufs must be propogated to MemBuf users. In many cases this means changing "static" MemBufs to pointers. --- diff --git a/src/HttpBody.cc b/src/HttpBody.cc index 3a6654b45a..1c8bb8ac24 100644 --- a/src/HttpBody.cc +++ b/src/HttpBody.cc @@ -1,6 +1,6 @@ /* - * $Id: HttpBody.cc,v 1.20 2003/02/21 22:50:05 robertc Exp $ + * $Id: HttpBody.cc,v 1.21 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 56 HTTP Message Body * AUTHOR: Alex Rousskov @@ -39,7 +39,7 @@ void httpBodyInit(HttpBody * body) { - body->mb = MemBufNull; + body->mb = new MemBuf; } void @@ -47,8 +47,8 @@ httpBodyClean(HttpBody * body) { assert(body); - if (!memBufIsNull(&body->mb)) - memBufClean(&body->mb); + if (!memBufIsNull(body->mb)) + memBufClean(body->mb); } /* set body by absorbing mb */ @@ -56,8 +56,8 @@ void httpBodySet(HttpBody * body, MemBuf * mb) { assert(body); - assert(memBufIsNull(&body->mb)); - body->mb = *mb; /* absorb */ + assert(memBufIsNull(body->mb)); + body->mb = mb; /* absorb */ } void @@ -65,15 +65,15 @@ httpBodyPackInto(const HttpBody * body, Packer * p) { assert(body && p); - if (body->mb.size) - packerAppend(p, body->mb.buf, body->mb.size); + if (body->mb->contentSize()) + packerAppend(p, body->mb->content(), body->mb->contentSize()); } #if UNUSED_CODE const char * httpBodyPtr(const HttpBody * body) { - return body->mb.buf ? body->mb.buf : ""; + return body->mb->content() ? body->mb->content() : ""; } #endif diff --git a/src/HttpReply.cc b/src/HttpReply.cc index dce639413d..a4925a0cf5 100644 --- a/src/HttpReply.cc +++ b/src/HttpReply.cc @@ -1,6 +1,6 @@ /* - * $Id: HttpReply.cc,v 1.71 2005/05/01 08:11:47 serassio Exp $ + * $Id: HttpReply.cc,v 1.72 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 58 HTTP Reply (Response) * AUTHOR: Alex Rousskov @@ -151,7 +151,7 @@ httpReplyParse(HttpReply * rep, const char *buf, ssize_t end) * becuase somebody may feed a non NULL-terminated buffer to * us. */ - MemBuf mb = MemBufNull; + MemBuf mb; int success; /* reset current state, because we are not used in incremental fashion */ httpReplyReset(rep); @@ -182,15 +182,15 @@ httpReplyPackInto(const HttpReply * rep, Packer * p) } /* create memBuf, create mem-based packer, pack, destroy packer, return MemBuf */ -MemBuf +MemBuf * httpReplyPack(const HttpReply * rep) { - MemBuf mb; + MemBuf *mb = new MemBuf; Packer p; assert(rep); - memBufDefInit(&mb); - packerToMemInit(&p, &mb); + memBufDefInit(mb); + packerToMemInit(&p, mb); httpReplyPackInto(rep, &p); packerClean(&p); return mb; @@ -207,14 +207,13 @@ httpReplySwapOut(HttpReply * rep, StoreEntry * e) storeEntryReplaceObject(e, rep); } -MemBuf +MemBuf * httpPackedReply(HttpVersion ver, http_status status, const char *ctype, int clen, time_t lmt, time_t expires) { HttpReply *rep = httpReplyCreate(); - MemBuf mb; httpReplySetHeaders(rep, ver, status, ctype, NULL, clen, lmt, expires); - mb = httpReplyPack(rep); + MemBuf *mb = httpReplyPack(rep); httpReplyDestroy(rep); return mb; } @@ -250,17 +249,16 @@ httpReplyMake304 (const HttpReply * rep) return rv; } -MemBuf +MemBuf * httpPacked304Reply(const HttpReply * rep) { /* Not as efficient as skipping the header duplication, * but easier to maintain */ HttpReply *temp; - MemBuf rv; assert (rep); temp = httpReplyMake304 (rep); - rv = httpReplyPack(temp); + MemBuf *rv = httpReplyPack(temp); httpReplyDestroy (temp); return rv; } diff --git a/src/HttpReply.h b/src/HttpReply.h index 22b1e7a742..9ebe781240 100644 --- a/src/HttpReply.h +++ b/src/HttpReply.h @@ -1,6 +1,6 @@ /* - * $Id: HttpReply.h,v 1.7 2004/08/30 05:12:31 robertc Exp $ + * $Id: HttpReply.h,v 1.8 2005/08/31 19:15:35 wessels Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -53,17 +53,17 @@ extern void httpReplyPackHeadersInto(const HttpReply * rep, Packer * p); extern void httpReplyPackInto(const HttpReply * rep, Packer * p); /* ez-routines */ /* mem-pack: returns a ready to use mem buffer with a packed reply */ -extern MemBuf httpReplyPack(const HttpReply * rep); +extern MemBuf *httpReplyPack(const HttpReply * rep); /* swap: create swap-based packer, pack, destroy packer */ extern void httpReplySwapOut(HttpReply * rep, StoreEntry * e); /* set commonly used info with one call */ extern void httpReplySetHeaders(HttpReply * rep, HttpVersion ver, http_status status, const char *reason, const char *ctype, int clen, time_t lmt, time_t expires); /* do everything in one call: init, set, pack, clean, return MemBuf */ -extern MemBuf httpPackedReply(HttpVersion ver, http_status status, const char *ctype, - int clen, time_t lmt, time_t expires); +extern MemBuf *httpPackedReply(HttpVersion ver, http_status status, const char *ctype, + int clen, time_t lmt, time_t expires); /* construct 304 reply and pack it into MemBuf, return MemBuf */ -extern MemBuf httpPacked304Reply(const HttpReply * rep); +extern MemBuf *httpPacked304Reply(const HttpReply * rep); /* construct a 304 reply and return it */ extern HttpReply *httpReplyMake304(const HttpReply *rep); /* update when 304 reply is received for a cached object */ diff --git a/src/MemBuf.cc b/src/MemBuf.cc index e806e1a1b5..cf3e0329ad 100644 --- a/src/MemBuf.cc +++ b/src/MemBuf.cc @@ -1,6 +1,6 @@ /* - * $Id: MemBuf.cc,v 1.37 2004/12/21 17:52:53 robertc Exp $ + * $Id: MemBuf.cc,v 1.38 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 59 auto-growing Memory Buffer with printf * AUTHOR: Alex Rousskov @@ -156,12 +156,17 @@ void memBufClean(MemBuf * mb) { assert(mb); - assert(mb->buf); - assert(!mb->stolen); /* not frozen */ - memFreeBuf(mb->capacity, mb->buf); - mb->buf = NULL; - mb->size = mb->capacity = mb->max_capacity = 0; + if (memBufIsNull(mb)) { + // nothing to do + } else { + assert(mb->buf); + assert(!mb->stolen); /* not frozen */ + + memFreeBuf(mb->capacity, mb->buf); + mb->buf = NULL; + mb->size = mb->capacity = mb->max_capacity = 0; + } } /* cleans the buffer without changing its capacity @@ -195,29 +200,80 @@ memBufIsNull(MemBuf * mb) return 0; } +mb_size_t MemBuf::spaceSize() const +{ + const mb_size_t terminatedSize = size + 1; + return (terminatedSize < capacity) ? capacity - terminatedSize : 0; +} -/* calls memcpy, appends exactly size bytes, extends buffer if needed */ -void -memBufAppend(MemBuf * mb, const char *buf, mb_size_t sz) +mb_size_t MemBuf::potentialSpaceSize() const { - assert(mb && buf && sz >= 0); - assert(mb->buf); - assert(!mb->stolen); /* not frozen */ + const mb_size_t terminatedSize = size + 1; + return (terminatedSize < max_capacity) ? max_capacity - terminatedSize : 0; +} - if (sz > 0) { - if (mb->size + sz + 1 > mb->capacity) - memBufGrow(mb, mb->size + sz + 1); +// removes sz bytes and "packs" by moving content left +void MemBuf::consume(mb_size_t shiftSize) +{ + const mb_size_t cSize = contentSize(); + assert(0 <= shiftSize && shiftSize <= cSize); + assert(!stolen); /* not frozen */ - assert(mb->size + sz <= mb->capacity); /* paranoid */ + if (shiftSize > 0) { + if (shiftSize < cSize) + xmemmove(buf, buf + shiftSize, cSize - shiftSize); - xmemcpy(mb->buf + mb->size, buf, sz); + size -= shiftSize; - mb->size += sz; + terminate(); + } +} - mb->buf[mb->size] = '\0'; /* \0 terminate in case we are used as a string. Not counted in the size */ +// calls memcpy, appends exactly size bytes, extends buffer if needed +void MemBuf::append(const char *newContent, mb_size_t sz) +{ + assert(sz >= 0); + assert(buf); + assert(!stolen); /* not frozen */ + + if (sz > 0) { + if (size + sz + 1 > capacity) + memBufGrow(this, size + sz + 1); + + assert(size + sz <= capacity); /* paranoid */ + + xmemcpy(space(), newContent, sz); + + appended(sz); } } +// updates content size after external append +void MemBuf::appended(mb_size_t sz) +{ + assert(size + sz <= capacity); + size += sz; + terminate(); +} + +// 0-terminate in case we are used as a string. +// Extra octet is not counted in the content size (or space size) +// XXX: but the extra octet is counted when growth decisions are made! +// This will cause the buffer to grow when spaceSize() == 1 on append, +// which will assert() if the buffer cannot grow any more. +void MemBuf::terminate() +{ + assert(size < capacity); + *space() = '\0'; +} + +void +memBufAppend(MemBuf * mb, const char *buf, mb_size_t sz) +{ + assert(mb); + mb->append(buf, sz); +} + /* calls memBufVPrintf */ #if STDC_HEADERS void diff --git a/src/MemBuf.cci b/src/MemBuf.cci index e1d8278c77..8f81408bdc 100644 --- a/src/MemBuf.cci +++ b/src/MemBuf.cci @@ -1,6 +1,6 @@ /* - * $Id: MemBuf.cci,v 1.1 2003/01/23 00:37:14 robertc Exp $ + * $Id: MemBuf.cci,v 1.2 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 59 auto-growing Memory Buffer with printf * AUTHOR: Robert Collins @@ -34,5 +34,10 @@ */ MemBuf::MemBuf() : buf (NULL), size (0), max_capacity (0), capacity(0), stolen(0) +{} + +MemBuf::~MemBuf() { + if (!stolen) + assert(NULL == buf); } diff --git a/src/MemBuf.h b/src/MemBuf.h index 19419db94b..36a5578a97 100644 --- a/src/MemBuf.h +++ b/src/MemBuf.h @@ -1,6 +1,5 @@ - /* - * $Id: MemBuf.h,v 1.2 2003/02/21 22:50:06 robertc Exp $ + * $Id: MemBuf.h,v 1.3 2005/08/31 19:15:35 wessels Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -42,11 +41,53 @@ class MemBuf public: _SQUID_INLINE_ MemBuf(); - /* public, read-only */ - char *buf; - mb_size_t size; /* used space, does not count 0-terminator */ + _SQUID_INLINE_ ~MemBuf(); + + /* use methods instead of deprecated buf and size members */ + + char *content() { return buf; } // start of the added data + + mb_size_t contentSize() const { return size; } // available data size + + bool hasContent() const { return size > 0; } + + // these space-related methods assume no growth and allow 0-termination + char *space() { return buf + size; } // space to add data + + mb_size_t spaceSize() const; + bool hasSpace() const { return size+1 < capacity; } + + mb_size_t potentialSpaceSize() const; // accounts for possible growth + bool hasPotentialSpace() const { return potentialSpaceSize() > 0; } + + // there is currently no stretch() method to grow without appending + + void consume(mb_size_t sz); // removes sz bytes, moving content left + void append(const char *c, mb_size_t sz); // grows if needed and possible + void appended(mb_size_t sz); // updates content size after external append + + // XXX: convert global memBuf*() functions into methods + + void terminate(); // zero-terminates the buffer w/o increasing contentSize + +private: + /* + * private copy constructor and assignment operator generates + * compiler errors if someone tries to copy/assign a MemBuf + */ + MemBuf(const MemBuf& m) {assert(false);}; + + MemBuf& operator= (const MemBuf& m) {assert(false); return *this;}; + +public: + /* public, read-only, depricated in favor of space*() and content*() */ + // XXX: hide these members completely and remove 0-termination + // so that consume() does not need to memmove all the time + char *buf; // available content + mb_size_t size; // used space, does not count 0-terminator /* private, stay away; use interface function instead */ + // XXX: make these private after converting memBuf*() functions to methods mb_size_t max_capacity; /* when grows: assert(new_capacity <= max_capacity) */ mb_size_t capacity; /* allocated space */ @@ -54,9 +95,6 @@ unsigned stolen: 1; /* the buffer has been stolen for use by someone else */ }; -/* to initialize static variables (see also MemBufNull) */ -#define MemBufNULL MemBuf(); - #ifdef _USE_INLINE_ #include "MemBuf.cci" #endif @@ -88,6 +126,4 @@ SQUIDCEXTERN FREE *memBufFreeFunc(MemBuf * mb); /* puts report on MemBuf _module_ usage into mb */ SQUIDCEXTERN void memBufReport(MemBuf * mb); -#define MemBufNull MemBuf(); - #endif /* SQUID_MEM_H */ diff --git a/src/access_log.cc b/src/access_log.cc index 88e39d1fab..14c054ba05 100644 --- a/src/access_log.cc +++ b/src/access_log.cc @@ -1,6 +1,6 @@ /* - * $Id: access_log.cc,v 1.101 2004/12/26 11:31:11 serassio Exp $ + * $Id: access_log.cc,v 1.102 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 46 Access Log * AUTHOR: Duane Wessels @@ -504,7 +504,7 @@ accessLogCustom(AccessLogEntry * al, customlog * log) logformat *lf; Logfile *logfile; logformat_token *fmt; - static MemBuf mb = MemBufNULL; + static MemBuf mb; char tmp[1024]; String sb; diff --git a/src/client_side.cc b/src/client_side.cc index 5f740570cb..4dd6abac37 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -1,6 +1,6 @@ /* - * $Id: client_side.cc,v 1.689 2005/08/31 17:21:58 wessels Exp $ + * $Id: client_side.cc,v 1.690 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -812,9 +812,9 @@ ClientSocketContext::sendBody(HttpReply * rep, StoreIOBuffer bodyData) memBufDefInit(&mb); packRange(bodyData, &mb); - if (mb.size) + if (mb.contentSize()) /* write */ - comm_old_write_mbuf(fd(), mb, clientWriteComplete, this); + comm_old_write_mbuf(fd(), &mb, clientWriteComplete, this); else writeComplete(fd(), NULL, 0, COMM_OK); } @@ -1168,11 +1168,10 @@ void ClientSocketContext::sendStartOfMessage(HttpReply * rep, StoreIOBuffer bodyData) { prepareReply(rep); - /* init mb; put status line and headers if any */ assert (rep); - MemBuf mb = httpReplyPack(rep); + MemBuf *mb = httpReplyPack(rep); /* Save length of headers for persistent conn checks */ - http->out.headers_sz = mb.size; + http->out.headers_sz = mb->contentSize(); #if HEADERS_LOG headersLog(0, 0, http->request->method, rep); @@ -1183,16 +1182,16 @@ ClientSocketContext::sendStartOfMessage(HttpReply * rep, StoreIOBuffer bodyData) size_t length = lengthToSend(bodyData.range()); noteSentBodyBytes (length); - memBufAppend(&mb, bodyData.data, length); + memBufAppend(mb, bodyData.data, length); } else { - packRange(bodyData, &mb); + packRange(bodyData, mb); } } /* write */ comm_old_write_mbuf(fd(), mb, clientWriteComplete, this); - /* if we don't do it, who will? */ + delete mb; } /* diff --git a/src/comm.cc b/src/comm.cc index d5694b26b7..4d4cc597e3 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -1,6 +1,6 @@ /* - * $Id: comm.cc,v 1.408 2005/08/27 18:40:20 serassio Exp $ + * $Id: comm.cc,v 1.409 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 5 Socket Functions * AUTHOR: Harvest Derived @@ -2159,8 +2159,8 @@ comm_old_write(int fd, const char *buf, int size, CWCB * handler, void *handler_ /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */ void -comm_old_write_mbuf(int fd, MemBuf mb, CWCB * handler, void *handler_data) { - comm_old_write(fd, mb.buf, mb.size, handler, handler_data, memBufFreeFunc(&mb)); +comm_old_write_mbuf(int fd, MemBuf *mb, CWCB * handler, void *handler_data) { + comm_old_write(fd, mb->buf, mb->size, handler, handler_data, memBufFreeFunc(mb)); } diff --git a/src/dns_internal.cc b/src/dns_internal.cc index 74be010085..4cf6118fed 100644 --- a/src/dns_internal.cc +++ b/src/dns_internal.cc @@ -1,6 +1,6 @@ /* - * $Id: dns_internal.cc,v 1.79 2005/08/28 08:55:21 serassio Exp $ + * $Id: dns_internal.cc,v 1.80 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 78 DNS lookups; interacts with lib/rfc1035.c * AUTHOR: Duane Wessels @@ -91,8 +91,8 @@ struct _nsvc int fd; unsigned short msglen; int read_msglen; - MemBuf msg; - MemBuf queue; + MemBuf *msg; + MemBuf *queue; bool busy; }; @@ -510,18 +510,20 @@ idnsDoSendQueryVC(nsvc *vc) if (vc->busy) return; - if (vc->queue.size == 0) + if (vc->queue->contentSize() == 0) return; - MemBuf mb = vc->queue; + MemBuf *mb = vc->queue; - vc->queue = MemBufNULL; + vc->queue = new MemBuf; vc->busy = 1; commSetTimeout(vc->fd, Config.Timeout.idns_query, NULL, NULL); comm_old_write_mbuf(vc->fd, mb, idnsSentQueryVC, vc); + + delete mb; } static void @@ -543,6 +545,9 @@ static void idnsVCClosed(int fd, void *data) { nsvc * vc = (nsvc *)data; + delete vc->queue; + delete vc->msg; + // XXX need to free and/or cbdataReferenceDone(vc) ? nameservers[vc->ns].vc = NULL; } @@ -559,9 +564,9 @@ idnsInitVC(int ns) else addr = Config.Addrs.udp_incoming; - vc->queue = MemBufNULL; + vc->queue = new MemBuf; - vc->msg = MemBufNULL; + vc->msg = new MemBuf; vc->fd = comm_open(SOCK_STREAM, IPPROTO_TCP, @@ -588,14 +593,13 @@ idnsSendQueryVC(idns_query * q, int ns) nsvc *vc = nameservers[ns].vc; - if (memBufIsNull(&vc->queue)) - memBufDefInit(&vc->queue); + memBufReset(vc->queue); short head = htons(q->sz); - memBufAppend(&vc->queue, (char *)&head, 2); + memBufAppend(vc->queue, (char *)&head, 2); - memBufAppend(&vc->queue, q->buf, q->sz); + memBufAppend(vc->queue, q->buf, q->sz); idnsDoSendQueryVC(vc); } @@ -937,20 +941,20 @@ idnsReadVC(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *dat return; } - vc->msg.size += len; + vc->msg->size += len; // XXX should not access -> size directly - if (vc->msg.size < vc->msglen) { - comm_read(fd, buf + len, vc->msglen - vc->msg.size, idnsReadVC, vc); + if (vc->msg->contentSize() < vc->msglen) { + comm_read(fd, buf + len, vc->msglen - vc->msg->contentSize(), idnsReadVC, vc); return; } debug(78, 3) ("idnsReadVC: FD %d: received %d bytes via tcp from %s.\n", fd, - (int) vc->msg.size, + (int) vc->msg->contentSize(), inet_ntoa(nameservers[vc->ns].S.sin_addr)); - idnsGrokReply(vc->msg.buf, vc->msg.size); - memBufClean(&vc->msg); + idnsGrokReply(vc->msg->buf, vc->msg->contentSize()); + memBufClean(vc->msg); comm_read(fd, (char *)&vc->msglen, 2 , idnsReadVCHeader, vc); } @@ -980,8 +984,8 @@ idnsReadVCHeader(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, voi vc->msglen = ntohs(vc->msglen); - memBufInit(&vc->msg, vc->msglen, vc->msglen); - comm_read(fd, vc->msg.buf, vc->msglen, idnsReadVC, vc); + memBufInit(vc->msg, vc->msglen, vc->msglen); + comm_read(fd, vc->msg->buf, vc->msglen, idnsReadVC, vc); } /* diff --git a/src/errorpage.cc b/src/errorpage.cc index 6a30bc23d9..7170fc3c9e 100644 --- a/src/errorpage.cc +++ b/src/errorpage.cc @@ -1,6 +1,6 @@ /* - * $Id: errorpage.cc,v 1.201 2005/05/01 08:11:48 serassio Exp $ + * $Id: errorpage.cc,v 1.202 2005/08/31 19:15:35 wessels Exp $ * * DEBUG: section 4 Error Generation * AUTHOR: Duane Wessels @@ -101,7 +101,7 @@ static char *errorLoadText(const char *page_name); static const char *errorFindHardText(err_type type); static ErrorDynamicPageInfo *errorDynamicPageInfoCreate(int id, const char *page_name); static void errorDynamicPageInfoDestroy(ErrorDynamicPageInfo * info); -static MemBuf errorBuildContent(ErrorState * err); +static MemBuf *errorBuildContent(ErrorState * err); static int errorDump(ErrorState * err, MemBuf * mb); static const char *errorConvert(char token, ErrorState * err); static CWCB errorSendComplete; @@ -483,7 +483,7 @@ static int errorDump(ErrorState * err, MemBuf * mb) { HttpRequest *r = err->request; - MemBuf str = MemBufNULL; + MemBuf str; const char *p = NULL; /* takes priority over mb if set */ memBufReset(&str); /* email subject line */ @@ -593,7 +593,7 @@ static const char * errorConvert(char token, ErrorState * err) { HttpRequest *r = err->request; - static MemBuf mb = MemBufNULL; + static MemBuf mb; const char *p = NULL; /* takes priority over mb if set */ int do_quote = 1; @@ -747,11 +747,11 @@ errorConvert(char token, ErrorState * err) if (err->page_id != ERR_SQUID_SIGNATURE) { const int saved_id = err->page_id; - MemBuf sign_mb; err->page_id = ERR_SQUID_SIGNATURE; - sign_mb = errorBuildContent(err); - memBufPrintf(&mb, "%s", sign_mb.buf); - memBufClean(&sign_mb); + MemBuf *sign_mb = errorBuildContent(err); + memBufPrintf(&mb, "%s", sign_mb->content()); + memBufClean(sign_mb); + delete sign_mb; err->page_id = saved_id; do_quote = 0; } else { @@ -854,8 +854,8 @@ errorBuildReply(ErrorState * err) httpHeaderPutStrf(&rep->header, HDR_X_SQUID_ERROR, "%d %s\n", err->httpStatus, "Access Denied"); } else { - MemBuf content = errorBuildContent(err); - httpReplySetHeaders(rep, version, err->httpStatus, NULL, "text/html", content.size, 0, squid_curtime); + MemBuf *content = errorBuildContent(err); + httpReplySetHeaders(rep, version, err->httpStatus, NULL, "text/html", content->contentSize(), 0, squid_curtime); /* * include some information for downstream caches. Implicit * replaceable content. This isn't quite sufficient. xerrno is not @@ -866,37 +866,37 @@ errorBuildReply(ErrorState * err) */ httpHeaderPutStrf(&rep->header, HDR_X_SQUID_ERROR, "%s %d", name, err->xerrno); - httpBodySet(&rep->body, &content); - /* do not memBufClean() the content, it was absorbed by httpBody */ + httpBodySet(&rep->body, content); + /* do not memBufClean() or delete the content, it was absorbed by httpBody */ } return rep; } -static MemBuf +static MemBuf * errorBuildContent(ErrorState * err) { - MemBuf content; + MemBuf *content = new MemBuf; const char *m; const char *p; const char *t; assert(err != NULL); assert(err->page_id > ERR_NONE && err->page_id < error_page_count); - memBufDefInit(&content); + memBufDefInit(content); m = error_text[err->page_id]; assert(m); while ((p = strchr(m, '%'))) { - memBufAppend(&content, m, p - m); /* copy */ + memBufAppend(content, m, p - m); /* copy */ t = errorConvert(*++p, err); /* convert */ - memBufPrintf(&content, "%s", t); /* copy */ + memBufPrintf(content, "%s", t); /* copy */ m = p + 1; /* advance */ } if (*m) - memBufPrintf(&content, "%s", m); /* copy tail */ + memBufPrintf(content, "%s", m); /* copy tail */ - assert((size_t)content.size == strlen(content.buf)); + assert((size_t)content->contentSize() == strlen(content->content())); return content; } diff --git a/src/external_acl.cc b/src/external_acl.cc index 9141e756d6..27d42fdb3a 100644 --- a/src/external_acl.cc +++ b/src/external_acl.cc @@ -1,6 +1,6 @@ /* - * $Id: external_acl.cc,v 1.63 2005/05/06 01:57:55 hno Exp $ + * $Id: external_acl.cc,v 1.64 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 82 External ACL * AUTHOR: Henrik Nordstrom, MARA Systems AB @@ -734,7 +734,7 @@ external_acl_cache_touch(external_acl * def, external_acl_entry * entry) static char * makeExternalAclKey(ACLChecklist * ch, external_acl_data * acl_data) { - static MemBuf mb = MemBufNULL; + static MemBuf mb; char buf[256]; int first = 1; wordlist *arg; diff --git a/src/helper.cc b/src/helper.cc index ee0db7c81e..dcd19002c4 100644 --- a/src/helper.cc +++ b/src/helper.cc @@ -1,6 +1,6 @@ /* - * $Id: helper.cc,v 1.65 2005/01/27 19:57:09 serassio Exp $ + * $Id: helper.cc,v 1.66 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 84 Helper process maintenance * AUTHOR: Harvest Derived? @@ -765,8 +765,14 @@ helperServerFree(int fd, void *data) srv->rbuf = NULL; } - if (!memBufIsNull(&srv->wqueue)) - memBufClean(&srv->wqueue); + memBufClean(srv->wqueue); + delete srv->wqueue; + + if (srv->writebuf) { + memBufClean(srv->writebuf); + delete srv->writebuf; + srv->writebuf = NULL; + } for (i = 0; i < concurrency; i++) { if ((r = srv->requests[i])) { @@ -828,8 +834,9 @@ helperStatefulServerFree(int fd, void *data) } #if 0 - if (!memBufIsNull(&srv->wqueue)) - memBufClean(&srv->wqueue); + memBufClean(srv->wqueue); + + delete srv->wqueue; #endif @@ -1311,7 +1318,9 @@ helperDispatchWriteDone(int fd, char *buf, size_t len, comm_err_t flag, int xerr { helper_server *srv = (helper_server *)data; - memBufClean(&srv->writebuf); + memBufClean(srv->writebuf); + delete srv->writebuf; + srv->writebuf = NULL; srv->flags.writing = 0; if (flag != COMM_OK) { @@ -1321,13 +1330,13 @@ helperDispatchWriteDone(int fd, char *buf, size_t len, comm_err_t flag, int xerr return; } - if (!memBufIsNull(&srv->wqueue)) { + if (!memBufIsNull(srv->wqueue)) { srv->writebuf = srv->wqueue; - srv->wqueue = MemBufNull; + srv->wqueue = new MemBuf; srv->flags.writing = 1; comm_write(srv->wfd, - srv->writebuf.buf, - srv->writebuf.size, + srv->writebuf->content(), + srv->writebuf->contentSize(), helperDispatchWriteDone, /* Handler */ srv); /* Handler-data */ } @@ -1358,21 +1367,22 @@ helperDispatch(helper_server * srv, helper_request * r) srv->stats.pending += 1; r->dispatch_time = current_time; - if (memBufIsNull(&srv->wqueue)) - memBufDefInit(&srv->wqueue); + if (memBufIsNull(srv->wqueue)) + memBufDefInit(srv->wqueue); if (hlp->concurrency) - memBufPrintf(&srv->wqueue, "%d %s", slot, r->buf); + memBufPrintf(srv->wqueue, "%d %s", slot, r->buf); else - memBufAppend(&srv->wqueue, r->buf, strlen(r->buf)); + memBufAppend(srv->wqueue, r->buf, strlen(r->buf)); if (!srv->flags.writing) { + assert(NULL == srv->writebuf); srv->writebuf = srv->wqueue; - srv->wqueue = MemBufNull; + srv->wqueue = new MemBuf; srv->flags.writing = 1; comm_write(srv->wfd, - srv->writebuf.buf, - srv->writebuf.size, + srv->writebuf->content(), + srv->writebuf->contentSize(), helperDispatchWriteDone, /* Handler */ srv); /* Handler-data */ } diff --git a/src/http.cc b/src/http.cc index 8b86b53d82..197e1740a1 100644 --- a/src/http.cc +++ b/src/http.cc @@ -1,6 +1,6 @@ /* - * $Id: http.cc,v 1.454 2005/08/19 17:03:28 wessels Exp $ + * $Id: http.cc,v 1.455 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 11 Hypertext Transfer Protocol (HTTP) * AUTHOR: Harvest Derived @@ -1720,7 +1720,7 @@ httpSendRequest(HttpStateData * httpState) &mb, httpState->flags); debug(11, 6) ("httpSendRequest: FD %d:\n%s\n", fd, mb.buf); - comm_old_write_mbuf(fd, mb, sendHeaderDone, httpState); + comm_old_write_mbuf(fd, &mb, sendHeaderDone, httpState); } void diff --git a/src/ident.cc b/src/ident.cc index 00da131480..0054139fb6 100644 --- a/src/ident.cc +++ b/src/ident.cc @@ -1,6 +1,6 @@ /* - * $Id: ident.cc,v 1.69 2003/03/04 01:40:28 robertc Exp $ + * $Id: ident.cc,v 1.70 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 30 Ident (RFC 931) * AUTHOR: Duane Wessels @@ -115,7 +115,6 @@ identConnectDone(int fd, comm_err_t status, int xerrno, void *data) { IdentStateData *state = (IdentStateData *)data; IdentClient *c; - MemBuf mb; if (status != COMM_OK) { /* Failed to connect */ @@ -137,11 +136,12 @@ identConnectDone(int fd, comm_err_t status, int xerrno, void *data) return; } + MemBuf mb; memBufDefInit(&mb); memBufPrintf(&mb, "%d, %d\r\n", ntohs(state->my_peer.sin_port), ntohs(state->me.sin_port)); - comm_old_write_mbuf(fd, mb, NULL, state); + comm_old_write_mbuf(fd, &mb, NULL, state); comm_read(fd, state->buf, BUFSIZ, identReadReply, state); commSetTimeout(fd, Config.Timeout.ident, identTimeout, state); } diff --git a/src/internal.cc b/src/internal.cc index 9651d2779e..ba9159e707 100644 --- a/src/internal.cc +++ b/src/internal.cc @@ -1,6 +1,6 @@ /* - * $Id: internal.cc,v 1.30 2003/09/01 03:49:39 robertc Exp $ + * $Id: internal.cc,v 1.31 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 76 Internal Squid Object handling * AUTHOR: Duane, Alex, Henrik @@ -100,7 +100,6 @@ internalStaticCheck(const char *urlpath) char * internalRemoteUri(const char *host, u_short port, const char *dir, const char *name) { - static MemBuf mb = MemBufNULL; static char lc_host[SQUIDHOSTNAMELEN]; assert(host && name); /* convert host name to lower case */ @@ -116,6 +115,8 @@ internalRemoteUri(const char *host, u_short port, const char *dir, const char *n strlen(lc_host) - 1); /* build uri in mb */ + static MemBuf mb; + memBufReset(&mb); memBufPrintf(&mb, "http://%s", lc_host); diff --git a/src/mime.cc b/src/mime.cc index 41f0101514..92abd8dcd7 100644 --- a/src/mime.cc +++ b/src/mime.cc @@ -1,6 +1,6 @@ /* - * $Id: mime.cc,v 1.116 2005/03/06 14:46:30 serassio Exp $ + * $Id: mime.cc,v 1.117 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 25 MIME Parsing * AUTHOR: Harvest Derived @@ -330,16 +330,16 @@ mimeGetIcon(const char *fn) const char * mimeGetIconURL(const char *fn) { - static MemBuf mb = MemBufNULL; char const *icon = mimeGetIcon(fn); if (icon == NULL) return null_string; if (Config.icons.use_short_names) { + static MemBuf mb; memBufReset(&mb); memBufPrintf(&mb, "/squid-internal-static/icons/%s", icon); - return mb.buf; + return mb.content(); } else { return internalLocalUri("/squid-internal-static/icons/", icon); } diff --git a/src/protos.h b/src/protos.h index 561723c476..573ee96462 100644 --- a/src/protos.h +++ b/src/protos.h @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.507 2005/08/27 19:36:36 serassio Exp $ + * $Id: protos.h,v 1.508 2005/08/31 19:15:36 wessels Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -144,7 +144,7 @@ SQUIDCEXTERN void comm_old_write(int fd, CWCB * handler, void *handler_data, FREE *); -SQUIDCEXTERN void comm_old_write_mbuf(int fd, MemBuf mb, CWCB * handler, void *handler_data); +SQUIDCEXTERN void comm_old_write_mbuf(int fd, MemBuf *mb, CWCB * handler, void *handler_data); SQUIDCEXTERN void commCallCloseHandlers(int fd); SQUIDCEXTERN int commSetTimeout(int fd, int, PF *, void *); SQUIDCEXTERN int ignoreErrno(int); diff --git a/src/structs.h b/src/structs.h index e5f2904573..714191889f 100644 --- a/src/structs.h +++ b/src/structs.h @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.523 2005/08/19 17:03:28 wessels Exp $ + * $Id: structs.h,v 1.524 2005/08/31 19:15:36 wessels Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -864,7 +864,7 @@ struct _Packer struct _HttpBody { /* private */ - MemBuf mb; + MemBuf *mb; }; #include "SquidString.h" @@ -1986,8 +1986,8 @@ struct _helper_server int pid; int rfd; int wfd; - MemBuf wqueue; - MemBuf writebuf; + MemBuf *wqueue; + MemBuf *writebuf; char *rbuf; size_t rbuf_sz; off_t roffset; diff --git a/src/tunnel.cc b/src/tunnel.cc index d352c8197e..b083e50cb8 100644 --- a/src/tunnel.cc +++ b/src/tunnel.cc @@ -1,6 +1,6 @@ /* - * $Id: tunnel.cc,v 1.150 2005/06/09 16:04:30 serassio Exp $ + * $Id: tunnel.cc,v 1.151 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 26 Secure Sockets Layer Proxy * AUTHOR: Duane Wessels @@ -680,13 +680,13 @@ static void sslProxyConnected(int fd, void *data) { SslStateData *sslState = (SslStateData *)data; - MemBuf mb; HttpHeader hdr_out(hoRequest); Packer p; http_state_flags flags; debug(26, 3) ("sslProxyConnected: FD %d sslState=%p\n", fd, sslState); memset(&flags, '\0', sizeof(flags)); flags.proxying = sslState->request->flags.proxying; + MemBuf mb; memBufDefInit(&mb); memBufPrintf(&mb, "CONNECT %s HTTP/1.0\r\n", sslState->url); httpBuildRequestHeader(sslState->request, @@ -700,7 +700,7 @@ sslProxyConnected(int fd, void *data) packerClean(&p); memBufAppend(&mb, "\r\n", 2); - comm_old_write_mbuf(sslState->server.fd(), mb, sslProxyConnectedWriteDone, sslState); + comm_old_write_mbuf(sslState->server.fd(), &mb, sslProxyConnectedWriteDone, sslState); commSetTimeout(sslState->server.fd(), Config.Timeout.read, diff --git a/src/urn.cc b/src/urn.cc index cb400e4ad7..ce5e6a7cc8 100644 --- a/src/urn.cc +++ b/src/urn.cc @@ -1,6 +1,6 @@ /* - * $Id: urn.cc,v 1.86 2003/09/01 03:49:40 robertc Exp $ + * $Id: urn.cc,v 1.87 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 52 URN Parsing * AUTHOR: Kostas Anagnostakis @@ -311,7 +311,7 @@ urnHandleReply(void *data, StoreIOBuffer result) url_entry *urls; url_entry *u; url_entry *min_u; - MemBuf mb; + MemBuf *mb = NULL; ErrorState *err; int i; int urlcnt = 0; @@ -404,8 +404,9 @@ urnHandleReply(void *data, StoreIOBuffer result) min_u = urnFindMinRtt(urls, urnState->request->method, NULL); qsort(urls, urlcnt, sizeof(*urls), url_entry_sort); storeBuffer(e); - memBufDefInit(&mb); - memBufPrintf(&mb, + mb = new MemBuf; + memBufDefInit(mb); + memBufPrintf(mb, "Select URL for %s\n" "\n" "

Select URL for %s

\n" @@ -414,20 +415,20 @@ urnHandleReply(void *data, StoreIOBuffer result) for (i = 0; i < urlcnt; i++) { u = &urls[i]; debug(52, 3) ("URL {%s}\n", u->url); - memBufPrintf(&mb, + memBufPrintf(mb, "%s", u->url, u->url); if (urls[i].rtt > 0) - memBufPrintf(&mb, + memBufPrintf(mb, "%4d ms", u->rtt); else - memBufPrintf(&mb, "Unknown"); + memBufPrintf(mb, "Unknown"); - memBufPrintf(&mb, + memBufPrintf(mb, "%s\n", u->flags.cached ? " [cached]" : " "); } - memBufPrintf(&mb, + memBufPrintf(mb, "" "
\n" "
\n" @@ -436,7 +437,7 @@ urnHandleReply(void *data, StoreIOBuffer result) full_appname_string, getMyHostname()); rep = httpReplyCreate(); httpReplySetHeaders(rep, version, HTTP_MOVED_TEMPORARILY, NULL, - "text/html", mb.size, 0, squid_curtime); + "text/html", mb->contentSize(), 0, squid_curtime); if (urnState->flags.force_menu) { debug(51, 3) ("urnHandleReply: forcing menu\n"); @@ -444,7 +445,8 @@ urnHandleReply(void *data, StoreIOBuffer result) httpHeaderPutStr(&rep->header, HDR_LOCATION, min_u->url); } - httpBodySet(&rep->body, &mb); + httpBodySet(&rep->body, mb); + /* don't clean or delete mb; rep->body owns it now */ httpReplySwapOut(rep, e); e->complete(); diff --git a/src/wais.cc b/src/wais.cc index 605b8784ae..d1e7eb11fb 100644 --- a/src/wais.cc +++ b/src/wais.cc @@ -1,6 +1,6 @@ /* - * $Id: wais.cc,v 1.150 2003/08/10 11:00:45 robertc Exp $ + * $Id: wais.cc,v 1.151 2005/08/31 19:15:36 wessels Exp $ * * DEBUG: section 24 WAIS Relay * AUTHOR: Harvest Derived @@ -240,7 +240,7 @@ waisSendRequest(int fd, void *data) memBufPrintf(&mb, "\r\n"); debug(24, 6) ("waisSendRequest: buf: %s\n", mb.buf); - comm_old_write_mbuf(fd, mb, waisSendComplete, waisState); + comm_old_write_mbuf(fd, &mb, waisSendComplete, waisState); if (EBIT_TEST(waisState->entry->flags, ENTRY_CACHABLE)) storeSetPublicKey(waisState->entry); /* Make it public */