Page 1 of 1

ChibiOS Memory Allocators with C++ Standard Library

Posted: Sun Dec 28, 2014 6:51 pm
by sddavis14
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!

Re: ChibiOS Memory Allocators with C++ Standard Library

Posted: Sun Dec 28, 2014 7:52 pm
by Giovanni
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

Re: ChibiOS Memory Allocators with C++ Standard Library

Posted: Mon Dec 29, 2014 12:28 am
by sddavis14
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!