From 620bd6c88e3999b5a4b3b5c6fd8933ee47a1bc8b Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 8 Dec 2014 16:37:26 +0100 Subject: [PATCH] MINOR: buffer: implement b_alloc_fast() This function allocates a buffer and replaces *buf with this buffer. If no memory is available, &buf_wanted is used instead. 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. The difference with b_alloc() is that this function only picks from the pool and never calls malloc(), so it can fail even if some memory is available. It is the caller's job to refill the buffer pool if needed. --- include/common/buffer.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/include/common/buffer.h b/include/common/buffer.h index 221839020f..b1c16e8a55 100644 --- a/include/common/buffer.h +++ b/include/common/buffer.h @@ -422,6 +422,27 @@ static inline struct buffer *b_alloc(struct buffer **buf) return b; } +/* Allocates a buffer and replaces *buf with this buffer. If no memory is + * available, &buf_wanted is used instead. 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. The difference with b_alloc() is that + * this function only picks from the pool and never calls malloc(), so it can + * fail even if some memory is available. + */ +static inline struct buffer *b_alloc_fast(struct buffer **buf) +{ + struct buffer *b; + + *buf = &buf_wanted; + b = pool_get_first(pool2_buffer); + if (likely(b)) { + b->size = pool2_buffer->size - sizeof(struct buffer); + b_reset(b); + *buf = b; + } + return b; +} + /* Releases buffer *buf (no check of emptiness) */ static inline void __b_drop(struct buffer **buf) {