]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: chunks: Add function to get a large/regular chunk depending on a buffer
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 2 Jul 2026 14:51:21 +0000 (16:51 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 8 Jul 2026 06:52:06 +0000 (08:52 +0200)
get_best_trash_chunk() function was added to be able to get a large or a
regular chunk depending on a given size but never larget than a given
buffer. It will be usefull in a futur fix, to prevent unexpected large chunk
usage.

alloc_best_trash_chunk() function is similar but instead of returning one of
the static chunks, it allocate it from the corresponding pool, large or
regular.

include/haproxy/chunk.h
src/chunk.c

index 7c9350f7b58048d224e91d28188a59c054c3df5f..bf6f3e7e22e36b0ef02e16fafba3d711ef3fe902 100644 (file)
@@ -51,6 +51,7 @@ struct buffer *get_trash_chunk(void);
 struct buffer *get_large_trash_chunk(void);
 struct buffer *get_small_trash_chunk(void);
 struct buffer *get_trash_chunk_sz(size_t size);
+struct buffer *get_best_trash_chunk(const struct buffer *buf, size_t size);
 struct buffer *get_larger_trash_chunk(struct buffer *chunk);
 int init_trash_buffers(int first);
 
@@ -172,6 +173,21 @@ static forceinline struct buffer *alloc_trash_chunk_sz(size_t size)
        else
                return NULL;
 }
+/* Returns a trash chunk accordingly to the requested size and never larger
+ * that the buffer <buf>. So if <buf> is a large buffer,
+ * alloc_trash_chunk_sz() function is called. Otherwise, if the size is
+ * smaller enough, a regular buffer is allocated. If <size> is too big and
+ * <buf> is not a large buffer, NULL is returned.
+ */
+static forceinline struct buffer *alloc_best_trash_chunk(const struct buffer *buf, size_t size)
+{
+       if (pool_head_large_trash && buf->size == pool_head_large_trash->size)
+               return alloc_trash_chunk_sz(size);
+       else if (size <= pool_head_trash->size)
+               return alloc_trash_chunk();
+       else
+               return NULL;
+}
 
 /*
  * free a trash chunk allocated by alloc_trash_chunk(). NOP on NULL.
index ab4f002b1b98adfcbe28e09b44d96c0b50690557..f82bd4af66cb1f833179d408e1c8707666e83cb0 100644 (file)
@@ -160,6 +160,22 @@ struct buffer *get_trash_chunk_sz(size_t size)
                return NULL;
 }
 
+/* Returns a trash chunk accordingly to the requested size and never larger
+ * that the buffer <buf>. So if <buf> is a large buffer,
+ * get_trash_chunk_sz() function is called. Otherwise, if the size is
+ * smaller enough, a regular buffer is returned. If <size> is too big and
+ * <buf> is not a large buffer, NULL is returned.
+ */
+struct buffer *get_best_trash_chunk(const struct buffer *buf, size_t size)
+{
+       if (large_trash_size && buf->size == large_trash_size)
+               return get_trash_chunk_sz(size);
+       else if (size <= trash_size)
+               return get_trash_chunk();
+       else
+               return NULL;
+}
+
 /* Returns a larger buffer than <chk> if possible or NULL otherwise. If a larger
  * buffer is returned, content of <chk> are copied.
  */