2
votes

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

1
"functions like..."!? Specify exactly what functions are required, or provide a link to the mbedTLS product documentation you are referring to.Clifford
@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
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
@Clifford my question is simple: How can I allocate dynamic memory in FreeRTOS that has heap4 ad memory management? That's allFederico
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

1 Answers

1
votes

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);
}