/* 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)
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 */
/* 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
/* 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;
}
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
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 */
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 */
};
/* 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;
}
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);
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);
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 */
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) {
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 */
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];
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];
* 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,
* 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
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];
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];
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. */
}
}
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.
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 */
}
}
}
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) {
*/
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) {
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)
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;