diff --git a/samples/cpp_synchronization/prj.conf b/samples/cpp_synchronization/prj.conf index fa7da8057923e..50396291ec5c2 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 73f4261ae1951..5f10e31f09ca7 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 fa502b5374b90..73c0c64a63753 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 29f6b6414ec2f..ad739c1c6a73e 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 0b31f0ddf7b52..8f42f6e197793 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/cpp/prj.conf b/tests/application_development/cpp/prj.conf index 5162a7aea2bd5..0cd5090b439c4 100644 --- a/tests/application_development/cpp/prj.conf +++ b/tests/application_development/cpp/prj.conf @@ -2,3 +2,4 @@ CONFIG_CPLUSPLUS=y CONFIG_NET_BUF=y CONFIG_ZTEST=y CONFIG_ZTEST_STACKSIZE=2048 +CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=128 diff --git a/tests/application_development/cpp/src/main.cpp b/tests/application_development/cpp/src/main.cpp index cdafa07677546..c3114e784b794 100644 --- a/tests/application_development/cpp/src/main.cpp +++ b/tests/application_development/cpp/src/main.cpp @@ -31,6 +31,14 @@ #include +class foo_class { +public: + foo_class(int foo) : foo(foo) {} + int get_foo() const { return foo;} +private: + int foo; +}; + struct foo { int v1; }; @@ -49,7 +57,19 @@ static int test_init(struct device *dev) SYS_INIT(test_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); + +static void test_new_delete(void) +{ + foo_class *test_foo = new foo_class(10); + zassert_equal(test_foo->get_foo(), 10, NULL); + delete test_foo; +} + void test_main(void) { - /* Does nothing. This is a compile only test. */ + ztest_test_suite(cpp_tests, + ztest_unit_test(test_new_delete) + ); + + ztest_run_test_suite(cpp_tests); } diff --git a/tests/application_development/cpp/testcase.yaml b/tests/application_development/cpp/testcase.yaml index 02fbd0b6756fe..7cbba64d34132 100644 --- a/tests/application_development/cpp/testcase.yaml +++ b/tests/application_development/cpp/testcase.yaml @@ -1,5 +1,5 @@ tests: misc.app_dev.cpp: arch_exclude: posix - build_only: true + platform_exclude: qemu_x86_coverage tags: cpp diff --git a/tests/application_development/libcxx/prj.conf b/tests/application_development/libcxx/prj.conf index aade1fafb7d70..b07b5f571b178 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 cf2bb7477c922..2afc9f9443d0d 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