malloc/k_malloc is unable to allocate memory size set in CONFIG_HEAP_MEM_POOL_SIZE

I have made a standalone firmware to test the memory allocation functions on nrf53.  i need to use realloc() in a separate application fw and am therefore testing the std c malloc and realloc function. This standalone fw is allocating memory for a string, appending an ascii char onto this string and then looping this process. On each loop memory is reallocated so that there is a free block for the next char. The issue is i set CONFIG_HEAP_MEM_POOL_SIZE=4096 but the program seems to fall over after allocating 1336 Bytes. I have tried switching to k_malloc and creating an equivalent of realloc using k_malloc but this had not fixed the issue either. 

I am curious as to why i cannot allocate more memory despite defining a heap size of 4096?

I have tested with other heap sizes too. With 5096 i can allocate 1674, with 6096 i can allocate 2009 and with 7096 set i can allocate 2337. 

here is a link to my standalone code.

memcpy.zip

  • Hi,

     

    Just to show how the kernel .heap is setup, here it is declared, and you can see the different k_*alloc functions below these lines:

    https://github.com/nrfconnect/sdk-zephyr/blob/v3.4.99-ncs1-2/kernel/mempool.c#L61

     

    Instead of doing a full realloc, I would start with smaller steps, for instance something like this to get the ceil value for k_malloc/k_calloc:

        int test_malloc_size = CONFIG_HEAP_MEM_POOL_SIZE;
        while (1) {
            void * ptr = k_malloc(test_malloc_size);
            if (!ptr) {
                test_malloc_size -= 8;
            } else {
                LOG_DBG("Heap configured size: %d", CONFIG_HEAP_MEM_POOL_SIZE);
                LOG_DBG("Heap max size allocated: %d", test_malloc_size);
                k_free(ptr);
                break;
            }
        }

     

    This gives me an output similar to this this:

     [00:00:00.000,335] <dbg> windoor_debug: main: Heap configured size: 4096
     [00:00:00.000,366] <dbg> windoor_debug: main: Heap max size allocated: 4024
    

     

    Unfortunately, the zephyr heap documentation does not address a max allocation size, the sys_heap API touches on the subject in the header comments:

    https://github.com/nrfconnect/sdk-zephyr/blob/v3.4.99-ncs1-2/include/zephyr/sys/sys_heap.h#L18-L55

     

    In addition, there are overhead to the globally assigned memory, if we look into the sys_heap_init function:

    https://github.com/nrfconnect/sdk-zephyr/blob/v3.4.99-ncs1-2/lib/os/heap.c#L485

      

    k_realloc is a currently missing feature, but it is being discussed/addressed in this issue:

    https://github.com/zephyrproject-rtos/zephyr/issues/41151

    The issue is i set CONFIG_HEAP_MEM_POOL_SIZE=4096 but the program seems to fall over after allocating 1336 Bytes. I have tried switching to k_malloc and creating an equivalent of realloc using k_malloc but this had not fixed the issue either. 

    I am curious as to why i cannot allocate more memory despite defining a heap size of 4096?

    If we look at the sizes that are successfully allocated, it is close to three times lower than one allocation in my test. This could be a allocation issue in the routines themselves. Could you try my test and see if you get similar results?

     

    Kind regards,

    Håkon

Related