diff --git a/tlsf.c b/tlsf.c index 68105d0..a8018c7 100644 --- a/tlsf.c +++ b/tlsf.c @@ -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; } }