Build: Improve C++ support

Can choose the C++ standard (C++98/11/14/17/2a)
Can link with standard C++ library (libstdc++)
Add support of C++ exceptions
Add support of C++ RTTI
Add C++ options to subsys/cpp/Kconfig
Implements new and delete using k_malloc and k_free
if CONFIG_HEAP_MEM_POOL_SIZE is defined

Signed-off-by: Benoit Leforestier <benoit.leforestier@gmail.com>
This commit is contained in:
Benoit Leforestier 2018-10-23 18:20:51 +02:00 committed by Anas Nashif
commit 26e0f9a9e1
7 changed files with 233 additions and 63 deletions

View file

@ -4,4 +4,5 @@ zephyr_sources_ifdef(CONFIG_CPLUSPLUS
cpp_init_array.c
cpp_ctors.c
cpp_dtors.c
cpp_new.cpp
)

70
subsys/cpp/Kconfig Normal file
View file

@ -0,0 +1,70 @@
# Kconfig - C++ configuration options
#
# Copyright (c) 2018 B. Leforestier
#
# SPDX-License-Identifier: Apache-2.0
#
menu "C++ Options"
config CPLUSPLUS
bool "Enable C++ support for the application"
help
This option enables the use of applications built with C++.
choice
prompt "C++ Standard"
default STD_CPP11
help
C++ Standards.
config STD_CPP98
bool "C++ 98"
help
1998 C++ standard as modified by the 2003 technical corrigendum
and some later defect reports.
config STD_CPP11
bool "C++ 11"
help
2011 C++ standard, previously known as C++0x.
config STD_CPP14
bool "C++ 14"
help
2014 C++ standard.
config STD_CPP17
bool "C++ 17"
help
2017 C++ standard, previously known as C++0x.
config STD_CPP2A
bool "C++ 2a"
help
Next revision of the C++ standard, which is expected to be published in 2020.
endchoice
config LIB_CPLUSPLUS
depends on CPLUSPLUS
bool "Link with STD C++ library"
help
Link with STD C++ Library.
config EXCEPTIONS
depends on CPLUSPLUS
select LIB_CPLUSPLUS
bool "Enable C++ exceptions support"
help
This option enables support of C++ exceptions.
config RTTI
depends on CPLUSPLUS
select LIB_CPLUSPLUS
bool "Enable C++ RTTI support"
help
This option enables support of C++ RTTI.
endmenu

118
subsys/cpp/cpp_new.cpp Normal file
View file

@ -0,0 +1,118 @@
/*
* Copyright (c) 2018
*
* SPDX-License-Identifier: Apache-2.0
*/
#if defined(CONFIG_LIB_CPLUSPLUS)
#include <new>
#endif // CONFIG_LIB_CPLUSPLUS
#include <kernel.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
}
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
}
void operator delete(void* ptr) noexcept
{
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
k_free(ptr);
#else
ARG_UNUSED(ptr);
#endif
}
void operator delete[](void* ptr) noexcept
{
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
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)
void operator delete(void* ptr, size_t) noexcept
{
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
k_free(ptr);
#else
ARG_UNUSED(ptr);
#endif
}
void operator delete[](void* ptr, size_t) noexcept
{
#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
k_free(ptr);
#else
ARG_UNUSED(ptr);
#endif
}
#endif // __cplusplus > 201103L