Heap: fix free bytes calculation for TLSF heap

* Closes https://github.com/espressif/esp-idf/issues/8270
This commit is contained in:
Omar Chebib
2022-03-03 13:01:54 +08:00
parent 8f67af174e
commit 4ce4c5a68a
3 changed files with 34 additions and 3 deletions

View File

@@ -503,3 +503,23 @@ TEST_CASE("multi_heap aligned allocations", "[multi_heap]")
printf("[ALIGNED_ALLOC] heap_size after: %d \n", multi_heap_free_size(heap));
REQUIRE((old_size - multi_heap_free_size(heap)) <= leakage);
}
// TLSF has some overhead when allocating blocks, check that overhead
TEST_CASE("multi_heap allocation overhead", "[multi_heap]")
{
uint8_t heapdata[4 * 1024];
size_t alloc_size = 256;
multi_heap_handle_t heap = multi_heap_register(heapdata, sizeof(heapdata));
size_t free_bytes_1 = multi_heap_free_size(heap);
/* Allocate any amount of data, in any case there will be an overhead */
void *x = multi_heap_malloc(heap, alloc_size);
/* free_bytes_2 should be free_bytes_1 - alloc_size - overhead.
* We don't know the value of overhead, let's check that it is non-zero */
size_t free_bytes_2 = multi_heap_free_size(heap);
REQUIRE( free_bytes_1 > free_bytes_2 );
REQUIRE( free_bytes_1 - free_bytes_2 > alloc_size );
multi_heap_free(heap, x);
}