sys/new_delete: add malloc/free based new/delete implementation#17464
sys/new_delete: add malloc/free based new/delete implementation#17464maribu merged 2 commits intoRIOT-OS:masterfrom
Conversation
maribu
left a comment
There was a problem hiding this comment.
I have some style suggestions and a question, see inline
| void * operator new(size_t size, void * ptr) noexcept { | ||
| (void)size; | ||
| return ptr; | ||
| } |
There was a problem hiding this comment.
I don't quite get this one. I cannot find an API doc, but the arguments do not seem to make much sense unless this expected to behave somewhat like realloc
There was a problem hiding this comment.
According to https://www.cplusplus.com/reference/new/operator%20new/
void* operator new (std::size_t size, void* ptr) noexcept;
is the placement function which simply returns ptr and doesn't allocate storage:
ptr
A pointer to an already-allocated memory block of the proper size.
If called by a new-expression, the object is initialized (or constructed) at this location.
There was a problem hiding this comment.
The counterpart is
void operator delete (void* ptr, void* voidptr2) noexcept;
There was a problem hiding this comment.
Found an good example how it could be used at cppreference.com
// within any block scope...
{
alignas(T) unsigned char buf[sizeof(T)];
// Statically allocate the storage with automatic storage duration
// which is large enough for any object of type `T`.
T* tptr = new(buf) T; // Construct a `T` object, placing it directly into your
// pre-allocated storage at memory address `buf`.
tptr->~T(); // You must **manually** call the object's destructor
// if its side effects is depended by the program.
} // Leaving this block scope automatically deallocates `buf`.
|
@maribu I didn't change anything, it's just the original code. Do you think it is necessary to bring it into RIOT style? The static tests are happy with the code as it is. |
|
@maribu Fixed the style including indentation. |
d9af56c to
8bdb9fd
Compare
|
May I squash? |
|
Compilation fails for AVR. It seems that |
That old GCC does support C++11, if explicitly enabled. If I recall correctly it is just not the default back than (but it is for more recent toolchains). We could fix that with just adding Either way, please squash at will. |
On some platforms `libstdc++` is not used or not available, like on the AVR. Such platforms can use this module to implement the C++ `new` and `delete` operators using `malloc` and `free` respectively. However, to be thread-safe, a thread-safe implementation of `malloc` and `free` must be present.
8fb7b7d to
d778e77
Compare
👍 Indeed, had to do the same for ESP's Squashed. |
|
Thanks for reviewing and merging. With |
Contribution description
This PR adds a module extracted from the Arduino core to provide C++
newanddeleteoperators.On some platforms
libstdc++is not used or not available, like on the AVR. Such platforms can use this module to implement the C++newanddeleteoperators usingmallocandfreerespectively. However, to be thread-safe, a thread-safe implementation ofmallocandfreemust be present.The
new_deletemodule is used by default by AVR ifcppmodule is used.Testing procedure
Green CI. Module is compiled as part of C++ compilation of the
arduinomodule for ATmega boards.Issues/PRs references
Prerequesite for #17460 and #12518