ChibiOS Memory Allocators with C++ Standard Library

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
sddavis14
Posts: 2
Joined: Sun Dec 28, 2014 6:38 pm

ChibiOS Memory Allocators with C++ Standard Library

Post 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!
User avatar
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

Post 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
sddavis14
Posts: 2
Joined: Sun Dec 28, 2014 6:38 pm

Re: ChibiOS Memory Allocators with C++ Standard Library

Post 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!
Post Reply