Skip to content

Commit

Permalink
[Logs] print size and caps when malloc fails
Browse files Browse the repository at this point in the history
  • Loading branch information
chipweinberger committed Feb 9, 2023
1 parent 0025915 commit 92b7149
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions components/heap/heap_caps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 requested_size, uint32_t caps)
{
char sSize[8];
char sCaps[8];
shex(sSize,requested_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.
Expand All @@ -54,15 +75,16 @@ 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) {
alloc_failed_callback(requested_size, caps, function_name);
}

#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
}

Expand Down

0 comments on commit 92b7149

Please sign in to comment.