2

I'm working on an embedded system that has CMSIS FreeRtos with heap4 as memory management scheme.

Now I'm trying to port the mbedTls to my system and I must provide dynamic allocation functions like alloc and free.

mbedTLS require two function to allocate and free memory. These are the function prototypes required by mbedTLS:

void * (*mbedtls_calloc)( size_t, size_t ) = MBEDTLS_PLATFORM_STD_CALLOC;
void (*mbedtls_free)( void * )             = MBEDTLS_PLATFORM_STD_FREE;

int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ),
                              void (*free_func)( void * ) )

Which is the best way to use correctly the heap provided by FreeRTOS? Memory pool for example.

Heap4 does not provide function like calloc and free. So, which function should I wrap to allow mbedTls to allocate memory?

Thanks in advance for the help.

Federico

Federico
  • 1,117
  • 6
  • 20
  • 37
  • 1
    "functions like..."!? Specify _exactly_ what functions are required, or provide a link to the mbedTLS product documentation you are referring to. – Clifford Feb 28 '18 at 12:24
  • @Clifford I need functions to allocate and free memory that are efficient in FreeRTOS. I don't know if the use of memory pool is the best choice. – Federico Feb 28 '18 at 13:29
  • not what I was asking. What does mbedTLS require - semantically. You mentioned allocation, calloc, and free but said "functions like" implying there are others. – Clifford Feb 28 '18 at 13:30
  • @Clifford my question is simple: How can I allocate dynamic memory in FreeRTOS that has heap4 ad memory management? That's all – Federico Feb 28 '18 at 13:33
  • with respect, that is not what you asked. Your edit answers my question, mbedTLS uses callbacks. The memory allocation functions in freeRTOS are documented, and common to all schemes. You may have to create wrappers to support some semantics such as calloc initialisation. I am struggling to see what is not obvious about this question. – Clifford Feb 28 '18 at 13:41
  • @Clifford I refer to CMSIS RTOS that is similar to FreeRTOS but the APIs are different. I know that I should write a wrapper to allocate and free memory but in CMSIS RTOS the only way to allocate memory is through memory pool. And I don't know if there is or not other way to do that. – Federico Feb 28 '18 at 13:48
  • The underlying FreeRTOS API remains available even with the CMSIS wrapper. Apart from that that is not your only option. You can wrap the standard global heap functions with mutex wrappers, or in some cases (ARM/Keil library for example), the standard library includes mutex callback hooks already to make malloc/free thread-safe. You are asking an X-Y problem. Describe your environment, and tool chain, and what you want to achieve rather than describing how you are trying to achieve it and asking how to implement your solution. – Clifford Feb 28 '18 at 13:58
  • heap4 does provide `pvPortMalloc` and `vPortFree`. – K. Koovalsky Feb 28 '18 at 15:03

1 Answers1

1

STEP 1: Make a wrapper of calloc and free functions in your source as shown below.

void *pvWrap_mbedtls_calloc( size_t sNb, size_t sSize )
{
    void *vPtr = NULL;
    if (sSize > 0) {
        vPtr = pvPortMalloc(sSize * sNb); // Call FreeRTOS or other standard API
        if(vPtr)
           memset(vPtr, 0, (sSize * sNb)); // Must required
    }
    return vPtr;
}

void vWrap_mbedtls_free( void *vPtr )
{
    if (vPtr) {
        vPortFree(vPtr); // Call FreeRTOS or other standard API
    }
}

STEP 2: Register these API at initialization time of you application as shown below.

void Custom_MBEDTLS_Init(void)
{
    mbedtls_platform_set_calloc_free(&pvWrap_mbedtls_calloc, &vWrap_mbedtls_free);
} 
Logan859
  • 93
  • 7