]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: buffer: use b_alloc() to allocate and initialize a buffer
authorWilly Tarreau <w@1wt.eu>
Mon, 24 Nov 2014 10:30:16 +0000 (11:30 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 24 Dec 2014 22:47:32 +0000 (23:47 +0100)
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
src/compression.c
src/peers.c
src/proto_http.c
src/session.c

index fd2957579c164d883c06b932b334e8faf1de05db..52565dd9b5ea294c8c3830569c4fdd3f352a28ca 100644 (file)
@@ -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 */
 
 /*
index b6c4ae201d714db37e3443faa9d551e5f13b019b..db2209adcb95f93dce2ce80eef78dd93b04bda0f 100644 (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
         * a new temporary buffer that we initialize with a new empty chunk.
         */
-
-       out->size = global.tune.bufsize;
        b_reset(out);
 
        if (in->o > 0) {
index 9ff177354a628d2c7a3c7b4e166946c4969be604..a5dff87e60f0214bda89466d7581b193b23c6cb5 100644 (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)
                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];
index 3fffc5bb461782b85346269357304d5dc7d86f27..ee1a812c8d30d5d91185c732213c7c50ddd6ad34 100644 (file)
@@ -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 */
                }
 
index d57a2d5da246428b45787db450ae4b01e51bcc6d..05fd72e56ca92015d12e94e1be3b2698b8ddd9be 100644 (file)
@@ -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];