From bb9f20652998ec87d364c59de88169b531092558 Mon Sep 17 00:00:00 2001 From: Robert James Kaes Date: Tue, 14 May 2002 00:43:38 +0000 Subject: Moved the definition of the struct buffer_s into the buffer.c file and out of the buffer.h file. This also required the removal of the BUFFER_SIZE macro, and replace it with the buffer_size() function. --- src/buffer.c | 34 ++++++++++++++++++++++++++-------- src/buffer.h | 19 ++++--------------- src/reqs.c | 14 +++++++------- 3 files changed, 37 insertions(+), 30 deletions(-) (limited to 'src') 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, @@ -37,6 +37,16 @@ struct bufline_s { size_t pos; /* start sending from this offset */ }; +/* + * 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 @@ -103,7 +113,7 @@ new_buffer(void) * moment. */ BUFFER_HEAD(buffptr) = BUFFER_TAIL(buffptr) = NULL; - BUFFER_SIZE(buffptr) = 0; + buffptr->size = 0; return buffptr; } @@ -127,6 +137,14 @@ delete_buffer(struct buffer_s *buffptr) safefree(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. */ @@ -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. diff --git a/src/reqs.c b/src/reqs.c index 746bd70..cd78d01 100644 --- a/src/reqs.c +++ b/src/reqs.c @@ -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; } -- cgit v1.2.3