mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-02 20:24:32 +02:00
Merge branch 'fix/heap-get-allocated-size_v5.0' into 'release/v5.0'
heap: Fix erroneous value returned by heap_caps_get_allocated_size() when poisoning is enabled (backport v5.0) See merge request espressif/esp-idf!22187
This commit is contained in:
@@ -370,7 +370,7 @@ multi_heap_handle_t multi_heap_register(void *start, size_t size)
|
|||||||
return multi_heap_register_impl(start, size);
|
return multi_heap_register_impl(start, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void subtract_poison_overhead(size_t *arg) {
|
static inline __attribute__((always_inline)) void subtract_poison_overhead(size_t *arg) {
|
||||||
if (*arg > POISON_OVERHEAD) {
|
if (*arg > POISON_OVERHEAD) {
|
||||||
*arg -= POISON_OVERHEAD;
|
*arg -= POISON_OVERHEAD;
|
||||||
} else {
|
} else {
|
||||||
@@ -383,6 +383,7 @@ size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p)
|
|||||||
poison_head_t *head = verify_allocated_region(p, true);
|
poison_head_t *head = verify_allocated_region(p, true);
|
||||||
assert(head != NULL);
|
assert(head != NULL);
|
||||||
size_t result = multi_heap_get_allocated_size_impl(heap, head);
|
size_t result = multi_heap_get_allocated_size_impl(heap, head);
|
||||||
|
subtract_poison_overhead(&result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,3 +1,8 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||||
|
*/
|
||||||
/*
|
/*
|
||||||
Generic test for malloc/free
|
Generic test for malloc/free
|
||||||
*/
|
*/
|
||||||
@@ -18,7 +23,6 @@
|
|||||||
static int **allocatedMem;
|
static int **allocatedMem;
|
||||||
static int noAllocated;
|
static int noAllocated;
|
||||||
|
|
||||||
|
|
||||||
static int tryAllocMem(void) {
|
static int tryAllocMem(void) {
|
||||||
int i, j;
|
int i, j;
|
||||||
const int allocateMaxK=1024*5; //try to allocate a max of 5MiB
|
const int allocateMaxK=1024*5; //try to allocate a max of 5MiB
|
||||||
@@ -154,3 +158,25 @@ TEST_CASE("malloc/calloc(0) should not call failure callback", "[heap]")
|
|||||||
TEST_ASSERT_NULL(ptr);
|
TEST_ASSERT_NULL(ptr);
|
||||||
TEST_ASSERT_FALSE(failure_occured);
|
TEST_ASSERT_FALSE(failure_occured);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("test get allocated size", "[heap]")
|
||||||
|
{
|
||||||
|
// random values to test, some are 4 bytes aligned, some are not
|
||||||
|
const size_t alloc_sizes[] = { 1035, 1064, 1541 };
|
||||||
|
const size_t iterations = sizeof(alloc_sizes) / sizeof(size_t);
|
||||||
|
void *ptr_array[iterations];
|
||||||
|
|
||||||
|
for (size_t i = 0; i < iterations; i++) {
|
||||||
|
ptr_array[i] = heap_caps_malloc(alloc_sizes[i], MALLOC_CAP_DEFAULT);
|
||||||
|
TEST_ASSERT_NOT_NULL(ptr_array[i]);
|
||||||
|
|
||||||
|
// test that the heap_caps_get_allocated_size() returns the right number of bytes (aligned to 4 bytes
|
||||||
|
// since the heap component aligns to 4 bytes)
|
||||||
|
const size_t aligned_size = (alloc_sizes[i] + 3) & ~3;
|
||||||
|
const size_t real_size = heap_caps_get_allocated_size(ptr_array[i]);
|
||||||
|
printf("initial size: %d, requested size : %d, allocated size: %d\n", alloc_sizes[i], aligned_size, real_size);
|
||||||
|
TEST_ASSERT_EQUAL(aligned_size, real_size);
|
||||||
|
|
||||||
|
heap_caps_free(ptr_array[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -789,7 +789,8 @@ components/heap/test/test_aligned_alloc_caps.c
|
|||||||
components/heap/test/test_allocator_timings.c
|
components/heap/test/test_allocator_timings.c
|
||||||
components/heap/test/test_heap_trace.c
|
components/heap/test/test_heap_trace.c
|
||||||
components/heap/test/test_leak.c
|
components/heap/test/test_leak.c
|
||||||
components/heap/test/test_malloc.c
|
components/heap/test/test_malloc_caps.c
|
||||||
|
components/heap/test/test_realloc.c
|
||||||
components/heap/test/test_runtime_heap_reg.c
|
components/heap/test/test_runtime_heap_reg.c
|
||||||
components/heap/test_multi_heap_host/main.cpp
|
components/heap/test_multi_heap_host/main.cpp
|
||||||
components/heap/test_multi_heap_host/test_multi_heap.cpp
|
components/heap/test_multi_heap_host/test_multi_heap.cpp
|
||||||
|
Reference in New Issue
Block a user