From 5a05275bf050751ecfa90e3fd78a87b1a796a516 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 8 Dec 2014 16:37:26 +0100 Subject: MINOR: buffers: implement b_fast_alloc() 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 6154522..07a06a8 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_fast_alloc(struct buffer **buf) +{ + struct buffer *b; + + *buf = &buf_wanted; + b = pool_fast_alloc2(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) { -- 1.7.12.1