FX3 malloc

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi,

   

Is it valid to use malloc()/calloc() functions with CyU3PByteAlloc()/CyU3PMemAlloc()?

   

I need to use some functions like strdup() that uses malloc() internally.

0 Likes
6 Replies
arasc_296896
Level 3
Level 3

 Hi,

   

I had the same problem dealing with malloc() function. Though it is a part of standard library (stdlib.h) i got an error message saying "undefined reference to `_sbrk' line 0 C/C++ Problem". 

   

And usage of CyU3PMemAlloc() and CyU3PMemFree() is not as dependable as I faced issues while allocating and freeing memory for larger iterations. 

0 Likes
Anonymous
Not applicable

@eAR, solution for your issue is very simple: just add file cyfxcppsyscall.cpp to likage. It contains "implementation" for all required syscalls. _sbrk() too.

   

But, I don't fully understand how to _sbrk() works and is it legal to mix malloc() and pool-based memory allocations?

0 Likes
arasc_296896
Level 3
Level 3

 Yes that's again a concern on whether malloc will use memory spaces from CY_U3P_MEM_HEAP_SIZE as specified in cyfxtx.c . But sorry that I couldn't get the first part... are u suggesting me to include cyfxcppsyscall.cpp in my project ? orif I should link it in properties option , can you brief ?

0 Likes
arasc_296896
Level 3
Level 3

 You can try printing the address assigned using both functions separately would be my suggestion ! And also I faced an issue like once the heap pool is empty, even though if I free the memory using CyU3PMemFree(), I am running out of space and CyU3PMemAlloc() allocates null to my variables.. Have you faced any similar issues ?

0 Likes
Anonymous
Not applicable

> are u suggesting me to include cyfxcppsyscall.cpp in my project ? orif I should link it in properties option , can you brief ?

   

cyfxcppsyscall.cpp located at the $FX3_INSTALL_PATH/firmware/common. It contains implementation for the some system calls that required by stdlib. As I right understand, this calls needed only by C++ applications. My project is C++ based so I use it as is. If your project is a plain C based, you can simple rename it to .c and add to project.

   

In short: in some situations you MUST provide implementation of the some calls required by used libraries.'

   

Look into: https://balau82.wordpress.com/2010/12/16/using-newlib-in-arm-bare-metal-programs/

0 Likes
Anonymous
Not applicable

Answer to my question:

   

with default ld-script it is valid to use malloc-family function. Malloc allocator uses it own memory area differ to default ThreadX mem-pool.

0 Likes