]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MEDIUM] replace BUFSIZE with buf->size in computations
authorWilly Tarreau <w@1wt.eu>
Sun, 16 Aug 2009 21:27:46 +0000 (23:27 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 16 Aug 2009 21:27:46 +0000 (23:27 +0200)
The first step towards dynamic buffer size consists in removing
all static definitions of the buffer size. Instead, we store a
buffer's size in itself. Right now they're all preinitialized
to BUFSIZE, but we will change that.

include/proto/buffers.h
include/types/buffers.h
src/buffers.c
src/client.c
src/proto_http.c
src/proto_uxst.c
src/session.c
src/stream_sock.c

index dc9961a2bd97d4ab6f91bbb40c2f887aca82d93a..53fd41ffcf1b60dbe45f5dc7b76e15ed36b5be02 100644 (file)
@@ -41,6 +41,7 @@ int init_buffer();
 /* Initializes all fields in the buffer. The ->max_len field is initialized last
  * so that the compiler can optimize it away if changed immediately after the
  * call to this function. By default, it is set to the full size of the buffer.
+ * This implies that buffer_init() must only be called once ->size is set !
  * The BF_EMPTY flags is set.
  */
 static inline void buffer_init(struct buffer *buf)
@@ -53,7 +54,7 @@ static inline void buffer_init(struct buffer *buf)
        buf->cons = NULL;
        buf->flags = BF_EMPTY;
        buf->r = buf->lr = buf->w = buf->data;
-       buf->max_len = BUFSIZE;
+       buf->max_len = buf->size;
 }
 
 /* returns 1 if the buffer is empty, 0 otherwise */
@@ -64,7 +65,7 @@ static inline int buffer_isempty(const struct buffer *buf)
 
 /* returns 1 if the buffer is full, 0 otherwise */
 static inline int buffer_isfull(const struct buffer *buf) {
-       return buf->l == BUFSIZE;
+       return buf->l == buf->size;
 }
 
 /* Check buffer timeouts, and set the corresponding flags. The
@@ -238,10 +239,10 @@ static inline void buffer_check_shutw(struct buffer *b)
 /* returns the maximum number of bytes writable at once in this buffer */
 static inline int buffer_max(const struct buffer *buf)
 {
-       if (buf->l == BUFSIZE)
+       if (buf->l == buf->size)
                return 0;
        else if (buf->r >= buf->w)
-               return buf->data + BUFSIZE - buf->r;
+               return buf->data + buf->size - buf->r;
        else
                return buf->w - buf->r;
 }
index b9836b23ee3647d94fee6bab2d246f9eb1746cc8..84929485e352fc872bdc84313bf8db95f782b172 100644 (file)
@@ -2,7 +2,7 @@
   include/types/buffers.h
   Buffer management definitions, macros and inline functions.
 
-  Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
+  Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -136,6 +136,7 @@ struct buffer {
        int cto;                        /* connect timeout, in ticks */
        unsigned int l;                 /* data length */
        char *r, *w, *lr;               /* read ptr, write ptr, last read */
+       unsigned int size;              /* buffer size in bytes */
        unsigned int max_len;           /* read limit, used to keep room for header rewriting */
        unsigned int send_max;          /* number of bytes the sender can consume om this buffer, <= l */
        unsigned int to_forward;        /* number of bytes to forward after send_max without a wake-up */
@@ -148,7 +149,7 @@ struct buffer {
        struct stream_interface *prod;  /* producer attached to this buffer */
        struct stream_interface *cons;  /* consumer attached to this buffer */
        struct pipe *pipe;              /* non-NULL only when data present */
-       char data[BUFSIZE];
+       char data[0];                   /* <size> bytes */
 };
 
 
index c8681cfb8c6487153fec8cfa96bca8c603bb025f..55b79639b7ea289a8aac50055cd30511791a80eb 100644 (file)
@@ -24,7 +24,7 @@ struct pool_head *pool2_buffer;
 /* perform minimal intializations, report 0 in case of error, 1 if OK. */
 int init_buffer()
 {
-       pool2_buffer = create_pool("buffer", sizeof(struct buffer), MEM_F_SHARED);
+       pool2_buffer = create_pool("buffer", sizeof(struct buffer) + BUFSIZE, MEM_F_SHARED);
        return pool2_buffer != NULL;
 }
 
@@ -48,7 +48,7 @@ int buffer_write(struct buffer *buf, const char *msg, int len)
        buf->send_max += len;
        buf->r += len;
        buf->total += len;
-       if (buf->r == buf->data + BUFSIZE)
+       if (buf->r == buf->data + buf->size)
                buf->r = buf->data;
 
        buf->flags &= ~(BF_EMPTY|BF_FULL);
@@ -82,7 +82,7 @@ int buffer_write_chunk(struct buffer *buf, struct chunk *chunk)
        buf->send_max += chunk->len;
        buf->r += chunk->len;
        buf->total += chunk->len;
-       if (buf->r == buf->data + BUFSIZE)
+       if (buf->r == buf->data + buf->size)
                buf->r = buf->data;
 
        buf->flags &= ~(BF_EMPTY|BF_FULL);
@@ -111,7 +111,7 @@ int buffer_replace(struct buffer *b, char *pos, char *end, const char *str)
        len = strlen(str);
        delta = len - (end - pos);
 
-       if (delta + b->r >= b->data + BUFSIZE)
+       if (delta + b->r >= b->data + b->size)
                return 0;  /* no space left */
 
        /* first, protect the end of the buffer */
@@ -145,7 +145,7 @@ int buffer_replace2(struct buffer *b, char *pos, char *end, const char *str, int
 
        delta = len - (end - pos);
 
-       if (delta + b->r >= b->data + BUFSIZE)
+       if (delta + b->r >= b->data + b->size)
                return 0;  /* no space left */
 
        if (b->data + b->l < end) {
@@ -192,7 +192,7 @@ int buffer_insert_line2(struct buffer *b, char *pos, const char *str, int len)
 
        delta = len + 2;
 
-       if (delta + b->r >= b->data + BUFSIZE)
+       if (delta + b->r >= b->data + b->size)
                return 0;  /* no space left */
 
        /* first, protect the end of the buffer */
index ec8f1201780a65600c9189ae8c4d7fb0f7d9c278..92baefc63bb8580f1f4a2bc86326c289a6caf9ef 100644 (file)
@@ -384,6 +384,7 @@ int event_accept(int fd) {
                if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
                        goto out_fail_req; /* no memory */
 
+               s->req->size = BUFSIZE;
                buffer_init(s->req);
                s->req->prod = &s->si[0];
                s->req->cons = &s->si[1];
@@ -410,6 +411,7 @@ int event_accept(int fd) {
                if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
                        goto out_fail_rep; /* no memory */
 
+               s->rep->size = BUFSIZE;
                buffer_init(s->rep);
                s->rep->prod = &s->si[1];
                s->rep->cons = &s->si[0];
index e495d32bbcdec288a9eea0dab1d65e431db768da..d2c591916ecd499dbcc3ecf3e6f122725f116bea 100644 (file)
@@ -2576,7 +2576,7 @@ int http_process_request(struct session *s, struct buffer *req, int an_bit)
         * could. Let's switch to the DATA state.                    *
         ************************************************************/
 
-       buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */
+       buffer_set_rlim(req, req->size); /* no more rewrite needed */
        s->logs.tv_request = now;
 
        /* When a connection is tarpitted, we use the tarpit timeout,
@@ -3190,7 +3190,7 @@ int process_response(struct session *t)
                 * could. Let's switch to the DATA state.                    *
                 ************************************************************/
 
-               buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */
+               buffer_set_rlim(rep, rep->size); /* no more rewrite needed */
                t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
 
                /* if the user wants to log as soon as possible, without counting
index 880ec993a7d021ccc09b261d5ecab1bc9843f07d..0cd6a35865c34210be4c9e33daca3be8d88d4d75 100644 (file)
@@ -482,6 +482,7 @@ int uxst_event_accept(int fd) {
                if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
                        goto out_free_task;
 
+               s->req->size = BUFSIZE;
                buffer_init(s->req);
                s->req->prod = &s->si[0];
                s->req->cons = &s->si[1];
@@ -498,6 +499,7 @@ int uxst_event_accept(int fd) {
                if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
                        goto out_free_req;
 
+               s->rep->size = BUFSIZE;
                buffer_init(s->rep);
 
                s->rep->prod = &s->si[1];
index 33109d6c1a3ae0db02f7556ed15cb3545a64999d..23c4409c6884045a90fcc2faf7a755673d2a4462 100644 (file)
@@ -320,7 +320,7 @@ void sess_establish(struct session *s, struct stream_interface *si)
        struct buffer *rep = si->ib;
 
        if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
-               buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */
+               buffer_set_rlim(rep, rep->size); /* no rewrite needed */
 
                /* if the user wants to log as soon as possible, without counting
                 * bytes from the server, then this is the right moment. */
@@ -330,7 +330,7 @@ void sess_establish(struct session *s, struct stream_interface *si)
                }
        }
        else {
-               buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */
+               buffer_set_rlim(rep, req->size - MAXREWRITE); /* rewrite needed */
                s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
                /* reset hdr_idx which was already initialized by the request.
                 * right now, the http parser does it.
index f20bb3f0cd9b897a492bb4cde77dcde2221dbd41..3a69d1f1979cfb2618fe6fb71d695aababcccf6c 100644 (file)
@@ -345,7 +345,7 @@ int stream_sock_read(int fd) {
                        b->flags |= BF_READ_PARTIAL;
                        b->flags &= ~BF_EMPTY;
 
-                       if (b->r == b->data + BUFSIZE) {
+                       if (b->r == b->data + b->size) {
                                b->r = b->data; /* wrap around the buffer */
                        }
 
@@ -367,7 +367,7 @@ int stream_sock_read(int fd) {
                                        }
                                }
                                else if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
-                                        (cur_read <= BUFSIZE / 2)) {
+                                        (cur_read <= b->size / 2)) {
                                        b->xfer_large = 0;
                                        b->xfer_small++;
                                        if (b->xfer_small >= 2) {
@@ -397,7 +397,7 @@ int stream_sock_read(int fd) {
                         */
                        if (ret < max) {
                                if ((b->flags & (BF_STREAMER | BF_STREAMER_FAST)) &&
-                                   (cur_read <= BUFSIZE / 2)) {
+                                   (cur_read <= b->size / 2)) {
                                        b->xfer_large = 0;
                                        b->xfer_small++;
                                        if (b->xfer_small >= 3) {
@@ -580,7 +580,7 @@ static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
                if (b->r > b->w)
                        max = b->r - b->w;
                else
-                       max = b->data + BUFSIZE - b->w;
+                       max = b->data + b->size - b->w;
 
                /* limit the amount of outgoing data if required */
                if (max > b->send_max)
@@ -628,7 +628,7 @@ static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
                        b->flags |= BF_WRITE_PARTIAL;
 
                        b->w += ret;
-                       if (b->w == b->data + BUFSIZE)
+                       if (b->w == b->data + b->size)
                                b->w = b->data; /* wrap around the buffer */
 
                        b->l -= ret;