QUERY · ISSUE
memory malloc error on stm32f407
port-stm32
print error log:
#Malloc: 0x20013560 16
#Malloc: 0x20013570 12
#Malloc: 0x200135A0 16
#Malloc: 0x200135B0 24
**_#Malloc bytes:4096 failed_**
#Testing 1kB...
Addr:0x20013B10
#Testing 1.25kB...
Addr:0x20013B10
#Testing 1.5kB...
Addr:0x20013B10
#Testing 2kB...
**_Addr:0x2001B5B0_**
#Testing 3kB...
###Malloc bytes:3072 failed
source code:
void *m_malloc(size_t num_bytes) {
void *ptr = malloc(num_bytes);
if (ptr == NULL && num_bytes != 0) {
//PrintInfo("num_bytes : %d ", num_bytes);
SEGGER_RTT_printf(0, "#Malloc bytes:%d failed\r\n", num_bytes);
SEGGER_RTT_printf(0, "#Testing 1kB...\r\n");
ptr = malloc(1024);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1024);
} else {
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
free(ptr);
}
SEGGER_RTT_printf(0, "#Testing 1.25kB...\r\n");
ptr = malloc(1250);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1250);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
SEGGER_RTT_printf(0, "#Testing 1.5kB...\r\n");
ptr = malloc(1536);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1536);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
SEGGER_RTT_printf(0, "#Testing 2kB...\r\n");
ptr = malloc(1900);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1900);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
SEGGER_RTT_printf(0, "#Testing 3kB...\r\n");
ptr = malloc(3072);
if (!ptr) {
SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 3072);
} else {
free(ptr);
SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
}
m_malloc_fail(num_bytes);
}
#if MICROPY_MEM_STATS
MP_STATE_MEM(total_bytes_allocated) += num_bytes;
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
UPDATE_PEAK();
#endif
// PrintInfo("malloc %d : %p", num_bytes, ptr);
SEGGER_RTT_printf(0, "#Malloc: 0x%08x %d\r\n", ptr, num_bytes);
return ptr;
}
heap & stack address:
.heap 0x20012f94 0x4000 load address 0x080b2a44
0x20012f94 . = ALIGN (0x4)
0x20016f94 . = (. + _minimum_heap_size)
*fill* 0x20012f94 0x4000
0x20016f94 . = ALIGN (0x4)
.stack 0x20016f94 0x800 load address 0x080b2a44
0x20016f94 . = ALIGN (0x4)
0x20017794 . = (. + _minimum_stack_size)
*fill* 0x20016f94 0x800
0x20017794 . = ALIGN (0x4)
CANDIDATE · PULL REQUEST
py: introduced tracked alloc/free functions, and use them to replace mbedtls malloc/free
This consolidates the duplicated malloc/free functions in mimxrt/mbedtls/mbedtls_port.c and stm32/mbedtls/mbedtls_port.c, but putting these common functions in py/malloc.c and making them generally available to all ports if needed.
This PR has one subtle change to functionality: the new malloc now returns NULL if it fails, instead of raising an exception. This matches the C malloc semantics and mbedtls has checks for NULL returned from malloc, and returns a suitable error code. That means that ssl code that previously raised a MemoryError will now raise OSError(...).