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.
This commit is contained in:
Christopher Faulet 2026-07-02 16:51:21 +02:00
parent ee5cfaf266
commit 2b993de2da
2 changed files with 32 additions and 0 deletions

View 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.

View 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.
*/