From 6ed2371bd16d85777811dbd9187e1c3a476553a4 Mon Sep 17 00:00:00 2001 From: Chip Weinberger Date: Mon, 30 Jan 2023 02:47:18 -0800 Subject: [PATCH 1/2] [Logs] print size and caps when malloc fails --- components/heap/heap_caps.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/components/heap/heap_caps.c b/components/heap/heap_caps.c index e28653f69c..cb82842f4f 100644 --- a/components/heap/heap_caps.c +++ b/components/heap/heap_caps.c @@ -32,6 +32,27 @@ possible. This should optimize the amount of RAM accessible to the code without static esp_alloc_failed_hook_t alloc_failed_callback; + +#ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS +IRAM_ATTR static void shex(char buf[8], uint32_t n) +{ + for (int i = 0; i < 8; i++) { + uint8_t b4 = (n >> 28 - (i * 4)) & 0b1111; + buf[i] = b4 <= 9 ? '0' + b4 : 'a' + b4 - 10; + } +} +IRAM_ATTR static void fmt_abort_str(char dest[48], size_t size, uint32_t caps) +{ + char sSize[8]; + char sCaps[8]; + shex(sSize,size); + shex(sCaps,caps); + memcpy(dest, "Mem alloc fail. size 0x00000000 caps 0x00000000", 48); + memcpy(dest+23, sSize, 8); + memcpy(dest+39, sCaps, 8); +} +#endif + /* This takes a memory chunk in a region that can be addressed as both DRAM as well as IRAM. It will convert it to IRAM in such a way that it can be later freed. It assumes both the address as well as the length to be word-aligned. @@ -54,7 +75,6 @@ IRAM_ATTR static void *dram_alloc_to_iram_addr(void *addr, size_t len) return iptr + 1; } - IRAM_ATTR NOINLINE_ATTR static void heap_caps_alloc_failed(size_t requested_size, uint32_t caps, const char *function_name) { if (alloc_failed_callback) { @@ -62,7 +82,9 @@ IRAM_ATTR NOINLINE_ATTR static void heap_caps_alloc_failed(size_t requested_size } #ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS - esp_system_abort("Memory allocation failed"); + char buf[48]; + fmt_abort_str(buf, requested_size, caps); + esp_system_abort(buf); #endif } From 6e35b1eba30055d92b48bec13b35c4a7a41cc31e Mon Sep 17 00:00:00 2001 From: Guillaume Souchere Date: Fri, 10 Feb 2023 07:41:30 +0100 Subject: [PATCH 2/2] heap: Fix priority in shex() bit shift --- components/heap/heap_caps.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/heap/heap_caps.c b/components/heap/heap_caps.c index cb82842f4f..7911702a45 100644 --- a/components/heap/heap_caps.c +++ b/components/heap/heap_caps.c @@ -34,22 +34,22 @@ static esp_alloc_failed_hook_t alloc_failed_callback; #ifdef CONFIG_HEAP_ABORT_WHEN_ALLOCATION_FAILS -IRAM_ATTR static void shex(char buf[8], uint32_t n) +IRAM_ATTR static void hex_to_str(char buf[8], uint32_t n) { for (int i = 0; i < 8; i++) { - uint8_t b4 = (n >> 28 - (i * 4)) & 0b1111; - buf[i] = b4 <= 9 ? '0' + b4 : 'a' + b4 - 10; + uint8_t b4 = (n >> (28 - i * 4)) & 0b1111; + buf[i] = b4 <= 9 ? '0' + b4 : 'a' + b4 - 10; } } IRAM_ATTR static void fmt_abort_str(char dest[48], size_t size, uint32_t caps) { char sSize[8]; char sCaps[8]; - shex(sSize,size); - shex(sCaps,caps); + hex_to_str(sSize, size); + hex_to_str(sCaps, caps); memcpy(dest, "Mem alloc fail. size 0x00000000 caps 0x00000000", 48); - memcpy(dest+23, sSize, 8); - memcpy(dest+39, sCaps, 8); + memcpy(dest + 23, sSize, 8); + memcpy(dest + 39, sCaps, 8); } #endif