Compare commits

...

4 commits

Author SHA1 Message Date
Guillaume Souchere
ddfa052fbf Merge branch 'fix/tlsf_fit_size_for_small_size' into 'idf'
fix(tlsf): tlsf_fit_size GoodFit should not be applied for small block size

See merge request espressif/tlsf!7
2024-01-16 17:11:06 +08:00
Guillaume Souchere
32701a163d 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.
2024-01-16 07:42:58 +01:00
Guillaume Souchere
d2e28f8724 Merge branch 'fix/call_check_hook_for_all_blocks' into 'idf'
tlsf: call the tlsf_check_hook from intergrity_walker to check all blocks

See merge request espressif/tlsf!6
2023-10-16 16:18:01 +08:00
Guillaume Souchere
262d11fcfa tlsf: move the call the tlsf_check_hook from tlsf_check to the intergrity_walker function
Previously the hook was called from tlsf_check function leading to only check the free blocks
and leaving the used blocks unchecked by the hook. By moving the function hook call to the
integrity_walker function, free and used blocks are now passed to the function hook.
2023-09-27 13:22:02 +02:00

58
tlsf.c
View file

@ -668,14 +668,32 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user)
const size_t this_block_size = block_size(block);
int status = 0;
(void)used;
tlsf_insist(integ->prev_status == this_prev_status && "prev status incorrect");
tlsf_insist(size == this_block_size && "block size incorrect");
if (tlsf_check_hook != NULL)
{
/* block_size(block) returns the size of the usable memory when the block is allocated.
* As the block under test is free, we need to subtract to the block size the next_free
* and prev_free fields of the block header as they are not a part of the usable memory
* when the block is free. In addition, we also need to subtract the size of prev_phys_block
* as this field is in fact part of the current free block and not part of the next (allocated)
* block. Check the comments in block_split function for more details.
*/
const size_t actual_free_block_size = used ? this_block_size :
this_block_size - offsetof(block_header_t, next_free)- block_header_overhead;
void* ptr_block = used ? (void*)block + block_start_offset :
(void*)block + sizeof(block_header_t);
tlsf_insist(tlsf_check_hook(ptr_block, actual_free_block_size, !used));
}
integ->prev_status = this_status;
integ->status += status;
}
int tlsf_check(tlsf_t tlsf)
{
int i, j;
@ -722,23 +740,6 @@ int tlsf_check(tlsf_t tlsf)
mapping_insert(control, block_size(block), &fli, &sli);
tlsf_insist(fli == i && sli == j && "block size indexed in wrong list");
if (tlsf_check_hook != NULL)
{
/* block_size(block) returns the size of the usable memory when the block is allocated.
* As the block under test is free, we need to subtract to the block size the next_free
* and prev_free fields of the block header as they are not a part of the usable memory
* when the block is free. In addition, we also need to subtract the size of prev_phys_block
* as this field is in fact part of the current free block and not part of the next (allocated)
* block. Check the comments in block_split function for more details.
*/
const size_t actual_free_block_size = block_size(block)
- offsetof(block_header_t, next_free)
- block_header_overhead;
tlsf_insist(tlsf_check_hook((void*)block + sizeof(block_header_t),
actual_free_block_size, is_block_free));
}
block = block->next_free;
}
}
@ -794,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