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 <jan.van_winkel@dxplore.eu>
This commit is contained in:
parent
5a8b143028
commit
8a98a67bf1
|
@ -1 +1,2 @@
|
|||
CONFIG_CPLUSPLUS=y
|
||||
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=128
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -4,115 +4,36 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#if defined(CONFIG_LIB_CPLUSPLUS)
|
||||
#include <new>
|
||||
#endif // CONFIG_LIB_CPLUSPLUS
|
||||
#include <kernel.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue