Merge branch 'feat/heap-update-spiram-tests' into 'master'

feat(heap): Update heap tests on external memory

Closes IDF-11257

See merge request espressif/esp-idf!33737
This commit is contained in:
Guillaume Souchere
2024-09-24 14:28:47 +08:00
3 changed files with 47 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -19,7 +19,7 @@
#include <malloc.h> #include <malloc.h>
#include <inttypes.h> #include <inttypes.h>
TEST_CASE("Capabilities aligned allocator test", "[heap]") TEST_CASE("Capabilities aligned allocator test", "[heap][psram]")
{ {
uint32_t alignments = 0; uint32_t alignments = 0;
@ -29,7 +29,7 @@ TEST_CASE("Capabilities aligned allocator test", "[heap]")
uint8_t *buf = (uint8_t *)memalign(alignments, (alignments + 137)); uint8_t *buf = (uint8_t *)memalign(alignments, (alignments + 137));
if(((alignments & (alignments - 1)) != 0) || (!alignments)) { if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL ); TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments); //printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", alignments);
} else { } else {
TEST_ASSERT( buf != NULL ); TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments); printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
@ -58,7 +58,7 @@ TEST_CASE("Capabilities aligned allocator test", "[heap]")
uint8_t *buf = (uint8_t *)heap_caps_aligned_alloc(alignments, 10*1024, MALLOC_CAP_SPIRAM); uint8_t *buf = (uint8_t *)heap_caps_aligned_alloc(alignments, 10*1024, MALLOC_CAP_SPIRAM);
if(((alignments & (alignments - 1)) != 0) || (!alignments)) { if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL ); TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments); //printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", alignments);
} else { } else {
TEST_ASSERT( buf != NULL ); TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments); printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
@ -86,7 +86,7 @@ TEST_CASE("Capabilities aligned calloc test", "[heap]")
uint8_t *buf = (uint8_t *)heap_caps_aligned_calloc(alignments, 1, (alignments + 137), MALLOC_CAP_DEFAULT); uint8_t *buf = (uint8_t *)heap_caps_aligned_calloc(alignments, 1, (alignments + 137), MALLOC_CAP_DEFAULT);
if(((alignments & (alignments - 1)) != 0) || (!alignments)) { if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL ); TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments); //printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", alignments);
} else { } else {
TEST_ASSERT( buf != NULL ); TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments); printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);
@ -126,7 +126,7 @@ TEST_CASE("Capabilities aligned calloc test", "[heap]")
uint8_t *buf = (uint8_t *)(uint8_t *)heap_caps_aligned_calloc(alignments, 1, 10*1024, MALLOC_CAP_SPIRAM); uint8_t *buf = (uint8_t *)(uint8_t *)heap_caps_aligned_calloc(alignments, 1, 10*1024, MALLOC_CAP_SPIRAM);
if(((alignments & (alignments - 1)) != 0) || (!alignments)) { if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
TEST_ASSERT( buf == NULL ); TEST_ASSERT( buf == NULL );
//printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", aligments); //printf("[ALIGNED_ALLOC] alignment: %"PRIu32" is not a power of two, don't allow allocation \n", alignments);
} else { } else {
TEST_ASSERT( buf != NULL ); TEST_ASSERT( buf != NULL );
printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments); printf("[ALIGNED_ALLOC] alignment required: %"PRIu32" \n", alignments);

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@ -64,10 +64,9 @@ TEST_CASE("Malloc/overwrite, then free all available DRAM", "[heap]")
TEST_ASSERT(m1==m2); TEST_ASSERT(m1==m2);
} }
#if CONFIG_SPIRAM_USE_MALLOC
#if (CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL > 1024) #if CONFIG_SPIRAM_USE_MALLOC && (CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL > 1024)
TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed memory is exhausted", "[heap]") TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed memory is exhausted", "[heap][psram]")
{ {
char** dmaMem=malloc(sizeof(char*)*512); char** dmaMem=malloc(sizeof(char*)*512);
assert(dmaMem); assert(dmaMem);
@ -85,8 +84,44 @@ TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed m
} }
#endif #endif
#endif #if CONFIG_SPIRAM
TEST_CASE("Check if default cap allocates in external memory in priority", "[heap][psram]")
{
const size_t alloc_size = 256;
const uint32_t internal_cap = MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL;
const uint32_t external_cap = MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM;
// get the free internal memory size
size_t free_internal_memory = heap_caps_get_free_size(internal_cap);
size_t free_external_memory = heap_caps_get_free_size(external_cap);
// allocate a small amount of memory using MALLOC_CAP_DEFAULT
void * ptr = heap_caps_malloc(alloc_size, MALLOC_CAP_DEFAULT);
TEST_ASSERT_NOT_NULL(ptr);
// check that external memory is used by making sure the free internal memory size is unchanged
// and the free external memory size has decreased by at least the size of the allocation
TEST_ASSERT(free_internal_memory == heap_caps_get_free_size(internal_cap));
TEST_ASSERT(free_external_memory >= heap_caps_get_free_size(external_cap) + alloc_size);
heap_caps_free(ptr);
free_internal_memory = heap_caps_get_free_size(internal_cap);
free_external_memory = heap_caps_get_free_size(external_cap);
// only test malloc if CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL is equal to 0 since otherwise, allocations
// with size under the limit will be done internally.
#if (CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL == 0)
// test again using malloc
ptr = malloc(alloc_size);
TEST_ASSERT_NOT_NULL(ptr);
TEST_ASSERT(free_internal_memory == heap_caps_get_free_size(internal_cap));
TEST_ASSERT(free_external_memory >= heap_caps_get_free_size(external_cap) + alloc_size);
heap_caps_free(ptr);
#endif
}
#endif
/* As you see, we are desperately trying to outsmart the compiler, so that it /* As you see, we are desperately trying to outsmart the compiler, so that it
* doesn't warn about oversized allocations in the next two unit tests. * doesn't warn about oversized allocations in the next two unit tests.

View File

@ -66,7 +66,7 @@ def test_heap_in_flash(dut: Dut) -> None:
] ]
) )
def test_heap(dut: Dut) -> None: def test_heap(dut: Dut) -> None:
dut.run_all_single_board_cases() dut.run_all_single_board_cases(group='psram')
@pytest.mark.generic @pytest.mark.generic