Two simultaneous heaps combining: kernel heap and libc heap

Hello Support team,

I have an issue with two simultaneous heaps in my project. I use nrf Connect SDK 3.2.2 with the next configuration options:

CONFIG_REQUIRES_FULL_LIBCPP=y
CONFIG_HEAP_MEM_POOL_SIZE=64536
By default I have Picolibc library with COMMON_LIBC_MALLOC_ARENA_SIZE=-1 As I understand it means that libc uses it's own heap with size of none allocated space in RAM
Having two simultaneous heaps is not a good idea with many drawbacks. So:
1. I would like to have only one heap with ability to get current status of free and occupied size. Kernel heap provides such API.
2. It is good to have an ability to configure the heap memory size as it is done for libc with config COMMON_LIBC_MALLOC_ARENA_SIZE=-1 to occupy none allocated memory.
 
Is there any way to combine these two heaps to one? I know that with current configuration when I allocates memory with 'new' operator it uses libc heap. When I create threads dynamically it uses kernel heap. I don't have an ability to estimate libc heap usage (or I don't know about it).
As I understand I can override 'new' and 'delete' operator for C++. It might be possible to override malloc, calloc and realloc functions for C. But how I can do it correctly and what is the list of functions I should override?
So, what is the good solution for it? What do you recommend? 
  • Hi Priyanka,

    Thank you for this answer. I think that the dependency in Kconfig for MCUMGR_GRP_SETTINGS_BUFFER_TYPE_HEAP is not quite correct. It relies on COMMON_LIBC_MALLOC_ARENA_SIZE > 0 condition but in implementation it uses malloc call to allocate the memory and as we see the malloc function does not come from the LibC all the time. 

    Anyway, any fix for heap allocation in my case needs to carry a local patch for now.

Related