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
7 changed files with 11 additions and 104 deletions
|
@ -1 +1,2 @@
|
||||||
CONFIG_CPLUSPLUS=y
|
CONFIG_CPLUSPLUS=y
|
||||||
|
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=128
|
||||||
|
|
|
@ -6,7 +6,9 @@ zephyr_sources(
|
||||||
cpp_dtors.c
|
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(
|
zephyr_sources(
|
||||||
cpp_virtual.c
|
cpp_virtual.c
|
||||||
cpp_vtable.cpp
|
cpp_vtable.cpp
|
||||||
|
|
|
@ -63,13 +63,6 @@ config RTTI
|
||||||
help
|
help
|
||||||
This option enables support of C++ RTTI.
|
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 # LIB_CPLUSPLUS
|
||||||
|
|
||||||
endif # ! MINIMAL_LIBC
|
endif # ! MINIMAL_LIBC
|
||||||
|
|
|
@ -4,115 +4,36 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(CONFIG_LIB_CPLUSPLUS)
|
#include <stdlib.h>
|
||||||
#include <new>
|
|
||||||
#endif // CONFIG_LIB_CPLUSPLUS
|
|
||||||
#include <kernel.h>
|
|
||||||
|
|
||||||
void* operator new(size_t size)
|
void* operator new(size_t size)
|
||||||
{
|
{
|
||||||
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
|
return malloc(size);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* operator new[](size_t size)
|
void* operator new[](size_t size)
|
||||||
{
|
{
|
||||||
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
|
return malloc(size);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator delete(void* ptr) noexcept
|
void operator delete(void* ptr) noexcept
|
||||||
{
|
{
|
||||||
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
|
free(ptr);
|
||||||
k_free(ptr);
|
|
||||||
#else
|
|
||||||
ARG_UNUSED(ptr);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator delete[](void* ptr) noexcept
|
void operator delete[](void* ptr) noexcept
|
||||||
{
|
{
|
||||||
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
|
free(ptr);
|
||||||
k_free(ptr);
|
|
||||||
#else
|
|
||||||
ARG_UNUSED(ptr);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#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)
|
#if (__cplusplus > 201103L)
|
||||||
void operator delete(void* ptr, size_t) noexcept
|
void operator delete(void* ptr, size_t) noexcept
|
||||||
{
|
{
|
||||||
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
|
free(ptr);
|
||||||
k_free(ptr);
|
|
||||||
#else
|
|
||||||
ARG_UNUSED(ptr);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator delete[](void* ptr, size_t) noexcept
|
void operator delete[](void* ptr, size_t) noexcept
|
||||||
{
|
{
|
||||||
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
|
free(ptr);
|
||||||
k_free(ptr);
|
|
||||||
#else
|
|
||||||
ARG_UNUSED(ptr);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
#endif // __cplusplus > 201103L
|
#endif // __cplusplus > 201103L
|
||||||
|
|
|
@ -14,8 +14,6 @@
|
||||||
* @brief basic virtual tables required for classes to build
|
* @brief basic virtual tables required for classes to build
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#if !defined(CONFIG_LIB_CPLUSPLUS)
|
|
||||||
|
|
||||||
namespace __cxxabiv1 {
|
namespace __cxxabiv1 {
|
||||||
class __class_type_info {
|
class __class_type_info {
|
||||||
virtual void dummy();
|
virtual void dummy();
|
||||||
|
@ -26,4 +24,3 @@ namespace __cxxabiv1 {
|
||||||
void __class_type_info::dummy() { } // causes the vtable to get created here
|
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
|
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_CPLUSPLUS=y
|
||||||
CONFIG_LIB_CPLUSPLUS=y
|
CONFIG_LIB_CPLUSPLUS=y
|
||||||
CONFIG_STD_CPP17=y
|
CONFIG_STD_CPP17=y
|
||||||
CONFIG_HEAP_MEM_POOL_SIZE=1024
|
|
||||||
CONFIG_ZTEST=y
|
CONFIG_ZTEST=y
|
||||||
CONFIG_ZTEST_STACKSIZE=2048
|
CONFIG_ZTEST_STACKSIZE=2048
|
||||||
|
|
|
@ -4,12 +4,6 @@ tests:
|
||||||
platform_exclude: qemu_x86_coverage
|
platform_exclude: qemu_x86_coverage
|
||||||
min_flash: 54
|
min_flash: 54
|
||||||
tags: cpp
|
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:
|
misc.app_dev.libcxx.exceptions:
|
||||||
arch_exclude: posix
|
arch_exclude: posix
|
||||||
platform_exclude: qemu_x86_coverage
|
platform_exclude: qemu_x86_coverage
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue