-
Notifications
You must be signed in to change notification settings - Fork 2.1k
sys/new_delete: add malloc/free based new/delete implementation #17464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Copyright (C) 2021 Gunar Schorcht | ||
| # | ||
| # This file is subject to the terms and conditions of the GNU Lesser | ||
| # General Public License v2.1. See the file LICENSE in the top level | ||
| # directory for more details. | ||
| # | ||
|
|
||
| config MODULE_CPP_NEW_DELETE | ||
| bool | ||
| depends on TEST_KCONFIG | ||
| depends on MODULE_CPP | ||
| help | ||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| include $(RIOTBASE)/Makefile.base | ||
|
|
||
| CXXFLAGS += -std=c++11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| @defgroup sys_cpp_new_delete C++ new and delete operator module | ||
| @ingroup sys | ||
| @brief This module provides the `new` and `delete` operators for platforms | ||
| that do not use `libstdc++`. | ||
| @warning This module is automatically selected, if needed. Never add it | ||
| manually. | ||
|
|
||
| # Background | ||
|
|
||
| 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. | ||
|
|
||
| # Usage | ||
|
|
||
| This module is intended to be use by platforms that not providing the required | ||
| operators. Hence, application developers and users should never select this | ||
| module by hand. | ||
|
|
||
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright (c) 2014 Arduino. All right reserved. | ||
| * | ||
| * This library is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 2.1 of the License, or (at your option) any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the GNU Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with this library; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| */ | ||
|
|
||
| #include <stdlib.h> | ||
|
|
||
| void *operator new(size_t size) { | ||
| return malloc(size); | ||
| } | ||
|
|
||
| void *operator new[](size_t size) { | ||
| return malloc(size); | ||
| } | ||
|
|
||
| void *operator new(size_t size, void *ptr) noexcept { | ||
| (void)size; | ||
| return ptr; | ||
| } | ||
|
|
||
| void operator delete(void *ptr) { | ||
| free(ptr); | ||
| } | ||
|
|
||
| void operator delete[](void *ptr) { | ||
| free(ptr); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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/
is the
placementfunction which simply returnsptrand doesn't allocate storage: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
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