From 8a98a67bf1630b923b0c0669499427e81a723fe5 Mon Sep 17 00:00:00 2001 From: Jan Van Winkel Date: Thu, 14 Nov 2019 00:23:33 +0100 Subject: [PATCH] cpp: Use malloc/free instead of kernel variants in new/delete Use malloc/free instead of k_malloc/k_free in operator new/delete implementation or use libstdc++ implementation when available. Further updated cpp_synchronization sample to enable minimal libc heap as virtual destructor requires operator delete which depends on free. Signed-off-by: Jan Van Winkel --- samples/cpp_synchronization/prj.conf | 1 + subsys/cpp/CMakeLists.txt | 4 +- subsys/cpp/Kconfig | 7 -- subsys/cpp/cpp_new.cpp | 93 ++----------------- subsys/cpp/cpp_vtable.cpp | 3 - tests/application_development/libcxx/prj.conf | 1 - .../libcxx/testcase.yaml | 6 -- 7 files changed, 11 insertions(+), 104 deletions(-) diff --git a/samples/cpp_synchronization/prj.conf b/samples/cpp_synchronization/prj.conf index fa7da805792..50396291ec5 100644 --- a/samples/cpp_synchronization/prj.conf +++ b/samples/cpp_synchronization/prj.conf @@ -1 +1,2 @@ CONFIG_CPLUSPLUS=y +CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=128 diff --git a/subsys/cpp/CMakeLists.txt b/subsys/cpp/CMakeLists.txt index 73f4261ae19..5f10e31f09c 100644 --- a/subsys/cpp/CMakeLists.txt +++ b/subsys/cpp/CMakeLists.txt @@ -6,7 +6,9 @@ zephyr_sources( cpp_dtors.c ) -if (NOT CONFIG_LIB_CPLUSPLUS OR CONFIG_ZEPHYR_CPLUSPLUS) +if (NOT CONFIG_LIB_CPLUSPLUS AND + (NOT CONFIG_MINIMAL_LIBC OR + (CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE GREATER 0))) zephyr_sources( cpp_virtual.c cpp_vtable.cpp diff --git a/subsys/cpp/Kconfig b/subsys/cpp/Kconfig index fa502b5374b..73c0c64a637 100644 --- a/subsys/cpp/Kconfig +++ b/subsys/cpp/Kconfig @@ -63,13 +63,6 @@ config RTTI help This option enables support of C++ RTTI. - -config ZEPHYR_CPLUSPLUS - bool "Use Zephyr C++ Implementation" - help - Use Zephyr implementation for operator new, delete, pure virtual - functions and vtables. - endif # LIB_CPLUSPLUS endif # ! MINIMAL_LIBC diff --git a/subsys/cpp/cpp_new.cpp b/subsys/cpp/cpp_new.cpp index 29f6b6414ec..ad739c1c6a7 100644 --- a/subsys/cpp/cpp_new.cpp +++ b/subsys/cpp/cpp_new.cpp @@ -4,115 +4,36 @@ * SPDX-License-Identifier: Apache-2.0 */ -#if defined(CONFIG_LIB_CPLUSPLUS) -#include -#endif // CONFIG_LIB_CPLUSPLUS -#include +#include void* operator new(size_t size) { -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - void* ptr = k_malloc(size); -#if defined(__cpp_exceptions) && defined(CONFIG_LIB_CPLUSPLUS) - if (!ptr) - throw std::bad_alloc(); -#endif - return ptr; -#else - ARG_UNUSED(size); - return NULL; -#endif + return malloc(size); } void* operator new[](size_t size) { -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - void* ptr = k_malloc(size); -#if defined(__cpp_exceptions) && defined(CONFIG_LIB_CPLUSPLUS) - if (!ptr) - throw std::bad_alloc(); -#endif - return ptr; -#else - ARG_UNUSED(size); - return NULL; -#endif + return malloc(size); } void operator delete(void* ptr) noexcept { -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - k_free(ptr); -#else - ARG_UNUSED(ptr); -#endif + free(ptr); } void operator delete[](void* ptr) noexcept { -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - k_free(ptr); -#else - ARG_UNUSED(ptr); -#endif + free(ptr); } -#if defined(CONFIG_LIB_CPLUSPLUS) -void* operator new(size_t size, const std::nothrow_t&) noexcept -{ -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - return k_malloc(size); -#else - ARG_UNUSED(size); - return NULL; -#endif -} - -void* operator new[](size_t size, const std::nothrow_t&) noexcept -{ -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - return k_malloc(size); -#else - ARG_UNUSED(size); - return NULL; -#endif -} - -void operator delete(void* ptr, const std::nothrow_t&) noexcept -{ -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - k_free(ptr); -#else - ARG_UNUSED(ptr); -#endif -} - -void operator delete[](void* ptr, const std::nothrow_t&) noexcept -{ -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - k_free(ptr); -#else - ARG_UNUSED(ptr); -#endif -} -#endif // CONFIG_LIB_CPLUSPLUS - #if (__cplusplus > 201103L) void operator delete(void* ptr, size_t) noexcept { -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - k_free(ptr); -#else - ARG_UNUSED(ptr); -#endif + free(ptr); } void operator delete[](void* ptr, size_t) noexcept { -#if (CONFIG_HEAP_MEM_POOL_SIZE > 0) - k_free(ptr); -#else - ARG_UNUSED(ptr); -#endif + free(ptr); } #endif // __cplusplus > 201103L diff --git a/subsys/cpp/cpp_vtable.cpp b/subsys/cpp/cpp_vtable.cpp index 0b31f0ddf7b..8f42f6e1977 100644 --- a/subsys/cpp/cpp_vtable.cpp +++ b/subsys/cpp/cpp_vtable.cpp @@ -14,8 +14,6 @@ * @brief basic virtual tables required for classes to build * */ -#if !defined(CONFIG_LIB_CPLUSPLUS) - namespace __cxxabiv1 { class __class_type_info { virtual void dummy(); @@ -26,4 +24,3 @@ namespace __cxxabiv1 { void __class_type_info::dummy() { } // causes the vtable to get created here void __si_class_type_info::dummy() { } // causes the vtable to get created here }; -#endif // !defined(CONFIG_LIB_CPLUSPLUS) diff --git a/tests/application_development/libcxx/prj.conf b/tests/application_development/libcxx/prj.conf index aade1fafb7d..b07b5f571b1 100644 --- a/tests/application_development/libcxx/prj.conf +++ b/tests/application_development/libcxx/prj.conf @@ -2,6 +2,5 @@ CONFIG_NEWLIB_LIBC=y CONFIG_CPLUSPLUS=y CONFIG_LIB_CPLUSPLUS=y CONFIG_STD_CPP17=y -CONFIG_HEAP_MEM_POOL_SIZE=1024 CONFIG_ZTEST=y CONFIG_ZTEST_STACKSIZE=2048 diff --git a/tests/application_development/libcxx/testcase.yaml b/tests/application_development/libcxx/testcase.yaml index cf2bb7477c9..2afc9f9443d 100644 --- a/tests/application_development/libcxx/testcase.yaml +++ b/tests/application_development/libcxx/testcase.yaml @@ -4,12 +4,6 @@ tests: platform_exclude: qemu_x86_coverage min_flash: 54 tags: cpp - misc.app_dev.libcxx.zephyr_cpp: - arch_exclude: posix - platform_exclude: qemu_x86_coverage - tags: cpp - extra_configs: - - CONFIG_ZEPHYR_CPLUSPLUS=y misc.app_dev.libcxx.exceptions: arch_exclude: posix platform_exclude: qemu_x86_coverage