diff options
Diffstat (limited to '')
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | src/buffer.c | 34 | ||||
-rw-r--r-- | src/buffer.h | 19 | ||||
-rw-r--r-- | src/reqs.c | 14 |
4 files changed, 41 insertions, 30 deletions
@@ -1,5 +1,9 @@ 2002-05-13 Robert James Kaes <rjkaes@flarenet.com> + * src/buffer.h (_TINYPROXY_BUFFER_H_): Moved the definition of the + buffer_s structure. Data hiding is a good thing. Also, replaced + the BUFFER_SIZE macro with a buffer_size() function. + * src/vector.c: Added the "tail" pointer to make insertions more efficient. diff --git a/src/buffer.c b/src/buffer.c index 8763d37..2d9356b 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1,4 +1,4 @@ -/* $Id: buffer.c,v 1.19 2001-12-19 20:41:28 rjkaes Exp $ +/* $Id: buffer.c,v 1.20 2002-05-14 00:43:38 rjkaes Exp $ * * The buffer used in each connection is a linked list of lines. As the lines * are read in and written out the buffer expands and contracts. Basically, @@ -38,6 +38,16 @@ struct bufline_s { }; /* + * The buffer structure points to the beginning and end of the buffer list + * (and includes the total size) + */ +struct buffer_s { + struct bufline_s *head; /* top of the buffer */ + struct bufline_s *tail; /* bottom of the buffer */ + size_t size; /* total size of the buffer */ +}; + +/* * Take a string of data and a length and make a new line which can be added * to the buffer. The data IS copied, so make sure if you allocated your * data buffer on the heap, delete it because you now have TWO copies. @@ -103,7 +113,7 @@ new_buffer(void) * moment. */ BUFFER_HEAD(buffptr) = BUFFER_TAIL(buffptr) = NULL; - BUFFER_SIZE(buffptr) = 0; + buffptr->size = 0; return buffptr; } @@ -128,6 +138,14 @@ delete_buffer(struct buffer_s *buffptr) } /* + * Return the current size of the buffer. + */ +size_t buffer_size(struct buffer_s *buffptr) +{ + return buffptr->size; +} + +/* * Push a new line on to the end of the buffer. */ int @@ -144,9 +162,9 @@ add_to_buffer(struct buffer_s *buffptr, unsigned char *data, size_t length) * have a size greater than zero, and vice-versa. */ if (BUFFER_HEAD(buffptr) == NULL) - assert(BUFFER_SIZE(buffptr) == 0); + assert(buffptr->size == 0); else - assert(BUFFER_SIZE(buffptr) > 0); + assert(buffptr->size > 0); /* * Make a new line so we can add it to the buffer. @@ -154,12 +172,12 @@ add_to_buffer(struct buffer_s *buffptr, unsigned char *data, size_t length) if (!(newline = makenewline(data, length))) return -1; - if (BUFFER_SIZE(buffptr) == 0) + if (buffptr->size == 0) BUFFER_HEAD(buffptr) = BUFFER_TAIL(buffptr) = newline; else BUFFER_TAIL(buffptr) = (BUFFER_TAIL(buffptr)->next = newline); - BUFFER_SIZE(buffptr) += length; + buffptr->size += length; return 0; } @@ -200,7 +218,7 @@ read_buffer(int fd, struct buffer_s * buffptr) /* * Don't allow the buffer to grow larger than MAXBUFFSIZE */ - if (BUFFER_SIZE(buffptr) >= MAXBUFFSIZE) + if (buffptr->size >= MAXBUFFSIZE) return 0; bytesin = read(fd, buffer, READ_BUFFER_SIZE); @@ -251,7 +269,7 @@ write_buffer(int fd, struct buffer_s * buffptr) assert(fd >= 0); assert(buffptr != NULL); - if (BUFFER_SIZE(buffptr) == 0) + if (buffptr->size == 0) return 0; /* Sanity check. It would be bad to be using a NULL pointer! */ diff --git a/src/buffer.h b/src/buffer.h index 0c2506c..c72b92c 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -1,4 +1,4 @@ -/* $Id: buffer.h,v 1.7 2001-11-25 22:05:42 rjkaes Exp $ +/* $Id: buffer.h,v 1.8 2002-05-14 00:43:38 rjkaes Exp $ * * See 'buffer.c' for a detailed description. * @@ -18,23 +18,12 @@ #ifndef _TINYPROXY_BUFFER_H_ #define _TINYPROXY_BUFFER_H_ -/* - * This structure contains the total size of a buffer, plus pointers to the - * head and tail of the buffer. - */ -struct buffer_s { - struct bufline_s *head; /* top of the buffer */ - struct bufline_s *tail; /* bottom of the buffer */ - size_t size; /* total size of the buffer */ -}; - -/* - * Return the size of a buffer (pass a pointer to a buffer_s structure.) - */ -#define BUFFER_SIZE(x) (x)->size +/* Forward declaration */ +struct buffer_s; extern struct buffer_s *new_buffer(void); extern void delete_buffer(struct buffer_s *buffptr); +extern size_t buffer_size(struct buffer_s *buffptr); /* * Add a new line to the given buffer. The data IS copied into the structure. @@ -1,4 +1,4 @@ -/* $Id: reqs.c,v 1.71 2002-05-08 03:29:23 rjkaes Exp $ +/* $Id: reqs.c,v 1.72 2002-05-14 00:43:38 rjkaes Exp $ * * This is where all the work in tinyproxy is actually done. Incoming * connections have a new thread created for them. The thread then @@ -964,13 +964,13 @@ relay_connection(struct conn_s *connptr) config.idletimeout - difftime(time(NULL), last_access); tv.tv_usec = 0; - if (BUFFER_SIZE(connptr->sbuffer) > 0) + if (buffer_size(connptr->sbuffer) > 0) FD_SET(connptr->client_fd, &wset); - if (BUFFER_SIZE(connptr->cbuffer) > 0) + if (buffer_size(connptr->cbuffer) > 0) FD_SET(connptr->server_fd, &wset); - if (BUFFER_SIZE(connptr->sbuffer) < MAXBUFFSIZE) + if (buffer_size(connptr->sbuffer) < MAXBUFFSIZE) FD_SET(connptr->server_fd, &rset); - if (BUFFER_SIZE(connptr->cbuffer) < MAXBUFFSIZE) + if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE) FD_SET(connptr->client_fd, &rset); ret = select(maxfd, &rset, &wset, NULL, &tv); @@ -1026,7 +1026,7 @@ relay_connection(struct conn_s *connptr) * remainder to the client and then exit. */ socket_blocking(connptr->client_fd); - while (BUFFER_SIZE(connptr->sbuffer) > 0) { + while (buffer_size(connptr->sbuffer) > 0) { if (write_buffer(connptr->client_fd, connptr->sbuffer) < 0) break; } @@ -1035,7 +1035,7 @@ relay_connection(struct conn_s *connptr) * Try to send any remaining data to the server if we can. */ socket_blocking(connptr->server_fd); - while (BUFFER_SIZE(connptr->cbuffer) > 0) { + while (buffer_size(connptr->cbuffer) > 0) { if (write_buffer(connptr->client_fd, connptr->cbuffer) < 0) break; } |