]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: dynbuf: make b_alloc() always check if the buffer is allocated
authorWilly Tarreau <w@1wt.eu>
Mon, 22 Mar 2021 15:10:22 +0000 (16:10 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 22 Mar 2021 15:14:45 +0000 (16:14 +0100)
Right now there is a discrepancy beteween b_alloc() and b_allow_margin():
the former forcefully overwrites the target pointer while the latter tests
it and returns it as-is if already allocated.

As a matter of fact, all callers of b_alloc() either preliminary test the
buffer, or assume it's already null.

Let's remove this pain and make the function test the buffer's allocation
before doing it again, and match call places' expectations.

doc/internals/buffer-api.txt
include/haproxy/dynbuf.h

index d4e26dbd998d8316b6532d4f1bb4f25dc7d20966..a163e387ae831323148bebc3460435fd65792e83 100644 (file)
@@ -547,13 +547,12 @@ buffer_almost_full  | const buffer *buf| returns true if the buffer is not null
                     | ret: int         | and at least 3/4 of the buffer's space
                     |                  | are used. A waiting buffer will match.
 --------------------+------------------+---------------------------------------
-b_alloc             | buffer *buf      | allocates a buffer and assigns it to
-                    | ret: buffer *    | *buf. If no memory is available, (1)
+b_alloc             | buffer *buf      | ensures that <buf> is allocated or
+                    | ret: buffer *    | allocates a buffer and assigns it to
+                    |                  | *buf. If no memory is available, (1)
                     |                  | is assigned instead with a zero size.
-                    |                  | 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 allocated buffer is returned, or
+                    |                  | NULL in case no memory is available
 --------------------+------------------+---------------------------------------
 b_alloc_fast        | buffer *buf      | allocates a buffer and assigns it to
                     | ret: buffer *    | *buf. If no memory is available, (1)
index c31b83aaac439f8db3e7b3930a88197f3b571744..00e5fff508e9f508606795f31f48b9f08f239a53 100644 (file)
@@ -56,15 +56,17 @@ static inline int buffer_almost_full(const struct buffer *buf)
 /* Functions below are used for buffer allocation */
 /**************************************************/
 
-/* Allocates a buffer and assigns it to *buf. If no memory is available,
- * ((char *)1) is assigned instead with a zero size. No control is made to
- * check if *buf already pointed to another buffer. The allocated buffer is
+/* Ensures that <buf> is allocated, or allocates it. If no memory is available,
+ * ((char *)1) is assigned instead with a zero size. The allocated buffer is
  * returned, or NULL in case no memory is available.
  */
 static inline struct buffer *b_alloc(struct buffer *buf)
 {
        char *area;
 
+       if (buf->size)
+               return buf;
+
        *buf = BUF_WANTED;
        area = pool_alloc_dirty(pool_head_buffer);
        if (unlikely(!area)) {