MEDIUM: buffer: use b_alloc() to allocate and initialize a buffer

b_alloc() now allocates a buffer and initializes it to the size specified
in the pool minus the size of the struct buffer itself. This ensures that
callers do not need to care about buffer details anymore. Also this never
applies memory poisonning, which is slow and useless on buffers.
This commit is contained in:
Willy Tarreau 2014-11-24 11:30:16 +01:00
parent 474cf54a97
commit e583ea583a
5 changed files with 19 additions and 18 deletions

View File

@ -395,6 +395,20 @@ static inline void b_reset(struct buffer *buf)
buf->p = buf->data; buf->p = buf->data;
} }
/* Allocates a buffer and replaces *buf with this buffer. No control is made
* to check if *buf already pointed to another buffer. The allocated buffer
* is returned, or NULL in case no memory is available.
*/
static inline struct buffer *b_alloc(struct buffer **buf)
{
*buf = pool_alloc_dirty(pool2_buffer);
if (likely(*buf)) {
(*buf)->size = pool2_buffer->size - sizeof(struct buffer);
b_reset(*buf);
}
return *buf;
}
#endif /* _COMMON_BUFFER_H */ #endif /* _COMMON_BUFFER_H */
/* /*

View File

@ -137,8 +137,6 @@ int http_compression_buffer_init(struct session *s, struct buffer *in, struct bu
/* We start by copying the current buffer's pending outgoing data into /* We start by copying the current buffer's pending outgoing data into
* a new temporary buffer that we initialize with a new empty chunk. * a new temporary buffer that we initialize with a new empty chunk.
*/ */
out->size = global.tune.bufsize;
b_reset(out); b_reset(out);
if (in->o > 0) { if (in->o > 0) {

View File

@ -1237,11 +1237,9 @@ static struct session *peer_session_create(struct peer *peer, struct peer_sessio
if ((s->req = pool_alloc2(pool2_channel)) == NULL) if ((s->req = pool_alloc2(pool2_channel)) == NULL)
goto out_fail_req; /* no memory */ goto out_fail_req; /* no memory */
if ((s->req->buf = pool_alloc2(pool2_buffer)) == NULL) if (unlikely(b_alloc(&s->req->buf) == NULL))
goto out_fail_req_buf; /* no memory */ goto out_fail_req_buf; /* no memory */
s->req->buf->size = global.tune.bufsize;
b_reset(s->req->buf);
channel_init(s->req); channel_init(s->req);
s->req->prod = &s->si[0]; s->req->prod = &s->si[0];
s->req->cons = &s->si[1]; s->req->cons = &s->si[1];
@ -1264,11 +1262,9 @@ static struct session *peer_session_create(struct peer *peer, struct peer_sessio
if ((s->rep = pool_alloc2(pool2_channel)) == NULL) if ((s->rep = pool_alloc2(pool2_channel)) == NULL)
goto out_fail_rep; /* no memory */ goto out_fail_rep; /* no memory */
if ((s->rep->buf = pool_alloc2(pool2_buffer)) == NULL) if (unlikely(b_alloc(&s->rep->buf) == NULL))
goto out_fail_rep_buf; /* no memory */ goto out_fail_rep_buf; /* no memory */
s->rep->buf->size = global.tune.bufsize;
b_reset(s->rep->buf);
channel_init(s->rep); channel_init(s->rep);
s->rep->prod = &s->si[1]; s->rep->prod = &s->si[1];
s->rep->cons = &s->si[0]; s->rep->cons = &s->si[0];

View File

@ -6572,8 +6572,7 @@ int http_response_forward_body(struct session *s, struct channel *res, int an_bi
*/ */
if (unlikely(tmpbuf == NULL)) { if (unlikely(tmpbuf == NULL)) {
/* this is the first time we need the compression buffer */ /* this is the first time we need the compression buffer */
tmpbuf = pool_alloc2(pool2_buffer); if (b_alloc(&tmpbuf) == NULL)
if (tmpbuf == NULL)
goto aborted_xfer; /* no memory */ goto aborted_xfer; /* no memory */
} }

View File

@ -477,18 +477,15 @@ int session_complete(struct session *s)
if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL)) if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL))
goto out_free_task; /* no memory */ goto out_free_task; /* no memory */
if (unlikely((s->req->buf = pool_alloc2(pool2_buffer)) == NULL)) if (unlikely(b_alloc(&s->req->buf) == NULL))
goto out_free_req; /* no memory */ goto out_free_req; /* no memory */
if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL)) if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL))
goto out_free_req_buf; /* no memory */ goto out_free_req_buf; /* no memory */
if (unlikely((s->rep->buf = pool_alloc2(pool2_buffer)) == NULL)) if (unlikely(b_alloc(&s->rep->buf) == NULL))
goto out_free_rep; /* no memory */ goto out_free_rep; /* no memory */
/* initialize the request buffer */
s->req->buf->size = global.tune.bufsize;
b_reset(s->req->buf);
channel_init(s->req); channel_init(s->req);
s->req->prod = &s->si[0]; s->req->prod = &s->si[0];
s->req->cons = &s->si[1]; s->req->cons = &s->si[1];
@ -504,9 +501,6 @@ int session_complete(struct session *s)
s->req->wex = TICK_ETERNITY; s->req->wex = TICK_ETERNITY;
s->req->analyse_exp = TICK_ETERNITY; s->req->analyse_exp = TICK_ETERNITY;
/* initialize response buffer */
s->rep->buf->size = global.tune.bufsize;
b_reset(s->rep->buf);
channel_init(s->rep); channel_init(s->rep);
s->rep->prod = &s->si[1]; s->rep->prod = &s->si[1];
s->rep->cons = &s->si[0]; s->rep->cons = &s->si[0];