]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: buffer: implement b_alloc_fast()
authorWilly Tarreau <w@1wt.eu>
Mon, 8 Dec 2014 15:37:26 +0000 (16:37 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 24 Dec 2014 22:47:32 +0000 (23:47 +0100)
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

index 221839020f7861458ce3a77c4a57829d55eb483d..b1c16e8a55d8318e28ada4b6bf92d8f396c75fcc 100644 (file)
@@ -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)
 {