Hi everyone,
I'm developing a system that is using C++ with the standard library linked and ChibiOS. My toolchain is yagarto with newlib and the g++ compiler. I've overridden the new and delete operators to use chHeapAlloc/chHeapFree but obviously these are not used in the C++ standard library. It is my understanding that it relies on malloc in the C library. Is it okay to let the standard library use malloc? Will allocating containers like std::vector be thread safe despite not using the ChibiOS memory allocators? If not, how can I force the standard library to use the ChibiOS allocators or at least make malloc thread safe?
Thanks!
ChibiOS Memory Allocators with C++ Standard Library
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: ChibiOS Memory Allocators with C++ Standard Library
Hi,
The library allocator is not thread safe by itself unless a syscall.c module is implemented to support a reentrant newlib. Maybe it is possible you could do some linker tricks to replace the default malloc/calloc/free with thread safe variants.
Giovanni
The library allocator is not thread safe by itself unless a syscall.c module is implemented to support a reentrant newlib. Maybe it is possible you could do some linker tricks to replace the default malloc/calloc/free with thread safe variants.
Giovanni
Re: ChibiOS Memory Allocators with C++ Standard Library
I did a bit more testing and it turns out that implementing the C++ new and delete operators actually does replace their implementation in the standard library. I assumed that they didn't but thankfully I was wrong. Thanks for your quick reply!