mirror of
https://github.com/espressif/esp-idf.git
synced 2024-09-20 00:36:01 -04:00
Merge branch 'fix/heap-get-allocated-size' into 'master'
heap: Fix erroneous value returned by heap_caps_get_allocated_size() when poisoning is enabled Closes IDF-3917 See merge request espressif/esp-idf!22176
This commit is contained in:
commit
cba1095508
@ -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,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||||
*/
|
*/
|
||||||
@ -23,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
|
||||||
@ -159,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]")
|
||||||
|
{
|
||||||
|
const size_t iterations = 32;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < iterations; i++) {
|
||||||
|
// minimum block size is 12, so to avoid unecessary logic in the test,
|
||||||
|
// set the minimum requested size to 12.
|
||||||
|
const size_t alloc_size = rand() % 1024 + 12;
|
||||||
|
|
||||||
|
void *ptr = heap_caps_malloc(alloc_size, MALLOC_CAP_DEFAULT);
|
||||||
|
TEST_ASSERT_NOT_NULL(ptr);
|
||||||
|
|
||||||
|
// 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_size + 3) & ~3;
|
||||||
|
printf("initial size: %d, requested size : %d, allocated size: %d\n", alloc_size, aligned_size, heap_caps_get_allocated_size(ptr));
|
||||||
|
TEST_ASSERT_EQUAL(aligned_size, heap_caps_get_allocated_size(ptr));
|
||||||
|
|
||||||
|
heap_caps_free(ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user