Merge branch 'feature/add-hook-in-tlsf-check' into 'idf'

Provide a function hook in tlsf_check()

See merge request espressif/tlsf!2
This commit is contained in:
Guillaume Souchere
2022-08-25 00:04:28 +08:00
2 changed files with 23 additions and 2 deletions

23
tlsf.c
View File

@@ -618,7 +618,7 @@ typedef struct integrity_t
int status;
} integrity_t;
#define tlsf_insist(x) { tlsf_assert(x); if (!(x)) { status--; } }
#define tlsf_insist(x) { if (!(x)) { status--; } }
static void integrity_walker(void* ptr, size_t size, int used, void* user)
{
@@ -673,7 +673,8 @@ int tlsf_check(tlsf_t tlsf)
while (block != &control->block_null)
{
int fli, sli;
tlsf_insist(block_is_free(block) && "block should be free");
const bool is_block_free = block_is_free(block);
tlsf_insist(is_block_free && "block should be free");
tlsf_insist(!block_is_prev_free(block) && "blocks should have coalesced");
tlsf_insist(!block_is_free(block_next(block)) && "blocks should have coalesced");
tlsf_insist(block_is_prev_free(block_next(block)) && "block should be free");
@@ -681,6 +682,24 @@ int tlsf_check(tlsf_t tlsf)
mapping_insert(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;
}
}

2
tlsf.h
View File

@@ -64,6 +64,8 @@ int tlsf_check_pool(pool_t pool);
*/
__attribute__((weak)) void block_absorb_post_hook(void *start, size_t size, bool is_free);
__attribute__((weak)) bool tlsf_check_hook(void *start, size_t size, bool is_free);
#if defined(__cplusplus)
};
#endif