From 4bfd549301f1148118db88ca78e2fbc97fdfcb5d Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Mon, 3 Jan 2022 12:07:43 +0100 Subject: [PATCH 1/2] sys/new_delete: add malloc/free based new/delete implementation 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. --- sys/Kconfig | 1 + sys/cpp_new_delete/Kconfig | 17 +++++++++++++ sys/cpp_new_delete/Makefile | 3 +++ sys/cpp_new_delete/doc.txt | 22 +++++++++++++++++ sys/cpp_new_delete/new_delete.cpp | 40 +++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 sys/cpp_new_delete/Kconfig create mode 100644 sys/cpp_new_delete/Makefile create mode 100644 sys/cpp_new_delete/doc.txt create mode 100644 sys/cpp_new_delete/new_delete.cpp diff --git a/sys/Kconfig b/sys/Kconfig index 24e208eabe3f..86e21343ddfa 100644 --- a/sys/Kconfig +++ b/sys/Kconfig @@ -19,6 +19,7 @@ rsource "checksum/Kconfig" rsource "color/Kconfig" rsource "crypto/Kconfig" rsource "congure/Kconfig" +rsource "cpp_new_delete/Kconfig" rsource "cxx_ctor_guards/Kconfig" rsource "div/Kconfig" rsource "embunit/Kconfig" diff --git a/sys/cpp_new_delete/Kconfig b/sys/cpp_new_delete/Kconfig new file mode 100644 index 000000000000..15d161326d47 --- /dev/null +++ b/sys/cpp_new_delete/Kconfig @@ -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. diff --git a/sys/cpp_new_delete/Makefile b/sys/cpp_new_delete/Makefile new file mode 100644 index 000000000000..025387e061b6 --- /dev/null +++ b/sys/cpp_new_delete/Makefile @@ -0,0 +1,3 @@ +include $(RIOTBASE)/Makefile.base + +CXXFLAGS += -std=c++11 diff --git a/sys/cpp_new_delete/doc.txt b/sys/cpp_new_delete/doc.txt new file mode 100644 index 000000000000..f4a8d3f04d4d --- /dev/null +++ b/sys/cpp_new_delete/doc.txt @@ -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. + + */ diff --git a/sys/cpp_new_delete/new_delete.cpp b/sys/cpp_new_delete/new_delete.cpp new file mode 100644 index 000000000000..7d8866151854 --- /dev/null +++ b/sys/cpp_new_delete/new_delete.cpp @@ -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 + +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); +} From d778e77c975ed9b101e6d86ae07b2a86b5f68904 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Mon, 3 Jan 2022 12:05:12 +0100 Subject: [PATCH 2/2] cpu/avr8_common: use C++ new and delete operator --- cpu/avr8_common/Kconfig | 2 ++ cpu/avr8_common/Makefile.dep | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/cpu/avr8_common/Kconfig b/cpu/avr8_common/Kconfig index c60d56fa3637..e982adc42335 100644 --- a/cpu/avr8_common/Kconfig +++ b/cpu/avr8_common/Kconfig @@ -14,6 +14,8 @@ config CPU_ARCH_AVR8 select MODULE_MALLOC_THREAD_SAFE if TEST_KCONFIG # static C++ constructors need guards for thread safe initialization select MODULE_CXX_CTOR_GUARDS if MODULE_CPP + # new and delete operators needed + select MODULE_CPP_NEW_DELETE if MODULE_CPP ## Common CPU symbols config CPU_ARCH diff --git a/cpu/avr8_common/Makefile.dep b/cpu/avr8_common/Makefile.dep index 86d551f3c5de..8a35c0ac99bc 100644 --- a/cpu/avr8_common/Makefile.dep +++ b/cpu/avr8_common/Makefile.dep @@ -18,3 +18,8 @@ endif ifneq (,$(filter cpp,$(FEATURES_USED))) USEMODULE += cxx_ctor_guards endif + +# new and delete operators needed +ifneq (,$(filter cpp,$(FEATURES_USED))) + USEMODULE += cpp_new_delete +endif