aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthijs Kooijman <matthijs@stdin.nl>2020-09-17 17:15:42 +0200
committerMatthijs Kooijman <matthijs@stdin.nl>2020-09-17 20:01:23 +0200
commit07b6bd188f2395551fbd5eee3c3ca7ca3769bbbc (patch)
tree0853eae4772b01e5b3be8e025e6bb5f2edf12d4a
parent9a02bd8043c41f6ee57f136083ffc8bf8ec2bdc9 (diff)
Clean up and complete `<new>` header
This makes this header complete up to including C++14, except two exception classes that cannot be defined without `<exception>`. The functions related to the "new_handler" are declared but not actually defined, to prevent overhead and complexity. They are still declared to allow implementing them in user code if needed. This makes the implementation of all operator new and delete functions comply with the C++11/C++14 specification in terms of which should be actually implemented and which should be delegate to other functions. There are still some areas where these implementations are not entirely standards-compliant, which will be fixed in subsequent commits. This fixes part of #287 and fixes #47.
-rw-r--r--cores/arduino/new35
-rw-r--r--cores/arduino/new.cpp57
2 files changed, 79 insertions, 13 deletions
diff --git a/cores/arduino/new b/cores/arduino/new
index 763f5cc..3599571 100644
--- a/cores/arduino/new
+++ b/cores/arduino/new
@@ -21,11 +21,40 @@
#include <stdlib.h>
+namespace std {
+ struct nothrow_t {};
+ extern const nothrow_t nothrow;
+
+ // These are not actually implemented, to prevent overhead and
+ // complexity. They are still declared to allow implementing
+ // them in user code if needed.
+ typedef void (*new_handler)();
+ new_handler set_new_handler(new_handler new_p) noexcept;
+ new_handler get_new_handler() noexcept;
+} // namespace std
+
void * operator new(size_t size);
void * operator new[](size_t size);
-void * operator new(size_t size, void * ptr) noexcept;
-void operator delete(void * ptr);
-void operator delete[](void * ptr);
+
+void * operator new(size_t size, const std::nothrow_t tag) noexcept;
+void * operator new[](size_t size, const std::nothrow_t& tag) noexcept;
+
+void * operator new(size_t size, void *place) noexcept;
+void * operator new[](size_t size, void *place) noexcept;
+
+void operator delete(void * ptr) noexcept;
+void operator delete[](void * ptr) noexcept;
+
+#if __cplusplus >= 201402L
+void operator delete(void* ptr, size_t size) noexcept;
+void operator delete[](void * ptr, size_t size) noexcept;
+#endif // __cplusplus >= 201402L
+
+void operator delete(void* ptr, const std::nothrow_t& tag) noexcept;
+void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept;
+
+void operator delete(void* ptr, void* place) noexcept;
+void operator delete[](void* ptr, void* place) noexcept;
#endif
diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp
index fc30cf8..a36fd21 100644
--- a/cores/arduino/new.cpp
+++ b/cores/arduino/new.cpp
@@ -16,26 +16,63 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <stdlib.h>
+#include "new.h"
-void *operator new(size_t size) {
- return malloc(size);
+namespace std {
+ const nothrow_t nothrow;
}
-void *operator new[](size_t size) {
+void * operator new(size_t size) {
return malloc(size);
}
+void * operator new[](size_t size) {
+ return operator new(size);
+}
-void * operator new(size_t size, void * ptr) noexcept {
- (void)size;
- return ptr;
+void * operator new(size_t size, const std::nothrow_t tag) noexcept {
+ return operator new(size);
+}
+void * operator new[](size_t size, const std::nothrow_t& tag) noexcept {
+ return operator new[](size);
}
-void operator delete(void * ptr) {
- free(ptr);
+void * operator new(size_t size, void *place) noexcept {
+ // Nothing to do
+ (void)size; // unused
+ return place;
+}
+void * operator new[](size_t size, void *place) noexcept {
+ return operator new(size, place);
}
-void operator delete[](void * ptr) {
+void operator delete(void * ptr) noexcept {
free(ptr);
}
+void operator delete[](void * ptr) noexcept {
+ operator delete(ptr);
+}
+
+#if __cplusplus >= 201402L
+void operator delete(void* ptr, size_t size) noexcept {
+ operator delete(ptr);
+}
+void operator delete[](void * ptr, size_t size) noexcept {
+ operator delete[](ptr);
+}
+#endif // __cplusplus >= 201402L
+void operator delete(void* ptr, const std::nothrow_t& tag) noexcept {
+ operator delete(ptr);
+}
+void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept {
+ operator delete[](ptr);
+}
+
+void operator delete(void* ptr, void* place) noexcept {
+ (void)ptr; (void)place; // unused
+ // Nothing to do
+}
+void operator delete[](void* ptr, void* place) noexcept {
+ (void)ptr; (void)place; // unused
+ // Nothing to do
+}