-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
sys/new_delete: add malloc/free based new/delete implementation #17464
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The counterpart is
void operator delete (void* ptr, void* voidptr2) noexcept;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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++
new
anddelete
operators.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
anddelete
operators usingmalloc
andfree
respectively. However, to be thread-safe, a thread-safe implementation ofmalloc
andfree
must be present.The
new_delete
module is used by default by AVR ifcpp
module is used.Testing procedure
Green CI. Module is compiled as part of C++ compilation of the
arduino
module for ATmega boards.Issues/PRs references
Prerequesite for #17460 and #12518