From 32701a163d68f5a7e2ec97ce4a5a513d63f2b4be Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Mon, 15 Jan 2024 11:37:24 +0100 Subject: [PATCH] fix(tlsf): tlsf_fit_size GoodFit The good fit mechanism should not be applied for small blocks. If the size in parameter is inferior to control->small_block_size. then the function tlsf_fit_size should return size directly. --- tlsf.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tlsf.c b/tlsf.c index a8018c7..cc3d7af 100644 --- a/tlsf.c +++ b/tlsf.c @@ -795,17 +795,20 @@ int tlsf_check_pool(pool_t pool) size_t tlsf_fit_size(tlsf_t tlsf, size_t size) { - /* because it's GoodFit, allocable size is one range lower */ - if (size && tlsf != NULL) - { - size_t sl_interval; - control_t* control = tlsf_cast(control_t*, tlsf); - sl_interval = (1 << (32 - __builtin_clz(size) - 1)) / control->sl_index_count; - return size & ~(sl_interval - 1); + if (size == 0 || tlsf == NULL) { + return 0; } - return 0; -} + control_t* control = tlsf_cast(control_t*, tlsf); + if (size < control->small_block_size) { + return adjust_request_size(tlsf, size, ALIGN_SIZE); + } + + /* because it's GoodFit, allocable size is one range lower */ + size_t sl_interval; + sl_interval = (1 << (32 - __builtin_clz(size) - 1)) / control->sl_index_count; + return size & ~(sl_interval - 1); +} /* ** Size of the TLSF structures in a given memory block passed to