newlib and FreeRTOS memory management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-09-26 10:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-09-26 6:55 PM
With embedded C++, the preferred options are:
- don't use containers which require dynamic allocation (prefer std::array, intrusive lists, etc)
- implement your own container-specific allocators.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-09-27 6:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-09-27 7:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-08-07 7:23 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-08-07 8:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-21 4:36 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-21 2:49 PM
Yep, link busted....
The time machine to the rescue!
https://web.archive.org/web/20191122000347/http://www.nadler.com/embedded/newlibAndFreeRTOS.html
-- pa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-21 2:53 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-01-18 11:27 AM
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
