mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-09 20:41:14 +00:00
heap/heap_caps: Added tests for align allocation on both internal and external ram
This commit is contained in:
@@ -522,23 +522,14 @@ IRAM_ATTR void *heap_caps_aligned_alloc(size_t alignment, size_t size, int caps)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (caps & MALLOC_CAP_EXEC) {
|
//aligned alloc for now only supports default allocator or external
|
||||||
//MALLOC_CAP_EXEC forces an alloc from IRAM. There is a region which has both this as well as the following
|
//allocator.
|
||||||
//caps, but the following caps are not possible for IRAM. Thus, the combination is impossible and we return
|
if((caps & (MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM)) == 0) {
|
||||||
//NULL directly, even although our heap capabilities (based on soc_memory_tags & soc_memory_regions) would
|
return NULL;
|
||||||
//indicate there is a tag for this.
|
|
||||||
if ((caps & MALLOC_CAP_8BIT) || (caps & MALLOC_CAP_DMA)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
caps |= MALLOC_CAP_32BIT; // IRAM is 32-bit accessible RAM
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (caps & MALLOC_CAP_32BIT) {
|
//if caps requested are supported, clear undesired others:
|
||||||
/* 32-bit accessible RAM should allocated in 4 byte aligned sizes
|
caps &= (MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM);
|
||||||
* (Future versions of ESP-IDF should possibly fail if an invalid size is requested)
|
|
||||||
*/
|
|
||||||
size = (size + 3) & (~3); // int overflow checked above
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int prio = 0; prio < SOC_MEMORY_TYPE_NO_PRIOS; prio++) {
|
for (int prio = 0; prio < SOC_MEMORY_TYPE_NO_PRIOS; prio++) {
|
||||||
//Iterate over heaps and check capabilities at this priority
|
//Iterate over heaps and check capabilities at this priority
|
||||||
@@ -551,21 +542,10 @@ IRAM_ATTR void *heap_caps_aligned_alloc(size_t alignment, size_t size, int caps)
|
|||||||
//Heap has at least one of the caps requested. If caps has other bits set that this prio
|
//Heap has at least one of the caps requested. If caps has other bits set that this prio
|
||||||
//doesn't cover, see if they're available in other prios.
|
//doesn't cover, see if they're available in other prios.
|
||||||
if ((get_all_caps(heap) & caps) == caps) {
|
if ((get_all_caps(heap) & caps) == caps) {
|
||||||
//This heap can satisfy all the requested capabilities. See if we can grab some memory using it.
|
//Just try to alloc, nothing special.
|
||||||
if ((caps & MALLOC_CAP_EXEC) && esp_ptr_in_diram_dram((void *)heap->start)) {
|
ret = multi_heap_aligned_alloc(heap->heap, size, alignment);
|
||||||
//This is special, insofar that what we're going to get back is a DRAM address. If so,
|
if (ret != NULL) {
|
||||||
//we need to 'invert' it (lowest address in DRAM == highest address in IRAM and vice-versa) and
|
return ret;
|
||||||
//add a pointer to the DRAM equivalent before the address we're going to return.
|
|
||||||
ret = multi_heap_aligned_alloc(heap->heap, size + 4, alignment); // int overflow checked above
|
|
||||||
if (ret != NULL) {
|
|
||||||
return dram_alloc_to_iram_addr(ret, size + 4); // int overflow checked above
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//Just try to alloc, nothing special.
|
|
||||||
ret = multi_heap_aligned_alloc(heap->heap, size, alignment);
|
|
||||||
if (ret != NULL) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -222,6 +222,9 @@ void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t ali
|
|||||||
#else
|
#else
|
||||||
(void)data;
|
(void)data;
|
||||||
#endif
|
#endif
|
||||||
|
} else {
|
||||||
|
multi_heap_internal_unlock(heap);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Lets align our new obtained block address:
|
//Lets align our new obtained block address:
|
||||||
|
70
components/heap/test/test_aligned_alloc_caps.c
Normal file
70
components/heap/test/test_aligned_alloc_caps.c
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
Tests for the capabilities-based memory allocator.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <esp_types.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "unity.h"
|
||||||
|
#include "esp_attr.h"
|
||||||
|
#include "esp_heap_caps.h"
|
||||||
|
#include "esp_spi_flash.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
TEST_CASE("Capabilities aligned allocator test", "[heap]")
|
||||||
|
{
|
||||||
|
uint32_t alignments = 0;
|
||||||
|
|
||||||
|
printf("[ALIGNED_ALLOC] Allocating from default CAP: \n");
|
||||||
|
|
||||||
|
for(;alignments <= 1024; alignments++) {
|
||||||
|
uint8_t *buf = (uint8_t *)heap_caps_aligned_alloc(alignments, (alignments + 137), MALLOC_CAP_DEFAULT);
|
||||||
|
if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
|
||||||
|
TEST_ASSERT( buf == NULL );
|
||||||
|
//printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
|
||||||
|
} else {
|
||||||
|
TEST_ASSERT( buf != NULL );
|
||||||
|
printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
|
||||||
|
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
|
||||||
|
//Address of obtained block must be aligned with selected value
|
||||||
|
TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
|
||||||
|
|
||||||
|
//Write some data, if it corrupts memory probably the heap
|
||||||
|
//canary verification will fail:
|
||||||
|
memset(buf, 0xA5, (alignments + 137));
|
||||||
|
|
||||||
|
heap_caps_aligned_free(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Alloc from a non permitted area:
|
||||||
|
uint32_t *not_permitted_buf = (uint32_t *)heap_caps_aligned_alloc(alignments, (alignments + 137), MALLOC_CAP_EXEC | MALLOC_CAP_32BIT);
|
||||||
|
TEST_ASSERT( not_permitted_buf == NULL );
|
||||||
|
|
||||||
|
#if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_ESP32S2_SPIRAM_SUPPORT
|
||||||
|
alignments = 0;
|
||||||
|
printf("[ALIGNED_ALLOC] Allocating from external memory: \n");
|
||||||
|
|
||||||
|
for(;alignments <= 1024 * 1024; alignments++) {
|
||||||
|
//Now try to take aligned memory from IRAM:
|
||||||
|
uint8_t *buf = (uint8_t *)heap_caps_aligned_alloc(alignments, 10*1024, MALLOC_CAP_SPIRAM);
|
||||||
|
if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
|
||||||
|
TEST_ASSERT( buf == NULL );
|
||||||
|
//printf("[ALIGNED_ALLOC] alignment: %u is not a power of two, don't allow allocation \n", aligments);
|
||||||
|
} else {
|
||||||
|
TEST_ASSERT( buf != NULL );
|
||||||
|
printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
|
||||||
|
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
|
||||||
|
//Address of obtained block must be aligned with selected value
|
||||||
|
TEST_ASSERT(((intptr_t)buf & (alignments - 1)) == 0);
|
||||||
|
|
||||||
|
//Write some data, if it corrupts memory probably the heap
|
||||||
|
//canary verification will fail:
|
||||||
|
memset(buf, 0xA5, (10*1024));
|
||||||
|
heap_caps_aligned_free(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user