Mbedtls callloc

I am trying to incorporate mbedTLS library to segger embOS 3.90. The mbedtls library uses heap allocation for certain operation using function mbedtls_calloc. I tried to use OS_malloc function as below. However,

#define mbedtls_calloc(a,b) OS_malloc(a*b)

But it is always returning NULL. However, if I use the same function anywhere outside mbedtls - par se, am using the function withing thread function, it seesms to be working fine.

Although root cause is unclear, it is inferred that, while utilizing OS_malloc for mbedTLS crypto purpose, it is returning NULL. Support in resolving the issue would be appreciated.

Best Regards,
Gopi Krishnan

Hi Gopi,
May I know what Mbed TLS version you are using?

Have you tried using MBEDTLS_PLATFORM_CALLOC_MACRO , MBEDTLS_PLATFORM_FREE_MACRO instead?
Do you have MBEDTLS_PLATFORM_MEMORY defined?

If you are using gcc based compiler, have you tried looking at the processed code to verify that your MACRO is interpreted as you expect?
You can do it by replacing the -o xxx.o in your gcc compilation with -E

Regards,
Mbed TLS Team member
Ron

I am using IAR workbench for building my source. So, gcc option might not work for me.

I verified config.h;
#define MBEDTLS_PLATFORM_MEMORY //is defined

#define MBEDTLS_PLATFORM_FREE_MACRO OS_free

But for calloc, I am giving

#define MBEDTLS_PLATFORM_CALLOC_MACRO(a,b) OS_malloc(a*b)

Still am facing the same.

Hi gopi,
Thank you for your cooperation.
Please note that the definition is MBEDTLS_PLATFORM_CALLOC_MACRO not MBEDTLS_PLATFORM_CALLOC_MACRO(a,b), so perhaps it behaves different with your toolchain.

I am assuming you are working on latest Mbed TLS version, so I would suggest you not define MBEDTLS_PLATFORM_FREE_MACRO and MBEDTLS_PLATFORM_CALLOC_MACRO , but implements your own calloc and free functions, with the following signatures:

void * (*calloc_func)( size_t, size_t );
void (*free_func)( void * );

and in your application, call mbedtls_platform_set_calloc_free with these functions as parameters for the callbacks.