cancel
Showing results for 
Search instead for 
Did you mean: 

newlib and FreeRTOS memory management

DShar.17
Associate II

hi all,

we're using SW4STM32 on a project that uses CubeMX-generated code with FreeRTOS. Our own application is a mix of C and C++. We soon found that malloc/free (used behind the scenes by C++) don't play nice in the multi-threaded environment.

Dave Nadler's solution for overriding FreeRTOS memory management with newlib's [http://www.nadler.com/embedded/newlibAndFreeRTOS.html] works as far as we've tested it ...

... but is there no ready-made already integrated solution?

Thank you for your consideration,

-Dan

8 REPLIES 8
Rob.Riggs
Senior III

With embedded C++, the preferred options are:

  1. don't use containers which require dynamic allocation (prefer std::array, intrusive lists, etc)
  2. implement your own container-specific allocators.
  3. replace operator ::new and ::delete.

Are any of these an option for you?

The problem with 2 & 3 is that with C++ you need to enable exception handling in order to properly handle and detect out-of-memory conditions for containers. std::vector, for example, can only indicate that an allocation failed by throwing std::bad_alloc.

Pavel A.
Evangelist III

There are ready, integrated solutions for C++. They are called Linux, VxWorks and so on.

If you don't have enough RAM - simply don't use C++. C11 is good enough for very small systems.

The fun of C++ is in using the power stuff freely. Otherwise fun is out and pain in.

Enjoy the holiday!

-- pa

DShar.17
Associate II

Thanks for the quick reply, Rob.

Yes, those are all options for us.

Maybe I should've mentioned that the problem (newlib and FreeRTOS interaction) first came to light trying to "printf" from several threads (mix of C and C++) at once ... then we realized it was a bigger problem.

@Community memberharon​ - Please note I've just posted updated details for STM here:

http://www.nadler.com/embedded/newlibAndFreeRTOS.html

We use C++ heavily, with the caveats suggested by @Rob.Riggs​ above.

Hope that helps!

Best Regards, Dave

Thanks very much, Dave. We basically adapted your original solution much as you just did for STM. It's been happily running for months and months. It was great help, and much appreciated.

Cheers,

-Dan

Hi Dave,

I am interested in you solution, but unfortnutaly this link is broken :

http://www.nadler.com/embedded/newlibAndFreeRTOS.html

Can you kindly help ?

Best Regards

Marco

Link is not broken, just wait a while and it loads.

alexgorbatchov's syntax-highliter scripts are not loading, but the main page loads after getting 522 errors on the scripts.

Aaarrrggg...

I'll try fix it in the next few days.