--------------------- PatchSet 10415 Date: 2008/01/19 16:35:28 Author: adri Branch: s27_adri Tag: (none) Log: Convert the client-side connection buffer stuff to use buf_t's. This code is a bit ugly, as these buf_t's should be treated as append-only as I haven't written any "consume" logic yet. Therefore, any consume action is a "allocate new buffer and throw the unconsumed bit in there". Horribly inefficient, but it'll have to do until the rest of the client-side and request body code can be modified to use buf_t and buffer references. Members: libbuf/buf.c:1.1.2.13->1.1.2.14 libbuf/buf.h:1.1.2.7->1.1.2.8 src/client_side.c:1.202.2.9.4.40->1.202.2.9.4.41 src/structs.h:1.158.2.5.4.16->1.158.2.5.4.17 Index: squid/libbuf/buf.c =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/buf.c,v retrieving revision 1.1.2.13 retrieving revision 1.1.2.14 diff -u -r1.1.2.13 -r1.1.2.14 --- squid/libbuf/buf.c 14 Jan 2008 15:08:36 -0000 1.1.2.13 +++ squid/libbuf/buf.c 19 Jan 2008 16:35:28 -0000 1.1.2.14 @@ -31,7 +31,7 @@ } -static int +int buf_changesize(buf_t *b, int newsize) { char *p; Index: squid/libbuf/buf.h =================================================================== RCS file: /cvsroot/squid-sf//squid/libbuf/Attic/buf.h,v retrieving revision 1.1.2.7 retrieving revision 1.1.2.8 diff -u -r1.1.2.7 -r1.1.2.8 --- squid/libbuf/buf.h 28 Dec 2007 07:50:24 -0000 1.1.2.7 +++ squid/libbuf/buf.h 19 Jan 2008 16:35:28 -0000 1.1.2.8 @@ -41,6 +41,7 @@ extern int buf_make_immutable(buf_t *buf, int offset); extern int buf_append(buf_t *buf, const void *src, size_t len, buf_flags_t flags); extern int buf_grow_to_min_free(buf_t *b, int minfree); +extern int buf_changesize(buf_t *buf, int newsize); static inline int buf_len(const buf_t *buf) { return buf->len; } Index: squid/src/client_side.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/client_side.c,v retrieving revision 1.202.2.9.4.40 retrieving revision 1.202.2.9.4.41 diff -u -r1.202.2.9.4.40 -r1.202.2.9.4.41 --- squid/src/client_side.c 15 Jan 2008 14:36:44 -0000 1.202.2.9.4.40 +++ squid/src/client_side.c 19 Jan 2008 16:35:29 -0000 1.202.2.9.4.41 @@ -1,6 +1,6 @@ /* - * $Id: client_side.c,v 1.202.2.9.4.40 2008/01/15 14:36:44 adri Exp $ + * $Id: client_side.c,v 1.202.2.9.4.41 2008/01/19 16:35:29 adri Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -173,98 +173,102 @@ void clientConnBufSetup(ConnInBuf *in, int size) { - in->size = size; - in->offset = 0; - in->buf = memAllocBuf(size, &in->size); + in->inbuf = buf_create_size(size); } void clientConnBufFree(ConnInBuf *in) { - memFreeBuf(in->size, in->buf); - in->size = in->offset = 0; - in->buf = NULL; + if (in->inbuf) + in->inbuf = buf_deref(in->inbuf); } void clientConnBufGrow(ConnInBuf *in, int newsize) { - in->buf = memReallocBuf(in->buf, newsize, &in->size); - debug(33, 2) ("growing request buffer: offset=%ld size=%ld\n", - (long) in->offset, (long) in->size); + int ret; + if (! in->inbuf) + in->inbuf = buf_create_size(4096); + ret = buf_changesize(in->inbuf, newsize); + assert(ret); + debug(33, 2) ("growing request buffer: size=%ld\n", (long) newsize); } int clientConnBufFill(ConnInBuf *in, int fd) { int size; - int len = in->size - in->offset - 1; - - if (len == 0) { - /* Grow the request memory area to accomodate for a large request */ - clientConnBufGrow(in, in->size * 2); - len = in->size - in->offset - 1; - } statCounter.syscalls.sock.reads++; - size = FD_READ_METHOD(fd, in->buf + in->offset, len); - if (size > 0) { - in->offset += size; - } + if (! in->inbuf) + in->inbuf = buf_create_size(4096); + size = buf_fill(in->inbuf, fd, 1); return size; } + +/* + * This routine is extremely horrible. We "consume" a buffer by actually + * allocating a new buffer in its entirety and copying the non-consumed + * bits into that. + */ void clientConnBufConsume(ConnInBuf *in, int cbytes) { - if (cbytes == -1) - in->offset = 0; - else - in->offset -= cbytes; - debug(33, 5) ("removing %d bytes; clientConnBufLen(&conn->in) = %d\n", (int) cbytes, (int) in->offset); - if (in->offset > 0) - xmemmove(in->buf, in->buf + cbytes, in->offset); + buf_t *nb; + + /* cbytes == -1 -> consume the whole buffer */ + if (cbytes == -1 || cbytes == buf_len(in->inbuf)) { + if (in->inbuf) + in->inbuf = buf_deref(in->inbuf); + return; + } + + /* Weak referencing! Now nb holds the only reference! */ + nb = in->inbuf; in->inbuf = NULL; + in->inbuf = buf_create_size(buf_capacity(nb)); + debug(33, 5) ("removing %d bytes; len is now = %d\n", (int) cbytes, (int) buf_len(in->inbuf)); + (void) buf_append(in->inbuf, buf_buf(nb) + cbytes, buf_len(nb) - cbytes, BF_NONE); + nb = buf_deref(nb); } void clientConnBufCopy(ConnInBuf *in, char *dst, int numbytes) { - assert(numbytes <= in->offset); - xmemmove(dst, in->buf, numbytes); + assert(numbytes <= buf_len(in->inbuf)); + xmemmove(dst, buf_buf(in->inbuf), numbytes); } const char * clientConnBuf(ConnInBuf *in) { - return in->buf; + if (! in->inbuf) + return NULL; + return buf_buf(in->inbuf); } int clientConnBufLen(ConnInBuf *in) { - return in->offset; + if (! in->inbuf) + return 0; + return buf_len(in->inbuf); } int clientConnBufCapacity(ConnInBuf *in) { - return in->size; + if (! in->inbuf) + return 0; + return buf_capacity(in->inbuf); } +/* XXX this fails to make sense if the buffer is actually NULL! Bad bad adrian! Fix this! */ int clientConnBufIsFull(ConnInBuf *in) { return (clientConnBufLen(in) >= clientConnBufCapacity(in)); } -void -clientConnBufTrimWhitespace(ConnInBuf *in) -{ - while (in->offset > 0 && xisspace(in->buf[0])) { - xmemmove(in->buf, in->buf + 1, in->offset - 1); - in->offset--; - } -} - /* Temporary here while restructuring stuff */ static void storeClientCopyHeadersCB(void *data, mem_node_ref nr, ssize_t size) @@ -3973,7 +3977,7 @@ fde *F = &fd_table[fd]; ConnStateData *conn = data; if (conn->body.size_left && !F->flags.socket_eof) { - if (clientConnBufIsFull(&conn->in)) { + if (clientConnBufCapacity(&conn->in) > 0 && clientConnBufIsFull(&conn->in)) { commDeferFD(fd); return 1; } else { @@ -4280,8 +4284,9 @@ /* XXX Trim whitespace here! */ - /* Skip leading whitespace */ - clientConnBufTrimWhitespace(&conn->in); + /* XXX Skip leading whitespace! */ + //clientConnBufTrimWhitespace(&conn->in); + if (clientConnBufLen(&conn->in) == 0) { ret = 0; break; @@ -4415,7 +4420,7 @@ clientConnBufConsume(&conn->in, size); conn->body.size_left -= size; /* Resume the fd if necessary */ - if (! clientConnBufIsFull(&conn->in)) + if (! clientConnBufIsFull(&conn->in) || (clientConnBufCapacity(&conn->in) == 0)) commResumeFD(conn->fd); /* Remove request link if this is the last part of the body, as * clientReadRequest automatically continues to process next request */ Index: squid/src/structs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/structs.h,v retrieving revision 1.158.2.5.4.16 retrieving revision 1.158.2.5.4.17 diff -u -r1.158.2.5.4.16 -r1.158.2.5.4.17 --- squid/src/structs.h 14 Jan 2008 04:51:35 -0000 1.158.2.5.4.16 +++ squid/src/structs.h 19 Jan 2008 16:35:29 -0000 1.158.2.5.4.17 @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.158.2.5.4.16 2008/01/14 04:51:35 adri Exp $ + * $Id: structs.h,v 1.158.2.5.4.17 2008/01/19 16:35:29 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -1215,9 +1215,7 @@ }; struct _ConnInBuf { - char *buf; - size_t offset; - size_t size; + buf_t *inbuf; }; typedef struct _ConnInBuf ConnInBuf;