Skip to main content
Visitor II
September 26, 2018
Question

newlib and FreeRTOS memory management

  • September 26, 2018
  • 6 replies
  • 2606 views

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

    This topic has been closed for replies.

    6 replies

    Graduate II
    September 27, 2018

    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.

    Super User
    September 27, 2018

    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.17Author
    Visitor II
    September 27, 2018

    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.

    Explorer
    August 7, 2019

    @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

    DShar.17Author
    Visitor II
    August 7, 2019

    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

    Explorer
    June 21, 2020

    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.

    Visitor II
    January 18, 2025

    We use FreeRTOS memlang aliased instead of newlib...


    void *malloc(size_t nbytes) __attribute__((alias("pvPortMalloc")));
    void free(void *ptr) __attribute__((alias("vPortFree")));

    ...inspired by...

    https://github.com/CHERTS/esp8266-devkit/blob/master/Espressif/ESP8266_RTOS_SDK/third_party/freertos/heap_4.c