From: Christopher Faulet Date: Thu, 2 Jul 2026 14:51:21 +0000 (+0200) Subject: MINOR: chunks: Add function to get a large/regular chunk depending on a buffer X-Git-Tag: v3.5-dev2~23 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/$%7BGITURL%7D/static/gitweb.js?a=commitdiff_plain;h=2b993de2dafdc3584c06fd56612a7c647627135b;p=haproxy.git MINOR: chunks: Add function to get a large/regular chunk depending on a buffer 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. --- diff --git a/include/haproxy/chunk.h b/include/haproxy/chunk.h index 7c9350f7b..bf6f3e7e2 100644 --- a/include/haproxy/chunk.h +++ b/include/haproxy/chunk.h @@ -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 . So if is a large buffer, + * alloc_trash_chunk_sz() function is called. Otherwise, if the size is + * smaller enough, a regular buffer is allocated. If is too big and + * 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. diff --git a/src/chunk.c b/src/chunk.c index ab4f002b1..f82bd4af6 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -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 . So if is a large buffer, + * get_trash_chunk_sz() function is called. Otherwise, if the size is + * smaller enough, a regular buffer is returned. If is too big and + * 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 if possible or NULL otherwise. If a larger * buffer is returned, content of are copied. */