From e583ea583a5e028c25130e25c2d259be3e4fe148 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 24 Nov 2014 11:30:16 +0100 Subject: [PATCH] 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. --- include/common/buffer.h | 14 ++++++++++++++ src/compression.c | 2 -- src/peers.c | 8 ++------ src/proto_http.c | 3 +-- src/session.c | 10 ++-------- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/include/common/buffer.h b/include/common/buffer.h index fd2957579..52565dd9b 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -395,6 +395,20 @@ static inline void b_reset(struct buffer *buf) 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 */ /* diff --git a/src/compression.c b/src/compression.c index b6c4ae201..db2209adc 100644 --- a/src/compression.c +++ b/src/compression.c @@ -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 * a new temporary buffer that we initialize with a new empty chunk. */ - - out->size = global.tune.bufsize; b_reset(out); if (in->o > 0) { diff --git a/src/peers.c b/src/peers.c index 9ff177354..a5dff87e6 100644 --- a/src/peers.c +++ b/src/peers.c @@ -1237,11 +1237,9 @@ static struct session *peer_session_create(struct peer *peer, struct peer_sessio if ((s->req = pool_alloc2(pool2_channel)) == NULL) 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 */ - s->req->buf->size = global.tune.bufsize; - b_reset(s->req->buf); channel_init(s->req); s->req->prod = &s->si[0]; 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) 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 */ - s->rep->buf->size = global.tune.bufsize; - b_reset(s->rep->buf); channel_init(s->rep); s->rep->prod = &s->si[1]; s->rep->cons = &s->si[0]; diff --git a/src/proto_http.c b/src/proto_http.c index 3fffc5bb4..ee1a812c8 100644 --- a/src/proto_http.c +++ b/src/proto_http.c @@ -6572,8 +6572,7 @@ int http_response_forward_body(struct session *s, struct channel *res, int an_bi */ if (unlikely(tmpbuf == NULL)) { /* this is the first time we need the compression buffer */ - tmpbuf = pool_alloc2(pool2_buffer); - if (tmpbuf == NULL) + if (b_alloc(&tmpbuf) == NULL) goto aborted_xfer; /* no memory */ } diff --git a/src/session.c b/src/session.c index d57a2d5da..05fd72e56 100644 --- a/src/session.c +++ b/src/session.c @@ -477,18 +477,15 @@ int session_complete(struct session *s) if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL)) 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 */ if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL)) 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 */ - /* initialize the request buffer */ - s->req->buf->size = global.tune.bufsize; - b_reset(s->req->buf); channel_init(s->req); s->req->prod = &s->si[0]; s->req->cons = &s->si[1]; @@ -504,9 +501,6 @@ int session_complete(struct session *s) s->req->wex = 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); s->rep->prod = &s->si[1]; s->rep->cons = &s->si[0];